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"
44 #include "unaligned.h"
49 VLOG_DEFINE_THIS_MODULE(dpdk);
50 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
52 #define DPDK_PORT_WATCHDOG_INTERVAL 5
54 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
55 #define OVS_VPORT_DPDK "ovs_dpdk"
58 * need to reserve tons of extra space in the mbufs so we can align the
59 * DMA addresses to 4KB.
62 #define MTU_TO_MAX_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
63 #define MBUF_SIZE(mtu) (MTU_TO_MAX_LEN(mtu) + (512) + \
64 sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
66 /* TODO: mempool size should be based on system resources. */
67 #define NB_MBUF (4096 * 64)
68 #define MP_CACHE_SZ (256 * 2)
71 #define NON_PMD_THREAD_TX_QUEUE 0
73 /* TODO: Needs per NIC value for these constants. */
74 #define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
75 #define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
76 #define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
78 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
79 #define TX_HTHRESH 0 /* Default values of TX host threshold reg. */
80 #define TX_WTHRESH 0 /* Default values of TX write-back threshold reg. */
82 static const struct rte_eth_conf port_conf = {
84 .mq_mode = ETH_MQ_RX_RSS,
86 .header_split = 0, /* Header Split disabled */
87 .hw_ip_checksum = 0, /* IP checksum offload disabled */
88 .hw_vlan_filter = 0, /* VLAN filtering disabled */
89 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
95 .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6,
99 .mq_mode = ETH_MQ_TX_NONE,
103 static const struct rte_eth_rxconf rx_conf = {
105 .pthresh = RX_PTHRESH,
106 .hthresh = RX_HTHRESH,
107 .wthresh = RX_WTHRESH,
111 static const struct rte_eth_txconf tx_conf = {
113 .pthresh = TX_PTHRESH,
114 .hthresh = TX_HTHRESH,
115 .wthresh = TX_WTHRESH,
121 enum { MAX_RX_QUEUE_LEN = 64 };
122 enum { MAX_TX_QUEUE_LEN = 64 };
123 enum { DRAIN_TSC = 200000ULL };
125 static int rte_eal_init_ret = ENODEV;
127 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
129 /* Contains all 'struct dpdk_dev's. */
130 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
131 = LIST_INITIALIZER(&dpdk_list);
133 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
134 = LIST_INITIALIZER(&dpdk_mp_list);
137 struct rte_mempool *mp;
141 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
144 struct dpdk_tx_queue {
145 rte_spinlock_t tx_lock;
148 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
156 struct dpdk_tx_queue tx_q[NR_QUEUE];
158 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
160 struct dpdk_mp *dpdk_mp;
164 struct netdev_stats stats_offset;
165 struct netdev_stats stats;
167 uint8_t hwaddr[ETH_ADDR_LEN];
168 enum netdev_flags flags;
170 struct rte_eth_link link;
174 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
177 struct netdev_rxq_dpdk {
178 struct netdev_rxq up;
182 static int netdev_dpdk_construct(struct netdev *);
185 is_dpdk_class(const struct netdev_class *class)
187 return class->construct == netdev_dpdk_construct;
190 /* TODO: use dpdk malloc for entire OVS. infact huge page shld be used
191 * for all other sengments data, bss and text. */
194 dpdk_rte_mzalloc(size_t sz)
198 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
206 free_dpdk_buf(struct ofpbuf *b)
208 struct rte_mbuf *pkt = (struct rte_mbuf *) b;
210 rte_mempool_put(pkt->pool, pkt);
214 __rte_pktmbuf_init(struct rte_mempool *mp,
215 void *opaque_arg OVS_UNUSED,
217 unsigned i OVS_UNUSED)
219 struct rte_mbuf *m = _m;
220 uint32_t buf_len = mp->elt_size - sizeof(struct ofpbuf);
222 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct ofpbuf));
224 memset(m, 0, mp->elt_size);
226 /* start of buffer is just after mbuf structure */
227 m->buf_addr = (char *)m + sizeof(struct ofpbuf);
228 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
229 sizeof(struct ofpbuf);
230 m->buf_len = (uint16_t)buf_len;
232 /* keep some headroom between start of buffer and data */
233 m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
235 /* init some constant fields */
236 m->type = RTE_MBUF_PKT;
239 m->pkt.in_port = 0xff;
243 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
244 void *opaque_arg OVS_UNUSED,
246 unsigned i OVS_UNUSED)
248 struct rte_mbuf *m = _m;
250 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
252 ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
255 static struct dpdk_mp *
256 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
258 struct dpdk_mp *dmp = NULL;
259 char mp_name[RTE_MEMPOOL_NAMESIZE];
261 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
262 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
268 dmp = dpdk_rte_mzalloc(sizeof *dmp);
269 dmp->socket_id = socket_id;
273 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d", dmp->mtu);
274 dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
276 sizeof(struct rte_pktmbuf_pool_private),
277 rte_pktmbuf_pool_init, NULL,
278 ovs_rte_pktmbuf_init, NULL,
281 if (dmp->mp == NULL) {
285 list_push_back(&dpdk_mp_list, &dmp->list_node);
290 dpdk_mp_put(struct dpdk_mp *dmp)
298 ovs_assert(dmp->refcount >= 0);
301 /* I could not find any API to destroy mp. */
302 if (dmp->refcount == 0) {
303 list_delete(dmp->list_node);
304 /* destroy mp-pool. */
310 check_link_status(struct netdev_dpdk *dev)
312 struct rte_eth_link link;
314 rte_eth_link_get_nowait(dev->port_id, &link);
316 if (dev->link.link_status != link.link_status) {
317 netdev_change_seq_changed(&dev->up);
319 dev->link_reset_cnt++;
321 if (dev->link.link_status) {
322 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
323 dev->port_id, (unsigned)dev->link.link_speed,
324 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
325 ("full-duplex") : ("half-duplex"));
327 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
333 dpdk_watchdog(void *dummy OVS_UNUSED)
335 struct netdev_dpdk *dev;
337 pthread_detach(pthread_self());
340 ovs_mutex_lock(&dpdk_mutex);
341 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
342 ovs_mutex_lock(&dev->mutex);
343 check_link_status(dev);
344 ovs_mutex_unlock(&dev->mutex);
346 ovs_mutex_unlock(&dpdk_mutex);
347 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
354 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
356 struct rte_pktmbuf_pool_private *mbp_priv;
357 struct ether_addr eth_addr;
361 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
365 diag = rte_eth_dev_configure(dev->port_id, NR_QUEUE, NR_QUEUE, &port_conf);
367 VLOG_ERR("eth dev config error %d",diag);
371 for (i = 0; i < NR_QUEUE; i++) {
372 diag = rte_eth_tx_queue_setup(dev->port_id, i, MAX_TX_QUEUE_LEN, 0,
375 VLOG_ERR("eth dev tx queue setup error %d",diag);
380 for (i = 0; i < NR_QUEUE; i++) {
381 diag = rte_eth_rx_queue_setup(dev->port_id, i, MAX_RX_QUEUE_LEN, 0,
382 &rx_conf, dev->dpdk_mp->mp);
384 VLOG_ERR("eth dev rx queue setup error %d",diag);
389 diag = rte_eth_dev_start(dev->port_id);
391 VLOG_ERR("eth dev start error %d",diag);
395 rte_eth_promiscuous_enable(dev->port_id);
396 rte_eth_allmulticast_enable(dev->port_id);
398 memset(ð_addr, 0x0, sizeof(eth_addr));
399 rte_eth_macaddr_get(dev->port_id, ð_addr);
400 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
401 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
403 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
404 rte_eth_link_get_nowait(dev->port_id, &dev->link);
406 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
407 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
409 dev->flags = NETDEV_UP | NETDEV_PROMISC;
413 static struct netdev_dpdk *
414 netdev_dpdk_cast(const struct netdev *netdev)
416 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
419 static struct netdev *
420 netdev_dpdk_alloc(void)
422 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
427 netdev_dpdk_construct(struct netdev *netdev_)
429 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
430 unsigned int port_no;
435 if (rte_eal_init_ret) {
436 return rte_eal_init_ret;
439 ovs_mutex_lock(&dpdk_mutex);
440 cport = netdev_->name + 4; /* Names always start with "dpdk" */
442 if (strncmp(netdev_->name, "dpdk", 4)) {
447 port_no = strtol(cport, 0, 0); /* string must be null terminated */
449 for (i = 0; i < NR_QUEUE; i++) {
450 rte_spinlock_init(&netdev->tx_q[i].tx_lock);
453 ovs_mutex_init(&netdev->mutex);
455 ovs_mutex_lock(&netdev->mutex);
458 netdev->mtu = ETHER_MTU;
459 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
461 /* TODO: need to discover device node at run time. */
462 netdev->socket_id = SOCKET0;
463 netdev->port_id = port_no;
465 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
466 if (!netdev->dpdk_mp) {
471 err = dpdk_eth_dev_init(netdev);
475 netdev_->n_rxq = NR_QUEUE;
477 list_push_back(&dpdk_list, &netdev->list_node);
480 ovs_mutex_unlock(&netdev->mutex);
482 ovs_mutex_unlock(&dpdk_mutex);
487 netdev_dpdk_destruct(struct netdev *netdev_)
489 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
491 ovs_mutex_lock(&dev->mutex);
492 rte_eth_dev_stop(dev->port_id);
493 ovs_mutex_unlock(&dev->mutex);
495 ovs_mutex_lock(&dpdk_mutex);
496 list_remove(&dev->list_node);
497 dpdk_mp_put(dev->dpdk_mp);
498 ovs_mutex_unlock(&dpdk_mutex);
500 ovs_mutex_destroy(&dev->mutex);
504 netdev_dpdk_dealloc(struct netdev *netdev_)
506 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
512 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
514 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
516 ovs_mutex_lock(&dev->mutex);
518 /* TODO: Allow to configure number of queues. */
519 smap_add_format(args, "configured_rx_queues", "%u", netdev_->n_rxq);
520 smap_add_format(args, "configured_tx_queues", "%u", netdev_->n_rxq);
521 ovs_mutex_unlock(&dev->mutex);
526 static struct netdev_rxq *
527 netdev_dpdk_rxq_alloc(void)
529 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
534 static struct netdev_rxq_dpdk *
535 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
537 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
541 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
543 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
544 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
546 ovs_mutex_lock(&netdev->mutex);
547 rx->port_id = netdev->port_id;
548 ovs_mutex_unlock(&netdev->mutex);
554 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
559 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
561 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
567 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
569 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
572 if (txq->count == 0) {
575 rte_spinlock_lock(&txq->tx_lock);
576 nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
577 if (nb_tx != txq->count) {
578 /* free buffers if we couldn't transmit packets */
579 rte_mempool_put_bulk(dev->dpdk_mp->mp,
580 (void **) &txq->burst_pkts[nb_tx],
581 (txq->count - nb_tx));
584 rte_spinlock_unlock(&txq->tx_lock);
588 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **packets, int *c)
590 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
591 struct netdev *netdev = rx->up.netdev;
592 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
595 dpdk_queue_flush(dev, rxq_->queue_id);
597 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
598 (struct rte_mbuf **) packets, MAX_RX_QUEUE_LEN);
609 dpdk_queue_pkt(struct netdev_dpdk *dev, int qid,
610 struct rte_mbuf *pkt)
612 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
617 rte_spinlock_lock(&txq->tx_lock);
618 txq->burst_pkts[txq->count++] = pkt;
619 if (txq->count == MAX_TX_QUEUE_LEN) {
622 cur_tsc = rte_get_timer_cycles();
623 if (txq->count == 1) {
626 diff_tsc = cur_tsc - txq->tsc;
627 if (diff_tsc >= DRAIN_TSC) {
630 rte_spinlock_unlock(&txq->tx_lock);
634 nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
635 if (nb_tx != txq->count) {
636 /* free buffers if we couldn't transmit packets */
637 rte_mempool_put_bulk(dev->dpdk_mp->mp,
638 (void **) &txq->burst_pkts[nb_tx],
639 (txq->count - nb_tx));
642 rte_spinlock_unlock(&txq->tx_lock);
645 /* Tx function. Transmit packets indefinitely */
647 dpdk_do_tx_copy(struct netdev *netdev, char *buf, int size)
649 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
650 struct rte_mbuf *pkt;
652 pkt = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
654 ovs_mutex_lock(&dev->mutex);
655 dev->stats.tx_dropped++;
656 ovs_mutex_unlock(&dev->mutex);
660 /* We have to do a copy for now */
661 memcpy(pkt->pkt.data, buf, size);
663 rte_pktmbuf_data_len(pkt) = size;
664 rte_pktmbuf_pkt_len(pkt) = size;
666 dpdk_queue_pkt(dev, NON_PMD_THREAD_TX_QUEUE, pkt);
667 dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
671 netdev_dpdk_send(struct netdev *netdev,
672 struct ofpbuf *ofpbuf, bool may_steal)
674 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
677 if (ofpbuf_size(ofpbuf) > dev->max_packet_len) {
678 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
679 (int)ofpbuf_size(ofpbuf) , dev->max_packet_len);
681 ovs_mutex_lock(&dev->mutex);
682 dev->stats.tx_dropped++;
683 ovs_mutex_unlock(&dev->mutex);
689 if (!may_steal || ofpbuf->source != OFPBUF_DPDK) {
690 dpdk_do_tx_copy(netdev, (char *) ofpbuf_data(ofpbuf), ofpbuf_size(ofpbuf));
693 ofpbuf_delete(ofpbuf);
698 qid = rte_lcore_id() % NR_QUEUE;
700 dpdk_queue_pkt(dev, qid, (struct rte_mbuf *)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);
718 netdev_change_seq_changed(netdev);
720 ovs_mutex_unlock(&dev->mutex);
726 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
727 uint8_t mac[ETH_ADDR_LEN])
729 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
731 ovs_mutex_lock(&dev->mutex);
732 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
733 ovs_mutex_unlock(&dev->mutex);
739 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
741 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
743 ovs_mutex_lock(&dev->mutex);
745 ovs_mutex_unlock(&dev->mutex);
751 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
753 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
755 struct dpdk_mp *old_mp;
758 ovs_mutex_lock(&dpdk_mutex);
759 ovs_mutex_lock(&dev->mutex);
760 if (dev->mtu == mtu) {
765 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
771 rte_eth_dev_stop(dev->port_id);
774 old_mp = dev->dpdk_mp;
777 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
779 err = dpdk_eth_dev_init(dev);
784 dev->dpdk_mp = old_mp;
785 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
786 dpdk_eth_dev_init(dev);
791 netdev_change_seq_changed(netdev);
793 ovs_mutex_unlock(&dev->mutex);
794 ovs_mutex_unlock(&dpdk_mutex);
799 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
802 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
804 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
805 struct rte_eth_stats rte_stats;
808 netdev_dpdk_get_carrier(netdev, &gg);
809 ovs_mutex_lock(&dev->mutex);
810 rte_eth_stats_get(dev->port_id, &rte_stats);
812 *stats = dev->stats_offset;
814 stats->rx_packets += rte_stats.ipackets;
815 stats->tx_packets += rte_stats.opackets;
816 stats->rx_bytes += rte_stats.ibytes;
817 stats->tx_bytes += rte_stats.obytes;
818 stats->rx_errors += rte_stats.ierrors;
819 stats->tx_errors += rte_stats.oerrors;
820 stats->multicast += rte_stats.imcasts;
822 stats->tx_dropped += dev->stats.tx_dropped;
823 ovs_mutex_unlock(&dev->mutex);
829 netdev_dpdk_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
831 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
833 ovs_mutex_lock(&dev->mutex);
834 dev->stats_offset = *stats;
835 ovs_mutex_unlock(&dev->mutex);
841 netdev_dpdk_get_features(const struct netdev *netdev_,
842 enum netdev_features *current,
843 enum netdev_features *advertised OVS_UNUSED,
844 enum netdev_features *supported OVS_UNUSED,
845 enum netdev_features *peer OVS_UNUSED)
847 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
848 struct rte_eth_link link;
850 ovs_mutex_lock(&dev->mutex);
852 ovs_mutex_unlock(&dev->mutex);
854 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
855 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
856 *current = NETDEV_F_AUTONEG;
858 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
859 if (link.link_speed == ETH_LINK_SPEED_10) {
860 *current = NETDEV_F_10MB_HD;
862 if (link.link_speed == ETH_LINK_SPEED_100) {
863 *current = NETDEV_F_100MB_HD;
865 if (link.link_speed == ETH_LINK_SPEED_1000) {
866 *current = NETDEV_F_1GB_HD;
868 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
869 if (link.link_speed == ETH_LINK_SPEED_10) {
870 *current = NETDEV_F_10MB_FD;
872 if (link.link_speed == ETH_LINK_SPEED_100) {
873 *current = NETDEV_F_100MB_FD;
875 if (link.link_speed == ETH_LINK_SPEED_1000) {
876 *current = NETDEV_F_1GB_FD;
878 if (link.link_speed == ETH_LINK_SPEED_10000) {
879 *current = NETDEV_F_10GB_FD;
887 netdev_dpdk_get_ifindex(const struct netdev *netdev)
889 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
892 ovs_mutex_lock(&dev->mutex);
893 ifindex = dev->port_id;
894 ovs_mutex_unlock(&dev->mutex);
900 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
902 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
904 ovs_mutex_lock(&dev->mutex);
905 check_link_status(dev);
906 *carrier = dev->link.link_status;
907 ovs_mutex_unlock(&dev->mutex);
913 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
915 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
916 long long int carrier_resets;
918 ovs_mutex_lock(&dev->mutex);
919 carrier_resets = dev->link_reset_cnt;
920 ovs_mutex_unlock(&dev->mutex);
922 return carrier_resets;
926 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
927 long long int interval OVS_UNUSED)
933 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
934 enum netdev_flags off, enum netdev_flags on,
935 enum netdev_flags *old_flagsp)
936 OVS_REQUIRES(dev->mutex)
940 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
944 *old_flagsp = dev->flags;
948 if (dev->flags == *old_flagsp) {
952 if (dev->flags & NETDEV_UP) {
953 err = rte_eth_dev_start(dev->port_id);
958 if (dev->flags & NETDEV_PROMISC) {
959 rte_eth_promiscuous_enable(dev->port_id);
962 if (!(dev->flags & NETDEV_UP)) {
963 rte_eth_dev_stop(dev->port_id);
970 netdev_dpdk_update_flags(struct netdev *netdev_,
971 enum netdev_flags off, enum netdev_flags on,
972 enum netdev_flags *old_flagsp)
974 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
977 ovs_mutex_lock(&netdev->mutex);
978 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
979 ovs_mutex_unlock(&netdev->mutex);
985 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
987 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
988 struct rte_eth_dev_info dev_info;
990 if (dev->port_id <= 0)
993 ovs_mutex_lock(&dev->mutex);
994 rte_eth_dev_info_get(dev->port_id, &dev_info);
995 ovs_mutex_unlock(&dev->mutex);
997 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
999 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1000 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1001 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1002 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1003 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1004 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1005 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1006 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1007 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1008 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1010 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1011 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1017 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1018 OVS_REQUIRES(dev->mutex)
1020 enum netdev_flags old_flags;
1023 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1025 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1030 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1031 const char *argv[], void *aux OVS_UNUSED)
1035 if (!strcasecmp(argv[argc - 1], "up")) {
1037 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1040 unixctl_command_reply_error(conn, "Invalid Admin State");
1045 struct netdev *netdev = netdev_from_name(argv[1]);
1046 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1047 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1049 ovs_mutex_lock(&dpdk_dev->mutex);
1050 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1051 ovs_mutex_unlock(&dpdk_dev->mutex);
1053 netdev_close(netdev);
1055 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1056 netdev_close(netdev);
1060 struct netdev_dpdk *netdev;
1062 ovs_mutex_lock(&dpdk_mutex);
1063 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1064 ovs_mutex_lock(&netdev->mutex);
1065 netdev_dpdk_set_admin_state__(netdev, up);
1066 ovs_mutex_unlock(&netdev->mutex);
1068 ovs_mutex_unlock(&dpdk_mutex);
1070 unixctl_command_reply(conn, "OK");
1074 dpdk_class_init(void)
1078 if (rte_eal_init_ret) {
1082 result = rte_pmd_init_all();
1084 VLOG_ERR("Cannot init PMD");
1088 result = rte_eal_pci_probe();
1090 VLOG_ERR("Cannot probe PCI");
1094 if (rte_eth_dev_count() < 1) {
1095 VLOG_ERR("No Ethernet devices found. Try assigning ports to UIO.");
1098 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1100 list_init(&dpdk_list);
1101 list_init(&dpdk_mp_list);
1103 unixctl_command_register("netdev-dpdk/set-admin-state",
1104 "[netdev] up|down", 1, 2,
1105 netdev_dpdk_set_admin_state, NULL);
1107 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1111 static struct netdev_class netdev_dpdk_class = {
1113 dpdk_class_init, /* init */
1114 NULL, /* netdev_dpdk_run */
1115 NULL, /* netdev_dpdk_wait */
1118 netdev_dpdk_construct,
1119 netdev_dpdk_destruct,
1120 netdev_dpdk_dealloc,
1121 netdev_dpdk_get_config,
1122 NULL, /* netdev_dpdk_set_config */
1123 NULL, /* get_tunnel_config */
1125 netdev_dpdk_send, /* send */
1126 NULL, /* send_wait */
1128 netdev_dpdk_set_etheraddr,
1129 netdev_dpdk_get_etheraddr,
1130 netdev_dpdk_get_mtu,
1131 netdev_dpdk_set_mtu,
1132 netdev_dpdk_get_ifindex,
1133 netdev_dpdk_get_carrier,
1134 netdev_dpdk_get_carrier_resets,
1135 netdev_dpdk_set_miimon,
1136 netdev_dpdk_get_stats,
1137 netdev_dpdk_set_stats,
1138 netdev_dpdk_get_features,
1139 NULL, /* set_advertisements */
1141 NULL, /* set_policing */
1142 NULL, /* get_qos_types */
1143 NULL, /* get_qos_capabilities */
1146 NULL, /* get_queue */
1147 NULL, /* set_queue */
1148 NULL, /* delete_queue */
1149 NULL, /* get_queue_stats */
1150 NULL, /* queue_dump_start */
1151 NULL, /* queue_dump_next */
1152 NULL, /* queue_dump_done */
1153 NULL, /* dump_queue_stats */
1158 NULL, /* add_router */
1159 NULL, /* get_next_hop */
1160 netdev_dpdk_get_status,
1161 NULL, /* arp_lookup */
1163 netdev_dpdk_update_flags,
1165 netdev_dpdk_rxq_alloc,
1166 netdev_dpdk_rxq_construct,
1167 netdev_dpdk_rxq_destruct,
1168 netdev_dpdk_rxq_dealloc,
1169 netdev_dpdk_rxq_recv,
1170 NULL, /* rxq_wait */
1171 NULL, /* rxq_drain */
1175 dpdk_init(int argc, char **argv)
1179 if (strcmp(argv[1], "--dpdk"))
1185 /* Make sure things are initialized ... */
1186 result = rte_eal_init(argc, argv);
1188 ovs_abort(result, "Cannot init EAL\n");
1191 rte_eal_init_ret = 0;
1197 netdev_dpdk_register(void)
1199 netdev_register_provider(&netdev_dpdk_class);
1203 pmd_thread_setaffinity_cpu(int cpu)
1209 CPU_SET(cpu, &cpuset);
1210 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1212 VLOG_ERR("Thread affinity error %d",err);
1215 RTE_PER_LCORE(_lcore_id) = cpu;