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];
156 /* dpdk has no way to remove dpdk ring ethernet devices
157 so we have to keep them around once they've been created
160 static struct list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
161 = LIST_INITIALIZER(&dpdk_ring_list);
164 /* For the client rings */
165 struct rte_ring *cring_tx;
166 struct rte_ring *cring_rx;
167 int user_port_id; /* User given port no, parsed from port name */
168 int eth_port_id; /* ethernet device port id */
169 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
177 struct dpdk_tx_queue tx_q[NR_QUEUE];
179 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
181 struct dpdk_mp *dpdk_mp;
185 struct netdev_stats stats_offset;
186 struct netdev_stats stats;
188 uint8_t hwaddr[ETH_ADDR_LEN];
189 enum netdev_flags flags;
191 struct rte_eth_link link;
195 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
198 struct netdev_rxq_dpdk {
199 struct netdev_rxq up;
203 static int netdev_dpdk_construct(struct netdev *);
206 is_dpdk_class(const struct netdev_class *class)
208 return class->construct == netdev_dpdk_construct;
211 /* TODO: use dpdk malloc for entire OVS. infact huge page shld be used
212 * for all other sengments data, bss and text. */
215 dpdk_rte_mzalloc(size_t sz)
219 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
227 free_dpdk_buf(struct dpif_packet *p)
229 struct ofpbuf *buf = &p->ofpbuf;
230 struct rte_mbuf *pkt = (struct rte_mbuf *) buf->dpdk_buf;
232 rte_mempool_put(pkt->pool, pkt);
236 __rte_pktmbuf_init(struct rte_mempool *mp,
237 void *opaque_arg OVS_UNUSED,
239 unsigned i OVS_UNUSED)
241 struct rte_mbuf *m = _m;
242 uint32_t buf_len = mp->elt_size - sizeof(struct dpif_packet);
244 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dpif_packet));
246 memset(m, 0, mp->elt_size);
248 /* start of buffer is just after mbuf structure */
249 m->buf_addr = (char *)m + sizeof(struct dpif_packet);
250 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
251 sizeof(struct dpif_packet);
252 m->buf_len = (uint16_t)buf_len;
254 /* keep some headroom between start of buffer and data */
255 m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
257 /* init some constant fields */
258 m->type = RTE_MBUF_PKT;
261 m->pkt.in_port = 0xff;
265 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
266 void *opaque_arg OVS_UNUSED,
268 unsigned i OVS_UNUSED)
270 struct rte_mbuf *m = _m;
272 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
274 ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
277 static struct dpdk_mp *
278 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
280 struct dpdk_mp *dmp = NULL;
281 char mp_name[RTE_MEMPOOL_NAMESIZE];
283 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
284 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
290 dmp = dpdk_rte_mzalloc(sizeof *dmp);
291 dmp->socket_id = socket_id;
295 if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d", dmp->mtu) < 0) {
299 dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
301 sizeof(struct rte_pktmbuf_pool_private),
302 rte_pktmbuf_pool_init, NULL,
303 ovs_rte_pktmbuf_init, NULL,
306 if (dmp->mp == NULL) {
310 list_push_back(&dpdk_mp_list, &dmp->list_node);
315 dpdk_mp_put(struct dpdk_mp *dmp)
323 ovs_assert(dmp->refcount >= 0);
326 /* I could not find any API to destroy mp. */
327 if (dmp->refcount == 0) {
328 list_delete(dmp->list_node);
329 /* destroy mp-pool. */
335 check_link_status(struct netdev_dpdk *dev)
337 struct rte_eth_link link;
339 rte_eth_link_get_nowait(dev->port_id, &link);
341 if (dev->link.link_status != link.link_status) {
342 netdev_change_seq_changed(&dev->up);
344 dev->link_reset_cnt++;
346 if (dev->link.link_status) {
347 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
348 dev->port_id, (unsigned)dev->link.link_speed,
349 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
350 ("full-duplex") : ("half-duplex"));
352 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
358 dpdk_watchdog(void *dummy OVS_UNUSED)
360 struct netdev_dpdk *dev;
362 pthread_detach(pthread_self());
365 ovs_mutex_lock(&dpdk_mutex);
366 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
367 ovs_mutex_lock(&dev->mutex);
368 check_link_status(dev);
369 ovs_mutex_unlock(&dev->mutex);
371 ovs_mutex_unlock(&dpdk_mutex);
372 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
379 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
381 struct rte_pktmbuf_pool_private *mbp_priv;
382 struct ether_addr eth_addr;
386 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
390 diag = rte_eth_dev_configure(dev->port_id, NR_QUEUE, NR_QUEUE, &port_conf);
392 VLOG_ERR("eth dev config error %d",diag);
396 for (i = 0; i < NR_QUEUE; i++) {
397 diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
398 dev->socket_id, &tx_conf);
400 VLOG_ERR("eth dev tx queue setup error %d",diag);
405 for (i = 0; i < NR_QUEUE; i++) {
406 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
408 &rx_conf, dev->dpdk_mp->mp);
410 VLOG_ERR("eth dev rx queue setup error %d",diag);
415 diag = rte_eth_dev_start(dev->port_id);
417 VLOG_ERR("eth dev start error %d",diag);
421 rte_eth_promiscuous_enable(dev->port_id);
422 rte_eth_allmulticast_enable(dev->port_id);
424 memset(ð_addr, 0x0, sizeof(eth_addr));
425 rte_eth_macaddr_get(dev->port_id, ð_addr);
426 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
427 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
429 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
430 rte_eth_link_get_nowait(dev->port_id, &dev->link);
432 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
433 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
435 dev->flags = NETDEV_UP | NETDEV_PROMISC;
439 static struct netdev_dpdk *
440 netdev_dpdk_cast(const struct netdev *netdev)
442 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
445 static struct netdev *
446 netdev_dpdk_alloc(void)
448 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
453 netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no) OVS_REQUIRES(dpdk_mutex)
455 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
459 ovs_mutex_init(&netdev->mutex);
461 ovs_mutex_lock(&netdev->mutex);
463 for (i = 0; i < NR_QUEUE; i++) {
464 rte_spinlock_init(&netdev->tx_q[i].tx_lock);
467 netdev->port_id = port_no;
470 netdev->mtu = ETHER_MTU;
471 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
473 /* TODO: need to discover device node at run time. */
474 netdev->socket_id = SOCKET0;
476 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
477 if (!netdev->dpdk_mp) {
482 err = dpdk_eth_dev_init(netdev);
486 netdev_->n_rxq = NR_QUEUE;
488 list_push_back(&dpdk_list, &netdev->list_node);
491 ovs_mutex_unlock(&netdev->mutex);
496 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
497 unsigned int *port_no)
501 if (strncmp(dev_name, prefix, strlen(prefix))) {
505 cport = dev_name + strlen(prefix);
506 *port_no = strtol(cport, 0, 0); /* string must be null terminated */
511 netdev_dpdk_construct(struct netdev *netdev)
513 unsigned int port_no;
516 if (rte_eal_init_ret) {
517 return rte_eal_init_ret;
520 /* Names always start with "dpdk" */
521 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
526 ovs_mutex_lock(&dpdk_mutex);
527 err = netdev_dpdk_init(netdev, port_no);
528 ovs_mutex_unlock(&dpdk_mutex);
533 netdev_dpdk_destruct(struct netdev *netdev_)
535 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
537 ovs_mutex_lock(&dev->mutex);
538 rte_eth_dev_stop(dev->port_id);
539 ovs_mutex_unlock(&dev->mutex);
541 ovs_mutex_lock(&dpdk_mutex);
542 list_remove(&dev->list_node);
543 dpdk_mp_put(dev->dpdk_mp);
544 ovs_mutex_unlock(&dpdk_mutex);
546 ovs_mutex_destroy(&dev->mutex);
550 netdev_dpdk_dealloc(struct netdev *netdev_)
552 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
558 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
560 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
562 ovs_mutex_lock(&dev->mutex);
564 /* TODO: Allow to configure number of queues. */
565 smap_add_format(args, "configured_rx_queues", "%u", netdev_->n_rxq);
566 smap_add_format(args, "configured_tx_queues", "%u", netdev_->n_rxq);
567 ovs_mutex_unlock(&dev->mutex);
572 static struct netdev_rxq *
573 netdev_dpdk_rxq_alloc(void)
575 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
580 static struct netdev_rxq_dpdk *
581 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
583 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
587 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
589 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
590 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
592 ovs_mutex_lock(&netdev->mutex);
593 rx->port_id = netdev->port_id;
594 ovs_mutex_unlock(&netdev->mutex);
600 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
605 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
607 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
613 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
615 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
618 nb_tx = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts, txq->count);
619 if (OVS_UNLIKELY(nb_tx != txq->count)) {
620 /* free buffers if we couldn't transmit packets */
621 rte_mempool_put_bulk(dev->dpdk_mp->mp,
622 (void **) &txq->burst_pkts[nb_tx],
623 (txq->count - nb_tx));
626 txq->tsc = rte_get_timer_cycles();
630 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
632 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
634 if (txq->count == 0) {
637 rte_spinlock_lock(&txq->tx_lock);
638 dpdk_queue_flush__(dev, qid);
639 rte_spinlock_unlock(&txq->tx_lock);
643 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
646 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
647 struct netdev *netdev = rx->up.netdev;
648 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
651 dpdk_queue_flush(dev, rxq_->queue_id);
653 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
654 (struct rte_mbuf **) packets,
655 MIN((int)NETDEV_MAX_RX_BATCH,
656 (int)MAX_RX_QUEUE_LEN));
667 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
668 struct rte_mbuf **pkts, int cnt)
670 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
675 rte_spinlock_lock(&txq->tx_lock);
677 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
678 int tocopy = MIN(freeslots, cnt-i);
680 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
681 tocopy * sizeof (struct rte_mbuf *));
683 txq->count += tocopy;
686 if (txq->count == MAX_TX_QUEUE_LEN) {
687 dpdk_queue_flush__(dev, qid);
689 diff_tsc = rte_get_timer_cycles() - txq->tsc;
690 if (diff_tsc >= DRAIN_TSC) {
691 dpdk_queue_flush__(dev, qid);
694 rte_spinlock_unlock(&txq->tx_lock);
697 /* Tx function. Transmit packets indefinitely */
699 dpdk_do_tx_copy(struct netdev *netdev, struct dpif_packet ** pkts, int cnt)
701 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
702 struct rte_mbuf *mbufs[cnt];
707 for (i = 0; i < cnt; i++) {
708 int size = ofpbuf_size(&pkts[i]->ofpbuf);
710 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
711 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
712 (int)size , dev->max_packet_len);
718 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
720 if (!mbufs[newcnt]) {
725 /* We have to do a copy for now */
726 memcpy(mbufs[newcnt]->pkt.data, ofpbuf_data(&pkts[i]->ofpbuf), size);
728 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
729 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
734 if (OVS_UNLIKELY(dropped)) {
735 ovs_mutex_lock(&dev->mutex);
736 dev->stats.tx_dropped += dropped;
737 ovs_mutex_unlock(&dev->mutex);
740 dpdk_queue_pkts(dev, NON_PMD_THREAD_TX_QUEUE, mbufs, newcnt);
741 dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
745 netdev_dpdk_send(struct netdev *netdev, struct dpif_packet **pkts, int cnt,
748 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
752 if (!may_steal || pkts[0]->ofpbuf.source != OFPBUF_DPDK) {
753 dpdk_do_tx_copy(netdev, pkts, cnt);
756 for (i = 0; i < cnt; i++) {
757 dpif_packet_delete(pkts[i]);
765 qid = rte_lcore_id() % NR_QUEUE;
767 for (i = 0; i < cnt; i++) {
768 int size = ofpbuf_size(&pkts[i]->ofpbuf);
769 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
770 if (next_tx_idx != i) {
771 dpdk_queue_pkts(dev, qid,
772 (struct rte_mbuf **)&pkts[next_tx_idx],
776 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
777 (int)size , dev->max_packet_len);
779 dpif_packet_delete(pkts[i]);
784 if (next_tx_idx != cnt) {
785 dpdk_queue_pkts(dev, qid,
786 (struct rte_mbuf **)&pkts[next_tx_idx],
790 if (OVS_UNLIKELY(dropped)) {
791 ovs_mutex_lock(&dev->mutex);
792 dev->stats.tx_dropped += dropped;
793 ovs_mutex_unlock(&dev->mutex);
802 netdev_dpdk_set_etheraddr(struct netdev *netdev,
803 const uint8_t mac[ETH_ADDR_LEN])
805 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
807 ovs_mutex_lock(&dev->mutex);
808 if (!eth_addr_equals(dev->hwaddr, mac)) {
809 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
810 netdev_change_seq_changed(netdev);
812 ovs_mutex_unlock(&dev->mutex);
818 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
819 uint8_t mac[ETH_ADDR_LEN])
821 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
823 ovs_mutex_lock(&dev->mutex);
824 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
825 ovs_mutex_unlock(&dev->mutex);
831 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
833 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
835 ovs_mutex_lock(&dev->mutex);
837 ovs_mutex_unlock(&dev->mutex);
843 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
845 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
847 struct dpdk_mp *old_mp;
850 ovs_mutex_lock(&dpdk_mutex);
851 ovs_mutex_lock(&dev->mutex);
852 if (dev->mtu == mtu) {
857 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
863 rte_eth_dev_stop(dev->port_id);
866 old_mp = dev->dpdk_mp;
869 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
871 err = dpdk_eth_dev_init(dev);
875 dev->dpdk_mp = old_mp;
876 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
877 dpdk_eth_dev_init(dev);
882 netdev_change_seq_changed(netdev);
884 ovs_mutex_unlock(&dev->mutex);
885 ovs_mutex_unlock(&dpdk_mutex);
890 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
893 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
895 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
896 struct rte_eth_stats rte_stats;
899 netdev_dpdk_get_carrier(netdev, &gg);
900 ovs_mutex_lock(&dev->mutex);
901 rte_eth_stats_get(dev->port_id, &rte_stats);
903 *stats = dev->stats_offset;
905 stats->rx_packets += rte_stats.ipackets;
906 stats->tx_packets += rte_stats.opackets;
907 stats->rx_bytes += rte_stats.ibytes;
908 stats->tx_bytes += rte_stats.obytes;
909 stats->rx_errors += rte_stats.ierrors;
910 stats->tx_errors += rte_stats.oerrors;
911 stats->multicast += rte_stats.imcasts;
913 stats->tx_dropped += dev->stats.tx_dropped;
914 ovs_mutex_unlock(&dev->mutex);
920 netdev_dpdk_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
922 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
924 ovs_mutex_lock(&dev->mutex);
925 dev->stats_offset = *stats;
926 ovs_mutex_unlock(&dev->mutex);
932 netdev_dpdk_get_features(const struct netdev *netdev_,
933 enum netdev_features *current,
934 enum netdev_features *advertised OVS_UNUSED,
935 enum netdev_features *supported OVS_UNUSED,
936 enum netdev_features *peer OVS_UNUSED)
938 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
939 struct rte_eth_link link;
941 ovs_mutex_lock(&dev->mutex);
943 ovs_mutex_unlock(&dev->mutex);
945 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
946 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
947 *current = NETDEV_F_AUTONEG;
949 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
950 if (link.link_speed == ETH_LINK_SPEED_10) {
951 *current = NETDEV_F_10MB_HD;
953 if (link.link_speed == ETH_LINK_SPEED_100) {
954 *current = NETDEV_F_100MB_HD;
956 if (link.link_speed == ETH_LINK_SPEED_1000) {
957 *current = NETDEV_F_1GB_HD;
959 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
960 if (link.link_speed == ETH_LINK_SPEED_10) {
961 *current = NETDEV_F_10MB_FD;
963 if (link.link_speed == ETH_LINK_SPEED_100) {
964 *current = NETDEV_F_100MB_FD;
966 if (link.link_speed == ETH_LINK_SPEED_1000) {
967 *current = NETDEV_F_1GB_FD;
969 if (link.link_speed == ETH_LINK_SPEED_10000) {
970 *current = NETDEV_F_10GB_FD;
978 netdev_dpdk_get_ifindex(const struct netdev *netdev)
980 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
983 ovs_mutex_lock(&dev->mutex);
984 ifindex = dev->port_id;
985 ovs_mutex_unlock(&dev->mutex);
991 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
993 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
995 ovs_mutex_lock(&dev->mutex);
996 check_link_status(dev);
997 *carrier = dev->link.link_status;
998 ovs_mutex_unlock(&dev->mutex);
1003 static long long int
1004 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1006 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1007 long long int carrier_resets;
1009 ovs_mutex_lock(&dev->mutex);
1010 carrier_resets = dev->link_reset_cnt;
1011 ovs_mutex_unlock(&dev->mutex);
1013 return carrier_resets;
1017 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1018 long long int interval OVS_UNUSED)
1024 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1025 enum netdev_flags off, enum netdev_flags on,
1026 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1030 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1034 *old_flagsp = dev->flags;
1038 if (dev->flags == *old_flagsp) {
1042 if (dev->flags & NETDEV_UP) {
1043 err = rte_eth_dev_start(dev->port_id);
1048 if (dev->flags & NETDEV_PROMISC) {
1049 rte_eth_promiscuous_enable(dev->port_id);
1052 if (!(dev->flags & NETDEV_UP)) {
1053 rte_eth_dev_stop(dev->port_id);
1060 netdev_dpdk_update_flags(struct netdev *netdev_,
1061 enum netdev_flags off, enum netdev_flags on,
1062 enum netdev_flags *old_flagsp)
1064 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1067 ovs_mutex_lock(&netdev->mutex);
1068 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1069 ovs_mutex_unlock(&netdev->mutex);
1075 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1077 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1078 struct rte_eth_dev_info dev_info;
1080 if (dev->port_id <= 0)
1083 ovs_mutex_lock(&dev->mutex);
1084 rte_eth_dev_info_get(dev->port_id, &dev_info);
1085 ovs_mutex_unlock(&dev->mutex);
1087 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1089 smap_add_format(args, "port_no", "%d", dev->port_id);
1090 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1091 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1092 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1093 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1094 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1095 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1096 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1097 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1098 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1099 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1101 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1102 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1108 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1109 OVS_REQUIRES(dev->mutex)
1111 enum netdev_flags old_flags;
1114 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1116 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1121 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1122 const char *argv[], void *aux OVS_UNUSED)
1126 if (!strcasecmp(argv[argc - 1], "up")) {
1128 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1131 unixctl_command_reply_error(conn, "Invalid Admin State");
1136 struct netdev *netdev = netdev_from_name(argv[1]);
1137 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1138 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1140 ovs_mutex_lock(&dpdk_dev->mutex);
1141 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1142 ovs_mutex_unlock(&dpdk_dev->mutex);
1144 netdev_close(netdev);
1146 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1147 netdev_close(netdev);
1151 struct netdev_dpdk *netdev;
1153 ovs_mutex_lock(&dpdk_mutex);
1154 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1155 ovs_mutex_lock(&netdev->mutex);
1156 netdev_dpdk_set_admin_state__(netdev, up);
1157 ovs_mutex_unlock(&netdev->mutex);
1159 ovs_mutex_unlock(&dpdk_mutex);
1161 unixctl_command_reply(conn, "OK");
1165 dpdk_common_init(void)
1167 unixctl_command_register("netdev-dpdk/set-admin-state",
1168 "[netdev] up|down", 1, 2,
1169 netdev_dpdk_set_admin_state, NULL);
1171 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1175 dpdk_class_init(void)
1179 result = rte_pmd_init_all();
1181 VLOG_ERR("Cannot init PMD");
1185 result = rte_eal_pci_probe();
1187 VLOG_ERR("Cannot probe PCI");
1191 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1199 dpdk_ring_create(const char dev_name[], unsigned int port_no,
1200 unsigned int *eth_port_id)
1202 struct dpdk_ring *ivshmem;
1206 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1207 if (ivshmem == NULL) {
1211 err = snprintf(ring_name, 10, "%s_tx", dev_name);
1216 ivshmem->cring_tx = rte_ring_create(ring_name, MAX_RX_QUEUE_LEN, SOCKET0, 0);
1217 if (ivshmem->cring_tx == NULL) {
1222 err = snprintf(ring_name, 10, "%s_rx", dev_name);
1227 ivshmem->cring_rx = rte_ring_create(ring_name, MAX_RX_QUEUE_LEN, SOCKET0, 0);
1228 if (ivshmem->cring_rx == NULL) {
1233 err = rte_eth_from_rings(&ivshmem->cring_rx, 1, &ivshmem->cring_tx, 1, SOCKET0);
1239 ivshmem->user_port_id = port_no;
1240 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1241 list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1243 *eth_port_id = ivshmem->eth_port_id;
1248 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1250 struct dpdk_ring *ivshmem;
1251 unsigned int port_no;
1254 /* Names always start with "dpdkr" */
1255 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1260 /* look through our list to find the device */
1261 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1262 if (ivshmem->user_port_id == port_no) {
1263 VLOG_INFO("Found dpdk ring device %s:\n", dev_name);
1264 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1268 /* Need to create the device rings */
1269 return dpdk_ring_create(dev_name, port_no, eth_port_id);
1273 netdev_dpdk_ring_construct(struct netdev *netdev)
1275 unsigned int port_no = 0;
1278 if (rte_eal_init_ret) {
1279 return rte_eal_init_ret;
1282 ovs_mutex_lock(&dpdk_mutex);
1284 err = dpdk_ring_open(netdev->name, &port_no);
1289 err = netdev_dpdk_init(netdev, port_no);
1292 ovs_mutex_unlock(&dpdk_mutex);
1296 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT) \
1300 NULL, /* netdev_dpdk_run */ \
1301 NULL, /* netdev_dpdk_wait */ \
1303 netdev_dpdk_alloc, \
1305 netdev_dpdk_destruct, \
1306 netdev_dpdk_dealloc, \
1307 netdev_dpdk_get_config, \
1308 NULL, /* netdev_dpdk_set_config */ \
1309 NULL, /* get_tunnel_config */ \
1311 netdev_dpdk_send, /* send */ \
1312 NULL, /* send_wait */ \
1314 netdev_dpdk_set_etheraddr, \
1315 netdev_dpdk_get_etheraddr, \
1316 netdev_dpdk_get_mtu, \
1317 netdev_dpdk_set_mtu, \
1318 netdev_dpdk_get_ifindex, \
1319 netdev_dpdk_get_carrier, \
1320 netdev_dpdk_get_carrier_resets, \
1321 netdev_dpdk_set_miimon, \
1322 netdev_dpdk_get_stats, \
1323 netdev_dpdk_set_stats, \
1324 netdev_dpdk_get_features, \
1325 NULL, /* set_advertisements */ \
1327 NULL, /* set_policing */ \
1328 NULL, /* get_qos_types */ \
1329 NULL, /* get_qos_capabilities */ \
1330 NULL, /* get_qos */ \
1331 NULL, /* set_qos */ \
1332 NULL, /* get_queue */ \
1333 NULL, /* set_queue */ \
1334 NULL, /* delete_queue */ \
1335 NULL, /* get_queue_stats */ \
1336 NULL, /* queue_dump_start */ \
1337 NULL, /* queue_dump_next */ \
1338 NULL, /* queue_dump_done */ \
1339 NULL, /* dump_queue_stats */ \
1341 NULL, /* get_in4 */ \
1342 NULL, /* set_in4 */ \
1343 NULL, /* get_in6 */ \
1344 NULL, /* add_router */ \
1345 NULL, /* get_next_hop */ \
1346 netdev_dpdk_get_status, \
1347 NULL, /* arp_lookup */ \
1349 netdev_dpdk_update_flags, \
1351 netdev_dpdk_rxq_alloc, \
1352 netdev_dpdk_rxq_construct, \
1353 netdev_dpdk_rxq_destruct, \
1354 netdev_dpdk_rxq_dealloc, \
1355 netdev_dpdk_rxq_recv, \
1356 NULL, /* rx_wait */ \
1357 NULL, /* rxq_drain */ \
1361 dpdk_init(int argc, char **argv)
1365 if (argc < 2 || strcmp(argv[1], "--dpdk"))
1368 /* Make sure program name passed to rte_eal_init() is vswitchd. */
1374 /* Make sure things are initialized ... */
1375 result = rte_eal_init(argc, argv);
1377 ovs_abort(result, "Cannot init EAL\n");
1381 rte_eal_init_ret = 0;
1383 if (argc > result) {
1384 argv[result] = argv[0];
1390 const struct netdev_class dpdk_class =
1394 netdev_dpdk_construct);
1396 const struct netdev_class dpdk_ring_class =
1400 netdev_dpdk_ring_construct);
1403 netdev_dpdk_register(void)
1405 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1407 if (rte_eal_init_ret) {
1411 if (ovsthread_once_start(&once)) {
1413 netdev_register_provider(&dpdk_class);
1414 netdev_register_provider(&dpdk_ring_class);
1415 ovsthread_once_done(&once);
1420 pmd_thread_setaffinity_cpu(int cpu)
1426 CPU_SET(cpu, &cpuset);
1427 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1429 VLOG_ERR("Thread affinity error %d",err);
1432 RTE_PER_LCORE(_lcore_id) = cpu;