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
89 #define ICMPR_HEADROOM 64
98 /* memif conenction handle */
99 memif_conn_handle_t conn;
101 memif_buffer_t *tx_bufs;
102 /* allocated tx buffers counter */
103 /* number of tx buffers pointing to shared memory */
106 memif_buffer_t *rx_bufs;
107 /* allcoated rx buffers counter */
108 /* number of rx buffers pointing to shared memory */
110 /* interface ip address */
113 uint64_t tx_counter, rx_counter, tx_err_counter;
114 uint64_t t_sec, t_nsec;
115 } memif_connection_t;
123 } icmpr_thread_data_t;
125 memif_connection_t memif_connection[MAX_CONNS];
126 icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
127 pthread_t thread[MAX_CONNS];
130 /* print details for all memif connections */
132 print_memif_details ()
139 buf = malloc (buflen);
140 printf ("MEMIF DETAILS\n");
141 printf ("==============================\n");
142 for (i = 0; i < MAX_CONNS; i++)
144 memif_connection_t *c = &memif_connection[i];
146 memset (&md, 0, sizeof (md));
147 memset (buf, 0, buflen);
149 err = memif_get_details (c->conn, &md, buf, buflen);
150 if (err != MEMIF_ERR_SUCCESS)
152 if (err != MEMIF_ERR_NOCONN)
153 INFO ("%s", memif_strerror (err));
157 printf ("interface index: %d\n", i);
159 printf ("\tinterface ip: %u.%u.%u.%u\n",
160 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
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);
164 printf ("\tremote app name: %s\n", (char *) md.remote_inst_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");
188 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
189 printf ("\trx queues:\n");
190 for (e = 0; e < md.rx_queues_num; e++)
192 printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
193 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
194 printf ("\t\tring rx mode: %s\n",
195 md.rx_queues[e].flags ? "polling" : "interrupt");
196 printf ("\t\tring head: %u\n", md.rx_queues[e].head);
197 printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
198 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
200 printf ("\ttx queues:\n");
201 for (e = 0; e < md.tx_queues_num; e++)
203 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
204 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
205 printf ("\t\tring rx mode: %s\n",
206 md.tx_queues[e].flags ? "polling" : "interrupt");
207 printf ("\t\tring head: %u\n", md.tx_queues[e].head);
208 printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
209 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
217 printf ("\treason: %s\n", md.error);
224 add_epoll_fd (int fd, uint32_t events)
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);
245 mod_epoll_fd (int fd, uint32_t events)
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);
266 del_epoll_fd (int 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);
284 /* informs user about connected status. private_ctx is used by user to identify connection
285 (multiple connections WIP) */
287 on_connect (memif_conn_handle_t conn, void *private_ctx)
289 INFO ("memif connected!");
290 memif_refill_queue (conn, 0, -1, ICMPR_HEADROOM);
295 /* informs user about disconnected status. private_ctx is used by user to identify connection
296 (multiple connections WIP) */
298 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
300 INFO ("memif disconnected!");
304 /* user needs to watch new fd or stop watching fd that is about to be closed.
305 control fd will be modified during connection establishment to minimize CPU usage */
307 control_fd_update (int fd, uint8_t events, void *ctx)
309 /* convert memif event definitions to epoll events */
310 if (events & MEMIF_FD_EVENT_DEL)
311 return del_epoll_fd (fd);
314 if (events & MEMIF_FD_EVENT_READ)
316 if (events & MEMIF_FD_EVENT_WRITE)
319 if (events & MEMIF_FD_EVENT_MOD)
320 return mod_epoll_fd (fd, evt);
322 return add_epoll_fd (fd, evt);
326 icmpr_buffer_alloc (long index, long n, uint16_t * r, uint16_t i,
329 memif_connection_t *c = &memif_connection[index];
331 /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
332 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 128);
333 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
335 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
339 DBG ("allocated %d/%ld buffers, %u free buffers", *r, n,
340 MAX_MEMIF_BUFS - c->tx_buf_num);
345 icmpr_tx_burst (long index, uint16_t qid)
347 memif_connection_t *c = &memif_connection[index];
350 /* inform peer memif interface about data in shared memory buffers */
351 /* mark memif buffers as free */
352 err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
353 if (err != MEMIF_ERR_SUCCESS)
354 INFO ("memif_tx_burst: %s", memif_strerror (err));
360 /* called when event is polled on interrupt file descriptor.
361 there are packets in shared memory ready to be received */
363 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
365 long index = *((long *) private_ctx);
366 memif_connection_t *c = &memif_connection[index];
367 if (c->index != index)
369 INFO ("invalid context: %ld/%u", index, c->index);
373 int err = MEMIF_ERR_SUCCESS, ret_val;
374 uint16_t rx = 0, tx = 0;
375 int i = 0; /* rx buffer iterator */
376 int j = 0; /* tx bufferiterator */
378 /* loop while there are packets in shm */
381 /* receive data from shared memory buffers */
382 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
385 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
387 INFO ("memif_rx_burst: %s", memif_strerror (err));
392 memset (c->tx_bufs, 0, sizeof (memif_buffer_t) * rx);
393 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
394 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
396 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
400 c->tx_err_counter += rx - tx;
404 resolve_packet ((void *) (c->rx_bufs + i)->data,
405 (c->rx_bufs + i)->len,
406 (void *) (c->tx_bufs + j)->data,
407 &(c->tx_bufs + j)->len, c->ip_addr);
413 err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
414 if (err != MEMIF_ERR_SUCCESS)
415 INFO ("memif_buffer_free: %s", memif_strerror (err));
418 DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
420 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
421 if (err != MEMIF_ERR_SUCCESS)
423 INFO ("memif_tx_burst: %s", memif_strerror (err));
429 while (ret_val == MEMIF_ERR_NOBUF);
434 err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
435 if (err != MEMIF_ERR_SUCCESS)
436 INFO ("memif_buffer_free: %s", memif_strerror (err));
438 DBG ("freed %d buffers. %u/%u alloc/free buffers",
439 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
443 /* called when event is polled on interrupt file descriptor.
444 there are packets in shared memory ready to be received */
445 /* dev test modification: loop until TX == RX (don't drop buffers) */
447 on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
449 long index = *((long *) private_ctx);
450 memif_connection_t *c = &memif_connection[index];
451 if (c->index != index)
453 INFO ("invalid context: %ld/%u", index, c->index);
457 int err = MEMIF_ERR_SUCCESS, ret_val;
458 uint16_t rx = 0, tx = 0;
459 int i; /* rx buffer iterator */
460 int j; /* tx bufferiterator */
462 /* loop while there are packets in shm */
465 /* receive data from shared memory buffers */
466 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
469 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
471 INFO ("memif_rx_burst: %s", memif_strerror (err));
477 /* loop while there are RX buffers to be processed */
481 /* try to alloc required number of buffers buffers */
482 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
483 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
485 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
495 resolve_packet ((void *) (c->rx_bufs + i)->data,
496 (c->rx_bufs + i)->len,
497 (void *) (c->tx_bufs + j)->data,
498 &(c->tx_bufs + j)->len, c->ip_addr);
499 resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
500 (c->rx_bufs + i + 1)->len,
501 (void *) (c->tx_bufs + j + 1)->data,
502 &(c->tx_bufs + j + 1)->len, c->ip_addr);
508 resolve_packet ((void *) (c->rx_bufs + i)->data,
509 (c->rx_bufs + i)->len,
510 (void *) (c->tx_bufs + j)->data,
511 &(c->tx_bufs + j)->len, c->ip_addr);
516 /* mark memif buffers and shared memory buffers as free */
517 /* free processed buffers */
518 err = memif_refill_queue (c->conn, qid, j, ICMPR_HEADROOM);
519 if (err != MEMIF_ERR_SUCCESS)
520 INFO ("memif_buffer_free: %s", memif_strerror (err));
523 DBG ("freed %d buffers. %u/%u alloc/free buffers",
524 rx, 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, ICMPR_HEADROOM);
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 rx, 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;
572 /* receive data from shared memory buffers */
573 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
575 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
577 INFO ("memif_rx_burst: %s", memif_strerror (err));
583 for (i = 0; i < rx; i++)
585 if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
588 if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
590 resolve_packet ((void *) (c->rx_bufs + i)->data,
591 (c->rx_bufs + i)->len,
592 (void *) (c->tx_bufs + i)->data,
593 &(c->tx_bufs + i)->len, c->ip_addr);
594 icmpr_tx_burst (c->index, qid);
598 err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
599 if (err != MEMIF_ERR_SUCCESS)
600 INFO ("memif_buffer_free: %s", memif_strerror (err));
605 while (ret_val == MEMIF_ERR_NOBUF);
610 err = memif_refill_queue (c->conn, qid, rx, ICMPR_HEADROOM);
611 if (err != MEMIF_ERR_SUCCESS)
612 INFO ("memif_buffer_free: %s", memif_strerror (err));
614 DBG ("freed %d buffers. %u/%u alloc/free buffers",
615 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
620 icmpr_memif_create (long index, long mode, char *s)
622 if (index >= MAX_CONNS)
624 INFO ("connection array overflow");
629 INFO ("don't even try...");
632 memif_connection_t *c = &memif_connection[index];
634 /* setting memif connection arguments */
635 memif_conn_args_t args;
636 memset (&args, 0, sizeof (args));
637 args.is_master = mode;
638 args.log2_ring_size = 11;
639 args.buffer_size = 2048;
640 args.num_s2m_rings = 1;
641 args.num_m2s_rings = 1;
642 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
644 /* socket filename is not specified, because this app is supposed to
645 connect to VPP over memif. so default socket filename will be used */
646 /* default socketfile = /run/vpp/memif.sock */
648 args.interface_id = index;
649 /* last argument for memif_create (void * private_ctx) is used by user
650 to identify connection. this context is returned with callbacks */
652 /* default interrupt */
655 err = memif_create (&c->conn,
656 &args, on_connect, on_disconnect, on_interrupt,
658 if (err != MEMIF_ERR_SUCCESS)
660 INFO ("memif_create: %s", memif_strerror (err));
666 if (strncmp (s, "0", 1) == 0)
668 err = memif_create (&c->conn,
669 &args, on_connect, on_disconnect, on_interrupt0,
671 if (err != MEMIF_ERR_SUCCESS)
673 INFO ("memif_create: %s", memif_strerror (err));
677 else if (strncmp (s, "1", 1) == 0)
679 err = memif_create (&c->conn,
680 &args, on_connect, on_disconnect, on_interrupt1,
682 if (err != MEMIF_ERR_SUCCESS)
684 INFO ("memif_create: %s", memif_strerror (err));
690 INFO ("Unknown interrupt descriptor");
696 /* alloc memif buffers */
699 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
702 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
706 c->ip_addr[2] = c->index + 1;
709 c->seq = c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
716 icmpr_memif_delete (long index)
718 if (index >= MAX_CONNS)
720 INFO ("connection array overflow");
725 INFO ("don't even try...");
728 memif_connection_t *c = &memif_connection[index];
740 /* disconenct then delete memif connection */
741 err = memif_delete (&c->conn);
742 if (err != MEMIF_ERR_SUCCESS)
743 INFO ("memif_delete: %s", memif_strerror (err));
745 INFO ("memif delete fail");
752 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
757 printf ("==============================\n");
758 printf ("libmemif version: %s", LIBMEMIF_VERSION);
763 printf ("memif version: %d\n", memif_get_version ());
764 printf ("commands:\n");
765 printf ("\thelp - prints this help\n");
766 printf ("\texit - exit app\n");
768 ("\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");
769 printf ("\tdel <index> - delete memif\n");
770 printf ("\tshow - show connection details\n");
771 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
773 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
774 printf ("\tsh-count - print counters\n");
775 printf ("\tcl-count - clear counters\n");
776 printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n");
782 /* application cleanup */
788 for (i = 0; i < MAX_CONNS; i++)
790 memif_connection_t *c = &memif_connection[i];
792 icmpr_memif_delete (i);
795 err = memif_cleanup ();
796 if (err != MEMIF_ERR_SUCCESS)
797 INFO ("memif_delete: %s", memif_strerror (err));
803 icmpr_set_ip (long index, char *ip)
805 if (index >= MAX_CONNS)
807 INFO ("connection array overflow");
812 INFO ("don't even try...");
815 memif_connection_t *c = &memif_connection[index];
818 INFO ("no connection at index %ld", index);
825 ui = strtok (ip, ".");
828 tmp[0] = strtol (ui, &end, 10);
830 ui = strtok (NULL, ".");
833 tmp[1] = strtol (ui, &end, 10);
835 ui = strtok (NULL, ".");
838 tmp[2] = strtol (ui, &end, 10);
840 ui = strtok (NULL, ".");
843 tmp[3] = strtol (ui, &end, 10);
845 c->ip_addr[0] = tmp[0];
846 c->ip_addr[1] = tmp[1];
847 c->ip_addr[2] = tmp[2];
848 c->ip_addr[3] = tmp[3];
850 INFO ("memif %ld ip address set to %u.%u.%u.%u",
851 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
856 INFO ("invalid ip address");
861 icmpr_set_rx_mode (long index, long qid, char *mode)
863 if (index >= MAX_CONNS)
865 INFO ("connection array overflow");
870 INFO ("don't even try...");
873 memif_connection_t *c = &memif_connection[index];
877 INFO ("no connection at index %ld", index);
881 if (strncmp (mode, "interrupt", 9) == 0)
883 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
886 else if (strncmp (mode, "polling", 7) == 0)
888 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
891 INFO ("expected rx mode <interrupt|polling>");
896 icmpr_print_counters ()
899 for (i = 0; i < MAX_CONNS; i++)
901 memif_connection_t *c = &memif_connection[i];
904 printf ("===============================\n");
905 printf ("interface index: %d\n", c->index);
906 printf ("\trx: %lu\n", c->rx_counter);
907 printf ("\ttx: %lu\n", c->tx_counter);
908 printf ("\ttx_err: %lu\n", c->tx_err_counter);
909 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
914 icmpr_reset_counters ()
917 for (i = 0; i < MAX_CONNS; i++)
919 memif_connection_t *c = &memif_connection[i];
922 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
928 icmpr_send_proc (void *data)
930 icmpr_thread_data_t *d = (icmpr_thread_data_t *) data;
931 int index = d->index;
932 uint64_t count = d->packet_num;
933 memif_connection_t *c = &memif_connection[index];
936 INFO ("No connection at index %d. Thread exiting...\n", index);
940 int err = MEMIF_ERR_SUCCESS;
942 struct timespec start, end;
943 memset (&start, 0, sizeof (start));
944 memset (&end, 0, sizeof (end));
946 timespec_get (&start, TIME_UTC);
950 err = memif_buffer_alloc (c->conn, 0, c->tx_bufs,
952 count ? count : MAX_MEMIF_BUFS, &tx, 128);
953 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
955 INFO ("memif_buffer_alloc: %s Thread exiting...\n",
956 memif_strerror (err));
965 generate_packet ((void *) c->tx_bufs[i].data,
966 &c->tx_bufs[i].len, c->ip_addr,
967 d->ip_daddr, d->hw_daddr, seq++);
968 generate_packet ((void *) c->tx_bufs[i + 1].data,
969 &c->tx_bufs[i + 1].len, c->ip_addr,
970 d->ip_daddr, d->hw_daddr, seq++);
974 generate_packet ((void *) c->tx_bufs[i].data,
975 &c->tx_bufs[i].len, c->ip_addr,
976 d->ip_daddr, d->hw_daddr, seq++);
980 err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
981 if (err != MEMIF_ERR_SUCCESS)
983 INFO ("memif_tx_burst: %s Thread exiting...\n",
984 memif_strerror (err));
991 timespec_get (&end, TIME_UTC);
993 INFO ("Pakcet sequence finished!");
994 INFO ("Seq len: %u", seq);
995 uint64_t t1 = end.tv_sec - start.tv_sec;
997 if (end.tv_nsec > start.tv_nsec)
999 t2 = end.tv_nsec - start.tv_nsec;
1003 t2 = start.tv_nsec - end.tv_nsec;
1008 INFO ("Seq time: %lus %luns", t1, t2);
1012 INFO ("Average pps: %f", tmp);
1013 INFO ("Thread exiting...");
1014 pthread_exit (NULL);
1018 icmpr_send (long index, long packet_num, char *hw, char *ip)
1020 memif_connection_t *c = &memif_connection[index];
1024 if (c->conn == NULL)
1027 icmpr_thread_data_t *data = &icmpr_thread_data[index];
1028 data->index = index;
1029 data->packet_num = packet_num;
1030 INFO ("PN: %lu", data->packet_num);
1031 printf ("%s\n", ip);
1032 printf ("%s\n", hw);
1034 ui = strtok (ip, ".");
1037 tmp[0] = strtol (ui, &end, 10);
1039 ui = strtok (NULL, ".");
1042 tmp[1] = strtol (ui, &end, 10);
1044 ui = strtok (NULL, ".");
1047 tmp[2] = strtol (ui, &end, 10);
1049 ui = strtok (NULL, ".");
1052 tmp[3] = strtol (ui, &end, 10);
1054 data->ip_daddr[0] = tmp[0];
1055 data->ip_daddr[1] = tmp[1];
1056 data->ip_daddr[2] = tmp[2];
1057 data->ip_daddr[3] = tmp[3];
1059 ui = strtok (hw, ":");
1062 tmp[0] = strtol (ui, &end, 16);
1063 ui = strtok (NULL, ":");
1066 tmp[1] = strtol (ui, &end, 16);
1067 ui = strtok (NULL, ":");
1070 tmp[2] = strtol (ui, &end, 16);
1071 ui = strtok (NULL, ":");
1074 tmp[3] = strtol (ui, &end, 16);
1075 ui = strtok (NULL, ":");
1078 tmp[4] = strtol (ui, &end, 16);
1079 ui = strtok (NULL, ":");
1082 tmp[5] = strtol (ui, &end, 16);
1084 data->hw_daddr[0] = tmp[0];
1085 data->hw_daddr[1] = tmp[1];
1086 data->hw_daddr[2] = tmp[2];
1087 data->hw_daddr[3] = tmp[3];
1088 data->hw_daddr[4] = tmp[4];
1089 data->hw_daddr[5] = tmp[5];
1091 pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
1095 INFO ("Invalid input\n");
1100 user_input_handler ()
1102 char *in = (char *) malloc (256);
1103 char *ui = fgets (in, 256, stdin);
1108 ui = strtok (in, " ");
1109 if (strncmp (ui, "exit", 4) == 0)
1113 exit (EXIT_SUCCESS);
1115 else if (strncmp (ui, "help", 4) == 0)
1120 else if (strncmp (ui, "conn", 4) == 0)
1122 ui = strtok (NULL, " ");
1124 a = strtol (ui, &end, 10);
1127 INFO ("expected id");
1130 ui = strtok (NULL, " ");
1132 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1134 INFO ("expected mode <0|1>");
1137 else if (strncmp (ui, "del", 3) == 0)
1139 ui = strtok (NULL, " ");
1141 icmpr_memif_delete (strtol (ui, &end, 10));
1143 INFO ("expected id");
1146 else if (strncmp (ui, "show", 4) == 0)
1148 print_memif_details ();
1151 else if (strncmp (ui, "ip-set", 6) == 0)
1153 ui = strtok (NULL, " ");
1155 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1157 INFO ("expected id");
1160 else if (strncmp (ui, "rx-mode", 7) == 0)
1162 ui = strtok (NULL, " ");
1164 a = strtol (ui, &end, 10);
1167 INFO ("expected id");
1170 ui = strtok (NULL, " ");
1172 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1174 INFO ("expected qid");
1177 else if (strncmp (ui, "sh-count", 8) == 0)
1179 icmpr_print_counters ();
1181 else if (strncmp (ui, "cl-count", 8) == 0)
1183 icmpr_reset_counters ();
1185 else if (strncmp (ui, "send", 4) == 0)
1187 ui = strtok (NULL, " ");
1189 a = strtol (ui, &end, 10);
1192 INFO ("expected id");
1195 ui = strtok (NULL, " ");
1197 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
1198 strtok (NULL, " "));
1200 INFO ("expected count");
1205 INFO ("unknown command: %s", ui);
1216 poll_event (int timeout)
1218 struct epoll_event evt;
1219 int app_err = 0, memif_err = 0, en = 0;
1220 uint32_t events = 0;
1221 struct timespec start, end;
1222 memset (&evt, 0, sizeof (evt));
1223 evt.events = EPOLLIN | EPOLLOUT;
1225 sigemptyset (&sigset);
1226 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1227 /* id event polled */
1228 timespec_get (&start, TIME_UTC);
1231 DBG ("epoll_pwait: %s", strerror (errno));
1236 /* this app does not use any other file descriptors than stds and memif control fds */
1237 if (evt.data.fd > 2)
1239 /* event of memif control fd */
1240 /* convert epolle events to memif events */
1241 if (evt.events & EPOLLIN)
1242 events |= MEMIF_FD_EVENT_READ;
1243 if (evt.events & EPOLLOUT)
1244 events |= MEMIF_FD_EVENT_WRITE;
1245 if (evt.events & EPOLLERR)
1246 events |= MEMIF_FD_EVENT_ERROR;
1247 memif_err = memif_control_fd_handler (evt.data.fd, events);
1248 if (memif_err != MEMIF_ERR_SUCCESS)
1249 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1251 else if (evt.data.fd == 0)
1253 app_err = user_input_handler ();
1257 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1261 timespec_get (&end, TIME_UTC);
1262 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1264 if ((app_err < 0) || (memif_err < 0))
1267 DBG ("user input handler error");
1269 DBG ("memif control fd handler error");
1279 epfd = epoll_create (1);
1280 add_epoll_fd (0, EPOLLIN);
1286 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1288 INFO ("Error opening log file: %s", strerror (errno));
1289 #endif /* LOG_FILE */
1291 /* initialize memory interface */
1293 /* if valid callback is passed as argument, fd event polling will be done by user
1294 all file descriptors and events will be passed to user in this callback */
1295 /* if callback is set to NULL libmemif will handle fd event polling */
1296 err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
1297 if (err != MEMIF_ERR_SUCCESS)
1299 INFO ("memif_init: %s", memif_strerror (err));
1304 for (i = 0; i < MAX_CONNS; i++)
1306 memset (&memif_connection[i], 0, sizeof (memif_connection_t));
1315 if (poll_event (-1) < 0)
1317 DBG ("poll_event error!");