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 /* XXX: 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 /* XXX: 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
100 | ETH_RSS_IPV4_UDP | ETH_RSS_IPV6_TCP | ETH_RSS_IPV6_UDP,
104 .mq_mode = ETH_MQ_TX_NONE,
108 static const struct rte_eth_rxconf rx_conf = {
110 .pthresh = RX_PTHRESH,
111 .hthresh = RX_HTHRESH,
112 .wthresh = RX_WTHRESH,
116 static const struct rte_eth_txconf tx_conf = {
118 .pthresh = TX_PTHRESH,
119 .hthresh = TX_HTHRESH,
120 .wthresh = TX_WTHRESH,
124 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS|ETH_TXQ_FLAGS_NOOFFLOADS,
127 enum { MAX_RX_QUEUE_LEN = 192 };
128 enum { MAX_TX_QUEUE_LEN = 384 };
129 enum { DPDK_RING_SIZE = 256 };
130 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
131 enum { DRAIN_TSC = 200000ULL };
133 static int rte_eal_init_ret = ENODEV;
135 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
137 /* Contains all 'struct dpdk_dev's. */
138 static struct list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
139 = LIST_INITIALIZER(&dpdk_list);
141 static struct list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
142 = LIST_INITIALIZER(&dpdk_mp_list);
144 /* This mutex must be used by non pmd threads when allocating or freeing
145 * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
146 * use mempools, a non pmd thread should hold this mutex while calling them */
147 struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
150 struct rte_mempool *mp;
154 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
157 struct dpdk_tx_queue {
158 rte_spinlock_t tx_lock;
161 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
164 /* dpdk has no way to remove dpdk ring ethernet devices
165 so we have to keep them around once they've been created
168 static struct list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
169 = LIST_INITIALIZER(&dpdk_ring_list);
172 /* For the client rings */
173 struct rte_ring *cring_tx;
174 struct rte_ring *cring_rx;
175 int user_port_id; /* User given port no, parsed from port name */
176 int eth_port_id; /* ethernet device port id */
177 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
185 struct dpdk_tx_queue tx_q[NR_QUEUE];
187 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
189 struct dpdk_mp *dpdk_mp;
193 struct netdev_stats stats;
195 uint8_t hwaddr[ETH_ADDR_LEN];
196 enum netdev_flags flags;
198 struct rte_eth_link link;
202 struct list list_node OVS_GUARDED_BY(dpdk_mutex);
205 struct netdev_rxq_dpdk {
206 struct netdev_rxq up;
210 static bool thread_is_pmd(void);
212 static int netdev_dpdk_construct(struct netdev *);
215 is_dpdk_class(const struct netdev_class *class)
217 return class->construct == netdev_dpdk_construct;
220 /* XXX: use dpdk malloc for entire OVS. infact huge page shld be used
221 * for all other sengments data, bss and text. */
224 dpdk_rte_mzalloc(size_t sz)
228 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
235 /* XXX this function should be called only by pmd threads (or by non pmd
236 * threads holding the nonpmd_mempool_mutex) */
238 free_dpdk_buf(struct dpif_packet *p)
240 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
242 rte_pktmbuf_free_seg(pkt);
246 __rte_pktmbuf_init(struct rte_mempool *mp,
247 void *opaque_arg OVS_UNUSED,
249 unsigned i OVS_UNUSED)
251 struct rte_mbuf *m = _m;
252 uint32_t buf_len = mp->elt_size - sizeof(struct dpif_packet);
254 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dpif_packet));
256 memset(m, 0, mp->elt_size);
258 /* start of buffer is just after mbuf structure */
259 m->buf_addr = (char *)m + sizeof(struct dpif_packet);
260 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
261 sizeof(struct dpif_packet);
262 m->buf_len = (uint16_t)buf_len;
264 /* keep some headroom between start of buffer and data */
265 m->pkt.data = (char*) m->buf_addr + RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
267 /* init some constant fields */
268 m->type = RTE_MBUF_PKT;
271 m->pkt.in_port = 0xff;
275 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
276 void *opaque_arg OVS_UNUSED,
278 unsigned i OVS_UNUSED)
280 struct rte_mbuf *m = _m;
282 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
284 ofpbuf_init_dpdk((struct ofpbuf *) m, m->buf_len);
287 static struct dpdk_mp *
288 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
290 struct dpdk_mp *dmp = NULL;
291 char mp_name[RTE_MEMPOOL_NAMESIZE];
293 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
294 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
300 dmp = dpdk_rte_mzalloc(sizeof *dmp);
301 dmp->socket_id = socket_id;
305 if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d_%d", dmp->mtu,
306 dmp->socket_id) < 0) {
310 dmp->mp = rte_mempool_create(mp_name, NB_MBUF, MBUF_SIZE(mtu),
312 sizeof(struct rte_pktmbuf_pool_private),
313 rte_pktmbuf_pool_init, NULL,
314 ovs_rte_pktmbuf_init, NULL,
317 if (dmp->mp == NULL) {
321 list_push_back(&dpdk_mp_list, &dmp->list_node);
326 dpdk_mp_put(struct dpdk_mp *dmp)
334 ovs_assert(dmp->refcount >= 0);
337 /* I could not find any API to destroy mp. */
338 if (dmp->refcount == 0) {
339 list_delete(dmp->list_node);
340 /* destroy mp-pool. */
346 check_link_status(struct netdev_dpdk *dev)
348 struct rte_eth_link link;
350 rte_eth_link_get_nowait(dev->port_id, &link);
352 if (dev->link.link_status != link.link_status) {
353 netdev_change_seq_changed(&dev->up);
355 dev->link_reset_cnt++;
357 if (dev->link.link_status) {
358 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
359 dev->port_id, (unsigned)dev->link.link_speed,
360 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
361 ("full-duplex") : ("half-duplex"));
363 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
369 dpdk_watchdog(void *dummy OVS_UNUSED)
371 struct netdev_dpdk *dev;
373 pthread_detach(pthread_self());
376 ovs_mutex_lock(&dpdk_mutex);
377 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
378 ovs_mutex_lock(&dev->mutex);
379 check_link_status(dev);
380 ovs_mutex_unlock(&dev->mutex);
382 ovs_mutex_unlock(&dpdk_mutex);
383 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
390 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
392 struct rte_pktmbuf_pool_private *mbp_priv;
393 struct ether_addr eth_addr;
397 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
401 diag = rte_eth_dev_configure(dev->port_id, dev->up.n_rxq, dev->up.n_txq,
404 VLOG_ERR("eth dev config error %d",diag);
408 for (i = 0; i < dev->up.n_txq; i++) {
409 diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
410 dev->socket_id, &tx_conf);
412 VLOG_ERR("eth dev tx queue setup error %d",diag);
417 for (i = 0; i < dev->up.n_rxq; i++) {
418 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
420 &rx_conf, dev->dpdk_mp->mp);
422 VLOG_ERR("eth dev rx queue setup error %d",diag);
427 diag = rte_eth_dev_start(dev->port_id);
429 VLOG_ERR("eth dev start error %d",diag);
433 rte_eth_promiscuous_enable(dev->port_id);
434 rte_eth_allmulticast_enable(dev->port_id);
436 memset(ð_addr, 0x0, sizeof(eth_addr));
437 rte_eth_macaddr_get(dev->port_id, ð_addr);
438 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
439 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
441 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
442 rte_eth_link_get_nowait(dev->port_id, &dev->link);
444 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
445 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
447 dev->flags = NETDEV_UP | NETDEV_PROMISC;
451 static struct netdev_dpdk *
452 netdev_dpdk_cast(const struct netdev *netdev)
454 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
457 static struct netdev *
458 netdev_dpdk_alloc(void)
460 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
465 netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no) OVS_REQUIRES(dpdk_mutex)
467 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
471 ovs_mutex_init(&netdev->mutex);
473 ovs_mutex_lock(&netdev->mutex);
475 for (i = 0; i < NR_QUEUE; i++) {
476 rte_spinlock_init(&netdev->tx_q[i].tx_lock);
479 netdev->port_id = port_no;
482 netdev->mtu = ETHER_MTU;
483 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
485 /* XXX: need to discover device node at run time. */
486 netdev->socket_id = SOCKET0;
488 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
489 if (!netdev->dpdk_mp) {
494 netdev_->n_txq = NR_QUEUE;
495 netdev_->n_rxq = NR_QUEUE;
496 err = dpdk_eth_dev_init(netdev);
501 list_push_back(&dpdk_list, &netdev->list_node);
504 ovs_mutex_unlock(&netdev->mutex);
509 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
510 unsigned int *port_no)
514 if (strncmp(dev_name, prefix, strlen(prefix))) {
518 cport = dev_name + strlen(prefix);
519 *port_no = strtol(cport, 0, 0); /* string must be null terminated */
524 netdev_dpdk_construct(struct netdev *netdev)
526 unsigned int port_no;
529 if (rte_eal_init_ret) {
530 return rte_eal_init_ret;
533 /* Names always start with "dpdk" */
534 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
539 ovs_mutex_lock(&dpdk_mutex);
540 err = netdev_dpdk_init(netdev, port_no);
541 ovs_mutex_unlock(&dpdk_mutex);
546 netdev_dpdk_destruct(struct netdev *netdev_)
548 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
550 ovs_mutex_lock(&dev->mutex);
551 rte_eth_dev_stop(dev->port_id);
552 ovs_mutex_unlock(&dev->mutex);
554 ovs_mutex_lock(&dpdk_mutex);
555 list_remove(&dev->list_node);
556 dpdk_mp_put(dev->dpdk_mp);
557 ovs_mutex_unlock(&dpdk_mutex);
559 ovs_mutex_destroy(&dev->mutex);
563 netdev_dpdk_dealloc(struct netdev *netdev_)
565 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
571 netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
573 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
575 ovs_mutex_lock(&dev->mutex);
577 /* XXX: Allow to configure number of queues. */
578 smap_add_format(args, "configured_rx_queues", "%u", netdev_->n_rxq);
579 smap_add_format(args, "configured_tx_queues", "%u", netdev_->n_rxq);
580 ovs_mutex_unlock(&dev->mutex);
586 netdev_dpdk_get_numa_id(const struct netdev *netdev_)
588 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
590 return netdev->socket_id;
593 /* Sets the number of tx queues and rx queues for the dpdk interface.
594 * If the configuration fails, do not try restoring its old configuration
595 * and just returns the error. */
597 netdev_dpdk_set_multiq(struct netdev *netdev_, unsigned int n_txq,
600 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
603 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
607 ovs_mutex_lock(&netdev->mutex);
608 rte_eth_dev_stop(netdev->port_id);
609 netdev->up.n_txq = n_txq;
610 netdev->up.n_rxq = n_rxq;
611 err = dpdk_eth_dev_init(netdev);
612 ovs_mutex_unlock(&netdev->mutex);
617 static struct netdev_rxq *
618 netdev_dpdk_rxq_alloc(void)
620 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
625 static struct netdev_rxq_dpdk *
626 netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
628 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
632 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
634 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
635 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
637 ovs_mutex_lock(&netdev->mutex);
638 rx->port_id = netdev->port_id;
639 ovs_mutex_unlock(&netdev->mutex);
645 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
650 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
652 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
658 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
660 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
663 while (nb_tx != txq->count) {
666 ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
675 if (OVS_UNLIKELY(nb_tx != txq->count)) {
676 /* free buffers, which we couldn't transmit, one at a time (each
677 * packet could come from a different mempool) */
680 for (i = nb_tx; i < txq->count; i++) {
681 rte_pktmbuf_free_seg(txq->burst_pkts[i]);
683 ovs_mutex_lock(&dev->mutex);
684 dev->stats.tx_dropped += txq->count-nb_tx;
685 ovs_mutex_unlock(&dev->mutex);
689 txq->tsc = rte_get_timer_cycles();
693 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
695 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
697 if (txq->count == 0) {
700 rte_spinlock_lock(&txq->tx_lock);
701 dpdk_queue_flush__(dev, qid);
702 rte_spinlock_unlock(&txq->tx_lock);
706 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
709 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
710 struct netdev *netdev = rx->up.netdev;
711 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
714 /* There is only one tx queue for this core. Do not flush other
716 if (rxq_->queue_id == rte_lcore_id()) {
717 dpdk_queue_flush(dev, rxq_->queue_id);
720 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
721 (struct rte_mbuf **) packets,
722 MIN((int)NETDEV_MAX_RX_BATCH,
723 (int)MAX_RX_QUEUE_LEN));
734 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
735 struct rte_mbuf **pkts, int cnt)
737 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
742 rte_spinlock_lock(&txq->tx_lock);
744 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
745 int tocopy = MIN(freeslots, cnt-i);
747 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
748 tocopy * sizeof (struct rte_mbuf *));
750 txq->count += tocopy;
753 if (txq->count == MAX_TX_QUEUE_LEN) {
754 dpdk_queue_flush__(dev, qid);
756 diff_tsc = rte_get_timer_cycles() - txq->tsc;
757 if (diff_tsc >= DRAIN_TSC) {
758 dpdk_queue_flush__(dev, qid);
761 rte_spinlock_unlock(&txq->tx_lock);
764 /* Tx function. Transmit packets indefinitely */
766 dpdk_do_tx_copy(struct netdev *netdev, struct dpif_packet ** pkts, int cnt)
767 OVS_NO_THREAD_SAFETY_ANALYSIS
769 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
770 struct rte_mbuf *mbufs[cnt];
775 /* If we are on a non pmd thread we have to use the mempool mutex, because
776 * every non pmd thread shares the same mempool cache */
778 if (!thread_is_pmd()) {
779 ovs_mutex_lock(&nonpmd_mempool_mutex);
782 for (i = 0; i < cnt; i++) {
783 int size = ofpbuf_size(&pkts[i]->ofpbuf);
785 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
786 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
787 (int)size , dev->max_packet_len);
793 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
795 if (!mbufs[newcnt]) {
800 /* We have to do a copy for now */
801 memcpy(mbufs[newcnt]->pkt.data, ofpbuf_data(&pkts[i]->ofpbuf), size);
803 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
804 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
809 if (OVS_UNLIKELY(dropped)) {
810 ovs_mutex_lock(&dev->mutex);
811 dev->stats.tx_dropped += dropped;
812 ovs_mutex_unlock(&dev->mutex);
815 dpdk_queue_pkts(dev, NON_PMD_THREAD_TX_QUEUE, mbufs, newcnt);
816 dpdk_queue_flush(dev, NON_PMD_THREAD_TX_QUEUE);
818 if (!thread_is_pmd()) {
819 ovs_mutex_unlock(&nonpmd_mempool_mutex);
824 netdev_dpdk_send(struct netdev *netdev, int qid, struct dpif_packet **pkts,
825 int cnt, bool may_steal)
827 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
831 if (!may_steal || pkts[0]->ofpbuf.source != OFPBUF_DPDK) {
832 dpdk_do_tx_copy(netdev, pkts, cnt);
835 for (i = 0; i < cnt; i++) {
836 dpif_packet_delete(pkts[i]);
843 qid = rte_lcore_id() % NR_QUEUE;
845 for (i = 0; i < cnt; i++) {
846 int size = ofpbuf_size(&pkts[i]->ofpbuf);
847 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
848 if (next_tx_idx != i) {
849 dpdk_queue_pkts(dev, qid,
850 (struct rte_mbuf **)&pkts[next_tx_idx],
854 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
855 (int)size , dev->max_packet_len);
857 dpif_packet_delete(pkts[i]);
862 if (next_tx_idx != cnt) {
863 dpdk_queue_pkts(dev, qid,
864 (struct rte_mbuf **)&pkts[next_tx_idx],
868 if (OVS_UNLIKELY(dropped)) {
869 ovs_mutex_lock(&dev->mutex);
870 dev->stats.tx_dropped += dropped;
871 ovs_mutex_unlock(&dev->mutex);
880 netdev_dpdk_set_etheraddr(struct netdev *netdev,
881 const uint8_t mac[ETH_ADDR_LEN])
883 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
885 ovs_mutex_lock(&dev->mutex);
886 if (!eth_addr_equals(dev->hwaddr, mac)) {
887 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
888 netdev_change_seq_changed(netdev);
890 ovs_mutex_unlock(&dev->mutex);
896 netdev_dpdk_get_etheraddr(const struct netdev *netdev,
897 uint8_t mac[ETH_ADDR_LEN])
899 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
901 ovs_mutex_lock(&dev->mutex);
902 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
903 ovs_mutex_unlock(&dev->mutex);
909 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
911 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
913 ovs_mutex_lock(&dev->mutex);
915 ovs_mutex_unlock(&dev->mutex);
921 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
923 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
925 struct dpdk_mp *old_mp;
928 ovs_mutex_lock(&dpdk_mutex);
929 ovs_mutex_lock(&dev->mutex);
930 if (dev->mtu == mtu) {
935 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
941 rte_eth_dev_stop(dev->port_id);
944 old_mp = dev->dpdk_mp;
947 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
949 err = dpdk_eth_dev_init(dev);
953 dev->dpdk_mp = old_mp;
954 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
955 dpdk_eth_dev_init(dev);
960 netdev_change_seq_changed(netdev);
962 ovs_mutex_unlock(&dev->mutex);
963 ovs_mutex_unlock(&dpdk_mutex);
968 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
971 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
973 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
974 struct rte_eth_stats rte_stats;
977 netdev_dpdk_get_carrier(netdev, &gg);
978 ovs_mutex_lock(&dev->mutex);
979 rte_eth_stats_get(dev->port_id, &rte_stats);
981 memset(stats, 0, sizeof(*stats));
983 stats->rx_packets = rte_stats.ipackets;
984 stats->tx_packets = rte_stats.opackets;
985 stats->rx_bytes = rte_stats.ibytes;
986 stats->tx_bytes = rte_stats.obytes;
987 stats->rx_errors = rte_stats.ierrors;
988 stats->tx_errors = rte_stats.oerrors;
989 stats->multicast = rte_stats.imcasts;
991 stats->tx_dropped = dev->stats.tx_dropped;
992 ovs_mutex_unlock(&dev->mutex);
998 netdev_dpdk_get_features(const struct netdev *netdev_,
999 enum netdev_features *current,
1000 enum netdev_features *advertised OVS_UNUSED,
1001 enum netdev_features *supported OVS_UNUSED,
1002 enum netdev_features *peer OVS_UNUSED)
1004 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1005 struct rte_eth_link link;
1007 ovs_mutex_lock(&dev->mutex);
1009 ovs_mutex_unlock(&dev->mutex);
1011 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1012 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1013 *current = NETDEV_F_AUTONEG;
1015 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1016 if (link.link_speed == ETH_LINK_SPEED_10) {
1017 *current = NETDEV_F_10MB_HD;
1019 if (link.link_speed == ETH_LINK_SPEED_100) {
1020 *current = NETDEV_F_100MB_HD;
1022 if (link.link_speed == ETH_LINK_SPEED_1000) {
1023 *current = NETDEV_F_1GB_HD;
1025 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1026 if (link.link_speed == ETH_LINK_SPEED_10) {
1027 *current = NETDEV_F_10MB_FD;
1029 if (link.link_speed == ETH_LINK_SPEED_100) {
1030 *current = NETDEV_F_100MB_FD;
1032 if (link.link_speed == ETH_LINK_SPEED_1000) {
1033 *current = NETDEV_F_1GB_FD;
1035 if (link.link_speed == ETH_LINK_SPEED_10000) {
1036 *current = NETDEV_F_10GB_FD;
1044 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1046 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1049 ovs_mutex_lock(&dev->mutex);
1050 ifindex = dev->port_id;
1051 ovs_mutex_unlock(&dev->mutex);
1057 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1059 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1061 ovs_mutex_lock(&dev->mutex);
1062 check_link_status(dev);
1063 *carrier = dev->link.link_status;
1064 ovs_mutex_unlock(&dev->mutex);
1069 static long long int
1070 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1072 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1073 long long int carrier_resets;
1075 ovs_mutex_lock(&dev->mutex);
1076 carrier_resets = dev->link_reset_cnt;
1077 ovs_mutex_unlock(&dev->mutex);
1079 return carrier_resets;
1083 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1084 long long int interval OVS_UNUSED)
1090 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1091 enum netdev_flags off, enum netdev_flags on,
1092 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1096 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1100 *old_flagsp = dev->flags;
1104 if (dev->flags == *old_flagsp) {
1108 if (dev->flags & NETDEV_UP) {
1109 err = rte_eth_dev_start(dev->port_id);
1114 if (dev->flags & NETDEV_PROMISC) {
1115 rte_eth_promiscuous_enable(dev->port_id);
1118 if (!(dev->flags & NETDEV_UP)) {
1119 rte_eth_dev_stop(dev->port_id);
1126 netdev_dpdk_update_flags(struct netdev *netdev_,
1127 enum netdev_flags off, enum netdev_flags on,
1128 enum netdev_flags *old_flagsp)
1130 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1133 ovs_mutex_lock(&netdev->mutex);
1134 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1135 ovs_mutex_unlock(&netdev->mutex);
1141 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1143 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1144 struct rte_eth_dev_info dev_info;
1146 if (dev->port_id < 0)
1149 ovs_mutex_lock(&dev->mutex);
1150 rte_eth_dev_info_get(dev->port_id, &dev_info);
1151 ovs_mutex_unlock(&dev->mutex);
1153 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1155 smap_add_format(args, "port_no", "%d", dev->port_id);
1156 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1157 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1158 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1159 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1160 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1161 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1162 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1163 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1164 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1165 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1167 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1168 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1174 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1175 OVS_REQUIRES(dev->mutex)
1177 enum netdev_flags old_flags;
1180 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1182 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1187 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1188 const char *argv[], void *aux OVS_UNUSED)
1192 if (!strcasecmp(argv[argc - 1], "up")) {
1194 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1197 unixctl_command_reply_error(conn, "Invalid Admin State");
1202 struct netdev *netdev = netdev_from_name(argv[1]);
1203 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1204 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1206 ovs_mutex_lock(&dpdk_dev->mutex);
1207 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1208 ovs_mutex_unlock(&dpdk_dev->mutex);
1210 netdev_close(netdev);
1212 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1213 netdev_close(netdev);
1217 struct netdev_dpdk *netdev;
1219 ovs_mutex_lock(&dpdk_mutex);
1220 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1221 ovs_mutex_lock(&netdev->mutex);
1222 netdev_dpdk_set_admin_state__(netdev, up);
1223 ovs_mutex_unlock(&netdev->mutex);
1225 ovs_mutex_unlock(&dpdk_mutex);
1227 unixctl_command_reply(conn, "OK");
1231 dpdk_common_init(void)
1233 unixctl_command_register("netdev-dpdk/set-admin-state",
1234 "[netdev] up|down", 1, 2,
1235 netdev_dpdk_set_admin_state, NULL);
1237 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1241 dpdk_class_init(void)
1245 result = rte_eal_pci_probe();
1247 VLOG_ERR("Cannot probe PCI");
1251 VLOG_INFO("Ethernet Device Count: %d", (int)rte_eth_dev_count());
1259 dpdk_ring_create(const char dev_name[], unsigned int port_no,
1260 unsigned int *eth_port_id)
1262 struct dpdk_ring *ivshmem;
1266 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1267 if (ivshmem == NULL) {
1271 err = snprintf(ring_name, 10, "%s_tx", dev_name);
1276 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, 0);
1277 if (ivshmem->cring_tx == NULL) {
1282 err = snprintf(ring_name, 10, "%s_rx", dev_name);
1287 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0, 0);
1288 if (ivshmem->cring_rx == NULL) {
1293 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
1294 &ivshmem->cring_tx, 1, SOCKET0);
1301 ivshmem->user_port_id = port_no;
1302 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1303 list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1305 *eth_port_id = ivshmem->eth_port_id;
1310 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1312 struct dpdk_ring *ivshmem;
1313 unsigned int port_no;
1316 /* Names always start with "dpdkr" */
1317 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1322 /* look through our list to find the device */
1323 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1324 if (ivshmem->user_port_id == port_no) {
1325 VLOG_INFO("Found dpdk ring device %s:\n", dev_name);
1326 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1330 /* Need to create the device rings */
1331 return dpdk_ring_create(dev_name, port_no, eth_port_id);
1335 netdev_dpdk_ring_construct(struct netdev *netdev)
1337 unsigned int port_no = 0;
1340 if (rte_eal_init_ret) {
1341 return rte_eal_init_ret;
1344 ovs_mutex_lock(&dpdk_mutex);
1346 err = dpdk_ring_open(netdev->name, &port_no);
1351 err = netdev_dpdk_init(netdev, port_no);
1354 ovs_mutex_unlock(&dpdk_mutex);
1358 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, MULTIQ) \
1362 NULL, /* netdev_dpdk_run */ \
1363 NULL, /* netdev_dpdk_wait */ \
1365 netdev_dpdk_alloc, \
1367 netdev_dpdk_destruct, \
1368 netdev_dpdk_dealloc, \
1369 netdev_dpdk_get_config, \
1370 NULL, /* netdev_dpdk_set_config */ \
1371 NULL, /* get_tunnel_config */ \
1372 netdev_dpdk_get_numa_id, /* get_numa_id */ \
1373 MULTIQ, /* set_multiq */ \
1375 netdev_dpdk_send, /* send */ \
1376 NULL, /* send_wait */ \
1378 netdev_dpdk_set_etheraddr, \
1379 netdev_dpdk_get_etheraddr, \
1380 netdev_dpdk_get_mtu, \
1381 netdev_dpdk_set_mtu, \
1382 netdev_dpdk_get_ifindex, \
1383 netdev_dpdk_get_carrier, \
1384 netdev_dpdk_get_carrier_resets, \
1385 netdev_dpdk_set_miimon, \
1386 netdev_dpdk_get_stats, \
1387 netdev_dpdk_get_features, \
1388 NULL, /* set_advertisements */ \
1390 NULL, /* set_policing */ \
1391 NULL, /* get_qos_types */ \
1392 NULL, /* get_qos_capabilities */ \
1393 NULL, /* get_qos */ \
1394 NULL, /* set_qos */ \
1395 NULL, /* get_queue */ \
1396 NULL, /* set_queue */ \
1397 NULL, /* delete_queue */ \
1398 NULL, /* get_queue_stats */ \
1399 NULL, /* queue_dump_start */ \
1400 NULL, /* queue_dump_next */ \
1401 NULL, /* queue_dump_done */ \
1402 NULL, /* dump_queue_stats */ \
1404 NULL, /* get_in4 */ \
1405 NULL, /* set_in4 */ \
1406 NULL, /* get_in6 */ \
1407 NULL, /* add_router */ \
1408 NULL, /* get_next_hop */ \
1409 netdev_dpdk_get_status, \
1410 NULL, /* arp_lookup */ \
1412 netdev_dpdk_update_flags, \
1414 netdev_dpdk_rxq_alloc, \
1415 netdev_dpdk_rxq_construct, \
1416 netdev_dpdk_rxq_destruct, \
1417 netdev_dpdk_rxq_dealloc, \
1418 netdev_dpdk_rxq_recv, \
1419 NULL, /* rx_wait */ \
1420 NULL, /* rxq_drain */ \
1424 dpdk_init(int argc, char **argv)
1428 if (argc < 2 || strcmp(argv[1], "--dpdk"))
1431 /* Make sure program name passed to rte_eal_init() is vswitchd. */
1437 /* Make sure things are initialized ... */
1438 result = rte_eal_init(argc, argv);
1440 ovs_abort(result, "Cannot init EAL\n");
1443 rte_memzone_dump(stdout);
1444 rte_eal_init_ret = 0;
1446 if (argc > result) {
1447 argv[result] = argv[0];
1450 /* We are called from the main thread here */
1451 thread_set_nonpmd();
1456 const struct netdev_class dpdk_class =
1460 netdev_dpdk_construct,
1461 netdev_dpdk_set_multiq);
1463 const struct netdev_class dpdk_ring_class =
1467 netdev_dpdk_ring_construct,
1471 netdev_dpdk_register(void)
1473 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1475 if (rte_eal_init_ret) {
1479 if (ovsthread_once_start(&once)) {
1481 netdev_register_provider(&dpdk_class);
1482 netdev_register_provider(&dpdk_ring_class);
1483 ovsthread_once_done(&once);
1488 pmd_thread_setaffinity_cpu(int cpu)
1494 CPU_SET(cpu, &cpuset);
1495 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
1497 VLOG_ERR("Thread affinity error %d",err);
1500 /* lcore_id 0 is reseved for use by non pmd threads. */
1501 RTE_PER_LCORE(_lcore_id) = cpu + 1;
1507 thread_set_nonpmd(void)
1509 /* We cannot have RTE_MAX_LCORE pmd threads, because lcore_id 0 is reserved
1510 * for non pmd threads */
1511 BUILD_ASSERT(NR_PMD_THREADS < RTE_MAX_LCORE);
1512 /* We have to use 0 to allow non pmd threads to perform certain DPDK
1513 * operations, like rte_eth_dev_configure(). */
1514 RTE_PER_LCORE(_lcore_id) = 0;
1520 return rte_lcore_id() != 0;