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> 53 #define APP_NAME "ICMP_Responder" 54 #define IF_NAME "memif_connection" 58 #define DBG(...) do { \ 59 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ 60 printf (__VA_ARGS__); \ 63 #define LOG(...) do { \ 65 dprintf (out_fd, __VA_ARGS__); \ 66 dprintf (out_fd, "\n"); \ 69 #define LOG_FILE "/tmp/memif_time_test.txt" 75 #define INFO(...) do { \ 76 printf ("INFO: "__VA_ARGS__); \ 82 #define MAX_MEMIF_BUFS 256 133 buf = malloc (buflen);
134 printf (
"MEMIF DETAILS\n");
135 printf (
"==============================\n");
140 memset (&md, 0,
sizeof (md));
141 memset (buf, 0, buflen);
151 printf (
"interface index: %d\n", i);
153 printf (
"\tinterface ip: %u.%u.%u.%u\n",
155 printf (
"\tinterface name: %s\n", (
char *) md.
if_name);
156 printf (
"\tapp name: %s\n", (
char *) md.
inst_name);
157 printf (
"\tremote interface name: %s\n", (
char *) md.
remote_if_name);
159 printf (
"\tid: %u\n", md.
id);
160 printf (
"\tsecret: %s\n", (
char *) md.
secret);
170 printf (
"ethernet\n");
176 printf (
"punt/inject\n");
179 printf (
"unknown\n");
183 printf (
"\trx queues:\n");
188 printf (
"\t\tring rx mode: %s\n",
194 printf (
"\ttx queues:\n");
199 printf (
"\t\tring rx mode: %s\n",
219 DBG (
"invalid fd %d", fd);
222 struct epoll_event evt;
223 memset (&evt, 0,
sizeof (evt));
226 if (epoll_ctl (
epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
228 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
231 DBG (
"fd %d added to epoll", fd);
240 DBG (
"invalid fd %d", fd);
243 struct epoll_event evt;
244 memset (&evt, 0,
sizeof (evt));
247 if (epoll_ctl (
epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
249 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
252 DBG (
"fd %d moddified on epoll", fd);
261 DBG (
"invalid fd %d", fd);
264 struct epoll_event evt;
265 memset (&evt, 0,
sizeof (evt));
266 if (epoll_ctl (
epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
268 DBG (
"epoll_ctl: %s fd %d", strerror (errno), fd);
271 DBG (
"fd %d removed from epoll", fd);
280 INFO (
"memif connected!");
290 INFO (
"memif disconnected!");
329 DBG (
"allocated %d/%ld buffers, %u free buffers", *r, n,
355 long index = *((
long *) private_ctx);
357 if (c->
index != index)
359 INFO (
"invalid context: %ld/%u", index, c->
index);
401 (
void *) (c->
tx_bufs + j)->data,
404 (c->
rx_bufs + i + 1)->data_len,
405 (
void *) (c->
tx_bufs + j + 1)->data,
414 (
void *) (c->
tx_bufs + j)->data,
427 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
449 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
460 long index = *((
long *) private_ctx);
462 if (c->
index != index)
464 INFO (
"invalid context: %ld/%u", index, c->
index);
510 (
void *) (c->
tx_bufs + j)->data,
513 (c->
rx_bufs + i + 1)->data_len,
514 (
void *) (c->
tx_bufs + j + 1)->data,
515 &(c->
tx_bufs + j + 1)->data_len,
524 (
void *) (c->
tx_bufs + j)->data,
538 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
562 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
573 long index = *((
long *) private_ctx);
575 if (c->
index != index)
577 INFO (
"invalid context: %ld/%u", index, c->
index);
600 for (i = 0; i < rx; i++)
602 if (((
struct ether_header *) (c->
rx_bufs + i)->data)->ether_type ==
609 (
void *) (c->
tx_bufs + i)->data,
631 DBG (
"freed %d buffers. %u/%u alloc/free buffers",
641 INFO (
"connection array overflow");
646 INFO (
"don't even try...");
654 memset (&args, 0,
sizeof (args));
685 if (strncmp (s,
"0", 1) == 0)
696 else if (strncmp (s,
"1", 1) == 0)
709 INFO (
"Unknown interrupt descriptor");
739 INFO (
"connection array overflow");
744 INFO (
"don't even try...");
764 INFO (
"memif delete fail");
771 printf (
"LIBMEMIF EXAMPLE APP: %s",
APP_NAME);
776 printf (
"==============================\n");
783 printf (
"commands:\n");
784 printf (
"\thelp - prints this help\n");
785 printf (
"\texit - exit app\n");
787 (
"\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");
788 printf (
"\tdel <index> - delete memif\n");
789 printf (
"\tshow - show connection details\n");
790 printf (
"\tip-set <index> <ip-addr> - set interface ip address\n");
792 (
"\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
793 printf (
"\tsh-count - print counters\n");
794 printf (
"\tcl-count - clear counters\n");
795 printf (
"\tsend <index> <tx> <ip> <mac> - send icmp\n");
826 INFO (
"connection array overflow");
831 INFO (
"don't even try...");
837 INFO (
"no connection at index %ld", index);
844 ui = strtok (ip,
".");
847 tmp[0] = strtol (ui, &end, 10);
849 ui = strtok (
NULL,
".");
852 tmp[1] = strtol (ui, &end, 10);
854 ui = strtok (
NULL,
".");
857 tmp[2] = strtol (ui, &end, 10);
859 ui = strtok (
NULL,
".");
862 tmp[3] = strtol (ui, &end, 10);
869 INFO (
"memif %ld ip address set to %u.%u.%u.%u",
875 INFO (
"invalid ip address");
884 INFO (
"connection array overflow");
889 INFO (
"don't even try...");
896 INFO (
"no connection at index %ld", index);
900 if (strncmp (mode,
"interrupt", 9) == 0)
905 else if (strncmp (mode,
"polling", 7) == 0)
910 INFO (
"expected rx mode <interrupt|polling>");
923 printf (
"===============================\n");
924 printf (
"interface index: %d\n", c->
index);
928 printf (
"\tts: %lus %luns\n", c->
t_sec, c->
t_nsec);
950 int index = d->
index;
955 INFO (
"No connection at index %d. Thread exiting...\n", index);
961 struct timespec start, end;
962 memset (&start, 0,
sizeof (start));
963 memset (&end, 0,
sizeof (end));
965 timespec_get (&start, TIME_UTC);
974 INFO (
"memif_buffer_alloc: %s Thread exiting...\n",
1002 INFO (
"memif_tx_burst: %s Thread exiting...\n",
1004 pthread_exit (
NULL);
1010 timespec_get (&end, TIME_UTC);
1011 INFO (
"Pakcet sequence finished!");
1012 INFO (
"Seq len: %u", seq);
1013 uint64_t t1 = end.tv_sec - start.tv_sec;
1015 if (end.tv_nsec > start.tv_nsec)
1017 t2 = end.tv_nsec - start.tv_nsec;
1021 t2 = start.tv_nsec - end.tv_nsec;
1026 INFO (
"Seq time: %lus %luns", t1, t2);
1030 INFO (
"Average pps: %f", tmp);
1031 INFO (
"Thread exiting...");
1032 pthread_exit (
NULL);
1046 data->
index = index;
1049 printf (
"%s\n", ip);
1050 printf (
"%s\n", hw);
1052 ui = strtok (ip,
".");
1055 tmp[0] = strtol (ui, &end, 10);
1057 ui = strtok (
NULL,
".");
1060 tmp[1] = strtol (ui, &end, 10);
1062 ui = strtok (
NULL,
".");
1065 tmp[2] = strtol (ui, &end, 10);
1067 ui = strtok (
NULL,
".");
1070 tmp[3] = strtol (ui, &end, 10);
1077 ui = strtok (hw,
":");
1080 tmp[0] = strtol (ui, &end, 16);
1081 ui = strtok (
NULL,
":");
1084 tmp[1] = strtol (ui, &end, 16);
1085 ui = strtok (
NULL,
":");
1088 tmp[2] = strtol (ui, &end, 16);
1089 ui = strtok (
NULL,
":");
1092 tmp[3] = strtol (ui, &end, 16);
1093 ui = strtok (
NULL,
":");
1096 tmp[4] = strtol (ui, &end, 16);
1097 ui = strtok (
NULL,
":");
1100 tmp[5] = strtol (ui, &end, 16);
1113 INFO (
"Invalid input\n");
1121 char *in = (
char *) malloc (256);
1122 char *ui = fgets (in, 256, stdin);
1127 ui = strtok (in,
" ");
1128 if (strncmp (ui,
"exit", 4) == 0)
1132 exit (EXIT_SUCCESS);
1134 else if (strncmp (ui,
"help", 4) == 0)
1139 else if (strncmp (ui,
"conn", 4) == 0)
1141 ui = strtok (
NULL,
" ");
1143 a = strtol (ui, &end, 10);
1146 INFO (
"expected id");
1149 ui = strtok (
NULL,
" ");
1153 INFO (
"expected mode <0|1>");
1156 else if (strncmp (ui,
"del", 3) == 0)
1158 ui = strtok (
NULL,
" ");
1162 INFO (
"expected id");
1165 else if (strncmp (ui,
"show", 4) == 0)
1170 else if (strncmp (ui,
"ip-set", 6) == 0)
1172 ui = strtok (
NULL,
" ");
1176 INFO (
"expected id");
1179 else if (strncmp (ui,
"rx-mode", 7) == 0)
1181 ui = strtok (
NULL,
" ");
1183 a = strtol (ui, &end, 10);
1186 INFO (
"expected id");
1189 ui = strtok (
NULL,
" ");
1193 INFO (
"expected qid");
1196 else if (strncmp (ui,
"sh-count", 8) == 0)
1200 else if (strncmp (ui,
"cl-count", 8) == 0)
1204 else if (strncmp (ui,
"send", 4) == 0)
1206 ui = strtok (
NULL,
" ");
1208 a = strtol (ui, &end, 10);
1211 INFO (
"expected id");
1214 ui = strtok (
NULL,
" ");
1217 strtok (
NULL,
" "));
1219 INFO (
"expected count");
1224 INFO (
"unknown command: %s", ui);
1237 struct epoll_event evt, *e;
1238 int app_err = 0, memif_err = 0, en = 0;
1240 uint32_t events = 0;
1241 struct timespec start, end;
1242 memset (&evt, 0,
sizeof (evt));
1243 evt.events = EPOLLIN | EPOLLOUT;
1245 sigemptyset (&sigset);
1246 en = epoll_pwait (
epfd, &evt, 1, timeout, &sigset);
1248 timespec_get (&start, TIME_UTC);
1251 DBG (
"epoll_pwait: %s", strerror (errno));
1257 if (evt.data.fd > 2)
1261 if (evt.events & EPOLLIN)
1263 if (evt.events & EPOLLOUT)
1265 if (evt.events & EPOLLERR)
1271 else if (evt.data.fd == 0)
1277 DBG (
"unexpected event at memif_epfd. fd %d", evt.data.fd);
1281 timespec_get (&end, TIME_UTC);
1282 LOG (
"interrupt: %ld", end.tv_nsec - start.tv_nsec);
1284 if ((app_err < 0) || (memif_err < 0))
1287 DBG (
"user input handler error");
1289 DBG (
"memif control fd handler error");
1299 epfd = epoll_create (1);
1306 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1308 INFO (
"Error opening log file: %s", strerror (errno));
1333 DBG (
"poll_event error!");
void icmpr_reset_counters()
int on_interrupt1(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
sll srl srl sll sra u16x4 i
memif_log2_ring_size_t log2_ring_size
int mod_epoll_fd(int fd, uint32_t events)
void * icmpr_send_proc(void *data)
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)
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
pthread_t thread[MAX_CONNS]
#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
void icmpr_print_counters()
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
int icmpr_send(long index, long packet_num, char *hw, char *ip)
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 generate_packet(void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
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.