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)) 125 buf = malloc (buflen);
126 printf (
"MEMIF DETAILS\n");
127 printf (
"==============================\n");
132 memset (&md, 0,
sizeof (md));
133 memset (buf, 0, buflen);
143 printf (
"interface index: %d\n", i);
145 printf (
"\tinterface ip: %u.%u.%u.%u\n",
147 printf (
"\tinterface name: %s\n", (
char *) md.
if_name);
148 printf (
"\tapp name: %s\n", (
char *) md.
inst_name);
149 printf (
"\tremote interface name: %s\n", (
char *) md.
remote_if_name);
151 printf (
"\tid: %u\n", md.
id);
152 printf (
"\tsecret: %s\n", (
char *) md.
secret);
162 printf (
"ethernet\n");
168 printf (
"punt/inject\n");
171 printf (
"unknown\n");
175 printf (
"\trx queues:\n");
182 printf (
"\t\tthread id: %u\n", thread_data[ti].
id);
183 printf (
"\t\tthread connection index: %u\n", thread_data[ti].index);
184 printf (
"\t\tthread running: ");
185 if (thread_data[ti].isRunning)
190 printf (
"\ttx queues:\n");
211 DBG (
"invalid fd %d", fd);
214 struct epoll_event evt;
215 memset (&evt, 0,
sizeof (evt));
218 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
220 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
223 DBG (
"fd %d added to epoll", fd);
232 DBG (
"invalid fd %d", fd);
235 struct epoll_event evt;
236 memset (&evt, 0,
sizeof (evt));
239 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
241 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
244 DBG (
"fd %d moddified on epoll", fd);
253 DBG (
"invalid fd %d", fd);
256 struct epoll_event evt;
257 memset (&evt, 0,
sizeof (evt));
258 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
260 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
263 DBG (
"fd %d removed from epoll", fd);
273 uint16_t rx = 0, tx = 0, fb = 0;
281 INFO (
"pthread id %u starts in polling mode", data->
id);
304 DBG (
"thread id: %u", data->
id);
306 DBG (
"received %d buffers. %u/%u alloc/free buffers",
319 DBG (
"allocated %d/%d buffers, %u free buffers",
323 for (i = 0; i < rx; i++)
327 (
void *) (data->
tx_bufs + i)->data,
337 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
353 INFO (
"thread %u error!", data->
id);
361 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
366 INFO (
"pthread id %u exit", data->
id);
376 uint16_t rx = 0, tx = 0, fb = 0;
377 struct epoll_event evt;
389 INFO (
"pthread id %u starts in interrupt mode", data->
id);
390 int thread_epfd = epoll_create (1);
404 memset (&evt, 0,
sizeof (evt));
405 evt.events = EPOLLIN | EPOLLOUT;
406 sigemptyset (&sigset);
407 en = epoll_pwait (thread_epfd, &evt, 1, -1, &sigset);
412 DBG (
"epoll_pwait: %s", strerror (errno));
433 DBG (
"thread id: %u", data->
id);
435 DBG (
"received %d buffers. %u/%u alloc/free buffers",
448 DBG (
"allocated %d/%d buffers, %u free buffers",
452 for (i = 0; i < rx; i++)
456 (
void *) (data->
tx_bufs + i)->data,
466 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
483 INFO (
"thread %u error!", data->
id);
491 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
496 INFO (
"pthread id %u exit", data->
id);
506 long index = (*(
long *) private_ctx);
508 INFO (
"memif connected! index %ld", index);
520 if (thread_data[ti].isRunning)
522 INFO (
"thread id: %d already running!", ti);
525 thread_data[ti].
index = index;
526 thread_data[ti].
qid =
i;
527 thread_data[ti].
id = ti;
529 pthread_create (&
thread[ti],
532 pthread_create (&
thread[ti],
534 (
void *) &thread_data[ti]);
547 long index = (*(
long *) private_ctx);
550 INFO (
"memif disconnected!");
556 if (!thread_data[ti].isRunning)
559 pthread_kill (
thread[ti], SIGUSR1);
560 pthread_join (
thread[ti], &ptr);
591 INFO (
"connection array overflow");
596 INFO (
"don't even try...");
603 memset (&args, 0,
sizeof (args));
641 INFO (
"connection array overflow");
646 INFO (
"don't even try...");
662 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
667 printf (
"==============================\n");
674 printf (
"commands:\n");
675 printf (
"\thelp - prints this help\n");
676 printf (
"\texit - exit app\n");
677 printf (
"\tconn <index> - create memif (slave-mode)\n");
678 printf (
"\tdel <index> - delete memif\n");
679 printf (
"\tshow - show connection details\n");
680 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
708 INFO (
"connection array overflow");
713 INFO (
"don't even try...");
719 INFO (
"no connection at index %ld", index);
726 ui = strtok (ip,
".");
729 tmp[0] = strtol (ui, &end, 10);
731 ui = strtok (
NULL,
".");
734 tmp[1] = strtol (ui, &end, 10);
736 ui = strtok (
NULL,
".");
739 tmp[2] = strtol (ui, &end, 10);
741 ui = strtok (
NULL,
".");
744 tmp[3] = strtol (ui, &end, 10);
751 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
757 INFO (
"invalid ip address");
765 char *in = (
char *) malloc (256);
766 char *ui = fgets (in, 256, stdin);
770 ui = strtok (in,
" ");
771 if (strncmp (ui,
"exit", 4) == 0)
777 else if (strncmp (ui,
"help", 4) == 0)
782 else if (strncmp (ui,
"conn", 4) == 0)
784 ui = strtok (
NULL,
" ");
788 INFO (
"expected id");
791 else if (strncmp (ui,
"del", 3) == 0)
793 ui = strtok (
NULL,
" ");
797 INFO (
"expected id");
800 else if (strncmp (ui,
"show", 4) == 0)
805 else if (strncmp (ui,
"ip-set", 6) == 0)
807 ui = strtok (
NULL,
" ");
811 INFO (
"expected id");
816 DBG (
"unknown command: %s", ui);
829 struct epoll_event evt;
830 int app_err = 0, memif_err = 0, en = 0;
832 memset (&evt, 0,
sizeof (evt));
833 evt.events = EPOLLIN | EPOLLOUT;
835 sigemptyset (&sigset);
836 en = epoll_pwait (
main_epfd, &evt, 1, timeout, &sigset);
839 DBG (
"epoll_pwait: %s", strerror (errno));
849 if (evt.events & EPOLLIN)
851 if (evt.events & EPOLLOUT)
853 if (evt.events & EPOLLERR)
859 else if (evt.data.fd == 0)
865 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
869 if ((app_err < 0) || (memif_err < 0))
872 DBG (
"user input handler error");
874 DBG (
"memif control fd handler error");
911 DBG (
"poll_event error!");
int add_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 ...
Optimized string handling code, including c11-compliant "safe C library" variants.
void user_signal_handler(int sig)
static void print_memif_details()
int icmpr_set_ip(long index, char *ip)
uint8_t * remote_inst_name
int poll_event(int timeout)
#define LIBMEMIF_VERSION
Libmemif version.
int memif_refill_queue(memif_conn_handle_t conn, uint16_t qid, uint16_t count, uint16_t headroom)
Memif refill ring.
int on_connect(memif_conn_handle_t conn, void *private_ctx)
int control_fd_update(int fd, uint8_t events, void *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
char * memif_strerror(int err_code)
Memif strerror.
int icmpr_memif_create(int is_master)
pthread_t thread[MAX_CONNS]
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_init(memif_control_fd_update_t *on_control_fd_update, char *app_name, memif_alloc_t *memif_alloc, memif_realloc_t *memif_realloc, memif_free_t *memif_free)
Memif initialization.
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.
memif_queue_details_t * rx_queues
void * memif_rx_poll(void *ptr)
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)
void * memif_conn_handle_t
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 mod_epoll_fd(int fd, uint32_t events)
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
f64 end
end of the time range
#define MEMIF_FD_EVENT_WRITE
uint16_t memif_get_version()
Memif get version.
Memif connection arguments.