2 * Copyright (c) 2014, 2015, 2016 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.
30 #include <sys/types.h>
34 #include "dp-packet.h"
35 #include "dpif-netdev.h"
36 #include "fatal-signal.h"
37 #include "netdev-dpdk.h"
38 #include "netdev-provider.h"
39 #include "netdev-vport.h"
41 #include "openvswitch/list.h"
42 #include "openvswitch/ofp-print.h"
43 #include "openvswitch/vlog.h"
45 #include "ovs-thread.h"
51 #include "unaligned.h"
55 #include "rte_config.h"
57 #include "rte_meter.h"
58 #include "rte_virtio_net.h"
60 VLOG_DEFINE_THIS_MODULE(dpdk
);
61 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
63 #define DPDK_PORT_WATCHDOG_INTERVAL 5
65 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
66 #define OVS_VPORT_DPDK "ovs_dpdk"
69 * need to reserve tons of extra space in the mbufs so we can align the
70 * DMA addresses to 4KB.
71 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
72 * performance for standard Ethernet MTU.
74 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN + (2 * VLAN_HEADER_LEN))
75 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
76 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
77 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len)- ETHER_HDR_LEN - ETHER_CRC_LEN)
78 #define MBUF_SIZE(mtu) ( MTU_TO_MAX_FRAME_LEN(mtu) \
79 + sizeof(struct dp_packet) \
80 + RTE_PKTMBUF_HEADROOM)
81 #define NETDEV_DPDK_MBUF_ALIGN 1024
83 /* Max and min number of packets in the mempool. OVS tries to allocate a
84 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
85 * enough hugepages) we keep halving the number until the allocation succeeds
86 * or we reach MIN_NB_MBUF */
88 #define MAX_NB_MBUF (4096 * 64)
89 #define MIN_NB_MBUF (4096 * 4)
90 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
92 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
93 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
) == 0);
95 /* The smallest possible NB_MBUF that we're going to try should be a multiple
96 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
97 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
))
102 #define NIC_PORT_RX_Q_SIZE 2048 /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
103 #define NIC_PORT_TX_Q_SIZE 2048 /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
105 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
106 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
107 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
108 * yet mapped to another queue. */
110 static char *cuse_dev_name
= NULL
; /* Character device cuse_dev_name. */
111 static char *vhost_sock_dir
= NULL
; /* Location of vhost-user sockets */
114 * Maximum amount of time in micro seconds to try and enqueue to vhost.
116 #define VHOST_ENQ_RETRY_USECS 100
118 static const struct rte_eth_conf port_conf
= {
120 .mq_mode
= ETH_MQ_RX_RSS
,
122 .header_split
= 0, /* Header Split disabled */
123 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
124 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
125 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
131 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
135 .mq_mode
= ETH_MQ_TX_NONE
,
139 enum { MAX_TX_QUEUE_LEN
= 384 };
140 enum { DPDK_RING_SIZE
= 256 };
141 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
142 enum { DRAIN_TSC
= 200000ULL };
149 static int rte_eal_init_ret
= ENODEV
;
151 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
153 /* Quality of Service */
155 /* An instance of a QoS configuration. Always associated with a particular
158 * Each QoS implementation subclasses this with whatever additional data it
162 const struct dpdk_qos_ops
*ops
;
165 /* A particular implementation of dpdk QoS operations.
167 * The functions below return 0 if successful or a positive errno value on
168 * failure, except where otherwise noted. All of them must be provided, except
169 * where otherwise noted.
171 struct dpdk_qos_ops
{
173 /* Name of the QoS type */
174 const char *qos_name
;
176 /* Called to construct the QoS implementation on 'netdev'. The
177 * implementation should make the appropriate calls to configure QoS
178 * according to 'details'. The implementation may assume that any current
179 * QoS configuration already installed should be destroyed before
180 * constructing the new configuration.
182 * The contents of 'details' should be documented as valid for 'ovs_name'
183 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
184 * (which is built as ovs-vswitchd.conf.db(8)).
186 * This function must return 0 if and only if it sets 'netdev->qos_conf'
187 * to an initialized 'struct qos_conf'.
189 * For all QoS implementations it should always be non-null.
191 int (*qos_construct
)(struct netdev
*netdev
, const struct smap
*details
);
193 /* Destroys the data structures allocated by the implementation as part of
196 * For all QoS implementations it should always be non-null.
198 void (*qos_destruct
)(struct netdev
*netdev
, struct qos_conf
*conf
);
200 /* Retrieves details of 'netdev->qos_conf' configuration into 'details'.
202 * The contents of 'details' should be documented as valid for 'ovs_name'
203 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
204 * (which is built as ovs-vswitchd.conf.db(8)).
206 int (*qos_get
)(const struct netdev
*netdev
, struct smap
*details
);
208 /* Reconfigures 'netdev->qos_conf' according to 'details', performing any
209 * required calls to complete the reconfiguration.
211 * The contents of 'details' should be documented as valid for 'ovs_name'
212 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
213 * (which is built as ovs-vswitchd.conf.db(8)).
215 * This function may be null if 'qos_conf' is not configurable.
217 int (*qos_set
)(struct netdev
*netdev
, const struct smap
*details
);
219 /* Modify an array of rte_mbufs. The modification is specific to
220 * each qos implementation.
222 * The function should take and array of mbufs and an int representing
223 * the current number of mbufs present in the array.
225 * After the function has performed a qos modification to the array of
226 * mbufs it returns an int representing the number of mbufs now present in
227 * the array. This value is can then be passed to the port send function
228 * along with the modified array for transmission.
230 * For all QoS implementations it should always be non-null.
232 int (*qos_run
)(struct netdev
*netdev
, struct rte_mbuf
**pkts
,
236 /* dpdk_qos_ops for each type of user space QoS implementation */
237 static const struct dpdk_qos_ops egress_policer_ops
;
240 * Array of dpdk_qos_ops, contains pointer to all supported QoS
243 static const struct dpdk_qos_ops
*const qos_confs
[] = {
248 /* Contains all 'struct dpdk_dev's. */
249 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
250 = OVS_LIST_INITIALIZER(&dpdk_list
);
252 static struct ovs_list dpdk_mp_list
OVS_GUARDED_BY(dpdk_mutex
)
253 = OVS_LIST_INITIALIZER(&dpdk_mp_list
);
255 /* This mutex must be used by non pmd threads when allocating or freeing
256 * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
257 * use mempools, a non pmd thread should hold this mutex while calling them */
258 static struct ovs_mutex nonpmd_mempool_mutex
= OVS_MUTEX_INITIALIZER
;
261 struct rte_mempool
*mp
;
265 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
268 /* There should be one 'struct dpdk_tx_queue' created for
270 struct dpdk_tx_queue
{
271 bool flush_tx
; /* Set to true to flush queue everytime */
272 /* pkts are queued. */
274 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
275 * from concurrent access. It is used only
276 * if the queue is shared among different
277 * pmd threads (see 'txq_needs_locking'). */
278 int map
; /* Mapping of configured vhost-user queues
279 * to enabled by guest. */
281 struct rte_mbuf
*burst_pkts
[MAX_TX_QUEUE_LEN
];
284 /* dpdk has no way to remove dpdk ring ethernet devices
285 so we have to keep them around once they've been created
288 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
289 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
292 /* For the client rings */
293 struct rte_ring
*cring_tx
;
294 struct rte_ring
*cring_rx
;
295 unsigned int user_port_id
; /* User given port no, parsed from port name */
296 int eth_port_id
; /* ethernet device port id */
297 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
304 enum dpdk_dev_type type
;
306 struct dpdk_tx_queue
*tx_q
;
308 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
310 struct dpdk_mp
*dpdk_mp
;
314 struct netdev_stats stats
;
316 rte_spinlock_t stats_lock
;
318 struct eth_addr hwaddr
;
319 enum netdev_flags flags
;
321 struct rte_eth_link link
;
324 /* The user might request more txqs than the NIC has. We remap those
325 * ('up.n_txq') on these ('real_n_txq').
326 * If the numbers match, 'txq_needs_locking' is false, otherwise it is
327 * true and we will take a spinlock on transmission */
330 bool txq_needs_locking
;
332 /* virtio-net structure for vhost device */
333 OVSRCU_TYPE(struct virtio_net
*) virtio_dev
;
335 /* Identifier used to distinguish vhost devices from each other */
336 char vhost_id
[PATH_MAX
];
339 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
341 /* QoS configuration and lock for the device */
342 struct qos_conf
*qos_conf
;
343 rte_spinlock_t qos_lock
;
347 struct netdev_rxq_dpdk
{
348 struct netdev_rxq up
;
352 static bool dpdk_thread_is_pmd(void);
354 static int netdev_dpdk_construct(struct netdev
*);
356 struct virtio_net
* netdev_dpdk_get_virtio(const struct netdev_dpdk
*dev
);
359 is_dpdk_class(const struct netdev_class
*class)
361 return class->construct
== netdev_dpdk_construct
;
364 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
365 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
366 * value, insufficient buffers are allocated to accomodate the packet in its
367 * entirety. Furthermore, certain drivers need to ensure that there is also
368 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
369 * frames). If the RX buffer is too small, then the driver enables scatter RX
370 * behaviour, which reduces performance. To prevent this, use a buffer size that
371 * is closest to 'mtu', but which satisfies the aforementioned criteria.
374 dpdk_buf_size(int mtu
)
376 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
377 NETDEV_DPDK_MBUF_ALIGN
);
380 /* XXX: use dpdk malloc for entire OVS. in fact huge page should be used
381 * for all other segments data, bss and text. */
384 dpdk_rte_mzalloc(size_t sz
)
388 ptr
= rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
395 /* XXX this function should be called only by pmd threads (or by non pmd
396 * threads holding the nonpmd_mempool_mutex) */
398 free_dpdk_buf(struct dp_packet
*p
)
400 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
402 rte_pktmbuf_free(pkt
);
406 ovs_rte_pktmbuf_init(struct rte_mempool
*mp
,
407 void *opaque_arg OVS_UNUSED
,
409 unsigned i OVS_UNUSED
)
411 struct rte_mbuf
*m
= _m
;
413 rte_pktmbuf_init(mp
, opaque_arg
, _m
, i
);
415 dp_packet_init_dpdk((struct dp_packet
*) m
, m
->buf_len
);
418 static struct dpdk_mp
*
419 dpdk_mp_get(int socket_id
, int mtu
) OVS_REQUIRES(dpdk_mutex
)
421 struct dpdk_mp
*dmp
= NULL
;
422 char mp_name
[RTE_MEMPOOL_NAMESIZE
];
424 struct rte_pktmbuf_pool_private mbp_priv
;
426 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
427 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
433 dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
434 dmp
->socket_id
= socket_id
;
437 mbp_priv
.mbuf_data_room_size
= MBUF_SIZE(mtu
) - sizeof(struct dp_packet
);
438 mbp_priv
.mbuf_priv_size
= sizeof (struct dp_packet
) - sizeof (struct rte_mbuf
);
440 mp_size
= MAX_NB_MBUF
;
442 if (snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
, "ovs_mp_%d_%d_%u",
443 dmp
->mtu
, dmp
->socket_id
, mp_size
) < 0) {
447 dmp
->mp
= rte_mempool_create(mp_name
, mp_size
, MBUF_SIZE(mtu
),
449 sizeof(struct rte_pktmbuf_pool_private
),
450 rte_pktmbuf_pool_init
, &mbp_priv
,
451 ovs_rte_pktmbuf_init
, NULL
,
453 } while (!dmp
->mp
&& rte_errno
== ENOMEM
&& (mp_size
/= 2) >= MIN_NB_MBUF
);
455 if (dmp
->mp
== NULL
) {
458 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", mp_name
, mp_size
);
461 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
466 dpdk_mp_put(struct dpdk_mp
*dmp
)
474 ovs_assert(dmp
->refcount
>= 0);
477 /* I could not find any API to destroy mp. */
478 if (dmp
->refcount
== 0) {
479 list_delete(dmp
->list_node
);
480 /* destroy mp-pool. */
486 check_link_status(struct netdev_dpdk
*dev
)
488 struct rte_eth_link link
;
490 rte_eth_link_get_nowait(dev
->port_id
, &link
);
492 if (dev
->link
.link_status
!= link
.link_status
) {
493 netdev_change_seq_changed(&dev
->up
);
495 dev
->link_reset_cnt
++;
497 if (dev
->link
.link_status
) {
498 VLOG_DBG_RL(&rl
, "Port %d Link Up - speed %u Mbps - %s",
499 dev
->port_id
, (unsigned)dev
->link
.link_speed
,
500 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
501 ("full-duplex") : ("half-duplex"));
503 VLOG_DBG_RL(&rl
, "Port %d Link Down", dev
->port_id
);
509 dpdk_watchdog(void *dummy OVS_UNUSED
)
511 struct netdev_dpdk
*dev
;
513 pthread_detach(pthread_self());
516 ovs_mutex_lock(&dpdk_mutex
);
517 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
518 ovs_mutex_lock(&dev
->mutex
);
519 check_link_status(dev
);
520 ovs_mutex_unlock(&dev
->mutex
);
522 ovs_mutex_unlock(&dpdk_mutex
);
523 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
530 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
535 /* A device may report more queues than it makes available (this has
536 * been observed for Intel xl710, which reserves some of them for
537 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
538 * available. When this happens we can retry the configuration
539 * and request less queues */
540 while (n_rxq
&& n_txq
) {
542 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
545 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &port_conf
);
550 for (i
= 0; i
< n_txq
; i
++) {
551 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, NIC_PORT_TX_Q_SIZE
,
552 dev
->socket_id
, NULL
);
554 VLOG_INFO("Interface %s txq(%d) setup error: %s",
555 dev
->up
.name
, i
, rte_strerror(-diag
));
561 /* Retry with less tx queues */
566 for (i
= 0; i
< n_rxq
; i
++) {
567 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, NIC_PORT_RX_Q_SIZE
,
568 dev
->socket_id
, NULL
,
571 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
572 dev
->up
.name
, i
, rte_strerror(-diag
));
578 /* Retry with less rx queues */
583 dev
->up
.n_rxq
= n_rxq
;
584 dev
->real_n_txq
= n_txq
;
594 dpdk_eth_dev_init(struct netdev_dpdk
*dev
) OVS_REQUIRES(dpdk_mutex
)
596 struct rte_pktmbuf_pool_private
*mbp_priv
;
597 struct rte_eth_dev_info info
;
598 struct ether_addr eth_addr
;
602 if (dev
->port_id
< 0 || dev
->port_id
>= rte_eth_dev_count()) {
606 rte_eth_dev_info_get(dev
->port_id
, &info
);
608 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
609 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
611 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
613 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
614 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
618 diag
= rte_eth_dev_start(dev
->port_id
);
620 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
621 rte_strerror(-diag
));
625 rte_eth_promiscuous_enable(dev
->port_id
);
626 rte_eth_allmulticast_enable(dev
->port_id
);
628 memset(ð_addr
, 0x0, sizeof(eth_addr
));
629 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
630 VLOG_INFO_RL(&rl
, "Port %d: "ETH_ADDR_FMT
"",
631 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
633 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
634 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
636 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
637 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
639 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
643 static struct netdev_dpdk
*
644 netdev_dpdk_cast(const struct netdev
*netdev
)
646 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
649 static struct netdev
*
650 netdev_dpdk_alloc(void)
652 struct netdev_dpdk
*dev
= dpdk_rte_mzalloc(sizeof *dev
);
657 netdev_dpdk_alloc_txq(struct netdev_dpdk
*dev
, unsigned int n_txqs
)
661 dev
->tx_q
= dpdk_rte_mzalloc(n_txqs
* sizeof *dev
->tx_q
);
662 for (i
= 0; i
< n_txqs
; i
++) {
663 int numa_id
= ovs_numa_get_numa_id(i
);
665 if (!dev
->txq_needs_locking
) {
666 /* Each index is considered as a cpu core id, since there should
667 * be one tx queue for each cpu core. If the corresponding core
668 * is not on the same numa node as 'dev', flags the
670 dev
->tx_q
[i
].flush_tx
= dev
->socket_id
== numa_id
;
672 /* Queues are shared among CPUs. Always flush */
673 dev
->tx_q
[i
].flush_tx
= true;
676 /* Initialize map for vhost devices. */
677 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
678 rte_spinlock_init(&dev
->tx_q
[i
].tx_lock
);
683 netdev_dpdk_init(struct netdev
*netdev
, unsigned int port_no
,
684 enum dpdk_dev_type type
)
685 OVS_REQUIRES(dpdk_mutex
)
687 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
692 ovs_mutex_init(&dev
->mutex
);
693 ovs_mutex_lock(&dev
->mutex
);
695 rte_spinlock_init(&dev
->stats_lock
);
697 /* If the 'sid' is negative, it means that the kernel fails
698 * to obtain the pci numa info. In that situation, always
700 if (type
== DPDK_DEV_ETH
) {
701 sid
= rte_eth_dev_socket_id(port_no
);
703 sid
= rte_lcore_to_socket_id(rte_get_master_lcore());
706 dev
->socket_id
= sid
< 0 ? SOCKET0
: sid
;
707 dev
->port_id
= port_no
;
710 dev
->mtu
= ETHER_MTU
;
711 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
713 buf_size
= dpdk_buf_size(dev
->mtu
);
714 dev
->dpdk_mp
= dpdk_mp_get(dev
->socket_id
, FRAME_LEN_TO_MTU(buf_size
));
720 /* Initialise QoS configuration to NULL and qos lock to unlocked */
721 dev
->qos_conf
= NULL
;
722 rte_spinlock_init(&dev
->qos_lock
);
724 netdev
->n_txq
= NR_QUEUE
;
725 netdev
->n_rxq
= NR_QUEUE
;
726 netdev
->requested_n_rxq
= NR_QUEUE
;
727 dev
->real_n_txq
= NR_QUEUE
;
729 if (type
== DPDK_DEV_ETH
) {
730 netdev_dpdk_alloc_txq(dev
, NR_QUEUE
);
731 err
= dpdk_eth_dev_init(dev
);
736 netdev_dpdk_alloc_txq(dev
, OVS_VHOST_MAX_QUEUE_NUM
);
739 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
745 ovs_mutex_unlock(&dev
->mutex
);
749 /* dev_name must be the prefix followed by a positive decimal number.
750 * (no leading + or - signs are allowed) */
752 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
753 unsigned int *port_no
)
757 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
761 cport
= dev_name
+ strlen(prefix
);
763 if (str_to_uint(cport
, 10, port_no
)) {
771 vhost_construct_helper(struct netdev
*netdev
) OVS_REQUIRES(dpdk_mutex
)
773 if (rte_eal_init_ret
) {
774 return rte_eal_init_ret
;
777 return netdev_dpdk_init(netdev
, -1, DPDK_DEV_VHOST
);
781 netdev_dpdk_vhost_cuse_construct(struct netdev
*netdev
)
783 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
786 ovs_mutex_lock(&dpdk_mutex
);
787 strncpy(dev
->vhost_id
, netdev
->name
, sizeof(dev
->vhost_id
));
788 err
= vhost_construct_helper(netdev
);
789 ovs_mutex_unlock(&dpdk_mutex
);
794 netdev_dpdk_vhost_user_construct(struct netdev
*netdev
)
796 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
797 const char *name
= netdev
->name
;
800 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
801 * the file system. '/' or '\' would traverse directories, so they're not
802 * acceptable in 'name'. */
803 if (strchr(name
, '/') || strchr(name
, '\\')) {
804 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
805 "A valid name must not include '/' or '\\'",
810 ovs_mutex_lock(&dpdk_mutex
);
811 /* Take the name of the vhost-user port and append it to the location where
812 * the socket is to be created, then register the socket.
814 snprintf(dev
->vhost_id
, sizeof(dev
->vhost_id
), "%s/%s",
815 vhost_sock_dir
, name
);
817 err
= rte_vhost_driver_register(dev
->vhost_id
);
819 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
822 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
823 VLOG_INFO("Socket %s created for vhost-user port %s\n",
824 dev
->vhost_id
, name
);
825 err
= vhost_construct_helper(netdev
);
828 ovs_mutex_unlock(&dpdk_mutex
);
833 netdev_dpdk_construct(struct netdev
*netdev
)
835 unsigned int port_no
;
838 if (rte_eal_init_ret
) {
839 return rte_eal_init_ret
;
842 /* Names always start with "dpdk" */
843 err
= dpdk_dev_parse_name(netdev
->name
, "dpdk", &port_no
);
848 ovs_mutex_lock(&dpdk_mutex
);
849 err
= netdev_dpdk_init(netdev
, port_no
, DPDK_DEV_ETH
);
850 ovs_mutex_unlock(&dpdk_mutex
);
855 netdev_dpdk_destruct(struct netdev
*netdev
)
857 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
859 ovs_mutex_lock(&dev
->mutex
);
860 rte_eth_dev_stop(dev
->port_id
);
861 ovs_mutex_unlock(&dev
->mutex
);
863 ovs_mutex_lock(&dpdk_mutex
);
865 ovs_list_remove(&dev
->list_node
);
866 dpdk_mp_put(dev
->dpdk_mp
);
867 ovs_mutex_unlock(&dpdk_mutex
);
871 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
873 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
875 /* Guest becomes an orphan if still attached. */
876 if (netdev_dpdk_get_virtio(dev
) != NULL
) {
877 VLOG_ERR("Removing port '%s' while vhost device still attached.",
879 VLOG_ERR("To restore connectivity after re-adding of port, VM on socket"
880 " '%s' must be restarted.",
884 if (rte_vhost_driver_unregister(dev
->vhost_id
)) {
885 VLOG_ERR("Unable to remove vhost-user socket %s", dev
->vhost_id
);
887 fatal_signal_remove_file_to_unlink(dev
->vhost_id
);
890 ovs_mutex_lock(&dpdk_mutex
);
892 ovs_list_remove(&dev
->list_node
);
893 dpdk_mp_put(dev
->dpdk_mp
);
894 ovs_mutex_unlock(&dpdk_mutex
);
898 netdev_dpdk_dealloc(struct netdev
*netdev
)
900 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
906 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
908 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
910 ovs_mutex_lock(&dev
->mutex
);
912 smap_add_format(args
, "requested_rx_queues", "%d", netdev
->requested_n_rxq
);
913 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
914 smap_add_format(args
, "requested_tx_queues", "%d", netdev
->n_txq
);
915 smap_add_format(args
, "configured_tx_queues", "%d", dev
->real_n_txq
);
916 ovs_mutex_unlock(&dev
->mutex
);
922 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
)
924 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
926 ovs_mutex_lock(&dev
->mutex
);
927 netdev
->requested_n_rxq
= MAX(smap_get_int(args
, "n_rxq",
928 netdev
->requested_n_rxq
), 1);
929 netdev_change_seq_changed(netdev
);
930 ovs_mutex_unlock(&dev
->mutex
);
936 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
938 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
940 return dev
->socket_id
;
943 /* Sets the number of tx queues and rx queues for the dpdk interface.
944 * If the configuration fails, do not try restoring its old configuration
945 * and just returns the error. */
947 netdev_dpdk_set_multiq(struct netdev
*netdev
, unsigned int n_txq
,
950 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
952 int old_rxq
, old_txq
;
954 if (netdev
->n_txq
== n_txq
&& netdev
->n_rxq
== n_rxq
) {
958 ovs_mutex_lock(&dpdk_mutex
);
959 ovs_mutex_lock(&dev
->mutex
);
961 rte_eth_dev_stop(dev
->port_id
);
963 old_txq
= netdev
->n_txq
;
964 old_rxq
= netdev
->n_rxq
;
965 netdev
->n_txq
= n_txq
;
966 netdev
->n_rxq
= n_rxq
;
969 err
= dpdk_eth_dev_init(dev
);
970 netdev_dpdk_alloc_txq(dev
, dev
->real_n_txq
);
972 /* If there has been an error, it means that the requested queues
973 * have not been created. Restore the old numbers. */
974 netdev
->n_txq
= old_txq
;
975 netdev
->n_rxq
= old_rxq
;
978 dev
->txq_needs_locking
= dev
->real_n_txq
!= netdev
->n_txq
;
980 ovs_mutex_unlock(&dev
->mutex
);
981 ovs_mutex_unlock(&dpdk_mutex
);
987 netdev_dpdk_vhost_cuse_set_multiq(struct netdev
*netdev
, unsigned int n_txq
,
990 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
993 if (netdev
->n_txq
== n_txq
&& netdev
->n_rxq
== n_rxq
) {
997 ovs_mutex_lock(&dpdk_mutex
);
998 ovs_mutex_lock(&dev
->mutex
);
1000 netdev
->n_txq
= n_txq
;
1001 dev
->real_n_txq
= 1;
1003 dev
->txq_needs_locking
= dev
->real_n_txq
!= netdev
->n_txq
;
1005 ovs_mutex_unlock(&dev
->mutex
);
1006 ovs_mutex_unlock(&dpdk_mutex
);
1012 netdev_dpdk_vhost_set_multiq(struct netdev
*netdev
, unsigned int n_txq
,
1015 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1018 if (netdev
->n_txq
== n_txq
&& netdev
->n_rxq
== n_rxq
) {
1022 ovs_mutex_lock(&dpdk_mutex
);
1023 ovs_mutex_lock(&dev
->mutex
);
1025 netdev
->n_txq
= n_txq
;
1026 netdev
->n_rxq
= n_rxq
;
1028 ovs_mutex_unlock(&dev
->mutex
);
1029 ovs_mutex_unlock(&dpdk_mutex
);
1034 static struct netdev_rxq
*
1035 netdev_dpdk_rxq_alloc(void)
1037 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1042 static struct netdev_rxq_dpdk
*
1043 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1045 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1049 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1051 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1052 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1054 ovs_mutex_lock(&dev
->mutex
);
1055 rx
->port_id
= dev
->port_id
;
1056 ovs_mutex_unlock(&dev
->mutex
);
1062 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1067 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1069 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1075 dpdk_queue_flush__(struct netdev_dpdk
*dev
, int qid
)
1077 struct dpdk_tx_queue
*txq
= &dev
->tx_q
[qid
];
1080 while (nb_tx
!= txq
->count
) {
1083 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, txq
->burst_pkts
+ nb_tx
,
1084 txq
->count
- nb_tx
);
1092 if (OVS_UNLIKELY(nb_tx
!= txq
->count
)) {
1093 /* free buffers, which we couldn't transmit, one at a time (each
1094 * packet could come from a different mempool) */
1097 for (i
= nb_tx
; i
< txq
->count
; i
++) {
1098 rte_pktmbuf_free(txq
->burst_pkts
[i
]);
1100 rte_spinlock_lock(&dev
->stats_lock
);
1101 dev
->stats
.tx_dropped
+= txq
->count
-nb_tx
;
1102 rte_spinlock_unlock(&dev
->stats_lock
);
1106 txq
->tsc
= rte_get_timer_cycles();
1110 dpdk_queue_flush(struct netdev_dpdk
*dev
, int qid
)
1112 struct dpdk_tx_queue
*txq
= &dev
->tx_q
[qid
];
1114 if (txq
->count
== 0) {
1117 dpdk_queue_flush__(dev
, qid
);
1121 is_vhost_running(struct virtio_net
*virtio_dev
)
1123 return (virtio_dev
!= NULL
&& (virtio_dev
->flags
& VIRTIO_DEV_RUNNING
));
1127 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1128 struct dp_packet
**packets
, int count
)
1131 struct dp_packet
*packet
;
1133 stats
->rx_packets
+= count
;
1134 for (i
= 0; i
< count
; i
++) {
1135 packet
= packets
[i
];
1137 if (OVS_UNLIKELY(dp_packet_size(packet
) < ETH_HEADER_LEN
)) {
1138 /* This only protects the following multicast counting from
1139 * too short packets, but it does not stop the packet from
1140 * further processing. */
1142 stats
->rx_length_errors
++;
1146 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1147 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1151 stats
->rx_bytes
+= dp_packet_size(packet
);
1156 * The receive path for the vhost port is the TX path out from guest.
1159 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1160 struct dp_packet
**packets
, int *c
)
1162 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1163 struct virtio_net
*virtio_dev
= netdev_dpdk_get_virtio(dev
);
1164 int qid
= rxq
->queue_id
;
1167 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev
))) {
1171 if (rxq
->queue_id
>= dev
->real_n_rxq
) {
1175 nb_rx
= rte_vhost_dequeue_burst(virtio_dev
, qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1177 (struct rte_mbuf
**)packets
,
1183 rte_spinlock_lock(&dev
->stats_lock
);
1184 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, packets
, nb_rx
);
1185 rte_spinlock_unlock(&dev
->stats_lock
);
1192 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet
**packets
,
1195 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1196 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1199 /* There is only one tx queue for this core. Do not flush other
1201 * Do not flush tx queue which is shared among CPUs
1202 * since it is always flushed */
1203 if (rxq
->queue_id
== rte_lcore_id() &&
1204 OVS_LIKELY(!dev
->txq_needs_locking
)) {
1205 dpdk_queue_flush(dev
, rxq
->queue_id
);
1208 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1209 (struct rte_mbuf
**) packets
,
1221 netdev_dpdk_qos_run__(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1224 struct netdev
*netdev
= &dev
->up
;
1226 if (dev
->qos_conf
!= NULL
) {
1227 rte_spinlock_lock(&dev
->qos_lock
);
1228 if (dev
->qos_conf
!= NULL
) {
1229 cnt
= dev
->qos_conf
->ops
->qos_run(netdev
, pkts
, cnt
);
1231 rte_spinlock_unlock(&dev
->qos_lock
);
1238 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1239 struct dp_packet
**packets
,
1244 int sent
= attempted
- dropped
;
1246 stats
->tx_packets
+= sent
;
1247 stats
->tx_dropped
+= dropped
;
1249 for (i
= 0; i
< sent
; i
++) {
1250 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1255 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1256 struct dp_packet
**pkts
, int cnt
,
1259 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1260 struct virtio_net
*virtio_dev
= netdev_dpdk_get_virtio(dev
);
1261 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1262 unsigned int total_pkts
= cnt
;
1263 unsigned int qos_pkts
= cnt
;
1266 qid
= dev
->tx_q
[qid
% dev
->real_n_txq
].map
;
1268 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev
) || qid
< 0)) {
1269 rte_spinlock_lock(&dev
->stats_lock
);
1270 dev
->stats
.tx_dropped
+= cnt
;
1271 rte_spinlock_unlock(&dev
->stats_lock
);
1275 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1277 /* Check has QoS has been configured for the netdev */
1278 cnt
= netdev_dpdk_qos_run__(dev
, cur_pkts
, cnt
);
1282 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1283 unsigned int tx_pkts
;
1285 tx_pkts
= rte_vhost_enqueue_burst(virtio_dev
, vhost_qid
,
1287 if (OVS_LIKELY(tx_pkts
)) {
1288 /* Packets have been sent.*/
1290 /* Prepare for possible next iteration.*/
1291 cur_pkts
= &cur_pkts
[tx_pkts
];
1293 uint64_t timeout
= VHOST_ENQ_RETRY_USECS
* rte_get_timer_hz() / 1E6
;
1294 unsigned int expired
= 0;
1297 start
= rte_get_timer_cycles();
1301 * Unable to enqueue packets to vhost interface.
1302 * Check available entries before retrying.
1304 while (!rte_vring_available_entries(virtio_dev
, vhost_qid
)) {
1305 if (OVS_UNLIKELY((rte_get_timer_cycles() - start
) > timeout
)) {
1311 /* break out of main loop. */
1317 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1319 rte_spinlock_lock(&dev
->stats_lock
);
1321 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
, cnt
);
1322 rte_spinlock_unlock(&dev
->stats_lock
);
1328 for (i
= 0; i
< total_pkts
; i
++) {
1329 dp_packet_delete(pkts
[i
]);
1335 dpdk_queue_pkts(struct netdev_dpdk
*dev
, int qid
,
1336 struct rte_mbuf
**pkts
, int cnt
)
1338 struct dpdk_tx_queue
*txq
= &dev
->tx_q
[qid
];
1344 int freeslots
= MAX_TX_QUEUE_LEN
- txq
->count
;
1345 int tocopy
= MIN(freeslots
, cnt
-i
);
1347 memcpy(&txq
->burst_pkts
[txq
->count
], &pkts
[i
],
1348 tocopy
* sizeof (struct rte_mbuf
*));
1350 txq
->count
+= tocopy
;
1353 if (txq
->count
== MAX_TX_QUEUE_LEN
|| txq
->flush_tx
) {
1354 dpdk_queue_flush__(dev
, qid
);
1356 diff_tsc
= rte_get_timer_cycles() - txq
->tsc
;
1357 if (diff_tsc
>= DRAIN_TSC
) {
1358 dpdk_queue_flush__(dev
, qid
);
1363 /* Tx function. Transmit packets indefinitely */
1365 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet
**pkts
,
1367 OVS_NO_THREAD_SAFETY_ANALYSIS
1369 #if !defined(__CHECKER__) && !defined(_WIN32)
1370 const size_t PKT_ARRAY_SIZE
= cnt
;
1372 /* Sparse or MSVC doesn't like variable length array. */
1373 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1375 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1376 struct rte_mbuf
*mbufs
[PKT_ARRAY_SIZE
];
1381 /* If we are on a non pmd thread we have to use the mempool mutex, because
1382 * every non pmd thread shares the same mempool cache */
1384 if (!dpdk_thread_is_pmd()) {
1385 ovs_mutex_lock(&nonpmd_mempool_mutex
);
1388 for (i
= 0; i
< cnt
; i
++) {
1389 int size
= dp_packet_size(pkts
[i
]);
1391 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1392 VLOG_WARN_RL(&rl
, "Too big size %d max_packet_len %d",
1393 (int)size
, dev
->max_packet_len
);
1399 mbufs
[newcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1401 if (!mbufs
[newcnt
]) {
1406 /* We have to do a copy for now */
1407 memcpy(rte_pktmbuf_mtod(mbufs
[newcnt
], void *), dp_packet_data(pkts
[i
]), size
);
1409 rte_pktmbuf_data_len(mbufs
[newcnt
]) = size
;
1410 rte_pktmbuf_pkt_len(mbufs
[newcnt
]) = size
;
1415 if (dev
->type
== DPDK_DEV_VHOST
) {
1416 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) mbufs
, newcnt
, true);
1418 unsigned int qos_pkts
= newcnt
;
1420 /* Check if QoS has been configured for this netdev. */
1421 newcnt
= netdev_dpdk_qos_run__(dev
, mbufs
, newcnt
);
1423 dropped
+= qos_pkts
- newcnt
;
1424 dpdk_queue_pkts(dev
, qid
, mbufs
, newcnt
);
1425 dpdk_queue_flush(dev
, qid
);
1428 if (OVS_UNLIKELY(dropped
)) {
1429 rte_spinlock_lock(&dev
->stats_lock
);
1430 dev
->stats
.tx_dropped
+= dropped
;
1431 rte_spinlock_unlock(&dev
->stats_lock
);
1434 if (!dpdk_thread_is_pmd()) {
1435 ovs_mutex_unlock(&nonpmd_mempool_mutex
);
1440 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
, struct dp_packet
**pkts
,
1441 int cnt
, bool may_steal
)
1443 if (OVS_UNLIKELY(pkts
[0]->source
!= DPBUF_DPDK
)) {
1446 dpdk_do_tx_copy(netdev
, qid
, pkts
, cnt
);
1448 for (i
= 0; i
< cnt
; i
++) {
1449 dp_packet_delete(pkts
[i
]);
1453 __netdev_dpdk_vhost_send(netdev
, qid
, pkts
, cnt
, may_steal
);
1459 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1460 struct dp_packet
**pkts
, int cnt
, bool may_steal
)
1464 if (OVS_UNLIKELY(dev
->txq_needs_locking
)) {
1465 qid
= qid
% dev
->real_n_txq
;
1466 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1469 if (OVS_UNLIKELY(!may_steal
||
1470 pkts
[0]->source
!= DPBUF_DPDK
)) {
1471 struct netdev
*netdev
= &dev
->up
;
1473 dpdk_do_tx_copy(netdev
, qid
, pkts
, cnt
);
1476 for (i
= 0; i
< cnt
; i
++) {
1477 dp_packet_delete(pkts
[i
]);
1481 int next_tx_idx
= 0;
1483 unsigned int qos_pkts
= 0;
1484 unsigned int temp_cnt
= 0;
1486 for (i
= 0; i
< cnt
; i
++) {
1487 int size
= dp_packet_size(pkts
[i
]);
1489 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1490 if (next_tx_idx
!= i
) {
1491 temp_cnt
= i
- next_tx_idx
;
1492 qos_pkts
= temp_cnt
;
1494 temp_cnt
= netdev_dpdk_qos_run__(dev
, (struct rte_mbuf
**)pkts
,
1496 dropped
+= qos_pkts
- temp_cnt
;
1497 dpdk_queue_pkts(dev
, qid
,
1498 (struct rte_mbuf
**)&pkts
[next_tx_idx
],
1503 VLOG_WARN_RL(&rl
, "Too big size %d max_packet_len %d",
1504 (int)size
, dev
->max_packet_len
);
1506 dp_packet_delete(pkts
[i
]);
1508 next_tx_idx
= i
+ 1;
1511 if (next_tx_idx
!= cnt
) {
1515 cnt
= netdev_dpdk_qos_run__(dev
, (struct rte_mbuf
**)pkts
, cnt
);
1516 dropped
+= qos_pkts
- cnt
;
1517 dpdk_queue_pkts(dev
, qid
, (struct rte_mbuf
**)&pkts
[next_tx_idx
],
1521 if (OVS_UNLIKELY(dropped
)) {
1522 rte_spinlock_lock(&dev
->stats_lock
);
1523 dev
->stats
.tx_dropped
+= dropped
;
1524 rte_spinlock_unlock(&dev
->stats_lock
);
1528 if (OVS_UNLIKELY(dev
->txq_needs_locking
)) {
1529 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1534 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1535 struct dp_packet
**pkts
, int cnt
, bool may_steal
)
1537 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1539 netdev_dpdk_send__(dev
, qid
, pkts
, cnt
, may_steal
);
1544 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1546 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1548 ovs_mutex_lock(&dev
->mutex
);
1549 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1551 netdev_change_seq_changed(netdev
);
1553 ovs_mutex_unlock(&dev
->mutex
);
1559 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1561 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1563 ovs_mutex_lock(&dev
->mutex
);
1565 ovs_mutex_unlock(&dev
->mutex
);
1571 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1573 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1575 ovs_mutex_lock(&dev
->mutex
);
1577 ovs_mutex_unlock(&dev
->mutex
);
1583 netdev_dpdk_set_mtu(const struct netdev
*netdev
, int mtu
)
1585 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1586 int old_mtu
, err
, dpdk_mtu
;
1587 struct dpdk_mp
*old_mp
;
1591 ovs_mutex_lock(&dpdk_mutex
);
1592 ovs_mutex_lock(&dev
->mutex
);
1593 if (dev
->mtu
== mtu
) {
1598 buf_size
= dpdk_buf_size(mtu
);
1599 dpdk_mtu
= FRAME_LEN_TO_MTU(buf_size
);
1601 mp
= dpdk_mp_get(dev
->socket_id
, dpdk_mtu
);
1607 rte_eth_dev_stop(dev
->port_id
);
1610 old_mp
= dev
->dpdk_mp
;
1613 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
1615 err
= dpdk_eth_dev_init(dev
);
1619 dev
->dpdk_mp
= old_mp
;
1620 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
1621 dpdk_eth_dev_init(dev
);
1625 dpdk_mp_put(old_mp
);
1626 netdev_change_seq_changed(netdev
);
1628 ovs_mutex_unlock(&dev
->mutex
);
1629 ovs_mutex_unlock(&dpdk_mutex
);
1634 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
1637 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
1638 struct netdev_stats
*stats
)
1640 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1642 ovs_mutex_lock(&dev
->mutex
);
1643 memset(stats
, 0, sizeof(*stats
));
1644 /* Unsupported Stats */
1645 stats
->collisions
= UINT64_MAX
;
1646 stats
->rx_crc_errors
= UINT64_MAX
;
1647 stats
->rx_fifo_errors
= UINT64_MAX
;
1648 stats
->rx_frame_errors
= UINT64_MAX
;
1649 stats
->rx_missed_errors
= UINT64_MAX
;
1650 stats
->rx_over_errors
= UINT64_MAX
;
1651 stats
->tx_aborted_errors
= UINT64_MAX
;
1652 stats
->tx_carrier_errors
= UINT64_MAX
;
1653 stats
->tx_errors
= UINT64_MAX
;
1654 stats
->tx_fifo_errors
= UINT64_MAX
;
1655 stats
->tx_heartbeat_errors
= UINT64_MAX
;
1656 stats
->tx_window_errors
= UINT64_MAX
;
1657 stats
->rx_dropped
+= UINT64_MAX
;
1659 rte_spinlock_lock(&dev
->stats_lock
);
1660 /* Supported Stats */
1661 stats
->rx_packets
+= dev
->stats
.rx_packets
;
1662 stats
->tx_packets
+= dev
->stats
.tx_packets
;
1663 stats
->tx_dropped
+= dev
->stats
.tx_dropped
;
1664 stats
->multicast
= dev
->stats
.multicast
;
1665 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
1666 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
1667 stats
->rx_errors
= dev
->stats
.rx_errors
;
1668 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
1669 rte_spinlock_unlock(&dev
->stats_lock
);
1671 ovs_mutex_unlock(&dev
->mutex
);
1677 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1679 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1680 struct rte_eth_stats rte_stats
;
1683 netdev_dpdk_get_carrier(netdev
, &gg
);
1684 ovs_mutex_lock(&dev
->mutex
);
1685 rte_eth_stats_get(dev
->port_id
, &rte_stats
);
1687 memset(stats
, 0, sizeof(*stats
));
1689 stats
->rx_packets
= rte_stats
.ipackets
;
1690 stats
->tx_packets
= rte_stats
.opackets
;
1691 stats
->rx_bytes
= rte_stats
.ibytes
;
1692 stats
->tx_bytes
= rte_stats
.obytes
;
1693 /* DPDK counts imissed as errors, but count them here as dropped instead */
1694 stats
->rx_errors
= rte_stats
.ierrors
- rte_stats
.imissed
;
1695 stats
->tx_errors
= rte_stats
.oerrors
;
1696 stats
->multicast
= rte_stats
.imcasts
;
1698 rte_spinlock_lock(&dev
->stats_lock
);
1699 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
1700 rte_spinlock_unlock(&dev
->stats_lock
);
1702 /* These are the available DPDK counters for packets not received due to
1703 * local resource constraints in DPDK and NIC respectively. */
1704 stats
->rx_dropped
= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
1705 stats
->collisions
= UINT64_MAX
;
1707 stats
->rx_length_errors
= UINT64_MAX
;
1708 stats
->rx_over_errors
= UINT64_MAX
;
1709 stats
->rx_crc_errors
= UINT64_MAX
;
1710 stats
->rx_frame_errors
= UINT64_MAX
;
1711 stats
->rx_fifo_errors
= UINT64_MAX
;
1712 stats
->rx_missed_errors
= rte_stats
.imissed
;
1714 stats
->tx_aborted_errors
= UINT64_MAX
;
1715 stats
->tx_carrier_errors
= UINT64_MAX
;
1716 stats
->tx_fifo_errors
= UINT64_MAX
;
1717 stats
->tx_heartbeat_errors
= UINT64_MAX
;
1718 stats
->tx_window_errors
= UINT64_MAX
;
1720 ovs_mutex_unlock(&dev
->mutex
);
1726 netdev_dpdk_get_features(const struct netdev
*netdev
,
1727 enum netdev_features
*current
,
1728 enum netdev_features
*advertised OVS_UNUSED
,
1729 enum netdev_features
*supported OVS_UNUSED
,
1730 enum netdev_features
*peer OVS_UNUSED
)
1732 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1733 struct rte_eth_link link
;
1735 ovs_mutex_lock(&dev
->mutex
);
1737 ovs_mutex_unlock(&dev
->mutex
);
1739 if (link
.link_duplex
== ETH_LINK_AUTONEG_DUPLEX
) {
1740 if (link
.link_speed
== ETH_LINK_SPEED_AUTONEG
) {
1741 *current
= NETDEV_F_AUTONEG
;
1743 } else if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
1744 if (link
.link_speed
== ETH_LINK_SPEED_10
) {
1745 *current
= NETDEV_F_10MB_HD
;
1747 if (link
.link_speed
== ETH_LINK_SPEED_100
) {
1748 *current
= NETDEV_F_100MB_HD
;
1750 if (link
.link_speed
== ETH_LINK_SPEED_1000
) {
1751 *current
= NETDEV_F_1GB_HD
;
1753 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
1754 if (link
.link_speed
== ETH_LINK_SPEED_10
) {
1755 *current
= NETDEV_F_10MB_FD
;
1757 if (link
.link_speed
== ETH_LINK_SPEED_100
) {
1758 *current
= NETDEV_F_100MB_FD
;
1760 if (link
.link_speed
== ETH_LINK_SPEED_1000
) {
1761 *current
= NETDEV_F_1GB_FD
;
1763 if (link
.link_speed
== ETH_LINK_SPEED_10000
) {
1764 *current
= NETDEV_F_10GB_FD
;
1772 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
1774 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1777 ovs_mutex_lock(&dev
->mutex
);
1778 ifindex
= dev
->port_id
;
1779 ovs_mutex_unlock(&dev
->mutex
);
1785 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
1787 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1789 ovs_mutex_lock(&dev
->mutex
);
1790 check_link_status(dev
);
1791 *carrier
= dev
->link
.link_status
;
1793 ovs_mutex_unlock(&dev
->mutex
);
1799 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
1801 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1802 struct virtio_net
*virtio_dev
= netdev_dpdk_get_virtio(dev
);
1804 ovs_mutex_lock(&dev
->mutex
);
1806 if (is_vhost_running(virtio_dev
)) {
1812 ovs_mutex_unlock(&dev
->mutex
);
1817 static long long int
1818 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
1820 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1821 long long int carrier_resets
;
1823 ovs_mutex_lock(&dev
->mutex
);
1824 carrier_resets
= dev
->link_reset_cnt
;
1825 ovs_mutex_unlock(&dev
->mutex
);
1827 return carrier_resets
;
1831 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
1832 long long int interval OVS_UNUSED
)
1838 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
1839 enum netdev_flags off
, enum netdev_flags on
,
1840 enum netdev_flags
*old_flagsp
) OVS_REQUIRES(dev
->mutex
)
1844 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
1848 *old_flagsp
= dev
->flags
;
1852 if (dev
->flags
== *old_flagsp
) {
1856 if (dev
->type
== DPDK_DEV_ETH
) {
1857 if (dev
->flags
& NETDEV_UP
) {
1858 err
= rte_eth_dev_start(dev
->port_id
);
1863 if (dev
->flags
& NETDEV_PROMISC
) {
1864 rte_eth_promiscuous_enable(dev
->port_id
);
1867 if (!(dev
->flags
& NETDEV_UP
)) {
1868 rte_eth_dev_stop(dev
->port_id
);
1876 netdev_dpdk_update_flags(struct netdev
*netdev
,
1877 enum netdev_flags off
, enum netdev_flags on
,
1878 enum netdev_flags
*old_flagsp
)
1880 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1883 ovs_mutex_lock(&dev
->mutex
);
1884 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
1885 ovs_mutex_unlock(&dev
->mutex
);
1891 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
1893 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1894 struct rte_eth_dev_info dev_info
;
1896 if (dev
->port_id
< 0)
1899 ovs_mutex_lock(&dev
->mutex
);
1900 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
1901 ovs_mutex_unlock(&dev
->mutex
);
1903 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
1905 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
1906 smap_add_format(args
, "numa_id", "%d", rte_eth_dev_socket_id(dev
->port_id
));
1907 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
1908 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
1909 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
1910 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
1911 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
1912 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
1913 smap_add_format(args
, "max_hash_mac_addrs", "%u", dev_info
.max_hash_mac_addrs
);
1914 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
1915 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
1917 if (dev_info
.pci_dev
) {
1918 smap_add_format(args
, "pci-vendor_id", "0x%u",
1919 dev_info
.pci_dev
->id
.vendor_id
);
1920 smap_add_format(args
, "pci-device_id", "0x%x",
1921 dev_info
.pci_dev
->id
.device_id
);
1928 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
1929 OVS_REQUIRES(dev
->mutex
)
1931 enum netdev_flags old_flags
;
1934 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
1936 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
1941 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
1942 const char *argv
[], void *aux OVS_UNUSED
)
1946 if (!strcasecmp(argv
[argc
- 1], "up")) {
1948 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
1951 unixctl_command_reply_error(conn
, "Invalid Admin State");
1956 struct netdev
*netdev
= netdev_from_name(argv
[1]);
1957 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
1958 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
1960 ovs_mutex_lock(&dpdk_dev
->mutex
);
1961 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
1962 ovs_mutex_unlock(&dpdk_dev
->mutex
);
1964 netdev_close(netdev
);
1966 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
1967 netdev_close(netdev
);
1971 struct netdev_dpdk
*netdev
;
1973 ovs_mutex_lock(&dpdk_mutex
);
1974 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
1975 ovs_mutex_lock(&netdev
->mutex
);
1976 netdev_dpdk_set_admin_state__(netdev
, up
);
1977 ovs_mutex_unlock(&netdev
->mutex
);
1979 ovs_mutex_unlock(&dpdk_mutex
);
1981 unixctl_command_reply(conn
, "OK");
1985 * Set virtqueue flags so that we do not receive interrupts.
1988 set_irq_status(struct virtio_net
*virtio_dev
)
1993 for (i
= 0; i
< virtio_dev
->virt_qp_nb
; i
++) {
1994 idx
= i
* VIRTIO_QNUM
;
1995 rte_vhost_enable_guest_notification(virtio_dev
, idx
+ VIRTIO_RXQ
, 0);
1996 rte_vhost_enable_guest_notification(virtio_dev
, idx
+ VIRTIO_TXQ
, 0);
2001 * Fixes mapping for vhost-user tx queues. Must be called after each
2002 * enabling/disabling of queues and real_n_txq modifications.
2005 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2006 OVS_REQUIRES(dev
->mutex
)
2008 int *enabled_queues
, n_enabled
= 0;
2009 int i
, k
, total_txqs
= dev
->real_n_txq
;
2011 enabled_queues
= dpdk_rte_mzalloc(total_txqs
* sizeof *enabled_queues
);
2013 for (i
= 0; i
< total_txqs
; i
++) {
2014 /* Enabled queues always mapped to themselves. */
2015 if (dev
->tx_q
[i
].map
== i
) {
2016 enabled_queues
[n_enabled
++] = i
;
2020 if (n_enabled
== 0 && total_txqs
!= 0) {
2021 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2026 for (i
= 0; i
< total_txqs
; i
++) {
2027 if (dev
->tx_q
[i
].map
!= i
) {
2028 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2029 k
= (k
+ 1) % n_enabled
;
2033 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2034 for (i
= 0; i
< total_txqs
; i
++) {
2035 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2038 rte_free(enabled_queues
);
2042 netdev_dpdk_vhost_set_queues(struct netdev_dpdk
*dev
, struct virtio_net
*virtio_dev
)
2043 OVS_REQUIRES(dev
->mutex
)
2047 qp_num
= virtio_dev
->virt_qp_nb
;
2048 if (qp_num
> dev
->up
.n_rxq
) {
2049 VLOG_ERR("vHost Device '%s' %"PRIu64
" can't be added - "
2050 "too many queues %d > %d", virtio_dev
->ifname
, virtio_dev
->device_fh
,
2051 qp_num
, dev
->up
.n_rxq
);
2055 dev
->real_n_rxq
= qp_num
;
2056 dev
->real_n_txq
= qp_num
;
2057 dev
->txq_needs_locking
= true;
2058 /* Enable TX queue 0 by default if it wasn't disabled. */
2059 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
2060 dev
->tx_q
[0].map
= 0;
2063 netdev_dpdk_remap_txqs(dev
);
2069 * A new virtio-net device is added to a vhost port.
2072 new_device(struct virtio_net
*virtio_dev
)
2074 struct netdev_dpdk
*dev
;
2075 bool exists
= false;
2077 ovs_mutex_lock(&dpdk_mutex
);
2078 /* Add device to the vhost port with the same name as that passed down. */
2079 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2080 if (strncmp(virtio_dev
->ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2081 ovs_mutex_lock(&dev
->mutex
);
2082 if (netdev_dpdk_vhost_set_queues(dev
, virtio_dev
)) {
2083 ovs_mutex_unlock(&dev
->mutex
);
2084 ovs_mutex_unlock(&dpdk_mutex
);
2087 ovsrcu_set(&dev
->virtio_dev
, virtio_dev
);
2089 virtio_dev
->flags
|= VIRTIO_DEV_RUNNING
;
2090 /* Disable notifications. */
2091 set_irq_status(virtio_dev
);
2092 ovs_mutex_unlock(&dev
->mutex
);
2096 ovs_mutex_unlock(&dpdk_mutex
);
2099 VLOG_INFO("vHost Device '%s' %"PRIu64
" can't be added - name not "
2100 "found", virtio_dev
->ifname
, virtio_dev
->device_fh
);
2105 VLOG_INFO("vHost Device '%s' %"PRIu64
" has been added", virtio_dev
->ifname
,
2106 virtio_dev
->device_fh
);
2110 /* Clears mapping for all available queues of vhost interface. */
2112 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2113 OVS_REQUIRES(dev
->mutex
)
2117 for (i
= 0; i
< dev
->real_n_txq
; i
++) {
2118 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2123 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2124 * flag to stop any more packets from being sent or received to/from a VM and
2125 * ensure all currently queued packets have been sent/received before removing
2129 destroy_device(volatile struct virtio_net
*virtio_dev
)
2131 struct netdev_dpdk
*dev
;
2132 bool exists
= false;
2134 ovs_mutex_lock(&dpdk_mutex
);
2135 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2136 if (netdev_dpdk_get_virtio(dev
) == virtio_dev
) {
2138 ovs_mutex_lock(&dev
->mutex
);
2139 virtio_dev
->flags
&= ~VIRTIO_DEV_RUNNING
;
2140 ovsrcu_set(&dev
->virtio_dev
, NULL
);
2141 netdev_dpdk_txq_map_clear(dev
);
2143 ovs_mutex_unlock(&dev
->mutex
);
2148 ovs_mutex_unlock(&dpdk_mutex
);
2150 if (exists
== true) {
2152 * Wait for other threads to quiesce after setting the 'virtio_dev'
2153 * to NULL, before returning.
2155 ovsrcu_synchronize();
2157 * As call to ovsrcu_synchronize() will end the quiescent state,
2158 * put thread back into quiescent state before returning.
2160 ovsrcu_quiesce_start();
2161 VLOG_INFO("vHost Device '%s' %"PRIu64
" has been removed",
2162 virtio_dev
->ifname
, virtio_dev
->device_fh
);
2164 VLOG_INFO("vHost Device '%s' %"PRIu64
" not found", virtio_dev
->ifname
,
2165 virtio_dev
->device_fh
);
2170 vring_state_changed(struct virtio_net
*virtio_dev
, uint16_t queue_id
,
2173 struct netdev_dpdk
*dev
;
2174 bool exists
= false;
2175 int qid
= queue_id
/ VIRTIO_QNUM
;
2177 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2181 ovs_mutex_lock(&dpdk_mutex
);
2182 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2183 if (strncmp(virtio_dev
->ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2184 ovs_mutex_lock(&dev
->mutex
);
2186 dev
->tx_q
[qid
].map
= qid
;
2188 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2190 netdev_dpdk_remap_txqs(dev
);
2192 ovs_mutex_unlock(&dev
->mutex
);
2196 ovs_mutex_unlock(&dpdk_mutex
);
2199 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s' %"
2200 PRIu64
" changed to \'%s\'", queue_id
, qid
,
2201 virtio_dev
->ifname
, virtio_dev
->device_fh
,
2202 (enable
== 1) ? "enabled" : "disabled");
2204 VLOG_INFO("vHost Device '%s' %"PRIu64
" not found", virtio_dev
->ifname
,
2205 virtio_dev
->device_fh
);
2213 netdev_dpdk_get_virtio(const struct netdev_dpdk
*dev
)
2215 return ovsrcu_get(struct virtio_net
*, &dev
->virtio_dev
);
2219 * These callbacks allow virtio-net devices to be added to vhost ports when
2220 * configuration has been fully complete.
2222 static const struct virtio_net_device_ops virtio_net_device_ops
=
2224 .new_device
= new_device
,
2225 .destroy_device
= destroy_device
,
2226 .vring_state_changed
= vring_state_changed
2230 start_vhost_loop(void *dummy OVS_UNUSED
)
2232 pthread_detach(pthread_self());
2233 /* Put the cuse thread into quiescent state. */
2234 ovsrcu_quiesce_start();
2235 rte_vhost_driver_session_start();
2240 dpdk_vhost_class_init(void)
2242 rte_vhost_driver_callback_register(&virtio_net_device_ops
);
2243 ovs_thread_create("vhost_thread", start_vhost_loop
, NULL
);
2248 dpdk_vhost_cuse_class_init(void)
2253 /* Register CUSE device to handle IOCTLs.
2254 * Unless otherwise specified on the vswitchd command line, cuse_dev_name
2255 * is set to vhost-net.
2257 err
= rte_vhost_driver_register(cuse_dev_name
);
2260 VLOG_ERR("CUSE device setup failure.");
2264 dpdk_vhost_class_init();
2269 dpdk_vhost_user_class_init(void)
2271 dpdk_vhost_class_init();
2276 dpdk_common_init(void)
2278 unixctl_command_register("netdev-dpdk/set-admin-state",
2279 "[netdev] up|down", 1, 2,
2280 netdev_dpdk_set_admin_state
, NULL
);
2282 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2288 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2289 unsigned int *eth_port_id
)
2291 struct dpdk_ring
*ivshmem
;
2292 char ring_name
[RTE_RING_NAMESIZE
];
2295 ivshmem
= dpdk_rte_mzalloc(sizeof *ivshmem
);
2296 if (ivshmem
== NULL
) {
2300 /* XXX: Add support for multiquque ring. */
2301 err
= snprintf(ring_name
, sizeof(ring_name
), "%s_tx", dev_name
);
2306 /* Create single producer tx ring, netdev does explicit locking. */
2307 ivshmem
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2309 if (ivshmem
->cring_tx
== NULL
) {
2314 err
= snprintf(ring_name
, sizeof(ring_name
), "%s_rx", dev_name
);
2319 /* Create single consumer rx ring, netdev does explicit locking. */
2320 ivshmem
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2322 if (ivshmem
->cring_rx
== NULL
) {
2327 err
= rte_eth_from_rings(dev_name
, &ivshmem
->cring_rx
, 1,
2328 &ivshmem
->cring_tx
, 1, SOCKET0
);
2335 ivshmem
->user_port_id
= port_no
;
2336 ivshmem
->eth_port_id
= rte_eth_dev_count() - 1;
2337 ovs_list_push_back(&dpdk_ring_list
, &ivshmem
->list_node
);
2339 *eth_port_id
= ivshmem
->eth_port_id
;
2344 dpdk_ring_open(const char dev_name
[], unsigned int *eth_port_id
) OVS_REQUIRES(dpdk_mutex
)
2346 struct dpdk_ring
*ivshmem
;
2347 unsigned int port_no
;
2350 /* Names always start with "dpdkr" */
2351 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2356 /* look through our list to find the device */
2357 LIST_FOR_EACH (ivshmem
, list_node
, &dpdk_ring_list
) {
2358 if (ivshmem
->user_port_id
== port_no
) {
2359 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2360 *eth_port_id
= ivshmem
->eth_port_id
; /* really all that is needed */
2364 /* Need to create the device rings */
2365 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2369 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2370 struct dp_packet
**pkts
, int cnt
, bool may_steal
)
2372 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2375 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that the
2376 * rss hash field is clear. This is because the same mbuf may be modified by
2377 * the consumer of the ring and return into the datapath without recalculating
2379 for (i
= 0; i
< cnt
; i
++) {
2380 dp_packet_rss_invalidate(pkts
[i
]);
2383 netdev_dpdk_send__(dev
, qid
, pkts
, cnt
, may_steal
);
2388 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2390 unsigned int port_no
= 0;
2393 if (rte_eal_init_ret
) {
2394 return rte_eal_init_ret
;
2397 ovs_mutex_lock(&dpdk_mutex
);
2399 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2404 err
= netdev_dpdk_init(netdev
, port_no
, DPDK_DEV_ETH
);
2407 ovs_mutex_unlock(&dpdk_mutex
);
2414 * Initialize QoS configuration operations.
2417 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2423 * Search existing QoS operations in qos_ops and compare each set of
2424 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2427 static const struct dpdk_qos_ops
*
2428 qos_lookup_name(const char *name
)
2430 const struct dpdk_qos_ops
*const *opsp
;
2432 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2433 const struct dpdk_qos_ops
*ops
= *opsp
;
2434 if (!strcmp(name
, ops
->qos_name
)) {
2442 * Call qos_destruct to clean up items associated with the netdevs
2443 * qos_conf. Set netdevs qos_conf to NULL.
2446 qos_delete_conf(struct netdev
*netdev
)
2448 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2450 rte_spinlock_lock(&dev
->qos_lock
);
2451 if (dev
->qos_conf
) {
2452 if (dev
->qos_conf
->ops
->qos_destruct
) {
2453 dev
->qos_conf
->ops
->qos_destruct(netdev
, dev
->qos_conf
);
2455 dev
->qos_conf
= NULL
;
2457 rte_spinlock_unlock(&dev
->qos_lock
);
2461 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2464 const struct dpdk_qos_ops
*const *opsp
;
2466 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2467 const struct dpdk_qos_ops
*ops
= *opsp
;
2468 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
2469 sset_add(types
, ops
->qos_name
);
2476 netdev_dpdk_get_qos(const struct netdev
*netdev
,
2477 const char **typep
, struct smap
*details
)
2479 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2482 ovs_mutex_lock(&dev
->mutex
);
2484 *typep
= dev
->qos_conf
->ops
->qos_name
;
2485 error
= (dev
->qos_conf
->ops
->qos_get
2486 ? dev
->qos_conf
->ops
->qos_get(netdev
, details
): 0);
2488 ovs_mutex_unlock(&dev
->mutex
);
2494 netdev_dpdk_set_qos(struct netdev
*netdev
,
2495 const char *type
, const struct smap
*details
)
2497 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2498 const struct dpdk_qos_ops
*new_ops
= NULL
;
2501 /* If type is empty or unsupported then the current QoS configuration
2502 * for the dpdk-netdev can be destroyed */
2503 new_ops
= qos_lookup_name(type
);
2505 if (type
[0] == '\0' || !new_ops
|| !new_ops
->qos_construct
) {
2506 qos_delete_conf(netdev
);
2510 ovs_mutex_lock(&dev
->mutex
);
2512 if (dev
->qos_conf
) {
2513 if (new_ops
== dev
->qos_conf
->ops
) {
2514 error
= new_ops
->qos_set
? new_ops
->qos_set(netdev
, details
) : 0;
2516 /* Delete existing QoS configuration. */
2517 qos_delete_conf(netdev
);
2518 ovs_assert(dev
->qos_conf
== NULL
);
2520 /* Install new QoS configuration. */
2521 error
= new_ops
->qos_construct(netdev
, details
);
2522 ovs_assert((error
== 0) == (dev
->qos_conf
!= NULL
));
2525 error
= new_ops
->qos_construct(netdev
, details
);
2526 ovs_assert((error
== 0) == (dev
->qos_conf
!= NULL
));
2529 ovs_mutex_unlock(&dev
->mutex
);
2533 /* egress-policer details */
2535 struct egress_policer
{
2536 struct qos_conf qos_conf
;
2537 struct rte_meter_srtcm_params app_srtcm_params
;
2538 struct rte_meter_srtcm egress_meter
;
2541 static struct egress_policer
*
2542 egress_policer_get__(const struct netdev
*netdev
)
2544 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2545 return CONTAINER_OF(dev
->qos_conf
, struct egress_policer
, qos_conf
);
2549 egress_policer_qos_construct(struct netdev
*netdev
,
2550 const struct smap
*details
)
2552 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2553 struct egress_policer
*policer
;
2558 rte_spinlock_lock(&dev
->qos_lock
);
2559 policer
= xmalloc(sizeof *policer
);
2560 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
2561 dev
->qos_conf
= &policer
->qos_conf
;
2562 cir_s
= smap_get(details
, "cir");
2563 cbs_s
= smap_get(details
, "cbs");
2564 policer
->app_srtcm_params
.cir
= cir_s
? strtoull(cir_s
, NULL
, 10) : 0;
2565 policer
->app_srtcm_params
.cbs
= cbs_s
? strtoull(cbs_s
, NULL
, 10) : 0;
2566 policer
->app_srtcm_params
.ebs
= 0;
2567 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
2568 &policer
->app_srtcm_params
);
2569 rte_spinlock_unlock(&dev
->qos_lock
);
2575 egress_policer_qos_destruct(struct netdev
*netdev OVS_UNUSED
,
2576 struct qos_conf
*conf
)
2578 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
2584 egress_policer_qos_get(const struct netdev
*netdev
, struct smap
*details
)
2586 struct egress_policer
*policer
= egress_policer_get__(netdev
);
2587 smap_add_format(details
, "cir", "%llu",
2588 1ULL * policer
->app_srtcm_params
.cir
);
2589 smap_add_format(details
, "cbs", "%llu",
2590 1ULL * policer
->app_srtcm_params
.cbs
);
2595 egress_policer_qos_set(struct netdev
*netdev
, const struct smap
*details
)
2597 struct egress_policer
*policer
;
2602 policer
= egress_policer_get__(netdev
);
2603 cir_s
= smap_get(details
, "cir");
2604 cbs_s
= smap_get(details
, "cbs");
2605 policer
->app_srtcm_params
.cir
= cir_s
? strtoull(cir_s
, NULL
, 10) : 0;
2606 policer
->app_srtcm_params
.cbs
= cbs_s
? strtoull(cbs_s
, NULL
, 10) : 0;
2607 policer
->app_srtcm_params
.ebs
= 0;
2608 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
2609 &policer
->app_srtcm_params
);
2615 egress_policer_pkt_handle__(struct rte_meter_srtcm
*meter
,
2616 struct rte_mbuf
*pkt
, uint64_t time
)
2618 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
2620 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
2625 egress_policer_run(struct netdev
*netdev
, struct rte_mbuf
**pkts
,
2630 struct egress_policer
*policer
= egress_policer_get__(netdev
);
2631 struct rte_mbuf
*pkt
= NULL
;
2632 uint64_t current_time
= rte_rdtsc();
2634 for(i
= 0; i
< pkt_cnt
; i
++) {
2636 /* Handle current packet */
2637 if (egress_policer_pkt_handle__(&policer
->egress_meter
, pkt
,
2644 rte_pktmbuf_free(pkt
);
2651 static const struct dpdk_qos_ops egress_policer_ops
= {
2652 "egress-policer", /* qos_name */
2653 egress_policer_qos_construct
,
2654 egress_policer_qos_destruct
,
2655 egress_policer_qos_get
,
2656 egress_policer_qos_set
,
2660 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, MULTIQ, SEND, \
2661 GET_CARRIER, GET_STATS, GET_FEATURES, GET_STATUS, RXQ_RECV) \
2664 true, /* is_pmd */ \
2666 NULL, /* netdev_dpdk_run */ \
2667 NULL, /* netdev_dpdk_wait */ \
2669 netdev_dpdk_alloc, \
2672 netdev_dpdk_dealloc, \
2673 netdev_dpdk_get_config, \
2674 netdev_dpdk_set_config, \
2675 NULL, /* get_tunnel_config */ \
2676 NULL, /* build header */ \
2677 NULL, /* push header */ \
2678 NULL, /* pop header */ \
2679 netdev_dpdk_get_numa_id, /* get_numa_id */ \
2680 MULTIQ, /* set_multiq */ \
2683 NULL, /* send_wait */ \
2685 netdev_dpdk_set_etheraddr, \
2686 netdev_dpdk_get_etheraddr, \
2687 netdev_dpdk_get_mtu, \
2688 netdev_dpdk_set_mtu, \
2689 netdev_dpdk_get_ifindex, \
2691 netdev_dpdk_get_carrier_resets, \
2692 netdev_dpdk_set_miimon, \
2695 NULL, /* set_advertisements */ \
2697 NULL, /* set_policing */ \
2698 netdev_dpdk_get_qos_types, \
2699 NULL, /* get_qos_capabilities */ \
2700 netdev_dpdk_get_qos, \
2701 netdev_dpdk_set_qos, \
2702 NULL, /* get_queue */ \
2703 NULL, /* set_queue */ \
2704 NULL, /* delete_queue */ \
2705 NULL, /* get_queue_stats */ \
2706 NULL, /* queue_dump_start */ \
2707 NULL, /* queue_dump_next */ \
2708 NULL, /* queue_dump_done */ \
2709 NULL, /* dump_queue_stats */ \
2711 NULL, /* set_in4 */ \
2712 NULL, /* get_addr_list */ \
2713 NULL, /* add_router */ \
2714 NULL, /* get_next_hop */ \
2716 NULL, /* arp_lookup */ \
2718 netdev_dpdk_update_flags, \
2720 netdev_dpdk_rxq_alloc, \
2721 netdev_dpdk_rxq_construct, \
2722 netdev_dpdk_rxq_destruct, \
2723 netdev_dpdk_rxq_dealloc, \
2725 NULL, /* rx_wait */ \
2726 NULL, /* rxq_drain */ \
2730 process_vhost_flags(char *flag
, char *default_val
, int size
,
2731 char **argv
, char **new_val
)
2735 /* Depending on which version of vhost is in use, process the vhost-specific
2736 * flag if it is provided on the vswitchd command line, otherwise resort to
2739 * For vhost-user: Process "-vhost_sock_dir" to set the custom location of
2740 * the vhost-user socket(s).
2741 * For vhost-cuse: Process "-cuse_dev_name" to set the custom name of the
2742 * vhost-cuse character device.
2744 if (!strcmp(argv
[1], flag
) && (strlen(argv
[2]) <= size
)) {
2746 *new_val
= xstrdup(argv
[2]);
2747 VLOG_INFO("User-provided %s in use: %s", flag
, *new_val
);
2749 VLOG_INFO("No %s provided - defaulting to %s", flag
, default_val
);
2750 *new_val
= default_val
;
2757 dpdk_init(int argc
, char **argv
)
2761 char *pragram_name
= argv
[0];
2763 if (argc
< 2 || strcmp(argv
[1], "--dpdk"))
2766 /* Remove the --dpdk argument from arg list.*/
2770 /* Reject --user option */
2772 for (i
= 0; i
< argc
; i
++) {
2773 if (!strcmp(argv
[i
], "--user")) {
2774 VLOG_ERR("Can not mix --dpdk and --user options, aborting.");
2779 if (process_vhost_flags("-cuse_dev_name", xstrdup("vhost-net"),
2780 PATH_MAX
, argv
, &cuse_dev_name
)) {
2782 if (process_vhost_flags("-vhost_sock_dir", xstrdup(ovs_rundir()),
2783 NAME_MAX
, argv
, &vhost_sock_dir
)) {
2787 err
= stat(vhost_sock_dir
, &s
);
2789 VLOG_ERR("vHostUser socket DIR '%s' does not exist.",
2794 /* Remove the vhost flag configuration parameters from the argument
2795 * list, so that the correct elements are passed to the DPDK
2796 * initialization function
2799 argv
+= 2; /* Increment by two to bypass the vhost flag arguments */
2803 /* Keep the program name argument as this is needed for call to
2806 argv
[0] = pragram_name
;
2808 /* Make sure things are initialized ... */
2809 result
= rte_eal_init(argc
, argv
);
2811 ovs_abort(result
, "Cannot init EAL");
2814 rte_memzone_dump(stdout
);
2815 rte_eal_init_ret
= 0;
2817 if (argc
> result
) {
2818 argv
[result
] = argv
[0];
2821 /* We are called from the main thread here */
2822 RTE_PER_LCORE(_lcore_id
) = NON_PMD_CORE_ID
;
2824 return result
+ 1 + base
;
2827 static const struct netdev_class dpdk_class
=
2831 netdev_dpdk_construct
,
2832 netdev_dpdk_destruct
,
2833 netdev_dpdk_set_multiq
,
2834 netdev_dpdk_eth_send
,
2835 netdev_dpdk_get_carrier
,
2836 netdev_dpdk_get_stats
,
2837 netdev_dpdk_get_features
,
2838 netdev_dpdk_get_status
,
2839 netdev_dpdk_rxq_recv
);
2841 static const struct netdev_class dpdk_ring_class
=
2845 netdev_dpdk_ring_construct
,
2846 netdev_dpdk_destruct
,
2847 netdev_dpdk_set_multiq
,
2848 netdev_dpdk_ring_send
,
2849 netdev_dpdk_get_carrier
,
2850 netdev_dpdk_get_stats
,
2851 netdev_dpdk_get_features
,
2852 netdev_dpdk_get_status
,
2853 netdev_dpdk_rxq_recv
);
2855 static const struct netdev_class OVS_UNUSED dpdk_vhost_cuse_class
=
2858 dpdk_vhost_cuse_class_init
,
2859 netdev_dpdk_vhost_cuse_construct
,
2860 netdev_dpdk_vhost_destruct
,
2861 netdev_dpdk_vhost_cuse_set_multiq
,
2862 netdev_dpdk_vhost_send
,
2863 netdev_dpdk_vhost_get_carrier
,
2864 netdev_dpdk_vhost_get_stats
,
2867 netdev_dpdk_vhost_rxq_recv
);
2869 static const struct netdev_class OVS_UNUSED dpdk_vhost_user_class
=
2872 dpdk_vhost_user_class_init
,
2873 netdev_dpdk_vhost_user_construct
,
2874 netdev_dpdk_vhost_destruct
,
2875 netdev_dpdk_vhost_set_multiq
,
2876 netdev_dpdk_vhost_send
,
2877 netdev_dpdk_vhost_get_carrier
,
2878 netdev_dpdk_vhost_get_stats
,
2881 netdev_dpdk_vhost_rxq_recv
);
2884 netdev_dpdk_register(void)
2886 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2888 if (rte_eal_init_ret
) {
2892 if (ovsthread_once_start(&once
)) {
2894 netdev_register_provider(&dpdk_class
);
2895 netdev_register_provider(&dpdk_ring_class
);
2897 netdev_register_provider(&dpdk_vhost_cuse_class
);
2899 netdev_register_provider(&dpdk_vhost_user_class
);
2901 ovsthread_once_done(&once
);
2906 pmd_thread_setaffinity_cpu(unsigned cpu
)
2912 CPU_SET(cpu
, &cpuset
);
2913 err
= pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t
), &cpuset
);
2915 VLOG_ERR("Thread affinity error %d",err
);
2918 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
2919 ovs_assert(cpu
!= NON_PMD_CORE_ID
);
2920 RTE_PER_LCORE(_lcore_id
) = cpu
;
2926 dpdk_thread_is_pmd(void)
2928 return rte_lcore_id() != NON_PMD_CORE_ID
;