21 #include <sys/types.h> 23 #include <sys/ioctl.h> 24 #include <sys/socket.h> 28 #include <sys/prctl.h> 34 #include <linux/icmp.h> 35 #include <arpa/inet.h> 37 #include <netinet/if_ether.h> 38 #include <net/if_arp.h> 39 #include <asm/byteorder.h> 42 #include <sys/epoll.h> 51 #define APP_NAME "ICMP_Responder" 52 #define IF_NAME "memif_connection" 56 #define DBG(...) do { \ 57 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ 58 printf (__VA_ARGS__); \ 65 #define INFO(...) do { \ 66 printf ("INFO: "__VA_ARGS__); \ 71 #define MAX_MEMIF_BUFS 256 74 #define MAX_THREADS ((MAX_CONNS) * (MAX_QUEUES)) 126 buf = malloc (buflen);
127 printf (
"MEMIF DETAILS\n");
128 printf (
"==============================\n");
133 memset (&md, 0,
sizeof (md));
134 memset (buf, 0, buflen);
144 printf (
"interface index: %d\n", i);
146 printf (
"\tinterface ip: %u.%u.%u.%u\n",
148 printf (
"\tinterface name: %s\n", (
char *) md.
if_name);
149 printf (
"\tapp name: %s\n", (
char *) md.
inst_name);
150 printf (
"\tremote interface name: %s\n", (
char *) md.
remote_if_name);
152 printf (
"\tid: %u\n", md.
id);
153 printf (
"\tsecret: %s\n", (
char *) md.
secret);
163 printf (
"ethernet\n");
169 printf (
"punt/inject\n");
172 printf (
"unknown\n");
176 printf (
"\trx queues:\n");
183 printf (
"\t\tthread id: %u\n", thread_data[ti].
id);
184 printf (
"\t\tthread connection index: %u\n", thread_data[ti].index);
185 printf (
"\t\tthread running: ");
186 if (thread_data[ti].isRunning)
191 printf (
"\ttx queues:\n");
212 DBG (
"invalid fd %d", fd);
215 struct epoll_event evt;
216 memset (&evt, 0,
sizeof (evt));
219 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
221 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
224 DBG (
"fd %d added to epoll", fd);
233 DBG (
"invalid fd %d", fd);
236 struct epoll_event evt;
237 memset (&evt, 0,
sizeof (evt));
240 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
242 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
245 DBG (
"fd %d moddified on epoll", fd);
254 DBG (
"invalid fd %d", fd);
257 struct epoll_event evt;
258 memset (&evt, 0,
sizeof (evt));
259 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
261 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
264 DBG (
"fd %d removed from epoll", fd);
274 uint16_t rx = 0, tx = 0, fb = 0;
282 INFO (
"pthread id %u starts in polling mode", data->
id);
305 DBG (
"thread id: %u", data->
id);
307 DBG (
"received %d buffers. %u/%u alloc/free buffers",
320 DBG (
"allocated %d/%d buffers, %u free buffers",
324 for (i = 0; i < rx; i++)
328 (
void *) (data->
tx_bufs + i)->data,
338 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
354 INFO (
"thread %u error!", data->
id);
362 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
367 INFO (
"pthread id %u exit", data->
id);
377 uint16_t rx = 0, tx = 0, fb = 0;
378 struct epoll_event evt, *e;
391 INFO (
"pthread id %u starts in interrupt mode", data->
id);
392 int thread_epfd = epoll_create (1);
406 memset (&evt, 0,
sizeof (evt));
407 evt.events = EPOLLIN | EPOLLOUT;
408 sigemptyset (&sigset);
409 en = epoll_pwait (thread_epfd, &evt, 1, -1, &sigset);
414 DBG (
"epoll_pwait: %s", strerror (errno));
435 DBG (
"thread id: %u", data->
id);
437 DBG (
"received %d buffers. %u/%u alloc/free buffers",
450 DBG (
"allocated %d/%d buffers, %u free buffers",
454 for (i = 0; i < rx; i++)
458 (
void *) (data->
tx_bufs + i)->data,
469 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
486 INFO (
"thread %u error!", data->
id);
494 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
499 INFO (
"pthread id %u exit", data->
id);
509 long index = (*(
long *) private_ctx);
511 INFO (
"memif connected! index %ld", index);
523 if (thread_data[ti].isRunning)
525 INFO (
"thread id: %d already running!", ti);
528 thread_data[ti].
index = index;
529 thread_data[ti].
qid =
i;
530 thread_data[ti].
id = ti;
532 pthread_create (&
thread[ti],
535 pthread_create (&
thread[ti],
537 (
void *) &thread_data[ti]);
550 long index = (*(
long *) private_ctx);
553 INFO (
"memif disconnected!");
559 if (!thread_data[ti].isRunning)
562 pthread_kill (
thread[ti], SIGUSR1);
563 pthread_join (
thread[ti], &ptr);
594 INFO (
"connection array overflow");
599 INFO (
"don't even try...");
607 memset (&args, 0,
sizeof (args));
646 INFO (
"connection array overflow");
651 INFO (
"don't even try...");
667 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
672 printf (
"==============================\n");
679 printf (
"commands:\n");
680 printf (
"\thelp - prints this help\n");
681 printf (
"\texit - exit app\n");
682 printf (
"\tconn <index> - create memif (slave-mode)\n");
683 printf (
"\tdel <index> - delete memif\n");
684 printf (
"\tshow - show connection details\n");
685 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
713 INFO (
"connection array overflow");
718 INFO (
"don't even try...");
724 INFO (
"no connection at index %ld", index);
731 ui = strtok (ip,
".");
734 tmp[0] = strtol (ui, &end, 10);
736 ui = strtok (
NULL,
".");
739 tmp[1] = strtol (ui, &end, 10);
741 ui = strtok (
NULL,
".");
744 tmp[2] = strtol (ui, &end, 10);
746 ui = strtok (
NULL,
".");
749 tmp[3] = strtol (ui, &end, 10);
756 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
762 INFO (
"invalid ip address");
771 char *in = (
char *) malloc (256);
772 char *ui = fgets (in, 256, stdin);
776 ui = strtok (in,
" ");
777 if (strncmp (ui,
"exit", 4) == 0)
783 else if (strncmp (ui,
"help", 4) == 0)
788 else if (strncmp (ui,
"conn", 4) == 0)
790 ui = strtok (
NULL,
" ");
794 INFO (
"expected id");
797 else if (strncmp (ui,
"del", 3) == 0)
799 ui = strtok (
NULL,
" ");
803 INFO (
"expected id");
806 else if (strncmp (ui,
"show", 4) == 0)
811 else if (strncmp (ui,
"ip-set", 6) == 0)
813 ui = strtok (
NULL,
" ");
817 INFO (
"expected id");
822 DBG (
"unknown command: %s", ui);
835 struct epoll_event evt, *e;
836 int app_err = 0, memif_err = 0, en = 0;
839 memset (&evt, 0,
sizeof (evt));
840 evt.events = EPOLLIN | EPOLLOUT;
842 sigemptyset (&sigset);
843 en = epoll_pwait (
main_epfd, &evt, 1, timeout, &sigset);
846 DBG (
"epoll_pwait: %s", strerror (errno));
856 if (evt.events & EPOLLIN)
858 if (evt.events & EPOLLOUT)
860 if (evt.events & EPOLLERR)
866 else if (evt.data.fd == 0)
872 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
876 if ((app_err < 0) || (memif_err < 0))
879 DBG (
"user input handler error");
881 DBG (
"memif control fd handler error");
918 DBG (
"poll_event error!");
sll srl srl sll sra u16x4 i
memif_log2_ring_size_t log2_ring_size
int mod_epoll_fd(int fd, uint32_t events)
int on_disconnect(memif_conn_handle_t conn, void *private_ctx)
#define MEMIF_FD_EVENT_READ
user needs to set events that occured on fd and pass them to memif_control_fd_handler ...
int control_fd_update(int fd, uint8_t events)
void user_signal_handler(int sig)
memif_interface_id_t interface_id
pthread_t thread[MAX_THREADS]
static void print_memif_details()
int icmpr_set_ip(long index, char *ip)
uint8_t * remote_inst_name
#define LIBMEMIF_VERSION
Libmemif version.
int on_connect(memif_conn_handle_t conn, void *private_ctx)
int memif_get_details(memif_conn_handle_t conn, memif_details_t *md, char *buf, ssize_t buflen)
Memif get details.
memif_interface_mode_t mode
int memif_buffer_free(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *count_out)
Memif buffer free.
char * memif_strerror(int err_code)
Memif strerror.
int icmpr_memif_create(int is_master)
uint8_t interface_name[32]
int resolve_packet(void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size, uint8_t ip_addr[4])
uint8_t * socket_filename
#define MEMIF_FD_EVENT_DEL
if set, informs that fd is going to be closed (user may want to stop watching for events on this fd) ...
int memif_set_rx_mode(memif_conn_handle_t c, memif_rx_mode_t rx_mode, uint16_t qid)
Memif set rx mode.
struct memif_connection memif_connection_t
int memif_get_queue_efd(memif_conn_handle_t conn, uint16_t qid, int *efd)
Memif get queue event file descriptor
int memif_tx_burst(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *tx)
Memif transmit buffer burst.
int memif_buffer_alloc(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *count_out, uint16_t size)
Memif buffer alloc.
int add_epoll_fd(int fd, uint32_t events)
memif_queue_details_t * rx_queues
void * memif_rx_poll(void *ptr)
uint8_t instance_name[32]
int memif_cleanup()
Memif cleanup.
int memif_create(memif_conn_handle_t *c, memif_conn_args_t *args, memif_connection_update_t *on_connect, memif_connection_update_t *on_disconnect, memif_interrupt_t *on_interrupt, void *private_ctx)
Memory interface create function.
void * memif_rx_interrupt(void *ptr)
int poll_event(int timeout)
void * memif_conn_handle_t
*brief Memif connection handle pointer of type void, pointing to internal structure ...
int memif_control_fd_handler(int fd, uint8_t events)
Memif control file descriptor handler.
int memif_init(memif_control_fd_update_t *on_control_fd_update, char *app_name)
Memif initialization.
int memif_rx_burst(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *rx)
Memif receive buffer burst.
int memif_delete(memif_conn_handle_t *conn)
Memif delete.
#define MEMIF_FD_EVENT_ERROR
inform libmemif that error occured on fd
#define MEMIF_FD_EVENT_MOD
update events
int main(int argc, char *argv[])
memif_queue_details_t * tx_queues
#define MEMIF_FD_EVENT_WRITE
Memif connection arguments.