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\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
190 printf ("\ttx queues:\n");
191 for (e = 0; e < md.tx_queues_num; e++)
193 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
194 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
195 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
207 add_epoll_fd (int fd, uint32_t events)
211 DBG ("invalid fd %d", fd);
214 struct epoll_event evt;
215 memset (&evt, 0, sizeof (evt));
218 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
220 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
223 DBG ("fd %d added to epoll", fd);
228 mod_epoll_fd (int fd, uint32_t events)
232 DBG ("invalid fd %d", fd);
235 struct epoll_event evt;
236 memset (&evt, 0, sizeof (evt));
239 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
241 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
244 DBG ("fd %d moddified on epoll", fd);
249 del_epoll_fd (int fd)
253 DBG ("invalid fd %d", fd);
256 struct epoll_event evt;
257 memset (&evt, 0, sizeof (evt));
258 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
260 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
263 DBG ("fd %d removed from epoll", fd);
267 /* informs user about connected status. private_ctx is used by user to identify connection
268 (multiple connections WIP) */
270 on_connect (memif_conn_handle_t conn, void *private_ctx)
272 INFO ("memif connected!");
277 /* informs user about disconnected status. private_ctx is used by user to identify connection
278 (multiple connections WIP) */
280 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
282 INFO ("memif disconnected!");
286 /* user needs to watch new fd or stop watching fd that is about to be closed.
287 control fd will be modified during connection establishment to minimize CPU usage */
289 control_fd_update (int fd, uint8_t events)
291 /* convert memif event definitions to epoll events */
292 if (events & MEMIF_FD_EVENT_DEL)
293 return del_epoll_fd (fd);
296 if (events & MEMIF_FD_EVENT_READ)
298 if (events & MEMIF_FD_EVENT_WRITE)
301 if (events & MEMIF_FD_EVENT_MOD)
302 return mod_epoll_fd (fd, evt);
304 return add_epoll_fd (fd, evt);
308 icmpr_buffer_alloc (long index, long n, uint16_t * r, uint16_t i,
311 memif_connection_t *c = &memif_connection[index];
313 /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
314 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 0);
315 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
317 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
321 DBG ("allocated %d/%ld buffers, %u free buffers", *r, n,
322 MAX_MEMIF_BUFS - c->tx_buf_num);
327 icmpr_tx_burst (long index, uint16_t qid)
329 memif_connection_t *c = &memif_connection[index];
332 /* inform peer memif interface about data in shared memory buffers */
333 /* mark memif buffers as free */
334 err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r);
335 if (err != MEMIF_ERR_SUCCESS)
336 INFO ("memif_tx_burst: %s", memif_strerror (err));
342 /* called when event is polled on interrupt file descriptor.
343 there are packets in shared memory ready to be received */
345 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
347 long index = *((long *) private_ctx);
348 memif_connection_t *c = &memif_connection[index];
349 if (c->index != index)
351 INFO ("invalid context: %ld/%u", index, c->index);
355 int err = MEMIF_ERR_SUCCESS, ret_val;
358 int i; /* rx buffer iterator */
359 int j; /* tx bufferiterator */
361 /* loop while there are packets in shm */
364 /* receive data from shared memory buffers */
365 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
368 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
370 INFO ("memif_rx_burst: %s", memif_strerror (err));
376 /* try to alloc required number of buffers buffers */
377 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
378 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
380 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
384 c->tx_err_counter = rx - tx;
391 resolve_packet ((void *) (c->rx_bufs + i)->data,
392 (c->rx_bufs + i)->data_len,
393 (void *) (c->tx_bufs + j)->data,
394 &(c->tx_bufs + j)->data_len, c->ip_addr);
395 resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
396 (c->rx_bufs + i + 1)->data_len,
397 (void *) (c->tx_bufs + j + 1)->data,
398 &(c->tx_bufs + j + 1)->data_len, c->ip_addr);
404 resolve_packet ((void *) (c->rx_bufs + i)->data,
405 (c->rx_bufs + i)->data_len,
406 (void *) (c->tx_bufs + j)->data,
407 &(c->tx_bufs + j)->data_len, c->ip_addr);
412 /* mark memif buffers and shared memory buffers as free */
413 /* free processed buffers */
414 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
415 if (err != MEMIF_ERR_SUCCESS)
416 INFO ("memif_buffer_free: %s", memif_strerror (err));
419 DBG ("freed %d buffers. %u/%u alloc/free buffers",
420 fb, rx, MAX_MEMIF_BUFS - rx);
422 /* transmit allocated buffers */
423 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
424 if (err != MEMIF_ERR_SUCCESS)
426 INFO ("memif_tx_burst: %s", memif_strerror (err));
432 while (ret_val == MEMIF_ERR_NOBUF);
437 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
438 if (err != MEMIF_ERR_SUCCESS)
439 INFO ("memif_buffer_free: %s", memif_strerror (err));
441 DBG ("freed %d buffers. %u/%u alloc/free buffers",
442 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
446 /* called when event is polled on interrupt file descriptor.
447 there are packets in shared memory ready to be received */
448 /* dev test modification: loop until TX == RX (don't drop buffers) */
450 on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
452 long index = *((long *) private_ctx);
453 memif_connection_t *c = &memif_connection[index];
454 if (c->index != index)
456 INFO ("invalid context: %ld/%u", index, c->index);
460 int err = MEMIF_ERR_SUCCESS, ret_val;
463 int i; /* rx buffer iterator */
464 int j; /* tx bufferiterator */
465 int prev_i; /* first allocated rx buffer */
467 /* loop while there are packets in shm */
470 /* receive data from shared memory buffers */
471 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
474 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
476 INFO ("memif_rx_burst: %s", memif_strerror (err));
482 /* loop while there are RX buffers to be processed */
486 /* try to alloc required number of buffers buffers */
487 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
488 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
490 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
500 resolve_packet ((void *) (c->rx_bufs + i)->data,
501 (c->rx_bufs + i)->data_len,
502 (void *) (c->tx_bufs + j)->data,
503 &(c->tx_bufs + j)->data_len, c->ip_addr);
504 resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
505 (c->rx_bufs + i + 1)->data_len,
506 (void *) (c->tx_bufs + j + 1)->data,
507 &(c->tx_bufs + j + 1)->data_len,
514 resolve_packet ((void *) (c->rx_bufs + i)->data,
515 (c->rx_bufs + i)->data_len,
516 (void *) (c->tx_bufs + j)->data,
517 &(c->tx_bufs + j)->data_len, c->ip_addr);
522 /* mark memif buffers and shared memory buffers as free */
523 /* free processed buffers */
524 err = memif_buffer_free (c->conn, qid, c->rx_bufs + prev_i, j, &fb);
525 if (err != MEMIF_ERR_SUCCESS)
526 INFO ("memif_buffer_free: %s", memif_strerror (err));
530 DBG ("freed %d buffers. %u/%u alloc/free buffers",
531 fb, rx, MAX_MEMIF_BUFS - rx);
533 /* transmit allocated buffers */
534 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
535 if (err != MEMIF_ERR_SUCCESS)
537 INFO ("memif_tx_burst: %s", memif_strerror (err));
545 while (ret_val == MEMIF_ERR_NOBUF);
550 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
551 if (err != MEMIF_ERR_SUCCESS)
552 INFO ("memif_buffer_free: %s", memif_strerror (err));
554 DBG ("freed %d buffers. %u/%u alloc/free buffers",
555 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
559 /* called when event is polled on interrupt file descriptor.
560 there are packets in shared memory ready to be received */
561 /* dev test modification: handle only ARP requests */
563 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
565 long index = *((long *) private_ctx);
566 memif_connection_t *c = &memif_connection[index];
567 if (c->index != index)
569 INFO ("invalid context: %ld/%u", index, c->index);
573 int err = MEMIF_ERR_SUCCESS, ret_val;
581 /* receive data from shared memory buffers */
582 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
584 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
586 INFO ("memif_rx_burst: %s", memif_strerror (err));
592 for (i = 0; i < rx; i++)
594 if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
597 if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
599 resolve_packet ((void *) (c->rx_bufs + i)->data,
600 (c->rx_bufs + i)->data_len,
601 (void *) (c->tx_bufs + i)->data,
602 &(c->tx_bufs + i)->data_len, c->ip_addr);
603 icmpr_tx_burst (c->index, qid);
607 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
608 if (err != MEMIF_ERR_SUCCESS)
609 INFO ("memif_buffer_free: %s", memif_strerror (err));
614 while (ret_val == MEMIF_ERR_NOBUF);
619 err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
620 if (err != MEMIF_ERR_SUCCESS)
621 INFO ("memif_buffer_free: %s", memif_strerror (err));
623 DBG ("freed %d buffers. %u/%u alloc/free buffers",
624 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
629 icmpr_memif_create (long index, long mode, char *s)
631 if (index >= MAX_CONNS)
633 INFO ("connection array overflow");
638 INFO ("don't even try...");
641 memif_connection_t *c = &memif_connection[index];
643 /* setting memif connection arguments */
644 memif_conn_args_t args;
646 memset (&args, 0, sizeof (args));
647 args.is_master = mode;
648 args.log2_ring_size = 11;
649 args.buffer_size = 2048;
650 args.num_s2m_rings = 2;
651 args.num_m2s_rings = 2;
652 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
653 strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
655 /* socket filename is not specified, because this app is supposed to
656 connect to VPP over memif. so default socket filename will be used */
657 /* default socketfile = /run/vpp/memif.sock */
659 args.interface_id = index;
660 /* last argument for memif_create (void * private_ctx) is used by user
661 to identify connection. this context is returned with callbacks */
663 /* default interrupt */
666 err = memif_create (&c->conn,
667 &args, on_connect, on_disconnect, on_interrupt,
669 if (err != MEMIF_ERR_SUCCESS)
671 INFO ("memif_create: %s", memif_strerror (err));
677 if (strncmp (s, "0", 1) == 0)
679 err = memif_create (&c->conn,
680 &args, on_connect, on_disconnect, on_interrupt0,
682 if (err != MEMIF_ERR_SUCCESS)
684 INFO ("memif_create: %s", memif_strerror (err));
688 else if (strncmp (s, "1", 1) == 0)
690 err = memif_create (&c->conn,
691 &args, on_connect, on_disconnect, on_interrupt1,
693 if (err != MEMIF_ERR_SUCCESS)
695 INFO ("memif_create: %s", memif_strerror (err));
701 INFO ("Unknown interrupt descriptor");
707 /* alloc memif buffers */
710 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
713 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
717 c->ip_addr[2] = c->index + 1;
720 c->seq = c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
727 icmpr_memif_delete (long index)
729 if (index >= MAX_CONNS)
731 INFO ("connection array overflow");
736 INFO ("don't even try...");
739 memif_connection_t *c = &memif_connection[index];
751 /* disconenct then delete memif connection */
752 err = memif_delete (&c->conn);
753 if (err != MEMIF_ERR_SUCCESS)
754 INFO ("memif_delete: %s", memif_strerror (err));
756 INFO ("memif delete fail");
763 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
768 printf ("==============================\n");
769 printf ("libmemif version: %s", LIBMEMIF_VERSION);
774 printf ("memif version: %d\n", MEMIF_VERSION);
775 printf ("commands:\n");
776 printf ("\thelp - prints this help\n");
777 printf ("\texit - exit app\n");
779 ("\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");
780 printf ("\tdel <index> - delete memif\n");
781 printf ("\tshow - show connection details\n");
782 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
784 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
785 printf ("\tsh-count - print counters\n");
786 printf ("\tcl-count - clear counters\n");
787 printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n");
793 /* application cleanup */
799 for (i = 0; i < MAX_CONNS; i++)
801 memif_connection_t *c = &memif_connection[i];
803 icmpr_memif_delete (i);
806 err = memif_cleanup ();
807 if (err != MEMIF_ERR_SUCCESS)
808 INFO ("memif_delete: %s", memif_strerror (err));
814 icmpr_set_ip (long index, char *ip)
816 if (index >= MAX_CONNS)
818 INFO ("connection array overflow");
823 INFO ("don't even try...");
826 memif_connection_t *c = &memif_connection[index];
829 INFO ("no connection at index %ld", index);
836 ui = strtok (ip, ".");
839 tmp[0] = strtol (ui, &end, 10);
841 ui = strtok (NULL, ".");
844 tmp[1] = strtol (ui, &end, 10);
846 ui = strtok (NULL, ".");
849 tmp[2] = strtol (ui, &end, 10);
851 ui = strtok (NULL, ".");
854 tmp[3] = strtol (ui, &end, 10);
856 c->ip_addr[0] = tmp[0];
857 c->ip_addr[1] = tmp[1];
858 c->ip_addr[2] = tmp[2];
859 c->ip_addr[3] = tmp[3];
861 INFO ("memif %ld ip address set to %u.%u.%u.%u",
862 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
867 INFO ("invalid ip address");
872 icmpr_set_rx_mode (long index, long qid, char *mode)
874 if (index >= MAX_CONNS)
876 INFO ("connection array overflow");
881 INFO ("don't even try...");
884 memif_connection_t *c = &memif_connection[index];
888 INFO ("no connection at index %ld", index);
892 if (strncmp (mode, "interrupt", 9) == 0)
894 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
897 else if (strncmp (mode, "polling", 7) == 0)
899 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
902 INFO ("expected rx mode <interrupt|polling>");
907 icmpr_print_counters ()
910 for (i = 0; i < MAX_CONNS; i++)
912 memif_connection_t *c = &memif_connection[i];
915 printf ("===============================\n");
916 printf ("interface index: %d\n", c->index);
917 printf ("\trx: %lu\n", c->rx_counter);
918 printf ("\ttx: %lu\n", c->tx_counter);
919 printf ("\ttx_err: %lu\n", c->tx_err_counter);
920 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
925 icmpr_reset_counters ()
928 for (i = 0; i < MAX_CONNS; i++)
930 memif_connection_t *c = &memif_connection[i];
933 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
939 icmpr_send_proc (void *data)
941 icmpr_thread_data_t *d = (icmpr_thread_data_t *) data;
942 int index = d->index;
943 uint64_t count = d->packet_num;
944 memif_connection_t *c = &memif_connection[index];
947 INFO ("No connection at index %d. Thread exiting...\n", index);
951 int err = MEMIF_ERR_SUCCESS;
953 struct timespec start, end;
954 memset (&start, 0, sizeof (start));
955 memset (&end, 0, sizeof (end));
957 timespec_get (&start, TIME_UTC);
961 err = memif_buffer_alloc (c->conn, 0, c->tx_bufs,
963 count ? count : MAX_MEMIF_BUFS, &tx, 0);
964 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
966 INFO ("memif_buffer_alloc: %s Thread exiting...\n",
967 memif_strerror (err));
976 generate_packet ((void *) c->tx_bufs[i].data,
977 &c->tx_bufs[i].data_len, c->ip_addr,
978 d->ip_daddr, d->hw_daddr, seq++);
979 generate_packet ((void *) c->tx_bufs[i + 1].data,
980 &c->tx_bufs[i + 1].data_len, c->ip_addr,
981 d->ip_daddr, d->hw_daddr, seq++);
985 generate_packet ((void *) c->tx_bufs[i].data,
986 &c->tx_bufs[i].data_len, c->ip_addr,
987 d->ip_daddr, d->hw_daddr, seq++);
991 err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
992 if (err != MEMIF_ERR_SUCCESS)
994 INFO ("memif_tx_burst: %s Thread exiting...\n",
995 memif_strerror (err));
1002 timespec_get (&end, TIME_UTC);
1003 INFO ("Pakcet sequence finished!");
1004 INFO ("Seq len: %u", seq);
1005 uint64_t t1 = end.tv_sec - start.tv_sec;
1007 if (end.tv_nsec > start.tv_nsec)
1009 t2 = end.tv_nsec - start.tv_nsec;
1013 t2 = start.tv_nsec - end.tv_nsec;
1018 INFO ("Seq time: %lus %luns", t1, t2);
1022 INFO ("Average pps: %f", tmp);
1023 INFO ("Thread exiting...");
1024 pthread_exit (NULL);
1028 icmpr_send (long index, long packet_num, char *hw, char *ip)
1030 memif_connection_t *c = &memif_connection[index];
1034 if (c->conn == NULL)
1037 icmpr_thread_data_t *data = &icmpr_thread_data[index];
1038 data->index = index;
1039 data->packet_num = packet_num;
1040 INFO ("PN: %lu", data->packet_num);
1041 printf ("%s\n", ip);
1042 printf ("%s\n", hw);
1044 ui = strtok (ip, ".");
1047 tmp[0] = strtol (ui, &end, 10);
1049 ui = strtok (NULL, ".");
1052 tmp[1] = strtol (ui, &end, 10);
1054 ui = strtok (NULL, ".");
1057 tmp[2] = strtol (ui, &end, 10);
1059 ui = strtok (NULL, ".");
1062 tmp[3] = strtol (ui, &end, 10);
1064 data->ip_daddr[0] = tmp[0];
1065 data->ip_daddr[1] = tmp[1];
1066 data->ip_daddr[2] = tmp[2];
1067 data->ip_daddr[3] = tmp[3];
1069 ui = strtok (hw, ":");
1072 tmp[0] = strtol (ui, &end, 16);
1073 ui = strtok (NULL, ":");
1076 tmp[1] = strtol (ui, &end, 16);
1077 ui = strtok (NULL, ":");
1080 tmp[2] = strtol (ui, &end, 16);
1081 ui = strtok (NULL, ":");
1084 tmp[3] = strtol (ui, &end, 16);
1085 ui = strtok (NULL, ":");
1088 tmp[4] = strtol (ui, &end, 16);
1089 ui = strtok (NULL, ":");
1092 tmp[5] = strtol (ui, &end, 16);
1094 data->hw_daddr[0] = tmp[0];
1095 data->hw_daddr[1] = tmp[1];
1096 data->hw_daddr[2] = tmp[2];
1097 data->hw_daddr[3] = tmp[3];
1098 data->hw_daddr[4] = tmp[4];
1099 data->hw_daddr[5] = tmp[5];
1101 pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
1105 INFO ("Invalid input\n");
1110 user_input_handler ()
1113 char *in = (char *) malloc (256);
1114 char *ui = fgets (in, 256, stdin);
1119 ui = strtok (in, " ");
1120 if (strncmp (ui, "exit", 4) == 0)
1124 exit (EXIT_SUCCESS);
1126 else if (strncmp (ui, "help", 4) == 0)
1131 else if (strncmp (ui, "conn", 4) == 0)
1133 ui = strtok (NULL, " ");
1135 a = strtol (ui, &end, 10);
1138 INFO ("expected id");
1141 ui = strtok (NULL, " ");
1143 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1145 INFO ("expected mode <0|1>");
1148 else if (strncmp (ui, "del", 3) == 0)
1150 ui = strtok (NULL, " ");
1152 icmpr_memif_delete (strtol (ui, &end, 10));
1154 INFO ("expected id");
1157 else if (strncmp (ui, "show", 4) == 0)
1159 print_memif_details ();
1162 else if (strncmp (ui, "ip-set", 6) == 0)
1164 ui = strtok (NULL, " ");
1166 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1168 INFO ("expected id");
1171 else if (strncmp (ui, "rx-mode", 7) == 0)
1173 ui = strtok (NULL, " ");
1175 a = strtol (ui, &end, 10);
1178 INFO ("expected id");
1181 ui = strtok (NULL, " ");
1183 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1185 INFO ("expected qid");
1188 else if (strncmp (ui, "sh-count", 8) == 0)
1190 icmpr_print_counters ();
1192 else if (strncmp (ui, "cl-count", 8) == 0)
1194 icmpr_reset_counters ();
1196 else if (strncmp (ui, "send", 4) == 0)
1198 ui = strtok (NULL, " ");
1200 a = strtol (ui, &end, 10);
1203 INFO ("expected id");
1206 ui = strtok (NULL, " ");
1208 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
1209 strtok (NULL, " "));
1211 INFO ("expected count");
1216 DBG ("unknown command: %s", ui);
1227 poll_event (int timeout)
1229 struct epoll_event evt, *e;
1230 int app_err = 0, memif_err = 0, en = 0;
1232 uint32_t events = 0;
1233 struct timespec start, end;
1234 memset (&evt, 0, sizeof (evt));
1235 evt.events = EPOLLIN | EPOLLOUT;
1237 sigemptyset (&sigset);
1238 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1239 /* id event polled */
1240 timespec_get (&start, TIME_UTC);
1243 DBG ("epoll_pwait: %s", strerror (errno));
1248 /* this app does not use any other file descriptors than stds and memif control fds */
1249 if (evt.data.fd > 2)
1251 /* event of memif control fd */
1252 /* convert epolle events to memif events */
1253 if (evt.events & EPOLLIN)
1254 events |= MEMIF_FD_EVENT_READ;
1255 if (evt.events & EPOLLOUT)
1256 events |= MEMIF_FD_EVENT_WRITE;
1257 if (evt.events & EPOLLERR)
1258 events |= MEMIF_FD_EVENT_ERROR;
1259 memif_err = memif_control_fd_handler (evt.data.fd, events);
1260 if (memif_err != MEMIF_ERR_SUCCESS)
1261 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1263 else if (evt.data.fd == 0)
1265 app_err = user_input_handler ();
1269 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1273 timespec_get (&end, TIME_UTC);
1274 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1276 if ((app_err < 0) || (memif_err < 0))
1279 DBG ("user input handler error");
1281 DBG ("memif control fd handler error");
1291 epfd = epoll_create (1);
1292 add_epoll_fd (0, EPOLLIN);
1298 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1300 INFO ("Error opening log file: %s", strerror (errno));
1301 #endif /* LOG_FILE */
1303 /* initialize memory interface */
1305 /* if valid callback is passed as argument, fd event polling will be done by user
1306 all file descriptors and events will be passed to user in this callback */
1307 /* if callback is set to NULL libmemif will handle fd event polling */
1308 err = memif_init (control_fd_update, APP_NAME);
1309 if (err != MEMIF_ERR_SUCCESS)
1310 INFO ("memif_init: %s", memif_strerror (err));
1312 for (i = 0; i < MAX_CONNS; i++)
1314 memif_connection[i].conn = NULL;
1323 if (poll_event (-1) < 0)
1325 DBG ("poll_event error!");