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> 57 #define APP_NAME "ICMP_Responder" 58 #define IF_NAME "memif_connection" 62 #define DBG(...) do { \ 63 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ 64 printf (__VA_ARGS__); \ 67 #define LOG(...) do { \ 69 dprintf (out_fd, __VA_ARGS__); \ 70 dprintf (out_fd, "\n"); \ 73 #define LOG_FILE "/tmp/memif_time_test.txt" 79 #define INFO(...) do { \ 80 printf ("INFO: "__VA_ARGS__); \ 86 #define MAX_MEMIF_BUFS 256 89 #define ICMPR_HEADROOM 64 113 uint64_t tx_counter, rx_counter, tx_err_counter;
114 uint64_t t_sec, t_nsec;
139 buf = malloc (buflen);
140 printf (
"MEMIF DETAILS\n");
141 printf (
"==============================\n");
146 memset (&md, 0,
sizeof (md));
147 memset (buf, 0, buflen);
157 printf (
"interface index: %d\n", i);
159 printf (
"\tinterface ip: %u.%u.%u.%u\n",
161 printf (
"\tinterface name: %s\n", (
char *) md.
if_name);
162 printf (
"\tapp name: %s\n", (
char *) md.
inst_name);
163 printf (
"\tremote interface name: %s\n", (
char *) md.
remote_if_name);
165 printf (
"\tid: %u\n", md.
id);
166 printf (
"\tsecret: %s\n", (
char *) md.
secret);
176 printf (
"ethernet\n");
182 printf (
"punt/inject\n");
185 printf (
"unknown\n");
189 printf (
"\trx queues:\n");
194 printf (
"\t\tring rx mode: %s\n",
200 printf (
"\ttx queues:\n");
205 printf (
"\t\tring rx mode: %s\n",
217 printf (
"\treason: %s\n", md.
error);
228 DBG (
"invalid fd %d", fd);
231 struct epoll_event evt;
232 memset (&evt, 0,
sizeof (evt));
235 if (epoll_ctl (
epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
237 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
240 DBG (
"fd %d added to epoll", fd);
249 DBG (
"invalid fd %d", fd);
252 struct epoll_event evt;
253 memset (&evt, 0,
sizeof (evt));
256 if (epoll_ctl (
epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
258 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
261 DBG (
"fd %d moddified on epoll", fd);
270 DBG (
"invalid fd %d", fd);
273 struct epoll_event evt;
274 memset (&evt, 0,
sizeof (evt));
275 if (epoll_ctl (
epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
277 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
280 DBG (
"fd %d removed from epoll", fd);
289 INFO (
"memif connected!");
300 INFO (
"memif disconnected!");
339 DBG (
"allocated %d/%ld buffers, %u free buffers", *r, n,
365 long index = *((
long *) private_ctx);
367 if (c->
index != index)
369 INFO (
"invalid context: %ld/%u", index, c->
index);
374 uint16_t rx = 0, tx = 0;
407 (
void *) (c->
tx_bufs + j)->data,
439 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
450 long index = *((
long *) private_ctx);
452 if (c->
index != index)
454 INFO (
"invalid context: %ld/%u", index, c->
index);
459 uint16_t rx = 0, tx = 0;
500 (
void *) (c->
tx_bufs + j)->data,
504 (
void *) (c->
tx_bufs + j + 1)->data,
513 (
void *) (c->
tx_bufs + j)->data,
527 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
551 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
562 long index = *((
long *) private_ctx);
564 if (c->
index != index)
566 INFO (
"invalid context: %ld/%u", index, c->
index);
589 for (i = 0; i < rx; i++)
591 if (((
struct ether_header *) (c->
rx_bufs + i)->data)->ether_type ==
598 (
void *) (c->
tx_bufs + i)->data,
620 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
630 INFO (
"connection array overflow");
635 INFO (
"don't even try...");
643 memset (&args, 0,
sizeof (args));
673 if (strncmp (s,
"0", 1) == 0)
684 else if (strncmp (s,
"1", 1) == 0)
697 INFO (
"Unknown interrupt descriptor");
727 INFO (
"connection array overflow");
732 INFO (
"don't even try...");
752 INFO (
"memif delete fail");
759 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
764 printf (
"==============================\n");
771 printf (
"commands:\n");
772 printf (
"\thelp - prints this help\n");
773 printf (
"\texit - exit app\n");
775 (
"\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");
776 printf (
"\tdel <index> - delete memif\n");
777 printf (
"\tshow - show connection details\n");
778 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
780 (
"\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
781 printf (
"\tsh-count - print counters\n");
782 printf (
"\tcl-count - clear counters\n");
783 printf (
"\tsend <index> <tx> <ip> <mac> - send icmp\n");
814 INFO (
"connection array overflow");
819 INFO (
"don't even try...");
825 INFO (
"no connection at index %ld", index);
832 ui = strtok (ip,
".");
835 tmp[0] = strtol (ui, &end, 10);
837 ui = strtok (
NULL,
".");
840 tmp[1] = strtol (ui, &end, 10);
842 ui = strtok (
NULL,
".");
845 tmp[2] = strtol (ui, &end, 10);
847 ui = strtok (
NULL,
".");
850 tmp[3] = strtol (ui, &end, 10);
857 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
863 INFO (
"invalid ip address");
872 INFO (
"connection array overflow");
877 INFO (
"don't even try...");
884 INFO (
"no connection at index %ld", index);
888 if (strncmp (mode,
"interrupt", 9) == 0)
893 else if (strncmp (mode,
"polling", 7) == 0)
898 INFO (
"expected rx mode <interrupt|polling>");
911 printf (
"===============================\n");
912 printf (
"interface index: %d\n", c->
index);
916 printf (
"\tts: %lus %luns\n", c->
t_sec, c->
t_nsec);
938 int index = d->
index;
943 INFO (
"No connection at index %d. Thread exiting...\n", index);
949 struct timespec start, end;
950 memset (&start, 0,
sizeof (start));
951 memset (&end, 0,
sizeof (end));
962 INFO (
"memif_buffer_alloc: %s Thread exiting...\n",
990 INFO (
"memif_tx_burst: %s Thread exiting...\n",
1000 INFO (
"Pakcet sequence finished!");
1001 INFO (
"Seq len: %u", seq);
1002 uint64_t t1 = end.tv_sec - start.tv_sec;
1004 if (end.tv_nsec > start.tv_nsec)
1006 t2 = end.tv_nsec - start.tv_nsec;
1010 t2 = start.tv_nsec - end.tv_nsec;
1015 INFO (
"Seq time: %lus %luns", t1, t2);
1019 INFO (
"Average pps: %f", tmp);
1020 INFO (
"Thread exiting...");
1021 pthread_exit (
NULL);
1035 data->
index = index;
1038 printf (
"%s\n", ip);
1039 printf (
"%s\n", hw);
1041 ui = strtok (ip,
".");
1044 tmp[0] = strtol (ui, &end, 10);
1046 ui = strtok (
NULL,
".");
1049 tmp[1] = strtol (ui, &end, 10);
1051 ui = strtok (
NULL,
".");
1054 tmp[2] = strtol (ui, &end, 10);
1056 ui = strtok (
NULL,
".");
1059 tmp[3] = strtol (ui, &end, 10);
1066 ui = strtok (hw,
":");
1069 tmp[0] = strtol (ui, &end, 16);
1070 ui = strtok (
NULL,
":");
1073 tmp[1] = strtol (ui, &end, 16);
1074 ui = strtok (
NULL,
":");
1077 tmp[2] = strtol (ui, &end, 16);
1078 ui = strtok (
NULL,
":");
1081 tmp[3] = strtol (ui, &end, 16);
1082 ui = strtok (
NULL,
":");
1085 tmp[4] = strtol (ui, &end, 16);
1086 ui = strtok (
NULL,
":");
1089 tmp[5] = strtol (ui, &end, 16);
1102 INFO (
"Invalid input\n");
1110 char *in = (
char *) malloc (256);
1111 char *ui = fgets (in, 256, stdin);
1116 ui = strtok (in,
" ");
1117 if (strncmp (ui,
"exit", 4) == 0)
1121 exit (EXIT_SUCCESS);
1123 else if (strncmp (ui,
"help", 4) == 0)
1128 else if (strncmp (ui,
"conn", 4) == 0)
1130 ui = strtok (
NULL,
" ");
1132 a = strtol (ui, &end, 10);
1135 INFO (
"expected id");
1138 ui = strtok (
NULL,
" ");
1142 INFO (
"expected mode <0|1>");
1145 else if (strncmp (ui,
"del", 3) == 0)
1147 ui = strtok (
NULL,
" ");
1151 INFO (
"expected id");
1154 else if (strncmp (ui,
"show", 4) == 0)
1159 else if (strncmp (ui,
"ip-set", 6) == 0)
1161 ui = strtok (
NULL,
" ");
1165 INFO (
"expected id");
1168 else if (strncmp (ui,
"rx-mode", 7) == 0)
1170 ui = strtok (
NULL,
" ");
1172 a = strtol (ui, &end, 10);
1175 INFO (
"expected id");
1178 ui = strtok (
NULL,
" ");
1182 INFO (
"expected qid");
1185 else if (strncmp (ui,
"sh-count", 8) == 0)
1189 else if (strncmp (ui,
"cl-count", 8) == 0)
1193 else if (strncmp (ui,
"send", 4) == 0)
1195 ui = strtok (
NULL,
" ");
1197 a = strtol (ui, &end, 10);
1200 INFO (
"expected id");
1203 ui = strtok (
NULL,
" ");
1206 strtok (
NULL,
" "));
1208 INFO (
"expected count");
1213 INFO (
"unknown command: %s", ui);
1226 struct epoll_event evt, *e;
1227 int app_err = 0, memif_err = 0, en = 0;
1229 uint32_t events = 0;
1230 struct timespec start, end;
1231 memset (&evt, 0,
sizeof (evt));
1232 evt.events = EPOLLIN | EPOLLOUT;
1234 sigemptyset (&sigset);
1235 en = epoll_pwait (
epfd, &evt, 1, timeout, &sigset);
1240 DBG (
"epoll_pwait: %s", strerror (errno));
1246 if (evt.data.fd > 2)
1250 if (evt.events & EPOLLIN)
1252 if (evt.events & EPOLLOUT)
1254 if (evt.events & EPOLLERR)
1260 else if (evt.data.fd == 0)
1266 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
1271 LOG (
"interrupt: %ld", end.tv_nsec - start.tv_nsec);
1273 if ((app_err < 0) || (memif_err < 0))
1276 DBG (
"user input handler error");
1278 DBG (
"memif control fd handler error");
1288 epfd = epoll_create (1);
1295 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1297 INFO (
"Error opening log file: %s", strerror (errno));
1326 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)
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)
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 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.
memif_queue_details_t * rx_queues
vl_api_vxlan_gbp_api_tunnel_mode_t mode
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 generate_packet(void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
void * memif_conn_handle_t
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 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.
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[])
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.