netdev-dpdk: Keep calling rte_eth_tx_burst() until it returns 0
[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 "dpif-netdev.h"
32 #include "list.h"
33 #include "netdev-dpdk.h"
34 #include "netdev-provider.h"
35 #include "netdev-vport.h"
36 #include "odp-util.h"
37 #include "ofp-print.h"
38 #include "ofpbuf.h"
39 #include "ovs-thread.h"
40 #include "ovs-rcu.h"
41 #include "packet-dpif.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 "vlog.h"
49
50 VLOG_DEFINE_THIS_MODULE(dpdk);
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
52
53 #define DPDK_PORT_WATCHDOG_INTERVAL 5
54
55 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
56 #define OVS_VPORT_DPDK "ovs_dpdk"
57
58 /*
59  * need to reserve tons of extra space in the mbufs so we can align the
60  * DMA addresses to 4KB.
61  */
62
63 #define MTU_TO_MAX_LEN(mtu)  ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
64 #define MBUF_SIZE(mtu)       (MTU_TO_MAX_LEN(mtu) + (512) + \
65                              sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
66
67 /* XXX: mempool size should be based on system resources. */
68 #define NB_MBUF              (4096 * 64)
69 #define MP_CACHE_SZ          (256 * 2)
70 #define SOCKET0              0
71
72 #define NON_PMD_THREAD_TX_QUEUE 0
73
74 #define NIC_PORT_RX_Q_SIZE 2048  /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
75 #define NIC_PORT_TX_Q_SIZE 2048  /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
76
77 /* XXX: Needs per NIC value for these constants. */
78 #define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
79 #define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
80 #define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
81
82 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
83 #define TX_HTHRESH 0  /* Default values of TX host threshold reg. */
84 #define TX_WTHRESH 0  /* Default values of TX write-back threshold reg. */
85
86 static const struct rte_eth_conf port_conf = {
87     .rxmode = {
88         .mq_mode = ETH_MQ_RX_RSS,
89         .split_hdr_size = 0,
90         .header_split   = 0, /* Header Split disabled */
91         .hw_ip_checksum = 0, /* IP checksum offload disabled */
92         .hw_vlan_filter = 0, /* VLAN filtering disabled */
93         .jumbo_frame    = 0, /* Jumbo Frame Support disabled */
94         .hw_strip_crc   = 0,
95     },
96     .rx_adv_conf = {
97         .rss_conf = {
98             .rss_key = NULL,
99             .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6,
100         },
101     },
102     .txmode = {
103         .mq_mode = ETH_MQ_TX_NONE,
104     },
105 };
106
107 static const struct rte_eth_rxconf rx_conf = {
108     .rx_thresh = {
109         .pthresh = RX_PTHRESH,
110         .hthresh = RX_HTHRESH,
111         .wthresh = RX_WTHRESH,
112     },
113 };
114
115 static const struct rte_eth_txconf tx_conf = {
116     .tx_thresh = {
117         .pthresh = TX_PTHRESH,
118         .hthresh = TX_HTHRESH,
119         .wthresh = TX_WTHRESH,
120     },
121     .tx_free_thresh = 0,
122     .tx_rs_thresh = 0,
123     .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS|ETH_TXQ_FLAGS_NOOFFLOADS,
124 };
125
126 enum { MAX_RX_QUEUE_LEN = 192 };
127 enum { MAX_TX_QUEUE_LEN = 384 };
128 enum { DRAIN_TSC = 200000ULL };
129
130 static int rte_eal_init_ret = ENODEV;
131
132 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
133
134 /* Contains all 'struct dpdk_dev's. */
135 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
136     = LIST_INITIALIZER(&dpdk_list);
137
138 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
139     = LIST_INITIALIZER(&dpdk_mp_list);
140
141 /* This mutex must be used by non pmd threads when allocating or freeing
142  * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
143  * use mempools, a non pmd thread should hold this mutex while calling them */
144 struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
145
146 struct dpdk_mp {
147     struct rte_mempool *mp;
148     int mtu;
149     int socket_id;
150     int refcount;
151     struct list list_node OVS_GUARDED_BY(dpdk_mutex);
152 };
153
154 struct dpdk_tx_queue {
155     rte_spinlock_t tx_lock;
156     int count;
157     uint64_t tsc;
158     struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
159 };
160
161 /* dpdk has no way to remove dpdk ring ethernet devices
162    so we have to keep them around once they've been created
163 */
164
165 static struct list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
166     = LIST_INITIALIZER(&dpdk_ring_list);
167
168 struct dpdk_ring {
169     /* For the client rings */
170     struct rte_ring *cring_tx;
171     struct rte_ring *cring_rx;
172     int user_port_id; /* User given port no, parsed from port name */
173     int eth_port_id; /* ethernet device port id */
174     struct list list_node OVS_GUARDED_BY(dpdk_mutex);
175 };
176
177 struct netdev_dpdk {
178     struct netdev up;
179     int port_id;
180     int max_packet_len;
181
182     struct dpdk_tx_queue tx_q[NR_QUEUE];
183
184     struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
185
186     struct dpdk_mp *dpdk_mp;
187     int mtu;
188     int socket_id;
189     int buf_size;
190     struct netdev_stats stats_offset;
191     struct netdev_stats stats;
192
193     uint8_t hwaddr[ETH_ADDR_LEN];
194     enum netdev_flags flags;
195
196     struct rte_eth_link link;
197     int link_reset_cnt;
198
199     /* In dpdk_list. */
200     struct list list_node OVS_GUARDED_BY(dpdk_mutex);
201 };
202
203 struct netdev_rxq_dpdk {
204     struct netdev_rxq up;
205     int port_id;
206 };
207
208 static bool thread_is_pmd(void);
209
210 static int netdev_dpdk_construct(struct netdev *);
211
212 static bool
213 is_dpdk_class(const struct netdev_class *class)
214 {
215     return class->construct == netdev_dpdk_construct;
216 }
217
218 /* XXX: use dpdk malloc for entire OVS. infact huge page shld be used
219  * for all other sengments data, bss and text. */
220
221 static void *
222 dpdk_rte_mzalloc(size_t sz)
223 {
224     void *ptr;
225
226     ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
227     if (ptr == NULL) {
228         out_of_memory();
229     }
230     return ptr;
231 }
232
233 /* XXX this function should be called only by pmd threads (or by non pmd
234  * threads holding the nonpmd_mempool_mutex) */
235 void
236 free_dpdk_buf(struct dpif_packet *p)
237 {
238     struct rte_mbuf *pkt = (struct rte_mbuf *) p;
239
240     rte_pktmbuf_free_seg(pkt);
241 }
242
243 static void
244 __rte_pktmbuf_init(struct rte_mempool *mp,
245                    void *opaque_arg OVS_UNUSED,
246                    void *_m,
247                    unsigned i OVS_UNUSED)
248 {
249     struct rte_mbuf *m = _m;
250     uint32_t buf_len = mp->elt_size - sizeof(struct dpif_packet);
251
252     RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dpif_packet));
253
254     memset(m, 0, mp->elt_size);
255
256     /* start of buffer is just after mbuf structure */
257     m->buf_addr = (char *)m + sizeof(struct dpif_packet);
258     m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
259                     sizeof(struct dpif_packet);
260     m->buf_len = (uint16_t)buf_len;
261
262     /* keep some headroom between start of buffer and data */
263     m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
264
265     /* init some constant fields */
266     m->type = RTE_MBUF_PKT;
267     m->pool = mp;
268     m->pkt.nb_segs = 1;
269     m->pkt.in_port = 0xff;
270 }
271
272 static void
273 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
274                      void *opaque_arg OVS_UNUSED,
275                      void *_m,
276                      unsigned i OVS_UNUSED)
277 {
278     struct rte_mbuf *m = _m;
279
280     __rte_pktmbuf_init(mp, opaque_arg, _m, i);
281
282     ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
283 }
284
285 static struct dpdk_mp *
286 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
287 {
288     struct dpdk_mp *dmp = NULL;
289     char mp_name[RTE_MEMPOOL_NAMESIZE];
290
291     LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
292         if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
293             dmp->refcount++;
294             return dmp;
295         }
296     }
297
298     dmp = dpdk_rte_mzalloc(sizeof *dmp);
299     dmp->socket_id = socket_id;
300     dmp->mtu = mtu;
301     dmp->refcount = 1;
302
303     if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d", dmp->mtu) < 0) {
304         return NULL;
305     }
306
307     dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
308                                  MP_CACHE_SZ,
309                                  sizeof(struct rte_pktmbuf_pool_private),
310                                  rte_pktmbuf_pool_init, NULL,
311                                  ovs_rte_pktmbuf_init, NULL,
312                                  socket_id, 0);
313
314     if (dmp->mp == NULL) {
315         return NULL;
316     }
317
318     list_push_back(&dpdk_mp_list, &dmp->list_node);
319     return dmp;
320 }
321
322 static void
323 dpdk_mp_put(struct dpdk_mp *dmp)
324 {
325
326     if (!dmp) {
327         return;
328     }
329
330     dmp->refcount--;
331     ovs_assert(dmp->refcount >= 0);
332
333 #if 0
334     /* I could not find any API to destroy mp. */
335     if (dmp->refcount == 0) {
336         list_delete(dmp->list_node);
337         /* destroy mp-pool. */
338     }
339 #endif
340 }
341
342 static void
343 check_link_status(struct netdev_dpdk *dev)
344 {
345     struct rte_eth_link link;
346
347     rte_eth_link_get_nowait(dev->port_id, &link);
348
349     if (dev->link.link_status != link.link_status) {
350         netdev_change_seq_changed(&dev->up);
351
352         dev->link_reset_cnt++;
353         dev->link = link;
354         if (dev->link.link_status) {
355             VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
356                         dev->port_id, (unsigned)dev->link.link_speed,
357                         (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
358                          ("full-duplex") : ("half-duplex"));
359         } else {
360             VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
361         }
362     }
363 }
364
365 static void *
366 dpdk_watchdog(void *dummy OVS_UNUSED)
367 {
368     struct netdev_dpdk *dev;
369
370     pthread_detach(pthread_self());
371
372     for (;;) {
373         ovs_mutex_lock(&dpdk_mutex);
374         LIST_FOR_EACH (dev, list_node, &dpdk_list) {
375             ovs_mutex_lock(&dev->mutex);
376             check_link_status(dev);
377             ovs_mutex_unlock(&dev->mutex);
378         }
379         ovs_mutex_unlock(&dpdk_mutex);
380         xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
381     }
382
383     return NULL;
384 }
385
386 static int
387 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
388 {
389     struct rte_pktmbuf_pool_private *mbp_priv;
390     struct ether_addr eth_addr;
391     int diag;
392     int i;
393
394     if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
395         return ENODEV;
396     }
397
398     diag = rte_eth_dev_configure(dev->port_id, NR_QUEUE, NR_QUEUE,  &port_conf);
399     if (diag) {
400         VLOG_ERR("eth dev config error %d",diag);
401         return -diag;
402     }
403
404     for (i = 0; i < NR_QUEUE; i++) {
405         diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
406                                       dev->socket_id, &tx_conf);
407         if (diag) {
408             VLOG_ERR("eth dev tx queue setup error %d",diag);
409             return -diag;
410         }
411     }
412
413     for (i = 0; i < NR_QUEUE; i++) {
414         diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
415                                       dev->socket_id,
416                                       &rx_conf, dev->dpdk_mp->mp);
417         if (diag) {
418             VLOG_ERR("eth dev rx queue setup error %d",diag);
419             return -diag;
420         }
421     }
422
423     diag = rte_eth_dev_start(dev->port_id);
424     if (diag) {
425         VLOG_ERR("eth dev start error %d",diag);
426         return -diag;
427     }
428
429     rte_eth_promiscuous_enable(dev->port_id);
430     rte_eth_allmulticast_enable(dev->port_id);
431
432     memset(&eth_addr, 0x0, sizeof(eth_addr));
433     rte_eth_macaddr_get(dev->port_id, &eth_addr);
434     VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
435                     dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
436
437     memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
438     rte_eth_link_get_nowait(dev->port_id, &dev->link);
439
440     mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
441     dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
442
443     dev->flags = NETDEV_UP | NETDEV_PROMISC;
444     return 0;
445 }
446
447 static struct netdev_dpdk *
448 netdev_dpdk_cast(const struct netdev *netdev)
449 {
450     return CONTAINER_OF(netdev, struct netdev_dpdk, up);
451 }
452
453 static struct netdev *
454 netdev_dpdk_alloc(void)
455 {
456     struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
457     return &netdev->up;
458 }
459
460 static int
461 netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no) OVS_REQUIRES(dpdk_mutex)
462 {
463     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
464     int err = 0;
465     int i;
466
467     ovs_mutex_init(&netdev->mutex);
468
469     ovs_mutex_lock(&netdev->mutex);
470
471     for (i = 0; i < NR_QUEUE; i++) {
472         rte_spinlock_init(&netdev->tx_q[i].tx_lock);
473     }
474
475     netdev->port_id = port_no;
476
477     netdev->flags = 0;
478     netdev->mtu = ETHER_MTU;
479     netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
480
481     /* XXX: need to discover device node at run time. */
482     netdev->socket_id = SOCKET0;
483
484     netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
485     if (!netdev->dpdk_mp) {
486         err = ENOMEM;
487         goto unlock;
488     }
489
490     err = dpdk_eth_dev_init(netdev);
491     if (err) {
492         goto unlock;
493     }
494     netdev_->n_rxq = NR_QUEUE;
495
496     list_push_back(&dpdk_list, &netdev->list_node);
497
498 unlock:
499     ovs_mutex_unlock(&netdev->mutex);
500     return err;
501 }
502
503 static int
504 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
505                     unsigned int *port_no)
506 {
507     const char *cport;
508
509     if (strncmp(dev_name, prefix, strlen(prefix))) {
510         return ENODEV;
511     }
512
513     cport = dev_name + strlen(prefix);
514     *port_no = strtol(cport, 0, 0); /* string must be null terminated */
515     return 0;
516 }
517
518 static int
519 netdev_dpdk_construct(struct netdev *netdev)
520 {
521     unsigned int port_no;
522     int err;
523
524     if (rte_eal_init_ret) {
525         return rte_eal_init_ret;
526     }
527
528     /* Names always start with "dpdk" */
529     err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
530     if (err) {
531         return err;
532     }
533
534     ovs_mutex_lock(&dpdk_mutex);
535     err = netdev_dpdk_init(netdev, port_no);
536     ovs_mutex_unlock(&dpdk_mutex);
537     return err;
538 }
539
540 static void
541 netdev_dpdk_destruct(struct netdev *netdev_)
542 {
543     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
544
545     ovs_mutex_lock(&dev->mutex);
546     rte_eth_dev_stop(dev->port_id);
547     ovs_mutex_unlock(&dev->mutex);
548
549     ovs_mutex_lock(&dpdk_mutex);
550     list_remove(&dev->list_node);
551     dpdk_mp_put(dev->dpdk_mp);
552     ovs_mutex_unlock(&dpdk_mutex);
553
554     ovs_mutex_destroy(&dev->mutex);
555 }
556
557 static void
558 netdev_dpdk_dealloc(struct netdev *netdev_)
559 {
560     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
561
562     rte_free(netdev);
563 }
564
565 static int
566 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
567 {
568     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
569
570     ovs_mutex_lock(&dev->mutex);
571
572     /* XXX: Allow to configure number of queues. */
573     smap_add_format(args, "configured_rx_queues", "%u", netdev_->n_rxq);
574     smap_add_format(args, "configured_tx_queues", "%u", netdev_->n_rxq);
575     ovs_mutex_unlock(&dev->mutex);
576
577     return 0;
578 }
579
580 static struct netdev_rxq *
581 netdev_dpdk_rxq_alloc(void)
582 {
583     struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
584
585     return &rx->up;
586 }
587
588 static struct netdev_rxq_dpdk *
589 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
590 {
591     return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
592 }
593
594 static int
595 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
596 {
597     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
598     struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
599
600     ovs_mutex_lock(&netdev->mutex);
601     rx->port_id = netdev->port_id;
602     ovs_mutex_unlock(&netdev->mutex);
603
604     return 0;
605 }
606
607 static void
608 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
609 {
610 }
611
612 static void
613 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
614 {
615     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
616
617     rte_free(rx);
618 }
619
620 static inline void
621 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
622 {
623     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
624     uint32_t nb_tx = 0;
625
626     while (nb_tx != txq->count) {
627         uint32_t ret;
628
629         ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
630                                txq->count - nb_tx);
631         if (!ret) {
632             break;
633         }
634
635         nb_tx += ret;
636     }
637
638     if (OVS_UNLIKELY(nb_tx != txq->count)) {
639         /* free buffers, which we couldn't transmit, one at a time (each
640          * packet could come from a different mempool) */
641         int i;
642
643         for (i = nb_tx; i < txq->count; i++) {
644             rte_pktmbuf_free_seg(txq->burst_pkts[i]);
645         }
646         ovs_mutex_lock(&dev->mutex);
647         dev->stats.tx_dropped += txq->count-nb_tx;
648         ovs_mutex_unlock(&dev->mutex);
649     }
650
651     txq->count = 0;
652     txq->tsc = rte_get_timer_cycles();
653 }
654
655 static inline void
656 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
657 {
658     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
659
660     if (txq->count == 0) {
661         return;
662     }
663     rte_spinlock_lock(&txq->tx_lock);
664     dpdk_queue_flush__(dev, qid);
665     rte_spinlock_unlock(&txq->tx_lock);
666 }
667
668 static int
669 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
670                      int *c)
671 {
672     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
673     struct netdev *netdev = rx->up.netdev;
674     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
675     int nb_rx;
676
677     dpdk_queue_flush(dev, rxq_->queue_id);
678
679     nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
680                              (struct rte_mbuf **) packets,
681                              MIN((int)NETDEV_MAX_RX_BATCH,
682                                  (int)MAX_RX_QUEUE_LEN));
683     if (!nb_rx) {
684         return EAGAIN;
685     }
686
687     *c = nb_rx;
688
689     return 0;
690 }
691
692 inline static void
693 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
694                struct rte_mbuf **pkts, int cnt)
695 {
696     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
697     uint64_t diff_tsc;
698
699     int i = 0;
700
701     rte_spinlock_lock(&txq->tx_lock);
702     while (i < cnt) {
703         int freeslots = MAX_TX_QUEUE_LEN - txq->count;
704         int tocopy = MIN(freeslots, cnt-i);
705
706         memcpy(&txq->burst_pkts[txq->count], &pkts[i],
707                tocopy * sizeof (struct rte_mbuf *));
708
709         txq->count += tocopy;
710         i += tocopy;
711
712         if (txq->count == MAX_TX_QUEUE_LEN) {
713             dpdk_queue_flush__(dev, qid);
714         }
715         diff_tsc = rte_get_timer_cycles() - txq->tsc;
716         if (diff_tsc >= DRAIN_TSC) {
717             dpdk_queue_flush__(dev, qid);
718         }
719     }
720     rte_spinlock_unlock(&txq->tx_lock);
721 }
722
723 /* Tx function. Transmit packets indefinitely */
724 static void
725 dpdk_do_tx_copy(struct netdev *netdev, struct dpif_packet ** pkts, int cnt)
726     OVS_NO_THREAD_SAFETY_ANALYSIS
727 {
728     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
729     struct rte_mbuf *mbufs[cnt];
730     int dropped = 0;
731     int newcnt = 0;
732     int i;
733
734     /* If we are on a non pmd thread we have to use the mempool mutex, because
735      * every non pmd thread shares the same mempool cache */
736
737     if (!thread_is_pmd()) {
738         ovs_mutex_lock(&nonpmd_mempool_mutex);
739     }
740
741     for (i = 0; i < cnt; i++) {
742         int size = ofpbuf_size(&pkts[i]->ofpbuf);
743
744         if (OVS_UNLIKELY(size > dev->max_packet_len)) {
745             VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
746                          (int)size , dev->max_packet_len);
747
748             dropped++;
749             continue;
750         }
751
752         mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
753
754         if (!mbufs[newcnt]) {
755             dropped += cnt - i;
756             break;
757         }
758
759         /* We have to do a copy for now */
760         memcpy(mbufs[newcnt]->pkt.data, ofpbuf_data(&pkts[i]->ofpbuf), size);
761
762         rte_pktmbuf_data_len(mbufs[newcnt]) = size;
763         rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
764
765         newcnt++;
766     }
767
768     if (OVS_UNLIKELY(dropped)) {
769         ovs_mutex_lock(&dev->mutex);
770         dev->stats.tx_dropped += dropped;
771         ovs_mutex_unlock(&dev->mutex);
772     }
773
774     dpdk_queue_pkts(dev, NON_PMD_THREAD_TX_QUEUE, mbufs, newcnt);
775     dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
776
777     if (!thread_is_pmd()) {
778         ovs_mutex_unlock(&nonpmd_mempool_mutex);
779     }
780 }
781
782 static int
783 netdev_dpdk_send(struct netdev *netdev, struct dpif_packet **pkts, int cnt,
784                  bool may_steal)
785 {
786     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
787     int ret;
788     int i;
789
790     if (!may_steal || pkts[0]->ofpbuf.source != OFPBUF_DPDK) {
791         dpdk_do_tx_copy(netdev, pkts, cnt);
792
793         if (may_steal) {
794             for (i = 0; i < cnt; i++) {
795                 dpif_packet_delete(pkts[i]);
796             }
797         }
798     } else {
799         int qid;
800         int next_tx_idx = 0;
801         int dropped = 0;
802
803         qid = rte_lcore_id() % NR_QUEUE;
804
805         for (i = 0; i < cnt; i++) {
806             int size = ofpbuf_size(&pkts[i]->ofpbuf);
807             if (OVS_UNLIKELY(size > dev->max_packet_len)) {
808                 if (next_tx_idx != i) {
809                     dpdk_queue_pkts(dev, qid,
810                                     (struct rte_mbuf **)&pkts[next_tx_idx],
811                                     i-next_tx_idx);
812                 }
813
814                 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
815                              (int)size , dev->max_packet_len);
816
817                 dpif_packet_delete(pkts[i]);
818                 dropped++;
819                 next_tx_idx = i + 1;
820             }
821         }
822         if (next_tx_idx != cnt) {
823            dpdk_queue_pkts(dev, qid,
824                             (struct rte_mbuf **)&pkts[next_tx_idx],
825                             cnt-next_tx_idx);
826         }
827
828         if (OVS_UNLIKELY(dropped)) {
829             ovs_mutex_lock(&dev->mutex);
830             dev->stats.tx_dropped += dropped;
831             ovs_mutex_unlock(&dev->mutex);
832         }
833     }
834     ret = 0;
835
836     return ret;
837 }
838
839 static int
840 netdev_dpdk_set_etheraddr(struct netdev *netdev,
841                           const uint8_t mac[ETH_ADDR_LEN])
842 {
843     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
844
845     ovs_mutex_lock(&dev->mutex);
846     if (!eth_addr_equals(dev->hwaddr, mac)) {
847         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
848         netdev_change_seq_changed(netdev);
849     }
850     ovs_mutex_unlock(&dev->mutex);
851
852     return 0;
853 }
854
855 static int
856 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
857                           uint8_t mac[ETH_ADDR_LEN])
858 {
859     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
860
861     ovs_mutex_lock(&dev->mutex);
862     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
863     ovs_mutex_unlock(&dev->mutex);
864
865     return 0;
866 }
867
868 static int
869 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
870 {
871     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
872
873     ovs_mutex_lock(&dev->mutex);
874     *mtup = dev->mtu;
875     ovs_mutex_unlock(&dev->mutex);
876
877     return 0;
878 }
879
880 static int
881 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
882 {
883     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
884     int old_mtu, err;
885     struct dpdk_mp *old_mp;
886     struct dpdk_mp *mp;
887
888     ovs_mutex_lock(&dpdk_mutex);
889     ovs_mutex_lock(&dev->mutex);
890     if (dev->mtu == mtu) {
891         err = 0;
892         goto out;
893     }
894
895     mp = dpdk_mp_get(dev->socket_id, dev->mtu);
896     if (!mp) {
897         err = ENOMEM;
898         goto out;
899     }
900
901     rte_eth_dev_stop(dev->port_id);
902
903     old_mtu = dev->mtu;
904     old_mp = dev->dpdk_mp;
905     dev->dpdk_mp = mp;
906     dev->mtu = mtu;
907     dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
908
909     err = dpdk_eth_dev_init(dev);
910     if (err) {
911         dpdk_mp_put(mp);
912         dev->mtu = old_mtu;
913         dev->dpdk_mp = old_mp;
914         dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
915         dpdk_eth_dev_init(dev);
916         goto out;
917     }
918
919     dpdk_mp_put(old_mp);
920     netdev_change_seq_changed(netdev);
921 out:
922     ovs_mutex_unlock(&dev->mutex);
923     ovs_mutex_unlock(&dpdk_mutex);
924     return err;
925 }
926
927 static int
928 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
929
930 static int
931 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
932 {
933     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
934     struct rte_eth_stats rte_stats;
935     bool gg;
936
937     netdev_dpdk_get_carrier(netdev, &gg);
938     ovs_mutex_lock(&dev->mutex);
939     rte_eth_stats_get(dev->port_id, &rte_stats);
940
941     *stats = dev->stats_offset;
942
943     stats->rx_packets += rte_stats.ipackets;
944     stats->tx_packets += rte_stats.opackets;
945     stats->rx_bytes += rte_stats.ibytes;
946     stats->tx_bytes += rte_stats.obytes;
947     stats->rx_errors += rte_stats.ierrors;
948     stats->tx_errors += rte_stats.oerrors;
949     stats->multicast += rte_stats.imcasts;
950
951     stats->tx_dropped += dev->stats.tx_dropped;
952     ovs_mutex_unlock(&dev->mutex);
953
954     return 0;
955 }
956
957 static int
958 netdev_dpdk_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
959 {
960     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
961
962     ovs_mutex_lock(&dev->mutex);
963     dev->stats_offset = *stats;
964     ovs_mutex_unlock(&dev->mutex);
965
966     return 0;
967 }
968
969 static int
970 netdev_dpdk_get_features(const struct netdev *netdev_,
971                          enum netdev_features *current,
972                          enum netdev_features *advertised OVS_UNUSED,
973                          enum netdev_features *supported OVS_UNUSED,
974                          enum netdev_features *peer OVS_UNUSED)
975 {
976     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
977     struct rte_eth_link link;
978
979     ovs_mutex_lock(&dev->mutex);
980     link = dev->link;
981     ovs_mutex_unlock(&dev->mutex);
982
983     if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
984         if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
985             *current = NETDEV_F_AUTONEG;
986         }
987     } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
988         if (link.link_speed == ETH_LINK_SPEED_10) {
989             *current = NETDEV_F_10MB_HD;
990         }
991         if (link.link_speed == ETH_LINK_SPEED_100) {
992             *current = NETDEV_F_100MB_HD;
993         }
994         if (link.link_speed == ETH_LINK_SPEED_1000) {
995             *current = NETDEV_F_1GB_HD;
996         }
997     } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
998         if (link.link_speed == ETH_LINK_SPEED_10) {
999             *current = NETDEV_F_10MB_FD;
1000         }
1001         if (link.link_speed == ETH_LINK_SPEED_100) {
1002             *current = NETDEV_F_100MB_FD;
1003         }
1004         if (link.link_speed == ETH_LINK_SPEED_1000) {
1005             *current = NETDEV_F_1GB_FD;
1006         }
1007         if (link.link_speed == ETH_LINK_SPEED_10000) {
1008             *current = NETDEV_F_10GB_FD;
1009         }
1010     }
1011
1012     return 0;
1013 }
1014
1015 static int
1016 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1017 {
1018     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1019     int ifindex;
1020
1021     ovs_mutex_lock(&dev->mutex);
1022     ifindex = dev->port_id;
1023     ovs_mutex_unlock(&dev->mutex);
1024
1025     return ifindex;
1026 }
1027
1028 static int
1029 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1030 {
1031     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1032
1033     ovs_mutex_lock(&dev->mutex);
1034     check_link_status(dev);
1035     *carrier = dev->link.link_status;
1036     ovs_mutex_unlock(&dev->mutex);
1037
1038     return 0;
1039 }
1040
1041 static long long int
1042 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1043 {
1044     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1045     long long int carrier_resets;
1046
1047     ovs_mutex_lock(&dev->mutex);
1048     carrier_resets = dev->link_reset_cnt;
1049     ovs_mutex_unlock(&dev->mutex);
1050
1051     return carrier_resets;
1052 }
1053
1054 static int
1055 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1056                        long long int interval OVS_UNUSED)
1057 {
1058     return 0;
1059 }
1060
1061 static int
1062 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1063                            enum netdev_flags off, enum netdev_flags on,
1064                            enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1065 {
1066     int err;
1067
1068     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1069         return EINVAL;
1070     }
1071
1072     *old_flagsp = dev->flags;
1073     dev->flags |= on;
1074     dev->flags &= ~off;
1075
1076     if (dev->flags == *old_flagsp) {
1077         return 0;
1078     }
1079
1080     if (dev->flags & NETDEV_UP) {
1081         err = rte_eth_dev_start(dev->port_id);
1082         if (err)
1083             return -err;
1084     }
1085
1086     if (dev->flags & NETDEV_PROMISC) {
1087         rte_eth_promiscuous_enable(dev->port_id);
1088     }
1089
1090     if (!(dev->flags & NETDEV_UP)) {
1091         rte_eth_dev_stop(dev->port_id);
1092     }
1093
1094     return 0;
1095 }
1096
1097 static int
1098 netdev_dpdk_update_flags(struct netdev *netdev_,
1099                          enum netdev_flags off, enum netdev_flags on,
1100                          enum netdev_flags *old_flagsp)
1101 {
1102     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1103     int error;
1104
1105     ovs_mutex_lock(&netdev->mutex);
1106     error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1107     ovs_mutex_unlock(&netdev->mutex);
1108
1109     return error;
1110 }
1111
1112 static int
1113 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1114 {
1115     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1116     struct rte_eth_dev_info dev_info;
1117
1118     if (dev->port_id <= 0)
1119         return ENODEV;
1120
1121     ovs_mutex_lock(&dev->mutex);
1122     rte_eth_dev_info_get(dev->port_id, &dev_info);
1123     ovs_mutex_unlock(&dev->mutex);
1124
1125     smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1126
1127     smap_add_format(args, "port_no", "%d", dev->port_id);
1128     smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1129     smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1130     smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1131     smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1132     smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1133     smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1134     smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1135     smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1136     smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1137     smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1138
1139     smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1140     smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1141
1142     return 0;
1143 }
1144
1145 static void
1146 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1147     OVS_REQUIRES(dev->mutex)
1148 {
1149     enum netdev_flags old_flags;
1150
1151     if (admin_state) {
1152         netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1153     } else {
1154         netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1155     }
1156 }
1157
1158 static void
1159 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1160                             const char *argv[], void *aux OVS_UNUSED)
1161 {
1162     bool up;
1163
1164     if (!strcasecmp(argv[argc - 1], "up")) {
1165         up = true;
1166     } else if ( !strcasecmp(argv[argc - 1], "down")) {
1167         up = false;
1168     } else {
1169         unixctl_command_reply_error(conn, "Invalid Admin State");
1170         return;
1171     }
1172
1173     if (argc > 2) {
1174         struct netdev *netdev = netdev_from_name(argv[1]);
1175         if (netdev && is_dpdk_class(netdev->netdev_class)) {
1176             struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1177
1178             ovs_mutex_lock(&dpdk_dev->mutex);
1179             netdev_dpdk_set_admin_state__(dpdk_dev, up);
1180             ovs_mutex_unlock(&dpdk_dev->mutex);
1181
1182             netdev_close(netdev);
1183         } else {
1184             unixctl_command_reply_error(conn, "Not a DPDK Interface");
1185             netdev_close(netdev);
1186             return;
1187         }
1188     } else {
1189         struct netdev_dpdk *netdev;
1190
1191         ovs_mutex_lock(&dpdk_mutex);
1192         LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1193             ovs_mutex_lock(&netdev->mutex);
1194             netdev_dpdk_set_admin_state__(netdev, up);
1195             ovs_mutex_unlock(&netdev->mutex);
1196         }
1197         ovs_mutex_unlock(&dpdk_mutex);
1198     }
1199     unixctl_command_reply(conn, "OK");
1200 }
1201
1202 static void
1203 dpdk_common_init(void)
1204 {
1205     unixctl_command_register("netdev-dpdk/set-admin-state",
1206                              "[netdev] up|down", 1, 2,
1207                              netdev_dpdk_set_admin_state, NULL);
1208
1209     ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1210 }
1211
1212 static int
1213 dpdk_class_init(void)
1214 {
1215     int result;
1216
1217     result = rte_eal_pci_probe();
1218     if (result) {
1219         VLOG_ERR("Cannot probe PCI");
1220         return -result;
1221     }
1222
1223     VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1224
1225     return 0;
1226 }
1227
1228 /* Client Rings */
1229
1230 static int
1231 dpdk_ring_create(const char dev_name[], unsigned int port_no,
1232                  unsigned int *eth_port_id)
1233 {
1234     struct dpdk_ring *ivshmem;
1235     char ring_name[10];
1236     int err;
1237
1238     ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1239     if (ivshmem == NULL) {
1240         return ENOMEM;
1241     }
1242
1243     err = snprintf(ring_name, 10, "%s_tx", dev_name);
1244     if (err < 0) {
1245         return -err;
1246     }
1247
1248     ivshmem->cring_tx = rte_ring_create(ring_name, MAX_RX_QUEUE_LEN, SOCKET0, 0);
1249     if (ivshmem->cring_tx == NULL) {
1250         rte_free(ivshmem);
1251         return ENOMEM;
1252     }
1253
1254     err = snprintf(ring_name, 10, "%s_rx", dev_name);
1255     if (err < 0) {
1256         return -err;
1257     }
1258
1259     ivshmem->cring_rx = rte_ring_create(ring_name, MAX_RX_QUEUE_LEN, SOCKET0, 0);
1260     if (ivshmem->cring_rx == NULL) {
1261         rte_free(ivshmem);
1262         return ENOMEM;
1263     }
1264
1265     err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
1266                              &ivshmem->cring_tx, 1, SOCKET0);
1267
1268     if (err < 0) {
1269         rte_free(ivshmem);
1270         return ENODEV;
1271     }
1272
1273     ivshmem->user_port_id = port_no;
1274     ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1275     list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1276
1277     *eth_port_id = ivshmem->eth_port_id;
1278     return 0;
1279 }
1280
1281 static int
1282 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1283 {
1284     struct dpdk_ring *ivshmem;
1285     unsigned int port_no;
1286     int err = 0;
1287
1288     /* Names always start with "dpdkr" */
1289     err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1290     if (err) {
1291         return err;
1292     }
1293
1294     /* look through our list to find the device */
1295     LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1296          if (ivshmem->user_port_id == port_no) {
1297             VLOG_INFO("Found dpdk ring device %s:\n", dev_name);
1298             *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1299             return 0;
1300          }
1301     }
1302     /* Need to create the device rings */
1303     return dpdk_ring_create(dev_name, port_no, eth_port_id);
1304 }
1305
1306 static int
1307 netdev_dpdk_ring_construct(struct netdev *netdev)
1308 {
1309     unsigned int port_no = 0;
1310     int err = 0;
1311
1312     if (rte_eal_init_ret) {
1313         return rte_eal_init_ret;
1314     }
1315
1316     ovs_mutex_lock(&dpdk_mutex);
1317
1318     err = dpdk_ring_open(netdev->name, &port_no);
1319     if (err) {
1320         goto unlock_dpdk;
1321     }
1322
1323     err = netdev_dpdk_init(netdev, port_no);
1324
1325 unlock_dpdk:
1326     ovs_mutex_unlock(&dpdk_mutex);
1327     return err;
1328 }
1329
1330 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT)              \
1331 {                                                             \
1332     NAME,                                                     \
1333     INIT,                       /* init */                    \
1334     NULL,                       /* netdev_dpdk_run */         \
1335     NULL,                       /* netdev_dpdk_wait */        \
1336                                                               \
1337     netdev_dpdk_alloc,                                        \
1338     CONSTRUCT,                                                \
1339     netdev_dpdk_destruct,                                     \
1340     netdev_dpdk_dealloc,                                      \
1341     netdev_dpdk_get_config,                                   \
1342     NULL,                       /* netdev_dpdk_set_config */  \
1343     NULL,                       /* get_tunnel_config */       \
1344                                                               \
1345     netdev_dpdk_send,           /* send */                    \
1346     NULL,                       /* send_wait */               \
1347                                                               \
1348     netdev_dpdk_set_etheraddr,                                \
1349     netdev_dpdk_get_etheraddr,                                \
1350     netdev_dpdk_get_mtu,                                      \
1351     netdev_dpdk_set_mtu,                                      \
1352     netdev_dpdk_get_ifindex,                                  \
1353     netdev_dpdk_get_carrier,                                  \
1354     netdev_dpdk_get_carrier_resets,                           \
1355     netdev_dpdk_set_miimon,                                   \
1356     netdev_dpdk_get_stats,                                    \
1357     netdev_dpdk_set_stats,                                    \
1358     netdev_dpdk_get_features,                                 \
1359     NULL,                       /* set_advertisements */      \
1360                                                               \
1361     NULL,                       /* set_policing */            \
1362     NULL,                       /* get_qos_types */           \
1363     NULL,                       /* get_qos_capabilities */    \
1364     NULL,                       /* get_qos */                 \
1365     NULL,                       /* set_qos */                 \
1366     NULL,                       /* get_queue */               \
1367     NULL,                       /* set_queue */               \
1368     NULL,                       /* delete_queue */            \
1369     NULL,                       /* get_queue_stats */         \
1370     NULL,                       /* queue_dump_start */        \
1371     NULL,                       /* queue_dump_next */         \
1372     NULL,                       /* queue_dump_done */         \
1373     NULL,                       /* dump_queue_stats */        \
1374                                                               \
1375     NULL,                       /* get_in4 */                 \
1376     NULL,                       /* set_in4 */                 \
1377     NULL,                       /* get_in6 */                 \
1378     NULL,                       /* add_router */              \
1379     NULL,                       /* get_next_hop */            \
1380     netdev_dpdk_get_status,                                   \
1381     NULL,                       /* arp_lookup */              \
1382                                                               \
1383     netdev_dpdk_update_flags,                                 \
1384                                                               \
1385     netdev_dpdk_rxq_alloc,                                    \
1386     netdev_dpdk_rxq_construct,                                \
1387     netdev_dpdk_rxq_destruct,                                 \
1388     netdev_dpdk_rxq_dealloc,                                  \
1389     netdev_dpdk_rxq_recv,                                     \
1390     NULL,                       /* rx_wait */                 \
1391     NULL,                       /* rxq_drain */               \
1392 }
1393
1394 int
1395 dpdk_init(int argc, char **argv)
1396 {
1397     int result;
1398
1399     if (argc < 2 || strcmp(argv[1], "--dpdk"))
1400         return 0;
1401
1402     /* Make sure program name passed to rte_eal_init() is vswitchd. */
1403     argv[1] = argv[0];
1404
1405     argc--;
1406     argv++;
1407
1408     /* Make sure things are initialized ... */
1409     result = rte_eal_init(argc, argv);
1410     if (result < 0) {
1411         ovs_abort(result, "Cannot init EAL\n");
1412     }
1413
1414     rte_memzone_dump(stdout);
1415     rte_eal_init_ret = 0;
1416
1417     if (argc > result) {
1418         argv[result] = argv[0];
1419     }
1420
1421     /* We are called from the main thread here */
1422     thread_set_nonpmd();
1423
1424     return result + 1;
1425 }
1426
1427 const struct netdev_class dpdk_class =
1428     NETDEV_DPDK_CLASS(
1429         "dpdk",
1430         dpdk_class_init,
1431         netdev_dpdk_construct);
1432
1433 const struct netdev_class dpdk_ring_class =
1434     NETDEV_DPDK_CLASS(
1435         "dpdkr",
1436         NULL,
1437         netdev_dpdk_ring_construct);
1438
1439 void
1440 netdev_dpdk_register(void)
1441 {
1442     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1443
1444     if (rte_eal_init_ret) {
1445         return;
1446     }
1447
1448     if (ovsthread_once_start(&once)) {
1449         dpdk_common_init();
1450         netdev_register_provider(&dpdk_class);
1451         netdev_register_provider(&dpdk_ring_class);
1452         ovsthread_once_done(&once);
1453     }
1454 }
1455
1456 int
1457 pmd_thread_setaffinity_cpu(int cpu)
1458 {
1459     cpu_set_t cpuset;
1460     int err;
1461
1462     CPU_ZERO(&cpuset);
1463     CPU_SET(cpu, &cpuset);
1464     err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1465     if (err) {
1466         VLOG_ERR("Thread affinity error %d",err);
1467         return err;
1468     }
1469     /* lcore_id 0 is reseved for use by non pmd threads. */
1470     RTE_PER_LCORE(_lcore_id) = cpu + 1;
1471
1472     return 0;
1473 }
1474
1475 void
1476 thread_set_nonpmd(void)
1477 {
1478     /* We cannot have RTE_MAX_LCORE pmd threads, because lcore_id 0 is reserved
1479      * for non pmd threads */
1480     BUILD_ASSERT(NR_PMD_THREADS < RTE_MAX_LCORE);
1481     /* We have to use 0 to allow non pmd threads to perform certain DPDK
1482      * operations, like rte_eth_dev_configure(). */
1483     RTE_PER_LCORE(_lcore_id) = 0;
1484 }
1485
1486 static bool
1487 thread_is_pmd(void)
1488 {
1489     return rte_lcore_id() != 0;
1490 }