2 *------------------------------------------------------------------
3 * Copyright (c) 2018 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 *------------------------------------------------------------------
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
29 #include <sys/prctl.h>
35 #include <linux/icmp.h>
36 #include <arpa/inet.h>
38 #include <netinet/if_ether.h>
39 #include <net/if_arp.h>
40 #include <asm/byteorder.h>
43 #include <sys/epoll.h>
55 #include <icmp_proto.h>
57 #define APP_NAME "ICMP_Responder"
58 #define IF_NAME "memif_connection"
61 #define DBG(...) do { \
62 printf(APP_NAME":%s:%d",__func__,__LINE__); \
63 printf(__VA_ARGS__); \
70 #define INFO(...) do { \
71 printf("INFO: "__VA_ARGS__); \
75 #define MAX_MEMIF_BUFS 256
79 #ifndef __NR_memfd_create
80 #if defined __x86_64__
81 #define __NR_memfd_create 319
83 #define __NR_memfd_create 385
84 #elif defined __aarch64__
85 #define __NR_memfd_create 279
87 #error "__NR_memfd_create unknown for this architecture"
91 #ifndef HAVE_MEMFD_CREATE
93 memfd_create (const char *name, unsigned int flags)
95 return syscall (__NR_memfd_create, name, flags);
99 #ifndef F_LINUX_SPECIFIC_BASE
100 #define F_LINUX_SPECIFIC_BASE 1024
103 #ifndef MFD_ALLOW_SEALING
104 #define MFD_ALLOW_SEALING 0x0002U
108 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
109 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
111 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
112 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
113 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
114 #define F_SEAL_WRITE 0x0008 /* prevent writes */
121 memif_conn_handle_t conn;
125 memif_buffer_t *tx_bufs;
126 memif_buffer_t *rx_bufs;
129 uint64_t tx_counter, rx_counter, tx_err_counter;
130 uint64_t t_sec, t_nsec;
131 } memif_connection_t;
139 } icmpr_thread_data_t;
141 icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
142 pthread_t thread[MAX_CONNS];
145 memif_connection_t memif_connection[MAX_CONNS];
150 printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
155 printf ("==============================\n");
156 printf ("libmemif version: %s", LIBMEMIF_VERSION);
161 printf ("memif version: %d\n", memif_get_version ());
162 printf ("commands:\n");
163 printf ("\thelp - prints this help\n");
164 printf ("\texit - exit app\n");
166 ("\tconn <index> <mode> <interrupt> - create memif. index used as interface id. mode: 0 = slave | 1 = master. interrupt: none = default | 1 = handle ARP only\n");
167 printf ("\tdel <index> - delete memif\n");
168 printf ("\tshow - show connection details\n");
169 printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n");
173 print_memif_details ()
180 buf = malloc (buflen);
181 printf ("MEMIF DETAILS\n");
182 printf ("==============================\n");
183 for (i = 0; i < MAX_CONNS; i++)
185 memif_connection_t *c = &memif_connection[i];
187 memset (&md, 0, sizeof (md));
188 memset (buf, 0, buflen);
190 err = memif_get_details (c->conn, &md, buf, buflen);
191 if (err != MEMIF_ERR_SUCCESS)
193 if (err != MEMIF_ERR_NOCONN)
194 INFO ("%s", memif_strerror (err));
198 printf ("interface index: %d\n", i);
200 printf ("\tinterface ip: %u.%u.%u.%u\n",
201 c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]);
202 printf ("\tinterface name: %s\n", (char *) md.if_name);
203 printf ("\tapp name: %s\n", (char *) md.inst_name);
204 printf ("\tremote interface name: %s\n", (char *) md.remote_if_name);
205 printf ("\tremote app name: %s\n", (char *) md.remote_inst_name);
206 printf ("\tid: %u\n", md.id);
207 printf ("\tsecret: %s\n", (char *) md.secret);
217 printf ("ethernet\n");
223 printf ("punt/inject\n");
226 printf ("unknown\n");
229 printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
230 printf ("\tregions:\n");
231 for (e = 0; e < md.regions_num; e++)
233 printf ("\t\tindex: %u\n", md.regions[e].index);
234 printf ("\t\taddress: %p\n", md.regions[e].addr);
235 printf ("\t\tsize: %u\n", md.regions[e].size);
236 printf ("\t\tfd: %d\n", md.regions[e].fd);
237 if (md.regions[e].is_external)
238 printf ("\t\texternal\n");
240 printf ("\trx queues:\n");
241 for (e = 0; e < md.rx_queues_num; e++)
243 printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
244 printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
245 printf ("\t\tring rx mode: %s\n",
246 md.rx_queues[e].flags ? "polling" : "interrupt");
247 printf ("\t\tring head: %u\n", md.rx_queues[e].head);
248 printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
249 printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
251 printf ("\ttx queues:\n");
252 for (e = 0; e < md.tx_queues_num; e++)
254 printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
255 printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
256 printf ("\t\tring rx mode: %s\n",
257 md.tx_queues[e].flags ? "polling" : "interrupt");
258 printf ("\t\tring head: %u\n", md.tx_queues[e].head);
259 printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
260 printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
272 icmpr_print_counters ()
275 for (i = 0; i < MAX_CONNS; i++)
277 memif_connection_t *c = &memif_connection[i];
280 printf ("===============================\n");
281 printf ("interface index: %d\n", c->index);
282 printf ("\trx: %lu\n", c->rx_counter);
283 printf ("\ttx: %lu\n", c->tx_counter);
284 printf ("\ttx_err: %lu\n", c->tx_err_counter);
289 icmpr_reset_counters ()
292 for (i = 0; i < MAX_CONNS; i++)
294 memif_connection_t *c = &memif_connection[i];
297 c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
302 add_epoll_fd (int fd, uint32_t events)
306 DBG ("invalid fd %d", fd);
309 struct epoll_event evt;
310 memset (&evt, 0, sizeof (evt));
313 if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
315 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
318 DBG ("fd %d added to epoll", fd);
323 mod_epoll_fd (int fd, uint32_t events)
327 DBG ("invalid fd %d", fd);
330 struct epoll_event evt;
331 memset (&evt, 0, sizeof (evt));
334 if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
336 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
339 DBG ("fd %d moddified on epoll", fd);
344 del_epoll_fd (int fd)
348 DBG ("invalid fd %d", fd);
351 struct epoll_event evt;
352 memset (&evt, 0, sizeof (evt));
353 if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
355 DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
358 DBG ("fd %d removed from epoll", fd);
363 on_connect (memif_conn_handle_t conn, void *private_ctx)
365 INFO ("memif connected!");
366 memif_refill_queue (conn, 0, -1, 0);
371 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
373 INFO ("memif disconnected!");
378 control_fd_update (int fd, uint8_t events)
380 /* convert memif event definitions to epoll events */
381 if (events & MEMIF_FD_EVENT_DEL)
382 return del_epoll_fd (fd);
385 if (events & MEMIF_FD_EVENT_READ)
387 if (events & MEMIF_FD_EVENT_WRITE)
390 if (events & MEMIF_FD_EVENT_MOD)
391 return mod_epoll_fd (fd, evt);
393 return add_epoll_fd (fd, evt);
397 icmpr_memif_delete (long index)
399 if (index >= MAX_CONNS)
401 INFO ("connection array overflow");
406 INFO ("don't even try...");
409 memif_connection_t *c = &memif_connection[index];
421 /* disconenct then delete memif connection */
422 err = memif_delete (&c->conn);
423 if (err != MEMIF_ERR_SUCCESS)
424 INFO ("memif_delete: %s", memif_strerror (err));
426 INFO ("memif delete fail");
433 /* application cleanup */
436 for (i = 0; i < MAX_CONNS; i++)
438 memif_connection_t *c = &memif_connection[i];
440 icmpr_memif_delete (i);
443 err = memif_cleanup ();
444 if (err != MEMIF_ERR_SUCCESS)
445 INFO ("memif_delete: %s", memif_strerror (err));
451 icmpr_add_external_region (void * *addr, uint32_t size, int *fd,
459 rfd = memfd_create ("memif region 1", MFD_ALLOW_SEALING);
461 fcntl (rfd, F_ADD_SEALS, F_SEAL_SHRINK);
463 err = ftruncate (rfd, size);
468 raddr = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, rfd, 0);
477 icmpr_get_external_region_addr (uint32_t size, int fd, void *private_ctx)
479 return mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
483 icmpr_del_external_region (void *addr, uint32_t size, int fd,
494 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
496 long index = *((long *) private_ctx);
497 memif_connection_t *c = &memif_connection[index];
498 if (c->index != index)
500 INFO ("invalid context: %ld/%u", index, c->index);
504 int err = MEMIF_ERR_SUCCESS, ret_val;
505 uint16_t rx = 0, tx = 0;
511 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
514 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
516 INFO ("memif_rx_burst: %s", memif_strerror (err));
520 memset (c->tx_bufs, 0, sizeof (memif_buffer_t) * rx);
521 err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
522 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
524 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
528 c->tx_err_counter += rx - tx;
532 resolve_packet ((void *) (c->rx_bufs + i)->data,
533 (c->rx_bufs + i)->len,
534 (void *) (c->tx_bufs + j)->data,
535 &(c->tx_bufs + j)->len, c->ip_addr);
541 err = memif_refill_queue (c->conn, qid, rx, 0);
542 if (err != MEMIF_ERR_SUCCESS)
543 INFO ("memif_buffer_free: %s", memif_strerror (err));
546 DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
548 err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
549 if (err != MEMIF_ERR_SUCCESS)
551 INFO ("memif_tx_burst: %s", memif_strerror (err));
556 while (ret_val == MEMIF_ERR_NOBUF);
561 err = memif_refill_queue (c->conn, qid, rx, 0);
562 if (err != MEMIF_ERR_SUCCESS)
563 INFO ("memif_buffer_free: %s", memif_strerror (err));
565 DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num,
566 MAX_MEMIF_BUFS - c->rx_buf_num);
571 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
573 long index = *((long *) private_ctx);
574 memif_connection_t *c = &memif_connection[index];
575 if (c->index != index)
577 INFO ("invalid context: %ld/%u", index, c->index);
581 int err = MEMIF_ERR_SUCCESS, ret_val;
587 /* receive data from shared memory buffers */
588 err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
590 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
592 INFO ("memif_rx_burst: %s", memif_strerror (err));
598 for (i = 0; i < rx; i++)
600 if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
604 memif_buffer_alloc (c->conn, qid, c->tx_bufs, 1, &tx, 128);
605 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
607 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
610 resolve_packet ((void *) (c->rx_bufs + i)->data,
611 (c->rx_bufs + i)->len,
612 (void *) (c->tx_bufs + i)->data,
613 &(c->tx_bufs + i)->len, c->ip_addr);
615 memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &tx);
616 if (err != MEMIF_ERR_SUCCESS)
617 INFO ("memif_tx_burst: %s", memif_strerror (err));
623 err = memif_refill_queue (c->conn, qid, rx, 0);
624 if (err != MEMIF_ERR_SUCCESS)
625 INFO ("memif_buffer_free: %s", memif_strerror (err));
630 while (ret_val == MEMIF_ERR_NOBUF);
635 err = memif_refill_queue (c->conn, qid, rx, 0);
636 if (err != MEMIF_ERR_SUCCESS)
637 INFO ("memif_buffer_free: %s", memif_strerror (err));
639 DBG ("freed %d buffers. %u/%u alloc/free buffers",
640 fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
645 icmpr_memif_create (long index, long mode, char *s)
647 if (index >= MAX_CONNS)
649 INFO ("connection array overflow");
657 memif_connection_t *c = &memif_connection[index];
659 memif_conn_args_t args;
660 memset (&args, 0, sizeof (args));
661 args.is_master = mode;
662 args.log2_ring_size = 11;
663 args.buffer_size = 2048;
664 args.num_s2m_rings = 1;
665 args.num_m2s_rings = 1;
666 strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
668 args.interface_id = index;
673 err = memif_create (&c->conn,
674 &args, on_connect, on_disconnect, on_interrupt,
676 if (err != MEMIF_ERR_SUCCESS)
678 INFO ("memif_create: %s", memif_strerror (err));
684 if (strncmp (s, "1", 1) == 0)
686 err = memif_create (&c->conn,
687 &args, on_connect, on_disconnect, on_interrupt1,
689 if (err != MEMIF_ERR_SUCCESS)
691 INFO ("memif_create: %s", memif_strerror (err));
697 INFO ("Unknown interrupt descriptor");
705 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
708 (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
712 c->ip_addr[2] = c->index + 1;
720 icmpr_send_proc (void *data)
722 icmpr_thread_data_t *d = (icmpr_thread_data_t *) data;
723 int index = d->index;
724 uint64_t count = d->packet_num;
725 memif_connection_t *c = &memif_connection[index];
728 INFO ("No connection at index %d.", index);
732 int err = MEMIF_ERR_SUCCESS;
734 struct timespec start, end;
735 memset (&start, 0, sizeof (start));
736 memset (&end, 0, sizeof (end));
738 timespec_get (&start, TIME_UTC);
743 memif_buffer_alloc (c->conn, 0, c->tx_bufs,
744 MAX_MEMIF_BUFS > count ? count : MAX_MEMIF_BUFS,
747 if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
749 INFO ("memif_buffer_alloc: %s", memif_strerror (err));
758 generate_packet ((void *) c->tx_bufs[i].data,
759 &c->tx_bufs[i].len, c->ip_addr, d->ip_daddr,
761 generate_packet ((void *) c->tx_bufs[i + 1].data,
762 &c->tx_bufs[i + 1].len, c->ip_addr,
763 d->ip_daddr, d->hw_daddr, seq++);
764 generate_packet ((void *) c->tx_bufs[i + 2].data,
765 &c->tx_bufs[i + 2].len, c->ip_addr,
766 d->ip_daddr, d->hw_daddr, seq++);
767 generate_packet ((void *) c->tx_bufs[i + 3].data,
768 &c->tx_bufs[i + 3].len, c->ip_addr,
769 d->ip_daddr, d->hw_daddr, seq++);
773 generate_packet ((void *) c->tx_bufs[i].data, &c->tx_bufs[i].len,
774 c->ip_addr, d->ip_daddr, d->hw_daddr, seq++);
779 err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
780 if (err != MEMIF_ERR_SUCCESS)
782 INFO ("memif_tx_burst: %s", memif_strerror (err));
790 timespec_get (&end, TIME_UTC);
791 INFO ("\n\nPacket sequence finished!\nSeq len: %u", seq);
792 uint64_t t1 = end.tv_sec - start.tv_sec;
794 if (end.tv_nsec > start.tv_nsec)
796 t2 = end.tv_nsec - start.tv_nsec;
800 t2 = start.tv_nsec - end.tv_nsec;
808 INFO ("Seq time: %lus %luns\nAverage pps: %f", t1, t2, tmp);
811 INFO ("Thread exiting...");
817 icmpr_send (long index, long packet_num, char *hw, char *ip)
819 if (index >= MAX_CONNS)
821 INFO ("connection array overflow");
829 memif_connection_t *c = &memif_connection[index];
834 icmpr_thread_data_t *data = &icmpr_thread_data[index];
836 data->packet_num = packet_num;
838 ui = strtok (ip, ".");
841 tmp[0] = strtol (ui, &end, 10);
842 ui = strtok (NULL, ".");
845 tmp[1] = strtol (ui, &end, 10);
846 ui = strtok (NULL, ".");
849 tmp[2] = strtol (ui, &end, 10);
850 ui = strtok (NULL, ".");
853 tmp[3] = strtol (ui, &end, 10);
855 data->ip_daddr[0] = tmp[0];
856 data->ip_daddr[1] = tmp[1];
857 data->ip_daddr[2] = tmp[2];
858 data->ip_daddr[3] = tmp[3];
860 ui = strtok (hw, ":");
863 tmp[0] = strtol (ui, &end, 16);
864 ui = strtok (NULL, ":");
867 tmp[1] = strtol (ui, &end, 16);
868 ui = strtok (NULL, ":");
871 tmp[2] = strtol (ui, &end, 16);
872 ui = strtok (NULL, ":");
875 tmp[3] = strtol (ui, &end, 16);
876 ui = strtok (NULL, ":");
879 tmp[4] = strtol (ui, &end, 16);
880 ui = strtok (NULL, ":");
883 tmp[5] = strtol (ui, &end, 16);
885 pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
889 INFO ("Invalid input\n");
894 user_input_handler ()
896 char *in = (char *) malloc (256);
897 char *ui = fgets (in, 256, stdin);
904 ui = strtok (in, " ");
906 if (strncmp (ui, "exit", 4) == 0)
912 else if (strncmp (ui, "help", 4) == 0)
917 else if (strncmp (ui, "conn", 4) == 0)
919 ui = strtok (NULL, " ");
921 a = strtol (ui, &end, 10);
924 INFO ("expected id");
927 ui = strtok (NULL, " ");
929 icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
931 INFO ("expected mode <0|1>");
934 else if (strncmp (ui, "del", 3) == 0)
936 ui = strtok (NULL, " ");
938 icmpr_memif_delete (strtol (ui, &end, 10));
941 INFO ("expected id");
945 else if (strncmp (ui, "send", 4) == 0)
947 ui = strtok (NULL, " ");
949 a = strtol (ui, &end, 10);
952 INFO ("expected id");
955 ui = strtok (NULL, " ");
957 icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
960 INFO ("expected count");
963 else if (strncmp (ui, "show", 4) == 0)
965 print_memif_details ();
975 poll_event (int timeout)
977 struct epoll_event evt;
978 int app_err = 0, memif_err = 0, en = 0;
980 memset (&evt, 0, sizeof (evt));
981 evt.events = EPOLLIN | EPOLLOUT;
983 sigemptyset (&sigset);
984 en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
985 /* id event polled */
988 DBG ("epoll_pwait: %s", strerror (errno));
993 /* this app does not use any other file descriptors than stds and memif control fds */
996 /* event of memif control fd */
997 /* convert epolle events to memif events */
998 if (evt.events & EPOLLIN)
999 events |= MEMIF_FD_EVENT_READ;
1000 if (evt.events & EPOLLOUT)
1001 events |= MEMIF_FD_EVENT_WRITE;
1002 if (evt.events & EPOLLERR)
1003 events |= MEMIF_FD_EVENT_ERROR;
1004 memif_err = memif_control_fd_handler (evt.data.fd, events);
1005 if (memif_err != MEMIF_ERR_SUCCESS)
1006 INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1008 else if (evt.data.fd == 0)
1010 app_err = user_input_handler ();
1014 DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1018 if ((app_err < 0) || (memif_err < 0))
1021 DBG ("user input handler error");
1023 DBG ("memif control fd handler error");
1033 epfd = epoll_create (1);
1034 add_epoll_fd (0, EPOLLIN);
1036 /* initialize memory interface */
1038 /* if valid callback is passed as argument, fd event polling will be done by user
1039 all file descriptors and events will be passed to user in this callback */
1040 /* if callback is set to NULL libmemif will handle fd event polling */
1041 err = memif_init (control_fd_update, APP_NAME, NULL, NULL, NULL);
1042 if (err != MEMIF_ERR_SUCCESS)
1044 INFO ("memif_init: %s", memif_strerror (err));
1049 for (i = 0; i < MAX_CONNS; i++)
1051 memset (&memif_connection[i], 0, sizeof (memif_connection_t));
1055 memif_register_external_region (icmpr_add_external_region,
1056 icmpr_get_external_region_addr,
1057 icmpr_del_external_region, NULL);
1064 if (poll_event (-1) < 0)
1066 DBG ("poll_event error!");