2 * Copyright (c) 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 #include "dpif-netdev.h"
33 #include "netdev-dpdk.h"
34 #include "netdev-provider.h"
35 #include "netdev-vport.h"
37 #include "ofp-print.h"
40 #include "ovs-thread.h"
42 #include "packet-dpif.h"
46 #include "unaligned.h"
51 VLOG_DEFINE_THIS_MODULE(dpdk);
52 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
54 #define DPDK_PORT_WATCHDOG_INTERVAL 5
56 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
57 #define OVS_VPORT_DPDK "ovs_dpdk"
60 * need to reserve tons of extra space in the mbufs so we can align the
61 * DMA addresses to 4KB.
64 #define MTU_TO_MAX_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
65 #define MBUF_SIZE(mtu) (MTU_TO_MAX_LEN(mtu) + (512) + \
66 sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
68 /* XXX: mempool size should be based on system resources. */
69 #define NB_MBUF (4096 * 64)
70 #define MP_CACHE_SZ (256 * 2)
73 #define NON_PMD_THREAD_TX_QUEUE 0
75 #define NIC_PORT_RX_Q_SIZE 2048 /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
76 #define NIC_PORT_TX_Q_SIZE 2048 /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
78 /* XXX: Needs per NIC value for these constants. */
79 #define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
80 #define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
81 #define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
83 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
84 #define TX_HTHRESH 0 /* Default values of TX host threshold reg. */
85 #define TX_WTHRESH 0 /* Default values of TX write-back threshold reg. */
87 static const struct rte_eth_conf port_conf = {
89 .mq_mode = ETH_MQ_RX_RSS,
91 .header_split = 0, /* Header Split disabled */
92 .hw_ip_checksum = 0, /* IP checksum offload disabled */
93 .hw_vlan_filter = 0, /* VLAN filtering disabled */
94 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
100 .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6
101 | ETH_RSS_IPV4_UDP | ETH_RSS_IPV6_TCP | ETH_RSS_IPV6_UDP,
105 .mq_mode = ETH_MQ_TX_NONE,
109 static const struct rte_eth_rxconf rx_conf = {
111 .pthresh = RX_PTHRESH,
112 .hthresh = RX_HTHRESH,
113 .wthresh = RX_WTHRESH,
117 static const struct rte_eth_txconf tx_conf = {
119 .pthresh = TX_PTHRESH,
120 .hthresh = TX_HTHRESH,
121 .wthresh = TX_WTHRESH,
125 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS|ETH_TXQ_FLAGS_NOOFFLOADS,
128 enum { MAX_RX_QUEUE_LEN = 192 };
129 enum { MAX_TX_QUEUE_LEN = 384 };
130 enum { DPDK_RING_SIZE = 256 };
131 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
132 enum { DRAIN_TSC = 200000ULL };
134 static int rte_eal_init_ret = ENODEV;
136 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
138 /* Contains all 'struct dpdk_dev's. */
139 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
140 = LIST_INITIALIZER(&dpdk_list);
142 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
143 = LIST_INITIALIZER(&dpdk_mp_list);
145 /* This mutex must be used by non pmd threads when allocating or freeing
146 * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
147 * use mempools, a non pmd thread should hold this mutex while calling them */
148 struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
151 struct rte_mempool *mp;
155 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
158 /* There should be one 'struct dpdk_tx_queue' created for
160 struct dpdk_tx_queue {
161 bool flush_tx; /* Set to true to flush queue everytime */
162 /* pkts are queued. */
165 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
168 /* dpdk has no way to remove dpdk ring ethernet devices
169 so we have to keep them around once they've been created
172 static struct list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
173 = LIST_INITIALIZER(&dpdk_ring_list);
176 /* For the client rings */
177 struct rte_ring *cring_tx;
178 struct rte_ring *cring_rx;
179 int user_port_id; /* User given port no, parsed from port name */
180 int eth_port_id; /* ethernet device port id */
181 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
189 struct dpdk_tx_queue *tx_q;
191 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
193 struct dpdk_mp *dpdk_mp;
197 struct netdev_stats stats;
199 uint8_t hwaddr[ETH_ADDR_LEN];
200 enum netdev_flags flags;
202 struct rte_eth_link link;
206 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
209 struct netdev_rxq_dpdk {
210 struct netdev_rxq up;
214 static bool thread_is_pmd(void);
216 static int netdev_dpdk_construct(struct netdev *);
219 is_dpdk_class(const struct netdev_class *class)
221 return class->construct == netdev_dpdk_construct;
224 /* XXX: use dpdk malloc for entire OVS. infact huge page shld be used
225 * for all other sengments data, bss and text. */
228 dpdk_rte_mzalloc(size_t sz)
232 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
239 /* XXX this function should be called only by pmd threads (or by non pmd
240 * threads holding the nonpmd_mempool_mutex) */
242 free_dpdk_buf(struct dpif_packet *p)
244 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
246 rte_pktmbuf_free_seg(pkt);
250 __rte_pktmbuf_init(struct rte_mempool *mp,
251 void *opaque_arg OVS_UNUSED,
253 unsigned i OVS_UNUSED)
255 struct rte_mbuf *m = _m;
256 uint32_t buf_len = mp->elt_size - sizeof(struct dpif_packet);
258 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dpif_packet));
260 memset(m, 0, mp->elt_size);
262 /* start of buffer is just after mbuf structure */
263 m->buf_addr = (char *)m + sizeof(struct dpif_packet);
264 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
265 sizeof(struct dpif_packet);
266 m->buf_len = (uint16_t)buf_len;
268 /* keep some headroom between start of buffer and data */
269 m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
271 /* init some constant fields */
272 m->type = RTE_MBUF_PKT;
275 m->pkt.in_port = 0xff;
279 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
280 void *opaque_arg OVS_UNUSED,
282 unsigned i OVS_UNUSED)
284 struct rte_mbuf *m = _m;
286 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
288 ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
291 static struct dpdk_mp *
292 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
294 struct dpdk_mp *dmp = NULL;
295 char mp_name[RTE_MEMPOOL_NAMESIZE];
297 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
298 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
304 dmp = dpdk_rte_mzalloc(sizeof *dmp);
305 dmp->socket_id = socket_id;
309 if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d_%d", dmp->mtu,
310 dmp->socket_id) < 0) {
314 dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
316 sizeof(struct rte_pktmbuf_pool_private),
317 rte_pktmbuf_pool_init, NULL,
318 ovs_rte_pktmbuf_init, NULL,
321 if (dmp->mp == NULL) {
325 list_push_back(&dpdk_mp_list, &dmp->list_node);
330 dpdk_mp_put(struct dpdk_mp *dmp)
338 ovs_assert(dmp->refcount >= 0);
341 /* I could not find any API to destroy mp. */
342 if (dmp->refcount == 0) {
343 list_delete(dmp->list_node);
344 /* destroy mp-pool. */
350 check_link_status(struct netdev_dpdk *dev)
352 struct rte_eth_link link;
354 rte_eth_link_get_nowait(dev->port_id, &link);
356 if (dev->link.link_status != link.link_status) {
357 netdev_change_seq_changed(&dev->up);
359 dev->link_reset_cnt++;
361 if (dev->link.link_status) {
362 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
363 dev->port_id, (unsigned)dev->link.link_speed,
364 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
365 ("full-duplex") : ("half-duplex"));
367 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
373 dpdk_watchdog(void *dummy OVS_UNUSED)
375 struct netdev_dpdk *dev;
377 pthread_detach(pthread_self());
380 ovs_mutex_lock(&dpdk_mutex);
381 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
382 ovs_mutex_lock(&dev->mutex);
383 check_link_status(dev);
384 ovs_mutex_unlock(&dev->mutex);
386 ovs_mutex_unlock(&dpdk_mutex);
387 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
394 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
396 struct rte_pktmbuf_pool_private *mbp_priv;
397 struct ether_addr eth_addr;
401 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
405 diag = rte_eth_dev_configure(dev->port_id, dev->up.n_rxq, dev->up.n_txq,
408 VLOG_ERR("eth dev config error %d",diag);
412 for (i = 0; i < dev->up.n_txq; i++) {
413 diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
414 dev->socket_id, &tx_conf);
416 VLOG_ERR("eth dev tx queue setup error %d",diag);
421 for (i = 0; i < dev->up.n_rxq; i++) {
422 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
424 &rx_conf, dev->dpdk_mp->mp);
426 VLOG_ERR("eth dev rx queue setup error %d",diag);
431 diag = rte_eth_dev_start(dev->port_id);
433 VLOG_ERR("eth dev start error %d",diag);
437 rte_eth_promiscuous_enable(dev->port_id);
438 rte_eth_allmulticast_enable(dev->port_id);
440 memset(ð_addr, 0x0, sizeof(eth_addr));
441 rte_eth_macaddr_get(dev->port_id, ð_addr);
442 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
443 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
445 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
446 rte_eth_link_get_nowait(dev->port_id, &dev->link);
448 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
449 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
451 dev->flags = NETDEV_UP | NETDEV_PROMISC;
455 static struct netdev_dpdk *
456 netdev_dpdk_cast(const struct netdev *netdev)
458 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
461 static struct netdev *
462 netdev_dpdk_alloc(void)
464 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
469 netdev_dpdk_set_txq(struct netdev_dpdk *netdev, unsigned int n_txqs)
473 netdev->tx_q = dpdk_rte_mzalloc(n_txqs * sizeof *netdev->tx_q);
474 /* Each index is considered as a cpu core id, since there should
475 * be one tx queue for each cpu core. */
476 for (i = 0; i < n_txqs; i++) {
477 int core_id = ovs_numa_get_numa_id(i);
479 /* If the corresponding core is not on the same numa node
480 * as 'netdev', flags the 'flush_tx'. */
481 netdev->tx_q[i].flush_tx = netdev->socket_id == core_id;
486 netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no)
487 OVS_REQUIRES(dpdk_mutex)
489 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
492 ovs_mutex_init(&netdev->mutex);
494 ovs_mutex_lock(&netdev->mutex);
496 netdev->socket_id = rte_eth_dev_socket_id(port_no);
497 netdev_dpdk_set_txq(netdev, NR_QUEUE);
498 netdev->port_id = port_no;
500 netdev->mtu = ETHER_MTU;
501 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
503 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
504 if (!netdev->dpdk_mp) {
509 netdev_->n_txq = NR_QUEUE;
510 netdev_->n_rxq = NR_QUEUE;
511 err = dpdk_eth_dev_init(netdev);
516 list_push_back(&dpdk_list, &netdev->list_node);
520 rte_free(netdev->tx_q);
522 ovs_mutex_unlock(&netdev->mutex);
527 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
528 unsigned int *port_no)
532 if (strncmp(dev_name, prefix, strlen(prefix))) {
536 cport = dev_name + strlen(prefix);
537 *port_no = strtol(cport, 0, 0); /* string must be null terminated */
542 netdev_dpdk_construct(struct netdev *netdev)
544 unsigned int port_no;
547 if (rte_eal_init_ret) {
548 return rte_eal_init_ret;
551 /* Names always start with "dpdk" */
552 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
557 ovs_mutex_lock(&dpdk_mutex);
558 err = netdev_dpdk_init(netdev, port_no);
559 ovs_mutex_unlock(&dpdk_mutex);
564 netdev_dpdk_destruct(struct netdev *netdev_)
566 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
568 ovs_mutex_lock(&dev->mutex);
569 rte_eth_dev_stop(dev->port_id);
570 ovs_mutex_unlock(&dev->mutex);
572 ovs_mutex_lock(&dpdk_mutex);
574 list_remove(&dev->list_node);
575 dpdk_mp_put(dev->dpdk_mp);
576 ovs_mutex_unlock(&dpdk_mutex);
578 ovs_mutex_destroy(&dev->mutex);
582 netdev_dpdk_dealloc(struct netdev *netdev_)
584 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
590 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
592 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
594 ovs_mutex_lock(&dev->mutex);
596 smap_add_format(args, "configured_rx_queues", "%d", netdev_->n_rxq);
597 smap_add_format(args, "configured_tx_queues", "%d", netdev_->n_txq);
598 ovs_mutex_unlock(&dev->mutex);
604 netdev_dpdk_get_numa_id(const struct netdev *netdev_)
606 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
608 return netdev->socket_id;
611 /* Sets the number of tx queues and rx queues for the dpdk interface.
612 * If the configuration fails, do not try restoring its old configuration
613 * and just returns the error. */
615 netdev_dpdk_set_multiq(struct netdev *netdev_, unsigned int n_txq,
618 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
621 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
625 ovs_mutex_lock(&netdev->mutex);
626 rte_eth_dev_stop(netdev->port_id);
627 netdev->up.n_txq = n_txq;
628 netdev->up.n_rxq = n_rxq;
629 err = dpdk_eth_dev_init(netdev);
630 if (!err && netdev->up.n_txq != n_txq) {
631 rte_free(netdev->tx_q);
632 netdev_dpdk_set_txq(netdev, n_txq);
634 ovs_mutex_unlock(&netdev->mutex);
639 static struct netdev_rxq *
640 netdev_dpdk_rxq_alloc(void)
642 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
647 static struct netdev_rxq_dpdk *
648 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
650 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
654 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
656 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
657 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
659 ovs_mutex_lock(&netdev->mutex);
660 rx->port_id = netdev->port_id;
661 ovs_mutex_unlock(&netdev->mutex);
667 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
672 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
674 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
680 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
682 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
685 while (nb_tx != txq->count) {
688 ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
697 if (OVS_UNLIKELY(nb_tx != txq->count)) {
698 /* free buffers, which we couldn't transmit, one at a time (each
699 * packet could come from a different mempool) */
702 for (i = nb_tx; i < txq->count; i++) {
703 rte_pktmbuf_free_seg(txq->burst_pkts[i]);
705 ovs_mutex_lock(&dev->mutex);
706 dev->stats.tx_dropped += txq->count-nb_tx;
707 ovs_mutex_unlock(&dev->mutex);
711 txq->tsc = rte_get_timer_cycles();
715 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
717 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
719 if (txq->count == 0) {
722 dpdk_queue_flush__(dev, qid);
726 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
729 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
730 struct netdev *netdev = rx->up.netdev;
731 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
734 /* There is only one tx queue for this core. Do not flush other
736 if (rxq_->queue_id == rte_lcore_id()) {
737 dpdk_queue_flush(dev, rxq_->queue_id);
740 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
741 (struct rte_mbuf **) packets,
742 MIN((int)NETDEV_MAX_RX_BATCH,
743 (int)MAX_RX_QUEUE_LEN));
754 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
755 struct rte_mbuf **pkts, int cnt)
757 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
763 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
764 int tocopy = MIN(freeslots, cnt-i);
766 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
767 tocopy * sizeof (struct rte_mbuf *));
769 txq->count += tocopy;
772 if (txq->count == MAX_TX_QUEUE_LEN || txq->flush_tx) {
773 dpdk_queue_flush__(dev, qid);
775 diff_tsc = rte_get_timer_cycles() - txq->tsc;
776 if (diff_tsc >= DRAIN_TSC) {
777 dpdk_queue_flush__(dev, qid);
782 /* Tx function. Transmit packets indefinitely */
784 dpdk_do_tx_copy(struct netdev *netdev, struct dpif_packet ** pkts, int cnt)
785 OVS_NO_THREAD_SAFETY_ANALYSIS
787 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
788 struct rte_mbuf *mbufs[cnt];
793 /* If we are on a non pmd thread we have to use the mempool mutex, because
794 * every non pmd thread shares the same mempool cache */
796 if (!thread_is_pmd()) {
797 ovs_mutex_lock(&nonpmd_mempool_mutex);
800 for (i = 0; i < cnt; i++) {
801 int size = ofpbuf_size(&pkts[i]->ofpbuf);
803 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
804 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
805 (int)size , dev->max_packet_len);
811 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
813 if (!mbufs[newcnt]) {
818 /* We have to do a copy for now */
819 memcpy(mbufs[newcnt]->pkt.data, ofpbuf_data(&pkts[i]->ofpbuf), size);
821 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
822 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
827 if (OVS_UNLIKELY(dropped)) {
828 ovs_mutex_lock(&dev->mutex);
829 dev->stats.tx_dropped += dropped;
830 ovs_mutex_unlock(&dev->mutex);
833 dpdk_queue_pkts(dev, NON_PMD_THREAD_TX_QUEUE, mbufs, newcnt);
834 dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
836 if (!thread_is_pmd()) {
837 ovs_mutex_unlock(&nonpmd_mempool_mutex);
842 netdev_dpdk_send(struct netdev *netdev, int qid, struct dpif_packet **pkts,
843 int cnt, bool may_steal)
845 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
849 if (!may_steal || pkts[0]->ofpbuf.source != OFPBUF_DPDK) {
850 dpdk_do_tx_copy(netdev, pkts, cnt);
853 for (i = 0; i < cnt; i++) {
854 dpif_packet_delete(pkts[i]);
861 for (i = 0; i < cnt; i++) {
862 int size = ofpbuf_size(&pkts[i]->ofpbuf);
863 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
864 if (next_tx_idx != i) {
865 dpdk_queue_pkts(dev, qid,
866 (struct rte_mbuf **)&pkts[next_tx_idx],
870 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
871 (int)size , dev->max_packet_len);
873 dpif_packet_delete(pkts[i]);
878 if (next_tx_idx != cnt) {
879 dpdk_queue_pkts(dev, qid,
880 (struct rte_mbuf **)&pkts[next_tx_idx],
884 if (OVS_UNLIKELY(dropped)) {
885 ovs_mutex_lock(&dev->mutex);
886 dev->stats.tx_dropped += dropped;
887 ovs_mutex_unlock(&dev->mutex);
896 netdev_dpdk_set_etheraddr(struct netdev *netdev,
897 const uint8_t mac[ETH_ADDR_LEN])
899 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
901 ovs_mutex_lock(&dev->mutex);
902 if (!eth_addr_equals(dev->hwaddr, mac)) {
903 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
904 netdev_change_seq_changed(netdev);
906 ovs_mutex_unlock(&dev->mutex);
912 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
913 uint8_t mac[ETH_ADDR_LEN])
915 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
917 ovs_mutex_lock(&dev->mutex);
918 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
919 ovs_mutex_unlock(&dev->mutex);
925 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
927 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
929 ovs_mutex_lock(&dev->mutex);
931 ovs_mutex_unlock(&dev->mutex);
937 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
939 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
941 struct dpdk_mp *old_mp;
944 ovs_mutex_lock(&dpdk_mutex);
945 ovs_mutex_lock(&dev->mutex);
946 if (dev->mtu == mtu) {
951 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
957 rte_eth_dev_stop(dev->port_id);
960 old_mp = dev->dpdk_mp;
963 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
965 err = dpdk_eth_dev_init(dev);
969 dev->dpdk_mp = old_mp;
970 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
971 dpdk_eth_dev_init(dev);
976 netdev_change_seq_changed(netdev);
978 ovs_mutex_unlock(&dev->mutex);
979 ovs_mutex_unlock(&dpdk_mutex);
984 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
987 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
989 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
990 struct rte_eth_stats rte_stats;
993 netdev_dpdk_get_carrier(netdev, &gg);
994 ovs_mutex_lock(&dev->mutex);
995 rte_eth_stats_get(dev->port_id, &rte_stats);
997 memset(stats, 0, sizeof(*stats));
999 stats->rx_packets = rte_stats.ipackets;
1000 stats->tx_packets = rte_stats.opackets;
1001 stats->rx_bytes = rte_stats.ibytes;
1002 stats->tx_bytes = rte_stats.obytes;
1003 stats->rx_errors = rte_stats.ierrors;
1004 stats->tx_errors = rte_stats.oerrors;
1005 stats->multicast = rte_stats.imcasts;
1007 stats->tx_dropped = dev->stats.tx_dropped;
1008 ovs_mutex_unlock(&dev->mutex);
1014 netdev_dpdk_get_features(const struct netdev *netdev_,
1015 enum netdev_features *current,
1016 enum netdev_features *advertised OVS_UNUSED,
1017 enum netdev_features *supported OVS_UNUSED,
1018 enum netdev_features *peer OVS_UNUSED)
1020 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1021 struct rte_eth_link link;
1023 ovs_mutex_lock(&dev->mutex);
1025 ovs_mutex_unlock(&dev->mutex);
1027 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1028 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1029 *current = NETDEV_F_AUTONEG;
1031 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1032 if (link.link_speed == ETH_LINK_SPEED_10) {
1033 *current = NETDEV_F_10MB_HD;
1035 if (link.link_speed == ETH_LINK_SPEED_100) {
1036 *current = NETDEV_F_100MB_HD;
1038 if (link.link_speed == ETH_LINK_SPEED_1000) {
1039 *current = NETDEV_F_1GB_HD;
1041 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1042 if (link.link_speed == ETH_LINK_SPEED_10) {
1043 *current = NETDEV_F_10MB_FD;
1045 if (link.link_speed == ETH_LINK_SPEED_100) {
1046 *current = NETDEV_F_100MB_FD;
1048 if (link.link_speed == ETH_LINK_SPEED_1000) {
1049 *current = NETDEV_F_1GB_FD;
1051 if (link.link_speed == ETH_LINK_SPEED_10000) {
1052 *current = NETDEV_F_10GB_FD;
1060 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1062 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1065 ovs_mutex_lock(&dev->mutex);
1066 ifindex = dev->port_id;
1067 ovs_mutex_unlock(&dev->mutex);
1073 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1075 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1077 ovs_mutex_lock(&dev->mutex);
1078 check_link_status(dev);
1079 *carrier = dev->link.link_status;
1080 ovs_mutex_unlock(&dev->mutex);
1085 static long long int
1086 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1088 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1089 long long int carrier_resets;
1091 ovs_mutex_lock(&dev->mutex);
1092 carrier_resets = dev->link_reset_cnt;
1093 ovs_mutex_unlock(&dev->mutex);
1095 return carrier_resets;
1099 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1100 long long int interval OVS_UNUSED)
1106 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1107 enum netdev_flags off, enum netdev_flags on,
1108 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1112 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1116 *old_flagsp = dev->flags;
1120 if (dev->flags == *old_flagsp) {
1124 if (dev->flags & NETDEV_UP) {
1125 err = rte_eth_dev_start(dev->port_id);
1130 if (dev->flags & NETDEV_PROMISC) {
1131 rte_eth_promiscuous_enable(dev->port_id);
1134 if (!(dev->flags & NETDEV_UP)) {
1135 rte_eth_dev_stop(dev->port_id);
1142 netdev_dpdk_update_flags(struct netdev *netdev_,
1143 enum netdev_flags off, enum netdev_flags on,
1144 enum netdev_flags *old_flagsp)
1146 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1149 ovs_mutex_lock(&netdev->mutex);
1150 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1151 ovs_mutex_unlock(&netdev->mutex);
1157 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1159 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1160 struct rte_eth_dev_info dev_info;
1162 if (dev->port_id < 0)
1165 ovs_mutex_lock(&dev->mutex);
1166 rte_eth_dev_info_get(dev->port_id, &dev_info);
1167 ovs_mutex_unlock(&dev->mutex);
1169 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1171 smap_add_format(args, "port_no", "%d", dev->port_id);
1172 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1173 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1174 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1175 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1176 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1177 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1178 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1179 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1180 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1181 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1183 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1184 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1190 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1191 OVS_REQUIRES(dev->mutex)
1193 enum netdev_flags old_flags;
1196 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1198 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1203 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1204 const char *argv[], void *aux OVS_UNUSED)
1208 if (!strcasecmp(argv[argc - 1], "up")) {
1210 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1213 unixctl_command_reply_error(conn, "Invalid Admin State");
1218 struct netdev *netdev = netdev_from_name(argv[1]);
1219 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1220 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1222 ovs_mutex_lock(&dpdk_dev->mutex);
1223 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1224 ovs_mutex_unlock(&dpdk_dev->mutex);
1226 netdev_close(netdev);
1228 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1229 netdev_close(netdev);
1233 struct netdev_dpdk *netdev;
1235 ovs_mutex_lock(&dpdk_mutex);
1236 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1237 ovs_mutex_lock(&netdev->mutex);
1238 netdev_dpdk_set_admin_state__(netdev, up);
1239 ovs_mutex_unlock(&netdev->mutex);
1241 ovs_mutex_unlock(&dpdk_mutex);
1243 unixctl_command_reply(conn, "OK");
1247 dpdk_common_init(void)
1249 unixctl_command_register("netdev-dpdk/set-admin-state",
1250 "[netdev] up|down", 1, 2,
1251 netdev_dpdk_set_admin_state, NULL);
1253 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1257 dpdk_class_init(void)
1261 result = rte_eal_pci_probe();
1263 VLOG_ERR("Cannot probe PCI");
1267 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1275 dpdk_ring_create(const char dev_name[], unsigned int port_no,
1276 unsigned int *eth_port_id)
1278 struct dpdk_ring *ivshmem;
1282 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1283 if (ivshmem == NULL) {
1287 err = snprintf(ring_name, 10, "%s_tx", dev_name);
1292 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, 0);
1293 if (ivshmem->cring_tx == NULL) {
1298 err = snprintf(ring_name, 10, "%s_rx", dev_name);
1303 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, 0);
1304 if (ivshmem->cring_rx == NULL) {
1309 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
1310 &ivshmem->cring_tx, 1, SOCKET0);
1317 ivshmem->user_port_id = port_no;
1318 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1319 list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1321 *eth_port_id = ivshmem->eth_port_id;
1326 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1328 struct dpdk_ring *ivshmem;
1329 unsigned int port_no;
1332 /* Names always start with "dpdkr" */
1333 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1338 /* look through our list to find the device */
1339 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1340 if (ivshmem->user_port_id == port_no) {
1341 VLOG_INFO("Found dpdk ring device %s:\n", dev_name);
1342 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1346 /* Need to create the device rings */
1347 return dpdk_ring_create(dev_name, port_no, eth_port_id);
1351 netdev_dpdk_ring_construct(struct netdev *netdev)
1353 unsigned int port_no = 0;
1356 if (rte_eal_init_ret) {
1357 return rte_eal_init_ret;
1360 ovs_mutex_lock(&dpdk_mutex);
1362 err = dpdk_ring_open(netdev->name, &port_no);
1367 err = netdev_dpdk_init(netdev, port_no);
1370 ovs_mutex_unlock(&dpdk_mutex);
1374 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, MULTIQ) \
1378 NULL, /* netdev_dpdk_run */ \
1379 NULL, /* netdev_dpdk_wait */ \
1381 netdev_dpdk_alloc, \
1383 netdev_dpdk_destruct, \
1384 netdev_dpdk_dealloc, \
1385 netdev_dpdk_get_config, \
1386 NULL, /* netdev_dpdk_set_config */ \
1387 NULL, /* get_tunnel_config */ \
1388 netdev_dpdk_get_numa_id, /* get_numa_id */ \
1389 MULTIQ, /* set_multiq */ \
1391 netdev_dpdk_send, /* send */ \
1392 NULL, /* send_wait */ \
1394 netdev_dpdk_set_etheraddr, \
1395 netdev_dpdk_get_etheraddr, \
1396 netdev_dpdk_get_mtu, \
1397 netdev_dpdk_set_mtu, \
1398 netdev_dpdk_get_ifindex, \
1399 netdev_dpdk_get_carrier, \
1400 netdev_dpdk_get_carrier_resets, \
1401 netdev_dpdk_set_miimon, \
1402 netdev_dpdk_get_stats, \
1403 netdev_dpdk_get_features, \
1404 NULL, /* set_advertisements */ \
1406 NULL, /* set_policing */ \
1407 NULL, /* get_qos_types */ \
1408 NULL, /* get_qos_capabilities */ \
1409 NULL, /* get_qos */ \
1410 NULL, /* set_qos */ \
1411 NULL, /* get_queue */ \
1412 NULL, /* set_queue */ \
1413 NULL, /* delete_queue */ \
1414 NULL, /* get_queue_stats */ \
1415 NULL, /* queue_dump_start */ \
1416 NULL, /* queue_dump_next */ \
1417 NULL, /* queue_dump_done */ \
1418 NULL, /* dump_queue_stats */ \
1420 NULL, /* get_in4 */ \
1421 NULL, /* set_in4 */ \
1422 NULL, /* get_in6 */ \
1423 NULL, /* add_router */ \
1424 NULL, /* get_next_hop */ \
1425 netdev_dpdk_get_status, \
1426 NULL, /* arp_lookup */ \
1428 netdev_dpdk_update_flags, \
1430 netdev_dpdk_rxq_alloc, \
1431 netdev_dpdk_rxq_construct, \
1432 netdev_dpdk_rxq_destruct, \
1433 netdev_dpdk_rxq_dealloc, \
1434 netdev_dpdk_rxq_recv, \
1435 NULL, /* rx_wait */ \
1436 NULL, /* rxq_drain */ \
1440 dpdk_init(int argc, char **argv)
1444 if (argc < 2 || strcmp(argv[1], "--dpdk"))
1447 /* Make sure program name passed to rte_eal_init() is vswitchd. */
1453 /* Make sure things are initialized ... */
1454 result = rte_eal_init(argc, argv);
1456 ovs_abort(result, "Cannot init EAL\n");
1459 rte_memzone_dump(stdout);
1460 rte_eal_init_ret = 0;
1462 if (argc > result) {
1463 argv[result] = argv[0];
1466 /* We are called from the main thread here */
1467 thread_set_nonpmd();
1472 const struct netdev_class dpdk_class =
1476 netdev_dpdk_construct,
1477 netdev_dpdk_set_multiq);
1479 const struct netdev_class dpdk_ring_class =
1483 netdev_dpdk_ring_construct,
1487 netdev_dpdk_register(void)
1489 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1491 if (rte_eal_init_ret) {
1495 if (ovsthread_once_start(&once)) {
1497 netdev_register_provider(&dpdk_class);
1498 netdev_register_provider(&dpdk_ring_class);
1499 ovsthread_once_done(&once);
1504 pmd_thread_setaffinity_cpu(int cpu)
1510 CPU_SET(cpu, &cpuset);
1511 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1513 VLOG_ERR("Thread affinity error %d",err);
1516 /* lcore_id 0 is reseved for use by non pmd threads. */
1518 RTE_PER_LCORE(_lcore_id) = cpu;
1524 thread_set_nonpmd(void)
1526 /* We have to use 0 to allow non pmd threads to perform certain DPDK
1527 * operations, like rte_eth_dev_configure(). */
1528 RTE_PER_LCORE(_lcore_id) = 0;
1534 return rte_lcore_id() != 0;