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 "connectivity.h"
32 #include "dpif-netdev.h"
34 #include "netdev-dpdk.h"
35 #include "netdev-provider.h"
36 #include "netdev-vport.h"
38 #include "ofp-print.h"
40 #include "ovs-thread.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 /* TODO: 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 /* TODO: Needs per NIC value for these constants. */
76 #define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
77 #define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
78 #define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
80 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
81 #define TX_HTHRESH 0 /* Default values of TX host threshold reg. */
82 #define TX_WTHRESH 0 /* Default values of TX write-back threshold reg. */
84 static const struct rte_eth_conf port_conf = {
86 .mq_mode = ETH_MQ_RX_RSS,
88 .header_split = 0, /* Header Split disabled */
89 .hw_ip_checksum = 0, /* IP checksum offload disabled */
90 .hw_vlan_filter = 0, /* VLAN filtering disabled */
91 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
97 .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6,
101 .mq_mode = ETH_MQ_TX_NONE,
105 static const struct rte_eth_rxconf rx_conf = {
107 .pthresh = RX_PTHRESH,
108 .hthresh = RX_HTHRESH,
109 .wthresh = RX_WTHRESH,
113 static const struct rte_eth_txconf tx_conf = {
115 .pthresh = TX_PTHRESH,
116 .hthresh = TX_HTHRESH,
117 .wthresh = TX_WTHRESH,
123 enum { MAX_RX_QUEUE_LEN = 64 };
124 enum { MAX_TX_QUEUE_LEN = 64 };
125 enum { DRAIN_TSC = 200000ULL };
127 static int rte_eal_init_ret = ENODEV;
129 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
131 /* Contains all 'struct dpdk_dev's. */
132 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
133 = LIST_INITIALIZER(&dpdk_list);
135 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
136 = LIST_INITIALIZER(&dpdk_mp_list);
138 static pthread_t watchdog_thread;
141 struct rte_mempool *mp;
145 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
148 struct dpdk_tx_queue {
149 rte_spinlock_t tx_lock;
152 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
160 struct dpdk_tx_queue tx_q[NR_QUEUE];
162 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
164 struct dpdk_mp *dpdk_mp;
168 struct netdev_stats stats_offset;
169 struct netdev_stats stats;
171 uint8_t hwaddr[ETH_ADDR_LEN];
172 enum netdev_flags flags;
174 struct rte_eth_link link;
178 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
181 struct netdev_rxq_dpdk {
182 struct netdev_rxq up;
186 static int netdev_dpdk_construct(struct netdev *);
189 is_dpdk_class(const struct netdev_class *class)
191 return class->construct == netdev_dpdk_construct;
194 /* TODO: use dpdk malloc for entire OVS. infact huge page shld be used
195 * for all other sengments data, bss and text. */
198 dpdk_rte_mzalloc(size_t sz)
202 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
210 free_dpdk_buf(struct ofpbuf *b)
212 struct rte_mbuf *pkt;
219 rte_mempool_put(pkt->pool, pkt);
222 static struct dpdk_mp *
223 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
225 struct dpdk_mp *dmp = NULL;
226 char mp_name[RTE_MEMPOOL_NAMESIZE];
228 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
229 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
235 dmp = dpdk_rte_mzalloc(sizeof *dmp);
236 dmp->socket_id = socket_id;
240 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d", dmp->mtu);
241 dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
243 sizeof(struct rte_pktmbuf_pool_private),
244 rte_pktmbuf_pool_init, NULL,
245 rte_pktmbuf_init, NULL,
248 if (dmp->mp == NULL) {
252 list_push_back(&dpdk_mp_list, &dmp->list_node);
257 dpdk_mp_put(struct dpdk_mp *dmp)
265 ovs_assert(dmp->refcount >= 0);
268 /* I could not find any API to destroy mp. */
269 if (dmp->refcount == 0) {
270 list_delete(dmp->list_node);
271 /* destroy mp-pool. */
277 check_link_status(struct netdev_dpdk *dev)
279 struct rte_eth_link link;
281 rte_eth_link_get_nowait(dev->port_id, &link);
283 if (dev->link.link_status != link.link_status) {
284 seq_change(connectivity_seq_get());
286 dev->link_reset_cnt++;
288 if (dev->link.link_status) {
289 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
290 dev->port_id, (unsigned)dev->link.link_speed,
291 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
292 ("full-duplex") : ("half-duplex"));
294 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
300 dpdk_watchdog(void *dummy OVS_UNUSED)
302 struct netdev_dpdk *dev;
304 pthread_detach(pthread_self());
307 ovs_mutex_lock(&dpdk_mutex);
308 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
309 ovs_mutex_lock(&dev->mutex);
310 check_link_status(dev);
311 ovs_mutex_unlock(&dev->mutex);
313 ovs_mutex_unlock(&dpdk_mutex);
314 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
321 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
323 struct rte_pktmbuf_pool_private *mbp_priv;
324 struct ether_addr eth_addr;
328 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
332 diag = rte_eth_dev_configure(dev->port_id, NR_QUEUE, NR_QUEUE, &port_conf);
334 VLOG_ERR("eth dev config error %d",diag);
338 for (i = 0; i < NR_QUEUE; i++) {
339 diag = rte_eth_tx_queue_setup(dev->port_id, i, 64, 0, &tx_conf);
341 VLOG_ERR("eth dev tx queue setup error %d",diag);
346 for (i = 0; i < NR_QUEUE; i++) {
347 diag = rte_eth_rx_queue_setup(dev->port_id, i, 64, 0, &rx_conf,
350 VLOG_ERR("eth dev rx queue setup error %d",diag);
355 diag = rte_eth_dev_start(dev->port_id);
357 VLOG_ERR("eth dev start error %d",diag);
361 rte_eth_promiscuous_enable(dev->port_id);
362 rte_eth_allmulticast_enable(dev->port_id);
364 memset(ð_addr, 0x0, sizeof(eth_addr));
365 rte_eth_macaddr_get(dev->port_id, ð_addr);
366 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
367 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
369 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
370 rte_eth_link_get_nowait(dev->port_id, &dev->link);
372 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
373 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
375 dev->flags = NETDEV_UP | NETDEV_PROMISC;
379 static struct netdev_dpdk *
380 netdev_dpdk_cast(const struct netdev *netdev)
382 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
385 static struct netdev *
386 netdev_dpdk_alloc(void)
388 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
393 netdev_dpdk_construct(struct netdev *netdev_)
395 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
396 unsigned int port_no;
401 if (rte_eal_init_ret) {
402 return rte_eal_init_ret;
405 ovs_mutex_lock(&dpdk_mutex);
406 cport = netdev_->name + 4; /* Names always start with "dpdk" */
408 if (strncmp(netdev_->name, "dpdk", 4)) {
413 port_no = strtol(cport, 0, 0); /* string must be null terminated */
415 for (i = 0; i < NR_QUEUE; i++) {
416 rte_spinlock_init(&netdev->tx_q[i].tx_lock);
419 ovs_mutex_init(&netdev->mutex);
421 ovs_mutex_lock(&netdev->mutex);
424 netdev->mtu = ETHER_MTU;
425 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
427 /* TODO: need to discover device node at run time. */
428 netdev->socket_id = SOCKET0;
429 netdev->port_id = port_no;
431 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
432 if (!netdev->dpdk_mp) {
437 err = dpdk_eth_dev_init(netdev);
441 netdev_->n_rxq = NR_QUEUE;
443 list_push_back(&dpdk_list, &netdev->list_node);
446 ovs_mutex_unlock(&netdev->mutex);
448 ovs_mutex_unlock(&dpdk_mutex);
453 netdev_dpdk_destruct(struct netdev *netdev_)
455 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
457 ovs_mutex_lock(&dev->mutex);
458 rte_eth_dev_stop(dev->port_id);
459 ovs_mutex_unlock(&dev->mutex);
461 ovs_mutex_lock(&dpdk_mutex);
462 list_remove(&dev->list_node);
463 dpdk_mp_put(dev->dpdk_mp);
464 ovs_mutex_unlock(&dpdk_mutex);
466 ovs_mutex_destroy(&dev->mutex);
470 netdev_dpdk_dealloc(struct netdev *netdev_)
472 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
478 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
480 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
482 ovs_mutex_lock(&dev->mutex);
484 /* TODO: Allow to configure number of queues. */
485 smap_add_format(args, "configured_rx_queues", "%u", netdev_->n_rxq);
486 smap_add_format(args, "configured_tx_queues", "%u", netdev_->n_rxq);
487 ovs_mutex_unlock(&dev->mutex);
492 static struct netdev_rxq *
493 netdev_dpdk_rxq_alloc(void)
495 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
500 static struct netdev_rxq_dpdk *
501 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
503 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
507 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
509 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
510 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
512 ovs_mutex_lock(&netdev->mutex);
513 rx->port_id = netdev->port_id;
514 ovs_mutex_unlock(&netdev->mutex);
520 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
525 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
527 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
533 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
535 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
538 if (txq->count == 0) {
541 rte_spinlock_lock(&txq->tx_lock);
542 nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
543 if (nb_tx != txq->count) {
544 /* free buffers if we couldn't transmit packets */
545 rte_mempool_put_bulk(dev->dpdk_mp->mp,
546 (void **) &txq->burst_pkts[nb_tx],
547 (txq->count - nb_tx));
550 rte_spinlock_unlock(&txq->tx_lock);
553 inline static struct ofpbuf *
554 build_ofpbuf(struct rte_mbuf *pkt)
561 ofpbuf_set_data(b, pkt->pkt.data);
562 ofpbuf_set_base(b, (char *)ofpbuf_data(b) - DP_NETDEV_HEADROOM - VLAN_ETH_HEADER_LEN);
563 b->allocated = pkt->buf_len;
564 ofpbuf_set_size(b, rte_pktmbuf_data_len(pkt));
565 b->source = OFPBUF_DPDK;
573 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **packet, int *c)
575 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
576 struct netdev *netdev = rx->up.netdev;
577 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
578 struct rte_mbuf *burst_pkts[MAX_RX_QUEUE_LEN];
582 dpdk_queue_flush(dev, rxq_->queue_id);
584 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
585 burst_pkts, MAX_RX_QUEUE_LEN);
590 for (i = 0; i < nb_rx; i++) {
591 packet[i] = build_ofpbuf(burst_pkts[i]);
600 dpdk_queue_pkt(struct netdev_dpdk *dev, int qid,
601 struct rte_mbuf *pkt)
603 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
608 rte_spinlock_lock(&txq->tx_lock);
609 txq->burst_pkts[txq->count++] = pkt;
610 if (txq->count == MAX_TX_QUEUE_LEN) {
613 cur_tsc = rte_get_timer_cycles();
614 if (txq->count == 1) {
617 diff_tsc = cur_tsc - txq->tsc;
618 if (diff_tsc >= DRAIN_TSC) {
621 rte_spinlock_unlock(&txq->tx_lock);
625 nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
626 if (nb_tx != txq->count) {
627 /* free buffers if we couldn't transmit packets */
628 rte_mempool_put_bulk(dev->dpdk_mp->mp,
629 (void **) &txq->burst_pkts[nb_tx],
630 (txq->count - nb_tx));
633 rte_spinlock_unlock(&txq->tx_lock);
636 /* Tx function. Transmit packets indefinitely */
638 dpdk_do_tx_copy(struct netdev *netdev, char *buf, int size)
640 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
641 struct rte_mbuf *pkt;
643 pkt = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
645 ovs_mutex_lock(&dev->mutex);
646 dev->stats.tx_dropped++;
647 ovs_mutex_unlock(&dev->mutex);
651 /* We have to do a copy for now */
652 memcpy(pkt->pkt.data, buf, size);
654 rte_pktmbuf_data_len(pkt) = size;
655 rte_pktmbuf_pkt_len(pkt) = size;
657 dpdk_queue_pkt(dev, NON_PMD_THREAD_TX_QUEUE, pkt);
658 dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
662 netdev_dpdk_send(struct netdev *netdev,
663 struct ofpbuf *ofpbuf, bool may_steal)
665 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
668 if (ofpbuf_size(ofpbuf) > dev->max_packet_len) {
669 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
670 (int)ofpbuf_size(ofpbuf) , dev->max_packet_len);
672 ovs_mutex_lock(&dev->mutex);
673 dev->stats.tx_dropped++;
674 ovs_mutex_unlock(&dev->mutex);
680 rte_prefetch0(&ofpbuf->private_p);
682 !ofpbuf->private_p || ofpbuf->source != OFPBUF_DPDK) {
683 dpdk_do_tx_copy(netdev, (char *) ofpbuf_data(ofpbuf), ofpbuf_size(ofpbuf));
685 struct rte_mbuf *pkt;
688 pkt = ofpbuf->private_p;
689 ofpbuf->private_p = NULL;
690 rte_pktmbuf_data_len(pkt) = ofpbuf_size(ofpbuf);
691 rte_pktmbuf_pkt_len(pkt) = ofpbuf_size(ofpbuf);
693 qid = rte_lcore_id() % NR_QUEUE;
695 dpdk_queue_pkt(dev, qid, pkt);
697 ofpbuf->private_p = NULL;
703 ofpbuf_delete(ofpbuf);
710 netdev_dpdk_set_etheraddr(struct netdev *netdev,
711 const uint8_t mac[ETH_ADDR_LEN])
713 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
715 ovs_mutex_lock(&dev->mutex);
716 if (!eth_addr_equals(dev->hwaddr, mac)) {
717 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
719 ovs_mutex_unlock(&dev->mutex);
725 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
726 uint8_t mac[ETH_ADDR_LEN])
728 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
730 ovs_mutex_lock(&dev->mutex);
731 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
732 ovs_mutex_unlock(&dev->mutex);
738 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
740 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
742 ovs_mutex_lock(&dev->mutex);
744 ovs_mutex_unlock(&dev->mutex);
750 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
752 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
754 struct dpdk_mp *old_mp;
757 ovs_mutex_lock(&dpdk_mutex);
758 ovs_mutex_lock(&dev->mutex);
759 if (dev->mtu == mtu) {
764 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
770 rte_eth_dev_stop(dev->port_id);
773 old_mp = dev->dpdk_mp;
776 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
778 err = dpdk_eth_dev_init(dev);
783 dev->dpdk_mp = old_mp;
784 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
785 dpdk_eth_dev_init(dev);
791 ovs_mutex_unlock(&dev->mutex);
792 ovs_mutex_unlock(&dpdk_mutex);
797 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
800 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
802 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
803 struct rte_eth_stats rte_stats;
806 netdev_dpdk_get_carrier(netdev, &gg);
807 ovs_mutex_lock(&dev->mutex);
808 rte_eth_stats_get(dev->port_id, &rte_stats);
810 *stats = dev->stats_offset;
812 stats->rx_packets += rte_stats.ipackets;
813 stats->tx_packets += rte_stats.opackets;
814 stats->rx_bytes += rte_stats.ibytes;
815 stats->tx_bytes += rte_stats.obytes;
816 stats->rx_errors += rte_stats.ierrors;
817 stats->tx_errors += rte_stats.oerrors;
818 stats->multicast += rte_stats.imcasts;
820 stats->tx_dropped += dev->stats.tx_dropped;
821 ovs_mutex_unlock(&dev->mutex);
827 netdev_dpdk_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
829 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
831 ovs_mutex_lock(&dev->mutex);
832 dev->stats_offset = *stats;
833 ovs_mutex_unlock(&dev->mutex);
839 netdev_dpdk_get_features(const struct netdev *netdev_,
840 enum netdev_features *current,
841 enum netdev_features *advertised OVS_UNUSED,
842 enum netdev_features *supported OVS_UNUSED,
843 enum netdev_features *peer OVS_UNUSED)
845 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
846 struct rte_eth_link link;
848 ovs_mutex_lock(&dev->mutex);
850 ovs_mutex_unlock(&dev->mutex);
852 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
853 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
854 *current = NETDEV_F_AUTONEG;
856 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
857 if (link.link_speed == ETH_LINK_SPEED_10) {
858 *current = NETDEV_F_10MB_HD;
860 if (link.link_speed == ETH_LINK_SPEED_100) {
861 *current = NETDEV_F_100MB_HD;
863 if (link.link_speed == ETH_LINK_SPEED_1000) {
864 *current = NETDEV_F_1GB_HD;
866 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
867 if (link.link_speed == ETH_LINK_SPEED_10) {
868 *current = NETDEV_F_10MB_FD;
870 if (link.link_speed == ETH_LINK_SPEED_100) {
871 *current = NETDEV_F_100MB_FD;
873 if (link.link_speed == ETH_LINK_SPEED_1000) {
874 *current = NETDEV_F_1GB_FD;
876 if (link.link_speed == ETH_LINK_SPEED_10000) {
877 *current = NETDEV_F_10GB_FD;
885 netdev_dpdk_get_ifindex(const struct netdev *netdev)
887 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
890 ovs_mutex_lock(&dev->mutex);
891 ifindex = dev->port_id;
892 ovs_mutex_unlock(&dev->mutex);
898 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
900 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
902 ovs_mutex_lock(&dev->mutex);
903 check_link_status(dev);
904 *carrier = dev->link.link_status;
905 ovs_mutex_unlock(&dev->mutex);
911 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
913 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
914 long long int carrier_resets;
916 ovs_mutex_lock(&dev->mutex);
917 carrier_resets = dev->link_reset_cnt;
918 ovs_mutex_unlock(&dev->mutex);
920 return carrier_resets;
924 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
925 long long int interval OVS_UNUSED)
931 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
932 enum netdev_flags off, enum netdev_flags on,
933 enum netdev_flags *old_flagsp)
934 OVS_REQUIRES(dev->mutex)
938 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
942 *old_flagsp = dev->flags;
946 if (dev->flags == *old_flagsp) {
950 if (dev->flags & NETDEV_UP) {
951 err = rte_eth_dev_start(dev->port_id);
956 if (dev->flags & NETDEV_PROMISC) {
957 rte_eth_promiscuous_enable(dev->port_id);
960 if (!(dev->flags & NETDEV_UP)) {
961 rte_eth_dev_stop(dev->port_id);
968 netdev_dpdk_update_flags(struct netdev *netdev_,
969 enum netdev_flags off, enum netdev_flags on,
970 enum netdev_flags *old_flagsp)
972 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
975 ovs_mutex_lock(&netdev->mutex);
976 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
977 ovs_mutex_unlock(&netdev->mutex);
983 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
985 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
986 struct rte_eth_dev_info dev_info;
988 if (dev->port_id <= 0)
991 ovs_mutex_lock(&dev->mutex);
992 rte_eth_dev_info_get(dev->port_id, &dev_info);
993 ovs_mutex_unlock(&dev->mutex);
995 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
997 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
998 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
999 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1000 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1001 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1002 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1003 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1004 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1005 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1006 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1008 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1009 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1015 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1016 OVS_REQUIRES(dev->mutex)
1018 enum netdev_flags old_flags;
1021 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1023 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1028 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1029 const char *argv[], void *aux OVS_UNUSED)
1033 if (!strcasecmp(argv[argc - 1], "up")) {
1035 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1038 unixctl_command_reply_error(conn, "Invalid Admin State");
1043 struct netdev *netdev = netdev_from_name(argv[1]);
1044 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1045 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1047 ovs_mutex_lock(&dpdk_dev->mutex);
1048 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1049 ovs_mutex_unlock(&dpdk_dev->mutex);
1051 netdev_close(netdev);
1053 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1054 netdev_close(netdev);
1058 struct netdev_dpdk *netdev;
1060 ovs_mutex_lock(&dpdk_mutex);
1061 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1062 ovs_mutex_lock(&netdev->mutex);
1063 netdev_dpdk_set_admin_state__(netdev, up);
1064 ovs_mutex_unlock(&netdev->mutex);
1066 ovs_mutex_unlock(&dpdk_mutex);
1068 unixctl_command_reply(conn, "OK");
1072 dpdk_class_init(void)
1076 if (rte_eal_init_ret) {
1080 result = rte_pmd_init_all();
1082 VLOG_ERR("Cannot init PMD");
1086 result = rte_eal_pci_probe();
1088 VLOG_ERR("Cannot probe PCI");
1092 if (rte_eth_dev_count() < 1) {
1093 VLOG_ERR("No Ethernet devices found. Try assigning ports to UIO.");
1096 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1098 list_init(&dpdk_list);
1099 list_init(&dpdk_mp_list);
1101 unixctl_command_register("netdev-dpdk/set-admin-state",
1102 "[netdev] up|down", 1, 2,
1103 netdev_dpdk_set_admin_state, NULL);
1105 xpthread_create(&watchdog_thread, NULL, dpdk_watchdog, NULL);
1109 static struct netdev_class netdev_dpdk_class = {
1111 dpdk_class_init, /* init */
1112 NULL, /* netdev_dpdk_run */
1113 NULL, /* netdev_dpdk_wait */
1116 netdev_dpdk_construct,
1117 netdev_dpdk_destruct,
1118 netdev_dpdk_dealloc,
1119 netdev_dpdk_get_config,
1120 NULL, /* netdev_dpdk_set_config */
1121 NULL, /* get_tunnel_config */
1123 netdev_dpdk_send, /* send */
1124 NULL, /* send_wait */
1126 netdev_dpdk_set_etheraddr,
1127 netdev_dpdk_get_etheraddr,
1128 netdev_dpdk_get_mtu,
1129 netdev_dpdk_set_mtu,
1130 netdev_dpdk_get_ifindex,
1131 netdev_dpdk_get_carrier,
1132 netdev_dpdk_get_carrier_resets,
1133 netdev_dpdk_set_miimon,
1134 netdev_dpdk_get_stats,
1135 netdev_dpdk_set_stats,
1136 netdev_dpdk_get_features,
1137 NULL, /* set_advertisements */
1139 NULL, /* set_policing */
1140 NULL, /* get_qos_types */
1141 NULL, /* get_qos_capabilities */
1144 NULL, /* get_queue */
1145 NULL, /* set_queue */
1146 NULL, /* delete_queue */
1147 NULL, /* get_queue_stats */
1148 NULL, /* queue_dump_start */
1149 NULL, /* queue_dump_next */
1150 NULL, /* queue_dump_done */
1151 NULL, /* dump_queue_stats */
1156 NULL, /* add_router */
1157 NULL, /* get_next_hop */
1158 netdev_dpdk_get_status,
1159 NULL, /* arp_lookup */
1161 netdev_dpdk_update_flags,
1163 netdev_dpdk_rxq_alloc,
1164 netdev_dpdk_rxq_construct,
1165 netdev_dpdk_rxq_destruct,
1166 netdev_dpdk_rxq_dealloc,
1167 netdev_dpdk_rxq_recv,
1168 NULL, /* rxq_wait */
1169 NULL, /* rxq_drain */
1173 dpdk_init(int argc, char **argv)
1177 if (strcmp(argv[1], "--dpdk"))
1183 /* Make sure things are initialized ... */
1184 result = rte_eal_init(argc, argv);
1186 ovs_abort(result, "Cannot init EAL\n");
1189 rte_eal_init_ret = 0;
1195 netdev_dpdk_register(void)
1197 netdev_register_provider(&netdev_dpdk_class);
1201 pmd_thread_setaffinity_cpu(int cpu)
1207 CPU_SET(cpu, &cpuset);
1208 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1210 VLOG_ERR("Thread affinity error %d",err);
1213 RTE_PER_LCORE(_lcore_id) = cpu;