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