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