4b36f52a49699030a884339c9067fa093c564013
[sliver-openvswitch.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 "connectivity.h"
32 #include "dpif-netdev.h"
33 #include "list.h"
34 #include "netdev-dpdk.h"
35 #include "netdev-provider.h"
36 #include "netdev-vport.h"
37 #include "odp-util.h"
38 #include "ofp-print.h"
39 #include "ofpbuf.h"
40 #include "ovs-thread.h"
41 #include "ovs-rcu.h"
42 #include "packets.h"
43 #include "shash.h"
44 #include "seq.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 /* TODO: 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 NON_PMD_THREAD_TX_QUEUE 0
74
75 /* TODO: Needs per NIC value for these constants. */
76 #define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
77 #define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
78 #define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
79
80 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
81 #define TX_HTHRESH 0  /* Default values of TX host threshold reg. */
82 #define TX_WTHRESH 0  /* Default values of TX write-back threshold reg. */
83
84 static const struct rte_eth_conf port_conf = {
85         .rxmode = {
86                 .mq_mode = ETH_MQ_RX_RSS,
87                 .split_hdr_size = 0,
88                 .header_split   = 0, /* Header Split disabled */
89                 .hw_ip_checksum = 0, /* IP checksum offload disabled */
90                 .hw_vlan_filter = 0, /* VLAN filtering disabled */
91                 .jumbo_frame    = 0, /* Jumbo Frame Support disabled */
92                 .hw_strip_crc   = 0,
93         },
94         .rx_adv_conf = {
95                 .rss_conf = {
96                         .rss_key = NULL,
97                         .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6,
98                 },
99         },
100         .txmode = {
101                 .mq_mode = ETH_MQ_TX_NONE,
102         },
103 };
104
105 static const struct rte_eth_rxconf rx_conf = {
106         .rx_thresh = {
107                 .pthresh = RX_PTHRESH,
108                 .hthresh = RX_HTHRESH,
109                 .wthresh = RX_WTHRESH,
110         },
111 };
112
113 static const struct rte_eth_txconf tx_conf = {
114         .tx_thresh = {
115                 .pthresh = TX_PTHRESH,
116                 .hthresh = TX_HTHRESH,
117                 .wthresh = TX_WTHRESH,
118         },
119         .tx_free_thresh = 0,
120         .tx_rs_thresh = 0,
121 };
122
123 enum { MAX_RX_QUEUE_LEN = 64 };
124 enum { MAX_TX_QUEUE_LEN = 64 };
125 enum { DRAIN_TSC = 200000ULL };
126
127 static int rte_eal_init_ret = ENODEV;
128
129 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
130
131 /* Contains all 'struct dpdk_dev's. */
132 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
133     = LIST_INITIALIZER(&dpdk_list);
134
135 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
136     = LIST_INITIALIZER(&dpdk_mp_list);
137
138 static pthread_t watchdog_thread;
139
140 struct dpdk_mp {
141     struct rte_mempool *mp;
142     int mtu;
143     int socket_id;
144     int refcount;
145     struct list list_node OVS_GUARDED_BY(dpdk_mutex);
146 };
147
148 struct dpdk_tx_queue {
149     rte_spinlock_t tx_lock;
150     int count;
151     uint64_t tsc;
152     struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
153 };
154
155 struct netdev_dpdk {
156     struct netdev up;
157     int port_id;
158     int max_packet_len;
159
160     struct dpdk_tx_queue tx_q[NR_QUEUE];
161
162     struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
163
164     struct dpdk_mp *dpdk_mp;
165     int mtu;
166     int socket_id;
167     int buf_size;
168     struct netdev_stats stats_offset;
169     struct netdev_stats stats;
170
171     uint8_t hwaddr[ETH_ADDR_LEN];
172     enum netdev_flags flags;
173
174     struct rte_eth_link link;
175     int link_reset_cnt;
176
177     /* In dpdk_list. */
178     struct list list_node OVS_GUARDED_BY(dpdk_mutex);
179 };
180
181 struct netdev_rxq_dpdk {
182     struct netdev_rxq up;
183     int port_id;
184 };
185
186 static int netdev_dpdk_construct(struct netdev *);
187
188 static bool
189 is_dpdk_class(const struct netdev_class *class)
190 {
191     return class->construct == netdev_dpdk_construct;
192 }
193
194 /* TODO: use dpdk malloc for entire OVS. infact huge page shld be used
195  * for all other sengments data, bss and text. */
196
197 static void *
198 dpdk_rte_mzalloc(size_t sz)
199 {
200     void *ptr;
201
202     ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
203     if (ptr == NULL) {
204         out_of_memory();
205     }
206     return ptr;
207 }
208
209 void
210 free_dpdk_buf(struct ofpbuf *b)
211 {
212     struct rte_mbuf *pkt = (struct rte_mbuf *) b;
213
214     rte_mempool_put(pkt->pool, pkt);
215 }
216
217 static void
218 __rte_pktmbuf_init(struct rte_mempool *mp,
219                    void *opaque_arg OVS_UNUSED,
220                    void *_m,
221                    unsigned i OVS_UNUSED)
222 {
223     struct rte_mbuf *m = _m;
224     uint32_t buf_len = mp->elt_size - sizeof(struct ofpbuf);
225
226     RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct ofpbuf));
227
228     memset(m, 0, mp->elt_size);
229
230     /* start of buffer is just after mbuf structure */
231     m->buf_addr = (char *)m + sizeof(struct ofpbuf);
232     m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
233                     sizeof(struct ofpbuf);
234     m->buf_len = (uint16_t)buf_len;
235
236     /* keep some headroom between start of buffer and data */
237     m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
238
239     /* init some constant fields */
240     m->type = RTE_MBUF_PKT;
241     m->pool = mp;
242     m->pkt.nb_segs = 1;
243     m->pkt.in_port = 0xff;
244 }
245
246 static void
247 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
248                      void *opaque_arg OVS_UNUSED,
249                      void *_m,
250                      unsigned i OVS_UNUSED)
251 {
252     struct rte_mbuf *m = _m;
253
254     __rte_pktmbuf_init(mp, opaque_arg, _m, i);
255
256     ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
257 }
258
259 static struct dpdk_mp *
260 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
261 {
262     struct dpdk_mp *dmp = NULL;
263     char mp_name[RTE_MEMPOOL_NAMESIZE];
264
265     LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
266         if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
267             dmp->refcount++;
268             return dmp;
269         }
270     }
271
272     dmp = dpdk_rte_mzalloc(sizeof *dmp);
273     dmp->socket_id = socket_id;
274     dmp->mtu = mtu;
275     dmp->refcount = 1;
276
277     snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d", dmp->mtu);
278     dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
279                                  MP_CACHE_SZ,
280                                  sizeof(struct rte_pktmbuf_pool_private),
281                                  rte_pktmbuf_pool_init, NULL,
282                                  ovs_rte_pktmbuf_init, NULL,
283                                  socket_id, 0);
284
285     if (dmp->mp == NULL) {
286         return NULL;
287     }
288
289     list_push_back(&dpdk_mp_list, &dmp->list_node);
290     return dmp;
291 }
292
293 static void
294 dpdk_mp_put(struct dpdk_mp *dmp)
295 {
296
297     if (!dmp) {
298         return;
299     }
300
301     dmp->refcount--;
302     ovs_assert(dmp->refcount >= 0);
303
304 #if 0
305     /* I could not find any API to destroy mp. */
306     if (dmp->refcount == 0) {
307         list_delete(dmp->list_node);
308         /* destroy mp-pool. */
309     }
310 #endif
311 }
312
313 static void
314 check_link_status(struct netdev_dpdk *dev)
315 {
316     struct rte_eth_link link;
317
318     rte_eth_link_get_nowait(dev->port_id, &link);
319
320     if (dev->link.link_status != link.link_status) {
321         seq_change(connectivity_seq_get());
322
323         dev->link_reset_cnt++;
324         dev->link = link;
325         if (dev->link.link_status) {
326             VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
327                         dev->port_id, (unsigned)dev->link.link_speed,
328                         (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
329                          ("full-duplex") : ("half-duplex"));
330         } else {
331             VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
332         }
333     }
334 }
335
336 static void *
337 dpdk_watchdog(void *dummy OVS_UNUSED)
338 {
339     struct netdev_dpdk *dev;
340
341     pthread_detach(pthread_self());
342
343     for (;;) {
344         ovs_mutex_lock(&dpdk_mutex);
345         LIST_FOR_EACH (dev, list_node, &dpdk_list) {
346             ovs_mutex_lock(&dev->mutex);
347             check_link_status(dev);
348             ovs_mutex_unlock(&dev->mutex);
349         }
350         ovs_mutex_unlock(&dpdk_mutex);
351         xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
352     }
353
354     return NULL;
355 }
356
357 static int
358 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
359 {
360     struct rte_pktmbuf_pool_private *mbp_priv;
361     struct ether_addr eth_addr;
362     int diag;
363     int i;
364
365     if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
366         return -ENODEV;
367     }
368
369     diag = rte_eth_dev_configure(dev->port_id, NR_QUEUE, NR_QUEUE,  &port_conf);
370     if (diag) {
371         VLOG_ERR("eth dev config error %d",diag);
372         return diag;
373     }
374
375     for (i = 0; i < NR_QUEUE; i++) {
376         diag = rte_eth_tx_queue_setup(dev->port_id, i, 64, 0, &tx_conf);
377         if (diag) {
378             VLOG_ERR("eth dev tx queue setup error %d",diag);
379             return diag;
380         }
381     }
382
383     for (i = 0; i < NR_QUEUE; i++) {
384         diag = rte_eth_rx_queue_setup(dev->port_id, i, 64, 0, &rx_conf,
385                                       dev->dpdk_mp->mp);
386         if (diag) {
387             VLOG_ERR("eth dev rx queue setup error %d",diag);
388             return diag;
389         }
390     }
391
392     diag = rte_eth_dev_start(dev->port_id);
393     if (diag) {
394         VLOG_ERR("eth dev start error %d",diag);
395         return diag;
396     }
397
398     rte_eth_promiscuous_enable(dev->port_id);
399     rte_eth_allmulticast_enable(dev->port_id);
400
401     memset(&eth_addr, 0x0, sizeof(eth_addr));
402     rte_eth_macaddr_get(dev->port_id, &eth_addr);
403     VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
404                     dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
405
406     memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
407     rte_eth_link_get_nowait(dev->port_id, &dev->link);
408
409     mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
410     dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
411
412     dev->flags = NETDEV_UP | NETDEV_PROMISC;
413     return 0;
414 }
415
416 static struct netdev_dpdk *
417 netdev_dpdk_cast(const struct netdev *netdev)
418 {
419     return CONTAINER_OF(netdev, struct netdev_dpdk, up);
420 }
421
422 static struct netdev *
423 netdev_dpdk_alloc(void)
424 {
425     struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
426     return &netdev->up;
427 }
428
429 static int
430 netdev_dpdk_construct(struct netdev *netdev_)
431 {
432     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
433     unsigned int port_no;
434     char *cport;
435     int err;
436     int i;
437
438     if (rte_eal_init_ret) {
439         return rte_eal_init_ret;
440     }
441
442     ovs_mutex_lock(&dpdk_mutex);
443     cport = netdev_->name + 4; /* Names always start with "dpdk" */
444
445     if (strncmp(netdev_->name, "dpdk", 4)) {
446         err = ENODEV;
447         goto unlock_dpdk;
448     }
449
450     port_no = strtol(cport, 0, 0); /* string must be null terminated */
451
452     for (i = 0; i < NR_QUEUE; i++) {
453         rte_spinlock_init(&netdev->tx_q[i].tx_lock);
454     }
455
456     ovs_mutex_init(&netdev->mutex);
457
458     ovs_mutex_lock(&netdev->mutex);
459     netdev->flags = 0;
460
461     netdev->mtu = ETHER_MTU;
462     netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
463
464     /* TODO: need to discover device node at run time. */
465     netdev->socket_id = SOCKET0;
466     netdev->port_id = port_no;
467
468     netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
469     if (!netdev->dpdk_mp) {
470         err = ENOMEM;
471         goto unlock_dev;
472     }
473
474     err = dpdk_eth_dev_init(netdev);
475     if (err) {
476         goto unlock_dev;
477     }
478     netdev_->n_rxq = NR_QUEUE;
479
480     list_push_back(&dpdk_list, &netdev->list_node);
481
482 unlock_dev:
483     ovs_mutex_unlock(&netdev->mutex);
484 unlock_dpdk:
485     ovs_mutex_unlock(&dpdk_mutex);
486     return err;
487 }
488
489 static void
490 netdev_dpdk_destruct(struct netdev *netdev_)
491 {
492     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
493
494     ovs_mutex_lock(&dev->mutex);
495     rte_eth_dev_stop(dev->port_id);
496     ovs_mutex_unlock(&dev->mutex);
497
498     ovs_mutex_lock(&dpdk_mutex);
499     list_remove(&dev->list_node);
500     dpdk_mp_put(dev->dpdk_mp);
501     ovs_mutex_unlock(&dpdk_mutex);
502
503     ovs_mutex_destroy(&dev->mutex);
504 }
505
506 static void
507 netdev_dpdk_dealloc(struct netdev *netdev_)
508 {
509     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
510
511     rte_free(netdev);
512 }
513
514 static int
515 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
516 {
517     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
518
519     ovs_mutex_lock(&dev->mutex);
520
521     /* TODO: Allow to configure number of queues. */
522     smap_add_format(args, "configured_rx_queues", "%u", netdev_->n_rxq);
523     smap_add_format(args, "configured_tx_queues", "%u", netdev_->n_rxq);
524     ovs_mutex_unlock(&dev->mutex);
525
526     return 0;
527 }
528
529 static struct netdev_rxq *
530 netdev_dpdk_rxq_alloc(void)
531 {
532     struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
533
534     return &rx->up;
535 }
536
537 static struct netdev_rxq_dpdk *
538 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
539 {
540     return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
541 }
542
543 static int
544 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
545 {
546     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
547     struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
548
549     ovs_mutex_lock(&netdev->mutex);
550     rx->port_id = netdev->port_id;
551     ovs_mutex_unlock(&netdev->mutex);
552
553     return 0;
554 }
555
556 static void
557 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
558 {
559 }
560
561 static void
562 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
563 {
564     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
565
566     rte_free(rx);
567 }
568
569 inline static void
570 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
571 {
572     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
573     uint32_t nb_tx;
574
575     if (txq->count == 0) {
576         return;
577     }
578     rte_spinlock_lock(&txq->tx_lock);
579     nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
580     if (nb_tx != txq->count) {
581         /* free buffers if we couldn't transmit packets */
582         rte_mempool_put_bulk(dev->dpdk_mp->mp,
583                              (void **) &txq->burst_pkts[nb_tx],
584                              (txq->count - nb_tx));
585     }
586     txq->count = 0;
587     rte_spinlock_unlock(&txq->tx_lock);
588 }
589
590 static int
591 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **packets, int *c)
592 {
593     struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
594     struct netdev *netdev = rx->up.netdev;
595     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
596     int nb_rx;
597
598     dpdk_queue_flush(dev, rxq_->queue_id);
599
600     nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
601                              (struct rte_mbuf **) packets, MAX_RX_QUEUE_LEN);
602     if (!nb_rx) {
603         return EAGAIN;
604     }
605
606     *c = nb_rx;
607
608     return 0;
609 }
610
611 inline static void
612 dpdk_queue_pkt(struct netdev_dpdk *dev, int qid,
613                struct rte_mbuf *pkt)
614 {
615     struct dpdk_tx_queue *txq = &dev->tx_q[qid];
616     uint64_t diff_tsc;
617     uint64_t cur_tsc;
618     uint32_t nb_tx;
619
620     rte_spinlock_lock(&txq->tx_lock);
621     txq->burst_pkts[txq->count++] = pkt;
622     if (txq->count == MAX_TX_QUEUE_LEN) {
623         goto flush;
624     }
625     cur_tsc = rte_get_timer_cycles();
626     if (txq->count == 1) {
627         txq->tsc = cur_tsc;
628     }
629     diff_tsc = cur_tsc - txq->tsc;
630     if (diff_tsc >= DRAIN_TSC) {
631         goto flush;
632     }
633     rte_spinlock_unlock(&txq->tx_lock);
634     return;
635
636 flush:
637     nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
638     if (nb_tx != txq->count) {
639         /* free buffers if we couldn't transmit packets */
640         rte_mempool_put_bulk(dev->dpdk_mp->mp,
641                              (void **) &txq->burst_pkts[nb_tx],
642                              (txq->count - nb_tx));
643     }
644     txq->count = 0;
645     rte_spinlock_unlock(&txq->tx_lock);
646 }
647
648 /* Tx function. Transmit packets indefinitely */
649 static void
650 dpdk_do_tx_copy(struct netdev *netdev, char *buf, int size)
651 {
652     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
653     struct rte_mbuf *pkt;
654
655     pkt = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
656     if (!pkt) {
657         ovs_mutex_lock(&dev->mutex);
658         dev->stats.tx_dropped++;
659         ovs_mutex_unlock(&dev->mutex);
660         return;
661     }
662
663     /* We have to do a copy for now */
664     memcpy(pkt->pkt.data, buf, size);
665
666     rte_pktmbuf_data_len(pkt) = size;
667     rte_pktmbuf_pkt_len(pkt) = size;
668
669     dpdk_queue_pkt(dev, NON_PMD_THREAD_TX_QUEUE, pkt);
670     dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
671 }
672
673 static int
674 netdev_dpdk_send(struct netdev *netdev,
675                  struct ofpbuf *ofpbuf, bool may_steal)
676 {
677     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
678     int ret;
679
680     if (ofpbuf_size(ofpbuf) > dev->max_packet_len) {
681         VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
682                      (int)ofpbuf_size(ofpbuf) , dev->max_packet_len);
683
684         ovs_mutex_lock(&dev->mutex);
685         dev->stats.tx_dropped++;
686         ovs_mutex_unlock(&dev->mutex);
687
688         ret = E2BIG;
689         goto out;
690     }
691
692     if (!may_steal || ofpbuf->source != OFPBUF_DPDK) {
693         dpdk_do_tx_copy(netdev, (char *) ofpbuf_data(ofpbuf), ofpbuf_size(ofpbuf));
694
695         if (may_steal) {
696             ofpbuf_delete(ofpbuf);
697         }
698     } else {
699         int qid;
700
701         qid = rte_lcore_id() % NR_QUEUE;
702
703         dpdk_queue_pkt(dev, qid, (struct rte_mbuf *)ofpbuf);
704
705     }
706     ret = 0;
707
708 out:
709     return ret;
710 }
711
712 static int
713 netdev_dpdk_set_etheraddr(struct netdev *netdev,
714                           const uint8_t mac[ETH_ADDR_LEN])
715 {
716     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
717
718     ovs_mutex_lock(&dev->mutex);
719     if (!eth_addr_equals(dev->hwaddr, mac)) {
720         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
721     }
722     ovs_mutex_unlock(&dev->mutex);
723
724     return 0;
725 }
726
727 static int
728 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
729                           uint8_t mac[ETH_ADDR_LEN])
730 {
731     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
732
733     ovs_mutex_lock(&dev->mutex);
734     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
735     ovs_mutex_unlock(&dev->mutex);
736
737     return 0;
738 }
739
740 static int
741 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
742 {
743     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
744
745     ovs_mutex_lock(&dev->mutex);
746     *mtup = dev->mtu;
747     ovs_mutex_unlock(&dev->mutex);
748
749     return 0;
750 }
751
752 static int
753 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
754 {
755     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
756     int old_mtu, err;
757     struct dpdk_mp *old_mp;
758     struct dpdk_mp *mp;
759
760     ovs_mutex_lock(&dpdk_mutex);
761     ovs_mutex_lock(&dev->mutex);
762     if (dev->mtu == mtu) {
763         err = 0;
764         goto out;
765     }
766
767     mp = dpdk_mp_get(dev->socket_id, dev->mtu);
768     if (!mp) {
769         err = ENOMEM;
770         goto out;
771     }
772
773     rte_eth_dev_stop(dev->port_id);
774
775     old_mtu = dev->mtu;
776     old_mp = dev->dpdk_mp;
777     dev->dpdk_mp = mp;
778     dev->mtu = mtu;
779     dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
780
781     err = dpdk_eth_dev_init(dev);
782     if (err) {
783
784         dpdk_mp_put(mp);
785         dev->mtu = old_mtu;
786         dev->dpdk_mp = old_mp;
787         dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
788         dpdk_eth_dev_init(dev);
789         goto out;
790     }
791
792     dpdk_mp_put(old_mp);
793 out:
794     ovs_mutex_unlock(&dev->mutex);
795     ovs_mutex_unlock(&dpdk_mutex);
796     return err;
797 }
798
799 static int
800 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
801
802 static int
803 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
804 {
805     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
806     struct rte_eth_stats rte_stats;
807     bool gg;
808
809     netdev_dpdk_get_carrier(netdev, &gg);
810     ovs_mutex_lock(&dev->mutex);
811     rte_eth_stats_get(dev->port_id, &rte_stats);
812
813     *stats = dev->stats_offset;
814
815     stats->rx_packets += rte_stats.ipackets;
816     stats->tx_packets += rte_stats.opackets;
817     stats->rx_bytes += rte_stats.ibytes;
818     stats->tx_bytes += rte_stats.obytes;
819     stats->rx_errors += rte_stats.ierrors;
820     stats->tx_errors += rte_stats.oerrors;
821     stats->multicast += rte_stats.imcasts;
822
823     stats->tx_dropped += dev->stats.tx_dropped;
824     ovs_mutex_unlock(&dev->mutex);
825
826     return 0;
827 }
828
829 static int
830 netdev_dpdk_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
831 {
832     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
833
834     ovs_mutex_lock(&dev->mutex);
835     dev->stats_offset = *stats;
836     ovs_mutex_unlock(&dev->mutex);
837
838     return 0;
839 }
840
841 static int
842 netdev_dpdk_get_features(const struct netdev *netdev_,
843                          enum netdev_features *current,
844                          enum netdev_features *advertised OVS_UNUSED,
845                          enum netdev_features *supported OVS_UNUSED,
846                          enum netdev_features *peer OVS_UNUSED)
847 {
848     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
849     struct rte_eth_link link;
850
851     ovs_mutex_lock(&dev->mutex);
852     link = dev->link;
853     ovs_mutex_unlock(&dev->mutex);
854
855     if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
856         if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
857             *current = NETDEV_F_AUTONEG;
858         }
859     } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
860         if (link.link_speed == ETH_LINK_SPEED_10) {
861             *current = NETDEV_F_10MB_HD;
862         }
863         if (link.link_speed == ETH_LINK_SPEED_100) {
864             *current = NETDEV_F_100MB_HD;
865         }
866         if (link.link_speed == ETH_LINK_SPEED_1000) {
867             *current = NETDEV_F_1GB_HD;
868         }
869     } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
870         if (link.link_speed == ETH_LINK_SPEED_10) {
871             *current = NETDEV_F_10MB_FD;
872         }
873         if (link.link_speed == ETH_LINK_SPEED_100) {
874             *current = NETDEV_F_100MB_FD;
875         }
876         if (link.link_speed == ETH_LINK_SPEED_1000) {
877             *current = NETDEV_F_1GB_FD;
878         }
879         if (link.link_speed == ETH_LINK_SPEED_10000) {
880             *current = NETDEV_F_10GB_FD;
881         }
882     }
883
884     return 0;
885 }
886
887 static int
888 netdev_dpdk_get_ifindex(const struct netdev *netdev)
889 {
890     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
891     int ifindex;
892
893     ovs_mutex_lock(&dev->mutex);
894     ifindex = dev->port_id;
895     ovs_mutex_unlock(&dev->mutex);
896
897     return ifindex;
898 }
899
900 static int
901 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
902 {
903     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
904
905     ovs_mutex_lock(&dev->mutex);
906     check_link_status(dev);
907     *carrier = dev->link.link_status;
908     ovs_mutex_unlock(&dev->mutex);
909
910     return 0;
911 }
912
913 static long long int
914 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
915 {
916     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
917     long long int carrier_resets;
918
919     ovs_mutex_lock(&dev->mutex);
920     carrier_resets = dev->link_reset_cnt;
921     ovs_mutex_unlock(&dev->mutex);
922
923     return carrier_resets;
924 }
925
926 static int
927 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
928                        long long int interval OVS_UNUSED)
929 {
930     return 0;
931 }
932
933 static int
934 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
935                            enum netdev_flags off, enum netdev_flags on,
936                            enum netdev_flags *old_flagsp)
937     OVS_REQUIRES(dev->mutex)
938 {
939     int err;
940
941     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
942         return EINVAL;
943     }
944
945     *old_flagsp = dev->flags;
946     dev->flags |= on;
947     dev->flags &= ~off;
948
949     if (dev->flags == *old_flagsp) {
950         return 0;
951     }
952
953     if (dev->flags & NETDEV_UP) {
954         err = rte_eth_dev_start(dev->port_id);
955         if (err)
956             return err;
957     }
958
959     if (dev->flags & NETDEV_PROMISC) {
960         rte_eth_promiscuous_enable(dev->port_id);
961     }
962
963     if (!(dev->flags & NETDEV_UP)) {
964         rte_eth_dev_stop(dev->port_id);
965     }
966
967     return 0;
968 }
969
970 static int
971 netdev_dpdk_update_flags(struct netdev *netdev_,
972                          enum netdev_flags off, enum netdev_flags on,
973                          enum netdev_flags *old_flagsp)
974 {
975     struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
976     int error;
977
978     ovs_mutex_lock(&netdev->mutex);
979     error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
980     ovs_mutex_unlock(&netdev->mutex);
981
982     return error;
983 }
984
985 static int
986 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
987 {
988     struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
989     struct rte_eth_dev_info dev_info;
990
991     if (dev->port_id <= 0)
992         return ENODEV;
993
994     ovs_mutex_lock(&dev->mutex);
995     rte_eth_dev_info_get(dev->port_id, &dev_info);
996     ovs_mutex_unlock(&dev->mutex);
997
998     smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
999
1000     smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1001     smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1002     smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1003     smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1004     smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1005     smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1006     smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1007     smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1008     smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1009     smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1010
1011     smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1012     smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1013
1014     return 0;
1015 }
1016
1017 static void
1018 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1019     OVS_REQUIRES(dev->mutex)
1020 {
1021     enum netdev_flags old_flags;
1022
1023     if (admin_state) {
1024         netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1025     } else {
1026         netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1027     }
1028 }
1029
1030 static void
1031 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1032                             const char *argv[], void *aux OVS_UNUSED)
1033 {
1034     bool up;
1035
1036     if (!strcasecmp(argv[argc - 1], "up")) {
1037         up = true;
1038     } else if ( !strcasecmp(argv[argc - 1], "down")) {
1039         up = false;
1040     } else {
1041         unixctl_command_reply_error(conn, "Invalid Admin State");
1042         return;
1043     }
1044
1045     if (argc > 2) {
1046         struct netdev *netdev = netdev_from_name(argv[1]);
1047         if (netdev && is_dpdk_class(netdev->netdev_class)) {
1048             struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1049
1050             ovs_mutex_lock(&dpdk_dev->mutex);
1051             netdev_dpdk_set_admin_state__(dpdk_dev, up);
1052             ovs_mutex_unlock(&dpdk_dev->mutex);
1053
1054             netdev_close(netdev);
1055         } else {
1056             unixctl_command_reply_error(conn, "Not a DPDK Interface");
1057             netdev_close(netdev);
1058             return;
1059         }
1060     } else {
1061         struct netdev_dpdk *netdev;
1062
1063         ovs_mutex_lock(&dpdk_mutex);
1064         LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1065             ovs_mutex_lock(&netdev->mutex);
1066             netdev_dpdk_set_admin_state__(netdev, up);
1067             ovs_mutex_unlock(&netdev->mutex);
1068         }
1069         ovs_mutex_unlock(&dpdk_mutex);
1070     }
1071     unixctl_command_reply(conn, "OK");
1072 }
1073
1074 static int
1075 dpdk_class_init(void)
1076 {
1077     int result;
1078
1079     if (rte_eal_init_ret) {
1080         return 0;
1081     }
1082
1083     result = rte_pmd_init_all();
1084     if (result) {
1085         VLOG_ERR("Cannot init PMD");
1086         return result;
1087     }
1088
1089     result = rte_eal_pci_probe();
1090     if (result) {
1091         VLOG_ERR("Cannot probe PCI");
1092         return result;
1093     }
1094
1095     if (rte_eth_dev_count() < 1) {
1096         VLOG_ERR("No Ethernet devices found. Try assigning ports to UIO.");
1097     }
1098
1099     VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1100
1101     list_init(&dpdk_list);
1102     list_init(&dpdk_mp_list);
1103
1104     unixctl_command_register("netdev-dpdk/set-admin-state",
1105                              "[netdev] up|down", 1, 2,
1106                              netdev_dpdk_set_admin_state, NULL);
1107
1108     xpthread_create(&watchdog_thread, NULL, dpdk_watchdog, NULL);
1109     return 0;
1110 }
1111
1112 static struct netdev_class netdev_dpdk_class = {
1113     "dpdk",
1114     dpdk_class_init,            /* init */
1115     NULL,                       /* netdev_dpdk_run */
1116     NULL,                       /* netdev_dpdk_wait */
1117
1118     netdev_dpdk_alloc,
1119     netdev_dpdk_construct,
1120     netdev_dpdk_destruct,
1121     netdev_dpdk_dealloc,
1122     netdev_dpdk_get_config,
1123     NULL,                       /* netdev_dpdk_set_config */
1124     NULL,                       /* get_tunnel_config */
1125
1126     netdev_dpdk_send,           /* send */
1127     NULL,                       /* send_wait */
1128
1129     netdev_dpdk_set_etheraddr,
1130     netdev_dpdk_get_etheraddr,
1131     netdev_dpdk_get_mtu,
1132     netdev_dpdk_set_mtu,
1133     netdev_dpdk_get_ifindex,
1134     netdev_dpdk_get_carrier,
1135     netdev_dpdk_get_carrier_resets,
1136     netdev_dpdk_set_miimon,
1137     netdev_dpdk_get_stats,
1138     netdev_dpdk_set_stats,
1139     netdev_dpdk_get_features,
1140     NULL,                       /* set_advertisements */
1141
1142     NULL,                       /* set_policing */
1143     NULL,                       /* get_qos_types */
1144     NULL,                       /* get_qos_capabilities */
1145     NULL,                       /* get_qos */
1146     NULL,                       /* set_qos */
1147     NULL,                       /* get_queue */
1148     NULL,                       /* set_queue */
1149     NULL,                       /* delete_queue */
1150     NULL,                       /* get_queue_stats */
1151     NULL,                       /* queue_dump_start */
1152     NULL,                       /* queue_dump_next */
1153     NULL,                       /* queue_dump_done */
1154     NULL,                       /* dump_queue_stats */
1155
1156     NULL,                       /* get_in4 */
1157     NULL,                       /* set_in4 */
1158     NULL,                       /* get_in6 */
1159     NULL,                       /* add_router */
1160     NULL,                       /* get_next_hop */
1161     netdev_dpdk_get_status,
1162     NULL,                       /* arp_lookup */
1163
1164     netdev_dpdk_update_flags,
1165
1166     netdev_dpdk_rxq_alloc,
1167     netdev_dpdk_rxq_construct,
1168     netdev_dpdk_rxq_destruct,
1169     netdev_dpdk_rxq_dealloc,
1170     netdev_dpdk_rxq_recv,
1171     NULL,                       /* rxq_wait */
1172     NULL,                       /* rxq_drain */
1173 };
1174
1175 int
1176 dpdk_init(int argc, char **argv)
1177 {
1178     int result;
1179
1180     if (strcmp(argv[1], "--dpdk"))
1181         return 0;
1182
1183     argc--;
1184     argv++;
1185
1186     /* Make sure things are initialized ... */
1187     result = rte_eal_init(argc, argv);
1188     if (result < 0)
1189         ovs_abort(result, "Cannot init EAL\n");
1190
1191     rte_memzone_dump();
1192     rte_eal_init_ret = 0;
1193
1194     return result;
1195 }
1196
1197 void
1198 netdev_dpdk_register(void)
1199 {
1200     netdev_register_provider(&netdev_dpdk_class);
1201 }
1202
1203 int
1204 pmd_thread_setaffinity_cpu(int cpu)
1205 {
1206     cpu_set_t cpuset;
1207     int err;
1208
1209     CPU_ZERO(&cpuset);
1210     CPU_SET(cpu, &cpuset);
1211     err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1212     if (err) {
1213         VLOG_ERR("Thread affinity error %d",err);
1214         return err;
1215     }
1216     RTE_PER_LCORE(_lcore_id) = cpu;
1217
1218     return 0;
1219 }