net/memif: fix Unix domain address length
[dpdk.git] / examples / tep_termination / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <arpa/inet.h>
6 #include <getopt.h>
7 #include <linux/if_ether.h>
8 #include <linux/if_vlan.h>
9 #include <linux/virtio_net.h>
10 #include <linux/virtio_ring.h>
11 #include <signal.h>
12 #include <stdint.h>
13 #include <sys/eventfd.h>
14 #include <sys/param.h>
15 #include <unistd.h>
16
17 #include <rte_atomic.h>
18 #include <rte_cycles.h>
19 #include <rte_ethdev.h>
20 #include <rte_log.h>
21 #include <rte_string_fns.h>
22 #include <rte_malloc.h>
23 #include <rte_vhost.h>
24 #include <rte_pause.h>
25
26 #include "main.h"
27 #include "vxlan.h"
28 #include "vxlan_setup.h"
29
30 /* the maximum number of external ports supported */
31 #define MAX_SUP_PORTS 1
32
33 /**
34  * Calculate the number of buffers needed per port
35  */
36 #define NUM_MBUFS_PER_PORT ((MAX_QUEUES * RTE_TEST_RX_DESC_DEFAULT) +\
37                                 (nb_switching_cores * MAX_PKT_BURST) +\
38                                 (nb_switching_cores * \
39                                 RTE_TEST_TX_DESC_DEFAULT) +\
40                                 (nb_switching_cores * MBUF_CACHE_SIZE))
41
42 #define MBUF_CACHE_SIZE 128
43 #define MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
44
45 #define MAX_PKT_BURST 32        /* Max burst size for RX/TX */
46 #define BURST_TX_DRAIN_US 100   /* TX drain every ~100us */
47
48 /* Defines how long we wait between retries on RX */
49 #define BURST_RX_WAIT_US 15
50
51 #define BURST_RX_RETRIES 4      /* Number of retries on RX. */
52
53 #define JUMBO_FRAME_MAX_SIZE    0x2600
54
55 /* Config_core_flag status definitions. */
56 #define REQUEST_DEV_REMOVAL 1
57 #define ACK_DEV_REMOVAL     0
58
59 /* Configurable number of RX/TX ring descriptors */
60 #define RTE_TEST_RX_DESC_DEFAULT 1024
61 #define RTE_TEST_TX_DESC_DEFAULT 512
62
63 /* Get first 4 bytes in mbuf headroom. */
64 #define MBUF_HEADROOM_UINT32(mbuf) (*(uint32_t *)((uint8_t *)(mbuf) \
65                 + sizeof(struct rte_mbuf)))
66
67 #define INVALID_PORT_ID 0xFFFF
68
69 /* Maximum character device basename size. */
70 #define MAX_BASENAME_SZ 20
71
72 /* Maximum long option length for option parsing. */
73 #define MAX_LONG_OPT_SZ 64
74
75 /* Used to compare MAC addresses. */
76 #define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
77
78 #define CMD_LINE_OPT_NB_DEVICES "nb-devices"
79 #define CMD_LINE_OPT_UDP_PORT "udp-port"
80 #define CMD_LINE_OPT_TX_CHECKSUM "tx-checksum"
81 #define CMD_LINE_OPT_TSO_SEGSZ "tso-segsz"
82 #define CMD_LINE_OPT_FILTER_TYPE "filter-type"
83 #define CMD_LINE_OPT_ENCAP "encap"
84 #define CMD_LINE_OPT_DECAP "decap"
85 #define CMD_LINE_OPT_RX_RETRY "rx-retry"
86 #define CMD_LINE_OPT_RX_RETRY_DELAY "rx-retry-delay"
87 #define CMD_LINE_OPT_RX_RETRY_NUM "rx-retry-num"
88 #define CMD_LINE_OPT_STATS "stats"
89 #define CMD_LINE_OPT_DEV_BASENAME "dev-basename"
90
91 /* mask of enabled ports */
92 static uint32_t enabled_port_mask;
93
94 /*Number of switching cores enabled*/
95 static uint32_t nb_switching_cores;
96
97 /* number of devices/queues to support*/
98 uint16_t nb_devices = 2;
99
100 /* max ring descriptor, ixgbe, i40e, e1000 all are 4096. */
101 #define MAX_RING_DESC 4096
102
103 struct vpool {
104         struct rte_mempool *pool;
105         struct rte_ring *ring;
106         uint32_t buf_size;
107 } vpool_array[MAX_QUEUES+MAX_QUEUES];
108
109 /* UDP tunneling port */
110 uint16_t udp_port = 4789;
111
112 /* enable/disable inner TX checksum */
113 uint8_t tx_checksum = 0;
114
115 /* TCP segment size */
116 uint16_t tso_segsz = 0;
117
118 /* enable/disable decapsulation */
119 uint8_t rx_decap = 1;
120
121 /* enable/disable encapsulation */
122 uint8_t tx_encap = 1;
123
124 /* RX filter type for tunneling packet */
125 uint8_t filter_idx = 1;
126
127 /* overlay packet operation */
128 struct ol_switch_ops overlay_options = {
129         .port_configure = vxlan_port_init,
130         .tunnel_setup = vxlan_link,
131         .tunnel_destroy = vxlan_unlink,
132         .tx_handle = vxlan_tx_pkts,
133         .rx_handle = vxlan_rx_pkts,
134         .param_handle = NULL,
135 };
136
137 /* Enable stats. */
138 uint32_t enable_stats = 0;
139 /* Enable retries on RX. */
140 static uint32_t enable_retry = 1;
141 /* Specify timeout (in useconds) between retries on RX. */
142 static uint32_t burst_rx_delay_time = BURST_RX_WAIT_US;
143 /* Specify the number of retries on RX. */
144 static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
145
146 /* Character device basename. Can be set by user. */
147 static char dev_basename[MAX_BASENAME_SZ] = "vhost-net";
148
149 static unsigned lcore_ids[RTE_MAX_LCORE];
150 uint16_t ports[RTE_MAX_ETHPORTS];
151
152 static unsigned nb_ports; /**< The number of ports specified in command line */
153
154 /* ethernet addresses of ports */
155 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
156
157 /* heads for the main used and free linked lists for the data path. */
158 static struct virtio_net_data_ll *ll_root_used;
159 static struct virtio_net_data_ll *ll_root_free;
160
161 /**
162  * Array of data core structures containing information on
163  * individual core linked lists.
164  */
165 static struct lcore_info lcore_info[RTE_MAX_LCORE];
166
167 /* Used for queueing bursts of TX packets. */
168 struct mbuf_table {
169         unsigned len;
170         unsigned txq_id;
171         struct rte_mbuf *m_table[MAX_PKT_BURST];
172 };
173
174 /* TX queue for each data core. */
175 struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
176
177 struct device_statistics dev_statistics[MAX_DEVICES];
178
179 /**
180  * Set character device basename.
181  */
182 static int
183 us_vhost_parse_basename(const char *q_arg)
184 {
185         /* parse number string */
186         if (strlen(q_arg) >= MAX_BASENAME_SZ)
187                 return -1;
188         else
189                 strlcpy((char *)&dev_basename, q_arg, MAX_BASENAME_SZ);
190
191         return 0;
192 }
193
194 /**
195  * Parse the portmask provided at run time.
196  */
197 static int
198 parse_portmask(const char *portmask)
199 {
200         char *end = NULL;
201         unsigned long pm;
202
203         /* parse hexadecimal string */
204         pm = strtoul(portmask, &end, 16);
205         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
206                 return -1;
207
208         if (pm == 0)
209                 return -1;
210
211         return pm;
212 }
213
214 /**
215  * Parse num options at run time.
216  */
217 static int
218 parse_num_opt(const char *q_arg, uint32_t max_valid_value)
219 {
220         char *end = NULL;
221         unsigned long num;
222
223         /* parse unsigned int string */
224         num = strtoul(q_arg, &end, 10);
225         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
226                 return -1;
227
228         if (num > max_valid_value)
229                 return -1;
230
231         return num;
232 }
233
234 /**
235  * Display usage
236  */
237 static void
238 tep_termination_usage(const char *prgname)
239 {
240         RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
241         "               --udp-port: UDP destination port for VXLAN packet\n"
242         "               --nb-devices[1-64]: The number of virtIO device\n"
243         "               --tx-checksum [0|1]: inner Tx checksum offload\n"
244         "               --tso-segsz [0-N]: TCP segment size\n"
245         "               --decap [0|1]: tunneling packet decapsulation\n"
246         "               --encap [0|1]: tunneling packet encapsulation\n"
247         "               --filter-type[1-3]: filter type for tunneling packet\n"
248         "                   1: Inner MAC and tenent ID\n"
249         "                   2: Inner MAC and VLAN, and tenent ID\n"
250         "                   3: Outer MAC, Inner MAC and tenent ID\n"
251         "               -p PORTMASK: Set mask for ports to be used by application\n"
252         "               --rx-retry [0|1]: disable/enable(default) retries on rx."
253         "                Enable retry if destintation queue is full\n"
254         "               --rx-retry-delay [0-N]: timeout(in usecond) between retries on RX."
255         "                This makes effect only if retries on rx enabled\n"
256         "               --rx-retry-num [0-N]: the number of retries on rx."
257         "                This makes effect only if retries on rx enabled\n"
258         "               --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
259         "               --dev-basename: The basename to be used for the character device.\n",
260                prgname);
261 }
262
263 /**
264  * Parse the arguments given in the command line of the application.
265  */
266 static int
267 tep_termination_parse_args(int argc, char **argv)
268 {
269         int opt, ret;
270         int option_index;
271         unsigned i;
272         const char *prgname = argv[0];
273         static struct option long_option[] = {
274                 {CMD_LINE_OPT_NB_DEVICES, required_argument, NULL, 0},
275                 {CMD_LINE_OPT_UDP_PORT, required_argument, NULL, 0},
276                 {CMD_LINE_OPT_TX_CHECKSUM, required_argument, NULL, 0},
277                 {CMD_LINE_OPT_TSO_SEGSZ, required_argument, NULL, 0},
278                 {CMD_LINE_OPT_DECAP, required_argument, NULL, 0},
279                 {CMD_LINE_OPT_ENCAP, required_argument, NULL, 0},
280                 {CMD_LINE_OPT_FILTER_TYPE, required_argument, NULL, 0},
281                 {CMD_LINE_OPT_RX_RETRY, required_argument, NULL, 0},
282                 {CMD_LINE_OPT_RX_RETRY_DELAY, required_argument, NULL, 0},
283                 {CMD_LINE_OPT_RX_RETRY_NUM, required_argument, NULL, 0},
284                 {CMD_LINE_OPT_STATS, required_argument, NULL, 0},
285                 {CMD_LINE_OPT_DEV_BASENAME, required_argument, NULL, 0},
286                 {NULL, 0, 0, 0},
287         };
288
289         /* Parse command line */
290         while ((opt = getopt_long(argc, argv, "p:",
291                         long_option, &option_index)) != EOF) {
292                 switch (opt) {
293                 /* Portmask */
294                 case 'p':
295                         enabled_port_mask = parse_portmask(optarg);
296                         if (enabled_port_mask == 0) {
297                                 RTE_LOG(INFO, VHOST_CONFIG,
298                                         "Invalid portmask\n");
299                                 tep_termination_usage(prgname);
300                                 return -1;
301                         }
302                         break;
303                 case 0:
304                         if (!strncmp(long_option[option_index].name,
305                                 CMD_LINE_OPT_NB_DEVICES,
306                                 sizeof(CMD_LINE_OPT_NB_DEVICES))) {
307                                 ret = parse_num_opt(optarg, MAX_DEVICES);
308                                 if (ret == -1) {
309                                         RTE_LOG(INFO, VHOST_CONFIG,
310                                         "Invalid argument for nb-devices [0-%d]\n",
311                                         MAX_DEVICES);
312                                         tep_termination_usage(prgname);
313                                         return -1;
314                                 } else
315                                         nb_devices = ret;
316                         }
317
318                         /* Enable/disable retries on RX. */
319                         if (!strncmp(long_option[option_index].name,
320                                 CMD_LINE_OPT_RX_RETRY,
321                                 sizeof(CMD_LINE_OPT_RX_RETRY))) {
322                                 ret = parse_num_opt(optarg, 1);
323                                 if (ret == -1) {
324                                         RTE_LOG(INFO, VHOST_CONFIG,
325                                                 "Invalid argument for rx-retry [0|1]\n");
326                                         tep_termination_usage(prgname);
327                                         return -1;
328                                 } else
329                                         enable_retry = ret;
330                         }
331
332                         if (!strncmp(long_option[option_index].name,
333                                 CMD_LINE_OPT_TSO_SEGSZ,
334                                 sizeof(CMD_LINE_OPT_TSO_SEGSZ))) {
335                                 ret = parse_num_opt(optarg, INT16_MAX);
336                                 if (ret == -1) {
337                                         RTE_LOG(INFO, VHOST_CONFIG,
338                                                 "Invalid argument for TCP segment size [0-N]\n");
339                                         tep_termination_usage(prgname);
340                                         return -1;
341                                 } else
342                                         tso_segsz = ret;
343                         }
344
345                         if (!strncmp(long_option[option_index].name,
346                                         CMD_LINE_OPT_UDP_PORT,
347                                         sizeof(CMD_LINE_OPT_UDP_PORT))) {
348                                 ret = parse_num_opt(optarg, INT16_MAX);
349                                 if (ret == -1) {
350                                         RTE_LOG(INFO, VHOST_CONFIG,
351                                                 "Invalid argument for UDP port [0-N]\n");
352                                         tep_termination_usage(prgname);
353                                         return -1;
354                                 } else
355                                         udp_port = ret;
356                         }
357
358                         /* Specify the retries delay time (in useconds) on RX.*/
359                         if (!strncmp(long_option[option_index].name,
360                                 CMD_LINE_OPT_RX_RETRY_DELAY,
361                                 sizeof(CMD_LINE_OPT_RX_RETRY_DELAY))) {
362                                 ret = parse_num_opt(optarg, INT32_MAX);
363                                 if (ret == -1) {
364                                         RTE_LOG(INFO, VHOST_CONFIG,
365                                                 "Invalid argument for rx-retry-delay [0-N]\n");
366                                         tep_termination_usage(prgname);
367                                         return -1;
368                                 } else
369                                         burst_rx_delay_time = ret;
370                         }
371
372                         /* Specify the retries number on RX. */
373                         if (!strncmp(long_option[option_index].name,
374                                 CMD_LINE_OPT_RX_RETRY_NUM,
375                                 sizeof(CMD_LINE_OPT_RX_RETRY_NUM))) {
376                                 ret = parse_num_opt(optarg, INT32_MAX);
377                                 if (ret == -1) {
378                                         RTE_LOG(INFO, VHOST_CONFIG,
379                                                 "Invalid argument for rx-retry-num [0-N]\n");
380                                         tep_termination_usage(prgname);
381                                         return -1;
382                                 } else
383                                         burst_rx_retry_num = ret;
384                         }
385
386                         if (!strncmp(long_option[option_index].name,
387                                 CMD_LINE_OPT_TX_CHECKSUM,
388                                 sizeof(CMD_LINE_OPT_TX_CHECKSUM))) {
389                                 ret = parse_num_opt(optarg, 1);
390                                 if (ret == -1) {
391                                         RTE_LOG(INFO, VHOST_CONFIG,
392                                                 "Invalid argument for tx-checksum [0|1]\n");
393                                         tep_termination_usage(prgname);
394                                         return -1;
395                                 } else
396                                         tx_checksum = ret;
397                         }
398
399                         if (!strncmp(long_option[option_index].name,
400                                         CMD_LINE_OPT_FILTER_TYPE,
401                                         sizeof(CMD_LINE_OPT_FILTER_TYPE))) {
402                                 ret = parse_num_opt(optarg, 3);
403                                 if ((ret == -1) || (ret == 0)) {
404                                         RTE_LOG(INFO, VHOST_CONFIG,
405                                                 "Invalid argument for filter type [1-3]\n");
406                                         tep_termination_usage(prgname);
407                                         return -1;
408                                 } else
409                                         filter_idx = ret - 1;
410                         }
411
412                         /* Enable/disable encapsulation on RX. */
413                         if (!strncmp(long_option[option_index].name,
414                                 CMD_LINE_OPT_DECAP,
415                                 sizeof(CMD_LINE_OPT_DECAP))) {
416                                 ret = parse_num_opt(optarg, 1);
417                                 if (ret == -1) {
418                                         RTE_LOG(INFO, VHOST_CONFIG,
419                                                 "Invalid argument for decap [0|1]\n");
420                                         tep_termination_usage(prgname);
421                                         return -1;
422                                 } else
423                                         rx_decap = ret;
424                         }
425
426                         /* Enable/disable encapsulation on TX. */
427                         if (!strncmp(long_option[option_index].name,
428                                 CMD_LINE_OPT_ENCAP,
429                                 sizeof(CMD_LINE_OPT_ENCAP))) {
430                                 ret = parse_num_opt(optarg, 1);
431                                 if (ret == -1) {
432                                         RTE_LOG(INFO, VHOST_CONFIG,
433                                                 "Invalid argument for encap [0|1]\n");
434                                         tep_termination_usage(prgname);
435                                         return -1;
436                                 } else
437                                         tx_encap = ret;
438                         }
439
440                         /* Enable/disable stats. */
441                         if (!strncmp(long_option[option_index].name,
442                                 CMD_LINE_OPT_STATS,
443                                 sizeof(CMD_LINE_OPT_STATS))) {
444                                 ret = parse_num_opt(optarg, INT32_MAX);
445                                 if (ret == -1) {
446                                         RTE_LOG(INFO, VHOST_CONFIG,
447                                                         "Invalid argument for stats [0..N]\n");
448                                         tep_termination_usage(prgname);
449                                         return -1;
450                                 } else
451                                         enable_stats = ret;
452                         }
453
454                         /* Set character device basename. */
455                         if (!strncmp(long_option[option_index].name,
456                                 CMD_LINE_OPT_DEV_BASENAME,
457                                 sizeof(CMD_LINE_OPT_DEV_BASENAME))) {
458                                 if (us_vhost_parse_basename(optarg) == -1) {
459                                         RTE_LOG(INFO, VHOST_CONFIG,
460                                                 "Invalid argument for character "
461                                                 "device basename (Max %d characters)\n",
462                                                 MAX_BASENAME_SZ);
463                                         tep_termination_usage(prgname);
464                                         return -1;
465                                 }
466                         }
467
468                         break;
469
470                         /* Invalid option - print options. */
471                 default:
472                         tep_termination_usage(prgname);
473                         return -1;
474                 }
475         }
476
477         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
478                 if (enabled_port_mask & (1 << i))
479                         ports[nb_ports++] = (uint8_t)i;
480         }
481
482         if ((nb_ports ==  0) || (nb_ports > MAX_SUP_PORTS)) {
483                 RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
484                         "but only %u port can be enabled\n", nb_ports,
485                         MAX_SUP_PORTS);
486                 return -1;
487         }
488
489         return 0;
490 }
491
492 /**
493  * Update the global var NB_PORTS and array PORTS
494  * according to system ports number and return valid ports number
495  */
496 static unsigned
497 check_ports_num(unsigned max_nb_ports)
498 {
499         unsigned valid_nb_ports = nb_ports;
500         unsigned portid;
501
502         if (nb_ports > max_nb_ports) {
503                 RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) "
504                         " exceeds total system port number(%u)\n",
505                         nb_ports, max_nb_ports);
506                 nb_ports = max_nb_ports;
507         }
508
509         for (portid = 0; portid < nb_ports; portid++) {
510                 if (!rte_eth_dev_is_valid_port(ports[portid])) {
511                         RTE_LOG(INFO, VHOST_PORT,
512                                 "\nSpecified port ID(%u) is not valid\n",
513                                 ports[portid]);
514                         ports[portid] = INVALID_PORT_ID;
515                         valid_nb_ports--;
516                 }
517         }
518         return valid_nb_ports;
519 }
520
521 /**
522  * This function routes the TX packet to the correct interface. This may be a local device
523  * or the physical port.
524  */
525 static __rte_always_inline void
526 virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m)
527 {
528         struct mbuf_table *tx_q;
529         struct rte_mbuf **m_table;
530         unsigned len, ret = 0;
531         const uint16_t lcore_id = rte_lcore_id();
532
533         RTE_LOG_DP(DEBUG, VHOST_DATA, "(%d) TX: MAC address is external\n",
534                 vdev->vid);
535
536         /* Add packet to the port tx queue */
537         tx_q = &lcore_tx_queue[lcore_id];
538         len = tx_q->len;
539
540         tx_q->m_table[len] = m;
541         len++;
542         if (enable_stats) {
543                 dev_statistics[vdev->vid].tx_total++;
544                 dev_statistics[vdev->vid].tx++;
545         }
546
547         if (unlikely(len == MAX_PKT_BURST)) {
548                 m_table = (struct rte_mbuf **)tx_q->m_table;
549                 ret = overlay_options.tx_handle(ports[0],
550                         (uint16_t)tx_q->txq_id, m_table,
551                         (uint16_t)tx_q->len);
552
553                 /* Free any buffers not handled by TX and update
554                  * the port stats.
555                  */
556                 if (unlikely(ret < len)) {
557                         do {
558                                 rte_pktmbuf_free(m_table[ret]);
559                         } while (++ret < len);
560                 }
561
562                 len = 0;
563         }
564
565         tx_q->len = len;
566         return;
567 }
568
569 /**
570  * This function is called by each data core. It handles all
571  * RX/TX registered with the core. For TX the specific lcore
572  * linked list is used. For RX, MAC addresses are compared
573  * with all devices in the main linked list.
574  */
575 static int
576 switch_worker(__rte_unused void *arg)
577 {
578         struct rte_mempool *mbuf_pool = arg;
579         struct vhost_dev *vdev = NULL;
580         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
581         struct virtio_net_data_ll *dev_ll;
582         struct mbuf_table *tx_q;
583         volatile struct lcore_ll_info *lcore_ll;
584         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
585                                         / US_PER_S * BURST_TX_DRAIN_US;
586         uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
587         unsigned i, ret = 0;
588         const uint16_t lcore_id = rte_lcore_id();
589         const uint16_t num_cores = (uint16_t)rte_lcore_count();
590         uint16_t rx_count = 0;
591         uint16_t tx_count;
592         uint32_t retry = 0;
593
594         RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started\n", lcore_id);
595         lcore_ll = lcore_info[lcore_id].lcore_ll;
596         prev_tsc = 0;
597
598         tx_q = &lcore_tx_queue[lcore_id];
599         for (i = 0; i < num_cores; i++) {
600                 if (lcore_ids[i] == lcore_id) {
601                         tx_q->txq_id = i;
602                         break;
603                 }
604         }
605
606         while (1) {
607                 cur_tsc = rte_rdtsc();
608                 /*
609                  * TX burst queue drain
610                  */
611                 diff_tsc = cur_tsc - prev_tsc;
612                 if (unlikely(diff_tsc > drain_tsc)) {
613
614                         if (tx_q->len) {
615                                 RTE_LOG_DP(DEBUG, VHOST_DATA, "TX queue drained after "
616                                         "timeout with burst size %u\n",
617                                         tx_q->len);
618                                 ret = overlay_options.tx_handle(ports[0],
619                                         (uint16_t)tx_q->txq_id,
620                                         (struct rte_mbuf **)tx_q->m_table,
621                                         (uint16_t)tx_q->len);
622                                 if (unlikely(ret < tx_q->len)) {
623                                         do {
624                                                 rte_pktmbuf_free(tx_q->m_table[ret]);
625                                         } while (++ret < tx_q->len);
626                                 }
627
628                                 tx_q->len = 0;
629                         }
630
631                         prev_tsc = cur_tsc;
632
633                 }
634
635                 rte_prefetch0(lcore_ll->ll_root_used);
636
637                 /**
638                  * Inform the configuration core that we have exited
639                  * the linked list and that no devices are
640                  * in use if requested.
641                  */
642                 if (lcore_ll->dev_removal_flag == REQUEST_DEV_REMOVAL)
643                         lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
644
645                 /*
646                  * Process devices
647                  */
648                 dev_ll = lcore_ll->ll_root_used;
649
650                 while (dev_ll != NULL) {
651                         vdev = dev_ll->vdev;
652
653                         if (unlikely(vdev->remove)) {
654                                 dev_ll = dev_ll->next;
655                                 overlay_options.tunnel_destroy(vdev);
656                                 vdev->ready = DEVICE_SAFE_REMOVE;
657                                 continue;
658                         }
659                         if (likely(vdev->ready == DEVICE_RX)) {
660                                 /* Handle guest RX */
661                                 rx_count = rte_eth_rx_burst(ports[0],
662                                         vdev->rx_q, pkts_burst, MAX_PKT_BURST);
663
664                                 if (rx_count) {
665                                         /*
666                                         * Retry is enabled and the queue is
667                                         * full then we wait and retry to
668                                         * avoid packet loss. Here MAX_PKT_BURST
669                                         * must be less than virtio queue size
670                                         */
671                                         if (enable_retry && unlikely(rx_count >
672                                                 rte_vhost_avail_entries(vdev->vid, VIRTIO_RXQ))) {
673                                                 for (retry = 0; retry < burst_rx_retry_num;
674                                                         retry++) {
675                                                         rte_delay_us(burst_rx_delay_time);
676                                                         if (rx_count <= rte_vhost_avail_entries(vdev->vid, VIRTIO_RXQ))
677                                                                 break;
678                                                 }
679                                         }
680
681                                         ret_count = overlay_options.rx_handle(vdev->vid, pkts_burst, rx_count);
682                                         if (enable_stats) {
683                                                 rte_atomic64_add(
684                                                 &dev_statistics[vdev->vid].rx_total_atomic,
685                                                 rx_count);
686                                                 rte_atomic64_add(
687                                                 &dev_statistics[vdev->vid].rx_atomic, ret_count);
688                                         }
689                                         while (likely(rx_count)) {
690                                                 rx_count--;
691                                                 rte_pktmbuf_free(pkts_burst[rx_count]);
692                                         }
693
694                                 }
695                         }
696
697                         if (likely(!vdev->remove)) {
698                                 /* Handle guest TX*/
699                                 tx_count = rte_vhost_dequeue_burst(vdev->vid,
700                                                 VIRTIO_TXQ, mbuf_pool,
701                                                 pkts_burst, MAX_PKT_BURST);
702                                 /* If this is the first received packet we need to learn the MAC */
703                                 if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && tx_count) {
704                                         if (vdev->remove ||
705                                                 (overlay_options.tunnel_setup(vdev, pkts_burst[0]) == -1)) {
706                                                 while (tx_count)
707                                                         rte_pktmbuf_free(pkts_burst[--tx_count]);
708                                         }
709                                 }
710                                 while (tx_count)
711                                         virtio_tx_route(vdev, pkts_burst[--tx_count]);
712                         }
713
714                         /* move to the next device in the list */
715                         dev_ll = dev_ll->next;
716                 }
717         }
718
719         return 0;
720 }
721
722 /**
723  * Add an entry to a used linked list. A free entry must first be found
724  * in the free linked list using get_data_ll_free_entry();
725  */
726 static void
727 add_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
728         struct virtio_net_data_ll *ll_dev)
729 {
730         struct virtio_net_data_ll *ll = *ll_root_addr;
731
732         /* Set next as NULL and use a compiler barrier to avoid reordering. */
733         ll_dev->next = NULL;
734         rte_compiler_barrier();
735
736         /* If ll == NULL then this is the first device. */
737         if (ll) {
738                 /* Increment to the tail of the linked list. */
739                 while (ll->next != NULL)
740                         ll = ll->next;
741
742                 ll->next = ll_dev;
743         } else {
744                 *ll_root_addr = ll_dev;
745         }
746 }
747
748 /**
749  * Remove an entry from a used linked list. The entry must then be added to
750  * the free linked list using put_data_ll_free_entry().
751  */
752 static void
753 rm_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
754         struct virtio_net_data_ll *ll_dev,
755         struct virtio_net_data_ll *ll_dev_last)
756 {
757         struct virtio_net_data_ll *ll = *ll_root_addr;
758
759         if (unlikely((ll == NULL) || (ll_dev == NULL)))
760                 return;
761
762         if (ll_dev == ll)
763                 *ll_root_addr = ll_dev->next;
764         else
765                 if (likely(ll_dev_last != NULL))
766                         ll_dev_last->next = ll_dev->next;
767                 else
768                         RTE_LOG(ERR, VHOST_CONFIG,
769                                 "Remove entry form ll failed.\n");
770 }
771
772 /**
773  * Find and return an entry from the free linked list.
774  */
775 static struct virtio_net_data_ll *
776 get_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr)
777 {
778         struct virtio_net_data_ll *ll_free = *ll_root_addr;
779         struct virtio_net_data_ll *ll_dev;
780
781         if (ll_free == NULL)
782                 return NULL;
783
784         ll_dev = ll_free;
785         *ll_root_addr = ll_free->next;
786
787         return ll_dev;
788 }
789
790 /**
791  * Place an entry back on to the free linked list.
792  */
793 static void
794 put_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr,
795         struct virtio_net_data_ll *ll_dev)
796 {
797         struct virtio_net_data_ll *ll_free = *ll_root_addr;
798
799         if (ll_dev == NULL)
800                 return;
801
802         ll_dev->next = ll_free;
803         *ll_root_addr = ll_dev;
804 }
805
806 /**
807  * Creates a linked list of a given size.
808  */
809 static struct virtio_net_data_ll *
810 alloc_data_ll(uint32_t size)
811 {
812         struct virtio_net_data_ll *ll_new;
813         uint32_t i;
814
815         /* Malloc and then chain the linked list. */
816         ll_new = malloc(size * sizeof(struct virtio_net_data_ll));
817         if (ll_new == NULL) {
818                 RTE_LOG(ERR, VHOST_CONFIG,
819                         "Failed to allocate memory for ll_new.\n");
820                 return NULL;
821         }
822
823         for (i = 0; i < size - 1; i++) {
824                 ll_new[i].vdev = NULL;
825                 ll_new[i].next = &ll_new[i+1];
826         }
827         ll_new[i].next = NULL;
828
829         return ll_new;
830 }
831
832 /**
833  * Create the main linked list along with each individual cores
834  * linked list. A used and a free list are created to manage entries.
835  */
836 static int
837 init_data_ll(void)
838 {
839         int lcore;
840
841         RTE_LCORE_FOREACH_SLAVE(lcore) {
842                 lcore_info[lcore].lcore_ll =
843                         malloc(sizeof(struct lcore_ll_info));
844                 if (lcore_info[lcore].lcore_ll == NULL) {
845                         RTE_LOG(ERR, VHOST_CONFIG,
846                                 "Failed to allocate memory for lcore_ll.\n");
847                         return -1;
848                 }
849
850                 lcore_info[lcore].lcore_ll->device_num = 0;
851                 lcore_info[lcore].lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
852                 lcore_info[lcore].lcore_ll->ll_root_used = NULL;
853                 if (nb_devices % nb_switching_cores)
854                         lcore_info[lcore].lcore_ll->ll_root_free =
855                                 alloc_data_ll((nb_devices / nb_switching_cores)
856                                                 + 1);
857                 else
858                         lcore_info[lcore].lcore_ll->ll_root_free =
859                                 alloc_data_ll(nb_devices / nb_switching_cores);
860         }
861
862         /* Allocate devices up to a maximum of MAX_DEVICES. */
863         ll_root_free = alloc_data_ll(MIN((nb_devices), MAX_DEVICES));
864
865         return 0;
866 }
867
868 /**
869  * Remove a device from the specific data core linked list and
870  * from the main linked list. Synchonization occurs through the use
871  * of the lcore dev_removal_flag.
872  */
873 static void
874 destroy_device(int vid)
875 {
876         struct virtio_net_data_ll *ll_lcore_dev_cur;
877         struct virtio_net_data_ll *ll_main_dev_cur;
878         struct virtio_net_data_ll *ll_lcore_dev_last = NULL;
879         struct virtio_net_data_ll *ll_main_dev_last = NULL;
880         struct vhost_dev *vdev = NULL;
881         int lcore;
882
883         ll_main_dev_cur = ll_root_used;
884         while (ll_main_dev_cur != NULL) {
885                 if (ll_main_dev_cur->vdev->vid == vid) {
886                         vdev = ll_main_dev_cur->vdev;
887                         break;
888                 }
889         }
890         if (!vdev)
891                 return;
892
893         /* set the remove flag. */
894         vdev->remove = 1;
895         while (vdev->ready != DEVICE_SAFE_REMOVE)
896                 rte_pause();
897
898         /* Search for entry to be removed from lcore ll */
899         ll_lcore_dev_cur = lcore_info[vdev->coreid].lcore_ll->ll_root_used;
900         while (ll_lcore_dev_cur != NULL) {
901                 if (ll_lcore_dev_cur->vdev == vdev) {
902                         break;
903                 } else {
904                         ll_lcore_dev_last = ll_lcore_dev_cur;
905                         ll_lcore_dev_cur = ll_lcore_dev_cur->next;
906                 }
907         }
908
909         if (ll_lcore_dev_cur == NULL) {
910                 RTE_LOG(ERR, VHOST_CONFIG,
911                         "(%d) Failed to find the dev to be destroy.\n", vid);
912                 return;
913         }
914
915         /* Search for entry to be removed from main ll */
916         ll_main_dev_cur = ll_root_used;
917         ll_main_dev_last = NULL;
918         while (ll_main_dev_cur != NULL) {
919                 if (ll_main_dev_cur->vdev == vdev) {
920                         break;
921                 } else {
922                         ll_main_dev_last = ll_main_dev_cur;
923                         ll_main_dev_cur = ll_main_dev_cur->next;
924                 }
925         }
926
927         /* Remove entries from the lcore and main ll. */
928         rm_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
929                         ll_lcore_dev_cur, ll_lcore_dev_last);
930         rm_data_ll_entry(&ll_root_used, ll_main_dev_cur, ll_main_dev_last);
931
932         /* Set the dev_removal_flag on each lcore. */
933         RTE_LCORE_FOREACH_SLAVE(lcore) {
934                 lcore_info[lcore].lcore_ll->dev_removal_flag =
935                         REQUEST_DEV_REMOVAL;
936         }
937
938         /*
939          * Once each core has set the dev_removal_flag to
940          * ACK_DEV_REMOVAL we can be sure that they can no longer access
941          * the device removed from the linked lists and that the devices
942          * are no longer in use.
943          */
944         RTE_LCORE_FOREACH_SLAVE(lcore) {
945                 while (lcore_info[lcore].lcore_ll->dev_removal_flag
946                         != ACK_DEV_REMOVAL)
947                         rte_pause();
948         }
949
950         /* Add the entries back to the lcore and main free ll.*/
951         put_data_ll_free_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_free,
952                                 ll_lcore_dev_cur);
953         put_data_ll_free_entry(&ll_root_free, ll_main_dev_cur);
954
955         /* Decrement number of device on the lcore. */
956         lcore_info[vdev->coreid].lcore_ll->device_num--;
957
958         RTE_LOG(INFO, VHOST_DATA, "(%d) Device has been removed "
959                 "from data core\n", vid);
960
961         rte_free(vdev);
962
963 }
964
965 /**
966  * A new device is added to a data core. First the device is added
967  * to the main linked list and the allocated to a specific data core.
968  */
969 static int
970 new_device(int vid)
971 {
972         struct virtio_net_data_ll *ll_dev;
973         int lcore, core_add = 0;
974         uint32_t device_num_min = nb_devices;
975         struct vhost_dev *vdev;
976
977         vdev = rte_zmalloc("vhost device", sizeof(*vdev), RTE_CACHE_LINE_SIZE);
978         if (vdev == NULL) {
979                 RTE_LOG(INFO, VHOST_DATA,
980                         "(%d) Couldn't allocate memory for vhost dev\n", vid);
981                 return -1;
982         }
983         vdev->vid = vid;
984         /* Add device to main ll */
985         ll_dev = get_data_ll_free_entry(&ll_root_free);
986         if (ll_dev == NULL) {
987                 RTE_LOG(INFO, VHOST_DATA, "(%d) No free entry found in"
988                         " linked list Device limit of %d devices per core"
989                         " has been reached\n", vid, nb_devices);
990                 if (vdev->regions_hpa)
991                         rte_free(vdev->regions_hpa);
992                 rte_free(vdev);
993                 return -1;
994         }
995         ll_dev->vdev = vdev;
996         add_data_ll_entry(&ll_root_used, ll_dev);
997         vdev->rx_q = vid;
998
999         /* reset ready flag */
1000         vdev->ready = DEVICE_MAC_LEARNING;
1001         vdev->remove = 0;
1002
1003         /* Find a suitable lcore to add the device. */
1004         RTE_LCORE_FOREACH_SLAVE(lcore) {
1005                 if (lcore_info[lcore].lcore_ll->device_num < device_num_min) {
1006                         device_num_min = lcore_info[lcore].lcore_ll->device_num;
1007                         core_add = lcore;
1008                 }
1009         }
1010         /* Add device to lcore ll */
1011         ll_dev = get_data_ll_free_entry(&lcore_info[core_add].lcore_ll->ll_root_free);
1012         if (ll_dev == NULL) {
1013                 RTE_LOG(INFO, VHOST_DATA,
1014                         "(%d) Failed to add device to data core\n",
1015                         vid);
1016                 vdev->ready = DEVICE_SAFE_REMOVE;
1017                 destroy_device(vid);
1018                 rte_free(vdev->regions_hpa);
1019                 rte_free(vdev);
1020                 return -1;
1021         }
1022         ll_dev->vdev = vdev;
1023         vdev->coreid = core_add;
1024
1025         add_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
1026                         ll_dev);
1027
1028         /* Initialize device stats */
1029         memset(&dev_statistics[vid], 0,
1030                 sizeof(struct device_statistics));
1031
1032         /* Disable notifications. */
1033         rte_vhost_enable_guest_notification(vid, VIRTIO_RXQ, 0);
1034         rte_vhost_enable_guest_notification(vid, VIRTIO_TXQ, 0);
1035         lcore_info[vdev->coreid].lcore_ll->device_num++;
1036
1037         RTE_LOG(INFO, VHOST_DATA, "(%d) Device has been added to data core %d\n",
1038                 vid, vdev->coreid);
1039
1040         return 0;
1041 }
1042
1043 /**
1044  * These callback allow devices to be added to the data core when configuration
1045  * has been fully complete.
1046  */
1047 static const struct vhost_device_ops virtio_net_device_ops = {
1048         .new_device =  new_device,
1049         .destroy_device = destroy_device,
1050 };
1051
1052 /**
1053  * This is a thread will wake up after a period to print stats if the user has
1054  * enabled them.
1055  */
1056 static void *
1057 print_stats(__rte_unused void *arg)
1058 {
1059         struct virtio_net_data_ll *dev_ll;
1060         uint64_t tx_dropped, rx_dropped;
1061         uint64_t tx, tx_total, rx, rx_total, rx_ip_csum, rx_l4_csum;
1062         int vid;
1063         const char clr[] = { 27, '[', '2', 'J', '\0' };
1064         const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
1065
1066         while (1) {
1067                 sleep(enable_stats);
1068
1069                 /* Clear screen and move to top left */
1070                 printf("%s%s", clr, top_left);
1071
1072                 printf("\nDevice statistics ================================");
1073
1074                 dev_ll = ll_root_used;
1075                 while (dev_ll != NULL) {
1076                         vid = dev_ll->vdev->vid;
1077                         tx_total = dev_statistics[vid].tx_total;
1078                         tx = dev_statistics[vid].tx;
1079                         tx_dropped = tx_total - tx;
1080
1081                         rx_total = rte_atomic64_read(
1082                                 &dev_statistics[vid].rx_total_atomic);
1083                         rx = rte_atomic64_read(
1084                                 &dev_statistics[vid].rx_atomic);
1085                         rx_dropped = rx_total - rx;
1086                         rx_ip_csum = rte_atomic64_read(
1087                                 &dev_statistics[vid].rx_bad_ip_csum);
1088                         rx_l4_csum = rte_atomic64_read(
1089                                 &dev_statistics[vid].rx_bad_l4_csum);
1090
1091                         printf("\nStatistics for device %d ----------"
1092                                         "\nTX total:            %"PRIu64""
1093                                         "\nTX dropped:          %"PRIu64""
1094                                         "\nTX successful:               %"PRIu64""
1095                                         "\nRX total:            %"PRIu64""
1096                                         "\nRX bad IP csum:      %"PRIu64""
1097                                         "\nRX bad L4 csum:      %"PRIu64""
1098                                         "\nRX dropped:          %"PRIu64""
1099                                         "\nRX successful:               %"PRIu64"",
1100                                         vid,
1101                                         tx_total,
1102                                         tx_dropped,
1103                                         tx,
1104                                         rx_total,
1105                                         rx_ip_csum,
1106                                         rx_l4_csum,
1107                                         rx_dropped,
1108                                         rx);
1109
1110                         dev_ll = dev_ll->next;
1111                 }
1112                 printf("\n================================================\n");
1113         }
1114
1115         return NULL;
1116 }
1117
1118 /**
1119  * Main function, does initialisation and calls the per-lcore functions.
1120  */
1121 int
1122 main(int argc, char *argv[])
1123 {
1124         struct rte_mempool *mbuf_pool = NULL;
1125         unsigned lcore_id, core_id = 0;
1126         unsigned nb_ports, valid_nb_ports;
1127         int ret;
1128         uint16_t portid;
1129         uint16_t queue_id;
1130         static pthread_t tid;
1131
1132         /* init EAL */
1133         ret = rte_eal_init(argc, argv);
1134         if (ret < 0)
1135                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
1136         argc -= ret;
1137         argv += ret;
1138
1139         /* parse app arguments */
1140         ret = tep_termination_parse_args(argc, argv);
1141         if (ret < 0)
1142                 rte_exit(EXIT_FAILURE, "Invalid argument\n");
1143
1144         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1145                 if (rte_lcore_is_enabled(lcore_id))
1146                         lcore_ids[core_id++] = lcore_id;
1147
1148         /* set the number of swithcing cores available */
1149         nb_switching_cores = rte_lcore_count()-1;
1150
1151         /* Get the number of physical ports. */
1152         nb_ports = rte_eth_dev_count_avail();
1153
1154         /*
1155          * Update the global var NB_PORTS and global array PORTS
1156          * and get value of var VALID_NB_PORTS according to system ports number
1157          */
1158         valid_nb_ports = check_ports_num(nb_ports);
1159
1160         if ((valid_nb_ports == 0) || (valid_nb_ports > MAX_SUP_PORTS)) {
1161                 rte_exit(EXIT_FAILURE, "Current enabled port number is %u,"
1162                         "but only %u port can be enabled\n", nb_ports,
1163                         MAX_SUP_PORTS);
1164         }
1165         /* Create the mbuf pool. */
1166         mbuf_pool = rte_pktmbuf_pool_create(
1167                         "MBUF_POOL",
1168                         NUM_MBUFS_PER_PORT * valid_nb_ports,
1169                         MBUF_CACHE_SIZE,
1170                         0,
1171                         MBUF_DATA_SIZE,
1172                         rte_socket_id());
1173         if (mbuf_pool == NULL)
1174                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1175
1176         for (queue_id = 0; queue_id < MAX_QUEUES + 1; queue_id++)
1177                 vpool_array[queue_id].pool = mbuf_pool;
1178
1179         /* initialize all ports */
1180         RTE_ETH_FOREACH_DEV(portid) {
1181                 /* skip ports that are not enabled */
1182                 if ((enabled_port_mask & (1 << portid)) == 0) {
1183                         RTE_LOG(INFO, VHOST_PORT,
1184                                 "Skipping disabled port %d\n", portid);
1185                         continue;
1186                 }
1187                 if (overlay_options.port_configure(portid, mbuf_pool) != 0)
1188                         rte_exit(EXIT_FAILURE,
1189                                 "Cannot initialize network ports\n");
1190         }
1191
1192         /* Initialise all linked lists. */
1193         if (init_data_ll() == -1)
1194                 rte_exit(EXIT_FAILURE, "Failed to initialize linked list\n");
1195
1196         /* Initialize device stats */
1197         memset(&dev_statistics, 0, sizeof(dev_statistics));
1198
1199         /* Enable stats if the user option is set. */
1200         if (enable_stats) {
1201                 ret = rte_ctrl_thread_create(&tid, "print-stats", NULL,
1202                                         print_stats, NULL);
1203                 if (ret < 0)
1204                         rte_exit(EXIT_FAILURE, "Cannot create print-stats thread\n");
1205         }
1206
1207         /* Launch all data cores. */
1208         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1209                 rte_eal_remote_launch(switch_worker,
1210                         mbuf_pool, lcore_id);
1211         }
1212
1213         ret = rte_vhost_driver_register((char *)&dev_basename, 0);
1214         if (ret != 0)
1215                 rte_exit(EXIT_FAILURE, "failed to register vhost driver.\n");
1216
1217         rte_vhost_driver_disable_features(dev_basename,
1218                 1ULL << VIRTIO_NET_F_MRG_RXBUF);
1219
1220         ret = rte_vhost_driver_callback_register(dev_basename,
1221                 &virtio_net_device_ops);
1222         if (ret != 0) {
1223                 rte_exit(EXIT_FAILURE,
1224                         "failed to register vhost driver callbacks.\n");
1225         }
1226
1227         if (rte_vhost_driver_start(dev_basename) < 0) {
1228                 rte_exit(EXIT_FAILURE,
1229                         "failed to start vhost driver.\n");
1230         }
1231
1232         RTE_LCORE_FOREACH_SLAVE(lcore_id)
1233                 rte_eal_wait_lcore(lcore_id);
1234
1235         return 0;
1236 }