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