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"
39 #include "ovs-thread.h"
41 #include "packet-dpif.h"
45 #include "unaligned.h"
50 VLOG_DEFINE_THIS_MODULE(dpdk);
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
53 #define DPDK_PORT_WATCHDOG_INTERVAL 5
55 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
56 #define OVS_VPORT_DPDK "ovs_dpdk"
59 * need to reserve tons of extra space in the mbufs so we can align the
60 * DMA addresses to 4KB.
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)
67 /* TODO: mempool size should be based on system resources. */
68 #define NB_MBUF (4096 * 64)
69 #define MP_CACHE_SZ (256 * 2)
72 #define NON_PMD_THREAD_TX_QUEUE 0
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)*/
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. */
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. */
86 static const struct rte_eth_conf port_conf = {
88 .mq_mode = ETH_MQ_RX_RSS,
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 */
99 .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6,
103 .mq_mode = ETH_MQ_TX_NONE,
107 static const struct rte_eth_rxconf rx_conf = {
109 .pthresh = RX_PTHRESH,
110 .hthresh = RX_HTHRESH,
111 .wthresh = RX_WTHRESH,
115 static const struct rte_eth_txconf tx_conf = {
117 .pthresh = TX_PTHRESH,
118 .hthresh = TX_HTHRESH,
119 .wthresh = TX_WTHRESH,
123 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS|ETH_TXQ_FLAGS_NOOFFLOADS,
126 enum { MAX_RX_QUEUE_LEN = 64 };
127 enum { MAX_TX_QUEUE_LEN = 64 };
128 enum { DRAIN_TSC = 200000ULL };
130 static int rte_eal_init_ret = ENODEV;
132 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
134 /* Contains all 'struct dpdk_dev's. */
135 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
136 = LIST_INITIALIZER(&dpdk_list);
138 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
139 = LIST_INITIALIZER(&dpdk_mp_list);
142 struct rte_mempool *mp;
146 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
149 struct dpdk_tx_queue {
150 rte_spinlock_t tx_lock;
153 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
161 struct dpdk_tx_queue tx_q[NR_QUEUE];
163 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
165 struct dpdk_mp *dpdk_mp;
169 struct netdev_stats stats_offset;
170 struct netdev_stats stats;
172 uint8_t hwaddr[ETH_ADDR_LEN];
173 enum netdev_flags flags;
175 struct rte_eth_link link;
179 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
182 struct netdev_rxq_dpdk {
183 struct netdev_rxq up;
187 static int netdev_dpdk_construct(struct netdev *);
190 is_dpdk_class(const struct netdev_class *class)
192 return class->construct == netdev_dpdk_construct;
195 /* TODO: use dpdk malloc for entire OVS. infact huge page shld be used
196 * for all other sengments data, bss and text. */
199 dpdk_rte_mzalloc(size_t sz)
203 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
211 free_dpdk_buf(struct dpif_packet *p)
213 struct ofpbuf *buf = &p->ofpbuf;
214 struct rte_mbuf *pkt = (struct rte_mbuf *) buf->dpdk_buf;
216 rte_mempool_put(pkt->pool, pkt);
220 __rte_pktmbuf_init(struct rte_mempool *mp,
221 void *opaque_arg OVS_UNUSED,
223 unsigned i OVS_UNUSED)
225 struct rte_mbuf *m = _m;
226 uint32_t buf_len = mp->elt_size - sizeof(struct dpif_packet);
228 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dpif_packet));
230 memset(m, 0, mp->elt_size);
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;
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);
241 /* init some constant fields */
242 m->type = RTE_MBUF_PKT;
245 m->pkt.in_port = 0xff;
249 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
250 void *opaque_arg OVS_UNUSED,
252 unsigned i OVS_UNUSED)
254 struct rte_mbuf *m = _m;
256 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
258 ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
261 static struct dpdk_mp *
262 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
264 struct dpdk_mp *dmp = NULL;
265 char mp_name[RTE_MEMPOOL_NAMESIZE];
267 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
268 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
274 dmp = dpdk_rte_mzalloc(sizeof *dmp);
275 dmp->socket_id = socket_id;
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),
282 sizeof(struct rte_pktmbuf_pool_private),
283 rte_pktmbuf_pool_init, NULL,
284 ovs_rte_pktmbuf_init, NULL,
287 if (dmp->mp == NULL) {
291 list_push_back(&dpdk_mp_list, &dmp->list_node);
296 dpdk_mp_put(struct dpdk_mp *dmp)
304 ovs_assert(dmp->refcount >= 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. */
316 check_link_status(struct netdev_dpdk *dev)
318 struct rte_eth_link link;
320 rte_eth_link_get_nowait(dev->port_id, &link);
322 if (dev->link.link_status != link.link_status) {
323 netdev_change_seq_changed(&dev->up);
325 dev->link_reset_cnt++;
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"));
333 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
339 dpdk_watchdog(void *dummy OVS_UNUSED)
341 struct netdev_dpdk *dev;
343 pthread_detach(pthread_self());
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);
352 ovs_mutex_unlock(&dpdk_mutex);
353 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
360 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
362 struct rte_pktmbuf_pool_private *mbp_priv;
363 struct ether_addr eth_addr;
367 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
371 diag = rte_eth_dev_configure(dev->port_id, NR_QUEUE, NR_QUEUE, &port_conf);
373 VLOG_ERR("eth dev config error %d",diag);
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);
381 VLOG_ERR("eth dev tx queue setup error %d",diag);
386 for (i = 0; i < NR_QUEUE; i++) {
387 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
389 &rx_conf, dev->dpdk_mp->mp);
391 VLOG_ERR("eth dev rx queue setup error %d",diag);
396 diag = rte_eth_dev_start(dev->port_id);
398 VLOG_ERR("eth dev start error %d",diag);
402 rte_eth_promiscuous_enable(dev->port_id);
403 rte_eth_allmulticast_enable(dev->port_id);
405 memset(ð_addr, 0x0, sizeof(eth_addr));
406 rte_eth_macaddr_get(dev->port_id, ð_addr);
407 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
408 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
410 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
411 rte_eth_link_get_nowait(dev->port_id, &dev->link);
413 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
414 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
416 dev->flags = NETDEV_UP | NETDEV_PROMISC;
420 static struct netdev_dpdk *
421 netdev_dpdk_cast(const struct netdev *netdev)
423 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
426 static struct netdev *
427 netdev_dpdk_alloc(void)
429 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
434 netdev_dpdk_construct(struct netdev *netdev_)
436 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
437 unsigned int port_no;
442 if (rte_eal_init_ret) {
443 return rte_eal_init_ret;
446 ovs_mutex_lock(&dpdk_mutex);
447 cport = netdev_->name + 4; /* Names always start with "dpdk" */
449 if (strncmp(netdev_->name, "dpdk", 4)) {
454 port_no = strtol(cport, 0, 0); /* string must be null terminated */
456 for (i = 0; i < NR_QUEUE; i++) {
457 rte_spinlock_init(&netdev->tx_q[i].tx_lock);
460 ovs_mutex_init(&netdev->mutex);
462 ovs_mutex_lock(&netdev->mutex);
465 netdev->mtu = ETHER_MTU;
466 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
468 /* TODO: need to discover device node at run time. */
469 netdev->socket_id = SOCKET0;
470 netdev->port_id = port_no;
472 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
473 if (!netdev->dpdk_mp) {
478 err = dpdk_eth_dev_init(netdev);
482 netdev_->n_rxq = NR_QUEUE;
484 list_push_back(&dpdk_list, &netdev->list_node);
487 ovs_mutex_unlock(&netdev->mutex);
489 ovs_mutex_unlock(&dpdk_mutex);
494 netdev_dpdk_destruct(struct netdev *netdev_)
496 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
498 ovs_mutex_lock(&dev->mutex);
499 rte_eth_dev_stop(dev->port_id);
500 ovs_mutex_unlock(&dev->mutex);
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);
507 ovs_mutex_destroy(&dev->mutex);
511 netdev_dpdk_dealloc(struct netdev *netdev_)
513 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
519 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
521 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
523 ovs_mutex_lock(&dev->mutex);
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);
533 static struct netdev_rxq *
534 netdev_dpdk_rxq_alloc(void)
536 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
541 static struct netdev_rxq_dpdk *
542 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
544 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
548 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
550 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
551 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
553 ovs_mutex_lock(&netdev->mutex);
554 rx->port_id = netdev->port_id;
555 ovs_mutex_unlock(&netdev->mutex);
561 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
566 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
568 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
574 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
576 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
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));
587 txq->tsc = rte_get_timer_cycles();
591 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
593 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
595 if (txq->count == 0) {
598 rte_spinlock_lock(&txq->tx_lock);
599 dpdk_queue_flush__(dev, qid);
600 rte_spinlock_unlock(&txq->tx_lock);
604 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
607 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
608 struct netdev *netdev = rx->up.netdev;
609 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
612 dpdk_queue_flush(dev, rxq_->queue_id);
614 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
615 (struct rte_mbuf **) packets,
616 MIN((int)NETDEV_MAX_RX_BATCH,
617 (int)MAX_RX_QUEUE_LEN));
628 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
629 struct rte_mbuf **pkts, int cnt)
631 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
636 rte_spinlock_lock(&txq->tx_lock);
638 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
639 int tocopy = MIN(freeslots, cnt-i);
641 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
642 tocopy * sizeof (struct rte_mbuf *));
644 txq->count += tocopy;
647 if (txq->count == MAX_TX_QUEUE_LEN) {
648 dpdk_queue_flush__(dev, qid);
650 diff_tsc = rte_get_timer_cycles() - txq->tsc;
651 if (diff_tsc >= DRAIN_TSC) {
652 dpdk_queue_flush__(dev, qid);
655 rte_spinlock_unlock(&txq->tx_lock);
658 /* Tx function. Transmit packets indefinitely */
660 dpdk_do_tx_copy(struct netdev *netdev, struct dpif_packet ** pkts, int cnt)
662 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
663 struct rte_mbuf *mbufs[cnt];
666 for (i = 0; i < cnt; i++) {
667 int size = ofpbuf_size(&pkts[i]->ofpbuf);
668 if (size > dev->max_packet_len) {
669 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
670 (int)size , dev->max_packet_len);
672 ovs_mutex_lock(&dev->mutex);
673 dev->stats.tx_dropped++;
674 ovs_mutex_unlock(&dev->mutex);
679 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
681 if (!mbufs[newcnt]) {
682 ovs_mutex_lock(&dev->mutex);
683 dev->stats.tx_dropped++;
684 ovs_mutex_unlock(&dev->mutex);
688 /* We have to do a copy for now */
689 memcpy(mbufs[newcnt]->pkt.data, ofpbuf_data(&pkts[i]->ofpbuf), size);
691 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
692 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
697 dpdk_queue_pkts(dev, NON_PMD_THREAD_TX_QUEUE, mbufs, newcnt);
698 dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
702 netdev_dpdk_send(struct netdev *netdev, struct dpif_packet **pkts, int cnt,
705 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
709 if (!may_steal || pkts[0]->ofpbuf.source != OFPBUF_DPDK) {
710 dpdk_do_tx_copy(netdev, pkts, cnt);
713 for (i = 0; i < cnt; i++) {
714 dpif_packet_delete(pkts[i]);
722 qid = rte_lcore_id() % NR_QUEUE;
724 for (i = 0; i < cnt; i++) {
725 int size = ofpbuf_size(&pkts[i]->ofpbuf);
726 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
727 if (next_tx_idx != i) {
728 dpdk_queue_pkts(dev, qid,
729 (struct rte_mbuf **)&pkts[next_tx_idx],
733 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
734 (int)size , dev->max_packet_len);
736 dpif_packet_delete(pkts[i]);
741 if (next_tx_idx != cnt) {
742 dpdk_queue_pkts(dev, qid,
743 (struct rte_mbuf **)&pkts[next_tx_idx],
747 if (OVS_UNLIKELY(dropped)) {
748 ovs_mutex_lock(&dev->mutex);
749 dev->stats.tx_dropped += dropped;
750 ovs_mutex_unlock(&dev->mutex);
759 netdev_dpdk_set_etheraddr(struct netdev *netdev,
760 const uint8_t mac[ETH_ADDR_LEN])
762 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
764 ovs_mutex_lock(&dev->mutex);
765 if (!eth_addr_equals(dev->hwaddr, mac)) {
766 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
767 netdev_change_seq_changed(netdev);
769 ovs_mutex_unlock(&dev->mutex);
775 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
776 uint8_t mac[ETH_ADDR_LEN])
778 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
780 ovs_mutex_lock(&dev->mutex);
781 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
782 ovs_mutex_unlock(&dev->mutex);
788 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
790 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
792 ovs_mutex_lock(&dev->mutex);
794 ovs_mutex_unlock(&dev->mutex);
800 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
802 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
804 struct dpdk_mp *old_mp;
807 ovs_mutex_lock(&dpdk_mutex);
808 ovs_mutex_lock(&dev->mutex);
809 if (dev->mtu == mtu) {
814 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
820 rte_eth_dev_stop(dev->port_id);
823 old_mp = dev->dpdk_mp;
826 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
828 err = dpdk_eth_dev_init(dev);
832 dev->dpdk_mp = old_mp;
833 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
834 dpdk_eth_dev_init(dev);
839 netdev_change_seq_changed(netdev);
841 ovs_mutex_unlock(&dev->mutex);
842 ovs_mutex_unlock(&dpdk_mutex);
847 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
850 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
852 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
853 struct rte_eth_stats rte_stats;
856 netdev_dpdk_get_carrier(netdev, &gg);
857 ovs_mutex_lock(&dev->mutex);
858 rte_eth_stats_get(dev->port_id, &rte_stats);
860 *stats = dev->stats_offset;
862 stats->rx_packets += rte_stats.ipackets;
863 stats->tx_packets += rte_stats.opackets;
864 stats->rx_bytes += rte_stats.ibytes;
865 stats->tx_bytes += rte_stats.obytes;
866 stats->rx_errors += rte_stats.ierrors;
867 stats->tx_errors += rte_stats.oerrors;
868 stats->multicast += rte_stats.imcasts;
870 stats->tx_dropped += dev->stats.tx_dropped;
871 ovs_mutex_unlock(&dev->mutex);
877 netdev_dpdk_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
879 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
881 ovs_mutex_lock(&dev->mutex);
882 dev->stats_offset = *stats;
883 ovs_mutex_unlock(&dev->mutex);
889 netdev_dpdk_get_features(const struct netdev *netdev_,
890 enum netdev_features *current,
891 enum netdev_features *advertised OVS_UNUSED,
892 enum netdev_features *supported OVS_UNUSED,
893 enum netdev_features *peer OVS_UNUSED)
895 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
896 struct rte_eth_link link;
898 ovs_mutex_lock(&dev->mutex);
900 ovs_mutex_unlock(&dev->mutex);
902 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
903 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
904 *current = NETDEV_F_AUTONEG;
906 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
907 if (link.link_speed == ETH_LINK_SPEED_10) {
908 *current = NETDEV_F_10MB_HD;
910 if (link.link_speed == ETH_LINK_SPEED_100) {
911 *current = NETDEV_F_100MB_HD;
913 if (link.link_speed == ETH_LINK_SPEED_1000) {
914 *current = NETDEV_F_1GB_HD;
916 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
917 if (link.link_speed == ETH_LINK_SPEED_10) {
918 *current = NETDEV_F_10MB_FD;
920 if (link.link_speed == ETH_LINK_SPEED_100) {
921 *current = NETDEV_F_100MB_FD;
923 if (link.link_speed == ETH_LINK_SPEED_1000) {
924 *current = NETDEV_F_1GB_FD;
926 if (link.link_speed == ETH_LINK_SPEED_10000) {
927 *current = NETDEV_F_10GB_FD;
935 netdev_dpdk_get_ifindex(const struct netdev *netdev)
937 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
940 ovs_mutex_lock(&dev->mutex);
941 ifindex = dev->port_id;
942 ovs_mutex_unlock(&dev->mutex);
948 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
950 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
952 ovs_mutex_lock(&dev->mutex);
953 check_link_status(dev);
954 *carrier = dev->link.link_status;
955 ovs_mutex_unlock(&dev->mutex);
961 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
963 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
964 long long int carrier_resets;
966 ovs_mutex_lock(&dev->mutex);
967 carrier_resets = dev->link_reset_cnt;
968 ovs_mutex_unlock(&dev->mutex);
970 return carrier_resets;
974 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
975 long long int interval OVS_UNUSED)
981 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
982 enum netdev_flags off, enum netdev_flags on,
983 enum netdev_flags *old_flagsp)
984 OVS_REQUIRES(dev->mutex)
988 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
992 *old_flagsp = dev->flags;
996 if (dev->flags == *old_flagsp) {
1000 if (dev->flags & NETDEV_UP) {
1001 err = rte_eth_dev_start(dev->port_id);
1006 if (dev->flags & NETDEV_PROMISC) {
1007 rte_eth_promiscuous_enable(dev->port_id);
1010 if (!(dev->flags & NETDEV_UP)) {
1011 rte_eth_dev_stop(dev->port_id);
1018 netdev_dpdk_update_flags(struct netdev *netdev_,
1019 enum netdev_flags off, enum netdev_flags on,
1020 enum netdev_flags *old_flagsp)
1022 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1025 ovs_mutex_lock(&netdev->mutex);
1026 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1027 ovs_mutex_unlock(&netdev->mutex);
1033 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1035 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1036 struct rte_eth_dev_info dev_info;
1038 if (dev->port_id <= 0)
1041 ovs_mutex_lock(&dev->mutex);
1042 rte_eth_dev_info_get(dev->port_id, &dev_info);
1043 ovs_mutex_unlock(&dev->mutex);
1045 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1047 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1048 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1049 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1050 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1051 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1052 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1053 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1054 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1055 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1056 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1058 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1059 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1065 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1066 OVS_REQUIRES(dev->mutex)
1068 enum netdev_flags old_flags;
1071 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1073 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1078 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1079 const char *argv[], void *aux OVS_UNUSED)
1083 if (!strcasecmp(argv[argc - 1], "up")) {
1085 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1088 unixctl_command_reply_error(conn, "Invalid Admin State");
1093 struct netdev *netdev = netdev_from_name(argv[1]);
1094 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1095 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1097 ovs_mutex_lock(&dpdk_dev->mutex);
1098 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1099 ovs_mutex_unlock(&dpdk_dev->mutex);
1101 netdev_close(netdev);
1103 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1104 netdev_close(netdev);
1108 struct netdev_dpdk *netdev;
1110 ovs_mutex_lock(&dpdk_mutex);
1111 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1112 ovs_mutex_lock(&netdev->mutex);
1113 netdev_dpdk_set_admin_state__(netdev, up);
1114 ovs_mutex_unlock(&netdev->mutex);
1116 ovs_mutex_unlock(&dpdk_mutex);
1118 unixctl_command_reply(conn, "OK");
1122 dpdk_class_init(void)
1126 if (rte_eal_init_ret) {
1130 result = rte_pmd_init_all();
1132 VLOG_ERR("Cannot init PMD");
1136 result = rte_eal_pci_probe();
1138 VLOG_ERR("Cannot probe PCI");
1142 if (rte_eth_dev_count() < 1) {
1143 VLOG_ERR("No Ethernet devices found. Try assigning ports to UIO.");
1146 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1148 list_init(&dpdk_list);
1149 list_init(&dpdk_mp_list);
1151 unixctl_command_register("netdev-dpdk/set-admin-state",
1152 "[netdev] up|down", 1, 2,
1153 netdev_dpdk_set_admin_state, NULL);
1155 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1159 static struct netdev_class netdev_dpdk_class = {
1161 dpdk_class_init, /* init */
1162 NULL, /* netdev_dpdk_run */
1163 NULL, /* netdev_dpdk_wait */
1166 netdev_dpdk_construct,
1167 netdev_dpdk_destruct,
1168 netdev_dpdk_dealloc,
1169 netdev_dpdk_get_config,
1170 NULL, /* netdev_dpdk_set_config */
1171 NULL, /* get_tunnel_config */
1173 netdev_dpdk_send, /* send */
1174 NULL, /* send_wait */
1176 netdev_dpdk_set_etheraddr,
1177 netdev_dpdk_get_etheraddr,
1178 netdev_dpdk_get_mtu,
1179 netdev_dpdk_set_mtu,
1180 netdev_dpdk_get_ifindex,
1181 netdev_dpdk_get_carrier,
1182 netdev_dpdk_get_carrier_resets,
1183 netdev_dpdk_set_miimon,
1184 netdev_dpdk_get_stats,
1185 netdev_dpdk_set_stats,
1186 netdev_dpdk_get_features,
1187 NULL, /* set_advertisements */
1189 NULL, /* set_policing */
1190 NULL, /* get_qos_types */
1191 NULL, /* get_qos_capabilities */
1194 NULL, /* get_queue */
1195 NULL, /* set_queue */
1196 NULL, /* delete_queue */
1197 NULL, /* get_queue_stats */
1198 NULL, /* queue_dump_start */
1199 NULL, /* queue_dump_next */
1200 NULL, /* queue_dump_done */
1201 NULL, /* dump_queue_stats */
1206 NULL, /* add_router */
1207 NULL, /* get_next_hop */
1208 netdev_dpdk_get_status,
1209 NULL, /* arp_lookup */
1211 netdev_dpdk_update_flags,
1213 netdev_dpdk_rxq_alloc,
1214 netdev_dpdk_rxq_construct,
1215 netdev_dpdk_rxq_destruct,
1216 netdev_dpdk_rxq_dealloc,
1217 netdev_dpdk_rxq_recv,
1218 NULL, /* rxq_wait */
1219 NULL, /* rxq_drain */
1223 dpdk_init(int argc, char **argv)
1227 if (argc < 2 || strcmp(argv[1], "--dpdk"))
1230 /* Make sure program name passed to rte_eal_init() is vswitchd. */
1236 /* Make sure things are initialized ... */
1237 result = rte_eal_init(argc, argv);
1239 ovs_abort(result, "Cannot init EAL\n");
1243 rte_eal_init_ret = 0;
1245 if (argc > result) {
1246 argv[result] = argv[0];
1253 netdev_dpdk_register(void)
1255 netdev_register_provider(&netdev_dpdk_class);
1259 pmd_thread_setaffinity_cpu(int cpu)
1265 CPU_SET(cpu, &cpuset);
1266 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1268 VLOG_ERR("Thread affinity error %d",err);
1271 RTE_PER_LCORE(_lcore_id) = cpu;