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