FD.io VPP  v19.01.3-6-g70449b9b9
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 
141 icmpr_thread_data_t icmpr_thread_data[MAX_CONNS];
142 pthread_t thread[MAX_CONNS];
143 int epfd;
145 memif_connection_t memif_connection[MAX_CONNS];
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  {
185  memif_connection_t *c = &memif_connection[i];
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  {
277  memif_connection_t *c = &memif_connection[i];
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  {
294  memif_connection_t *c = &memif_connection[i];
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 moddified 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)
379 {
380  /* convert memif event definitions to epoll events */
381  if (events & MEMIF_FD_EVENT_DEL)
382  return del_epoll_fd (fd);
383 
384  uint32_t evt = 0;
385  if (events & MEMIF_FD_EVENT_READ)
386  evt |= EPOLLIN;
387  if (events & MEMIF_FD_EVENT_WRITE)
388  evt |= EPOLLOUT;
389 
390  if (events & MEMIF_FD_EVENT_MOD)
391  return mod_epoll_fd (fd, evt);
392 
393  return add_epoll_fd (fd, evt);
394 }
395 
396 int
397 icmpr_memif_delete (long index)
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  }
409  memif_connection_t *c = &memif_connection[index];
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  /* 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));
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  {
438  memif_connection_t *c = &memif_connection[i];
439  if (c->conn)
440  icmpr_memif_delete (i);
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  raddr = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, rfd, 0);
466 
467  *addr = raddr;
468  *fd = rfd;
469 
470  return 0;
471 }
472 
473 void *
474 icmpr_get_external_region_addr (uint32_t size, int fd, void *private_ctx)
475 {
476  return mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
477 }
478 
479 int
480 icmpr_del_external_region (void *addr, uint32_t size, int fd,
481  void *private_ctx)
482 {
483  munmap (addr, size);
484  if (fd > 0)
485  close (fd);
486  fd = -1;
487  return 0;
488 }
489 
490 int
491 on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
492 {
493  long index = *((long *) private_ctx);
494  memif_connection_t *c = &memif_connection[index];
495  if (c->index != index)
496  {
497  INFO ("invalid context: %ld/%u", index, c->index);
498  return 0;
499  }
500 
501  int err = MEMIF_ERR_SUCCESS, ret_val;
502  uint16_t rx = 0, tx = 0;
503  uint16_t fb = 0;
504  int i = 0;
505  int j = 0;
506 
507  do
508  {
509  err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx);
510  ret_val = err;
511  c->rx_counter += rx;
512  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF))
513  {
514  INFO ("memif_rx_burst: %s", memif_strerror (err));
515  goto error;
516  }
517  i = 0;
518  memset (c->tx_bufs, 0, sizeof (memif_buffer_t) * rx);
519  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
520  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
521  {
522  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
523  goto error;
524  }
525  j = 0;
526  c->tx_err_counter += rx - tx;
527 
528  while (tx)
529  {
530  resolve_packet ((void *) (c->rx_bufs + i)->data,
531  (c->rx_bufs + i)->len,
532  (void *) (c->tx_bufs + j)->data,
533  &(c->tx_bufs + j)->len, c->ip_addr);
534  i++;
535  j++;
536  tx--;
537  }
538 
539  err = memif_refill_queue (c->conn, qid, rx, 0);
540  if (err != MEMIF_ERR_SUCCESS)
541  INFO ("memif_buffer_free: %s", memif_strerror (err));
542  rx -= rx;
543 
544  DBG ("%u/%u alloc/free buffers", rx, MAX_MEMIF_BUFS - rx);
545 
546  err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
547  if (err != MEMIF_ERR_SUCCESS)
548  {
549  INFO ("memif_tx_burst: %s", memif_strerror (err));
550  goto error;
551  }
552  c->tx_counter += tx;
553  }
554  while (ret_val == MEMIF_ERR_NOBUF);
555 
556  return 0;
557 
558 error:
559  err = memif_refill_queue (c->conn, qid, rx, 0);
560  if (err != MEMIF_ERR_SUCCESS)
561  INFO ("memif_buffer_free: %s", memif_strerror (err));
562  c->rx_buf_num -= rx;
563  DBG ("freed %d buffers. %u/%u alloc/free buffers", fb, c->rx_buf_num,
565  return 0;
566 }
567 
568 int
569 on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
570 {
571  long index = *((long *) private_ctx);
572  memif_connection_t *c = &memif_connection[index];
573  if (c->index != index)
574  {
575  INFO ("invalid context: %ld/%u", index, c->index);
576  return 0;
577  }
578 
579  int err = MEMIF_ERR_SUCCESS, ret_val;
580  int i;
581  uint16_t rx, tx;
582  uint16_t fb;
583  uint16_t pck_seq;
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  fb, 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  }
657  memif_connection_t *c = &memif_connection[index];
658 
659  memif_conn_args_t args;
660  int fd = -1;
661  memset (&args, 0, sizeof (args));
662  args.is_master = mode;
663  args.log2_ring_size = 11;
664  args.buffer_size = 2048;
665  args.num_s2m_rings = 1;
666  args.num_m2s_rings = 1;
667  strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
668  args.mode = 0;
669  args.interface_id = index;
670 
671  int err;
672  if (s == NULL)
673  {
674  err = memif_create (&c->conn,
676  &ctx[index]);
677  if (err != MEMIF_ERR_SUCCESS)
678  {
679  INFO ("memif_create: %s", memif_strerror (err));
680  return 0;
681  }
682  }
683  else
684  {
685  if (strncmp (s, "1", 1) == 0)
686  {
687  err = memif_create (&c->conn,
689  &ctx[index]);
690  if (err != MEMIF_ERR_SUCCESS)
691  {
692  INFO ("memif_create: %s", memif_strerror (err));
693  return 0;
694  }
695  }
696  else
697  {
698  INFO ("Unknown interrupt descriptor");
699  goto done;
700  }
701  }
702 
703  c->index = index;
704  c->rx_buf_num = 0;
705  c->rx_bufs =
706  (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
707  c->tx_buf_num = 0;
708  c->tx_bufs =
709  (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
710 
711  c->ip_addr[0] = 192;
712  c->ip_addr[1] = 168;
713  c->ip_addr[2] = c->index + 1;
714  c->ip_addr[3] = 2;
715 
716 done:
717  return 0;
718 }
719 
720 void *
721 icmpr_send_proc (void *data)
722 {
724  int index = d->index;
725  uint64_t count = d->packet_num;
726  memif_connection_t *c = &memif_connection[index];
727  if (c->conn == NULL)
728  {
729  INFO ("No connection at index %d.", index);
730  goto error;
731  }
732  uint16_t tx, i;
733  int err = MEMIF_ERR_SUCCESS;
734  uint32_t seq = 0;
735  struct timespec start, end;
736  memset (&start, 0, sizeof (start));
737  memset (&end, 0, sizeof (end));
738 
739  timespec_get (&start, TIME_UTC);
740  while (count)
741  {
742  i = 0;
743  err =
744  memif_buffer_alloc (c->conn, 0, c->tx_bufs,
745  MAX_MEMIF_BUFS > count ? count : MAX_MEMIF_BUFS,
746  &tx, 128);
747 
748  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
749  {
750  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
751  goto error;
752  }
753  c->tx_buf_num += tx;
754 
755  while (tx)
756  {
757  while (tx > 4)
758  {
759  generate_packet ((void *) c->tx_bufs[i].data,
760  &c->tx_bufs[i].len, c->ip_addr, d->ip_daddr,
761  d->hw_daddr, seq++);
762  generate_packet ((void *) c->tx_bufs[i + 1].data,
763  &c->tx_bufs[i + 1].len, c->ip_addr,
764  d->ip_daddr, d->hw_daddr, seq++);
765  generate_packet ((void *) c->tx_bufs[i + 2].data,
766  &c->tx_bufs[i + 2].len, c->ip_addr,
767  d->ip_daddr, d->hw_daddr, seq++);
768  generate_packet ((void *) c->tx_bufs[i + 3].data,
769  &c->tx_bufs[i + 3].len, c->ip_addr,
770  d->ip_daddr, d->hw_daddr, seq++);
771  i += 4;
772  tx -= 4;
773  }
774  generate_packet ((void *) c->tx_bufs[i].data, &c->tx_bufs[i].len,
775  c->ip_addr, d->ip_daddr, d->hw_daddr, seq++);
776  i++;
777  tx--;
778  }
779 
780  err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx);
781  if (err != MEMIF_ERR_SUCCESS)
782  {
783  INFO ("memif_tx_burst: %s", memif_strerror (err));
784  goto error;
785  }
786  c->tx_buf_num -= tx;
787  c->tx_counter += tx;
788  count -= tx;
789  }
790 
791  timespec_get (&end, TIME_UTC);
792  INFO ("\n\nPacket sequence finished!\nSeq len: %u", seq);
793  uint64_t t1 = end.tv_sec - start.tv_sec;
794  uint64_t t2;
795  if (end.tv_nsec > start.tv_nsec)
796  {
797  t2 = end.tv_nsec - start.tv_nsec;
798  }
799  else
800  {
801  t2 = start.tv_nsec - end.tv_nsec;
802  t1--;
803  }
804  c->t_sec = t1;
805  c->t_nsec = t2;
806  double tmp = t1;
807  tmp += t2 / 1e+9;
808  tmp = seq / tmp;
809  INFO ("Seq time: %lus %luns\nAverage pps: %f", t1, t2, tmp);
810 
811 error:
812  INFO ("Thread exiting...");
813  pthread_exit (NULL);
814 }
815 
816 
817 int
818 icmpr_send (long index, long packet_num, char *hw, char *ip)
819 {
820  if (index >= MAX_CONNS)
821  {
822  INFO ("connection array overflow");
823  return 0;
824  }
825  if (index < 0)
826  {
827  INFO ("...");
828  return 0;
829  }
830  memif_connection_t *c = &memif_connection[index];
831  if (c->conn == NULL)
832  return -1;
833  int err, i;
834  uint16_t tx = 0, rx = 0;
835  char *end, *ui;
836  uint8_t tmp[6];
837  icmpr_thread_data_t *data = &icmpr_thread_data[index];
838  data->index = index;
839  data->packet_num = packet_num;
840 
841  ui = strtok (ip, ".");
842  if (ui == NULL)
843  goto error;
844  tmp[0] = strtol (ui, &end, 10);
845  ui = strtok (NULL, ".");
846  if (ui == NULL)
847  goto error;
848  tmp[1] = strtol (ui, &end, 10);
849  ui = strtok (NULL, ".");
850  if (ui == NULL)
851  goto error;
852  tmp[2] = strtol (ui, &end, 10);
853  ui = strtok (NULL, ".");
854  if (ui == NULL)
855  goto error;
856  tmp[3] = strtol (ui, &end, 10);
857 
858  data->ip_daddr[0] = tmp[0];
859  data->ip_daddr[1] = tmp[1];
860  data->ip_daddr[2] = tmp[2];
861  data->ip_daddr[3] = tmp[3];
862 
863  ui = strtok (hw, ":");
864  if (ui == NULL)
865  goto error;
866  tmp[0] = strtol (ui, &end, 16);
867  ui = strtok (NULL, ":");
868  if (ui == NULL)
869  goto error;
870  tmp[1] = strtol (ui, &end, 16);
871  ui = strtok (NULL, ":");
872  if (ui == NULL)
873  goto error;
874  tmp[2] = strtol (ui, &end, 16);
875  ui = strtok (NULL, ":");
876  if (ui == NULL)
877  goto error;
878  tmp[3] = strtol (ui, &end, 16);
879  ui = strtok (NULL, ":");
880  if (ui == NULL)
881  goto error;
882  tmp[4] = strtol (ui, &end, 16);
883  ui = strtok (NULL, ":");
884  if (ui == NULL)
885  goto error;
886  tmp[5] = strtol (ui, &end, 16);
887 
888  pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data);
889  return 0;
890 
891 error:
892  INFO ("Invalid input\n");
893  return 0;
894 }
895 
896 int
898 {
899  char *in = (char *) malloc (256);
900  char *ui = fgets (in, 256, stdin);
901  char *end;
902  long a;
903 
904  if (in[0] == '\n')
905  goto done;
906 
907  ui = strtok (in, " ");
908 
909  if (strncmp (ui, "exit", 4) == 0)
910  {
911  free (in);
912  icmpr_free ();
913  exit (EXIT_SUCCESS);
914  }
915  else if (strncmp (ui, "help", 4) == 0)
916  {
917  print_help ();
918  goto done;
919  }
920  else if (strncmp (ui, "conn", 4) == 0)
921  {
922  ui = strtok (NULL, " ");
923  if (ui != NULL)
924  a = strtol (ui, &end, 10);
925  else
926  {
927  INFO ("expected id");
928  goto done;
929  }
930  ui = strtok (NULL, " ");
931  if (ui != NULL)
932  icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " "));
933  else
934  INFO ("expected mode <0|1>");
935  goto done;
936  }
937  else if (strncmp (ui, "del", 3) == 0)
938  {
939  ui = strtok (NULL, " ");
940  if (ui != NULL)
941  icmpr_memif_delete (strtol (ui, &end, 10));
942  else
943  {
944  INFO ("expected id");
945  goto done;
946  }
947  }
948  else if (strncmp (ui, "send", 4) == 0)
949  {
950  ui = strtok (NULL, " ");
951  if (ui != NULL)
952  a = strtol (ui, &end, 10);
953  else
954  {
955  INFO ("expected id");
956  goto done;
957  }
958  ui = strtok (NULL, " ");
959  if (ui != NULL)
960  icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "),
961  strtok (NULL, " "));
962  else
963  INFO ("expected count");
964  goto done;
965  }
966  else if (strncmp (ui, "show", 4) == 0)
967  {
969  goto done;
970  }
971 
972 done:
973  free (in);
974  return 0;
975 }
976 
977 int
978 poll_event (int timeout)
979 {
980  struct epoll_event evt, *e;
981  int app_err = 0, memif_err = 0, en = 0;
982  int tmp, nfd;
983  uint32_t events = 0;
984  memset (&evt, 0, sizeof (evt));
985  evt.events = EPOLLIN | EPOLLOUT;
986  sigset_t sigset;
987  sigemptyset (&sigset);
988  en = epoll_pwait (epfd, &evt, 1, timeout, &sigset);
989  /* id event polled */
990  if (en < 0)
991  {
992  DBG ("epoll_pwait: %s", strerror (errno));
993  return -1;
994  }
995  if (en > 0)
996  {
997  /* this app does not use any other file descriptors than stds and memif control fds */
998  if (evt.data.fd > 2)
999  {
1000  /* event of memif control fd */
1001  /* convert epolle events to memif events */
1002  if (evt.events & EPOLLIN)
1003  events |= MEMIF_FD_EVENT_READ;
1004  if (evt.events & EPOLLOUT)
1005  events |= MEMIF_FD_EVENT_WRITE;
1006  if (evt.events & EPOLLERR)
1007  events |= MEMIF_FD_EVENT_ERROR;
1008  memif_err = memif_control_fd_handler (evt.data.fd, events);
1009  if (memif_err != MEMIF_ERR_SUCCESS)
1010  INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err));
1011  }
1012  else if (evt.data.fd == 0)
1013  {
1014  app_err = user_input_handler ();
1015  }
1016  else
1017  {
1018  DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd);
1019  }
1020  }
1021 
1022  if ((app_err < 0) || (memif_err < 0))
1023  {
1024  if (app_err < 0)
1025  DBG ("user input handler error");
1026  if (memif_err < 0)
1027  DBG ("memif control fd handler error");
1028  return -1;
1029  }
1030 
1031  return 0;
1032 }
1033 
1034 int
1036 {
1037  epfd = epoll_create (1);
1038  add_epoll_fd (0, EPOLLIN);
1039 
1040  /* initialize memory interface */
1041  int err, i;
1042  /* if valid callback is passed as argument, fd event polling will be done by user
1043  all file descriptors and events will be passed to user in this callback */
1044  /* if callback is set to NULL libmemif will handle fd event polling */
1046  if (err != MEMIF_ERR_SUCCESS)
1047  {
1048  INFO ("memif_init: %s", memif_strerror (err));
1049  icmpr_free ();
1050  exit (-1);
1051  }
1052 
1053  for (i = 0; i < MAX_CONNS; i++)
1054  {
1055  memset (&memif_connection[i], 0, sizeof (memif_connection_t));
1056  ctx[i] = i;
1057  }
1058 
1062 
1063  print_help ();
1064 
1065  /* main loop */
1066  while (1)
1067  {
1068  if (poll_event (-1) < 0)
1069  {
1070  DBG ("poll_event error!");
1071  }
1072  }
1073 }
uint64_t rx_counter
Definition: main.c:129
void * icmpr_send_proc(void *data)
Definition: main.c:721
uint8_t ip_daddr[4]
Definition: main.c:137
u32 flags
Definition: vhost_user.h:115
int icmpr_memif_delete()
Definition: main.c:193
void print_help()
Definition: main.c:204
int add_epoll_fd(int fd, uint32_t events)
Definition: main.c:302
a
Definition: bitmap.h:538
uint8_t * inst_name
Definition: libmemif.h:364
uint8_t * secret
Definition: libmemif.h:369
int on_disconnect(memif_conn_handle_t conn, void *private_ctx)
Definition: main.c:186
#define MEMIF_FD_EVENT_READ
user needs to set events that occured on fd and pass them to memif_control_fd_handler ...
Definition: libmemif.h:88
uint8_t num_m2s_rings
Definition: libmemif.h:259
int del_epoll_fd(int fd)
Definition: main.c:344
Optimized string handling code, including c11-compliant "safe C library" variants.
#define NULL
Definition: clib.h:58
uint16_t buffer_size
Definition: libmemif.h:260
int on_interrupt1(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Definition: main.c:569
uint32_t interface_id
Definition: libmemif.h:264
int i
#define APP_NAME
Definition: main.c:57
vhost_vring_addr_t addr
Definition: vhost_user.h:121
void icmpr_print_counters()
Definition: main.c:272
uint8_t * remote_inst_name
Definition: libmemif.h:366
int poll_event(int timeout)
Definition: main.c:978
#define LIBMEMIF_VERSION
Libmemif version.
Definition: libmemif.h:26
int memif_refill_queue(memif_conn_handle_t conn, uint16_t qid, uint16_t count, uint16_t headroom)
Memif refill ring.
Definition: main.c:1778
#define MFD_ALLOW_SEALING
Definition: main.c:104
int on_connect(memif_conn_handle_t conn, void *private_ctx)
Definition: main.c:177
uint8_t num_s2m_rings
Definition: libmemif.h:258
#define DBG(...)
Definition: main.c:67
uint32_t len
Definition: libmemif.h:287
#define F_SEAL_SHRINK
Definition: main.c:112
int icmpr_free()
Definition: main.c:258
int memif_get_details(memif_conn_handle_t conn, memif_details_t *md, char *buf, ssize_t buflen)
Memif get details.
Definition: main.c:1984
memif_interface_mode_t mode
Definition: libmemif.h:266
uint8_t mode
Definition: libmemif.h:371
uint8_t link_up_down
Definition: libmemif.h:381
char * memif_strerror(int err_code)
Memif strerror.
Definition: main.c:159
#define IF_NAME
Definition: main.c:58
static int memfd_create(const char *name, unsigned int flags)
Definition: main.c:93
int icmpr_memif_create(int is_master)
Definition: main.c:337
memif_conn_handle_t conn
Definition: main.c:76
pthread_t thread[MAX_CONNS]
Definition: main.c:142
uint8_t ip_addr[4]
Definition: main.c:90
uint16_t rx_buf_num
Definition: main.c:88
uint8_t interface_name[32]
Definition: libmemif.h:265
uword size
void * data
Definition: libmemif.h:293
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
uint16_t buffer_size
Definition: libmemif.h:323
int user_input_handler()
Definition: main.c:897
uint8_t * socket_filename
Definition: libmemif.h:372
#define TIME_UTC
Definition: main.c:51
#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:93
long ctx[MAX_CONNS]
Definition: main.c:144
struct memif_connection memif_connection_t
uint8_t is_master
Definition: libmemif.h:262
uint8_t rx_queues_num
Definition: libmemif.h:375
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:455
static void print_memif_details()
Definition: main.c:173
u8 name[64]
Definition: memclnt.api:152
uint64_t tx_counter
Definition: main.c:129
int icmpr_del_external_region(void *addr, uint32_t size, int fd, void *private_ctx)
Definition: main.c:480
uint16_t tx_buf_num
Definition: main.c:83
int on_interrupt(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Definition: main.c:287
uint8_t regions_num
Definition: libmemif.h:373
svmdb_client_t * c
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:1832
#define F_ADD_SEALS
Definition: main.c:108
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:1652
memif_queue_details_t * rx_queues
Definition: libmemif.h:377
uint8_t role
Definition: libmemif.h:370
void * icmpr_get_external_region_addr(uint32_t size, int fd, void *private_ctx)
Definition: main.c:474
int memif_cleanup()
Memif cleanup.
Definition: main.c:2162
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:629
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
uint64_t t_sec
Definition: main.c:130
uint8_t tx_queues_num
Definition: libmemif.h:376
int icmpr_add_external_region(void **addr, uint32_t size, int *fd, void *private_ctx)
Definition: main.c:451
size_t count
Definition: vapi.c:47
uint16_t index
Definition: main.c:135
void * memif_conn_handle_t
Memif connection handle pointer of type void, pointing to internal structure.
Definition: libmemif.h:101
#define MAX_CONNS
Definition: main.c:76
uint64_t packet_num
Definition: main.c:136
int memif_control_fd_handler(int fd, uint8_t events)
Memif control file descriptor handler.
Definition: main.c:893
int mod_epoll_fd(int fd, uint32_t events)
Definition: main.c:323
int control_fd_update(int fd, uint8_t events)
Definition: main.c:378
#define INFO(...)
Definition: main.c:70
uint8_t log2_ring_size
Definition: libmemif.h:261
void icmpr_reset_counters()
Definition: main.c:289
uint64_t tx_err_counter
Definition: main.c:129
int epfd
Definition: main.c:94
uint32_t id
Definition: libmemif.h:368
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:1899
uint64_t t_nsec
Definition: main.c:130
int memif_delete(memif_conn_handle_t *conn)
Memif delete.
Definition: main.c:1234
vl_api_gbp_vxlan_tunnel_mode_t mode
Definition: gbp.api:349
memif_region_details_t * regions
Definition: libmemif.h:374
#define MEMIF_FD_EVENT_ERROR
inform libmemif that error occured on fd
Definition: libmemif.h:91
#define MEMIF_FD_EVENT_MOD
update events
Definition: libmemif.h:95
int main(int argc, char *argv[])
Definition: main.c:97
int icmpr_send(long index, long packet_num, char *hw, char *ip)
Definition: main.c:818
memif_queue_details_t * tx_queues
Definition: libmemif.h:378
uint8_t * if_name
Definition: libmemif.h:363
uint8_t hw_daddr[6]
Definition: main.c:138
uint16_t index
Definition: main.c:74
Memif buffer.
Definition: libmemif.h:283
#define MEMIF_FD_EVENT_WRITE
Definition: libmemif.h:89
uint16_t memif_get_version()
Memif get version.
Definition: main.c:176
memif_buffer_t * tx_bufs
Definition: main.c:80
Memif connection arguments.
Definition: libmemif.h:253
Memif details.
Definition: libmemif.h:361
#define MAX_MEMIF_BUFS
Definition: main.c:75
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:421
uint8_t * remote_if_name
Definition: libmemif.h:365
memif_buffer_t * rx_bufs
Definition: main.c:85