FD.io VPP  v20.05.1-6-gf53edbc3b
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, void *ctx)
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  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);
497  memif_connection_t *c = &memif_connection[index];
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,
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);
574  memif_connection_t *c = &memif_connection[index];
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  }
657  memif_connection_t *c = &memif_connection[index];
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 =
705  (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS);
706  c->tx_buf_num = 0;
707  c->tx_bufs =
708  (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_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;
725  memif_connection_t *c = &memif_connection[index];
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,
744  MAX_MEMIF_BUFS > count ? count : MAX_MEMIF_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  }
829  memif_connection_t *c = &memif_connection[index];
830  if (c->conn == NULL)
831  return -1;
832  char *end, *ui;
833  uint8_t tmp[6];
834  icmpr_thread_data_t *data = &icmpr_thread_data[index];
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 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));
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 }
u8 count
Definition: dhcp.api:208
uint64_t rx_counter
Definition: main.c:129
void * icmpr_send_proc(void *data)
Definition: main.c:720
uint8_t ip_daddr[4]
Definition: main.c:137
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:404
uint8_t * secret
Definition: libmemif.h:409
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:89
uint8_t num_m2s_rings
Definition: libmemif.h:299
int del_epoll_fd(int fd)
Definition: main.c:344
Optimized string handling code, including c11-compliant "safe C library" variants.
uint16_t buffer_size
Definition: libmemif.h:300
int on_interrupt1(memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
Definition: main.c:571
uint32_t interface_id
Definition: libmemif.h:304
#define APP_NAME
Definition: main.c:57
vhost_vring_addr_t addr
Definition: vhost_user.h:254
void icmpr_print_counters()
Definition: main.c:272
uint8_t * remote_inst_name
Definition: libmemif.h:406
int poll_event(int timeout)
Definition: main.c:975
#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:2363
#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:298
int control_fd_update(int fd, uint8_t events, void *ctx)
Definition: main.c:378
#define DBG(...)
Definition: main.c:67
uint32_t len
Definition: libmemif.h:327
#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:2577
memif_interface_mode_t mode
Definition: libmemif.h:306
uint8_t mode
Definition: libmemif.h:411
uint8_t link_up_down
Definition: libmemif.h:421
char * memif_strerror(int err_code)
Memif strerror.
Definition: main.c:157
#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:340
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:305
void * data
Definition: libmemif.h:333
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:363
int user_input_handler()
Definition: main.c:894
uint8_t * socket_filename
Definition: libmemif.h:412
#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:94
long ctx[MAX_CONNS]
Definition: main.c:144
struct memif_connection memif_connection_t
u64 size
Definition: vhost_user.h:150
uint8_t is_master
Definition: libmemif.h:302
uint8_t rx_queues_num
Definition: libmemif.h:415
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:505
static void print_memif_details()
Definition: main.c:173
vl_api_tunnel_mode_t mode
Definition: gre.api:48
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:483
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
u32 flags
Definition: vhost_user.h:248
uint8_t regions_num
Definition: libmemif.h:413
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:2417
#define F_ADD_SEALS
Definition: main.c:108
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
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:2237
memif_queue_details_t * rx_queues
Definition: libmemif.h:417
uint8_t role
Definition: libmemif.h:410
string name[64]
Definition: ip.api:44
void * icmpr_get_external_region_addr(uint32_t size, int fd, void *private_ctx)
Definition: main.c:477
int memif_cleanup()
Memif cleanup.
Definition: main.c:2761
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:1095
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
u8 data[128]
Definition: ipsec_types.api:89
uint64_t t_sec
Definition: main.c:130
uint8_t tx_queues_num
Definition: libmemif.h:416
int icmpr_add_external_region(void **addr, uint32_t size, int *fd, void *private_ctx)
Definition: main.c:451
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:108
#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:1307
vl_api_address_t ip
Definition: l2.api:501
int mod_epoll_fd(int fd, uint32_t events)
Definition: main.c:323
#define INFO(...)
Definition: main.c:70
uint8_t log2_ring_size
Definition: libmemif.h:301
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:408
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:2484
uint64_t t_nsec
Definition: main.c:130
int memif_delete(memif_conn_handle_t *conn)
Memif delete.
Definition: main.c:1816
memif_region_details_t * regions
Definition: libmemif.h:414
#define MEMIF_FD_EVENT_ERROR
inform libmemif that error occured on fd
Definition: libmemif.h:92
#define MEMIF_FD_EVENT_MOD
update events
Definition: libmemif.h:96
int main(int argc, char *argv[])
Definition: main.c:101
int icmpr_send(long index, long packet_num, char *hw, char *ip)
Definition: main.c:817
memif_queue_details_t * tx_queues
Definition: libmemif.h:418
uint8_t * if_name
Definition: libmemif.h:403
f64 end
end of the time range
Definition: mactime.api:44
uint8_t hw_daddr[6]
Definition: main.c:138
uint16_t index
Definition: main.c:74
Memif buffer.
Definition: libmemif.h:323
#define MEMIF_FD_EVENT_WRITE
Definition: libmemif.h:90
uint16_t memif_get_version()
Memif get version.
Definition: main.c:174
memif_buffer_t * tx_bufs
Definition: main.c:80
Memif connection arguments.
Definition: libmemif.h:293
Memif details.
Definition: libmemif.h:401
#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:434
uint8_t * remote_if_name
Definition: libmemif.h:405
memif_buffer_t * rx_bufs
Definition: main.c:85