2 * Copyright (c) 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
31 #include "dpif-netdev.h"
33 #include "netdev-dpdk.h"
34 #include "netdev-provider.h"
35 #include "netdev-vport.h"
37 #include "ofp-print.h"
40 #include "ovs-thread.h"
42 #include "packet-dpif.h"
46 #include "unaligned.h"
51 VLOG_DEFINE_THIS_MODULE(dpdk);
52 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
54 #define DPDK_PORT_WATCHDOG_INTERVAL 5
56 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
57 #define OVS_VPORT_DPDK "ovs_dpdk"
60 * need to reserve tons of extra space in the mbufs so we can align the
61 * DMA addresses to 4KB.
64 #define MTU_TO_MAX_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
65 #define MBUF_SIZE(mtu) (MTU_TO_MAX_LEN(mtu) + (512) + \
66 sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
68 /* XXX: mempool size should be based on system resources. */
69 #define NB_MBUF (4096 * 64)
70 #define MP_CACHE_SZ (256 * 2)
73 #define NIC_PORT_RX_Q_SIZE 2048 /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
74 #define NIC_PORT_TX_Q_SIZE 2048 /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
76 /* XXX: Needs per NIC value for these constants. */
77 #define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
78 #define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
79 #define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
81 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
82 #define TX_HTHRESH 0 /* Default values of TX host threshold reg. */
83 #define TX_WTHRESH 0 /* Default values of TX write-back threshold reg. */
85 static const struct rte_eth_conf port_conf = {
87 .mq_mode = ETH_MQ_RX_RSS,
89 .header_split = 0, /* Header Split disabled */
90 .hw_ip_checksum = 0, /* IP checksum offload disabled */
91 .hw_vlan_filter = 0, /* VLAN filtering disabled */
92 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
98 .rss_hf = ETH_RSS_IPV4_TCP | ETH_RSS_IPV4 | ETH_RSS_IPV6
99 | ETH_RSS_IPV4_UDP | ETH_RSS_IPV6_TCP | ETH_RSS_IPV6_UDP,
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 = 192 };
127 enum { MAX_TX_QUEUE_LEN = 384 };
128 enum { DPDK_RING_SIZE = 256 };
129 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
130 enum { DRAIN_TSC = 200000ULL };
132 static int rte_eal_init_ret = ENODEV;
134 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
136 /* Contains all 'struct dpdk_dev's. */
137 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
138 = LIST_INITIALIZER(&dpdk_list);
140 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
141 = LIST_INITIALIZER(&dpdk_mp_list);
143 /* This mutex must be used by non pmd threads when allocating or freeing
144 * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
145 * use mempools, a non pmd thread should hold this mutex while calling them */
146 struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
149 struct rte_mempool *mp;
153 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
156 /* There should be one 'struct dpdk_tx_queue' created for
158 struct dpdk_tx_queue {
159 bool flush_tx; /* Set to true to flush queue everytime */
160 /* pkts are queued. */
163 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
166 /* dpdk has no way to remove dpdk ring ethernet devices
167 so we have to keep them around once they've been created
170 static struct list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
171 = LIST_INITIALIZER(&dpdk_ring_list);
174 /* For the client rings */
175 struct rte_ring *cring_tx;
176 struct rte_ring *cring_rx;
177 int user_port_id; /* User given port no, parsed from port name */
178 int eth_port_id; /* ethernet device port id */
179 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
187 struct dpdk_tx_queue *tx_q;
189 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
191 struct dpdk_mp *dpdk_mp;
195 struct netdev_stats stats;
197 uint8_t hwaddr[ETH_ADDR_LEN];
198 enum netdev_flags flags;
200 struct rte_eth_link link;
204 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
207 struct netdev_rxq_dpdk {
208 struct netdev_rxq up;
212 static bool thread_is_pmd(void);
214 static int netdev_dpdk_construct(struct netdev *);
217 is_dpdk_class(const struct netdev_class *class)
219 return class->construct == netdev_dpdk_construct;
222 /* XXX: use dpdk malloc for entire OVS. infact huge page shld be used
223 * for all other sengments data, bss and text. */
226 dpdk_rte_mzalloc(size_t sz)
230 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
237 /* XXX this function should be called only by pmd threads (or by non pmd
238 * threads holding the nonpmd_mempool_mutex) */
240 free_dpdk_buf(struct dpif_packet *p)
242 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
244 rte_pktmbuf_free_seg(pkt);
248 __rte_pktmbuf_init(struct rte_mempool *mp,
249 void *opaque_arg OVS_UNUSED,
251 unsigned i OVS_UNUSED)
253 struct rte_mbuf *m = _m;
254 uint32_t buf_len = mp->elt_size - sizeof(struct dpif_packet);
256 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dpif_packet));
258 memset(m, 0, mp->elt_size);
260 /* start of buffer is just after mbuf structure */
261 m->buf_addr = (char *)m + sizeof(struct dpif_packet);
262 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
263 sizeof(struct dpif_packet);
264 m->buf_len = (uint16_t)buf_len;
266 /* keep some headroom between start of buffer and data */
267 m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
269 /* init some constant fields */
270 m->type = RTE_MBUF_PKT;
273 m->pkt.in_port = 0xff;
277 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
278 void *opaque_arg OVS_UNUSED,
280 unsigned i OVS_UNUSED)
282 struct rte_mbuf *m = _m;
284 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
286 ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
289 static struct dpdk_mp *
290 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
292 struct dpdk_mp *dmp = NULL;
293 char mp_name[RTE_MEMPOOL_NAMESIZE];
295 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
296 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
302 dmp = dpdk_rte_mzalloc(sizeof *dmp);
303 dmp->socket_id = socket_id;
307 if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d_%d", dmp->mtu,
308 dmp->socket_id) < 0) {
312 dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
314 sizeof(struct rte_pktmbuf_pool_private),
315 rte_pktmbuf_pool_init, NULL,
316 ovs_rte_pktmbuf_init, NULL,
319 if (dmp->mp == NULL) {
323 list_push_back(&dpdk_mp_list, &dmp->list_node);
328 dpdk_mp_put(struct dpdk_mp *dmp)
336 ovs_assert(dmp->refcount >= 0);
339 /* I could not find any API to destroy mp. */
340 if (dmp->refcount == 0) {
341 list_delete(dmp->list_node);
342 /* destroy mp-pool. */
348 check_link_status(struct netdev_dpdk *dev)
350 struct rte_eth_link link;
352 rte_eth_link_get_nowait(dev->port_id, &link);
354 if (dev->link.link_status != link.link_status) {
355 netdev_change_seq_changed(&dev->up);
357 dev->link_reset_cnt++;
359 if (dev->link.link_status) {
360 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
361 dev->port_id, (unsigned)dev->link.link_speed,
362 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
363 ("full-duplex") : ("half-duplex"));
365 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
371 dpdk_watchdog(void *dummy OVS_UNUSED)
373 struct netdev_dpdk *dev;
375 pthread_detach(pthread_self());
378 ovs_mutex_lock(&dpdk_mutex);
379 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
380 ovs_mutex_lock(&dev->mutex);
381 check_link_status(dev);
382 ovs_mutex_unlock(&dev->mutex);
384 ovs_mutex_unlock(&dpdk_mutex);
385 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
392 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
394 struct rte_pktmbuf_pool_private *mbp_priv;
395 struct ether_addr eth_addr;
399 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
403 diag = rte_eth_dev_configure(dev->port_id, dev->up.n_rxq, dev->up.n_txq,
406 VLOG_ERR("eth dev config error %d",diag);
410 for (i = 0; i < dev->up.n_txq; i++) {
411 diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
412 dev->socket_id, &tx_conf);
414 VLOG_ERR("eth dev tx queue setup error %d",diag);
419 for (i = 0; i < dev->up.n_rxq; i++) {
420 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
422 &rx_conf, dev->dpdk_mp->mp);
424 VLOG_ERR("eth dev rx queue setup error %d",diag);
429 diag = rte_eth_dev_start(dev->port_id);
431 VLOG_ERR("eth dev start error %d",diag);
435 rte_eth_promiscuous_enable(dev->port_id);
436 rte_eth_allmulticast_enable(dev->port_id);
438 memset(ð_addr, 0x0, sizeof(eth_addr));
439 rte_eth_macaddr_get(dev->port_id, ð_addr);
440 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
441 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
443 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
444 rte_eth_link_get_nowait(dev->port_id, &dev->link);
446 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
447 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
449 dev->flags = NETDEV_UP | NETDEV_PROMISC;
453 static struct netdev_dpdk *
454 netdev_dpdk_cast(const struct netdev *netdev)
456 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
459 static struct netdev *
460 netdev_dpdk_alloc(void)
462 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
467 netdev_dpdk_alloc_txq(struct netdev_dpdk *netdev, unsigned int n_txqs)
471 netdev->tx_q = dpdk_rte_mzalloc(n_txqs * sizeof *netdev->tx_q);
472 /* Each index is considered as a cpu core id, since there should
473 * be one tx queue for each cpu core. */
474 for (i = 0; i < n_txqs; i++) {
475 int numa_id = ovs_numa_get_numa_id(i);
477 /* If the corresponding core is not on the same numa node
478 * as 'netdev', flags the 'flush_tx'. */
479 netdev->tx_q[i].flush_tx = netdev->socket_id == numa_id;
484 netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no)
485 OVS_REQUIRES(dpdk_mutex)
487 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
490 ovs_mutex_init(&netdev->mutex);
492 ovs_mutex_lock(&netdev->mutex);
494 netdev->socket_id = rte_eth_dev_socket_id(port_no);
495 netdev_dpdk_alloc_txq(netdev, NR_QUEUE);
496 netdev->port_id = port_no;
498 netdev->mtu = ETHER_MTU;
499 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
501 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
502 if (!netdev->dpdk_mp) {
507 netdev_->n_txq = NR_QUEUE;
508 netdev_->n_rxq = NR_QUEUE;
509 err = dpdk_eth_dev_init(netdev);
514 list_push_back(&dpdk_list, &netdev->list_node);
518 rte_free(netdev->tx_q);
520 ovs_mutex_unlock(&netdev->mutex);
525 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
526 unsigned int *port_no)
530 if (strncmp(dev_name, prefix, strlen(prefix))) {
534 cport = dev_name + strlen(prefix);
535 *port_no = strtol(cport, 0, 0); /* string must be null terminated */
540 netdev_dpdk_construct(struct netdev *netdev)
542 unsigned int port_no;
545 if (rte_eal_init_ret) {
546 return rte_eal_init_ret;
549 /* Names always start with "dpdk" */
550 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
555 ovs_mutex_lock(&dpdk_mutex);
556 err = netdev_dpdk_init(netdev, port_no);
557 ovs_mutex_unlock(&dpdk_mutex);
562 netdev_dpdk_destruct(struct netdev *netdev_)
564 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
566 ovs_mutex_lock(&dev->mutex);
567 rte_eth_dev_stop(dev->port_id);
568 ovs_mutex_unlock(&dev->mutex);
570 ovs_mutex_lock(&dpdk_mutex);
572 list_remove(&dev->list_node);
573 dpdk_mp_put(dev->dpdk_mp);
574 ovs_mutex_unlock(&dpdk_mutex);
576 ovs_mutex_destroy(&dev->mutex);
580 netdev_dpdk_dealloc(struct netdev *netdev_)
582 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
588 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
590 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
592 ovs_mutex_lock(&dev->mutex);
594 smap_add_format(args, "configured_rx_queues", "%d", netdev_->n_rxq);
595 smap_add_format(args, "configured_tx_queues", "%d", netdev_->n_txq);
596 ovs_mutex_unlock(&dev->mutex);
602 netdev_dpdk_get_numa_id(const struct netdev *netdev_)
604 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
606 return netdev->socket_id;
609 /* Sets the number of tx queues and rx queues for the dpdk interface.
610 * If the configuration fails, do not try restoring its old configuration
611 * and just returns the error. */
613 netdev_dpdk_set_multiq(struct netdev *netdev_, unsigned int n_txq,
616 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
619 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
623 ovs_mutex_lock(&dpdk_mutex);
624 ovs_mutex_lock(&netdev->mutex);
626 rte_eth_dev_stop(netdev->port_id);
628 netdev->up.n_txq = n_txq;
629 netdev->up.n_rxq = n_rxq;
630 rte_free(netdev->tx_q);
631 netdev_dpdk_alloc_txq(netdev, n_txq);
632 err = dpdk_eth_dev_init(netdev);
634 ovs_mutex_unlock(&netdev->mutex);
635 ovs_mutex_unlock(&dpdk_mutex);
640 static struct netdev_rxq *
641 netdev_dpdk_rxq_alloc(void)
643 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
648 static struct netdev_rxq_dpdk *
649 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
651 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
655 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
657 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
658 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
660 ovs_mutex_lock(&netdev->mutex);
661 rx->port_id = netdev->port_id;
662 ovs_mutex_unlock(&netdev->mutex);
668 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
673 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
675 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
681 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
683 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
686 while (nb_tx != txq->count) {
689 ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
698 if (OVS_UNLIKELY(nb_tx != txq->count)) {
699 /* free buffers, which we couldn't transmit, one at a time (each
700 * packet could come from a different mempool) */
703 for (i = nb_tx; i < txq->count; i++) {
704 rte_pktmbuf_free_seg(txq->burst_pkts[i]);
706 ovs_mutex_lock(&dev->mutex);
707 dev->stats.tx_dropped += txq->count-nb_tx;
708 ovs_mutex_unlock(&dev->mutex);
712 txq->tsc = rte_get_timer_cycles();
716 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
718 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
720 if (txq->count == 0) {
723 dpdk_queue_flush__(dev, qid);
727 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
730 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
731 struct netdev *netdev = rx->up.netdev;
732 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
735 /* There is only one tx queue for this core. Do not flush other
737 if (rxq_->queue_id == rte_lcore_id()) {
738 dpdk_queue_flush(dev, rxq_->queue_id);
741 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
742 (struct rte_mbuf **) packets,
743 MIN((int)NETDEV_MAX_RX_BATCH,
744 (int)MAX_RX_QUEUE_LEN));
755 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
756 struct rte_mbuf **pkts, int cnt)
758 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
764 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
765 int tocopy = MIN(freeslots, cnt-i);
767 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
768 tocopy * sizeof (struct rte_mbuf *));
770 txq->count += tocopy;
773 if (txq->count == MAX_TX_QUEUE_LEN || txq->flush_tx) {
774 dpdk_queue_flush__(dev, qid);
776 diff_tsc = rte_get_timer_cycles() - txq->tsc;
777 if (diff_tsc >= DRAIN_TSC) {
778 dpdk_queue_flush__(dev, qid);
783 /* Tx function. Transmit packets indefinitely */
785 dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dpif_packet ** pkts,
787 OVS_NO_THREAD_SAFETY_ANALYSIS
789 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
790 struct rte_mbuf *mbufs[cnt];
795 /* If we are on a non pmd thread we have to use the mempool mutex, because
796 * every non pmd thread shares the same mempool cache */
798 if (!thread_is_pmd()) {
799 ovs_mutex_lock(&nonpmd_mempool_mutex);
802 for (i = 0; i < cnt; i++) {
803 int size = ofpbuf_size(&pkts[i]->ofpbuf);
805 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
806 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
807 (int)size , dev->max_packet_len);
813 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
815 if (!mbufs[newcnt]) {
820 /* We have to do a copy for now */
821 memcpy(mbufs[newcnt]->pkt.data, ofpbuf_data(&pkts[i]->ofpbuf), size);
823 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
824 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
829 if (OVS_UNLIKELY(dropped)) {
830 ovs_mutex_lock(&dev->mutex);
831 dev->stats.tx_dropped += dropped;
832 ovs_mutex_unlock(&dev->mutex);
835 dpdk_queue_pkts(dev, qid, mbufs, newcnt);
836 dpdk_queue_flush(dev, qid);
838 if (!thread_is_pmd()) {
839 ovs_mutex_unlock(&nonpmd_mempool_mutex);
844 netdev_dpdk_send(struct netdev *netdev, int qid, struct dpif_packet **pkts,
845 int cnt, bool may_steal)
847 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
851 if (!may_steal || pkts[0]->ofpbuf.source != OFPBUF_DPDK) {
852 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
855 for (i = 0; i < cnt; i++) {
856 dpif_packet_delete(pkts[i]);
863 for (i = 0; i < cnt; i++) {
864 int size = ofpbuf_size(&pkts[i]->ofpbuf);
865 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
866 if (next_tx_idx != i) {
867 dpdk_queue_pkts(dev, qid,
868 (struct rte_mbuf **)&pkts[next_tx_idx],
872 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
873 (int)size , dev->max_packet_len);
875 dpif_packet_delete(pkts[i]);
880 if (next_tx_idx != cnt) {
881 dpdk_queue_pkts(dev, qid,
882 (struct rte_mbuf **)&pkts[next_tx_idx],
886 if (OVS_UNLIKELY(dropped)) {
887 ovs_mutex_lock(&dev->mutex);
888 dev->stats.tx_dropped += dropped;
889 ovs_mutex_unlock(&dev->mutex);
898 netdev_dpdk_set_etheraddr(struct netdev *netdev,
899 const uint8_t mac[ETH_ADDR_LEN])
901 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
903 ovs_mutex_lock(&dev->mutex);
904 if (!eth_addr_equals(dev->hwaddr, mac)) {
905 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
906 netdev_change_seq_changed(netdev);
908 ovs_mutex_unlock(&dev->mutex);
914 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
915 uint8_t mac[ETH_ADDR_LEN])
917 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
919 ovs_mutex_lock(&dev->mutex);
920 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
921 ovs_mutex_unlock(&dev->mutex);
927 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
929 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
931 ovs_mutex_lock(&dev->mutex);
933 ovs_mutex_unlock(&dev->mutex);
939 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
941 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
943 struct dpdk_mp *old_mp;
946 ovs_mutex_lock(&dpdk_mutex);
947 ovs_mutex_lock(&dev->mutex);
948 if (dev->mtu == mtu) {
953 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
959 rte_eth_dev_stop(dev->port_id);
962 old_mp = dev->dpdk_mp;
965 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
967 err = dpdk_eth_dev_init(dev);
971 dev->dpdk_mp = old_mp;
972 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
973 dpdk_eth_dev_init(dev);
978 netdev_change_seq_changed(netdev);
980 ovs_mutex_unlock(&dev->mutex);
981 ovs_mutex_unlock(&dpdk_mutex);
986 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
989 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
991 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
992 struct rte_eth_stats rte_stats;
995 netdev_dpdk_get_carrier(netdev, &gg);
996 ovs_mutex_lock(&dev->mutex);
997 rte_eth_stats_get(dev->port_id, &rte_stats);
999 memset(stats, 0, sizeof(*stats));
1001 stats->rx_packets = rte_stats.ipackets;
1002 stats->tx_packets = rte_stats.opackets;
1003 stats->rx_bytes = rte_stats.ibytes;
1004 stats->tx_bytes = rte_stats.obytes;
1005 stats->rx_errors = rte_stats.ierrors;
1006 stats->tx_errors = rte_stats.oerrors;
1007 stats->multicast = rte_stats.imcasts;
1009 stats->tx_dropped = dev->stats.tx_dropped;
1010 ovs_mutex_unlock(&dev->mutex);
1016 netdev_dpdk_get_features(const struct netdev *netdev_,
1017 enum netdev_features *current,
1018 enum netdev_features *advertised OVS_UNUSED,
1019 enum netdev_features *supported OVS_UNUSED,
1020 enum netdev_features *peer OVS_UNUSED)
1022 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1023 struct rte_eth_link link;
1025 ovs_mutex_lock(&dev->mutex);
1027 ovs_mutex_unlock(&dev->mutex);
1029 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1030 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1031 *current = NETDEV_F_AUTONEG;
1033 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1034 if (link.link_speed == ETH_LINK_SPEED_10) {
1035 *current = NETDEV_F_10MB_HD;
1037 if (link.link_speed == ETH_LINK_SPEED_100) {
1038 *current = NETDEV_F_100MB_HD;
1040 if (link.link_speed == ETH_LINK_SPEED_1000) {
1041 *current = NETDEV_F_1GB_HD;
1043 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1044 if (link.link_speed == ETH_LINK_SPEED_10) {
1045 *current = NETDEV_F_10MB_FD;
1047 if (link.link_speed == ETH_LINK_SPEED_100) {
1048 *current = NETDEV_F_100MB_FD;
1050 if (link.link_speed == ETH_LINK_SPEED_1000) {
1051 *current = NETDEV_F_1GB_FD;
1053 if (link.link_speed == ETH_LINK_SPEED_10000) {
1054 *current = NETDEV_F_10GB_FD;
1062 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1064 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1067 ovs_mutex_lock(&dev->mutex);
1068 ifindex = dev->port_id;
1069 ovs_mutex_unlock(&dev->mutex);
1075 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1077 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1079 ovs_mutex_lock(&dev->mutex);
1080 check_link_status(dev);
1081 *carrier = dev->link.link_status;
1082 ovs_mutex_unlock(&dev->mutex);
1087 static long long int
1088 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1090 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1091 long long int carrier_resets;
1093 ovs_mutex_lock(&dev->mutex);
1094 carrier_resets = dev->link_reset_cnt;
1095 ovs_mutex_unlock(&dev->mutex);
1097 return carrier_resets;
1101 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1102 long long int interval OVS_UNUSED)
1108 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1109 enum netdev_flags off, enum netdev_flags on,
1110 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1114 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1118 *old_flagsp = dev->flags;
1122 if (dev->flags == *old_flagsp) {
1126 if (dev->flags & NETDEV_UP) {
1127 err = rte_eth_dev_start(dev->port_id);
1132 if (dev->flags & NETDEV_PROMISC) {
1133 rte_eth_promiscuous_enable(dev->port_id);
1136 if (!(dev->flags & NETDEV_UP)) {
1137 rte_eth_dev_stop(dev->port_id);
1144 netdev_dpdk_update_flags(struct netdev *netdev_,
1145 enum netdev_flags off, enum netdev_flags on,
1146 enum netdev_flags *old_flagsp)
1148 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1151 ovs_mutex_lock(&netdev->mutex);
1152 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1153 ovs_mutex_unlock(&netdev->mutex);
1159 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1161 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1162 struct rte_eth_dev_info dev_info;
1164 if (dev->port_id < 0)
1167 ovs_mutex_lock(&dev->mutex);
1168 rte_eth_dev_info_get(dev->port_id, &dev_info);
1169 ovs_mutex_unlock(&dev->mutex);
1171 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1173 smap_add_format(args, "port_no", "%d", dev->port_id);
1174 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1175 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1176 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1177 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1178 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1179 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1180 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1181 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1182 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1183 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1185 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1186 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1192 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1193 OVS_REQUIRES(dev->mutex)
1195 enum netdev_flags old_flags;
1198 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1200 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1205 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1206 const char *argv[], void *aux OVS_UNUSED)
1210 if (!strcasecmp(argv[argc - 1], "up")) {
1212 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1215 unixctl_command_reply_error(conn, "Invalid Admin State");
1220 struct netdev *netdev = netdev_from_name(argv[1]);
1221 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1222 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1224 ovs_mutex_lock(&dpdk_dev->mutex);
1225 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1226 ovs_mutex_unlock(&dpdk_dev->mutex);
1228 netdev_close(netdev);
1230 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1231 netdev_close(netdev);
1235 struct netdev_dpdk *netdev;
1237 ovs_mutex_lock(&dpdk_mutex);
1238 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1239 ovs_mutex_lock(&netdev->mutex);
1240 netdev_dpdk_set_admin_state__(netdev, up);
1241 ovs_mutex_unlock(&netdev->mutex);
1243 ovs_mutex_unlock(&dpdk_mutex);
1245 unixctl_command_reply(conn, "OK");
1249 dpdk_common_init(void)
1251 unixctl_command_register("netdev-dpdk/set-admin-state",
1252 "[netdev] up|down", 1, 2,
1253 netdev_dpdk_set_admin_state, NULL);
1255 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1259 dpdk_class_init(void)
1263 result = rte_eal_pci_probe();
1265 VLOG_ERR("Cannot probe PCI");
1269 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1277 dpdk_ring_create(const char dev_name[], unsigned int port_no,
1278 unsigned int *eth_port_id)
1280 struct dpdk_ring *ivshmem;
1284 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1285 if (ivshmem == NULL) {
1289 err = snprintf(ring_name, 10, "%s_tx", dev_name);
1294 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, 0);
1295 if (ivshmem->cring_tx == NULL) {
1300 err = snprintf(ring_name, 10, "%s_rx", dev_name);
1305 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, 0);
1306 if (ivshmem->cring_rx == NULL) {
1311 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
1312 &ivshmem->cring_tx, 1, SOCKET0);
1319 ivshmem->user_port_id = port_no;
1320 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1321 list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1323 *eth_port_id = ivshmem->eth_port_id;
1328 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1330 struct dpdk_ring *ivshmem;
1331 unsigned int port_no;
1334 /* Names always start with "dpdkr" */
1335 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1340 /* look through our list to find the device */
1341 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1342 if (ivshmem->user_port_id == port_no) {
1343 VLOG_INFO("Found dpdk ring device %s:\n", dev_name);
1344 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1348 /* Need to create the device rings */
1349 return dpdk_ring_create(dev_name, port_no, eth_port_id);
1353 netdev_dpdk_ring_construct(struct netdev *netdev)
1355 unsigned int port_no = 0;
1358 if (rte_eal_init_ret) {
1359 return rte_eal_init_ret;
1362 ovs_mutex_lock(&dpdk_mutex);
1364 err = dpdk_ring_open(netdev->name, &port_no);
1369 err = netdev_dpdk_init(netdev, port_no);
1372 ovs_mutex_unlock(&dpdk_mutex);
1376 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, MULTIQ) \
1380 NULL, /* netdev_dpdk_run */ \
1381 NULL, /* netdev_dpdk_wait */ \
1383 netdev_dpdk_alloc, \
1385 netdev_dpdk_destruct, \
1386 netdev_dpdk_dealloc, \
1387 netdev_dpdk_get_config, \
1388 NULL, /* netdev_dpdk_set_config */ \
1389 NULL, /* get_tunnel_config */ \
1390 netdev_dpdk_get_numa_id, /* get_numa_id */ \
1391 MULTIQ, /* set_multiq */ \
1393 netdev_dpdk_send, /* send */ \
1394 NULL, /* send_wait */ \
1396 netdev_dpdk_set_etheraddr, \
1397 netdev_dpdk_get_etheraddr, \
1398 netdev_dpdk_get_mtu, \
1399 netdev_dpdk_set_mtu, \
1400 netdev_dpdk_get_ifindex, \
1401 netdev_dpdk_get_carrier, \
1402 netdev_dpdk_get_carrier_resets, \
1403 netdev_dpdk_set_miimon, \
1404 netdev_dpdk_get_stats, \
1405 netdev_dpdk_get_features, \
1406 NULL, /* set_advertisements */ \
1408 NULL, /* set_policing */ \
1409 NULL, /* get_qos_types */ \
1410 NULL, /* get_qos_capabilities */ \
1411 NULL, /* get_qos */ \
1412 NULL, /* set_qos */ \
1413 NULL, /* get_queue */ \
1414 NULL, /* set_queue */ \
1415 NULL, /* delete_queue */ \
1416 NULL, /* get_queue_stats */ \
1417 NULL, /* queue_dump_start */ \
1418 NULL, /* queue_dump_next */ \
1419 NULL, /* queue_dump_done */ \
1420 NULL, /* dump_queue_stats */ \
1422 NULL, /* get_in4 */ \
1423 NULL, /* set_in4 */ \
1424 NULL, /* get_in6 */ \
1425 NULL, /* add_router */ \
1426 NULL, /* get_next_hop */ \
1427 netdev_dpdk_get_status, \
1428 NULL, /* arp_lookup */ \
1430 netdev_dpdk_update_flags, \
1432 netdev_dpdk_rxq_alloc, \
1433 netdev_dpdk_rxq_construct, \
1434 netdev_dpdk_rxq_destruct, \
1435 netdev_dpdk_rxq_dealloc, \
1436 netdev_dpdk_rxq_recv, \
1437 NULL, /* rx_wait */ \
1438 NULL, /* rxq_drain */ \
1442 dpdk_init(int argc, char **argv)
1446 if (argc < 2 || strcmp(argv[1], "--dpdk"))
1449 /* Make sure program name passed to rte_eal_init() is vswitchd. */
1455 /* Make sure things are initialized ... */
1456 result = rte_eal_init(argc, argv);
1458 ovs_abort(result, "Cannot init EAL\n");
1461 rte_memzone_dump(stdout);
1462 rte_eal_init_ret = 0;
1464 if (argc > result) {
1465 argv[result] = argv[0];
1468 /* We are called from the main thread here */
1469 thread_set_nonpmd();
1474 const struct netdev_class dpdk_class =
1478 netdev_dpdk_construct,
1479 netdev_dpdk_set_multiq);
1481 const struct netdev_class dpdk_ring_class =
1485 netdev_dpdk_ring_construct,
1489 netdev_dpdk_register(void)
1491 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1493 if (rte_eal_init_ret) {
1497 if (ovsthread_once_start(&once)) {
1499 netdev_register_provider(&dpdk_class);
1500 netdev_register_provider(&dpdk_ring_class);
1501 ovsthread_once_done(&once);
1506 pmd_thread_setaffinity_cpu(int cpu)
1512 CPU_SET(cpu, &cpuset);
1513 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1515 VLOG_ERR("Thread affinity error %d",err);
1518 /* lcore_id 0 is reseved for use by non pmd threads. */
1520 RTE_PER_LCORE(_lcore_id) = cpu;
1526 thread_set_nonpmd(void)
1528 /* We have to use 0 to allow non pmd threads to perform certain DPDK
1529 * operations, like rte_eth_dev_configure(). */
1530 RTE_PER_LCORE(_lcore_id) = 0;
1536 return rte_lcore_id() != 0;