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)
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;
334 int i = 0; /* rx buffer iterator */
335 int j = 0; /* tx bufferiterator */
337 /* loop while there are packets in shm */
340 /* receive data from shared memory buffers (dequeue rx buffers) */
341 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
345 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
347 INFO ("memif_rx_burst: %s", memif_strerror (err));
351 /* process bufers in place */
352 for (i = 0; i < rx; i++)
354 resolve_packet2 ((void *) (c->bufs + i)->data,
355 &(c->bufs + i)->len, c->ip_addr);
358 /* enque processed buffers to tx ring */
359 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, i, &tx);
360 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
362 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
367 c->tx_err_counter += i - tx;
369 /* mark memif buffers and shared memory buffers as free */
370 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
371 if (err != MEMIF_ERR_SUCCESS)
372 INFO ("memif_buffer_free: %s", memif_strerror (err));
375 DBG ("freed %d buffers. %u/%u alloc/free buffers",
376 fb, rx, MAX_MEMIF_BUFS - rx);
378 /* transmit allocated buffers */
379 err = memif_tx_burst (c->conn, qid, c->bufs, rx, &tx);
380 if (err != MEMIF_ERR_SUCCESS)
382 INFO ("memif_tx_burst: %s", memif_strerror (err));
388 while (ret_val == MEMIF_ERR_NOBUF);
393 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
394 if (err != MEMIF_ERR_SUCCESS)
395 INFO ("memif_buffer_free: %s", memif_strerror (err));
397 DBG ("freed %d buffers. %u/%u alloc/free buffers",
398 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
402 /* add ethernet encap to packet in rx buffer then enqueue this buffer to tx and tranmit */
404 on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
406 long index = *((long *) private_ctx);
407 memif_connection_t *c = &memif_connection[index];
408 if (c->index != index)
410 INFO ("invalid context: %ld/%u", index, c->index);
414 int err = MEMIF_ERR_SUCCESS, ret_val;
415 uint16_t rx = 0, tx = 0;
417 int i = 0; /* rx buffer iterator */
418 int j = 0; /* tx bufferiterator */
420 /* loop while there are packets in shm */
423 /* receive data from shared memory buffers (dequeue rx buffers) */
424 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
428 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
430 INFO ("memif_rx_burst: %s", memif_strerror (err));
434 /* process bufers in place */
435 for (i = 0; i < rx; i++)
437 resolve_packet3 (&c->bufs[i].data, &c->bufs[i].len, c->ip_addr);
439 /* enque processed buffers to tx ring */
440 err = memif_buffer_enq_tx (c->conn, qid, c->bufs, rx, &tx);
441 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
443 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
448 c->tx_err_counter += i - tx;
450 /* mark memif buffers and shared memory buffers as free */
451 err = memif_refill_queue (c->conn, qid, rx, HEADROOM);
452 if (err != MEMIF_ERR_SUCCESS)
453 INFO ("memif_buffer_free: %s", memif_strerror (err));
456 DBG ("freed %d buffers. %u/%u alloc/free buffers",
457 fb, rx, MAX_MEMIF_BUFS - rx);
459 /* transmit allocated buffers */
460 err = memif_tx_burst (c->conn, qid, c->bufs, i, &tx);
461 if (err != MEMIF_ERR_SUCCESS)
463 INFO ("memif_tx_burst: %s", memif_strerror (err));
469 while (ret_val == MEMIF_ERR_NOBUF);
474 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
475 if (err != MEMIF_ERR_SUCCESS)
476 INFO ("memif_buffer_free: %s", memif_strerror (err));
478 DBG ("freed %d buffers. %u/%u alloc/free buffers",
479 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
483 /* called when event is polled on interrupt file descriptor.
484 there are packets in shared memory ready to be received */
485 /* dev test modification: handle only ARP requests */
487 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
489 long index = *((long *) private_ctx);
490 memif_connection_t *c = &memif_connection[index];
491 if (c->index != index)
493 INFO ("invalid context: %ld/%u", index, c->index);
497 int err = MEMIF_ERR_SUCCESS, ret_val;
505 /* receive data from shared memory buffers */
506 err = memif_rx_burst (c->conn, qid, c->bufs, MAX_MEMIF_BUFS, &rx);
510 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
512 INFO ("memif_rx_burst: %s", memif_strerror (err));
516 for (i = 0; i < rx; i++)
518 if (((struct ether_header *) (c->bufs + i)->data)->ether_type ==
521 /* process data in place */
522 resolve_packet2 ((void *) (c->bufs + i)->data,
523 &(c->bufs + i)->len, c->ip_addr);
524 /* enque buffer to tx ring */
525 memif_buffer_enq_tx (c->conn, qid, c->bufs, 1, &tx);
527 memif_tx_burst (c->conn, qid, c->bufs, 1, &tx);
531 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
532 if (err != MEMIF_ERR_SUCCESS)
533 INFO ("memif_buffer_free: %s", memif_strerror (err));
537 while (ret_val == MEMIF_ERR_NOBUF);
542 err = memif_refill_queue (c->conn, qid, -1, HEADROOM);
543 if (err != MEMIF_ERR_SUCCESS)
544 INFO ("memif_buffer_free: %s", memif_strerror (err));
546 DBG ("freed %d buffers. %u/%u alloc/free buffers",
547 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
552 icmpr_memif_create (long index, long mode, char *s)
554 if (index >= MAX_CONNS)
556 INFO ("connection array overflow");
561 INFO ("don't even try...");
564 memif_connection_t *c = &memif_connection[index];
566 /* setting memif connection arguments */
567 memif_conn_args_t args;
569 memset (&args, 0, sizeof (args));
570 args.is_master = mode;
571 args.log2_ring_size = 11;
572 args.buffer_size = 2048;
573 args.num_s2m_rings = 1;
574 args.num_m2s_rings = 1;
575 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
577 /* socket filename is not specified, because this app is supposed to
578 connect to VPP over memif. so default socket filename will be used */
579 /* default socketfile = /run/vpp/memif.sock */
581 args.interface_id = index;
582 /* last argument for memif_create (void * private_ctx) is used by user
583 to identify connection. this context is returned with callbacks */
585 /* default interrupt */
588 err = memif_create (&c->conn,
589 &args, on_connect, on_disconnect, on_interrupt,
591 if (err != MEMIF_ERR_SUCCESS)
593 INFO ("memif_create: %s", memif_strerror (err));
599 if (strncmp (s, "0", 1) == 0)
601 err = memif_create (&c->conn,
602 &args, on_connect, on_disconnect, on_interrupt0,
604 if (err != MEMIF_ERR_SUCCESS)
606 INFO ("memif_create: %s", memif_strerror (err));
610 else if (strncmp (s, "1", 1) == 0)
612 err = memif_create (&c->conn,
613 &args, on_connect, on_disconnect, on_interrupt1,
615 if (err != MEMIF_ERR_SUCCESS)
617 INFO ("memif_create: %s", memif_strerror (err));
623 INFO ("Unknown interrupt descriptor");
629 /* alloc memif buffers */
633 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
637 c->ip_addr[2] = c->index + 1;
640 c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
647 icmpr_memif_delete (long index)
649 if (index >= MAX_CONNS)
651 INFO ("connection array overflow");
656 INFO ("don't even try...");
659 memif_connection_t *c = &memif_connection[index];
668 /* disconenct then delete memif connection */
669 err = memif_delete (&c->conn);
670 if (err != MEMIF_ERR_SUCCESS)
671 INFO ("memif_delete: %s", memif_strerror (err));
673 INFO ("memif delete fail");
680 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
685 printf ("==============================\n");
686 printf ("libmemif version: %s", LIBMEMIF_VERSION);
691 printf ("memif version: %d\n", memif_get_version ());
692 printf ("commands:\n");
693 printf ("\thelp - prints this help\n");
694 printf ("\texit - exit app\n");
696 ("\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");
697 printf ("\tdel <index> - delete memif\n");
698 printf ("\tshow - show connection details\n");
699 printf ("\tip-set <index> <ip-addr> - set interface ip address\n");
701 ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n");
702 printf ("\tsh-count - print counters\n");
703 printf ("\tcl-count - clear counters\n");
705 ("\tsend <index> <tx> <ip> <mac> - send icmp, ommit mac to transmit on ip layer\n");
711 /* application cleanup */
717 for (i = 0; i < MAX_CONNS; i++)
719 memif_connection_t *c = &memif_connection[i];
721 icmpr_memif_delete (i);
724 err = memif_cleanup ();
725 if (err != MEMIF_ERR_SUCCESS)
726 INFO ("memif_delete: %s", memif_strerror (err));
732 icmpr_set_ip (long index, char *ip)
734 if (index >= MAX_CONNS)
736 INFO ("connection array overflow");
741 INFO ("don't even try...");
744 memif_connection_t *c = &memif_connection[index];
747 INFO ("no connection at index %ld", index);
754 ui = strtok (ip, ".");
757 tmp[0] = strtol (ui, &end, 10);
759 ui = strtok (NULL, ".");
762 tmp[1] = strtol (ui, &end, 10);
764 ui = strtok (NULL, ".");
767 tmp[2] = strtol (ui, &end, 10);
769 ui = strtok (NULL, ".");
772 tmp[3] = strtol (ui, &end, 10);
774 c->ip_addr[0] = tmp[0];
775 c->ip_addr[1] = tmp[1];
776 c->ip_addr[2] = tmp[2];
777 c->ip_addr[3] = tmp[3];
779 INFO ("memif %ld ip address set to %u.%u.%u.%u",
780 index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
785 INFO ("invalid ip address");
790 icmpr_set_rx_mode (long index, long qid, char *mode)
792 if (index >= MAX_CONNS)
794 INFO ("connection array overflow");
799 INFO ("don't even try...");
802 memif_connection_t *c = &memif_connection[index];
806 INFO ("no connection at index %ld", index);
810 if (strncmp (mode, "interrupt", 9) == 0)
812 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid);
815 else if (strncmp (mode, "polling", 7) == 0)
817 memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid);
820 INFO ("expected rx mode <interrupt|polling>");
825 icmpr_print_counters ()
828 for (i = 0; i < MAX_CONNS; i++)
830 memif_connection_t *c = &memif_connection[i];
833 printf ("===============================\n");
834 printf ("interface index: %d\n", c->index);
835 printf ("\trx: %lu\n", c->rx_counter);
836 printf ("\ttx: %lu\n", c->tx_counter);
837 printf ("\ttx_err: %lu\n", c->tx_err_counter);
838 printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec);
843 icmpr_reset_counters ()
846 for (i = 0; i < MAX_CONNS; i++)
848 memif_connection_t *c = &memif_connection[i];
851 c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter =
859 memif_connection_t *c = &memif_connection[flow->index];
862 INFO ("No connection at index %d. Stopping flow...\n", flow->index);
866 int err = MEMIF_ERR_SUCCESS;
870 flow->start = malloc (sizeof (struct timespec));
871 memset (flow->start, 0, sizeof (struct timespec));
872 timespec_get (flow->start, TIME_UTC);
876 err = memif_buffer_alloc (c->conn, 0, c->bufs,
878 flow->packet_count ? flow->packet_count :
879 MAX_MEMIF_BUFS, &tx, 64);
880 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
882 INFO ("memif_buffer_alloc: %s Stopping flow...\n",
883 memif_strerror (err));
890 generate_packet2 ((void *) c->bufs[i].data,
891 &c->bufs[i].len, c->ip_addr,
892 flow->ip_daddr, flow->hw_daddr, (flow->sequence)++,
897 err = memif_tx_burst (c->conn, 0, c->bufs, i, &tx);
898 if (err != MEMIF_ERR_SUCCESS)
900 INFO ("memif_tx_burst: %s Stopping flow...\n", memif_strerror (err));
906 flow->packet_count -= tx;
908 if (flow->packet_count == 0)
910 timespec_get (&flow->end, TIME_UTC);
911 INFO ("Flow finished!");
912 INFO ("Flow length: %lu", flow->tx);
913 uint64_t t1 = flow->end.tv_sec - flow->start->tv_sec;
915 if (flow->end.tv_nsec > flow->start->tv_nsec)
917 t2 = flow->end.tv_nsec - flow->start->tv_nsec;
921 t2 = flow->start->tv_nsec - flow->end.tv_nsec;
926 INFO ("Flow time: %lus %luns", t1, t2);
929 tmp = flow->tx / tmp;
930 INFO ("Average pps: %f", tmp);
931 INFO ("Stopping flow...");
949 icmpr_send (long index, long packet_num, char *input)
953 printf ("only one flow allowed\n");
957 memif_connection_t *c = &memif_connection[index];
964 flow = malloc (sizeof (icmpr_flow_t));
966 flow->packet_count = packet_num;
970 memset (&flow->end, 0, sizeof (struct timespec));
972 INFO ("packet count: %lu", flow->packet_count);
973 printf ("%s\n", input);
975 ui = strtok (input, ".");
978 tmp[0] = strtol (ui, &end, 10);
980 ui = strtok (NULL, ".");
983 tmp[1] = strtol (ui, &end, 10);
985 ui = strtok (NULL, ".");
988 tmp[2] = strtol (ui, &end, 10);
990 ui = strtok (NULL, ".");
993 tmp[3] = strtol (ui, &end, 10);
995 flow->ip_daddr[0] = tmp[0];
996 flow->ip_daddr[1] = tmp[1];
997 flow->ip_daddr[2] = tmp[2];
998 flow->ip_daddr[3] = tmp[3];
1000 ui = strtok (NULL, " ");
1003 flow->mode = ICMPR_FLOW_MODE_IP;
1007 ui = strtok (NULL, ":");
1010 tmp[0] = strtol (ui, &end, 16);
1011 ui = strtok (NULL, ":");
1014 tmp[1] = strtol (ui, &end, 16);
1015 ui = strtok (NULL, ":");
1018 tmp[2] = strtol (ui, &end, 16);
1019 ui = strtok (NULL, ":");
1022 tmp[3] = strtol (ui, &end, 16);
1023 ui = strtok (NULL, ":");
1026 tmp[4] = strtol (ui, &end, 16);
1027 ui = strtok (NULL, ":");
1030 tmp[5] = strtol (ui, &end, 16);
1032 flow->hw_daddr[0] = tmp[0];
1033 flow->hw_daddr[1] = tmp[1];
1034 flow->hw_daddr[2] = tmp[2];
1035 flow->hw_daddr[3] = tmp[3];
1036 flow->hw_daddr[4] = tmp[4];
1037 flow->hw_daddr[5] = tmp[5];
1039 flow->mode = ICMPR_FLOW_MODE_ETH;
1044 INFO ("Invalid input\n");
1052 user_input_handler ()
1055 char *in = (char *) malloc (256);
1056 char *ui = fgets (in, 256, stdin);
1061 ui = strtok (in, " ");
1062 if (strncmp (ui, "exit", 4) == 0)
1066 exit (EXIT_SUCCESS);
1068 else if (strncmp (ui, "help", 4) == 0)
1073 else if (strncmp (ui, "conn", 4) == 0)
1075 ui = strtok (NULL, " ");
1077 a = strtol (ui, &end, 10);
1080 INFO ("expected id");
1083 ui = strtok (NULL, " ");
1085 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
1087 INFO ("expected mode <0|1>");
1090 else if (strncmp (ui, "del", 3) == 0)
1092 ui = strtok (NULL, " ");
1094 icmpr_memif_delete (strtol (ui, &end, 10));
1096 INFO ("expected id");
1099 else if (strncmp (ui, "show", 4) == 0)
1101 print_memif_details ();
1104 else if (strncmp (ui, "ip-set", 6) == 0)
1106 ui = strtok (NULL, " ");
1108 icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " "));
1110 INFO ("expected id");
1113 else if (strncmp (ui, "rx-mode", 7) == 0)
1115 ui = strtok (NULL, " ");
1117 a = strtol (ui, &end, 10);
1120 INFO ("expected id");
1123 ui = strtok (NULL, " ");
1125 icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " "));
1127 INFO ("expected qid");
1130 else if (strncmp (ui, "sh-count", 8) == 0)
1132 icmpr_print_counters ();
1134 else if (strncmp (ui, "cl-count", 8) == 0)
1136 icmpr_reset_counters ();
1138 else if (strncmp (ui, "send", 4) == 0)
1140 ui = strtok (NULL, " ");
1142 a = strtol (ui, &end, 10);
1145 INFO ("expected id");
1148 ui = strtok (NULL, " ");
1150 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "));
1152 INFO ("expected count");
1157 INFO ("unknown command: %s", ui);
1168 poll_event (int timeout)
1170 struct epoll_event evt, *e;
1171 int app_err = 0, memif_err = 0, en = 0;
1173 uint32_t events = 0;
1174 struct timespec start, end;
1175 memset (&evt, 0, sizeof (evt));
1176 evt.events = EPOLLIN | EPOLLOUT;
1178 sigemptyset (&sigset);
1179 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
1180 /* id event polled */
1181 timespec_get (&start, TIME_UTC);
1184 DBG ("epoll_pwait: %s", strerror (errno));
1189 /* this app does not use any other file descriptors than stds and memif control fds */
1190 if (evt.data.fd > 2)
1192 /* event of memif control fd */
1193 /* convert epolle events to memif events */
1194 if (evt.events & EPOLLIN)
1195 events |= MEMIF_FD_EVENT_READ;
1196 if (evt.events & EPOLLOUT)
1197 events |= MEMIF_FD_EVENT_WRITE;
1198 if (evt.events & EPOLLERR)
1199 events |= MEMIF_FD_EVENT_ERROR;
1200 memif_err = memif_control_fd_handler (evt.data.fd, events);
1201 if (memif_err != MEMIF_ERR_SUCCESS)
1202 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1204 else if (evt.data.fd == 0)
1206 app_err = user_input_handler ();
1210 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1214 timespec_get (&end, TIME_UTC);
1215 LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec);
1217 if ((app_err < 0) || (memif_err < 0))
1220 DBG ("user input handler error");
1222 DBG ("memif control fd handler error");
1232 epfd = epoll_create (1);
1233 add_epoll_fd (0, EPOLLIN);
1241 out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO);
1243 INFO ("Error opening log file: %s", strerror (errno));
1244 #endif /* LOG_FILE */
1246 /* initialize memory interface */
1248 /* if valid callback is passed as argument, fd event polling will be done by user
1249 all file descriptors and events will be passed to user in this callback */
1250 /* if callback is set to NULL libmemif will handle fd event polling */
1251 err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
1252 if (err != MEMIF_ERR_SUCCESS)
1254 INFO ("memif_init: %s", memif_strerror (err));
1259 for (i = 0; i < MAX_CONNS; i++)
1261 memif_connection[i].conn = NULL;
1270 if (poll_event (0) < 0)
1272 DBG ("poll_event error!");