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> 52 #define APP_NAME "ICMP_Responder" 53 #define IF_NAME "memif_connection" 59 #define DBG(...) do { \ 60 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ 61 printf (__VA_ARGS__); \ 64 #define LOG(...) do { \ 66 dprintf (out_fd, __VA_ARGS__); \ 67 dprintf (out_fd, "\n"); \ 70 #define LOG_FILE "/tmp/memif_time_test.txt" 76 #define INFO(...) do { \ 77 printf ("INFO: "__VA_ARGS__); \ 83 #define MAX_MEMIF_BUFS 256 105 uint64_t tx_counter, rx_counter, tx_err_counter;
106 uint64_t t_sec, t_nsec;
134 buf = malloc (buflen);
135 printf (
"MEMIF DETAILS\n");
136 printf (
"==============================\n");
141 memset (&md, 0,
sizeof (md));
142 memset (buf, 0, buflen);
152 printf (
"interface index: %d\n", i);
154 printf (
"\tinterface ip: %u.%u.%u.%u\n",
156 printf (
"\tinterface name: %s\n", (
char *) md.
if_name);
157 printf (
"\tapp name: %s\n", (
char *) md.
inst_name);
158 printf (
"\tremote interface name: %s\n", (
char *) md.
remote_if_name);
160 printf (
"\tid: %u\n", md.
id);
161 printf (
"\tsecret: %s\n", (
char *) md.
secret);
171 printf (
"ethernet\n");
177 printf (
"punt/inject\n");
180 printf (
"unknown\n");
184 printf (
"\trx queues:\n");
189 printf (
"\t\tring rx mode: %s\n",
195 printf (
"\ttx queues:\n");
200 printf (
"\t\tring rx mode: %s\n",
220 DBG (
"invalid fd %d", fd);
223 struct epoll_event evt;
224 memset (&evt, 0,
sizeof (evt));
227 if (epoll_ctl (
epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
229 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
232 DBG (
"fd %d added to epoll", fd);
241 DBG (
"invalid fd %d", fd);
244 struct epoll_event evt;
245 memset (&evt, 0,
sizeof (evt));
248 if (epoll_ctl (
epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
250 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
253 DBG (
"fd %d moddified on epoll", fd);
262 DBG (
"invalid fd %d", fd);
265 struct epoll_event evt;
266 memset (&evt, 0,
sizeof (evt));
267 if (epoll_ctl (
epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
269 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
272 DBG (
"fd %d removed from epoll", fd);
281 INFO (
"memif connected!");
292 INFO (
"memif disconnected!");
323 long index = *((
long *) private_ctx);
325 if (c->
index != index)
327 INFO (
"invalid context: %ld/%u", index, c->
index);
332 uint16_t rx = 0, tx = 0;
352 for (i = 0; i < rx; i++)
375 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
397 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
406 long index = *((
long *) private_ctx);
408 if (c->
index != index)
410 INFO (
"invalid context: %ld/%u", index, c->
index);
415 uint16_t rx = 0, tx = 0;
435 for (i = 0; i < rx; i++)
456 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
478 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
489 long index = *((
long *) private_ctx);
491 if (c->
index != index)
493 INFO (
"invalid context: %ld/%u", index, c->
index);
516 for (i = 0; i < rx; i++)
518 if (((
struct ether_header *) (c->
bufs + i)->data)->ether_type ==
546 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
556 INFO (
"connection array overflow");
561 INFO (
"don't even try...");
569 memset (&args, 0,
sizeof (args));
599 if (strncmp (s,
"0", 1) == 0)
610 else if (strncmp (s,
"1", 1) == 0)
623 INFO (
"Unknown interrupt descriptor");
651 INFO (
"connection array overflow");
656 INFO (
"don't even try...");
673 INFO (
"memif delete fail");
680 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
685 printf (
"==============================\n");
692 printf (
"commands:\n");
693 printf (
"\thelp - prints this help\n");
694 printf (
"\texit - exit app\n");
696 (
"\tconn <index> <mode> [<interrupt-desc>] - create memif. index is also used as interface id, mode 0 = slave 1 = master, interrupt-desc none = default 0 = if ring is full wait 1 = handle only ARP requests\n");
697 printf (
"\tdel <index> - delete memif\n");
698 printf (
"\tshow - show connection details\n");
699 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
701 (
"\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
702 printf (
"\tsh-count - print counters\n");
703 printf (
"\tcl-count - clear counters\n");
705 (
"\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
736 INFO (
"connection array overflow");
741 INFO (
"don't even try...");
747 INFO (
"no connection at index %ld", index);
754 ui = strtok (ip,
".");
757 tmp[0] = strtol (ui, &end, 10);
759 ui = strtok (
NULL,
".");
762 tmp[1] = strtol (ui, &end, 10);
764 ui = strtok (
NULL,
".");
767 tmp[2] = strtol (ui, &end, 10);
769 ui = strtok (
NULL,
".");
772 tmp[3] = strtol (ui, &end, 10);
779 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
785 INFO (
"invalid ip address");
794 INFO (
"connection array overflow");
799 INFO (
"don't even try...");
806 INFO (
"no connection at index %ld", index);
810 if (strncmp (mode,
"interrupt", 9) == 0)
815 else if (strncmp (mode,
"polling", 7) == 0)
820 INFO (
"expected rx mode <interrupt|polling>");
833 printf (
"===============================\n");
834 printf (
"interface index: %d\n", c->
index);
838 printf (
"\tts: %lus %luns\n", c->
t_sec, c->
t_nsec);
862 INFO (
"No connection at index %d. Stopping flow...\n", flow->
index);
870 flow->
start = malloc (
sizeof (
struct timespec));
871 memset (flow->
start, 0, sizeof (
struct timespec));
882 INFO (
"memif_buffer_alloc: %s Stopping flow...\n",
911 INFO (
"Flow finished!");
912 INFO (
"Flow length: %lu", flow->
tx);
913 uint64_t t1 = flow->
end.tv_sec - flow->
start->tv_sec;
915 if (flow->
end.tv_nsec > flow->
start->tv_nsec)
917 t2 = flow->
end.tv_nsec - flow->
start->tv_nsec;
921 t2 = flow->
start->tv_nsec - flow->
end.tv_nsec;
926 INFO (
"Flow time: %lus %luns", t1, t2);
929 tmp = flow->
tx / tmp;
930 INFO (
"Average pps: %f", tmp);
931 INFO (
"Stopping flow...");
953 printf (
"only one flow allowed\n");
970 memset (&flow->
end, 0, sizeof (
struct timespec));
973 printf (
"%s\n", input);
975 ui = strtok (input,
".");
978 tmp[0] = strtol (ui, &end, 10);
980 ui = strtok (
NULL,
".");
983 tmp[1] = strtol (ui, &end, 10);
985 ui = strtok (
NULL,
".");
988 tmp[2] = strtol (ui, &end, 10);
990 ui = strtok (
NULL,
".");
993 tmp[3] = strtol (ui, &end, 10);
1000 ui = strtok (
NULL,
" ");
1007 ui = strtok (
NULL,
":");
1010 tmp[0] = strtol (ui, &end, 16);
1011 ui = strtok (
NULL,
":");
1014 tmp[1] = strtol (ui, &end, 16);
1015 ui = strtok (
NULL,
":");
1018 tmp[2] = strtol (ui, &end, 16);
1019 ui = strtok (
NULL,
":");
1022 tmp[3] = strtol (ui, &end, 16);
1023 ui = strtok (
NULL,
":");
1026 tmp[4] = strtol (ui, &end, 16);
1027 ui = strtok (
NULL,
":");
1030 tmp[5] = strtol (ui, &end, 16);
1044 INFO (
"Invalid input\n");
1055 char *in = (
char *) malloc (256);
1056 char *ui = fgets (in, 256, stdin);
1061 ui = strtok (in,
" ");
1062 if (strncmp (ui,
"exit", 4) == 0)
1066 exit (EXIT_SUCCESS);
1068 else if (strncmp (ui,
"help", 4) == 0)
1073 else if (strncmp (ui,
"conn", 4) == 0)
1075 ui = strtok (
NULL,
" ");
1077 a = strtol (ui, &end, 10);
1080 INFO (
"expected id");
1083 ui = strtok (
NULL,
" ");
1087 INFO (
"expected mode <0|1>");
1090 else if (strncmp (ui,
"del", 3) == 0)
1092 ui = strtok (
NULL,
" ");
1096 INFO (
"expected id");
1099 else if (strncmp (ui,
"show", 4) == 0)
1104 else if (strncmp (ui,
"ip-set", 6) == 0)
1106 ui = strtok (
NULL,
" ");
1110 INFO (
"expected id");
1113 else if (strncmp (ui,
"rx-mode", 7) == 0)
1115 ui = strtok (
NULL,
" ");
1117 a = strtol (ui, &end, 10);
1120 INFO (
"expected id");
1123 ui = strtok (
NULL,
" ");
1127 INFO (
"expected qid");
1130 else if (strncmp (ui,
"sh-count", 8) == 0)
1134 else if (strncmp (ui,
"cl-count", 8) == 0)
1138 else if (strncmp (ui,
"send", 4) == 0)
1140 ui = strtok (
NULL,
" ");
1142 a = strtol (ui, &end, 10);
1145 INFO (
"expected id");
1148 ui = strtok (
NULL,
" ");
1152 INFO (
"expected count");
1157 INFO (
"unknown command: %s", ui);
1170 struct epoll_event evt, *e;
1171 int app_err = 0, memif_err = 0, en = 0;
1173 uint32_t events = 0;
1174 struct timespec start, end;
1175 memset (&evt, 0,
sizeof (evt));
1176 evt.events = EPOLLIN | EPOLLOUT;
1178 sigemptyset (&sigset);
1179 en = epoll_pwait (
epfd, &evt, 1, timeout, &sigset);
1184 DBG (
"epoll_pwait: %s", strerror (errno));
1190 if (evt.data.fd > 2)
1194 if (evt.events & EPOLLIN)
1196 if (evt.events & EPOLLOUT)
1198 if (evt.events & EPOLLERR)
1204 else if (evt.data.fd == 0)
1210 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
1215 LOG (
"interrupt: %ld", end.tv_nsec - start.tv_nsec);
1217 if ((app_err < 0) || (memif_err < 0))
1220 DBG (
"user input handler error");
1222 DBG (
"memif control fd handler error");
1232 epfd = epoll_create (1);
1241 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1243 INFO (
"Error opening log file: %s", strerror (errno));
1272 DBG (
"poll_event error!");
void * icmpr_send_proc(void *data)
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 ...
int on_interrupt0(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Optimized string handling code, including c11-compliant "safe C library" variants.
int on_interrupt1(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
int resolve_packet2(void *pck, uint32_t *size, uint8_t ip_addr[4])
int generate_packet2(void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq, icmpr_flow_mode_t mode)
void icmpr_print_counters()
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 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
char * memif_strerror(int err_code)
Memif strerror.
int icmpr_memif_create(int is_master)
static void print_memif_details()
uint8_t interface_name[32]
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_buffer_enq_tx(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *count_out)
Memif buffer enq tx.
int resolve_packet3(void **pck_, uint32_t *size, uint8_t ip_addr[4])
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.
memif_queue_details_t * rx_queues
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_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 control_fd_update(int fd, uint8_t events)
void icmpr_reset_counters()
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.
vl_api_gbp_vxlan_tunnel_mode_t mode
#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[])
int icmpr_send(long index, long packet_num, char *hw, char *ip)
memif_queue_details_t * tx_queues
#define MEMIF_FD_EVENT_WRITE
uint16_t memif_get_version()
Memif get version.
Memif connection arguments.