netdev-dpdk: Use default NIC configuration.
[cascardo/ovs.git] / lib / netdev-dpdk.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <pthread.h>
24 #include <config.h>
25 #include <errno.h>
26 #include <sched.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30
31 #include "dp-packet.h"
32 #include "dpif-netdev.h"
33 #include "list.h"
34 #include "netdev-dpdk.h"
35 #include "netdev-provider.h"
36 #include "netdev-vport.h"
37 #include "odp-util.h"
38 #include "ofp-print.h"
39 #include "ovs-numa.h"
40 #include "ovs-thread.h"
41 #include "ovs-rcu.h"
42 #include "packets.h"
43 #include "shash.h"
44 #include "sset.h"
45 #include "unaligned.h"
46 #include "timeval.h"
47 #include "unixctl.h"
48 #include "openvswitch/vlog.h"
49
50 #include "rte_config.h"
51 #include "rte_mbuf.h"
52 #include "rte_virtio_net.h"
53
54 VLOG_DEFINE_THIS_MODULE(dpdk);
55 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
56
57 #define DPDK_PORT_WATCHDOG_INTERVAL 5
58
59 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
60 #define OVS_VPORT_DPDK "ovs_dpdk"
61
62 /*
63  * need to reserve tons of extra space in the mbufs so we can align the
64  * DMA addresses to 4KB.
65  */
66
67 #define MTU_TO_MAX_LEN(mtu)  ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
68 #define MBUF_SIZE(mtu)       (MTU_TO_MAX_LEN(mtu) + (512) + \
69                              sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
70
71 /* Max and min number of packets in the mempool.  OVS tries to allocate a
72  * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
73  * enough hugepages) we keep halving the number until the allocation succeeds
74  * or we reach MIN_NB_MBUF */
75
76 #define MAX_NB_MBUF          (4096 * 64)
77 #define MIN_NB_MBUF          (4096 * 4)
78 #define MP_CACHE_SZ          RTE_MEMPOOL_CACHE_MAX_SIZE
79
80 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
81 BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF) == 0);
82
83 /* The smallest possible NB_MBUF that we're going to try should be a multiple
84  * of MP_CACHE_SZ. This is advised by DPDK documentation. */
85 BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF))
86                   % MP_CACHE_SZ == 0);
87
88 #define SOCKET0              0
89
90 #define NIC_PORT_RX_Q_SIZE 2048  /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
91 #define NIC_PORT_TX_Q_SIZE 2048  /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
92
93 /* Character device cuse_dev_name. */
94 static char *cuse_dev_name = NULL;
95
96 /*
97  * Maximum amount of time in micro seconds to try and enqueue to vhost.
98  */
99 #define VHOST_ENQ_RETRY_USECS 100
100
101 static const struct rte_eth_conf port_conf = {
102     .rxmode = {
103         .mq_mode = ETH_MQ_RX_RSS,
104         .split_hdr_size = 0,
105         .header_split   = 0, /* Header Split disabled */
106         .hw_ip_checksum = 0, /* IP checksum offload disabled */
107         .hw_vlan_filter = 0, /* VLAN filtering disabled */
108         .jumbo_frame    = 0, /* Jumbo Frame Support disabled */
109         .hw_strip_crc   = 0,
110     },
111     .rx_adv_conf = {
112         .rss_conf = {
113             .rss_key = NULL,
114             .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP,
115         },
116     },
117     .txmode = {
118         .mq_mode = ETH_MQ_TX_NONE,
119     },
120 };
121
122 enum { MAX_TX_QUEUE_LEN = 384 };
123 enum { DPDK_RING_SIZE = 256 };
124 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
125 enum { DRAIN_TSC = 200000ULL };
126
127 enum dpdk_dev_type {
128     DPDK_DEV_ETH = 0,
129     DPDK_DEV_VHOST = 1
130 };
131
132 static int rte_eal_init_ret = ENODEV;
133
134 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
135
136 /* Contains all 'struct dpdk_dev's. */
137 static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
138     = OVS_LIST_INITIALIZER(&dpdk_list);
139
140 static struct ovs_list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
141     = OVS_LIST_INITIALIZER(&dpdk_mp_list);
142
143 /* This mutex must be used by non pmd threads when allocating or freeing
144  * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
145  * use mempools, a non pmd thread should hold this mutex while calling them */
146 static struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
147
148 struct dpdk_mp {
149     struct rte_mempool *mp;
150     int mtu;
151     int socket_id;
152     int refcount;
153     struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
154 };
155
156 /* There should be one 'struct dpdk_tx_queue' created for
157  * each cpu core. */
158 struct dpdk_tx_queue {
159     bool flush_tx;                 /* Set to true to flush queue everytime */
160                                    /* pkts are queued. */
161     int count;
162     uint64_t tsc;
163     struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
164 };
165
166 /* dpdk has no way to remove dpdk ring ethernet devices
167    so we have to keep them around once they've been created
168 */
169
170 static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
171     = OVS_LIST_INITIALIZER(&dpdk_ring_list);
172
173 struct dpdk_ring {
174     /* For the client rings */
175     struct rte_ring *cring_tx;
176     struct rte_ring *cring_rx;
177     int user_port_id; /* User given port no, parsed from port name */
178     int eth_port_id; /* ethernet device port id */
179     struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
180 };
181
182 struct netdev_dpdk {
183     struct netdev up;
184     int port_id;
185     int max_packet_len;
186     enum dpdk_dev_type type;
187
188     struct dpdk_tx_queue *tx_q;
189
190     struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
191
192     struct dpdk_mp *dpdk_mp;
193     int mtu;
194     int socket_id;
195     int buf_size;
196     struct netdev_stats stats;
197
198     uint8_t hwaddr[ETH_ADDR_LEN];
199     enum netdev_flags flags;
200
201     struct rte_eth_link link;
202     int link_reset_cnt;
203
204     /* virtio-net structure for vhost device */
205     OVSRCU_TYPE(struct virtio_net *) virtio_dev;
206
207     /* In dpdk_list. */
208     struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
209     rte_spinlock_t txq_lock;
210 };
211
212 struct netdev_rxq_dpdk {
213     struct netdev_rxq up;
214     int port_id;
215 };
216
217 static bool thread_is_pmd(void);
218
219 static int netdev_dpdk_construct(struct netdev *);
220
221 struct virtio_net * netdev_dpdk_get_virtio(const struct netdev_dpdk *dev);
222
223 static bool
224 is_dpdk_class(const struct netdev_class *class)
225 {
226     return class->construct == netdev_dpdk_construct;
227 }
228
229 /* XXX: use dpdk malloc for entire OVS. in fact huge page should be used
230  * for all other segments data, bss and text. */
231
232 static void *
233 dpdk_rte_mzalloc(size_t sz)
234 {
235     void *ptr;
236
237     ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
238     if (ptr == NULL) {
239         out_of_memory();
240     }
241     return ptr;
242 }
243
244 /* XXX this function should be called only by pmd threads (or by non pmd
245  * threads holding the nonpmd_mempool_mutex) */
246 void
247 free_dpdk_buf(struct dp_packet *p)
248 {
249     struct rte_mbuf *pkt = (struct rte_mbuf *) p;
250
251     rte_pktmbuf_free_seg(pkt);
252 }
253
254 static void
255 __rte_pktmbuf_init(struct rte_mempool *mp,
256                    void *opaque_arg OVS_UNUSED,
257                    void *_m,
258                    unsigned i OVS_UNUSED)
259 {
260     struct rte_mbuf *m = _m;
261     uint32_t buf_len = mp->elt_size - sizeof(struct dp_packet);
262
263     RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dp_packet));
264
265     memset(m, 0, mp->elt_size);
266
267     /* start of buffer is just after mbuf structure */
268     m->buf_addr = (char *)m + sizeof(struct dp_packet);
269     m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
270                     sizeof(struct dp_packet);
271     m->buf_len = (uint16_t)buf_len;
272
273     /* keep some headroom between start of buffer and data */
274     m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
275
276     /* init some constant fields */
277     m->pool = mp;
278     m->nb_segs = 1;
279     m->port = 0xff;
280 }
281
282 static void
283 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
284                      void *opaque_arg OVS_UNUSED,
285                      void *_m,
286                      unsigned i OVS_UNUSED)
287 {
288     struct rte_mbuf *m = _m;
289
290     __rte_pktmbuf_init(mp, opaque_arg, _m, i);
291
292     dp_packet_init_dpdk((struct dp_packet *) m, m->buf_len);
293 }
294
295 static struct dpdk_mp *
296 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
297 {
298     struct dpdk_mp *dmp = NULL;
299     char mp_name[RTE_MEMPOOL_NAMESIZE];
300     unsigned mp_size;
301
302     LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
303         if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
304             dmp->refcount++;
305             return dmp;
306         }
307     }
308
309     dmp = dpdk_rte_mzalloc(sizeof *dmp);
310     dmp->socket_id = socket_id;
311     dmp->mtu = mtu;
312     dmp->refcount = 1;
313
314     mp_size = MAX_NB_MBUF;
315     do {
316         if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d_%d_%u",
317                      dmp->mtu, dmp->socket_id, mp_size) < 0) {
318             return NULL;
319         }
320
321         dmp->mp = rte_mempool_create(mp_name, mp_size, MBUF_SIZE(mtu),
322                                      MP_CACHE_SZ,
323                                      sizeof(struct rte_pktmbuf_pool_private),
324                                      rte_pktmbuf_pool_init, NULL,
325                                      ovs_rte_pktmbuf_init, NULL,
326                                      socket_id, 0);
327     } while (!dmp->mp && rte_errno == ENOMEM && (mp_size /= 2) >= MIN_NB_MBUF);
328
329     if (dmp->mp == NULL) {
330         return NULL;
331     } else {
332         VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", mp_name, mp_size );
333     }
334
335     list_push_back(&dpdk_mp_list, &dmp->list_node);
336     return dmp;
337 }
338
339 static void
340 dpdk_mp_put(struct dpdk_mp *dmp)
341 {
342
343     if (!dmp) {
344         return;
345     }
346
347     dmp->refcount--;
348     ovs_assert(dmp->refcount >= 0);
349
350 #if 0
351     /* I could not find any API to destroy mp. */
352     if (dmp->refcount == 0) {
353         list_delete(dmp->list_node);
354         /* destroy mp-pool. */
355     }
356 #endif
357 }
358
359 static void
360 check_link_status(struct netdev_dpdk *dev)
361 {
362     struct rte_eth_link link;
363
364     rte_eth_link_get_nowait(dev->port_id, &link);
365
366     if (dev->link.link_status != link.link_status) {
367         netdev_change_seq_changed(&dev->up);
368
369         dev->link_reset_cnt++;
370         dev->link = link;
371         if (dev->link.link_status) {
372             VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
373                         dev->port_id, (unsigned)dev->link.link_speed,
374                         (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
375                          ("full-duplex") : ("half-duplex"));
376         } else {
377             VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
378         }
379     }
380 }
381
382 static void *
383 dpdk_watchdog(void *dummy OVS_UNUSED)
384 {
385     struct netdev_dpdk *dev;
386
387     pthread_detach(pthread_self());
388
389     for (;;) {
390         ovs_mutex_lock(&dpdk_mutex);
391         LIST_FOR_EACH (dev, list_node, &dpdk_list) {
392             ovs_mutex_lock(&dev->mutex);
393             check_link_status(dev);
394             ovs_mutex_unlock(&dev->mutex);
395         }
396         ovs_mutex_unlock(&dpdk_mutex);
397         xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
398     }
399
400     return NULL;
401 }
402
403 static int
404 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
405 {
406     struct rte_pktmbuf_pool_private *mbp_priv;
407     struct ether_addr eth_addr;
408     int diag;
409     int i;
410
411     if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
412         return ENODEV;
413     }
414
415     diag = rte_eth_dev_configure(dev->port_id, dev->up.n_rxq, dev->up.n_txq,
416                                  &port_conf);
417     if (diag) {
418         VLOG_ERR("eth dev config error %d",diag);
419         return -diag;
420     }
421
422     for (i = 0; i < dev->up.n_txq; i++) {
423         diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
424                                       dev->socket_id, NULL);
425         if (diag) {
426             VLOG_ERR("eth dev tx queue setup error %d",diag);
427             return -diag;
428         }
429     }
430
431     for (i = 0; i < dev->up.n_rxq; i++) {
432         diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
433                                       dev->socket_id,
434                                       NULL, dev->dpdk_mp->mp);
435         if (diag) {
436             VLOG_ERR("eth dev rx queue setup error %d",diag);
437             return -diag;
438         }
439     }
440
441     diag = rte_eth_dev_start(dev->port_id);
442     if (diag) {
443         VLOG_ERR("eth dev start error %d",diag);
444         return -diag;
445     }
446
447     rte_eth_promiscuous_enable(dev->port_id);
448     rte_eth_allmulticast_enable(dev->port_id);
449
450     memset(&eth_addr, 0x0, sizeof(eth_addr));
451     rte_eth_macaddr_get(dev->port_id, &eth_addr);
452     VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
453                     dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
454
455     memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
456     rte_eth_link_get_nowait(dev->port_id, &dev->link);
457
458     mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
459     dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
460
461     dev->flags = NETDEV_UP | NETDEV_PROMISC;
462     return 0;
463 }
464
465 static struct netdev_dpdk *
466 netdev_dpdk_cast(const struct netdev *netdev)
467 {
468     return CONTAINER_OF(netdev, struct netdev_dpdk, up);
469 }
470
471 static struct netdev *
472 netdev_dpdk_alloc(void)
473 {
474     struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
475     return &netdev->up;
476 }
477
478 static void
479 netdev_dpdk_alloc_txq(struct netdev_dpdk *netdev, unsigned int n_txqs)
480 {
481     int i;
482
483     netdev->tx_q = dpdk_rte_mzalloc(n_txqs * sizeof *netdev->tx_q);
484     /* Each index is considered as a cpu core id, since there should
485      * be one tx queue for each cpu core. */
486     for (i = 0; i < n_txqs; i++) {
487         int numa_id = ovs_numa_get_numa_id(i);
488
489         /* If the corresponding core is not on the same numa node
490          * as 'netdev', flags the 'flush_tx'. */
491         netdev->tx_q[i].flush_tx = netdev->socket_id == numa_id;
492     }
493 }
494
495 static int
496 netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no,
497                  enum dpdk_dev_type type)
498     OVS_REQUIRES(dpdk_mutex)
499 {
500     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
501     int sid;
502     int err = 0;
503
504     ovs_mutex_init(&netdev->mutex);
505     ovs_mutex_lock(&netdev->mutex);
506
507     /* If the 'sid' is negative, it means that the kernel fails
508      * to obtain the pci numa info.  In that situation, always
509      * use 'SOCKET0'. */
510     if (type == DPDK_DEV_ETH) {
511         sid = rte_eth_dev_socket_id(port_no);
512     } else {
513         sid = rte_lcore_to_socket_id(rte_get_master_lcore());
514     }
515
516     netdev->socket_id = sid < 0 ? SOCKET0 : sid;
517     netdev->port_id = port_no;
518     netdev->type = type;
519     netdev->flags = 0;
520     netdev->mtu = ETHER_MTU;
521     netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
522     rte_spinlock_init(&netdev->txq_lock);
523
524     netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
525     if (!netdev->dpdk_mp) {
526         err = ENOMEM;
527         goto unlock;
528     }
529
530     netdev_->n_txq = NR_QUEUE;
531     netdev_->n_rxq = NR_QUEUE;
532
533     if (type == DPDK_DEV_ETH) {
534         netdev_dpdk_alloc_txq(netdev, NR_QUEUE);
535         err = dpdk_eth_dev_init(netdev);
536         if (err) {
537             goto unlock;
538         }
539     }
540
541     list_push_back(&dpdk_list, &netdev->list_node);
542
543 unlock:
544     if (err) {
545         rte_free(netdev->tx_q);
546     }
547     ovs_mutex_unlock(&netdev->mutex);
548     return err;
549 }
550
551 static int
552 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
553                     unsigned int *port_no)
554 {
555     const char *cport;
556
557     if (strncmp(dev_name, prefix, strlen(prefix))) {
558         return ENODEV;
559     }
560
561     cport = dev_name + strlen(prefix);
562     *port_no = strtol(cport, NULL, 0); /* string must be null terminated */
563     return 0;
564 }
565
566 static int
567 netdev_dpdk_vhost_construct(struct netdev *netdev_)
568 {
569     int err;
570
571     if (rte_eal_init_ret) {
572         return rte_eal_init_ret;
573     }
574
575     ovs_mutex_lock(&dpdk_mutex);
576     err = netdev_dpdk_init(netdev_, -1, DPDK_DEV_VHOST);
577     ovs_mutex_unlock(&dpdk_mutex);
578
579     return err;
580 }
581
582 static int
583 netdev_dpdk_construct(struct netdev *netdev)
584 {
585     unsigned int port_no;
586     int err;
587
588     if (rte_eal_init_ret) {
589         return rte_eal_init_ret;
590     }
591
592     /* Names always start with "dpdk" */
593     err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
594     if (err) {
595         return err;
596     }
597
598     ovs_mutex_lock(&dpdk_mutex);
599     err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
600     ovs_mutex_unlock(&dpdk_mutex);
601     return err;
602 }
603
604 static void
605 netdev_dpdk_destruct(struct netdev *netdev_)
606 {
607     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
608
609     ovs_mutex_lock(&dev->mutex);
610     rte_eth_dev_stop(dev->port_id);
611     ovs_mutex_unlock(&dev->mutex);
612
613     ovs_mutex_lock(&dpdk_mutex);
614     rte_free(dev->tx_q);
615     list_remove(&dev->list_node);
616     dpdk_mp_put(dev->dpdk_mp);
617     ovs_mutex_unlock(&dpdk_mutex);
618 }
619
620 static void
621 netdev_dpdk_vhost_destruct(struct netdev *netdev_)
622 {
623     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
624
625     /* Can't remove a port while a guest is attached to it. */
626     if (netdev_dpdk_get_virtio(dev) != NULL) {
627         VLOG_ERR("Can not remove port, vhost device still attached");
628                 return;
629     }
630
631     ovs_mutex_lock(&dpdk_mutex);
632     list_remove(&dev->list_node);
633     dpdk_mp_put(dev->dpdk_mp);
634     ovs_mutex_unlock(&dpdk_mutex);
635 }
636
637 static void
638 netdev_dpdk_dealloc(struct netdev *netdev_)
639 {
640     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
641
642     rte_free(netdev);
643 }
644
645 static int
646 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
647 {
648     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
649
650     ovs_mutex_lock(&dev->mutex);
651
652     smap_add_format(args, "configured_rx_queues", "%d", netdev_->n_rxq);
653     smap_add_format(args, "configured_tx_queues", "%d", netdev_->n_txq);
654     ovs_mutex_unlock(&dev->mutex);
655
656     return 0;
657 }
658
659 static int
660 netdev_dpdk_get_numa_id(const struct netdev *netdev_)
661 {
662     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
663
664     return netdev->socket_id;
665 }
666
667 /* Sets the number of tx queues and rx queues for the dpdk interface.
668  * If the configuration fails, do not try restoring its old configuration
669  * and just returns the error. */
670 static int
671 netdev_dpdk_set_multiq(struct netdev *netdev_, unsigned int n_txq,
672                        unsigned int n_rxq)
673 {
674     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
675     int err = 0;
676
677     if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
678         return err;
679     }
680
681     ovs_mutex_lock(&dpdk_mutex);
682     ovs_mutex_lock(&netdev->mutex);
683
684     rte_eth_dev_stop(netdev->port_id);
685
686     netdev->up.n_txq = n_txq;
687     netdev->up.n_rxq = n_rxq;
688
689     rte_free(netdev->tx_q);
690     netdev_dpdk_alloc_txq(netdev, n_txq);
691     err = dpdk_eth_dev_init(netdev);
692
693     ovs_mutex_unlock(&netdev->mutex);
694     ovs_mutex_unlock(&dpdk_mutex);
695
696     return err;
697 }
698
699 static int
700 netdev_dpdk_vhost_set_multiq(struct netdev *netdev_, unsigned int n_txq,
701                        unsigned int n_rxq)
702 {
703     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
704     int err = 0;
705
706     if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
707         return err;
708     }
709
710     ovs_mutex_lock(&dpdk_mutex);
711     ovs_mutex_lock(&netdev->mutex);
712
713     netdev->up.n_txq = n_txq;
714     netdev->up.n_rxq = n_rxq;
715
716     ovs_mutex_unlock(&netdev->mutex);
717     ovs_mutex_unlock(&dpdk_mutex);
718
719     return err;
720 }
721
722 static struct netdev_rxq *
723 netdev_dpdk_rxq_alloc(void)
724 {
725     struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
726
727     return &rx->up;
728 }
729
730 static struct netdev_rxq_dpdk *
731 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
732 {
733     return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
734 }
735
736 static int
737 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
738 {
739     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
740     struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
741
742     ovs_mutex_lock(&netdev->mutex);
743     rx->port_id = netdev->port_id;
744     ovs_mutex_unlock(&netdev->mutex);
745
746     return 0;
747 }
748
749 static void
750 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
751 {
752 }
753
754 static void
755 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
756 {
757     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
758
759     rte_free(rx);
760 }
761
762 static inline void
763 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
764 {
765     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
766     uint32_t nb_tx = 0;
767
768     while (nb_tx != txq->count) {
769         uint32_t ret;
770
771         ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
772                                txq->count - nb_tx);
773         if (!ret) {
774             break;
775         }
776
777         nb_tx += ret;
778     }
779
780     if (OVS_UNLIKELY(nb_tx != txq->count)) {
781         /* free buffers, which we couldn't transmit, one at a time (each
782          * packet could come from a different mempool) */
783         int i;
784
785         for (i = nb_tx; i < txq->count; i++) {
786             rte_pktmbuf_free_seg(txq->burst_pkts[i]);
787         }
788         ovs_mutex_lock(&dev->mutex);
789         dev->stats.tx_dropped += txq->count-nb_tx;
790         ovs_mutex_unlock(&dev->mutex);
791     }
792
793     txq->count = 0;
794     txq->tsc = rte_get_timer_cycles();
795 }
796
797 static inline void
798 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
799 {
800     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
801
802     if (txq->count == 0) {
803         return;
804     }
805     dpdk_queue_flush__(dev, qid);
806 }
807
808 static bool
809 is_vhost_running(struct virtio_net *dev)
810 {
811     return (dev != NULL && (dev->flags & VIRTIO_DEV_RUNNING));
812 }
813
814 /*
815  * The receive path for the vhost port is the TX path out from guest.
816  */
817 static int
818 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq_,
819                            struct dp_packet **packets, int *c)
820 {
821     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
822     struct netdev *netdev = rx->up.netdev;
823     struct netdev_dpdk *vhost_dev = netdev_dpdk_cast(netdev);
824     struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(vhost_dev);
825     int qid = 1;
826     uint16_t nb_rx = 0;
827
828     if (OVS_UNLIKELY(!is_vhost_running(virtio_dev))) {
829         return EAGAIN;
830     }
831
832     nb_rx = rte_vhost_dequeue_burst(virtio_dev, qid,
833                                     vhost_dev->dpdk_mp->mp,
834                                     (struct rte_mbuf **)packets,
835                                     NETDEV_MAX_BURST);
836     if (!nb_rx) {
837         return EAGAIN;
838     }
839
840     vhost_dev->stats.rx_packets += (uint64_t)nb_rx;
841     *c = (int) nb_rx;
842     return 0;
843 }
844
845 static int
846 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **packets,
847                      int *c)
848 {
849     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
850     struct netdev *netdev = rx->up.netdev;
851     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
852     int nb_rx;
853
854     /* There is only one tx queue for this core.  Do not flush other
855      * queueus. */
856     if (rxq_->queue_id == rte_lcore_id()) {
857         dpdk_queue_flush(dev, rxq_->queue_id);
858     }
859
860     nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
861                              (struct rte_mbuf **) packets,
862                              NETDEV_MAX_BURST);
863     if (!nb_rx) {
864         return EAGAIN;
865     }
866
867     *c = nb_rx;
868
869     return 0;
870 }
871
872 static void
873 __netdev_dpdk_vhost_send(struct netdev *netdev, struct dp_packet **pkts,
874                          int cnt, bool may_steal)
875 {
876     struct netdev_dpdk *vhost_dev = netdev_dpdk_cast(netdev);
877     struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(vhost_dev);
878     struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
879     unsigned int total_pkts = cnt;
880     uint64_t start = 0;
881
882     if (OVS_UNLIKELY(!is_vhost_running(virtio_dev))) {
883         ovs_mutex_lock(&vhost_dev->mutex);
884         vhost_dev->stats.tx_dropped+= cnt;
885         ovs_mutex_unlock(&vhost_dev->mutex);
886         goto out;
887     }
888
889     /* There is vHost TX single queue, So we need to lock it for TX. */
890     rte_spinlock_lock(&vhost_dev->txq_lock);
891
892     do {
893         unsigned int tx_pkts;
894
895         tx_pkts = rte_vhost_enqueue_burst(virtio_dev, VIRTIO_RXQ,
896                                           cur_pkts, cnt);
897         if (OVS_LIKELY(tx_pkts)) {
898             /* Packets have been sent.*/
899             cnt -= tx_pkts;
900             /* Prepare for possible next iteration.*/
901             cur_pkts = &cur_pkts[tx_pkts];
902         } else {
903             uint64_t timeout = VHOST_ENQ_RETRY_USECS * rte_get_timer_hz() / 1E6;
904             unsigned int expired = 0;
905
906             if (!start) {
907                 start = rte_get_timer_cycles();
908             }
909
910             /*
911              * Unable to enqueue packets to vhost interface.
912              * Check available entries before retrying.
913              */
914             while (!rte_vring_available_entries(virtio_dev, VIRTIO_RXQ)) {
915                 if (OVS_UNLIKELY((rte_get_timer_cycles() - start) > timeout)) {
916                     expired = 1;
917                     break;
918                 }
919             }
920             if (expired) {
921                 /* break out of main loop. */
922                 break;
923             }
924         }
925     } while (cnt);
926
927     vhost_dev->stats.tx_packets += (total_pkts - cnt);
928     vhost_dev->stats.tx_dropped += cnt;
929     rte_spinlock_unlock(&vhost_dev->txq_lock);
930
931 out:
932     if (may_steal) {
933         int i;
934
935         for (i = 0; i < total_pkts; i++) {
936             dp_packet_delete(pkts[i]);
937         }
938     }
939 }
940
941 inline static void
942 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
943                struct rte_mbuf **pkts, int cnt)
944 {
945     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
946     uint64_t diff_tsc;
947
948     int i = 0;
949
950     while (i < cnt) {
951         int freeslots = MAX_TX_QUEUE_LEN - txq->count;
952         int tocopy = MIN(freeslots, cnt-i);
953
954         memcpy(&txq->burst_pkts[txq->count], &pkts[i],
955                tocopy * sizeof (struct rte_mbuf *));
956
957         txq->count += tocopy;
958         i += tocopy;
959
960         if (txq->count == MAX_TX_QUEUE_LEN || txq->flush_tx) {
961             dpdk_queue_flush__(dev, qid);
962         }
963         diff_tsc = rte_get_timer_cycles() - txq->tsc;
964         if (diff_tsc >= DRAIN_TSC) {
965             dpdk_queue_flush__(dev, qid);
966         }
967     }
968 }
969
970 /* Tx function. Transmit packets indefinitely */
971 static void
972 dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet **pkts,
973                 int cnt)
974     OVS_NO_THREAD_SAFETY_ANALYSIS
975 {
976 #if !defined(__CHECKER__) && !defined(_WIN32)
977     const size_t PKT_ARRAY_SIZE = cnt;
978 #else
979     /* Sparse or MSVC doesn't like variable length array. */
980     enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
981 #endif
982     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
983     struct rte_mbuf *mbufs[PKT_ARRAY_SIZE];
984     int dropped = 0;
985     int newcnt = 0;
986     int i;
987
988     /* If we are on a non pmd thread we have to use the mempool mutex, because
989      * every non pmd thread shares the same mempool cache */
990
991     if (!thread_is_pmd()) {
992         ovs_mutex_lock(&nonpmd_mempool_mutex);
993     }
994
995     for (i = 0; i < cnt; i++) {
996         int size = dp_packet_size(pkts[i]);
997
998         if (OVS_UNLIKELY(size > dev->max_packet_len)) {
999             VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1000                          (int)size , dev->max_packet_len);
1001
1002             dropped++;
1003             continue;
1004         }
1005
1006         mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
1007
1008         if (!mbufs[newcnt]) {
1009             dropped += cnt - i;
1010             break;
1011         }
1012
1013         /* We have to do a copy for now */
1014         memcpy(rte_pktmbuf_mtod(mbufs[newcnt], void *), dp_packet_data(pkts[i]), size);
1015
1016         rte_pktmbuf_data_len(mbufs[newcnt]) = size;
1017         rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
1018
1019         newcnt++;
1020     }
1021
1022     if (OVS_UNLIKELY(dropped)) {
1023         ovs_mutex_lock(&dev->mutex);
1024         dev->stats.tx_dropped += dropped;
1025         ovs_mutex_unlock(&dev->mutex);
1026     }
1027
1028     if (dev->type == DPDK_DEV_VHOST) {
1029         __netdev_dpdk_vhost_send(netdev, (struct dp_packet **) mbufs, newcnt, true);
1030     } else {
1031         dpdk_queue_pkts(dev, qid, mbufs, newcnt);
1032         dpdk_queue_flush(dev, qid);
1033     }
1034
1035     if (!thread_is_pmd()) {
1036         ovs_mutex_unlock(&nonpmd_mempool_mutex);
1037     }
1038 }
1039
1040 static int
1041 netdev_dpdk_vhost_send(struct netdev *netdev, int qid OVS_UNUSED, struct dp_packet **pkts,
1042                  int cnt, bool may_steal)
1043 {
1044     if (OVS_UNLIKELY(pkts[0]->source != DPBUF_DPDK)) {
1045         int i;
1046
1047         dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1048         if (may_steal) {
1049             for (i = 0; i < cnt; i++) {
1050                 dp_packet_delete(pkts[i]);
1051             }
1052         }
1053     } else {
1054         __netdev_dpdk_vhost_send(netdev, pkts, cnt, may_steal);
1055     }
1056     return 0;
1057 }
1058
1059 static inline void
1060 netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
1061                    struct dp_packet **pkts, int cnt, bool may_steal)
1062 {
1063     int i;
1064
1065     if (OVS_UNLIKELY(!may_steal ||
1066                      pkts[0]->source != DPBUF_DPDK)) {
1067         struct netdev *netdev = &dev->up;
1068
1069         dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1070
1071         if (may_steal) {
1072             for (i = 0; i < cnt; i++) {
1073                 dp_packet_delete(pkts[i]);
1074             }
1075         }
1076     } else {
1077         int next_tx_idx = 0;
1078         int dropped = 0;
1079
1080         for (i = 0; i < cnt; i++) {
1081             int size = dp_packet_size(pkts[i]);
1082
1083             if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1084                 if (next_tx_idx != i) {
1085                     dpdk_queue_pkts(dev, qid,
1086                                     (struct rte_mbuf **)&pkts[next_tx_idx],
1087                                     i-next_tx_idx);
1088                 }
1089
1090                 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1091                              (int)size , dev->max_packet_len);
1092
1093                 dp_packet_delete(pkts[i]);
1094                 dropped++;
1095                 next_tx_idx = i + 1;
1096             }
1097         }
1098         if (next_tx_idx != cnt) {
1099            dpdk_queue_pkts(dev, qid,
1100                             (struct rte_mbuf **)&pkts[next_tx_idx],
1101                             cnt-next_tx_idx);
1102         }
1103
1104         if (OVS_UNLIKELY(dropped)) {
1105             ovs_mutex_lock(&dev->mutex);
1106             dev->stats.tx_dropped += dropped;
1107             ovs_mutex_unlock(&dev->mutex);
1108         }
1109     }
1110 }
1111
1112 static int
1113 netdev_dpdk_eth_send(struct netdev *netdev, int qid,
1114                      struct dp_packet **pkts, int cnt, bool may_steal)
1115 {
1116     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1117
1118     netdev_dpdk_send__(dev, qid, pkts, cnt, may_steal);
1119     return 0;
1120 }
1121
1122 static int
1123 netdev_dpdk_set_etheraddr(struct netdev *netdev,
1124                           const uint8_t mac[ETH_ADDR_LEN])
1125 {
1126     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1127
1128     ovs_mutex_lock(&dev->mutex);
1129     if (!eth_addr_equals(dev->hwaddr, mac)) {
1130         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
1131         netdev_change_seq_changed(netdev);
1132     }
1133     ovs_mutex_unlock(&dev->mutex);
1134
1135     return 0;
1136 }
1137
1138 static int
1139 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
1140                           uint8_t mac[ETH_ADDR_LEN])
1141 {
1142     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1143
1144     ovs_mutex_lock(&dev->mutex);
1145     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
1146     ovs_mutex_unlock(&dev->mutex);
1147
1148     return 0;
1149 }
1150
1151 static int
1152 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
1153 {
1154     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1155
1156     ovs_mutex_lock(&dev->mutex);
1157     *mtup = dev->mtu;
1158     ovs_mutex_unlock(&dev->mutex);
1159
1160     return 0;
1161 }
1162
1163 static int
1164 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
1165 {
1166     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1167     int old_mtu, err;
1168     struct dpdk_mp *old_mp;
1169     struct dpdk_mp *mp;
1170
1171     ovs_mutex_lock(&dpdk_mutex);
1172     ovs_mutex_lock(&dev->mutex);
1173     if (dev->mtu == mtu) {
1174         err = 0;
1175         goto out;
1176     }
1177
1178     mp = dpdk_mp_get(dev->socket_id, dev->mtu);
1179     if (!mp) {
1180         err = ENOMEM;
1181         goto out;
1182     }
1183
1184     rte_eth_dev_stop(dev->port_id);
1185
1186     old_mtu = dev->mtu;
1187     old_mp = dev->dpdk_mp;
1188     dev->dpdk_mp = mp;
1189     dev->mtu = mtu;
1190     dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
1191
1192     err = dpdk_eth_dev_init(dev);
1193     if (err) {
1194         dpdk_mp_put(mp);
1195         dev->mtu = old_mtu;
1196         dev->dpdk_mp = old_mp;
1197         dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
1198         dpdk_eth_dev_init(dev);
1199         goto out;
1200     }
1201
1202     dpdk_mp_put(old_mp);
1203     netdev_change_seq_changed(netdev);
1204 out:
1205     ovs_mutex_unlock(&dev->mutex);
1206     ovs_mutex_unlock(&dpdk_mutex);
1207     return err;
1208 }
1209
1210 static int
1211 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
1212
1213 static int
1214 netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
1215                             struct netdev_stats *stats)
1216 {
1217     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1218
1219     ovs_mutex_lock(&dev->mutex);
1220     memset(stats, 0, sizeof(*stats));
1221     /* Unsupported Stats */
1222     stats->rx_errors = UINT64_MAX;
1223     stats->tx_errors = UINT64_MAX;
1224     stats->multicast = UINT64_MAX;
1225     stats->collisions = UINT64_MAX;
1226     stats->rx_crc_errors = UINT64_MAX;
1227     stats->rx_fifo_errors = UINT64_MAX;
1228     stats->rx_frame_errors = UINT64_MAX;
1229     stats->rx_length_errors = UINT64_MAX;
1230     stats->rx_missed_errors = UINT64_MAX;
1231     stats->rx_over_errors = UINT64_MAX;
1232     stats->tx_aborted_errors = UINT64_MAX;
1233     stats->tx_carrier_errors = UINT64_MAX;
1234     stats->tx_errors = UINT64_MAX;
1235     stats->tx_fifo_errors = UINT64_MAX;
1236     stats->tx_heartbeat_errors = UINT64_MAX;
1237     stats->tx_window_errors = UINT64_MAX;
1238     stats->rx_bytes += UINT64_MAX;
1239     stats->rx_dropped += UINT64_MAX;
1240     stats->tx_bytes += UINT64_MAX;
1241
1242     /* Supported Stats */
1243     stats->rx_packets += dev->stats.rx_packets;
1244     stats->tx_packets += dev->stats.tx_packets;
1245     stats->tx_dropped += dev->stats.tx_dropped;
1246     ovs_mutex_unlock(&dev->mutex);
1247
1248     return 0;
1249 }
1250
1251 static int
1252 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1253 {
1254     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1255     struct rte_eth_stats rte_stats;
1256     bool gg;
1257
1258     netdev_dpdk_get_carrier(netdev, &gg);
1259     ovs_mutex_lock(&dev->mutex);
1260     rte_eth_stats_get(dev->port_id, &rte_stats);
1261
1262     memset(stats, 0, sizeof(*stats));
1263
1264     stats->rx_packets = rte_stats.ipackets;
1265     stats->tx_packets = rte_stats.opackets;
1266     stats->rx_bytes = rte_stats.ibytes;
1267     stats->tx_bytes = rte_stats.obytes;
1268     stats->rx_errors = rte_stats.ierrors;
1269     stats->tx_errors = rte_stats.oerrors;
1270     stats->multicast = rte_stats.imcasts;
1271
1272     stats->tx_dropped = dev->stats.tx_dropped;
1273     ovs_mutex_unlock(&dev->mutex);
1274
1275     return 0;
1276 }
1277
1278 static int
1279 netdev_dpdk_get_features(const struct netdev *netdev_,
1280                          enum netdev_features *current,
1281                          enum netdev_features *advertised OVS_UNUSED,
1282                          enum netdev_features *supported OVS_UNUSED,
1283                          enum netdev_features *peer OVS_UNUSED)
1284 {
1285     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1286     struct rte_eth_link link;
1287
1288     ovs_mutex_lock(&dev->mutex);
1289     link = dev->link;
1290     ovs_mutex_unlock(&dev->mutex);
1291
1292     if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1293         if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1294             *current = NETDEV_F_AUTONEG;
1295         }
1296     } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1297         if (link.link_speed == ETH_LINK_SPEED_10) {
1298             *current = NETDEV_F_10MB_HD;
1299         }
1300         if (link.link_speed == ETH_LINK_SPEED_100) {
1301             *current = NETDEV_F_100MB_HD;
1302         }
1303         if (link.link_speed == ETH_LINK_SPEED_1000) {
1304             *current = NETDEV_F_1GB_HD;
1305         }
1306     } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1307         if (link.link_speed == ETH_LINK_SPEED_10) {
1308             *current = NETDEV_F_10MB_FD;
1309         }
1310         if (link.link_speed == ETH_LINK_SPEED_100) {
1311             *current = NETDEV_F_100MB_FD;
1312         }
1313         if (link.link_speed == ETH_LINK_SPEED_1000) {
1314             *current = NETDEV_F_1GB_FD;
1315         }
1316         if (link.link_speed == ETH_LINK_SPEED_10000) {
1317             *current = NETDEV_F_10GB_FD;
1318         }
1319     }
1320
1321     return 0;
1322 }
1323
1324 static int
1325 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1326 {
1327     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1328     int ifindex;
1329
1330     ovs_mutex_lock(&dev->mutex);
1331     ifindex = dev->port_id;
1332     ovs_mutex_unlock(&dev->mutex);
1333
1334     return ifindex;
1335 }
1336
1337 static int
1338 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1339 {
1340     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1341
1342     ovs_mutex_lock(&dev->mutex);
1343     check_link_status(dev);
1344     *carrier = dev->link.link_status;
1345
1346     ovs_mutex_unlock(&dev->mutex);
1347
1348     return 0;
1349 }
1350
1351 static int
1352 netdev_dpdk_vhost_get_carrier(const struct netdev *netdev_, bool *carrier)
1353 {
1354     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1355     struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(dev);
1356
1357     ovs_mutex_lock(&dev->mutex);
1358
1359     if (is_vhost_running(virtio_dev)) {
1360         *carrier = 1;
1361     } else {
1362         *carrier = 0;
1363     }
1364
1365     ovs_mutex_unlock(&dev->mutex);
1366
1367     return 0;
1368 }
1369
1370 static long long int
1371 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1372 {
1373     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1374     long long int carrier_resets;
1375
1376     ovs_mutex_lock(&dev->mutex);
1377     carrier_resets = dev->link_reset_cnt;
1378     ovs_mutex_unlock(&dev->mutex);
1379
1380     return carrier_resets;
1381 }
1382
1383 static int
1384 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1385                        long long int interval OVS_UNUSED)
1386 {
1387     return EOPNOTSUPP;
1388 }
1389
1390 static int
1391 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1392                            enum netdev_flags off, enum netdev_flags on,
1393                            enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1394 {
1395     int err;
1396
1397     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1398         return EINVAL;
1399     }
1400
1401     *old_flagsp = dev->flags;
1402     dev->flags |= on;
1403     dev->flags &= ~off;
1404
1405     if (dev->flags == *old_flagsp) {
1406         return 0;
1407     }
1408
1409     if (dev->type == DPDK_DEV_ETH) {
1410         if (dev->flags & NETDEV_UP) {
1411             err = rte_eth_dev_start(dev->port_id);
1412             if (err)
1413                 return -err;
1414         }
1415
1416         if (dev->flags & NETDEV_PROMISC) {
1417             rte_eth_promiscuous_enable(dev->port_id);
1418         }
1419
1420         if (!(dev->flags & NETDEV_UP)) {
1421             rte_eth_dev_stop(dev->port_id);
1422         }
1423     }
1424
1425     return 0;
1426 }
1427
1428 static int
1429 netdev_dpdk_update_flags(struct netdev *netdev_,
1430                          enum netdev_flags off, enum netdev_flags on,
1431                          enum netdev_flags *old_flagsp)
1432 {
1433     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1434     int error;
1435
1436     ovs_mutex_lock(&netdev->mutex);
1437     error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1438     ovs_mutex_unlock(&netdev->mutex);
1439
1440     return error;
1441 }
1442
1443 static int
1444 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1445 {
1446     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1447     struct rte_eth_dev_info dev_info;
1448
1449     if (dev->port_id < 0)
1450         return ENODEV;
1451
1452     ovs_mutex_lock(&dev->mutex);
1453     rte_eth_dev_info_get(dev->port_id, &dev_info);
1454     ovs_mutex_unlock(&dev->mutex);
1455
1456     smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1457
1458     smap_add_format(args, "port_no", "%d", dev->port_id);
1459     smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1460     smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1461     smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1462     smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1463     smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1464     smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1465     smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1466     smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1467     smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1468     smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1469
1470     smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1471     smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1472
1473     return 0;
1474 }
1475
1476 static void
1477 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1478     OVS_REQUIRES(dev->mutex)
1479 {
1480     enum netdev_flags old_flags;
1481
1482     if (admin_state) {
1483         netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1484     } else {
1485         netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1486     }
1487 }
1488
1489 static void
1490 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1491                             const char *argv[], void *aux OVS_UNUSED)
1492 {
1493     bool up;
1494
1495     if (!strcasecmp(argv[argc - 1], "up")) {
1496         up = true;
1497     } else if ( !strcasecmp(argv[argc - 1], "down")) {
1498         up = false;
1499     } else {
1500         unixctl_command_reply_error(conn, "Invalid Admin State");
1501         return;
1502     }
1503
1504     if (argc > 2) {
1505         struct netdev *netdev = netdev_from_name(argv[1]);
1506         if (netdev && is_dpdk_class(netdev->netdev_class)) {
1507             struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1508
1509             ovs_mutex_lock(&dpdk_dev->mutex);
1510             netdev_dpdk_set_admin_state__(dpdk_dev, up);
1511             ovs_mutex_unlock(&dpdk_dev->mutex);
1512
1513             netdev_close(netdev);
1514         } else {
1515             unixctl_command_reply_error(conn, "Not a DPDK Interface");
1516             netdev_close(netdev);
1517             return;
1518         }
1519     } else {
1520         struct netdev_dpdk *netdev;
1521
1522         ovs_mutex_lock(&dpdk_mutex);
1523         LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1524             ovs_mutex_lock(&netdev->mutex);
1525             netdev_dpdk_set_admin_state__(netdev, up);
1526             ovs_mutex_unlock(&netdev->mutex);
1527         }
1528         ovs_mutex_unlock(&dpdk_mutex);
1529     }
1530     unixctl_command_reply(conn, "OK");
1531 }
1532
1533 /*
1534  * Set virtqueue flags so that we do not receive interrupts.
1535  */
1536 static void
1537 set_irq_status(struct virtio_net *dev)
1538 {
1539     dev->virtqueue[VIRTIO_RXQ]->used->flags = VRING_USED_F_NO_NOTIFY;
1540     dev->virtqueue[VIRTIO_TXQ]->used->flags = VRING_USED_F_NO_NOTIFY;
1541 }
1542
1543 /*
1544  * A new virtio-net device is added to a vhost port.
1545  */
1546 static int
1547 new_device(struct virtio_net *dev)
1548 {
1549     struct netdev_dpdk *netdev;
1550     bool exists = false;
1551
1552     ovs_mutex_lock(&dpdk_mutex);
1553     /* Add device to the vhost port with the same name as that passed down. */
1554     LIST_FOR_EACH(netdev, list_node, &dpdk_list) {
1555         if (strncmp(dev->ifname, netdev->up.name, IFNAMSIZ) == 0) {
1556             ovs_mutex_lock(&netdev->mutex);
1557             ovsrcu_set(&netdev->virtio_dev, dev);
1558             ovs_mutex_unlock(&netdev->mutex);
1559             exists = true;
1560             dev->flags |= VIRTIO_DEV_RUNNING;
1561             /* Disable notifications. */
1562             set_irq_status(dev);
1563             break;
1564         }
1565     }
1566     ovs_mutex_unlock(&dpdk_mutex);
1567
1568     if (!exists) {
1569         VLOG_INFO("vHost Device '%s' (%ld) can't be added - name not found",
1570                    dev->ifname, dev->device_fh);
1571
1572         return -1;
1573     }
1574
1575     VLOG_INFO("vHost Device '%s' (%ld) has been added",
1576                dev->ifname, dev->device_fh);
1577     return 0;
1578 }
1579
1580 /*
1581  * Remove a virtio-net device from the specific vhost port.  Use dev->remove
1582  * flag to stop any more packets from being sent or received to/from a VM and
1583  * ensure all currently queued packets have been sent/received before removing
1584  *  the device.
1585  */
1586 static void
1587 destroy_device(volatile struct virtio_net *dev)
1588 {
1589     struct netdev_dpdk *vhost_dev;
1590
1591     ovs_mutex_lock(&dpdk_mutex);
1592     LIST_FOR_EACH (vhost_dev, list_node, &dpdk_list) {
1593         if (netdev_dpdk_get_virtio(vhost_dev) == dev) {
1594
1595             ovs_mutex_lock(&vhost_dev->mutex);
1596             dev->flags &= ~VIRTIO_DEV_RUNNING;
1597             ovsrcu_set(&vhost_dev->virtio_dev, NULL);
1598             ovs_mutex_unlock(&vhost_dev->mutex);
1599
1600             /*
1601              * Wait for other threads to quiesce before
1602              * setting the virtio_dev to NULL.
1603              */
1604             ovsrcu_synchronize();
1605             /*
1606              * As call to ovsrcu_synchronize() will end the quiescent state,
1607              * put thread back into quiescent state before returning.
1608              */
1609             ovsrcu_quiesce_start();
1610         }
1611     }
1612     ovs_mutex_unlock(&dpdk_mutex);
1613
1614     VLOG_INFO("vHost Device '%s' (%ld) has been removed",
1615                dev->ifname, dev->device_fh);
1616 }
1617
1618 struct virtio_net *
1619 netdev_dpdk_get_virtio(const struct netdev_dpdk *dev)
1620 {
1621     return ovsrcu_get(struct virtio_net *, &dev->virtio_dev);
1622 }
1623
1624 /*
1625  * These callbacks allow virtio-net devices to be added to vhost ports when
1626  * configuration has been fully complete.
1627  */
1628 static const struct virtio_net_device_ops virtio_net_device_ops =
1629 {
1630     .new_device =  new_device,
1631     .destroy_device = destroy_device,
1632 };
1633
1634 static void *
1635 start_cuse_session_loop(void *dummy OVS_UNUSED)
1636 {
1637      pthread_detach(pthread_self());
1638      /* Put the cuse thread into quiescent state. */
1639      ovsrcu_quiesce_start();
1640      rte_vhost_driver_session_start();
1641      return NULL;
1642 }
1643
1644 static int
1645 dpdk_vhost_class_init(void)
1646 {
1647     int err = -1;
1648
1649     rte_vhost_driver_callback_register(&virtio_net_device_ops);
1650
1651     /* Register CUSE device to handle IOCTLs.
1652      * Unless otherwise specified on the vswitchd command line, cuse_dev_name
1653      * is set to vhost-net.
1654      */
1655     err = rte_vhost_driver_register(cuse_dev_name);
1656
1657     if (err != 0) {
1658         VLOG_ERR("CUSE device setup failure.");
1659         return -1;
1660     }
1661
1662     ovs_thread_create("cuse_thread", start_cuse_session_loop, NULL);
1663     return 0;
1664 }
1665
1666 static void
1667 dpdk_common_init(void)
1668 {
1669     unixctl_command_register("netdev-dpdk/set-admin-state",
1670                              "[netdev] up|down", 1, 2,
1671                              netdev_dpdk_set_admin_state, NULL);
1672
1673     ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1674 }
1675
1676 /* Client Rings */
1677
1678 static int
1679 dpdk_ring_create(const char dev_name[], unsigned int port_no,
1680                  unsigned int *eth_port_id)
1681 {
1682     struct dpdk_ring *ivshmem;
1683     char ring_name[10];
1684     int err;
1685
1686     ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1687     if (ivshmem == NULL) {
1688         return ENOMEM;
1689     }
1690
1691     /* XXX: Add support for multiquque ring. */
1692     err = snprintf(ring_name, 10, "%s_tx", dev_name);
1693     if (err < 0) {
1694         return -err;
1695     }
1696
1697     /* Create single consumer/producer rings, netdev does explicit locking. */
1698     ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
1699                                         RING_F_SP_ENQ | RING_F_SC_DEQ);
1700     if (ivshmem->cring_tx == NULL) {
1701         rte_free(ivshmem);
1702         return ENOMEM;
1703     }
1704
1705     err = snprintf(ring_name, 10, "%s_rx", dev_name);
1706     if (err < 0) {
1707         return -err;
1708     }
1709
1710     /* Create single consumer/producer rings, netdev does explicit locking. */
1711     ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
1712                                         RING_F_SP_ENQ | RING_F_SC_DEQ);
1713     if (ivshmem->cring_rx == NULL) {
1714         rte_free(ivshmem);
1715         return ENOMEM;
1716     }
1717
1718     err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
1719                              &ivshmem->cring_tx, 1, SOCKET0);
1720
1721     if (err < 0) {
1722         rte_free(ivshmem);
1723         return ENODEV;
1724     }
1725
1726     ivshmem->user_port_id = port_no;
1727     ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1728     list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1729
1730     *eth_port_id = ivshmem->eth_port_id;
1731     return 0;
1732 }
1733
1734 static int
1735 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1736 {
1737     struct dpdk_ring *ivshmem;
1738     unsigned int port_no;
1739     int err = 0;
1740
1741     /* Names always start with "dpdkr" */
1742     err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1743     if (err) {
1744         return err;
1745     }
1746
1747     /* look through our list to find the device */
1748     LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1749          if (ivshmem->user_port_id == port_no) {
1750             VLOG_INFO("Found dpdk ring device %s:", dev_name);
1751             *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1752             return 0;
1753          }
1754     }
1755     /* Need to create the device rings */
1756     return dpdk_ring_create(dev_name, port_no, eth_port_id);
1757 }
1758
1759 static int
1760 netdev_dpdk_ring_send(struct netdev *netdev, int qid OVS_UNUSED,
1761                       struct dp_packet **pkts, int cnt, bool may_steal)
1762 {
1763     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1764     unsigned i;
1765
1766     /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that the
1767      * rss hash field is clear. This is because the same mbuf may be modified by
1768      * the consumer of the ring and return into the datapath without recalculating
1769      * the RSS hash. */
1770     for (i = 0; i < cnt; i++) {
1771         dp_packet_set_rss_hash(pkts[i], 0);
1772     }
1773
1774     /* DPDK Rings have a single TX queue, Therefore needs locking. */
1775     rte_spinlock_lock(&dev->txq_lock);
1776     netdev_dpdk_send__(dev, 0, pkts, cnt, may_steal);
1777     rte_spinlock_unlock(&dev->txq_lock);
1778     return 0;
1779 }
1780
1781 static int
1782 netdev_dpdk_ring_construct(struct netdev *netdev)
1783 {
1784     unsigned int port_no = 0;
1785     int err = 0;
1786
1787     if (rte_eal_init_ret) {
1788         return rte_eal_init_ret;
1789     }
1790
1791     ovs_mutex_lock(&dpdk_mutex);
1792
1793     err = dpdk_ring_open(netdev->name, &port_no);
1794     if (err) {
1795         goto unlock_dpdk;
1796     }
1797
1798     err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
1799
1800 unlock_dpdk:
1801     ovs_mutex_unlock(&dpdk_mutex);
1802     return err;
1803 }
1804
1805 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, MULTIQ, SEND, \
1806     GET_CARRIER, GET_STATS, GET_FEATURES, GET_STATUS, RXQ_RECV)          \
1807 {                                                             \
1808     NAME,                                                     \
1809     INIT,                       /* init */                    \
1810     NULL,                       /* netdev_dpdk_run */         \
1811     NULL,                       /* netdev_dpdk_wait */        \
1812                                                               \
1813     netdev_dpdk_alloc,                                        \
1814     CONSTRUCT,                                                \
1815     DESTRUCT,                                                 \
1816     netdev_dpdk_dealloc,                                      \
1817     netdev_dpdk_get_config,                                   \
1818     NULL,                       /* netdev_dpdk_set_config */  \
1819     NULL,                       /* get_tunnel_config */       \
1820     NULL,                       /* build header */            \
1821     NULL,                       /* push header */             \
1822     NULL,                       /* pop header */              \
1823     netdev_dpdk_get_numa_id,    /* get_numa_id */             \
1824     MULTIQ,                     /* set_multiq */              \
1825                                                               \
1826     SEND,                       /* send */                    \
1827     NULL,                       /* send_wait */               \
1828                                                               \
1829     netdev_dpdk_set_etheraddr,                                \
1830     netdev_dpdk_get_etheraddr,                                \
1831     netdev_dpdk_get_mtu,                                      \
1832     netdev_dpdk_set_mtu,                                      \
1833     netdev_dpdk_get_ifindex,                                  \
1834     GET_CARRIER,                                              \
1835     netdev_dpdk_get_carrier_resets,                           \
1836     netdev_dpdk_set_miimon,                                   \
1837     GET_STATS,                                                \
1838     GET_FEATURES,                                             \
1839     NULL,                       /* set_advertisements */      \
1840                                                               \
1841     NULL,                       /* set_policing */            \
1842     NULL,                       /* get_qos_types */           \
1843     NULL,                       /* get_qos_capabilities */    \
1844     NULL,                       /* get_qos */                 \
1845     NULL,                       /* set_qos */                 \
1846     NULL,                       /* get_queue */               \
1847     NULL,                       /* set_queue */               \
1848     NULL,                       /* delete_queue */            \
1849     NULL,                       /* get_queue_stats */         \
1850     NULL,                       /* queue_dump_start */        \
1851     NULL,                       /* queue_dump_next */         \
1852     NULL,                       /* queue_dump_done */         \
1853     NULL,                       /* dump_queue_stats */        \
1854                                                               \
1855     NULL,                       /* get_in4 */                 \
1856     NULL,                       /* set_in4 */                 \
1857     NULL,                       /* get_in6 */                 \
1858     NULL,                       /* add_router */              \
1859     NULL,                       /* get_next_hop */            \
1860     GET_STATUS,                                               \
1861     NULL,                       /* arp_lookup */              \
1862                                                               \
1863     netdev_dpdk_update_flags,                                 \
1864                                                               \
1865     netdev_dpdk_rxq_alloc,                                    \
1866     netdev_dpdk_rxq_construct,                                \
1867     netdev_dpdk_rxq_destruct,                                 \
1868     netdev_dpdk_rxq_dealloc,                                  \
1869     RXQ_RECV,                                                 \
1870     NULL,                       /* rx_wait */                 \
1871     NULL,                       /* rxq_drain */               \
1872 }
1873
1874 int
1875 dpdk_init(int argc, char **argv)
1876 {
1877     int result;
1878     int base = 0;
1879     char *pragram_name = argv[0];
1880
1881     if (argc < 2 || strcmp(argv[1], "--dpdk"))
1882         return 0;
1883
1884     /* Remove the --dpdk argument from arg list.*/
1885     argc--;
1886     argv++;
1887
1888     /* If the cuse_dev_name parameter has been provided, set 'cuse_dev_name' to
1889      * this string if it meets the correct criteria. Otherwise, set it to the
1890      * default (vhost-net).
1891      */
1892     if (!strcmp(argv[1], "--cuse_dev_name") &&
1893         (strlen(argv[2]) <= NAME_MAX)) {
1894
1895         cuse_dev_name = strdup(argv[2]);
1896
1897         /* Remove the cuse_dev_name configuration parameters from the argument
1898          * list, so that the correct elements are passed to the DPDK
1899          * initialization function
1900          */
1901         argc -= 2;
1902         argv += 2;    /* Increment by two to bypass the cuse_dev_name arguments */
1903         base = 2;
1904
1905         VLOG_ERR("User-provided cuse_dev_name in use: /dev/%s", cuse_dev_name);
1906     } else {
1907         cuse_dev_name = "vhost-net";
1908         VLOG_INFO("No cuse_dev_name provided - defaulting to /dev/vhost-net");
1909     }
1910
1911     /* Keep the program name argument as this is needed for call to
1912      * rte_eal_init()
1913      */
1914     argv[0] = pragram_name;
1915
1916     /* Make sure things are initialized ... */
1917     result = rte_eal_init(argc, argv);
1918     if (result < 0) {
1919         ovs_abort(result, "Cannot init EAL");
1920     }
1921
1922     rte_memzone_dump(stdout);
1923     rte_eal_init_ret = 0;
1924
1925     if (argc > result) {
1926         argv[result] = argv[0];
1927     }
1928
1929     /* We are called from the main thread here */
1930     thread_set_nonpmd();
1931
1932     return result + 1 + base;
1933 }
1934
1935 static const struct netdev_class dpdk_class =
1936     NETDEV_DPDK_CLASS(
1937         "dpdk",
1938         NULL,
1939         netdev_dpdk_construct,
1940         netdev_dpdk_destruct,
1941         netdev_dpdk_set_multiq,
1942         netdev_dpdk_eth_send,
1943         netdev_dpdk_get_carrier,
1944         netdev_dpdk_get_stats,
1945         netdev_dpdk_get_features,
1946         netdev_dpdk_get_status,
1947         netdev_dpdk_rxq_recv);
1948
1949 static const struct netdev_class dpdk_ring_class =
1950     NETDEV_DPDK_CLASS(
1951         "dpdkr",
1952         NULL,
1953         netdev_dpdk_ring_construct,
1954         netdev_dpdk_destruct,
1955         NULL,
1956         netdev_dpdk_ring_send,
1957         netdev_dpdk_get_carrier,
1958         netdev_dpdk_get_stats,
1959         netdev_dpdk_get_features,
1960         netdev_dpdk_get_status,
1961         netdev_dpdk_rxq_recv);
1962
1963 static const struct netdev_class dpdk_vhost_class =
1964     NETDEV_DPDK_CLASS(
1965         "dpdkvhost",
1966         dpdk_vhost_class_init,
1967         netdev_dpdk_vhost_construct,
1968         netdev_dpdk_vhost_destruct,
1969         netdev_dpdk_vhost_set_multiq,
1970         netdev_dpdk_vhost_send,
1971         netdev_dpdk_vhost_get_carrier,
1972         netdev_dpdk_vhost_get_stats,
1973         NULL,
1974         NULL,
1975         netdev_dpdk_vhost_rxq_recv);
1976
1977 void
1978 netdev_dpdk_register(void)
1979 {
1980     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1981
1982     if (rte_eal_init_ret) {
1983         return;
1984     }
1985
1986     if (ovsthread_once_start(&once)) {
1987         dpdk_common_init();
1988         netdev_register_provider(&dpdk_class);
1989         netdev_register_provider(&dpdk_ring_class);
1990         netdev_register_provider(&dpdk_vhost_class);
1991         ovsthread_once_done(&once);
1992     }
1993 }
1994
1995 int
1996 pmd_thread_setaffinity_cpu(int cpu)
1997 {
1998     cpu_set_t cpuset;
1999     int err;
2000
2001     CPU_ZERO(&cpuset);
2002     CPU_SET(cpu, &cpuset);
2003     err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2004     if (err) {
2005         VLOG_ERR("Thread affinity error %d",err);
2006         return err;
2007     }
2008     /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
2009     ovs_assert(cpu != NON_PMD_CORE_ID);
2010     RTE_PER_LCORE(_lcore_id) = cpu;
2011
2012     return 0;
2013 }
2014
2015 void
2016 thread_set_nonpmd(void)
2017 {
2018     /* We have to use NON_PMD_CORE_ID to allow non-pmd threads to perform
2019      * certain DPDK operations, like rte_eth_dev_configure(). */
2020     RTE_PER_LCORE(_lcore_id) = NON_PMD_CORE_ID;
2021 }
2022
2023 static bool
2024 thread_is_pmd(void)
2025 {
2026     return rte_lcore_id() != NON_PMD_CORE_ID;
2027 }