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;
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 modified 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;
350 for (i = 0; i < rx; i++)
373 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
395 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
404 long index = *((
long *) private_ctx);
406 if (c->
index != index)
408 INFO (
"invalid context: %ld/%u", index, c->
index);
413 uint16_t rx = 0, tx = 0;
431 for (i = 0; i < rx; i++)
452 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
474 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
485 long index = *((
long *) private_ctx);
487 if (c->
index != index)
489 INFO (
"invalid context: %ld/%u", index, c->
index);
510 for (i = 0; i < rx; i++)
512 if (((
struct ether_header *) (c->
bufs + i)->data)->ether_type ==
540 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
550 INFO (
"connection array overflow");
555 INFO (
"don't even try...");
562 memset (&args, 0,
sizeof (args));
592 if (strncmp (s,
"0", 1) == 0)
603 else if (strncmp (s,
"1", 1) == 0)
616 INFO (
"Unknown interrupt descriptor");
644 INFO (
"connection array overflow");
649 INFO (
"don't even try...");
666 INFO (
"memif delete fail");
673 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
678 printf (
"==============================\n");
685 printf (
"commands:\n");
686 printf (
"\thelp - prints this help\n");
687 printf (
"\texit - exit app\n");
689 (
"\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");
690 printf (
"\tdel <index> - delete memif\n");
691 printf (
"\tshow - show connection details\n");
692 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
694 (
"\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
695 printf (
"\tsh-count - print counters\n");
696 printf (
"\tcl-count - clear counters\n");
698 (
"\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
729 INFO (
"connection array overflow");
734 INFO (
"don't even try...");
740 INFO (
"no connection at index %ld", index);
747 ui = strtok (ip,
".");
750 tmp[0] = strtol (ui, &end, 10);
752 ui = strtok (NULL,
".");
755 tmp[1] = strtol (ui, &end, 10);
757 ui = strtok (NULL,
".");
760 tmp[2] = strtol (ui, &end, 10);
762 ui = strtok (NULL,
".");
765 tmp[3] = strtol (ui, &end, 10);
772 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
778 INFO (
"invalid ip address");
787 INFO (
"connection array overflow");
792 INFO (
"don't even try...");
799 INFO (
"no connection at index %ld", index);
803 if (strncmp (mode,
"interrupt", 9) == 0)
808 else if (strncmp (mode,
"polling", 7) == 0)
813 INFO (
"expected rx mode <interrupt|polling>");
826 printf (
"===============================\n");
827 printf (
"interface index: %d\n", c->
index);
831 printf (
"\tts: %lus %luns\n", c->
t_sec, c->
t_nsec);
855 INFO (
"No connection at index %d. Stopping flow...\n", flow->
index);
864 memset (flow->
start, 0, sizeof (
struct timespec));
875 INFO (
"memif_buffer_alloc: %s Stopping flow...\n",
904 INFO (
"Flow finished!");
905 INFO (
"Flow length: %lu", flow->
tx);
906 uint64_t t1 = flow->
end.tv_sec - flow->
start->tv_sec;
908 if (flow->
end.tv_nsec > flow->
start->tv_nsec)
910 t2 = flow->
end.tv_nsec - flow->
start->tv_nsec;
914 t2 = flow->
start->tv_nsec - flow->
end.tv_nsec;
919 INFO (
"Flow time: %lus %luns", t1, t2);
922 tmp = flow->
tx / tmp;
923 INFO (
"Average pps: %f", tmp);
924 INFO (
"Stopping flow...");
946 printf (
"only one flow allowed\n");
963 memset (&flow->
end, 0, sizeof (
struct timespec));
966 printf (
"%s\n", input);
968 ui = strtok (input,
".");
971 tmp[0] = strtol (ui, &end, 10);
973 ui = strtok (NULL,
".");
976 tmp[1] = strtol (ui, &end, 10);
978 ui = strtok (NULL,
".");
981 tmp[2] = strtol (ui, &end, 10);
983 ui = strtok (NULL,
".");
986 tmp[3] = strtol (ui, &end, 10);
993 ui = strtok (NULL,
" ");
1000 ui = strtok (NULL,
":");
1003 tmp[0] = strtol (ui, &end, 16);
1004 ui = strtok (NULL,
":");
1007 tmp[1] = strtol (ui, &end, 16);
1008 ui = strtok (NULL,
":");
1011 tmp[2] = strtol (ui, &end, 16);
1012 ui = strtok (NULL,
":");
1015 tmp[3] = strtol (ui, &end, 16);
1016 ui = strtok (NULL,
":");
1019 tmp[4] = strtol (ui, &end, 16);
1020 ui = strtok (NULL,
":");
1023 tmp[5] = strtol (ui, &end, 16);
1037 INFO (
"Invalid input\n");
1047 char *in = (
char *)
malloc (256);
1048 char *ui = fgets (in, 256, stdin);
1053 ui = strtok (in,
" ");
1054 if (strncmp (ui,
"exit", 4) == 0)
1058 exit (EXIT_SUCCESS);
1060 else if (strncmp (ui,
"help", 4) == 0)
1065 else if (strncmp (ui,
"conn", 4) == 0)
1067 ui = strtok (NULL,
" ");
1069 a = strtol (ui, &end, 10);
1072 INFO (
"expected id");
1075 ui = strtok (NULL,
" ");
1079 INFO (
"expected mode <0|1>");
1082 else if (strncmp (ui,
"del", 3) == 0)
1084 ui = strtok (NULL,
" ");
1088 INFO (
"expected id");
1091 else if (strncmp (ui,
"show", 4) == 0)
1096 else if (strncmp (ui,
"ip-set", 6) == 0)
1098 ui = strtok (NULL,
" ");
1100 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL,
" "));
1102 INFO (
"expected id");
1105 else if (strncmp (ui,
"rx-mode", 7) == 0)
1107 ui = strtok (NULL,
" ");
1109 a = strtol (ui, &end, 10);
1112 INFO (
"expected id");
1115 ui = strtok (NULL,
" ");
1119 INFO (
"expected qid");
1122 else if (strncmp (ui,
"sh-count", 8) == 0)
1126 else if (strncmp (ui,
"cl-count", 8) == 0)
1130 else if (strncmp (ui,
"send", 4) == 0)
1132 ui = strtok (NULL,
" ");
1134 a = strtol (ui, &end, 10);
1137 INFO (
"expected id");
1140 ui = strtok (NULL,
" ");
1142 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL,
" "));
1144 INFO (
"expected count");
1149 INFO (
"unknown command: %s", ui);
1162 struct epoll_event evt;
1163 int app_err = 0, memif_err = 0, en = 0;
1164 uint32_t events = 0;
1165 struct timespec start, end;
1166 memset (&evt, 0,
sizeof (evt));
1167 evt.events = EPOLLIN | EPOLLOUT;
1169 sigemptyset (&sigset);
1170 en = epoll_pwait (
epfd, &evt, 1, timeout, &sigset);
1175 DBG (
"epoll_pwait: %s", strerror (errno));
1181 if (evt.data.fd > 2)
1185 if (evt.events & EPOLLIN)
1187 if (evt.events & EPOLLOUT)
1189 if (evt.events & EPOLLERR)
1195 else if (evt.data.fd == 0)
1201 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
1206 LOG (
"interrupt: %ld", end.tv_nsec - start.tv_nsec);
1208 if ((app_err < 0) || (memif_err < 0))
1211 DBG (
"user input handler error");
1213 DBG (
"memif control fd handler error");
1223 epfd = epoll_create (1);
1232 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1234 INFO (
"Error opening log file: %s", strerror (errno));
1252 memif_connection[
i].
conn = NULL;
1263 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 occurred 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 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.
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.
vl_api_tunnel_mode_t mode
void * malloc(size_t size)
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.
sll srl srl sll sra u16x4 i
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)
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.
#define MEMIF_FD_EVENT_ERROR
inform libmemif that error occurred 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
f64 end
end of the time range
#define MEMIF_FD_EVENT_WRITE
uint16_t memif_get_version()
Memif get version.
Memif connection arguments.