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