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