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>
50 #include <icmp_proto.h>
52 #define APP_NAME "ICMP_Responder"
53 #define IF_NAME "memif_connection"
55 #define HEADROOM 0x80 /* 128b */
59 #define DBG(...) do { \
60 printf (APP_NAME":%s:%d: ", __func__, __LINE__); \
61 printf (__VA_ARGS__); \
64 #define LOG(...) do { \
66 dprintf (out_fd, __VA_ARGS__); \
67 dprintf (out_fd, "\n"); \
70 #define LOG_FILE "/tmp/memif_time_test.txt"
76 #define INFO(...) do { \
77 printf ("INFO: "__VA_ARGS__); \
82 /* maximum tx/rx memif buffers */
83 #define MAX_MEMIF_BUFS 256
93 /* memif conenction handle */
94 memif_conn_handle_t conn;
97 /* allocated tx buffers counter */
98 /* number of tx buffers pointing to shared memory */
100 /* allcoated rx buffers counter */
101 /* number of rx buffers pointing to shared memory */
103 /* interface ip address */
105 uint64_t tx_counter, rx_counter, tx_err_counter;
106 uint64_t t_sec, t_nsec;
107 } memif_connection_t;
112 icmpr_flow_mode_t mode;
113 uint64_t packet_count;
118 struct timespec *start, end;
121 memif_connection_t memif_connection[MAX_CONNS];
125 /* print details for all memif connections */
127 print_memif_details ()
134 buf = malloc (buflen);
135 printf ("MEMIF DETAILS\n");
136 printf ("==============================\n");
137 for (i = 0; i < MAX_CONNS; i++)
139 memif_connection_t *c = &memif_connection[i];
141 memset (&md, 0, sizeof (md));
142 memset (buf, 0, buflen);
144 err = memif_get_details (c->conn, &md, buf, buflen);
145 if (err != MEMIF_ERR_SUCCESS)
147 if (err != MEMIF_ERR_NOCONN)
148 INFO ("%s", memif_strerror (err));
152 printf ("interface index: %d\n", i);
154 printf ("\tinterface ip: %u.%u.%u.%u\n",
155 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
156 printf ("\tinterface name: %s\n", (char *) md.if_name);
157 printf ("\tapp name: %s\n", (char *) md.inst_name);
158 printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
159 printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
160 printf ("\tid: %u\n", md.id);
161 printf ("\tsecret: %s\n", (char *) md.secret);
171 printf ("ethernet\n");
177 printf ("punt/inject\n");
180 printf ("unknown\n");
183 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
184 printf ("\trx queues:\n");
185 for (e = 0; e < md.rx_queues_num; e++)
187 printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
188 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
189 printf ("\t\tring rx mode: %s\n",
190 md.rx_queues[e].flags ? "polling" : "interrupt");
191 printf ("\t\tring head: %u\n", md.rx_queues[e].head);
192 printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
193 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
195 printf ("\ttx queues:\n");
196 for (e = 0; e < md.tx_queues_num; e++)
198 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
199 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
200 printf ("\t\tring rx mode: %s\n",
201 md.tx_queues[e].flags ? "polling" : "interrupt");
202 printf ("\t\tring head: %u\n", md.tx_queues[e].head);
203 printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
204 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
216 add_epoll_fd (int fd, uint32_t events)
220 DBG ("invalid fd %d", fd);
223 struct epoll_event evt;
224 memset (&evt, 0, sizeof (evt));
227 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
229 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
232 DBG ("fd %d added to epoll", fd);
237 mod_epoll_fd (int fd, uint32_t events)
241 DBG ("invalid fd %d", fd);
244 struct epoll_event evt;
245 memset (&evt, 0, sizeof (evt));
248 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
250 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
253 DBG ("fd %d moddified on epoll", fd);
258 del_epoll_fd (int fd)
262 DBG ("invalid fd %d", fd);
265 struct epoll_event evt;
266 memset (&evt, 0, sizeof (evt));
267 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
269 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
272 DBG ("fd %d removed from epoll", fd);
276 /* informs user about connected status. private_ctx is used by user to identify connection
277 (multiple connections WIP) */
279 on_connect (memif_conn_handle_t conn, void *private_ctx)
281 INFO ("memif connected!");
282 memif_refill_queue (conn, 0, -1, HEADROOM);
287 /* informs user about disconnected status. private_ctx is used by user to identify connection
288 (multiple connections WIP) */
290 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
292 INFO ("memif disconnected!");
296 /* user needs to watch new fd or stop watching fd that is about to be closed.
297 control fd will be modified during connection establishment to minimize CPU usage */
299 control_fd_update (int fd, uint8_t events, void *ctx)
301 /* convert memif event definitions to epoll events */
302 if (events & MEMIF_FD_EVENT_DEL)
303 return del_epoll_fd (fd);
306 if (events & MEMIF_FD_EVENT_READ)
308 if (events & MEMIF_FD_EVENT_WRITE)
311 if (events & MEMIF_FD_EVENT_MOD)
312 return mod_epoll_fd (fd, evt);
314 return add_epoll_fd (fd, evt);
317 /* called when event is polled on interrupt file descriptor.
318 there are packets in shared memory ready to be received */
319 /* handle packet processing in rx buffer then enqueue this buffer to tx and transmit */
321 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
323 long index = *((long *) private_ctx);
324 memif_connection_t *c = &memif_connection[index];
325 if (c->index != index)
327 INFO ("invalid context: %ld/%u", index, c->index);
331 int err = MEMIF_ERR_SUCCESS, ret_val;
332 uint16_t rx = 0, tx = 0;
333 int i = 0; /* rx buffer iterator */
335 /* loop while there are packets in shm */
338 /* receive data from shared memory buffers (dequeue rx buffers) */
339 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
343 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
345 INFO ("memif_rx_burst: %s", memif_strerror (err));
349 /* process bufers in place */
350 for (i = 0; i < rx; i++)
352 resolve_packet2 ((void *) (c->bufs + i)->data,
353 &(c->bufs + i)->len, c->ip_addr);
356 /* enque processed buffers to tx ring */
357 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, i, &tx);
358 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
360 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
365 c->tx_err_counter += i - tx;
367 /* mark memif buffers and shared memory buffers as free */
368 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
369 if (err != MEMIF_ERR_SUCCESS)
370 INFO ("memif_buffer_free: %s", memif_strerror (err));
373 DBG ("freed %d buffers. %u/%u alloc/free buffers",
374 rx, rx, MAX_MEMIF_BUFS - rx);
376 /* transmit allocated buffers */
377 err = memif_tx_burst (c->conn, qid, c->bufs, rx, &tx);
378 if (err != MEMIF_ERR_SUCCESS)
380 INFO ("memif_tx_burst: %s", memif_strerror (err));
386 while (ret_val == MEMIF_ERR_NOBUF);
391 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
392 if (err != MEMIF_ERR_SUCCESS)
393 INFO ("memif_buffer_free: %s", memif_strerror (err));
395 DBG ("freed %d buffers. %u/%u alloc/free buffers",
396 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
400 /* add ethernet encap to packet in rx buffer then enqueue this buffer to tx and tranmit */
402 on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
404 long index = *((long *) private_ctx);
405 memif_connection_t *c = &memif_connection[index];
406 if (c->index != index)
408 INFO ("invalid context: %ld/%u", index, c->index);
412 int err = MEMIF_ERR_SUCCESS, ret_val;
413 uint16_t rx = 0, tx = 0;
414 int i = 0; /* rx buffer iterator */
416 /* loop while there are packets in shm */
419 /* receive data from shared memory buffers (dequeue rx buffers) */
420 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
424 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
426 INFO ("memif_rx_burst: %s", memif_strerror (err));
430 /* process bufers in place */
431 for (i = 0; i < rx; i++)
433 resolve_packet3 (&c->bufs[i].data, &c->bufs[i].len, c->ip_addr);
435 /* enque processed buffers to tx ring */
436 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, rx, &tx);
437 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
439 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
444 c->tx_err_counter += i - tx;
446 /* mark memif buffers and shared memory buffers as free */
447 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
448 if (err != MEMIF_ERR_SUCCESS)
449 INFO ("memif_buffer_free: %s", memif_strerror (err));
452 DBG ("freed %d buffers. %u/%u alloc/free buffers",
453 rx, rx, MAX_MEMIF_BUFS - rx);
455 /* transmit allocated buffers */
456 err = memif_tx_burst (c->conn, qid, c->bufs, i, &tx);
457 if (err != MEMIF_ERR_SUCCESS)
459 INFO ("memif_tx_burst: %s", memif_strerror (err));
465 while (ret_val == MEMIF_ERR_NOBUF);
470 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
471 if (err != MEMIF_ERR_SUCCESS)
472 INFO ("memif_buffer_free: %s", memif_strerror (err));
474 DBG ("freed %d buffers. %u/%u alloc/free buffers",
475 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
479 /* called when event is polled on interrupt file descriptor.
480 there are packets in shared memory ready to be received */
481 /* dev test modification: handle only ARP requests */
483 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
485 long index = *((long *) private_ctx);
486 memif_connection_t *c = &memif_connection[index];
487 if (c->index != index)
489 INFO ("invalid context: %ld/%u", index, c->index);
493 int err = MEMIF_ERR_SUCCESS, ret_val;
499 /* receive data from shared memory buffers */
500 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
504 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
506 INFO ("memif_rx_burst: %s", memif_strerror (err));
510 for (i = 0; i < rx; i++)
512 if (((struct ether_header *) (c->bufs + i)->data)->ether_type ==
515 /* process data in place */
516 resolve_packet2 ((void *) (c->bufs + i)->data,
517 &(c->bufs + i)->len, c->ip_addr);
518 /* enque buffer to tx ring */
519 memif_buffer_enq_tx (c->conn, qid, c->bufs, 1, &tx);
521 memif_tx_burst (c->conn, qid, c->bufs, 1, &tx);
525 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
526 if (err != MEMIF_ERR_SUCCESS)
527 INFO ("memif_buffer_free: %s", memif_strerror (err));
531 while (ret_val == MEMIF_ERR_NOBUF);
536 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
537 if (err != MEMIF_ERR_SUCCESS)
538 INFO ("memif_buffer_free: %s", memif_strerror (err));
540 DBG ("freed %d buffers. %u/%u alloc/free buffers",
541 rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
546 icmpr_memif_create (long index, long mode, char *s)
548 if (index >= MAX_CONNS)
550 INFO ("connection array overflow");
555 INFO ("don't even try...");
558 memif_connection_t *c = &memif_connection[index];
560 /* setting memif connection arguments */
561 memif_conn_args_t args;
562 memset (&args, 0, sizeof (args));
563 args.is_master = mode;
564 args.log2_ring_size = 11;
565 args.buffer_size = 2048;
566 args.num_s2m_rings = 1;
567 args.num_m2s_rings = 1;
568 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
570 /* socket filename is not specified, because this app is supposed to
571 connect to VPP over memif. so default socket filename will be used */
572 /* default socketfile = /run/vpp/memif.sock */
574 args.interface_id = index;
575 /* last argument for memif_create (void * private_ctx) is used by user
576 to identify connection. this context is returned with callbacks */
578 /* default interrupt */
581 err = memif_create (&c->conn,
582 &args, on_connect, on_disconnect, on_interrupt,
584 if (err != MEMIF_ERR_SUCCESS)
586 INFO ("memif_create: %s", memif_strerror (err));
592 if (strncmp (s, "0", 1) == 0)
594 err = memif_create (&c->conn,
595 &args, on_connect, on_disconnect, on_interrupt0,
597 if (err != MEMIF_ERR_SUCCESS)
599 INFO ("memif_create: %s", memif_strerror (err));
603 else if (strncmp (s, "1", 1) == 0)
605 err = memif_create (&c->conn,
606 &args, on_connect, on_disconnect, on_interrupt1,
608 if (err != MEMIF_ERR_SUCCESS)
610 INFO ("memif_create: %s", memif_strerror (err));
616 INFO ("Unknown interrupt descriptor");
622 /* alloc memif buffers */
626 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
630 c->ip_addr[2] = c->index + 1;
633 c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
640 icmpr_memif_delete (long index)
642 if (index >= MAX_CONNS)
644 INFO ("connection array overflow");
649 INFO ("don't even try...");
652 memif_connection_t *c = &memif_connection[index];
661 /* disconenct then delete memif connection */
662 err = memif_delete (&c->conn);
663 if (err != MEMIF_ERR_SUCCESS)
664 INFO ("memif_delete: %s", memif_strerror (err));
666 INFO ("memif delete fail");
673 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
678 printf ("==============================\n");
679 printf ("libmemif version: %s", LIBMEMIF_VERSION);
684 printf ("memif version: %d\n", memif_get_version ());
685 printf ("commands:\n");
686 printf ("\thelp - prints this help\n");
687 printf ("\texit - exit app\n");
689 ("\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");
690 printf ("\tdel <index> - delete memif\n");
691 printf ("\tshow - show connection details\n");
692 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
694 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
695 printf ("\tsh-count - print counters\n");
696 printf ("\tcl-count - clear counters\n");
698 ("\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
704 /* application cleanup */
710 for (i = 0; i < MAX_CONNS; i++)
712 memif_connection_t *c = &memif_connection[i];
714 icmpr_memif_delete (i);
717 err = memif_cleanup ();
718 if (err != MEMIF_ERR_SUCCESS)
719 INFO ("memif_delete: %s", memif_strerror (err));
725 icmpr_set_ip (long index, char *ip)
727 if (index >= MAX_CONNS)
729 INFO ("connection array overflow");
734 INFO ("don't even try...");
737 memif_connection_t *c = &memif_connection[index];
740 INFO ("no connection at index %ld", index);
747 ui = strtok (ip, ".");
750 tmp[0] = strtol (ui, &end, 10);
752 ui = strtok (NULL, ".");
755 tmp[1] = strtol (ui, &end, 10);
757 ui = strtok (NULL, ".");
760 tmp[2] = strtol (ui, &end, 10);
762 ui = strtok (NULL, ".");
765 tmp[3] = strtol (ui, &end, 10);
767 c->ip_addr[0] = tmp[0];
768 c->ip_addr[1] = tmp[1];
769 c->ip_addr[2] = tmp[2];
770 c->ip_addr[3] = tmp[3];
772 INFO ("memif %ld ip address set to %u.%u.%u.%u",
773 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
778 INFO ("invalid ip address");
783 icmpr_set_rx_mode (long index, long qid, char *mode)
785 if (index >= MAX_CONNS)
787 INFO ("connection array overflow");
792 INFO ("don't even try...");
795 memif_connection_t *c = &memif_connection[index];
799 INFO ("no connection at index %ld", index);
803 if (strncmp (mode, "interrupt", 9) == 0)
805 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
808 else if (strncmp (mode, "polling", 7) == 0)
810 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
813 INFO ("expected rx mode <interrupt|polling>");
818 icmpr_print_counters ()
821 for (i = 0; i < MAX_CONNS; i++)
823 memif_connection_t *c = &memif_connection[i];
826 printf ("===============================\n");
827 printf ("interface index: %d\n", c->index);
828 printf ("\trx: %lu\n", c->rx_counter);
829 printf ("\ttx: %lu\n", c->tx_counter);
830 printf ("\ttx_err: %lu\n", c->tx_err_counter);
831 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
836 icmpr_reset_counters ()
839 for (i = 0; i < MAX_CONNS; i++)
841 memif_connection_t *c = &memif_connection[i];
844 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
852 memif_connection_t *c = &memif_connection[flow->index];
855 INFO ("No connection at index %d. Stopping flow...\n", flow->index);
859 int err = MEMIF_ERR_SUCCESS;
863 flow->start = malloc (sizeof (struct timespec));
864 memset (flow->start, 0, sizeof (struct timespec));
865 timespec_get (flow->start, TIME_UTC);
869 err = memif_buffer_alloc (c->conn, 0, c->bufs,
871 flow->packet_count ? flow->packet_count :
872 MAX_MEMIF_BUFS, &tx, 64);
873 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
875 INFO ("memif_buffer_alloc: %s Stopping flow...\n",
876 memif_strerror (err));
883 generate_packet2 ((void *) c->bufs[i].data,
884 &c->bufs[i].len, c->ip_addr,
885 flow->ip_daddr, flow->hw_daddr, (flow->sequence)++,
890 err = memif_tx_burst (c->conn, 0, c->bufs, i, &tx);
891 if (err != MEMIF_ERR_SUCCESS)
893 INFO ("memif_tx_burst: %s Stopping flow...\n", memif_strerror (err));
899 flow->packet_count -= tx;
901 if (flow->packet_count == 0)
903 timespec_get (&flow->end, TIME_UTC);
904 INFO ("Flow finished!");
905 INFO ("Flow length: %lu", flow->tx);
906 uint64_t t1 = flow->end.tv_sec - flow->start->tv_sec;
908 if (flow->end.tv_nsec > flow->start->tv_nsec)
910 t2 = flow->end.tv_nsec - flow->start->tv_nsec;
914 t2 = flow->start->tv_nsec - flow->end.tv_nsec;
919 INFO ("Flow time: %lus %luns", t1, t2);
922 tmp = flow->tx / tmp;
923 INFO ("Average pps: %f", tmp);
924 INFO ("Stopping flow...");
942 icmpr_send (long index, long packet_num, char *input)
946 printf ("only one flow allowed\n");
950 memif_connection_t *c = &memif_connection[index];
957 flow = malloc (sizeof (icmpr_flow_t));
959 flow->packet_count = packet_num;
963 memset (&flow->end, 0, sizeof (struct timespec));
965 INFO ("packet count: %lu", flow->packet_count);
966 printf ("%s\n", input);
968 ui = strtok (input, ".");
971 tmp[0] = strtol (ui, &end, 10);
973 ui = strtok (NULL, ".");
976 tmp[1] = strtol (ui, &end, 10);
978 ui = strtok (NULL, ".");
981 tmp[2] = strtol (ui, &end, 10);
983 ui = strtok (NULL, ".");
986 tmp[3] = strtol (ui, &end, 10);
988 flow->ip_daddr[0] = tmp[0];
989 flow->ip_daddr[1] = tmp[1];
990 flow->ip_daddr[2] = tmp[2];
991 flow->ip_daddr[3] = tmp[3];
993 ui = strtok (NULL, " ");
996 flow->mode = ICMPR_FLOW_MODE_IP;
1000 ui = strtok (NULL, ":");
1003 tmp[0] = strtol (ui, &end, 16);
1004 ui = strtok (NULL, ":");
1007 tmp[1] = strtol (ui, &end, 16);
1008 ui = strtok (NULL, ":");
1011 tmp[2] = strtol (ui, &end, 16);
1012 ui = strtok (NULL, ":");
1015 tmp[3] = strtol (ui, &end, 16);
1016 ui = strtok (NULL, ":");
1019 tmp[4] = strtol (ui, &end, 16);
1020 ui = strtok (NULL, ":");
1023 tmp[5] = strtol (ui, &end, 16);
1025 flow->hw_daddr[0] = tmp[0];
1026 flow->hw_daddr[1] = tmp[1];
1027 flow->hw_daddr[2] = tmp[2];
1028 flow->hw_daddr[3] = tmp[3];
1029 flow->hw_daddr[4] = tmp[4];
1030 flow->hw_daddr[5] = tmp[5];
1032 flow->mode = ICMPR_FLOW_MODE_ETH;
1037 INFO ("Invalid input\n");
1045 user_input_handler ()
1047 char *in = (char *) malloc (256);
1048 char *ui = fgets (in, 256, stdin);
1053 ui = strtok (in, " ");
1054 if (strncmp (ui, "exit", 4) == 0)
1058 exit (EXIT_SUCCESS);
1060 else if (strncmp (ui, "help", 4) == 0)
1065 else if (strncmp (ui, "conn", 4) == 0)
1067 ui = strtok (NULL, " ");
1069 a = strtol (ui, &end, 10);
1072 INFO ("expected id");
1075 ui = strtok (NULL, " ");
1077 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1079 INFO ("expected mode <0|1>");
1082 else if (strncmp (ui, "del", 3) == 0)
1084 ui = strtok (NULL, " ");
1086 icmpr_memif_delete (strtol (ui, &end, 10));
1088 INFO ("expected id");
1091 else if (strncmp (ui, "show", 4) == 0)
1093 print_memif_details ();
1096 else if (strncmp (ui, "ip-set", 6) == 0)
1098 ui = strtok (NULL, " ");
1100 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1102 INFO ("expected id");
1105 else if (strncmp (ui, "rx-mode", 7) == 0)
1107 ui = strtok (NULL, " ");
1109 a = strtol (ui, &end, 10);
1112 INFO ("expected id");
1115 ui = strtok (NULL, " ");
1117 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1119 INFO ("expected qid");
1122 else if (strncmp (ui, "sh-count", 8) == 0)
1124 icmpr_print_counters ();
1126 else if (strncmp (ui, "cl-count", 8) == 0)
1128 icmpr_reset_counters ();
1130 else if (strncmp (ui, "send", 4) == 0)
1132 ui = strtok (NULL, " ");
1134 a = strtol (ui, &end, 10);
1137 INFO ("expected id");
1140 ui = strtok (NULL, " ");
1142 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "));
1144 INFO ("expected count");
1149 INFO ("unknown command: %s", ui);
1160 poll_event (int timeout)
1162 struct epoll_event evt;
1163 int app_err = 0, memif_err = 0, en = 0;
1164 uint32_t events = 0;
1165 struct timespec start, end;
1166 memset (&evt, 0, sizeof (evt));
1167 evt.events = EPOLLIN | EPOLLOUT;
1169 sigemptyset (&sigset);
1170 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1171 /* id event polled */
1172 timespec_get (&start, TIME_UTC);
1175 DBG ("epoll_pwait: %s", strerror (errno));
1180 /* this app does not use any other file descriptors than stds and memif control fds */
1181 if (evt.data.fd > 2)
1183 /* event of memif control fd */
1184 /* convert epolle events to memif events */
1185 if (evt.events & EPOLLIN)
1186 events |= MEMIF_FD_EVENT_READ;
1187 if (evt.events & EPOLLOUT)
1188 events |= MEMIF_FD_EVENT_WRITE;
1189 if (evt.events & EPOLLERR)
1190 events |= MEMIF_FD_EVENT_ERROR;
1191 memif_err = memif_control_fd_handler (evt.data.fd, events);
1192 if (memif_err != MEMIF_ERR_SUCCESS)
1193 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1195 else if (evt.data.fd == 0)
1197 app_err = user_input_handler ();
1201 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1205 timespec_get (&end, TIME_UTC);
1206 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1208 if ((app_err < 0) || (memif_err < 0))
1211 DBG ("user input handler error");
1213 DBG ("memif control fd handler error");
1223 epfd = epoll_create (1);
1224 add_epoll_fd (0, EPOLLIN);
1232 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1234 INFO ("Error opening log file: %s", strerror (errno));
1235 #endif /* LOG_FILE */
1237 /* initialize memory interface */
1239 /* if valid callback is passed as argument, fd event polling will be done by user
1240 all file descriptors and events will be passed to user in this callback */
1241 /* if callback is set to NULL libmemif will handle fd event polling */
1242 err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
1243 if (err != MEMIF_ERR_SUCCESS)
1245 INFO ("memif_init: %s", memif_strerror (err));
1250 for (i = 0; i < MAX_CONNS; i++)
1252 memif_connection[i].conn = NULL;
1261 if (poll_event (0) < 0)
1263 DBG ("poll_event error!");