1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
#include "kernel.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <glib.h>
#include <errno.h>
#include <sys/mman.h>
#include "helpers.h"
#include "log.h"
#include "bufferpool.h"
#include "main.h"
#include "statistics.h"
#include "xt_RTPENGINE.h"
#define PREFIX "/proc/rtpengine"
struct kernel_interface kernel;
static bool kernel_action_table(const char *action, unsigned int id) {
char s[64];
int saved_errno;
int fd;
int i;
ssize_t ret;
fd = open(PREFIX "/control", O_WRONLY | O_TRUNC);
if (fd == -1)
return false;
i = snprintf(s, sizeof(s), "%s %u\n", action, id);
if (i >= sizeof(s))
goto fail;
ret = write(fd, s, strlen(s));
if (ret == -1)
goto fail;
close(fd);
return true;
fail:
saved_errno = errno;
close(fd);
errno = saved_errno;
return false;
}
static bool kernel_create_table(unsigned int id) {
return kernel_action_table("add", id);
}
static bool kernel_delete_table(unsigned int id) {
return kernel_action_table("del", id);
}
static void *kernel_alloc(void) {
// Since we can't really request memory at a specific location that we know
// will be correctly aligned, request twice as much, which we know must be
// enough to contain at least one correctly aligned block. This may seem like
// a waste, but the extra pages won't ever be used, and so usually won't even
// be mapped.
void *b = mmap(NULL, BUFFERPOOL_SHARD_SIZE * 2, PROT_READ | PROT_WRITE, MAP_SHARED, kernel.fd, 0);
if (b == NULL || b == MAP_FAILED) {
ilog(LOG_CRIT, "Failed to allocate shared kernel memory: %s", strerror(errno));
abort();
}
// find the aligned block
void *aligned = (void *) (((intptr_t) b + BUFFERPOOL_SHARD_SIZE - 1) & BUFFERPOOL_TOP_MASK);
// place a pointer to the real beginning of the block just past the end, so we
// know what to free
void **back_ptr = aligned + BUFFERPOOL_SHARD_SIZE;
// make sure there is enough extra space to store our back pointer (there should be, unless
// our page size is really tiny)
assert((void *) back_ptr + sizeof(void *) < b + BUFFERPOOL_SHARD_SIZE * 2);
*back_ptr = b;
return aligned;
}
static void kernel_free(void *p) {
// restore saved pointer to read beginning of the block
void **back_ptr = p + BUFFERPOOL_SHARD_SIZE;
p = *back_ptr;
munmap(p, BUFFERPOOL_SHARD_SIZE * 2);
}
static int kernel_open_table(unsigned int id) {
char s[64];
int fd;
sprintf(s, PREFIX "/%u/control", id);
fd = open(s, O_RDWR | O_TRUNC);
if (fd == -1)
return -1;
return fd;
}
bool kernel_init_table(void) {
if (!kernel.is_open)
return true;
ssize_t ret;
struct rtpengine_command_init cmd = {
.cmd = REMG_INIT,
.init = {
.last_cmd = __REMG_LAST,
.msg_size = {
[REMG_INIT] = sizeof(struct rtpengine_command_init),
[REMG_ADD_TARGET] = sizeof(struct rtpengine_command_add_target),
[REMG_DEL_TARGET] = sizeof(struct rtpengine_command_del_target),
[REMG_ADD_DESTINATION] = sizeof(struct rtpengine_command_destination),
[REMG_ADD_CALL] = sizeof(struct rtpengine_command_add_call),
[REMG_DEL_CALL] = sizeof(struct rtpengine_command_del_call),
[REMG_ADD_STREAM] = sizeof(struct rtpengine_command_add_stream),
[REMG_DEL_STREAM] = sizeof(struct rtpengine_command_del_stream),
[REMG_PACKET] = sizeof(struct rtpengine_command_packet),
[REMG_INIT_PLAY_STREAMS] = sizeof(struct rtpengine_command_init_play_streams),
[REMG_GET_PACKET_STREAM] = sizeof(struct rtpengine_command_get_packet_stream),
[REMG_PLAY_STREAM_PACKET] = sizeof(struct rtpengine_command_play_stream_packet),
[REMG_PLAY_STREAM] = sizeof(struct rtpengine_command_play_stream),
[REMG_STOP_STREAM] = sizeof(struct rtpengine_command_stop_stream),
[REMG_FREE_PACKET_STREAM] = sizeof(struct rtpengine_command_free_packet_stream),
},
.rtpe_stats = rtpe_stats,
},
};
ret = write(kernel.fd, &cmd, sizeof(cmd));
if (ret <= 0)
return false;
return true;
}
bool kernel_setup_table(unsigned int id) {
if (kernel.is_wanted)
abort();
kernel.is_wanted = true;
if (!kernel_delete_table(id) && errno != ENOENT) {
ilog(LOG_ERR, "FAILED TO DELETE KERNEL TABLE %i (%s), KERNEL FORWARDING DISABLED",
id, strerror(errno));
return false;
}
if (!kernel_create_table(id)) {
ilog(LOG_ERR, "FAILED TO CREATE KERNEL TABLE %i (%s), KERNEL FORWARDING DISABLED",
id, strerror(errno));
return false;
}
int fd = kernel_open_table(id);
if (fd == -1) {
ilog(LOG_ERR, "FAILED TO OPEN KERNEL TABLE %i (%s), KERNEL FORWARDING DISABLED",
id, strerror(errno));
return false;
}
kernel.fd = fd;
kernel.table = id;
kernel.is_open = true;
shm_bufferpool = bufferpool_new(kernel_alloc, kernel_free);
return true;
}
void kernel_shutdown_table(void) {
if (!kernel.is_open)
return;
// ignore errors
close(kernel.fd);
kernel_delete_table(kernel.table);
}
void kernel_add_stream(struct rtpengine_target_info *mti) {
ssize_t ret;
if (!kernel.is_open)
return;
struct rtpengine_command_add_target cmd = {
.cmd = REMG_ADD_TARGET,
.target = *mti,
};
ret = write(kernel.fd, &cmd, sizeof(cmd));
if (ret == sizeof(cmd))
return;
ilog(LOG_ERROR, "Failed to push relay stream to kernel: %s", strerror(errno));
}
void kernel_add_destination(struct rtpengine_destination_info *mdi) {
ssize_t ret;
if (!kernel.is_open)
return;
struct rtpengine_command_destination cmd = {
.cmd = REMG_ADD_DESTINATION,
.destination = *mdi,
};
ret = write(kernel.fd, &cmd, sizeof(cmd));
if (ret == sizeof(cmd))
return;
ilog(LOG_ERROR, "Failed to push relay stream destination to kernel: %s", strerror(errno));
}
bool kernel_del_stream(struct rtpengine_command_del_target *cmd) {
ssize_t ret;
if (!kernel.is_open)
return false;
cmd->cmd = REMG_DEL_TARGET;
ret = write(kernel.fd, cmd, sizeof(*cmd));
if (ret == sizeof(*cmd))
return true;
ilog(LOG_ERROR, "Failed to delete relay stream from kernel: %s", strerror(errno));
return false;
}
unsigned int kernel_add_call(const char *id) {
ssize_t ret;
if (!kernel.is_open)
return UNINIT_IDX;
struct rtpengine_command_add_call cmd = {
.cmd = REMG_ADD_CALL,
};
snprintf(cmd.call.call_id, sizeof(cmd.call.call_id), "%s", id);
ret = read(kernel.fd, &cmd, sizeof(cmd));
if (ret != sizeof(cmd))
return UNINIT_IDX;
return cmd.call.call_idx;
}
void kernel_del_call(unsigned int idx) {
ssize_t ret;
if (!kernel.is_open)
return;
struct rtpengine_command_del_call cmd = {
.cmd = REMG_DEL_CALL,
.call_idx = idx,
};
ret = write(kernel.fd, &cmd, sizeof(cmd));
if (ret == sizeof(cmd))
return;
ilog(LOG_ERROR, "Failed to delete intercept call from kernel: %s", strerror(errno));
}
unsigned int kernel_add_intercept_stream(unsigned int call_idx, const char *id) {
ssize_t ret;
if (!kernel.is_open)
return UNINIT_IDX;
struct rtpengine_command_add_stream cmd = {
.cmd = REMG_ADD_STREAM,
.stream.idx.call_idx = call_idx,
};
snprintf(cmd.stream.stream_name, sizeof(cmd.stream.stream_name), "%s", id);
ret = read(kernel.fd, &cmd, sizeof(cmd));
if (ret != sizeof(cmd))
return UNINIT_IDX;
return cmd.stream.idx.stream_idx;
}
bool kernel_init_player(int num_media, int num_sessions) {
if (num_media <= 0 || num_sessions <= 0)
return false;
if (!kernel.is_open)
return false;
struct rtpengine_command_init_play_streams ips = {
.cmd = REMG_INIT_PLAY_STREAMS,
.num_packet_streams = num_media,
.num_play_streams = num_sessions,
};
ssize_t ret = write(kernel.fd, &ips, sizeof(ips));
if (ret != sizeof(ips))
return false;
kernel.use_player = true;
return true;
}
unsigned int kernel_get_packet_stream(void) {
if (!kernel.use_player)
return -1;
struct rtpengine_command_get_packet_stream gps = { .cmd = REMG_GET_PACKET_STREAM };
ssize_t ret = read(kernel.fd, &gps, sizeof(gps));
if (ret != sizeof(gps))
return -1;
return gps.packet_stream_idx;
}
bool kernel_add_stream_packet(unsigned int idx, const char *buf, size_t len, unsigned long delay_ms,
uint32_t ts, uint32_t dur)
{
if (!kernel.use_player)
return false;
size_t total_len = len + sizeof(struct rtpengine_command_play_stream_packet);
struct rtpengine_command_play_stream_packet *cmd = alloca(total_len);
*cmd = (__typeof__(*cmd)) {
.cmd = REMG_PLAY_STREAM_PACKET,
.play_stream_packet.packet_stream_idx = idx,
.play_stream_packet.delay_ms = delay_ms,
.play_stream_packet.delay_ts = ts,
.play_stream_packet.duration_ts = dur,
};
memcpy(&cmd->play_stream_packet.data, buf, len);
ssize_t ret = write(kernel.fd, cmd, total_len);
if (ret != total_len)
return false;
return true;
}
unsigned int kernel_start_stream_player(struct rtpengine_play_stream_info *info) {
if (!kernel.use_player)
return -1;
struct rtpengine_command_play_stream ps = {
.cmd = REMG_PLAY_STREAM,
.info = *info,
};
ssize_t ret = read(kernel.fd, &ps, sizeof(ps));
if (ret == sizeof(ps))
return ps.play_idx;
return -1;
}
bool kernel_stop_stream_player(unsigned int idx) {
if (!kernel.use_player)
return false;
struct rtpengine_command_stop_stream ss = {
.cmd = REMG_STOP_STREAM,
.play_idx = idx,
};
ssize_t ret = write(kernel.fd, &ss, sizeof(ss));
if (ret == sizeof(ss))
return true;
return false;
}
bool kernel_free_packet_stream(unsigned int idx) {
if (!kernel.use_player)
return false;
struct rtpengine_command_free_packet_stream fps = {
.cmd = REMG_FREE_PACKET_STREAM,
.packet_stream_idx = idx,
};
ssize_t ret = write(kernel.fd, &fps, sizeof(fps));
if (ret == sizeof(fps))
return true;
return false;
}
|