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>
51 #include <icmp_proto.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__); \
81 /* maximum tx/rx memif buffers */
82 #define MAX_MEMIF_BUFS 256
92 /* memif conenction handle */
93 memif_conn_handle_t conn;
95 memif_buffer_t *tx_bufs;
96 /* allocated tx buffers counter */
97 /* number of tx buffers pointing to shared memory */
100 memif_buffer_t *rx_bufs;
101 /* allcoated rx buffers counter */
102 /* number of rx buffers pointing to shared memory */
104 /* interface ip address */
107 uint64_t tx_counter, rx_counter, tx_err_counter;
108 uint64_t t_sec, t_nsec;
109 } memif_connection_t;
117 } icmpr_thread_data_t;
119 memif_connection_t memif_connection[MAX_CONNS];
120 icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
121 pthread_t thread[MAX_CONNS];
124 /* print details for all memif connections */
126 print_memif_details ()
133 buf = malloc (buflen);
134 printf ("MEMIF DETAILS\n");
135 printf ("==============================\n");
136 for (i = 0; i < MAX_CONNS; i++)
138 memif_connection_t *c = &memif_connection[i];
140 memset (&md, 0, sizeof (md));
141 memset (buf, 0, buflen);
143 err = memif_get_details (c->conn, &md, buf, buflen);
144 if (err != MEMIF_ERR_SUCCESS)
146 if (err != MEMIF_ERR_NOCONN)
147 INFO ("%s", memif_strerror (err));
151 printf ("interface index: %d\n", i);
153 printf ("\tinterface ip: %u.%u.%u.%u\n",
154 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
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);
158 printf ("\tremote app name: %s\n", (char *) md.remote_inst_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");
182 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
183 printf ("\trx queues:\n");
184 for (e = 0; e < md.rx_queues_num; e++)
186 printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
187 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
188 printf ("\t\tring rx mode: %s\n",
189 md.rx_queues[e].flags ? "polling" : "interrupt");
190 printf ("\t\tring head: %u\n", md.rx_queues[e].head);
191 printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
192 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
194 printf ("\ttx queues:\n");
195 for (e = 0; e < md.tx_queues_num; e++)
197 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
198 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
199 printf ("\t\tring rx mode: %s\n",
200 md.tx_queues[e].flags ? "polling" : "interrupt");
201 printf ("\t\tring head: %u\n", md.tx_queues[e].head);
202 printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
203 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
215 add_epoll_fd (int fd, uint32_t events)
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);
236 mod_epoll_fd (int fd, uint32_t events)
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);
257 del_epoll_fd (int 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);
275 /* informs user about connected status. private_ctx is used by user to identify connection
276 (multiple connections WIP) */
278 on_connect (memif_conn_handle_t conn, void *private_ctx)
280 INFO ("memif connected!");
285 /* informs user about disconnected status. private_ctx is used by user to identify connection
286 (multiple connections WIP) */
288 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
290 INFO ("memif disconnected!");
294 /* user needs to watch new fd or stop watching fd that is about to be closed.
295 control fd will be modified during connection establishment to minimize CPU usage */
297 control_fd_update (int fd, uint8_t events)
299 /* convert memif event definitions to epoll events */
300 if (events & MEMIF_FD_EVENT_DEL)
301 return del_epoll_fd (fd);
304 if (events & MEMIF_FD_EVENT_READ)
306 if (events & MEMIF_FD_EVENT_WRITE)
309 if (events & MEMIF_FD_EVENT_MOD)
310 return mod_epoll_fd (fd, evt);
312 return add_epoll_fd (fd, evt);
316 icmpr_buffer_alloc (long index, long n, uint16_t * r, uint16_t i,
319 memif_connection_t *c = &memif_connection[index];
321 /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
322 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 0);
323 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
325 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
329 DBG ("allocated %d/%ld buffers, %u free buffers", *r, n,
330 MAX_MEMIF_BUFS - c->tx_buf_num);
335 icmpr_tx_burst (long index, uint16_t qid)
337 memif_connection_t *c = &memif_connection[index];
340 /* inform peer memif interface about data in shared memory buffers */
341 /* mark memif buffers as free */
342 err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
343 if (err != MEMIF_ERR_SUCCESS)
344 INFO ("memif_tx_burst: %s", memif_strerror (err));
350 /* called when event is polled on interrupt file descriptor.
351 there are packets in shared memory ready to be received */
353 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
355 long index = *((long *) private_ctx);
356 memif_connection_t *c = &memif_connection[index];
357 if (c->index != index)
359 INFO ("invalid context: %ld/%u", index, c->index);
363 int err = MEMIF_ERR_SUCCESS, ret_val;
366 int i; /* rx buffer iterator */
367 int j; /* tx bufferiterator */
369 /* loop while there are packets in shm */
372 /* receive data from shared memory buffers */
373 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
376 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
378 INFO ("memif_rx_burst: %s", memif_strerror (err));
384 /* try to alloc required number of buffers buffers */
385 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
386 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
388 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
392 c->tx_err_counter = rx - tx;
399 resolve_packet ((void *) (c->rx_bufs + i)->data,
400 (c->rx_bufs + i)->data_len,
401 (void *) (c->tx_bufs + j)->data,
402 &(c->tx_bufs + j)->data_len, c->ip_addr);
403 resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
404 (c->rx_bufs + i + 1)->data_len,
405 (void *) (c->tx_bufs + j + 1)->data,
406 &(c->tx_bufs + j + 1)->data_len, c->ip_addr);
412 resolve_packet ((void *) (c->rx_bufs + i)->data,
413 (c->rx_bufs + i)->data_len,
414 (void *) (c->tx_bufs + j)->data,
415 &(c->tx_bufs + j)->data_len, c->ip_addr);
420 /* mark memif buffers and shared memory buffers as free */
421 /* free processed buffers */
422 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
423 if (err != MEMIF_ERR_SUCCESS)
424 INFO ("memif_buffer_free: %s", memif_strerror (err));
427 DBG ("freed %d buffers. %u/%u alloc/free buffers",
428 fb, rx, MAX_MEMIF_BUFS - rx);
430 /* transmit allocated buffers */
431 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
432 if (err != MEMIF_ERR_SUCCESS)
434 INFO ("memif_tx_burst: %s", memif_strerror (err));
440 while (ret_val == MEMIF_ERR_NOBUF);
445 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
446 if (err != MEMIF_ERR_SUCCESS)
447 INFO ("memif_buffer_free: %s", memif_strerror (err));
449 DBG ("freed %d buffers. %u/%u alloc/free buffers",
450 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
454 /* called when event is polled on interrupt file descriptor.
455 there are packets in shared memory ready to be received */
456 /* dev test modification: loop until TX == RX (don't drop buffers) */
458 on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
460 long index = *((long *) private_ctx);
461 memif_connection_t *c = &memif_connection[index];
462 if (c->index != index)
464 INFO ("invalid context: %ld/%u", index, c->index);
468 int err = MEMIF_ERR_SUCCESS, ret_val;
471 int i; /* rx buffer iterator */
472 int j; /* tx bufferiterator */
473 int prev_i; /* first allocated rx buffer */
475 /* loop while there are packets in shm */
478 /* receive data from shared memory buffers */
479 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
482 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
484 INFO ("memif_rx_burst: %s", memif_strerror (err));
490 /* loop while there are RX buffers to be processed */
494 /* try to alloc required number of buffers buffers */
495 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
496 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
498 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
508 resolve_packet ((void *) (c->rx_bufs + i)->data,
509 (c->rx_bufs + i)->data_len,
510 (void *) (c->tx_bufs + j)->data,
511 &(c->tx_bufs + j)->data_len, c->ip_addr);
512 resolve_packet ((void *) (c->rx_bufs + i + 1)->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,
522 resolve_packet ((void *) (c->rx_bufs + i)->data,
523 (c->rx_bufs + i)->data_len,
524 (void *) (c->tx_bufs + j)->data,
525 &(c->tx_bufs + j)->data_len, c->ip_addr);
530 /* mark memif buffers and shared memory buffers as free */
531 /* free processed buffers */
532 err = memif_buffer_free (c->conn, qid, c->rx_bufs + prev_i, j, &fb);
533 if (err != MEMIF_ERR_SUCCESS)
534 INFO ("memif_buffer_free: %s", memif_strerror (err));
538 DBG ("freed %d buffers. %u/%u alloc/free buffers",
539 fb, rx, MAX_MEMIF_BUFS - rx);
541 /* transmit allocated buffers */
542 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
543 if (err != MEMIF_ERR_SUCCESS)
545 INFO ("memif_tx_burst: %s", memif_strerror (err));
553 while (ret_val == MEMIF_ERR_NOBUF);
558 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
559 if (err != MEMIF_ERR_SUCCESS)
560 INFO ("memif_buffer_free: %s", memif_strerror (err));
562 DBG ("freed %d buffers. %u/%u alloc/free buffers",
563 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
567 /* called when event is polled on interrupt file descriptor.
568 there are packets in shared memory ready to be received */
569 /* dev test modification: handle only ARP requests */
571 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
573 long index = *((long *) private_ctx);
574 memif_connection_t *c = &memif_connection[index];
575 if (c->index != index)
577 INFO ("invalid context: %ld/%u", index, c->index);
581 int err = MEMIF_ERR_SUCCESS, ret_val;
589 /* receive data from shared memory buffers */
590 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
592 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
594 INFO ("memif_rx_burst: %s", memif_strerror (err));
600 for (i = 0; i < rx; i++)
602 if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
605 if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
607 resolve_packet ((void *) (c->rx_bufs + i)->data,
608 (c->rx_bufs + i)->data_len,
609 (void *) (c->tx_bufs + i)->data,
610 &(c->tx_bufs + i)->data_len, c->ip_addr);
611 icmpr_tx_burst (c->index, qid);
615 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
616 if (err != MEMIF_ERR_SUCCESS)
617 INFO ("memif_buffer_free: %s", memif_strerror (err));
622 while (ret_val == MEMIF_ERR_NOBUF);
627 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
628 if (err != MEMIF_ERR_SUCCESS)
629 INFO ("memif_buffer_free: %s", memif_strerror (err));
631 DBG ("freed %d buffers. %u/%u alloc/free buffers",
632 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
637 icmpr_memif_create (long index, long mode, char *s)
639 if (index >= MAX_CONNS)
641 INFO ("connection array overflow");
646 INFO ("don't even try...");
649 memif_connection_t *c = &memif_connection[index];
651 /* setting memif connection arguments */
652 memif_conn_args_t args;
654 memset (&args, 0, sizeof (args));
655 args.is_master = mode;
656 args.log2_ring_size = 11;
657 args.buffer_size = 2048;
658 args.num_s2m_rings = 2;
659 args.num_m2s_rings = 2;
660 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
661 strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
663 /* socket filename is not specified, because this app is supposed to
664 connect to VPP over memif. so default socket filename will be used */
665 /* default socketfile = /run/vpp/memif.sock */
667 args.interface_id = index;
668 /* last argument for memif_create (void * private_ctx) is used by user
669 to identify connection. this context is returned with callbacks */
671 /* default interrupt */
674 err = memif_create (&c->conn,
675 &args, on_connect, on_disconnect, on_interrupt,
677 if (err != MEMIF_ERR_SUCCESS)
679 INFO ("memif_create: %s", memif_strerror (err));
685 if (strncmp (s, "0", 1) == 0)
687 err = memif_create (&c->conn,
688 &args, on_connect, on_disconnect, on_interrupt0,
690 if (err != MEMIF_ERR_SUCCESS)
692 INFO ("memif_create: %s", memif_strerror (err));
696 else if (strncmp (s, "1", 1) == 0)
698 err = memif_create (&c->conn,
699 &args, on_connect, on_disconnect, on_interrupt1,
701 if (err != MEMIF_ERR_SUCCESS)
703 INFO ("memif_create: %s", memif_strerror (err));
709 INFO ("Unknown interrupt descriptor");
715 /* alloc memif buffers */
718 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
721 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
725 c->ip_addr[2] = c->index + 1;
728 c->seq = c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
735 icmpr_memif_delete (long index)
737 if (index >= MAX_CONNS)
739 INFO ("connection array overflow");
744 INFO ("don't even try...");
747 memif_connection_t *c = &memif_connection[index];
759 /* disconenct then delete memif connection */
760 err = memif_delete (&c->conn);
761 if (err != MEMIF_ERR_SUCCESS)
762 INFO ("memif_delete: %s", memif_strerror (err));
764 INFO ("memif delete fail");
771 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
776 printf ("==============================\n");
777 printf ("libmemif version: %s", LIBMEMIF_VERSION);
782 printf ("memif version: %d\n", MEMIF_VERSION);
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");
801 /* application cleanup */
807 for (i = 0; i < MAX_CONNS; i++)
809 memif_connection_t *c = &memif_connection[i];
811 icmpr_memif_delete (i);
814 err = memif_cleanup ();
815 if (err != MEMIF_ERR_SUCCESS)
816 INFO ("memif_delete: %s", memif_strerror (err));
822 icmpr_set_ip (long index, char *ip)
824 if (index >= MAX_CONNS)
826 INFO ("connection array overflow");
831 INFO ("don't even try...");
834 memif_connection_t *c = &memif_connection[index];
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);
864 c->ip_addr[0] = tmp[0];
865 c->ip_addr[1] = tmp[1];
866 c->ip_addr[2] = tmp[2];
867 c->ip_addr[3] = tmp[3];
869 INFO ("memif %ld ip address set to %u.%u.%u.%u",
870 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
875 INFO ("invalid ip address");
880 icmpr_set_rx_mode (long index, long qid, char *mode)
882 if (index >= MAX_CONNS)
884 INFO ("connection array overflow");
889 INFO ("don't even try...");
892 memif_connection_t *c = &memif_connection[index];
896 INFO ("no connection at index %ld", index);
900 if (strncmp (mode, "interrupt", 9) == 0)
902 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
905 else if (strncmp (mode, "polling", 7) == 0)
907 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
910 INFO ("expected rx mode <interrupt|polling>");
915 icmpr_print_counters ()
918 for (i = 0; i < MAX_CONNS; i++)
920 memif_connection_t *c = &memif_connection[i];
923 printf ("===============================\n");
924 printf ("interface index: %d\n", c->index);
925 printf ("\trx: %lu\n", c->rx_counter);
926 printf ("\ttx: %lu\n", c->tx_counter);
927 printf ("\ttx_err: %lu\n", c->tx_err_counter);
928 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
933 icmpr_reset_counters ()
936 for (i = 0; i < MAX_CONNS; i++)
938 memif_connection_t *c = &memif_connection[i];
941 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
947 icmpr_send_proc (void *data)
949 icmpr_thread_data_t *d = (icmpr_thread_data_t *) data;
950 int index = d->index;
951 uint64_t count = d->packet_num;
952 memif_connection_t *c = &memif_connection[index];
955 INFO ("No connection at index %d. Thread exiting...\n", index);
959 int err = MEMIF_ERR_SUCCESS;
961 struct timespec start, end;
962 memset (&start, 0, sizeof (start));
963 memset (&end, 0, sizeof (end));
965 timespec_get (&start, TIME_UTC);
969 err = memif_buffer_alloc (c->conn, 0, c->tx_bufs,
971 count ? count : MAX_MEMIF_BUFS, &tx, 0);
972 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
974 INFO ("memif_buffer_alloc: %s Thread exiting...\n",
975 memif_strerror (err));
984 generate_packet ((void *) c->tx_bufs[i].data,
985 &c->tx_bufs[i].data_len, c->ip_addr,
986 d->ip_daddr, d->hw_daddr, seq++);
987 generate_packet ((void *) c->tx_bufs[i + 1].data,
988 &c->tx_bufs[i + 1].data_len, c->ip_addr,
989 d->ip_daddr, d->hw_daddr, seq++);
993 generate_packet ((void *) c->tx_bufs[i].data,
994 &c->tx_bufs[i].data_len, c->ip_addr,
995 d->ip_daddr, d->hw_daddr, seq++);
999 err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
1000 if (err != MEMIF_ERR_SUCCESS)
1002 INFO ("memif_tx_burst: %s Thread exiting...\n",
1003 memif_strerror (err));
1004 pthread_exit (NULL);
1006 c->tx_buf_num -= tx;
1007 c->tx_counter += tx;
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);
1036 icmpr_send (long index, long packet_num, char *hw, char *ip)
1038 memif_connection_t *c = &memif_connection[index];
1042 if (c->conn == NULL)
1045 icmpr_thread_data_t *data = &icmpr_thread_data[index];
1046 data->index = index;
1047 data->packet_num = packet_num;
1048 INFO ("PN: %lu", data->packet_num);
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);
1072 data->ip_daddr[0] = tmp[0];
1073 data->ip_daddr[1] = tmp[1];
1074 data->ip_daddr[2] = tmp[2];
1075 data->ip_daddr[3] = tmp[3];
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);
1102 data->hw_daddr[0] = tmp[0];
1103 data->hw_daddr[1] = tmp[1];
1104 data->hw_daddr[2] = tmp[2];
1105 data->hw_daddr[3] = tmp[3];
1106 data->hw_daddr[4] = tmp[4];
1107 data->hw_daddr[5] = tmp[5];
1109 pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
1113 INFO ("Invalid input\n");
1118 user_input_handler ()
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, " ");
1151 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1153 INFO ("expected mode <0|1>");
1156 else if (strncmp (ui, "del", 3) == 0)
1158 ui = strtok (NULL, " ");
1160 icmpr_memif_delete (strtol (ui, &end, 10));
1162 INFO ("expected id");
1165 else if (strncmp (ui, "show", 4) == 0)
1167 print_memif_details ();
1170 else if (strncmp (ui, "ip-set", 6) == 0)
1172 ui = strtok (NULL, " ");
1174 icmpr_set_ip (strtol (ui, &end, 10), 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, " ");
1191 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1193 INFO ("expected qid");
1196 else if (strncmp (ui, "sh-count", 8) == 0)
1198 icmpr_print_counters ();
1200 else if (strncmp (ui, "cl-count", 8) == 0)
1202 icmpr_reset_counters ();
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, " ");
1216 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
1217 strtok (NULL, " "));
1219 INFO ("expected count");
1224 INFO ("unknown command: %s", ui);
1235 poll_event (int timeout)
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);
1247 /* id event polled */
1248 timespec_get (&start, TIME_UTC);
1251 DBG ("epoll_pwait: %s", strerror (errno));
1256 /* this app does not use any other file descriptors than stds and memif control fds */
1257 if (evt.data.fd > 2)
1259 /* event of memif control fd */
1260 /* convert epolle events to memif events */
1261 if (evt.events & EPOLLIN)
1262 events |= MEMIF_FD_EVENT_READ;
1263 if (evt.events & EPOLLOUT)
1264 events |= MEMIF_FD_EVENT_WRITE;
1265 if (evt.events & EPOLLERR)
1266 events |= MEMIF_FD_EVENT_ERROR;
1267 memif_err = memif_control_fd_handler (evt.data.fd, events);
1268 if (memif_err != MEMIF_ERR_SUCCESS)
1269 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1271 else if (evt.data.fd == 0)
1273 app_err = user_input_handler ();
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);
1300 add_epoll_fd (0, EPOLLIN);
1306 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1308 INFO ("Error opening log file: %s", strerror (errno));
1309 #endif /* LOG_FILE */
1311 /* initialize memory interface */
1313 /* if valid callback is passed as argument, fd event polling will be done by user
1314 all file descriptors and events will be passed to user in this callback */
1315 /* if callback is set to NULL libmemif will handle fd event polling */
1316 err = memif_init (control_fd_update, APP_NAME);
1317 if (err != MEMIF_ERR_SUCCESS)
1318 INFO ("memif_init: %s", memif_strerror (err));
1320 for (i = 0; i < MAX_CONNS; i++)
1322 memif_connection[i].conn = NULL;
1331 if (poll_event (-1) < 0)
1333 DBG ("poll_event error!");