FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
main.c
Go to the documentation of this file.
1 /*
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:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17 
18 #define _GNU_SOURCE
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <net/if.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <sys/uio.h>
28 #include <sys/mman.h>
29 #include <sys/prctl.h>
30 #include <inttypes.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <netdb.h>
34 #include <linux/ip.h>
35 #include <linux/icmp.h>
36 #include <arpa/inet.h>
37 #include <stdlib.h>
38 #include <netinet/if_ether.h>
39 #include <net/if_arp.h>
40 #include <asm/byteorder.h>
41 #include <byteswap.h>
42 #include <string.h>
43 #include <sys/epoll.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <signal.h>
47 #include <pthread.h>
48 #include <time.h>
49 
50 #ifndef TIME_UTC
51 #define TIME_UTC 1
52 #endif /* TIME_UTC */
53 
54 #include <libmemif.h>
55 #include <icmp_proto.h>
56 
57 #define APP_NAME "ICMP_Responder"
58 #define IF_NAME "memif_connection"
59 
60 #ifdef ICMP_DBG
61 #define DBG(...) do { \
62  printf(APP_NAME":%s:%d",__func__,__LINE__); \
63  printf(__VA_ARGS__); \
64  printf("\n"); \
65  } while (0)
66 #else
67 #define DBG(...)
68 #endif /* ICMP_DBG */
69 
70 #define INFO(...) do { \
71  printf("INFO: "__VA_ARGS__); \
72  printf("\n"); \
73  } while (0)
74 
75 #define MAX_MEMIF_BUFS 256
76 #define MAX_CONNS 50
77 
78 
79 #ifndef __NR_memfd_create
80 #if defined __x86_64__
81 #define __NR_memfd_create 319
82 #elif defined __arm__
83 #define __NR_memfd_create 385
84 #elif defined __aarch64__
85 #define __NR_memfd_create 279
86 #else
87 #error "__NR_memfd_create unknown for this architecture"
88 #endif
89 #endif
90 
91 #ifndef HAVE_MEMFD_CREATE
92 static inline int
93 memfd_create (const char *name, unsigned int flags)
94 {
95  return syscall (__NR_memfd_create, name, flags);
96 }
97 #endif
98 
99 #ifndef F_LINUX_SPECIFIC_BASE
100 #define F_LINUX_SPECIFIC_BASE 1024
101 #endif
102 
103 #ifndef MFD_ALLOW_SEALING
104 #define MFD_ALLOW_SEALING 0x0002U
105 #endif
106 
107 #ifndef F_ADD_SEALS
108 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
109 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
110 
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 */
115 #endif
116 
117 typedef struct
118 {
119  uint16_t index;
120 
121  memif_conn_handle_t conn;
122 
123  uint16_t tx_buf_num;
124  uint16_t rx_buf_num;
125  memif_buffer_t *tx_bufs;
126  memif_buffer_t *rx_bufs;
127 
128  uint8_t ip_addr[4];
129  uint64_t tx_counter, rx_counter, tx_err_counter;
130  uint64_t t_sec, t_nsec;
132 
133 typedef struct
134 {
135  uint16_t index;
136  uint64_t packet_num;
137  uint8_t ip_daddr[4];
138  uint8_t hw_daddr[6];
140 
142 pthread_t thread[MAX_CONNS];
143 int epfd;
146 
147 static void
149 {
150  printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME);
151 #ifdef ICMP_DBG
152  printf (" (debug)");
153 #endif
154  printf ("\n");
155  printf ("==============================\n");
156  printf ("libmemif version: %s", LIBMEMIF_VERSION);
157 #ifdef MEMIF_DBG
158  printf (" (debug)");
159 #endif
160  printf ("\n");
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");
165  printf
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");
170 }
171 
172 static void
174 {
175  memif_details_t md;
176  ssize_t buflen;
177  char *buf;
178  int err, i, e;
179  buflen = 2048;
180  buf = malloc (buflen);
181  printf ("MEMIF DETAILS\n");
182  printf ("==============================\n");
183  for (i = 0; i < MAX_CONNS; i++)
184  {
186 
187  memset (&md, 0, sizeof (md));
188  memset (buf, 0, buflen);
189 
190  err = memif_get_details (c->conn, &md, buf, buflen);
191  if (err != MEMIF_ERR_SUCCESS)
192  {
193  if (err != MEMIF_ERR_NOCONN)
194  INFO ("%s", memif_strerror (err));
195  continue;
196  }
197 
198  printf ("interface index: %d\n", i);
199 
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);
208  printf ("\trole: ");
209  if (md.role)
210  printf ("slave\n");
211  else
212  printf ("master\n");
213  printf ("\tmode: ");
214  switch (md.mode)
215  {
216  case 0:
217  printf ("ethernet\n");
218  break;
219  case 1:
220  printf ("ip\n");
221  break;
222  case 2:
223  printf ("punt/inject\n");
224  break;
225  default:
226  printf ("unknown\n");
227  break;
228  }
229  printf ("\tsocket filename: %s\n", (char *) md.socket_filename);
230  printf ("\tregions:\n");
231  for (e = 0; e < md.regions_num; e++)
232  {
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");
239  }
240  printf ("\trx queues:\n");
241  for (e = 0; e < md.rx_queues_num; e++)
242  {
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);
250  }
251  printf ("\ttx queues:\n");
252  for (e = 0; e < md.tx_queues_num; e++)
253  {
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);
261  }
262  printf ("\tlink: ");
263  if (md.link_up_down)
264  printf ("up\n");
265  else
266  printf ("down\n");
267  }
268  free (buf);
269 }
270 
271 void
273 {
274  int i;
275  for (i = 0; i < MAX_CONNS; i++)
276  {
278  if (c->conn == NULL)
279  continue;
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);
285  }
286 }
287 
288 void
290 {
291  int i;
292  for (i = 0; i < MAX_CONNS; i++)
293  {
295  if (c->conn == NULL)
296  continue;
297  c->tx_err_counter = c->tx_counter = c->rx_counter = 0;
298  }
299 }
300 
301 int
302 add_epoll_fd (int fd, uint32_t events)
303 {
304  if (fd < 0)
305  {
306  DBG ("invalid fd %d", fd);
307  return -1;
308  }
309  struct epoll_event evt;
310  memset (&evt, 0, sizeof (evt));
311  evt.events = events;
312  evt.data.fd = fd;
313  if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
314  {
315  DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
316  return -1;
317  }
318  DBG ("fd %d added to epoll", fd);
319  return 0;
320 }
321 
322 int
323 mod_epoll_fd (int fd, uint32_t events)
324 {
325  if (fd < 0)
326  {
327  DBG ("invalid fd %d", fd);
328  return -1;
329  }
330  struct epoll_event evt;
331  memset (&evt, 0, sizeof (evt));
332  evt.events = events;
333  evt.data.fd = fd;
334  if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
335  {
336  DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
337  return -1;
338  }
339  DBG ("fd %d modified on epoll", fd);
340  return 0;
341 }
342 
343 int
344 del_epoll_fd (int fd)
345 {
346  if (fd < 0)
347  {
348  DBG ("invalid fd %d", fd);
349  return -1;
350  }
351  struct epoll_event evt;
352  memset (&evt, 0, sizeof (evt));
353  if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
354  {
355  DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
356  return -1;
357  }
358  DBG ("fd %d removed from epoll", fd);
359  return 0;
360 }
361 
362 int
363 on_connect (memif_conn_handle_t conn, void *private_ctx)
364 {
365  INFO ("memif connected!");
366  memif_refill_queue (conn, 0, -1, 0);
367  return 0;
368 }
369 
370 int
371 on_disconnect (memif_conn_handle_t conn, void *private_ctx)
372 {
373  INFO ("memif disconnected!");
374  return 0;
375 }
376 
377 int
378 control_fd_update (int fd, uint8_t events, void *ctx)
379 {
380  /* convert memif event definitions to epoll events */
382  return del_epoll_fd (fd);
383 
384  uint32_t evt = 0;
386  evt |= EPOLLIN;
388  evt |= EPOLLOUT;
389 
391  return mod_epoll_fd (fd, evt);
392 
393  return add_epoll_fd (fd, evt);
394 }
395 
396 int
398 {
399  if (index >= MAX_CONNS)
400  {
401  INFO ("connection array overflow");
402  return 0;
403  }
404  if (index < 0)
405  {
406  INFO ("don't even try...");
407  return 0;
408  }
410 
411  if (c->rx_bufs)
412  free (c->rx_bufs);
413  c->rx_bufs = NULL;
414  c->rx_buf_num = 0;
415  if (c->tx_bufs)
416  free (c->tx_bufs);
417  c->tx_bufs = NULL;
418  c->tx_buf_num = 0;
419 
420  int err;
421  /* disconnect then delete memif connection */
422  err = memif_delete (&c->conn);
423  if (err != MEMIF_ERR_SUCCESS)
424  INFO ("memif_delete: %s", memif_strerror (err));
425  if (c->conn != NULL)
426  INFO ("memif delete fail");
427  return 0;
428 }
429 
430 int
432 {
433  /* application cleanup */
434  int err;
435  long i;
436  for (i = 0; i < MAX_CONNS; i++)
437  {
439  if (c->conn)
441  }
442 
443  err = memif_cleanup ();
444  if (err != MEMIF_ERR_SUCCESS)
445  INFO ("memif_delete: %s", memif_strerror (err));
446 
447  return 0;
448 }
449 
450 int
451 icmpr_add_external_region (void * *addr, uint32_t size, int *fd,
452  void *private_ctx)
453 {
454 
455  int rfd;
456  void *raddr;
457  int err;
458 
459  rfd = memfd_create ("memif region 1", MFD_ALLOW_SEALING);
460 
461  fcntl (rfd, F_ADD_SEALS, F_SEAL_SHRINK);
462 
463  err = ftruncate (rfd, size);
464 
465  if (err)
466  return err;
467 
468  raddr = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, rfd, 0);
469 
470  *addr = raddr;
471  *fd = rfd;
472 
473  return 0;
474 }
475 
476 void *
477 icmpr_get_external_region_addr (uint32_t size, int fd, void *private_ctx)
478 {
479  return mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
480 }
481 
482 int
483 icmpr_del_external_region (void *addr, uint32_t size, int fd,
484  void *private_ctx)
485 {
486  munmap (addr, size);
487  if (fd > 0)
488  close (fd);
489  fd = -1;
490  return 0;
491 }
492 
493 int
494 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
495 {
496  long index = *((long *) private_ctx);
498  if (c->index != index)
499  {
500  INFO ("invalid context: %ld/%u", index, c->index);
501  return 0;
502  }
503 
504  int err = MEMIF_ERR_SUCCESS, ret_val;
505  uint16_t rx = 0, tx = 0;
506  int i = 0;
507  int j = 0;
508 
509  do
510  {
511  err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
512  ret_val = err;
513  c->rx_counter += rx;
514  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
515  {
516  INFO ("memif_rx_burst: %s", memif_strerror (err));
517  goto error;
518  }
519  i = 0;
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))
523  {
524  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
525  goto error;
526  }
527  j = 0;
528  c->tx_err_counter += rx - tx;
529 
530  while (tx)
531  {
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);
536  i++;
537  j++;
538  tx--;
539  }
540 
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));
544  rx -= rx;
545 
546  DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
547 
548  err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
549  if (err != MEMIF_ERR_SUCCESS)
550  {
551  INFO ("memif_tx_burst: %s", memif_strerror (err));
552  goto error;
553  }
554  c->tx_counter += tx;
555  }
556  while (ret_val == MEMIF_ERR_NOBUF);
557 
558  return 0;
559 
560 error:
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));
564  c->rx_buf_num -= rx;
565  DBG ("freed %d buffers. %u/%u alloc/free buffers", rx, c->rx_buf_num,
566  MAX_MEMIF_BUFS - c->rx_buf_num);
567  return 0;
568 }
569 
570 int
571 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
572 {
573  long index = *((long *) private_ctx);
575  if (c->index != index)
576  {
577  INFO ("invalid context: %ld/%u", index, c->index);
578  return 0;
579  }
580 
581  int err = MEMIF_ERR_SUCCESS, ret_val;
582  int i;
583  uint16_t rx, tx;
584 
585  do
586  {
587  /* receive data from shared memory buffers */
588  err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
589  ret_val = err;
590  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
591  {
592  INFO ("memif_rx_burst: %s", memif_strerror (err));
593  goto error;
594  }
595  c->rx_buf_num += rx;
596  c->rx_counter += rx;
597 
598  for (i = 0; i < rx; i++)
599  {
600  if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type ==
601  0x0608)
602  {
603  err =
604  memif_buffer_alloc (c->conn, qid, c->tx_bufs, 1, &tx, 128);
605  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
606  {
607  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
608  goto error;
609  }
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);
614  err =
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));
618  c->tx_buf_num -= tx;
619  c->tx_counter += tx;
620  }
621  }
622 
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));
626  c->rx_buf_num -= rx;
627 
628 
629  }
630  while (ret_val == MEMIF_ERR_NOBUF);
631 
632  return 0;
633 
634 error:
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));
638  c->rx_buf_num -= rx;
639  DBG ("freed %d buffers. %u/%u alloc/free buffers",
640  rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
641  return 0;
642 }
643 
644 int
645 icmpr_memif_create (long index, long mode, char *s)
646 {
647  if (index >= MAX_CONNS)
648  {
649  INFO ("connection array overflow");
650  return 0;
651  }
652  if (index < 0)
653  {
654  INFO ("...");
655  return 0;
656  }
658 
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));
667  args.mode = 0;
668  args.interface_id = index;
669 
670  int err;
671  if (s == NULL)
672  {
673  err = memif_create (&c->conn,
675  &ctx[index]);
676  if (err != MEMIF_ERR_SUCCESS)
677  {
678  INFO ("memif_create: %s", memif_strerror (err));
679  return 0;
680  }
681  }
682  else
683  {
684  if (strncmp (s, "1", 1) == 0)
685  {
686  err = memif_create (&c->conn,
688  &ctx[index]);
689  if (err != MEMIF_ERR_SUCCESS)
690  {
691  INFO ("memif_create: %s", memif_strerror (err));
692  return 0;
693  }
694  }
695  else
696  {
697  INFO ("Unknown interrupt descriptor");
698  goto done;
699  }
700  }
701 
702  c->index = index;
703  c->rx_buf_num = 0;
704  c->rx_bufs =
706  c->tx_buf_num = 0;
707  c->tx_bufs =
709 
710  c->ip_addr[0] = 192;
711  c->ip_addr[1] = 168;
712  c->ip_addr[2] = c->index + 1;
713  c->ip_addr[3] = 2;
714 
715 done:
716  return 0;
717 }
718 
719 void *
721 {
723  int index = d->index;
724  uint64_t count = d->packet_num;
726  if (c->conn == NULL)
727  {
728  INFO ("No connection at index %d.", index);
729  goto error;
730  }
731  uint16_t tx, i;
732  int err = MEMIF_ERR_SUCCESS;
733  uint32_t seq = 0;
734  struct timespec start, end;
735  memset (&start, 0, sizeof (start));
736  memset (&end, 0, sizeof (end));
737 
738  timespec_get (&start, TIME_UTC);
739  while (count)
740  {
741  i = 0;
742  err =
743  memif_buffer_alloc (c->conn, 0, c->tx_bufs,
745  &tx, 128);
746 
747  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
748  {
749  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
750  goto error;
751  }
752  c->tx_buf_num += tx;
753 
754  while (tx)
755  {
756  while (tx > 4)
757  {
758  generate_packet ((void *) c->tx_bufs[i].data,
759  &c->tx_bufs[i].len, c->ip_addr, d->ip_daddr,
760  d->hw_daddr, seq++);
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++);
770  i += 4;
771  tx -= 4;
772  }
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++);
775  i++;
776  tx--;
777  }
778 
779  err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
780  if (err != MEMIF_ERR_SUCCESS)
781  {
782  INFO ("memif_tx_burst: %s", memif_strerror (err));
783  goto error;
784  }
785  c->tx_buf_num -= tx;
786  c->tx_counter += tx;
787  count -= tx;
788  }
789 
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;
793  uint64_t t2;
794  if (end.tv_nsec > start.tv_nsec)
795  {
796  t2 = end.tv_nsec - start.tv_nsec;
797  }
798  else
799  {
800  t2 = start.tv_nsec - end.tv_nsec;
801  t1--;
802  }
803  c->t_sec = t1;
804  c->t_nsec = t2;
805  double tmp = t1;
806  tmp += t2 / 1e+9;
807  tmp = seq / tmp;
808  INFO ("Seq time: %lus %luns\nAverage pps: %f", t1, t2, tmp);
809 
810 error:
811  INFO ("Thread exiting...");
812  pthread_exit (NULL);
813 }
814 
815 
816 int
817 icmpr_send (long index, long packet_num, char *hw, char *ip)
818 {
819  if (index >= MAX_CONNS)
820  {
821  INFO ("connection array overflow");
822  return 0;
823  }
824  if (index < 0)
825  {
826  INFO ("...");
827  return 0;
828  }
830  if (c->conn == NULL)
831  return -1;
832  char *end, *ui;
833  uint8_t tmp[6];
835  data->index = index;
836  data->packet_num = packet_num;
837 
838  ui = strtok (ip, ".");
839  if (ui == NULL)
840  goto error;
841  tmp[0] = strtol (ui, &end, 10);
842  ui = strtok (NULL, ".");
843  if (ui == NULL)
844  goto error;
845  tmp[1] = strtol (ui, &end, 10);
846  ui = strtok (NULL, ".");
847  if (ui == NULL)
848  goto error;
849  tmp[2] = strtol (ui, &end, 10);
850  ui = strtok (NULL, ".");
851  if (ui == NULL)
852  goto error;
853  tmp[3] = strtol (ui, &end, 10);
854 
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];
859 
860  ui = strtok (hw, ":");
861  if (ui == NULL)
862  goto error;
863  tmp[0] = strtol (ui, &end, 16);
864  ui = strtok (NULL, ":");
865  if (ui == NULL)
866  goto error;
867  tmp[1] = strtol (ui, &end, 16);
868  ui = strtok (NULL, ":");
869  if (ui == NULL)
870  goto error;
871  tmp[2] = strtol (ui, &end, 16);
872  ui = strtok (NULL, ":");
873  if (ui == NULL)
874  goto error;
875  tmp[3] = strtol (ui, &end, 16);
876  ui = strtok (NULL, ":");
877  if (ui == NULL)
878  goto error;
879  tmp[4] = strtol (ui, &end, 16);
880  ui = strtok (NULL, ":");
881  if (ui == NULL)
882  goto error;
883  tmp[5] = strtol (ui, &end, 16);
884 
885  pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
886  return 0;
887 
888 error:
889  INFO ("Invalid input\n");
890  return 0;
891 }
892 
893 int
895 {
896  char *in = (char *) malloc (256);
897  char *ui = fgets (in, 256, stdin);
898  char *end;
899  long a;
900 
901  if (in[0] == '\n')
902  goto done;
903 
904  ui = strtok (in, " ");
905 
906  if (strncmp (ui, "exit", 4) == 0)
907  {
908  free (in);
909  icmpr_free ();
910  exit (EXIT_SUCCESS);
911  }
912  else if (strncmp (ui, "help", 4) == 0)
913  {
914  print_help ();
915  goto done;
916  }
917  else if (strncmp (ui, "conn", 4) == 0)
918  {
919  ui = strtok (NULL, " ");
920  if (ui != NULL)
921  a = strtol (ui, &end, 10);
922  else
923  {
924  INFO ("expected id");
925  goto done;
926  }
927  ui = strtok (NULL, " ");
928  if (ui != NULL)
929  icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
930  else
931  INFO ("expected mode <0|1>");
932  goto done;
933  }
934  else if (strncmp (ui, "del", 3) == 0)
935  {
936  ui = strtok (NULL, " ");
937  if (ui != NULL)
938  icmpr_memif_delete (strtol (ui, &end, 10));
939  else
940  {
941  INFO ("expected id");
942  goto done;
943  }
944  }
945  else if (strncmp (ui, "send", 4) == 0)
946  {
947  ui = strtok (NULL, " ");
948  if (ui != NULL)
949  a = strtol (ui, &end, 10);
950  else
951  {
952  INFO ("expected id");
953  goto done;
954  }
955  ui = strtok (NULL, " ");
956  if (ui != NULL)
957  icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
958  strtok (NULL, " "));
959  else
960  INFO ("expected count");
961  goto done;
962  }
963  else if (strncmp (ui, "show", 4) == 0)
964  {
966  goto done;
967  }
968 
969 done:
970  free (in);
971  return 0;
972 }
973 
974 int
975 poll_event (int timeout)
976 {
977  struct epoll_event evt;
978  int app_err = 0, memif_err = 0, en = 0;
979  uint32_t events = 0;
980  memset (&evt, 0, sizeof (evt));
981  evt.events = EPOLLIN | EPOLLOUT;
982  sigset_t sigset;
983  sigemptyset (&sigset);
984  en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
985  /* id event polled */
986  if (en < 0)
987  {
988  DBG ("epoll_pwait: %s", strerror (errno));
989  return -1;
990  }
991  if (en > 0)
992  {
993  /* this app does not use any other file descriptors than stds and memif control fds */
994  if (evt.data.fd > 2)
995  {
996  /* event of memif control fd */
997  /* convert epoll events to memif events */
998  if (evt.events & EPOLLIN)
1000  if (evt.events & EPOLLOUT)
1002  if (evt.events & EPOLLERR)
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));
1007  }
1008  else if (evt.data.fd == 0)
1009  {
1010  app_err = user_input_handler ();
1011  }
1012  else
1013  {
1014  DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1015  }
1016  }
1017 
1018  if ((app_err < 0) || (memif_err < 0))
1019  {
1020  if (app_err < 0)
1021  DBG ("user input handler error");
1022  if (memif_err < 0)
1023  DBG ("memif control fd handler error");
1024  return -1;
1025  }
1026 
1027  return 0;
1028 }
1029 
1030 int
1032 {
1033  epfd = epoll_create (1);
1034  add_epoll_fd (0, EPOLLIN);
1035 
1036  /* initialize memory interface */
1037  int err, i;
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)
1043  {
1044  INFO ("memif_init: %s", memif_strerror (err));
1045  icmpr_free ();
1046  exit (-1);
1047  }
1048 
1049  for (i = 0; i < MAX_CONNS; i++)
1050  {
1051  memset (&memif_connection[i], 0, sizeof (memif_connection_t));
1052  ctx[i] = i;
1053  }
1054 
1058 
1059  print_help ();
1060 
1061  /* main loop */
1062  while (1)
1063  {
1064  if (poll_event (-1) < 0)
1065  {
1066  DBG ("poll_event error!");
1067  }
1068  }
1069 }
memif_queue_details_t::head
uint16_t head
Definition: libmemif.h:361
tmp
u32 * tmp
Definition: interface_output.c:1078
MEMIF_FD_EVENT_MOD
#define MEMIF_FD_EVENT_MOD
update events
Definition: libmemif.h:98
DBG
#define DBG(...)
Definition: main.c:67
memif_register_external_region
void memif_register_external_region(memif_add_external_region_t *ar, memif_get_external_region_addr_t *gr, memif_del_external_region_t *dr, memif_get_external_buffer_offset_t *go)
Register external region.
Definition: main.c:431
epfd
int epfd
Definition: main.c:94
icmpr_print_counters
void icmpr_print_counters()
Definition: main.c:272
icmpr_thread_data_t::ip_daddr
uint8_t ip_daddr[4]
Definition: main.c:137
icmpr_thread_data
icmpr_thread_data_t icmpr_thread_data[MAX_CONNS]
Definition: main.c:141
memif_queue_details_t::flags
uint16_t flags
Definition: libmemif.h:360
MEMIF_FD_EVENT_WRITE
#define MEMIF_FD_EVENT_WRITE
Definition: libmemif.h:92
on_connect
int on_connect(memif_conn_handle_t conn, void *private_ctx)
Definition: main.c:177
memif_region_details_t::addr
void * addr
Definition: libmemif.h:376
memif_conn_args_t::mode
memif_interface_mode_t mode
Definition: libmemif.h:308
INFO
#define INFO(...)
Definition: main.c:70
control_fd_update
int control_fd_update(int fd, uint8_t events, void *ctx)
Definition: main.c:378
icmpr_free
int icmpr_free()
Definition: main.c:258
F_SEAL_SHRINK
#define F_SEAL_SHRINK
Definition: main.c:112
MEMIF_ERR_SUCCESS
@ MEMIF_ERR_SUCCESS
Definition: libmemif.h:38
name
string name[64]
Definition: fib.api:25
MAX_MEMIF_BUFS
#define MAX_MEMIF_BUFS
Definition: main.c:75
icmpr_memif_create
int icmpr_memif_create(int is_master)
Definition: main.c:340
memif_details_t::remote_if_name
uint8_t * remote_if_name
Definition: libmemif.h:405
string.h
MEMIF_ERR_NOBUF_RING
@ MEMIF_ERR_NOBUF_RING
Definition: libmemif.h:59
user_input_handler
int user_input_handler()
Definition: main.c:894
IF_NAME
#define IF_NAME
Definition: main.c:58
TIME_UTC
#define TIME_UTC
Definition: main.c:51
memif_queue_details_t::tail
uint16_t tail
Definition: libmemif.h:362
memif_conn_args_t::buffer_size
uint16_t buffer_size
Definition: libmemif.h:302
memif_queue_details_t::ring_size
uint32_t ring_size
Definition: libmemif.h:357
memif_create
int memif_create(memif_conn_handle_t *c, memif_conn_args_t *args, memif_connection_update_t *on_connect, memif_connection_update_t *on_disconnect, memif_interrupt_t *on_interrupt, void *private_ctx)
Memory interface create function.
Definition: main.c:1065
thread
pthread_t thread[MAX_CONNS]
Definition: main.c:142
libmemif.h
memif_conn_args_t
Memif connection arguments.
Definition: libmemif.h:295
memif_region_details_t::is_external
uint8_t is_external
Definition: libmemif.h:379
memif_region_details_t::fd
int fd
Definition: libmemif.h:378
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
add_epoll_fd
int add_epoll_fd(int fd, uint32_t events)
Definition: main.c:302
memif_get_details
int memif_get_details(memif_conn_handle_t conn, memif_details_t *md, char *buf, ssize_t buflen)
Memif get details.
Definition: main.c:2637
icmpr_del_external_region
int icmpr_del_external_region(void *addr, uint32_t size, int fd, void *private_ctx)
Definition: main.c:483
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
memif_details_t::tx_queues
memif_queue_details_t * tx_queues
Definition: libmemif.h:418
memif_details_t::rx_queues_num
uint8_t rx_queues_num
Definition: libmemif.h:415
memif_conn_args_t::interface_id
uint32_t interface_id
Definition: libmemif.h:306
del_epoll_fd
int del_epoll_fd(int fd)
Definition: main.c:344
raddr
vl_api_address_t raddr
Definition: ikev2_types.api:149
error
Definition: cJSON.c:88
on_disconnect
int on_disconnect(memif_conn_handle_t conn, void *private_ctx)
Definition: main.c:186
memif_details_t::inst_name
uint8_t * inst_name
Definition: libmemif.h:404
memif_queue_details_t::buffer_size
uint16_t buffer_size
Definition: libmemif.h:363
on_interrupt
int on_interrupt(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Definition: main.c:287
count
u8 count
Definition: dhcp.api:208
memif_init
int memif_init(memif_control_fd_update_t *on_control_fd_update, char *app_name, memif_alloc_t *memif_alloc, memif_realloc_t *memif_realloc, memif_free_t *memif_free)
Memif initialization.
Definition: main.c:502
APP_NAME
#define APP_NAME
Definition: main.c:57
memif_details_t::id
uint32_t id
Definition: libmemif.h:408
icmpr_get_external_region_addr
void * icmpr_get_external_region_addr(uint32_t size, int fd, void *private_ctx)
Definition: main.c:477
generate_packet
int generate_packet(void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
Definition: icmp_proto.c:314
memif_details_t
Memif details.
Definition: libmemif.h:401
memif_region_details_t::index
uint8_t index
Definition: libmemif.h:375
memif_get_version
uint16_t memif_get_version()
Memif get version.
Definition: main.c:171
poll_event
int poll_event(int timeout)
Definition: main.c:975
memif_connection_t
struct memif_connection memif_connection_t
Definition: memif_private.h:137
end
f64 end
end of the time range
Definition: mactime.api:44
memif_conn_args_t::num_m2s_rings
uint8_t num_m2s_rings
Definition: libmemif.h:301
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
memif_details_t::remote_inst_name
uint8_t * remote_inst_name
Definition: libmemif.h:406
memif_connection_t::tx_err_counter
uint64_t tx_err_counter
Definition: main.c:129
memif_buffer_alloc
int memif_buffer_alloc(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *count_out, uint16_t size)
Memif buffer alloc.
Definition: main.c:2265
LIBMEMIF_VERSION
#define LIBMEMIF_VERSION
Libmemif version.
Definition: libmemif.h:26
memif_details_t::tx_queues_num
uint8_t tx_queues_num
Definition: libmemif.h:416
time.h
icmpr_thread_data_t
Definition: main.c:133
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
memif_details_t::secret
uint8_t * secret
Definition: libmemif.h:409
events
static perfmon_event_t events[]
Definition: core.c:21
MFD_ALLOW_SEALING
#define MFD_ALLOW_SEALING
Definition: main.c:104
memif_cleanup
int memif_cleanup()
Memif cleanup.
Definition: main.c:2821
icmpr_reset_counters
void icmpr_reset_counters()
Definition: main.c:289
MEMIF_FD_EVENT_ERROR
#define MEMIF_FD_EVENT_ERROR
inform libmemif that error occurred on fd
Definition: libmemif.h:94
memif_details_t::if_name
uint8_t * if_name
Definition: libmemif.h:403
memif_connection
Definition: main.c:57
MEMIF_ERR_NOBUF
@ MEMIF_ERR_NOBUF
Definition: libmemif.h:60
MEMIF_FD_EVENT_DEL
#define MEMIF_FD_EVENT_DEL
if set, informs that fd is going to be closed (user may want to stop watching for events on this fd)
Definition: libmemif.h:96
memif_details_t::regions_num
uint8_t regions_num
Definition: libmemif.h:413
memif_conn_handle_t
void * memif_conn_handle_t
Memif connection handle pointer of type void, pointing to internal structure.
Definition: libmemif.h:110
data
u8 data[128]
Definition: ipsec_types.api:92
icmpr_send
int icmpr_send(long index, long packet_num, char *hw, char *ip)
Definition: main.c:817
memfd_create
static int memfd_create(const char *name, unsigned int flags)
Definition: main.c:93
memif_details_t::socket_filename
uint8_t * socket_filename
Definition: libmemif.h:412
size
u32 size
Definition: vhost_user.h:125
index
u32 index
Definition: flow_types.api:221
memif_details_t::regions
memif_region_details_t * regions
Definition: libmemif.h:414
main
int main(int argc, char *argv[])
Definition: main.c:106
memif_conn_args_t::num_s2m_rings
uint8_t num_s2m_rings
Definition: libmemif.h:300
memif_tx_burst
int memif_tx_burst(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *tx)
Memif transmit buffer burst.
Definition: main.c:2444
memif_buffer_t
Memif buffer.
Definition: libmemif.h:325
memif_control_fd_handler
int memif_control_fd_handler(int fd, uint8_t events)
Memif control file descriptor handler.
Definition: main.c:1277
print_memif_details
static void print_memif_details()
Definition: main.c:173
buf
u64 buf
Definition: application.c:493
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
memif_conn_args_t::log2_ring_size
uint8_t log2_ring_size
Definition: libmemif.h:303
memif_connection_t::t_sec
uint64_t t_sec
Definition: main.c:130
icmpr_send_proc
void * icmpr_send_proc(void *data)
Definition: main.c:720
memif_details_t::mode
uint8_t mode
Definition: libmemif.h:411
memif_details_t::link_up_down
uint8_t link_up_down
Definition: libmemif.h:421
memif_conn_args_t::is_master
uint8_t is_master
Definition: libmemif.h:304
memif_conn_args_t::interface_name
uint8_t interface_name[32]
Definition: libmemif.h:307
memif_delete
int memif_delete(memif_conn_handle_t *conn)
Memif delete.
Definition: main.c:1784
resolve_packet
int resolve_packet(void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size, uint8_t ip_addr[4])
Definition: icmp_proto.c:208
memif_connection_t
Definition: main.c:72
icmp_proto.h
icmpr_thread_data_t::index
uint16_t index
Definition: main.c:135
a
a
Definition: bitmap.h:544
memif_details_t::role
uint8_t role
Definition: libmemif.h:410
ip
vl_api_address_t ip
Definition: l2.api:558
icmpr_thread_data_t::packet_num
uint64_t packet_num
Definition: main.c:136
free
void free(void *p)
Definition: mem.c:42
icmpr_memif_delete
int icmpr_memif_delete()
Definition: main.c:193
memif_strerror
char * memif_strerror(int err_code)
Memif strerror.
Definition: main.c:157
malloc
void * malloc(size_t size)
Definition: mem.c:33
F_ADD_SEALS
#define F_ADD_SEALS
Definition: main.c:108
print_help
void print_help()
Definition: main.c:204
icmpr_thread_data_t::hw_daddr
uint8_t hw_daddr[6]
Definition: main.c:138
memif_region_details_t::size
uint32_t size
Definition: libmemif.h:377
memif_details_t::rx_queues
memif_queue_details_t * rx_queues
Definition: libmemif.h:417
icmpr_add_external_region
int icmpr_add_external_region(void **addr, uint32_t size, int *fd, void *private_ctx)
Definition: main.c:451
MEMIF_ERR_NOCONN
@ MEMIF_ERR_NOCONN
Definition: libmemif.h:52
MAX_CONNS
#define MAX_CONNS
Definition: main.c:76
memif_refill_queue
int memif_refill_queue(memif_conn_handle_t conn, uint16_t qid, uint16_t count, uint16_t headroom)
Memif refill queue.
Definition: main.c:2389
MEMIF_FD_EVENT_READ
#define MEMIF_FD_EVENT_READ
user needs to set events that occurred on fd and pass them to memif_control_fd_handler
Definition: libmemif.h:91
memif_rx_burst
int memif_rx_burst(memif_conn_handle_t conn, uint16_t qid, memif_buffer_t *bufs, uint16_t count, uint16_t *rx)
Memif receive buffer burst.
Definition: main.c:2545
on_interrupt1
int on_interrupt1(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Definition: main.c:571
memif_queue_details_t::qid
uint8_t qid
Definition: libmemif.h:356
mod_epoll_fd
int mod_epoll_fd(int fd, uint32_t events)
Definition: main.c:323
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105