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