2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
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>
55 #include <icmp_proto.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__); \
85 /* maximum tx/rx memif buffers */
86 #define MAX_MEMIF_BUFS 256
96 /* memif conenction handle */
97 memif_conn_handle_t conn;
99 memif_buffer_t *tx_bufs;
100 /* allocated tx buffers counter */
101 /* number of tx buffers pointing to shared memory */
104 memif_buffer_t *rx_bufs;
105 /* allcoated rx buffers counter */
106 /* number of rx buffers pointing to shared memory */
108 /* interface ip address */
111 uint64_t tx_counter, rx_counter, tx_err_counter;
112 uint64_t t_sec, t_nsec;
113 } memif_connection_t;
121 } icmpr_thread_data_t;
123 memif_connection_t memif_connection[MAX_CONNS];
124 icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
125 pthread_t thread[MAX_CONNS];
128 /* print details for all memif connections */
130 print_memif_details ()
137 buf = malloc (buflen);
138 printf ("MEMIF DETAILS\n");
139 printf ("==============================\n");
140 for (i = 0; i < MAX_CONNS; i++)
142 memif_connection_t *c = &memif_connection[i];
144 memset (&md, 0, sizeof (md));
145 memset (buf, 0, buflen);
147 err = memif_get_details (c->conn, &md, buf, buflen);
148 if (err != MEMIF_ERR_SUCCESS)
150 if (err != MEMIF_ERR_NOCONN)
151 INFO ("%s", memif_strerror (err));
155 printf ("interface index: %d\n", i);
157 printf ("\tinterface ip: %u.%u.%u.%u\n",
158 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
159 printf ("\tinterface name: %s\n", (char *) md.if_name);
160 printf ("\tapp name: %s\n", (char *) md.inst_name);
161 printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
162 printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
163 printf ("\tid: %u\n", md.id);
164 printf ("\tsecret: %s\n", (char *) md.secret);
174 printf ("ethernet\n");
180 printf ("punt/inject\n");
183 printf ("unknown\n");
186 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
187 printf ("\trx queues:\n");
188 for (e = 0; e < md.rx_queues_num; e++)
190 printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
191 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
192 printf ("\t\tring rx mode: %s\n",
193 md.rx_queues[e].flags ? "polling" : "interrupt");
194 printf ("\t\tring head: %u\n", md.rx_queues[e].head);
195 printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
196 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
198 printf ("\ttx queues:\n");
199 for (e = 0; e < md.tx_queues_num; e++)
201 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
202 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
203 printf ("\t\tring rx mode: %s\n",
204 md.tx_queues[e].flags ? "polling" : "interrupt");
205 printf ("\t\tring head: %u\n", md.tx_queues[e].head);
206 printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
207 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
219 add_epoll_fd (int fd, uint32_t events)
223 DBG ("invalid fd %d", fd);
226 struct epoll_event evt;
227 memset (&evt, 0, sizeof (evt));
230 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
232 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
235 DBG ("fd %d added to epoll", fd);
240 mod_epoll_fd (int fd, uint32_t events)
244 DBG ("invalid fd %d", fd);
247 struct epoll_event evt;
248 memset (&evt, 0, sizeof (evt));
251 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
253 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
256 DBG ("fd %d moddified on epoll", fd);
261 del_epoll_fd (int fd)
265 DBG ("invalid fd %d", fd);
268 struct epoll_event evt;
269 memset (&evt, 0, sizeof (evt));
270 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
272 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
275 DBG ("fd %d removed from epoll", fd);
279 /* informs user about connected status. private_ctx is used by user to identify connection
280 (multiple connections WIP) */
282 on_connect (memif_conn_handle_t conn, void *private_ctx)
284 INFO ("memif connected!");
285 memif_refill_queue (conn, 0, -1, 0);
290 /* informs user about disconnected status. private_ctx is used by user to identify connection
291 (multiple connections WIP) */
293 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
295 INFO ("memif disconnected!");
299 /* user needs to watch new fd or stop watching fd that is about to be closed.
300 control fd will be modified during connection establishment to minimize CPU usage */
302 control_fd_update (int fd, uint8_t events)
304 /* convert memif event definitions to epoll events */
305 if (events & MEMIF_FD_EVENT_DEL)
306 return del_epoll_fd (fd);
309 if (events & MEMIF_FD_EVENT_READ)
311 if (events & MEMIF_FD_EVENT_WRITE)
314 if (events & MEMIF_FD_EVENT_MOD)
315 return mod_epoll_fd (fd, evt);
317 return add_epoll_fd (fd, evt);
321 icmpr_buffer_alloc (long index, long n, uint16_t * r, uint16_t i,
324 memif_connection_t *c = &memif_connection[index];
326 /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
327 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 128);
328 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
330 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
334 DBG ("allocated %d/%ld buffers, %u free buffers", *r, n,
335 MAX_MEMIF_BUFS - c->tx_buf_num);
340 icmpr_tx_burst (long index, uint16_t qid)
342 memif_connection_t *c = &memif_connection[index];
345 /* inform peer memif interface about data in shared memory buffers */
346 /* mark memif buffers as free */
347 err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
348 if (err != MEMIF_ERR_SUCCESS)
349 INFO ("memif_tx_burst: %s", memif_strerror (err));
355 /* called when event is polled on interrupt file descriptor.
356 there are packets in shared memory ready to be received */
358 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
360 long index = *((long *) private_ctx);
361 memif_connection_t *c = &memif_connection[index];
362 if (c->index != index)
364 INFO ("invalid context: %ld/%u", index, c->index);
368 int err = MEMIF_ERR_SUCCESS, ret_val;
369 uint16_t rx = 0, tx = 0;
371 int i = 0; /* rx buffer iterator */
372 int j = 0; /* tx bufferiterator */
374 /* loop while there are packets in shm */
377 /* receive data from shared memory buffers */
378 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
381 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
383 INFO ("memif_rx_burst: %s", memif_strerror (err));
388 memset (c->tx_bufs, 0, sizeof (memif_buffer_t) * rx);
389 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
390 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
392 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
396 c->tx_err_counter += rx - tx;
400 resolve_packet ((void *) (c->rx_bufs + i)->data,
401 (c->rx_bufs + i)->len,
402 (void *) (c->tx_bufs + j)->data,
403 &(c->tx_bufs + j)->len, c->ip_addr);
409 err = memif_refill_queue (c->conn, qid, rx, 0);
410 if (err != MEMIF_ERR_SUCCESS)
411 INFO ("memif_buffer_free: %s", memif_strerror (err));
414 DBG ("freed %d buffers. %u/%u alloc/free buffers",
415 rx, rx, MAX_MEMIF_BUFS - rx);
417 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
418 if (err != MEMIF_ERR_SUCCESS)
420 INFO ("memif_tx_burst: %s", memif_strerror (err));
426 while (ret_val == MEMIF_ERR_NOBUF);
431 err = memif_refill_queue (c->conn, qid, rx, 0);
432 if (err != MEMIF_ERR_SUCCESS)
433 INFO ("memif_buffer_free: %s", memif_strerror (err));
435 DBG ("freed %d buffers. %u/%u alloc/free buffers",
436 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
440 /* called when event is polled on interrupt file descriptor.
441 there are packets in shared memory ready to be received */
442 /* dev test modification: loop until TX == RX (don't drop buffers) */
444 on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
446 long index = *((long *) private_ctx);
447 memif_connection_t *c = &memif_connection[index];
448 if (c->index != index)
450 INFO ("invalid context: %ld/%u", index, c->index);
454 int err = MEMIF_ERR_SUCCESS, ret_val;
455 uint16_t rx = 0, tx = 0;
457 int i; /* rx buffer iterator */
458 int j; /* tx bufferiterator */
459 int prev_i; /* first allocated rx buffer */
461 /* loop while there are packets in shm */
464 /* receive data from shared memory buffers */
465 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
468 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
470 INFO ("memif_rx_burst: %s", memif_strerror (err));
476 /* loop while there are RX buffers to be processed */
480 /* try to alloc required number of buffers buffers */
481 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
482 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
484 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
494 resolve_packet ((void *) (c->rx_bufs + i)->data,
495 (c->rx_bufs + i)->len,
496 (void *) (c->tx_bufs + j)->data,
497 &(c->tx_bufs + j)->len, c->ip_addr);
498 resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
499 (c->rx_bufs + i + 1)->len,
500 (void *) (c->tx_bufs + j + 1)->data,
501 &(c->tx_bufs + j + 1)->len, c->ip_addr);
507 resolve_packet ((void *) (c->rx_bufs + i)->data,
508 (c->rx_bufs + i)->len,
509 (void *) (c->tx_bufs + j)->data,
510 &(c->tx_bufs + j)->len, c->ip_addr);
515 /* mark memif buffers and shared memory buffers as free */
516 /* free processed buffers */
517 err = memif_refill_queue (c->conn, qid, j, 0);
518 if (err != MEMIF_ERR_SUCCESS)
519 INFO ("memif_buffer_free: %s", memif_strerror (err));
523 DBG ("freed %d buffers. %u/%u alloc/free buffers",
524 fb, rx, MAX_MEMIF_BUFS - rx);
526 /* transmit allocated buffers */
527 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
528 if (err != MEMIF_ERR_SUCCESS)
530 INFO ("memif_tx_burst: %s", memif_strerror (err));
538 while (ret_val == MEMIF_ERR_NOBUF);
543 err = memif_refill_queue (c->conn, qid, rx, 0);
544 if (err != MEMIF_ERR_SUCCESS)
545 INFO ("memif_buffer_free: %s", memif_strerror (err));
547 DBG ("freed %d buffers. %u/%u alloc/free buffers",
548 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
552 /* called when event is polled on interrupt file descriptor.
553 there are packets in shared memory ready to be received */
554 /* dev test modification: handle only ARP requests */
556 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
558 long index = *((long *) private_ctx);
559 memif_connection_t *c = &memif_connection[index];
560 if (c->index != index)
562 INFO ("invalid context: %ld/%u", index, c->index);
566 int err = MEMIF_ERR_SUCCESS, ret_val;
574 /* receive data from shared memory buffers */
575 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
577 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
579 INFO ("memif_rx_burst: %s", memif_strerror (err));
585 for (i = 0; i < rx; i++)
587 if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
590 if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
592 resolve_packet ((void *) (c->rx_bufs + i)->data,
593 (c->rx_bufs + i)->len,
594 (void *) (c->tx_bufs + i)->data,
595 &(c->tx_bufs + i)->len, c->ip_addr);
596 icmpr_tx_burst (c->index, qid);
600 err = memif_refill_queue (c->conn, qid, rx, 0);
601 if (err != MEMIF_ERR_SUCCESS)
602 INFO ("memif_buffer_free: %s", memif_strerror (err));
607 while (ret_val == MEMIF_ERR_NOBUF);
612 err = memif_refill_queue (c->conn, qid, rx, 0);
613 if (err != MEMIF_ERR_SUCCESS)
614 INFO ("memif_buffer_free: %s", memif_strerror (err));
616 DBG ("freed %d buffers. %u/%u alloc/free buffers",
617 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
622 icmpr_memif_create (long index, long mode, char *s)
624 if (index >= MAX_CONNS)
626 INFO ("connection array overflow");
631 INFO ("don't even try...");
634 memif_connection_t *c = &memif_connection[index];
636 /* setting memif connection arguments */
637 memif_conn_args_t args;
639 memset (&args, 0, sizeof (args));
640 args.is_master = mode;
641 args.log2_ring_size = 11;
642 args.buffer_size = 2048;
643 args.num_s2m_rings = 1;
644 args.num_m2s_rings = 1;
645 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
647 /* socket filename is not specified, because this app is supposed to
648 connect to VPP over memif. so default socket filename will be used */
649 /* default socketfile = /run/vpp/memif.sock */
651 args.interface_id = index;
652 /* last argument for memif_create (void * private_ctx) is used by user
653 to identify connection. this context is returned with callbacks */
655 /* default interrupt */
658 err = memif_create (&c->conn,
659 &args, on_connect, on_disconnect, on_interrupt,
661 if (err != MEMIF_ERR_SUCCESS)
663 INFO ("memif_create: %s", memif_strerror (err));
669 if (strncmp (s, "0", 1) == 0)
671 err = memif_create (&c->conn,
672 &args, on_connect, on_disconnect, on_interrupt0,
674 if (err != MEMIF_ERR_SUCCESS)
676 INFO ("memif_create: %s", memif_strerror (err));
680 else if (strncmp (s, "1", 1) == 0)
682 err = memif_create (&c->conn,
683 &args, on_connect, on_disconnect, on_interrupt1,
685 if (err != MEMIF_ERR_SUCCESS)
687 INFO ("memif_create: %s", memif_strerror (err));
693 INFO ("Unknown interrupt descriptor");
699 /* alloc memif buffers */
702 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
705 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
709 c->ip_addr[2] = c->index + 1;
712 c->seq = c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
719 icmpr_memif_delete (long index)
721 if (index >= MAX_CONNS)
723 INFO ("connection array overflow");
728 INFO ("don't even try...");
731 memif_connection_t *c = &memif_connection[index];
743 /* disconenct then delete memif connection */
744 err = memif_delete (&c->conn);
745 if (err != MEMIF_ERR_SUCCESS)
746 INFO ("memif_delete: %s", memif_strerror (err));
748 INFO ("memif delete fail");
755 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
760 printf ("==============================\n");
761 printf ("libmemif version: %s", LIBMEMIF_VERSION);
766 printf ("memif version: %d\n", memif_get_version ());
767 printf ("commands:\n");
768 printf ("\thelp - prints this help\n");
769 printf ("\texit - exit app\n");
771 ("\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");
772 printf ("\tdel <index> - delete memif\n");
773 printf ("\tshow - show connection details\n");
774 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
776 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
777 printf ("\tsh-count - print counters\n");
778 printf ("\tcl-count - clear counters\n");
779 printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n");
785 /* application cleanup */
791 for (i = 0; i < MAX_CONNS; i++)
793 memif_connection_t *c = &memif_connection[i];
795 icmpr_memif_delete (i);
798 err = memif_cleanup ();
799 if (err != MEMIF_ERR_SUCCESS)
800 INFO ("memif_delete: %s", memif_strerror (err));
806 icmpr_set_ip (long index, char *ip)
808 if (index >= MAX_CONNS)
810 INFO ("connection array overflow");
815 INFO ("don't even try...");
818 memif_connection_t *c = &memif_connection[index];
821 INFO ("no connection at index %ld", index);
828 ui = strtok (ip, ".");
831 tmp[0] = strtol (ui, &end, 10);
833 ui = strtok (NULL, ".");
836 tmp[1] = strtol (ui, &end, 10);
838 ui = strtok (NULL, ".");
841 tmp[2] = strtol (ui, &end, 10);
843 ui = strtok (NULL, ".");
846 tmp[3] = strtol (ui, &end, 10);
848 c->ip_addr[0] = tmp[0];
849 c->ip_addr[1] = tmp[1];
850 c->ip_addr[2] = tmp[2];
851 c->ip_addr[3] = tmp[3];
853 INFO ("memif %ld ip address set to %u.%u.%u.%u",
854 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
859 INFO ("invalid ip address");
864 icmpr_set_rx_mode (long index, long qid, char *mode)
866 if (index >= MAX_CONNS)
868 INFO ("connection array overflow");
873 INFO ("don't even try...");
876 memif_connection_t *c = &memif_connection[index];
880 INFO ("no connection at index %ld", index);
884 if (strncmp (mode, "interrupt", 9) == 0)
886 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
889 else if (strncmp (mode, "polling", 7) == 0)
891 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
894 INFO ("expected rx mode <interrupt|polling>");
899 icmpr_print_counters ()
902 for (i = 0; i < MAX_CONNS; i++)
904 memif_connection_t *c = &memif_connection[i];
907 printf ("===============================\n");
908 printf ("interface index: %d\n", c->index);
909 printf ("\trx: %lu\n", c->rx_counter);
910 printf ("\ttx: %lu\n", c->tx_counter);
911 printf ("\ttx_err: %lu\n", c->tx_err_counter);
912 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
917 icmpr_reset_counters ()
920 for (i = 0; i < MAX_CONNS; i++)
922 memif_connection_t *c = &memif_connection[i];
925 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
931 icmpr_send_proc (void *data)
933 icmpr_thread_data_t *d = (icmpr_thread_data_t *) data;
934 int index = d->index;
935 uint64_t count = d->packet_num;
936 memif_connection_t *c = &memif_connection[index];
939 INFO ("No connection at index %d. Thread exiting...\n", index);
943 int err = MEMIF_ERR_SUCCESS;
945 struct timespec start, end;
946 memset (&start, 0, sizeof (start));
947 memset (&end, 0, sizeof (end));
949 timespec_get (&start, TIME_UTC);
953 err = memif_buffer_alloc (c->conn, 0, c->tx_bufs,
955 count ? count : MAX_MEMIF_BUFS, &tx, 128);
956 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
958 INFO ("memif_buffer_alloc: %s Thread exiting...\n",
959 memif_strerror (err));
968 generate_packet ((void *) c->tx_bufs[i].data,
969 &c->tx_bufs[i].len, c->ip_addr,
970 d->ip_daddr, d->hw_daddr, seq++);
971 generate_packet ((void *) c->tx_bufs[i + 1].data,
972 &c->tx_bufs[i + 1].len, c->ip_addr,
973 d->ip_daddr, d->hw_daddr, seq++);
977 generate_packet ((void *) c->tx_bufs[i].data,
978 &c->tx_bufs[i].len, c->ip_addr,
979 d->ip_daddr, d->hw_daddr, seq++);
983 err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
984 if (err != MEMIF_ERR_SUCCESS)
986 INFO ("memif_tx_burst: %s Thread exiting...\n",
987 memif_strerror (err));
994 timespec_get (&end, TIME_UTC);
995 INFO ("Pakcet sequence finished!");
996 INFO ("Seq len: %u", seq);
997 uint64_t t1 = end.tv_sec - start.tv_sec;
999 if (end.tv_nsec > start.tv_nsec)
1001 t2 = end.tv_nsec - start.tv_nsec;
1005 t2 = start.tv_nsec - end.tv_nsec;
1010 INFO ("Seq time: %lus %luns", t1, t2);
1014 INFO ("Average pps: %f", tmp);
1015 INFO ("Thread exiting...");
1016 pthread_exit (NULL);
1020 icmpr_send (long index, long packet_num, char *hw, char *ip)
1022 memif_connection_t *c = &memif_connection[index];
1026 if (c->conn == NULL)
1029 icmpr_thread_data_t *data = &icmpr_thread_data[index];
1030 data->index = index;
1031 data->packet_num = packet_num;
1032 INFO ("PN: %lu", data->packet_num);
1033 printf ("%s\n", ip);
1034 printf ("%s\n", hw);
1036 ui = strtok (ip, ".");
1039 tmp[0] = strtol (ui, &end, 10);
1041 ui = strtok (NULL, ".");
1044 tmp[1] = strtol (ui, &end, 10);
1046 ui = strtok (NULL, ".");
1049 tmp[2] = strtol (ui, &end, 10);
1051 ui = strtok (NULL, ".");
1054 tmp[3] = strtol (ui, &end, 10);
1056 data->ip_daddr[0] = tmp[0];
1057 data->ip_daddr[1] = tmp[1];
1058 data->ip_daddr[2] = tmp[2];
1059 data->ip_daddr[3] = tmp[3];
1061 ui = strtok (hw, ":");
1064 tmp[0] = strtol (ui, &end, 16);
1065 ui = strtok (NULL, ":");
1068 tmp[1] = strtol (ui, &end, 16);
1069 ui = strtok (NULL, ":");
1072 tmp[2] = strtol (ui, &end, 16);
1073 ui = strtok (NULL, ":");
1076 tmp[3] = strtol (ui, &end, 16);
1077 ui = strtok (NULL, ":");
1080 tmp[4] = strtol (ui, &end, 16);
1081 ui = strtok (NULL, ":");
1084 tmp[5] = strtol (ui, &end, 16);
1086 data->hw_daddr[0] = tmp[0];
1087 data->hw_daddr[1] = tmp[1];
1088 data->hw_daddr[2] = tmp[2];
1089 data->hw_daddr[3] = tmp[3];
1090 data->hw_daddr[4] = tmp[4];
1091 data->hw_daddr[5] = tmp[5];
1093 pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
1097 INFO ("Invalid input\n");
1102 user_input_handler ()
1105 char *in = (char *) malloc (256);
1106 char *ui = fgets (in, 256, stdin);
1111 ui = strtok (in, " ");
1112 if (strncmp (ui, "exit", 4) == 0)
1116 exit (EXIT_SUCCESS);
1118 else if (strncmp (ui, "help", 4) == 0)
1123 else if (strncmp (ui, "conn", 4) == 0)
1125 ui = strtok (NULL, " ");
1127 a = strtol (ui, &end, 10);
1130 INFO ("expected id");
1133 ui = strtok (NULL, " ");
1135 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1137 INFO ("expected mode <0|1>");
1140 else if (strncmp (ui, "del", 3) == 0)
1142 ui = strtok (NULL, " ");
1144 icmpr_memif_delete (strtol (ui, &end, 10));
1146 INFO ("expected id");
1149 else if (strncmp (ui, "show", 4) == 0)
1151 print_memif_details ();
1154 else if (strncmp (ui, "ip-set", 6) == 0)
1156 ui = strtok (NULL, " ");
1158 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1160 INFO ("expected id");
1163 else if (strncmp (ui, "rx-mode", 7) == 0)
1165 ui = strtok (NULL, " ");
1167 a = strtol (ui, &end, 10);
1170 INFO ("expected id");
1173 ui = strtok (NULL, " ");
1175 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1177 INFO ("expected qid");
1180 else if (strncmp (ui, "sh-count", 8) == 0)
1182 icmpr_print_counters ();
1184 else if (strncmp (ui, "cl-count", 8) == 0)
1186 icmpr_reset_counters ();
1188 else if (strncmp (ui, "send", 4) == 0)
1190 ui = strtok (NULL, " ");
1192 a = strtol (ui, &end, 10);
1195 INFO ("expected id");
1198 ui = strtok (NULL, " ");
1200 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
1201 strtok (NULL, " "));
1203 INFO ("expected count");
1208 INFO ("unknown command: %s", ui);
1219 poll_event (int timeout)
1221 struct epoll_event evt, *e;
1222 int app_err = 0, memif_err = 0, en = 0;
1224 uint32_t events = 0;
1225 struct timespec start, end;
1226 memset (&evt, 0, sizeof (evt));
1227 evt.events = EPOLLIN | EPOLLOUT;
1229 sigemptyset (&sigset);
1230 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1231 /* id event polled */
1232 timespec_get (&start, TIME_UTC);
1235 DBG ("epoll_pwait: %s", strerror (errno));
1240 /* this app does not use any other file descriptors than stds and memif control fds */
1241 if (evt.data.fd > 2)
1243 /* event of memif control fd */
1244 /* convert epolle events to memif events */
1245 if (evt.events & EPOLLIN)
1246 events |= MEMIF_FD_EVENT_READ;
1247 if (evt.events & EPOLLOUT)
1248 events |= MEMIF_FD_EVENT_WRITE;
1249 if (evt.events & EPOLLERR)
1250 events |= MEMIF_FD_EVENT_ERROR;
1251 memif_err = memif_control_fd_handler (evt.data.fd, events);
1252 if (memif_err != MEMIF_ERR_SUCCESS)
1253 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1255 else if (evt.data.fd == 0)
1257 app_err = user_input_handler ();
1261 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1265 timespec_get (&end, TIME_UTC);
1266 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1268 if ((app_err < 0) || (memif_err < 0))
1271 DBG ("user input handler error");
1273 DBG ("memif control fd handler error");
1283 epfd = epoll_create (1);
1284 add_epoll_fd (0, EPOLLIN);
1290 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1292 INFO ("Error opening log file: %s", strerror (errno));
1293 #endif /* LOG_FILE */
1295 /* initialize memory interface */
1297 /* if valid callback is passed as argument, fd event polling will be done by user
1298 all file descriptors and events will be passed to user in this callback */
1299 /* if callback is set to NULL libmemif will handle fd event polling */
1300 err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
1301 if (err != MEMIF_ERR_SUCCESS)
1303 INFO ("memif_init: %s", memif_strerror (err));
1308 for (i = 0; i < MAX_CONNS; i++)
1310 memset (&memif_connection[i], 0, sizeof (memif_connection_t));
1319 if (poll_event (-1) < 0)
1321 DBG ("poll_event error!");