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