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> 50 #define APP_NAME "ICMP_Responder" 51 #define IF_NAME "memif_connection" 55 #define DBG(...) do { \ 56 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ 57 printf (__VA_ARGS__); \ 64 #define INFO(...) do { \ 65 printf ("INFO: "__VA_ARGS__); \ 70 #define MAX_MEMIF_BUFS 256 106 buf = malloc (buflen);
107 printf (
"MEMIF DETAILS\n");
108 printf (
"==============================\n");
113 memset (&md, 0,
sizeof (md));
114 memset (buf, 0, buflen);
124 printf (
"interface index: %d\n", i);
126 printf (
"\tinterface ip: %u.%u.%u.%u\n",
128 printf (
"\tinterface name: %s\n", (
char *) md.
if_name);
129 printf (
"\tapp name: %s\n", (
char *) md.
inst_name);
130 printf (
"\tremote interface name: %s\n", (
char *) md.
remote_if_name);
132 printf (
"\tid: %u\n", md.
id);
133 printf (
"\tsecret: %s\n", (
char *) md.
secret);
143 printf (
"ethernet\n");
149 printf (
"punt/inject\n");
152 printf (
"unknown\n");
156 printf (
"\trx queues:\n");
163 printf (
"\ttx queues:\n");
184 DBG (
"invalid fd %d", fd);
187 struct epoll_event evt;
188 memset (&evt, 0,
sizeof (evt));
191 if (epoll_ctl (
epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
193 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
196 DBG (
"fd %d added to epoll", fd);
205 DBG (
"invalid fd %d", fd);
208 struct epoll_event evt;
209 memset (&evt, 0,
sizeof (evt));
212 if (epoll_ctl (
epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
214 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
217 DBG (
"fd %d moddified on epoll", fd);
226 DBG (
"invalid fd %d", fd);
229 struct epoll_event evt;
230 memset (&evt, 0,
sizeof (evt));
231 if (epoll_ctl (
epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
233 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
236 DBG (
"fd %d removed from epoll", fd);
245 INFO (
"memif connected!");
254 INFO (
"memif disconnected!");
294 DBG (
"allocated %d/%ld buffers, %u free buffers", r, n,
320 long index = *((
long *) private_ctx);
322 if (c->
index != index)
324 INFO (
"invalid context: %ld/%u", index, c->
index);
340 DBG (
"received %d buffers. %u/%u alloc/free buffers",
345 INFO (
"buffer_alloc error");
349 for (i = 0; i < rx; i++)
353 (
void *) (c->
tx_bufs + i)->data,
363 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
375 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
385 INFO (
"connection array overflow");
390 INFO (
"don't even try...");
398 memset (&args, 0,
sizeof (args));
444 INFO (
"connection array overflow");
449 INFO (
"don't even try...");
469 INFO (
"memif delete fail");
476 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
481 printf (
"==============================\n");
488 printf (
"commands:\n");
489 printf (
"\thelp - prints this help\n");
490 printf (
"\texit - exit app\n");
492 (
"\tconn <index> <mode> - create memif. index is also used as interface id, mode 0 = slave 1 = master\n");
493 printf (
"\tdel <index> - delete memif\n");
494 printf (
"\tshow - show connection details\n");
495 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
497 (
"\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
525 INFO (
"connection array overflow");
530 INFO (
"don't even try...");
536 INFO (
"no connection at index %ld", index);
543 ui = strtok (ip,
".");
546 tmp[0] = strtol (ui, &end, 10);
548 ui = strtok (
NULL,
".");
551 tmp[1] = strtol (ui, &end, 10);
553 ui = strtok (
NULL,
".");
556 tmp[2] = strtol (ui, &end, 10);
558 ui = strtok (
NULL,
".");
561 tmp[3] = strtol (ui, &end, 10);
568 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
574 INFO (
"invalid ip address");
583 INFO (
"connection array overflow");
588 INFO (
"don't even try...");
595 INFO (
"no connection at index %ld", index);
599 if (strncmp (mode,
"interrupt", 9) == 0)
604 else if (strncmp (mode,
"polling", 7) == 0)
609 INFO (
"expected rx mode <interrupt|polling>");
617 char *in = (
char *) malloc (256);
618 char *ui = fgets (in, 256, stdin);
623 ui = strtok (in,
" ");
624 if (strncmp (ui,
"exit", 4) == 0)
630 else if (strncmp (ui,
"help", 4) == 0)
635 else if (strncmp (ui,
"conn", 4) == 0)
637 ui = strtok (
NULL,
" ");
639 a = strtol (ui, &end, 10);
642 INFO (
"expected id");
645 ui = strtok (
NULL,
" ");
649 INFO (
"expected mode <0|1>");
652 else if (strncmp (ui,
"del", 3) == 0)
654 ui = strtok (
NULL,
" ");
658 INFO (
"expected id");
661 else if (strncmp (ui,
"show", 4) == 0)
666 else if (strncmp (ui,
"ip-set", 6) == 0)
668 ui = strtok (
NULL,
" ");
672 INFO (
"expected id");
675 else if (strncmp (ui,
"rx-mode", 7) == 0)
677 ui = strtok (
NULL,
" ");
679 a = strtol (ui, &end, 10);
682 INFO (
"expected id");
685 ui = strtok (
NULL,
" ");
689 INFO (
"expected qid");
694 DBG (
"unknown command: %s", ui);
707 struct epoll_event evt, *e;
708 int app_err = 0, memif_err = 0, en = 0;
711 memset (&evt, 0,
sizeof (evt));
712 evt.events = EPOLLIN | EPOLLOUT;
714 sigemptyset (&sigset);
715 en = epoll_pwait (
epfd, &evt, 1, timeout, &sigset);
718 DBG (
"epoll_pwait: %s", strerror (errno));
728 if (evt.events & EPOLLIN)
730 if (evt.events & EPOLLOUT)
732 if (evt.events & EPOLLERR)
738 else if (evt.data.fd == 0)
744 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
748 if ((app_err < 0) || (memif_err < 0))
751 DBG (
"user input handler error");
753 DBG (
"memif control fd handler error");
763 epfd = epoll_create (1);
788 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)
memif_interface_id_t interface_id
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.
int icmpr_set_rx_mode(long index, long qid, char *mode)
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 icmpr_tx_burst(uint16_t qid)
int on_interrupt(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
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
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.
int poll_event(int timeout)
void * memif_conn_handle_t
*brief Memif connection handle pointer of type void, pointing to internal structure ...
int icmpr_buffer_alloc(long n, uint16_t qid)
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.
static void print_memif_details()
#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.