2 * Copyright (c) 2014, 2015, 2016, 2017 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.
18 #include "netdev-dpdk.h"
25 #include <linux/virtio_net.h>
26 #include <sys/socket.h>
29 #include <rte_bus_pci.h>
30 #include <rte_config.h>
31 #include <rte_cycles.h>
32 #include <rte_errno.h>
33 #include <rte_eth_ring.h>
34 #include <rte_ethdev.h>
35 #include <rte_malloc.h>
37 #include <rte_meter.h>
39 #include <rte_vhost.h>
40 #include <rte_version.h>
43 #include "dp-packet.h"
45 #include "dpif-netdev.h"
46 #include "fatal-signal.h"
47 #include "netdev-provider.h"
48 #include "netdev-vport.h"
50 #include "openvswitch/dynamic-string.h"
51 #include "openvswitch/list.h"
52 #include "openvswitch/ofp-print.h"
53 #include "openvswitch/vlog.h"
55 #include "ovs-thread.h"
58 #include "openvswitch/shash.h"
61 #include "unaligned.h"
65 enum {VIRTIO_RXQ
, VIRTIO_TXQ
, VIRTIO_QNUM
};
67 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
68 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
70 #define DPDK_PORT_WATCHDOG_INTERVAL 5
72 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
73 #define OVS_VPORT_DPDK "ovs_dpdk"
76 * need to reserve tons of extra space in the mbufs so we can align the
77 * DMA addresses to 4KB.
78 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
79 * performance for standard Ethernet MTU.
81 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
82 + (2 * VLAN_HEADER_LEN))
83 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
84 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
85 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
86 - ETHER_HDR_LEN - ETHER_CRC_LEN)
87 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
88 + sizeof(struct dp_packet) \
89 + RTE_PKTMBUF_HEADROOM), \
91 #define NETDEV_DPDK_MBUF_ALIGN 1024
92 #define NETDEV_DPDK_MAX_PKT_LEN 9728
94 /* Min number of packets in the mempool. OVS tries to allocate a mempool with
95 * roughly estimated number of mbufs: if this fails (because the system doesn't
96 * have enough hugepages) we keep halving the number until the allocation
97 * succeeds or we reach MIN_NB_MBUF */
98 #define MIN_NB_MBUF (4096 * 4)
99 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
102 * DPDK XSTATS Counter names definition
104 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
105 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
106 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
107 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
108 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
109 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
110 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
112 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
113 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
114 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
115 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
116 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
117 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
118 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
120 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
121 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
122 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
123 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
124 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
125 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
126 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
127 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
131 /* Default size of Physical NIC RXQ */
132 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
133 /* Default size of Physical NIC TXQ */
134 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
135 /* Maximum size of Physical NIC Queues */
136 #define NIC_PORT_MAX_Q_SIZE 4096
138 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
139 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
140 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
141 * yet mapped to another queue. */
143 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
145 /* DPDK library uses uint16_t for port_id. */
146 typedef uint16_t dpdk_port_t
;
148 #define VHOST_ENQ_RETRY_NUM 8
149 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
151 static const struct rte_eth_conf port_conf
= {
153 .mq_mode
= ETH_MQ_RX_RSS
,
155 .header_split
= 0, /* Header Split disabled */
156 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
157 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
158 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
164 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
168 .mq_mode
= ETH_MQ_TX_NONE
,
173 * These callbacks allow virtio-net devices to be added to vhost ports when
174 * configuration has been fully completed.
176 static int new_device(int vid
);
177 static void destroy_device(int vid
);
178 static int vring_state_changed(int vid
, uint16_t queue_id
, int enable
);
179 static const struct vhost_device_ops virtio_net_device_ops
=
181 .new_device
= new_device
,
182 .destroy_device
= destroy_device
,
183 .vring_state_changed
= vring_state_changed
,
184 .features_changed
= NULL
187 enum { DPDK_RING_SIZE
= 256 };
188 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
189 enum { DRAIN_TSC
= 200000ULL };
196 /* Quality of Service */
198 /* An instance of a QoS configuration. Always associated with a particular
201 * Each QoS implementation subclasses this with whatever additional data it
205 const struct dpdk_qos_ops
*ops
;
209 /* A particular implementation of dpdk QoS operations.
211 * The functions below return 0 if successful or a positive errno value on
212 * failure, except where otherwise noted. All of them must be provided, except
213 * where otherwise noted.
215 struct dpdk_qos_ops
{
217 /* Name of the QoS type */
218 const char *qos_name
;
220 /* Called to construct a qos_conf object. The implementation should make
221 * the appropriate calls to configure QoS according to 'details'.
223 * The contents of 'details' should be documented as valid for 'ovs_name'
224 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
225 * (which is built as ovs-vswitchd.conf.db(8)).
227 * This function must return 0 if and only if it sets '*conf' to an
228 * initialized 'struct qos_conf'.
230 * For all QoS implementations it should always be non-null.
232 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
234 /* Destroys the data structures allocated by the implementation as part of
237 * For all QoS implementations it should always be non-null.
239 void (*qos_destruct
)(struct qos_conf
*conf
);
241 /* Retrieves details of 'conf' configuration into 'details'.
243 * The contents of 'details' should be documented as valid for 'ovs_name'
244 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
245 * (which is built as ovs-vswitchd.conf.db(8)).
247 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
249 /* Returns true if 'conf' is already configured according to 'details'.
251 * The contents of 'details' should be documented as valid for 'ovs_name'
252 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
253 * (which is built as ovs-vswitchd.conf.db(8)).
255 * For all QoS implementations it should always be non-null.
257 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
258 const struct smap
*details
);
260 /* Modify an array of rte_mbufs. The modification is specific to
261 * each qos implementation.
263 * The function should take and array of mbufs and an int representing
264 * the current number of mbufs present in the array.
266 * After the function has performed a qos modification to the array of
267 * mbufs it returns an int representing the number of mbufs now present in
268 * the array. This value is can then be passed to the port send function
269 * along with the modified array for transmission.
271 * For all QoS implementations it should always be non-null.
273 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
274 int pkt_cnt
, bool may_steal
);
277 /* dpdk_qos_ops for each type of user space QoS implementation */
278 static const struct dpdk_qos_ops egress_policer_ops
;
281 * Array of dpdk_qos_ops, contains pointer to all supported QoS
284 static const struct dpdk_qos_ops
*const qos_confs
[] = {
289 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
291 /* Contains all 'struct dpdk_dev's. */
292 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
293 = OVS_LIST_INITIALIZER(&dpdk_list
);
295 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
296 = OVS_MUTEX_INITIALIZER
;
298 /* There should be one 'struct dpdk_tx_queue' created for
300 struct dpdk_tx_queue
{
301 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
302 * from concurrent access. It is used only
303 * if the queue is shared among different
304 * pmd threads (see 'concurrent_txq'). */
305 int map
; /* Mapping of configured vhost-user queues
306 * to enabled by guest. */
309 /* dpdk has no way to remove dpdk ring ethernet devices
310 so we have to keep them around once they've been created
313 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
314 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
317 /* For the client rings */
318 struct rte_ring
*cring_tx
;
319 struct rte_ring
*cring_rx
;
320 unsigned int user_port_id
; /* User given port no, parsed from port name */
321 dpdk_port_t eth_port_id
; /* ethernet device port id */
322 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
325 struct ingress_policer
{
326 struct rte_meter_srtcm_params app_srtcm_params
;
327 struct rte_meter_srtcm in_policer
;
328 rte_spinlock_t policer_lock
;
331 enum dpdk_hw_ol_features
{
332 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
336 * In order to avoid confusion in variables names, following naming convention
337 * should be used, if possible:
339 * 'struct netdev' : 'netdev'
340 * 'struct netdev_dpdk' : 'dev'
341 * 'struct netdev_rxq' : 'rxq'
342 * 'struct netdev_rxq_dpdk' : 'rx'
345 * struct netdev *netdev = netdev_from_name(name);
346 * struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
348 * Also, 'netdev' should be used instead of 'dev->up', where 'netdev' was
353 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline0
,
356 /* If true, device was attached by rte_eth_dev_attach(). */
358 struct eth_addr hwaddr
;
363 enum dpdk_dev_type type
;
364 enum netdev_flags flags
;
365 char *devargs
; /* Device arguments for dpdk ports */
366 struct dpdk_tx_queue
*tx_q
;
367 struct rte_eth_link link
;
369 /* 4 pad bytes here. */
372 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline1
,
373 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
374 struct rte_mempool
*mp
;
376 /* virtio identifier for vhost devices */
379 /* True if vHost device is 'up' and has been reconfigured at least once */
380 bool vhost_reconfigured
;
381 /* 3 pad bytes here. */
384 PADDED_MEMBERS(CACHE_LINE_SIZE
,
385 /* Identifier used to distinguish vhost devices from each other. */
386 char vhost_id
[PATH_MAX
];
389 PADDED_MEMBERS(CACHE_LINE_SIZE
,
392 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
394 /* QoS configuration and lock for the device */
395 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
397 /* Ingress Policer */
398 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
399 uint32_t policer_rate
;
400 uint32_t policer_burst
;
403 PADDED_MEMBERS(CACHE_LINE_SIZE
,
404 struct netdev_stats stats
;
406 rte_spinlock_t stats_lock
;
407 /* 44 pad bytes here. */
410 PADDED_MEMBERS(CACHE_LINE_SIZE
,
411 /* The following properties cannot be changed when a device is running,
412 * so we remember the request and update them next time
413 * netdev_dpdk*_reconfigure() is called */
417 int requested_rxq_size
;
418 int requested_txq_size
;
420 /* Number of rx/tx descriptors for physical devices */
424 /* Socket ID detected when vHost device is brought up */
425 int requested_socket_id
;
427 /* Denotes whether vHost port is client/server mode */
428 uint64_t vhost_driver_flags
;
430 /* DPDK-ETH Flow control */
431 struct rte_eth_fc_conf fc_conf
;
433 /* DPDK-ETH hardware offload features,
434 * from the enum set 'dpdk_hw_ol_features' */
435 uint32_t hw_ol_features
;
439 struct netdev_rxq_dpdk
{
440 struct netdev_rxq up
;
444 static void netdev_dpdk_destruct(struct netdev
*netdev
);
445 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
447 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
449 struct ingress_policer
*
450 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
453 is_dpdk_class(const struct netdev_class
*class)
455 return class->destruct
== netdev_dpdk_destruct
456 || class->destruct
== netdev_dpdk_vhost_destruct
;
459 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
460 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
461 * value, insufficient buffers are allocated to accomodate the packet in its
462 * entirety. Furthermore, certain drivers need to ensure that there is also
463 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
464 * frames). If the RX buffer is too small, then the driver enables scatter RX
465 * behaviour, which reduces performance. To prevent this, use a buffer size
466 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
469 dpdk_buf_size(int mtu
)
471 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
472 NETDEV_DPDK_MBUF_ALIGN
);
475 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
477 * Unlike xmalloc(), this function can return NULL on failure. */
479 dpdk_rte_mzalloc(size_t sz
)
481 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
485 free_dpdk_buf(struct dp_packet
*p
)
487 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
489 rte_pktmbuf_free(pkt
);
493 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
494 void *opaque_arg OVS_UNUSED
,
496 unsigned i OVS_UNUSED
)
498 struct rte_mbuf
*pkt
= _p
;
500 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
503 /* Returns a valid pointer when either of the following is true:
504 * - a new mempool was just created;
505 * - a matching mempool already exists. */
506 static struct rte_mempool
*
507 dpdk_mp_create(struct netdev_dpdk
*dev
, int mtu
)
509 char mp_name
[RTE_MEMPOOL_NAMESIZE
];
510 const char *netdev_name
= netdev_get_name(&dev
->up
);
511 int socket_id
= dev
->requested_socket_id
;
513 uint32_t hash
= hash_string(netdev_name
, 0);
514 struct rte_mempool
*mp
= NULL
;
517 * XXX: rough estimation of number of mbufs required for this port:
518 * <packets required to fill the device rxqs>
519 * + <packets that could be stuck on other ports txqs>
520 * + <packets in the pmd threads>
521 * + <additional memory for corner cases>
523 n_mbufs
= dev
->requested_n_rxq
* dev
->requested_rxq_size
524 + dev
->requested_n_txq
* dev
->requested_txq_size
525 + MIN(RTE_MAX_LCORE
, dev
->requested_n_rxq
) * NETDEV_MAX_BURST
528 ovs_mutex_lock(&dpdk_mp_mutex
);
530 /* Full DPDK memory pool name must be unique and cannot be
531 * longer than RTE_MEMPOOL_NAMESIZE. */
532 int ret
= snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
,
533 "ovs%08x%02d%05d%07u",
534 hash
, socket_id
, mtu
, n_mbufs
);
535 if (ret
< 0 || ret
>= RTE_MEMPOOL_NAMESIZE
) {
536 VLOG_DBG("snprintf returned %d. "
537 "Failed to generate a mempool name for \"%s\". "
538 "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.",
539 ret
, netdev_name
, hash
, socket_id
, mtu
, n_mbufs
);
543 VLOG_DBG("Port %s: Requesting a mempool of %u mbufs "
544 "on socket %d for %d Rx and %d Tx queues.",
545 netdev_name
, n_mbufs
, socket_id
,
546 dev
->requested_n_rxq
, dev
->requested_n_txq
);
548 mp
= rte_pktmbuf_pool_create(mp_name
, n_mbufs
, MP_CACHE_SZ
,
549 sizeof (struct dp_packet
) - sizeof (struct rte_mbuf
),
550 MBUF_SIZE(mtu
) - sizeof(struct dp_packet
), socket_id
);
553 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
555 /* rte_pktmbuf_pool_create has done some initialization of the
556 * rte_mbuf part of each dp_packet. Some OvS specific fields
557 * of the packet still need to be initialized by
558 * ovs_rte_pktmbuf_init. */
559 rte_mempool_obj_iter(mp
, ovs_rte_pktmbuf_init
, NULL
);
560 } else if (rte_errno
== EEXIST
) {
561 /* A mempool with the same name already exists. We just
562 * retrieve its pointer to be returned to the caller. */
563 mp
= rte_mempool_lookup(mp_name
);
564 /* As the mempool create returned EEXIST we can expect the
565 * lookup has returned a valid pointer. If for some reason
566 * that's not the case we keep track of it. */
567 VLOG_DBG("A mempool with name \"%s\" already exists at %p.",
570 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
573 } while (!mp
&& rte_errno
== ENOMEM
&& (n_mbufs
/= 2) >= MIN_NB_MBUF
);
575 ovs_mutex_unlock(&dpdk_mp_mutex
);
579 /* Release an existing mempool. */
581 dpdk_mp_free(struct rte_mempool
*mp
)
587 ovs_mutex_lock(&dpdk_mp_mutex
);
588 VLOG_DBG("Releasing \"%s\" mempool", mp
->name
);
589 rte_mempool_free(mp
);
590 ovs_mutex_unlock(&dpdk_mp_mutex
);
593 /* Tries to allocate a new mempool - or re-use an existing one where
594 * appropriate - on requested_socket_id with a size determined by
595 * requested_mtu and requested Rx/Tx queues.
596 * On success - or when re-using an existing mempool - the new configuration
598 * On error, device will be left unchanged. */
600 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
601 OVS_REQUIRES(dev
->mutex
)
603 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
604 struct rte_mempool
*mp
;
607 mp
= dpdk_mp_create(dev
, FRAME_LEN_TO_MTU(buf_size
));
609 VLOG_ERR("Failed to create memory pool for netdev "
610 "%s, with MTU %d on socket %d: %s\n",
611 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
612 rte_strerror(rte_errno
));
615 /* If a new MTU was requested and its rounded value equals the one
616 * that is currently used, then the existing mempool is returned. */
618 /* A new mempool was created, release the previous one. */
619 dpdk_mp_free(dev
->mp
);
624 dev
->mtu
= dev
->requested_mtu
;
625 dev
->socket_id
= dev
->requested_socket_id
;
626 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
633 check_link_status(struct netdev_dpdk
*dev
)
635 struct rte_eth_link link
;
637 rte_eth_link_get_nowait(dev
->port_id
, &link
);
639 if (dev
->link
.link_status
!= link
.link_status
) {
640 netdev_change_seq_changed(&dev
->up
);
642 dev
->link_reset_cnt
++;
644 if (dev
->link
.link_status
) {
645 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
646 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
647 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
648 ("full-duplex") : ("half-duplex"));
650 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
656 dpdk_watchdog(void *dummy OVS_UNUSED
)
658 struct netdev_dpdk
*dev
;
660 pthread_detach(pthread_self());
663 ovs_mutex_lock(&dpdk_mutex
);
664 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
665 ovs_mutex_lock(&dev
->mutex
);
666 if (dev
->type
== DPDK_DEV_ETH
) {
667 check_link_status(dev
);
669 ovs_mutex_unlock(&dev
->mutex
);
671 ovs_mutex_unlock(&dpdk_mutex
);
672 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
679 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
683 struct rte_eth_conf conf
= port_conf
;
685 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
687 if (dev
->mtu
> ETHER_MTU
) {
688 conf
.rxmode
.enable_scatter
= 1;
691 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
692 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
693 /* A device may report more queues than it makes available (this has
694 * been observed for Intel xl710, which reserves some of them for
695 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
696 * available. When this happens we can retry the configuration
697 * and request less queues */
698 while (n_rxq
&& n_txq
) {
700 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
703 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
705 VLOG_WARN("Interface %s eth_dev setup error %s\n",
706 dev
->up
.name
, rte_strerror(-diag
));
710 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
712 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
713 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
717 for (i
= 0; i
< n_txq
; i
++) {
718 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
719 dev
->socket_id
, NULL
);
721 VLOG_INFO("Interface %s txq(%d) setup error: %s",
722 dev
->up
.name
, i
, rte_strerror(-diag
));
728 /* Retry with less tx queues */
733 for (i
= 0; i
< n_rxq
; i
++) {
734 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
735 dev
->socket_id
, NULL
, dev
->mp
);
737 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
738 dev
->up
.name
, i
, rte_strerror(-diag
));
744 /* Retry with less rx queues */
749 dev
->up
.n_rxq
= n_rxq
;
750 dev
->up
.n_txq
= n_txq
;
759 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
761 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
762 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
768 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
769 OVS_REQUIRES(dev
->mutex
)
771 struct rte_pktmbuf_pool_private
*mbp_priv
;
772 struct rte_eth_dev_info info
;
773 struct ether_addr eth_addr
;
776 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
777 DEV_RX_OFFLOAD_TCP_CKSUM
|
778 DEV_RX_OFFLOAD_IPV4_CKSUM
;
780 rte_eth_dev_info_get(dev
->port_id
, &info
);
782 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
783 rx_chksm_offload_capa
) {
784 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
786 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
788 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
791 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
792 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
794 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
796 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
797 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
801 diag
= rte_eth_dev_start(dev
->port_id
);
803 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
804 rte_strerror(-diag
));
808 rte_eth_promiscuous_enable(dev
->port_id
);
809 rte_eth_allmulticast_enable(dev
->port_id
);
811 memset(ð_addr
, 0x0, sizeof(eth_addr
));
812 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
813 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
814 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
816 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
817 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
819 mbp_priv
= rte_mempool_get_priv(dev
->mp
);
820 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
822 /* Get the Flow control configuration for DPDK-ETH */
823 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
825 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
832 static struct netdev_dpdk
*
833 netdev_dpdk_cast(const struct netdev
*netdev
)
835 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
838 static struct netdev
*
839 netdev_dpdk_alloc(void)
841 struct netdev_dpdk
*dev
;
843 dev
= dpdk_rte_mzalloc(sizeof *dev
);
851 static struct dpdk_tx_queue
*
852 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
854 struct dpdk_tx_queue
*txqs
;
857 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
859 for (i
= 0; i
< n_txqs
; i
++) {
860 /* Initialize map for vhost devices. */
861 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
862 rte_spinlock_init(&txqs
[i
].tx_lock
);
870 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
871 enum dpdk_dev_type type
, int socket_id
)
872 OVS_REQUIRES(dpdk_mutex
)
874 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
876 ovs_mutex_init(&dev
->mutex
);
878 rte_spinlock_init(&dev
->stats_lock
);
880 /* If the 'sid' is negative, it means that the kernel fails
881 * to obtain the pci numa info. In that situation, always
883 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
884 dev
->requested_socket_id
= dev
->socket_id
;
885 dev
->port_id
= port_no
;
888 dev
->requested_mtu
= ETHER_MTU
;
889 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
890 ovsrcu_index_init(&dev
->vid
, -1);
891 dev
->vhost_reconfigured
= false;
892 dev
->attached
= false;
894 ovsrcu_init(&dev
->qos_conf
, NULL
);
896 ovsrcu_init(&dev
->ingress_policer
, NULL
);
897 dev
->policer_rate
= 0;
898 dev
->policer_burst
= 0;
902 dev
->requested_n_rxq
= NR_QUEUE
;
903 dev
->requested_n_txq
= NR_QUEUE
;
904 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
905 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
907 /* Initialize the flow control to NULL */
908 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
910 /* Initilize the hardware offload flags to 0 */
911 dev
->hw_ol_features
= 0;
913 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
915 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
917 netdev_request_reconfigure(netdev
);
922 /* dev_name must be the prefix followed by a positive decimal number.
923 * (no leading + or - signs are allowed) */
925 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
926 unsigned int *port_no
)
930 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
934 cport
= dev_name
+ strlen(prefix
);
936 if (str_to_uint(cport
, 10, port_no
)) {
944 vhost_common_construct(struct netdev
*netdev
)
945 OVS_REQUIRES(dpdk_mutex
)
947 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
948 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
950 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
955 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
956 DPDK_DEV_VHOST
, socket_id
);
960 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
962 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
963 const char *name
= netdev
->name
;
966 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
967 * the file system. '/' or '\' would traverse directories, so they're not
968 * acceptable in 'name'. */
969 if (strchr(name
, '/') || strchr(name
, '\\')) {
970 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
971 "A valid name must not include '/' or '\\'",
976 ovs_mutex_lock(&dpdk_mutex
);
977 /* Take the name of the vhost-user port and append it to the location where
978 * the socket is to be created, then register the socket.
980 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
981 dpdk_get_vhost_sock_dir(), name
);
983 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
984 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
986 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
990 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
991 VLOG_INFO("Socket %s created for vhost-user port %s\n",
992 dev
->vhost_id
, name
);
995 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
996 &virtio_net_device_ops
);
998 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1003 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1004 1ULL << VIRTIO_NET_F_HOST_TSO4
1005 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1006 | 1ULL << VIRTIO_NET_F_CSUM
);
1008 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1009 "port: %s\n", name
);
1013 err
= rte_vhost_driver_start(dev
->vhost_id
);
1015 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1016 "port: %s\n", name
);
1020 err
= vhost_common_construct(netdev
);
1022 VLOG_ERR("vhost_common_construct failed for vhost user "
1023 "port: %s\n", name
);
1027 ovs_mutex_unlock(&dpdk_mutex
);
1028 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1029 "please migrate to dpdkvhostuserclient ports.");
1034 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1038 ovs_mutex_lock(&dpdk_mutex
);
1039 err
= vhost_common_construct(netdev
);
1041 VLOG_ERR("vhost_common_construct failed for vhost user client"
1042 "port: %s\n", netdev
->name
);
1044 ovs_mutex_unlock(&dpdk_mutex
);
1049 netdev_dpdk_construct(struct netdev
*netdev
)
1053 ovs_mutex_lock(&dpdk_mutex
);
1054 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1055 DPDK_DEV_ETH
, SOCKET0
);
1056 ovs_mutex_unlock(&dpdk_mutex
);
1061 common_destruct(struct netdev_dpdk
*dev
)
1062 OVS_REQUIRES(dpdk_mutex
)
1063 OVS_EXCLUDED(dev
->mutex
)
1065 rte_free(dev
->tx_q
);
1066 dpdk_mp_free(dev
->mp
);
1068 ovs_list_remove(&dev
->list_node
);
1069 free(ovsrcu_get_protected(struct ingress_policer
*,
1070 &dev
->ingress_policer
));
1071 ovs_mutex_destroy(&dev
->mutex
);
1075 netdev_dpdk_destruct(struct netdev
*netdev
)
1077 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1078 char devname
[RTE_ETH_NAME_MAX_LEN
];
1080 ovs_mutex_lock(&dpdk_mutex
);
1082 rte_eth_dev_stop(dev
->port_id
);
1084 if (dev
->attached
) {
1085 rte_eth_dev_close(dev
->port_id
);
1086 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1087 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1089 VLOG_INFO("Device '%s' has been detached", devname
);
1094 common_destruct(dev
);
1096 ovs_mutex_unlock(&dpdk_mutex
);
1099 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1100 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1101 * deadlock, none of the mutexes must be held while calling this function. */
1103 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1105 OVS_EXCLUDED(dpdk_mutex
)
1106 OVS_EXCLUDED(dev
->mutex
)
1108 return rte_vhost_driver_unregister(vhost_id
);
1112 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1114 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1117 ovs_mutex_lock(&dpdk_mutex
);
1119 /* Guest becomes an orphan if still attached. */
1120 if (netdev_dpdk_get_vid(dev
) >= 0
1121 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1122 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1124 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1125 "socket '%s' must be restarted.", dev
->vhost_id
);
1128 vhost_id
= xstrdup(dev
->vhost_id
);
1130 common_destruct(dev
);
1132 ovs_mutex_unlock(&dpdk_mutex
);
1138 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1139 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1140 netdev
->name
, vhost_id
);
1141 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1142 /* OVS server mode - remove this socket from list for deletion */
1143 fatal_signal_remove_file_to_unlink(vhost_id
);
1150 netdev_dpdk_dealloc(struct netdev
*netdev
)
1152 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1158 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1160 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1162 ovs_mutex_lock(&dev
->mutex
);
1164 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1165 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1166 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1167 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1168 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1170 if (dev
->type
== DPDK_DEV_ETH
) {
1171 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1172 dev
->requested_rxq_size
);
1173 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1175 smap_add_format(args
, "requested_txq_descriptors", "%d",
1176 dev
->requested_txq_size
);
1177 smap_add_format(args
, "configured_txq_descriptors", "%d",
1179 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1180 smap_add(args
, "rx_csum_offload", "true");
1182 smap_add(args
, "rx_csum_offload", "false");
1185 ovs_mutex_unlock(&dev
->mutex
);
1190 static struct netdev_dpdk
*
1191 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1192 OVS_REQUIRES(dpdk_mutex
)
1194 struct netdev_dpdk
*dev
;
1196 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1197 if (dev
->port_id
== port_id
) {
1206 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1207 const char *devargs
, char **errp
)
1209 /* Get the name up to the first comma. */
1210 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1211 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1213 if (rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1214 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1215 /* Device not found in DPDK, attempt to attach it */
1216 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1217 /* Attach successful */
1218 dev
->attached
= true;
1219 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1221 /* Attach unsuccessful */
1222 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1223 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1233 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1234 OVS_REQUIRES(dev
->mutex
)
1238 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1239 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1240 dev
->requested_n_rxq
= new_n_rxq
;
1241 netdev_request_reconfigure(&dev
->up
);
1246 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1247 const char *flag
, int default_size
, int *new_size
)
1249 int queue_size
= smap_get_int(args
, flag
, default_size
);
1251 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1252 || !is_pow2(queue_size
)) {
1253 queue_size
= default_size
;
1256 if (queue_size
!= *new_size
) {
1257 *new_size
= queue_size
;
1258 netdev_request_reconfigure(netdev
);
1263 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1266 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1267 bool rx_fc_en
, tx_fc_en
, autoneg
;
1268 enum rte_eth_fc_mode fc_mode
;
1269 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1270 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1271 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1273 const char *new_devargs
;
1276 ovs_mutex_lock(&dpdk_mutex
);
1277 ovs_mutex_lock(&dev
->mutex
);
1279 dpdk_set_rxq_config(dev
, args
);
1281 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1282 NIC_PORT_DEFAULT_RXQ_SIZE
,
1283 &dev
->requested_rxq_size
);
1284 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1285 NIC_PORT_DEFAULT_TXQ_SIZE
,
1286 &dev
->requested_txq_size
);
1288 new_devargs
= smap_get(args
, "dpdk-devargs");
1290 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1291 /* The user requested a new device. If we return error, the caller
1292 * will delete this netdev and try to recreate it. */
1297 /* dpdk-devargs is required for device configuration */
1298 if (new_devargs
&& new_devargs
[0]) {
1299 /* Don't process dpdk-devargs if value is unchanged and port id
1301 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1302 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1303 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1306 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1308 } else if (new_port_id
== dev
->port_id
) {
1309 /* Already configured, do not reconfigure again */
1312 struct netdev_dpdk
*dup_dev
;
1314 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1316 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1317 "which is already in use by '%s'",
1318 netdev_get_name(netdev
), new_devargs
,
1319 netdev_get_name(&dup_dev
->up
));
1322 int sid
= rte_eth_dev_socket_id(new_port_id
);
1324 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1325 dev
->devargs
= xstrdup(new_devargs
);
1326 dev
->port_id
= new_port_id
;
1327 netdev_request_reconfigure(&dev
->up
);
1333 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1334 "The old 'dpdk<port_id>' names are not supported",
1335 netdev_get_name(netdev
));
1343 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1344 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1345 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1347 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1348 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1349 dev
->fc_conf
.mode
= fc_mode
;
1350 dev
->fc_conf
.autoneg
= autoneg
;
1351 dpdk_eth_flow_ctrl_setup(dev
);
1355 ovs_mutex_unlock(&dev
->mutex
);
1356 ovs_mutex_unlock(&dpdk_mutex
);
1362 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1363 char **errp OVS_UNUSED
)
1365 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1367 ovs_mutex_lock(&dev
->mutex
);
1368 dpdk_set_rxq_config(dev
, args
);
1369 ovs_mutex_unlock(&dev
->mutex
);
1375 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1376 const struct smap
*args
,
1377 char **errp OVS_UNUSED
)
1379 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1382 ovs_mutex_lock(&dev
->mutex
);
1383 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1384 path
= smap_get(args
, "vhost-server-path");
1385 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1386 strcpy(dev
->vhost_id
, path
);
1387 netdev_request_reconfigure(netdev
);
1390 ovs_mutex_unlock(&dev
->mutex
);
1396 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1398 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1400 return dev
->socket_id
;
1403 /* Sets the number of tx queues for the dpdk interface. */
1405 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1407 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1409 ovs_mutex_lock(&dev
->mutex
);
1411 if (dev
->requested_n_txq
== n_txq
) {
1415 dev
->requested_n_txq
= n_txq
;
1416 netdev_request_reconfigure(netdev
);
1419 ovs_mutex_unlock(&dev
->mutex
);
1423 static struct netdev_rxq
*
1424 netdev_dpdk_rxq_alloc(void)
1426 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1435 static struct netdev_rxq_dpdk
*
1436 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1438 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1442 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1444 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1445 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1447 ovs_mutex_lock(&dev
->mutex
);
1448 rx
->port_id
= dev
->port_id
;
1449 ovs_mutex_unlock(&dev
->mutex
);
1455 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1460 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1462 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1467 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1468 * 'pkts', even in case of failure.
1470 * Returns the number of packets that weren't transmitted. */
1472 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1473 struct rte_mbuf
**pkts
, int cnt
)
1477 while (nb_tx
!= cnt
) {
1480 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1488 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1489 /* Free buffers, which we couldn't transmit, one at a time (each
1490 * packet could come from a different mempool) */
1493 for (i
= nb_tx
; i
< cnt
; i
++) {
1494 rte_pktmbuf_free(pkts
[i
]);
1502 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1503 struct rte_mbuf
*pkt
, uint64_t time
)
1505 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1507 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1512 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1513 struct rte_mbuf
**pkts
, int pkt_cnt
,
1518 struct rte_mbuf
*pkt
= NULL
;
1519 uint64_t current_time
= rte_rdtsc();
1521 for (i
= 0; i
< pkt_cnt
; i
++) {
1523 /* Handle current packet */
1524 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1531 rte_pktmbuf_free(pkt
);
1540 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1541 int pkt_cnt
, bool may_steal
)
1545 rte_spinlock_lock(&policer
->policer_lock
);
1546 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1547 pkt_cnt
, may_steal
);
1548 rte_spinlock_unlock(&policer
->policer_lock
);
1554 is_vhost_running(struct netdev_dpdk
*dev
)
1556 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1560 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1561 unsigned int packet_size
)
1563 /* Hard-coded search for the size bucket. */
1564 if (packet_size
< 256) {
1565 if (packet_size
>= 128) {
1566 stats
->rx_128_to_255_packets
++;
1567 } else if (packet_size
<= 64) {
1568 stats
->rx_1_to_64_packets
++;
1570 stats
->rx_65_to_127_packets
++;
1573 if (packet_size
>= 1523) {
1574 stats
->rx_1523_to_max_packets
++;
1575 } else if (packet_size
>= 1024) {
1576 stats
->rx_1024_to_1522_packets
++;
1577 } else if (packet_size
< 512) {
1578 stats
->rx_256_to_511_packets
++;
1580 stats
->rx_512_to_1023_packets
++;
1586 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1587 struct dp_packet
**packets
, int count
,
1591 unsigned int packet_size
;
1592 struct dp_packet
*packet
;
1594 stats
->rx_packets
+= count
;
1595 stats
->rx_dropped
+= dropped
;
1596 for (i
= 0; i
< count
; i
++) {
1597 packet
= packets
[i
];
1598 packet_size
= dp_packet_size(packet
);
1600 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1601 /* This only protects the following multicast counting from
1602 * too short packets, but it does not stop the packet from
1603 * further processing. */
1605 stats
->rx_length_errors
++;
1609 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1611 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1612 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1616 stats
->rx_bytes
+= packet_size
;
1621 * The receive path for the vhost port is the TX path out from guest.
1624 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1625 struct dp_packet_batch
*batch
)
1627 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1628 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1630 uint16_t dropped
= 0;
1631 int qid
= rxq
->queue_id
;
1632 int vid
= netdev_dpdk_get_vid(dev
);
1634 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
1635 || !(dev
->flags
& NETDEV_UP
))) {
1639 nb_rx
= rte_vhost_dequeue_burst(vid
, qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1641 (struct rte_mbuf
**) batch
->packets
,
1649 nb_rx
= ingress_policer_run(policer
,
1650 (struct rte_mbuf
**) batch
->packets
,
1655 rte_spinlock_lock(&dev
->stats_lock
);
1656 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1658 rte_spinlock_unlock(&dev
->stats_lock
);
1660 batch
->count
= nb_rx
;
1661 dp_packet_batch_init_packet_fields(batch
);
1667 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1669 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1670 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1671 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1675 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1679 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1680 (struct rte_mbuf
**) batch
->packets
,
1688 nb_rx
= ingress_policer_run(policer
,
1689 (struct rte_mbuf
**) batch
->packets
,
1694 /* Update stats to reflect dropped packets */
1695 if (OVS_UNLIKELY(dropped
)) {
1696 rte_spinlock_lock(&dev
->stats_lock
);
1697 dev
->stats
.rx_dropped
+= dropped
;
1698 rte_spinlock_unlock(&dev
->stats_lock
);
1701 batch
->count
= nb_rx
;
1702 dp_packet_batch_init_packet_fields(batch
);
1708 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1709 int cnt
, bool may_steal
)
1711 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1714 rte_spinlock_lock(&qos_conf
->lock
);
1715 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1716 rte_spinlock_unlock(&qos_conf
->lock
);
1723 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1728 struct rte_mbuf
*pkt
;
1730 for (i
= 0; i
< pkt_cnt
; i
++) {
1732 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1733 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1734 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1735 rte_pktmbuf_free(pkt
);
1739 if (OVS_UNLIKELY(i
!= cnt
)) {
1749 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1750 struct dp_packet
**packets
,
1755 int sent
= attempted
- dropped
;
1757 stats
->tx_packets
+= sent
;
1758 stats
->tx_dropped
+= dropped
;
1760 for (i
= 0; i
< sent
; i
++) {
1761 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1766 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1767 struct dp_packet
**pkts
, int cnt
)
1769 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1770 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1771 unsigned int total_pkts
= cnt
;
1772 unsigned int dropped
= 0;
1774 int vid
= netdev_dpdk_get_vid(dev
);
1776 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1778 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
|| qid
< 0
1779 || !(dev
->flags
& NETDEV_UP
))) {
1780 rte_spinlock_lock(&dev
->stats_lock
);
1781 dev
->stats
.tx_dropped
+= cnt
;
1782 rte_spinlock_unlock(&dev
->stats_lock
);
1786 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1788 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1789 /* Check has QoS has been configured for the netdev */
1790 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
1791 dropped
= total_pkts
- cnt
;
1794 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1795 unsigned int tx_pkts
;
1797 tx_pkts
= rte_vhost_enqueue_burst(vid
, vhost_qid
, cur_pkts
, cnt
);
1798 if (OVS_LIKELY(tx_pkts
)) {
1799 /* Packets have been sent.*/
1801 /* Prepare for possible retry.*/
1802 cur_pkts
= &cur_pkts
[tx_pkts
];
1804 /* No packets sent - do not retry.*/
1807 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1809 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1811 rte_spinlock_lock(&dev
->stats_lock
);
1812 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1814 rte_spinlock_unlock(&dev
->stats_lock
);
1817 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1818 dp_packet_delete(pkts
[i
]);
1822 /* Tx function. Transmit packets indefinitely */
1824 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1825 OVS_NO_THREAD_SAFETY_ANALYSIS
1827 const size_t batch_cnt
= dp_packet_batch_size(batch
);
1828 #if !defined(__CHECKER__) && !defined(_WIN32)
1829 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
1831 /* Sparse or MSVC doesn't like variable length array. */
1832 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1834 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1835 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1836 uint32_t cnt
= batch_cnt
;
1837 uint32_t dropped
= 0;
1839 if (dev
->type
!= DPDK_DEV_VHOST
) {
1840 /* Check if QoS has been configured for this netdev. */
1841 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
1843 dropped
+= batch_cnt
- cnt
;
1846 dp_packet_batch_apply_cutlen(batch
);
1850 for (uint32_t i
= 0; i
< cnt
; i
++) {
1851 struct dp_packet
*packet
= batch
->packets
[i
];
1852 uint32_t size
= dp_packet_size(packet
);
1854 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1855 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
1856 size
, dev
->max_packet_len
);
1862 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->mp
);
1863 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
1868 /* We have to do a copy for now */
1869 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
1870 dp_packet_data(packet
), size
);
1871 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
1876 if (OVS_LIKELY(txcnt
)) {
1877 if (dev
->type
== DPDK_DEV_VHOST
) {
1878 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1881 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
1885 if (OVS_UNLIKELY(dropped
)) {
1886 rte_spinlock_lock(&dev
->stats_lock
);
1887 dev
->stats
.tx_dropped
+= dropped
;
1888 rte_spinlock_unlock(&dev
->stats_lock
);
1893 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1894 struct dp_packet_batch
*batch
,
1895 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1898 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1899 dpdk_do_tx_copy(netdev
, qid
, batch
);
1900 dp_packet_delete_batch(batch
, may_steal
);
1902 dp_packet_batch_apply_cutlen(batch
);
1903 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1909 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1910 struct dp_packet_batch
*batch
, bool may_steal
,
1911 bool concurrent_txq
)
1913 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1914 dp_packet_delete_batch(batch
, may_steal
);
1918 if (OVS_UNLIKELY(concurrent_txq
)) {
1919 qid
= qid
% dev
->up
.n_txq
;
1920 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1923 if (OVS_UNLIKELY(!may_steal
||
1924 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1925 struct netdev
*netdev
= &dev
->up
;
1927 dpdk_do_tx_copy(netdev
, qid
, batch
);
1928 dp_packet_delete_batch(batch
, may_steal
);
1930 int tx_cnt
, dropped
;
1931 int batch_cnt
= dp_packet_batch_size(batch
);
1932 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1934 dp_packet_batch_apply_cutlen(batch
);
1936 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
1937 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
1938 dropped
= batch_cnt
- tx_cnt
;
1940 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
1942 if (OVS_UNLIKELY(dropped
)) {
1943 rte_spinlock_lock(&dev
->stats_lock
);
1944 dev
->stats
.tx_dropped
+= dropped
;
1945 rte_spinlock_unlock(&dev
->stats_lock
);
1949 if (OVS_UNLIKELY(concurrent_txq
)) {
1950 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1955 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1956 struct dp_packet_batch
*batch
, bool may_steal
,
1957 bool concurrent_txq
)
1959 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1961 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1966 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1968 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1970 ovs_mutex_lock(&dev
->mutex
);
1971 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1973 netdev_change_seq_changed(netdev
);
1975 ovs_mutex_unlock(&dev
->mutex
);
1981 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1983 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1985 ovs_mutex_lock(&dev
->mutex
);
1987 ovs_mutex_unlock(&dev
->mutex
);
1993 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1995 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1997 ovs_mutex_lock(&dev
->mutex
);
1999 ovs_mutex_unlock(&dev
->mutex
);
2005 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2007 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2009 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2010 || mtu
< ETHER_MIN_MTU
) {
2011 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2015 ovs_mutex_lock(&dev
->mutex
);
2016 if (dev
->requested_mtu
!= mtu
) {
2017 dev
->requested_mtu
= mtu
;
2018 netdev_request_reconfigure(netdev
);
2020 ovs_mutex_unlock(&dev
->mutex
);
2026 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2029 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2030 struct netdev_stats
*stats
)
2032 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2034 ovs_mutex_lock(&dev
->mutex
);
2036 rte_spinlock_lock(&dev
->stats_lock
);
2037 /* Supported Stats */
2038 stats
->rx_packets
= dev
->stats
.rx_packets
;
2039 stats
->tx_packets
= dev
->stats
.tx_packets
;
2040 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2041 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2042 stats
->multicast
= dev
->stats
.multicast
;
2043 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2044 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2045 stats
->rx_errors
= dev
->stats
.rx_errors
;
2046 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2048 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2049 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2050 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2051 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2052 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2053 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2054 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2056 rte_spinlock_unlock(&dev
->stats_lock
);
2058 ovs_mutex_unlock(&dev
->mutex
);
2064 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2065 const struct rte_eth_xstat
*xstats
,
2066 const struct rte_eth_xstat_name
*names
,
2067 const unsigned int size
)
2069 for (unsigned int i
= 0; i
< size
; i
++) {
2070 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2071 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2072 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2073 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2074 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2075 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2076 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2077 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2078 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2079 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2080 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2081 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2082 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2083 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2084 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2085 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2086 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2087 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2088 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2089 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2090 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2091 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2092 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2093 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2094 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2095 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2096 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2097 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2098 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2099 stats
->multicast
= xstats
[i
].value
;
2100 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2101 stats
->tx_multicast_packets
= xstats
[i
].value
;
2102 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2103 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2104 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2105 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2106 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2107 stats
->rx_undersized_errors
= xstats
[i
].value
;
2108 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2109 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2110 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2111 stats
->rx_jabber_errors
= xstats
[i
].value
;
2117 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2119 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2120 struct rte_eth_stats rte_stats
;
2123 netdev_dpdk_get_carrier(netdev
, &gg
);
2124 ovs_mutex_lock(&dev
->mutex
);
2126 struct rte_eth_xstat
*rte_xstats
= NULL
;
2127 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2128 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2130 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2131 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2132 ovs_mutex_unlock(&dev
->mutex
);
2136 /* Get length of statistics */
2137 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2138 if (rte_xstats_len
< 0) {
2139 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2142 /* Reserve memory for xstats names and values */
2143 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2144 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2146 /* Retreive xstats names */
2147 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2150 if (rte_xstats_new_len
!= rte_xstats_len
) {
2151 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2154 /* Retreive xstats values */
2155 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2156 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2158 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2159 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2162 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2167 free(rte_xstats_names
);
2169 stats
->rx_packets
= rte_stats
.ipackets
;
2170 stats
->tx_packets
= rte_stats
.opackets
;
2171 stats
->rx_bytes
= rte_stats
.ibytes
;
2172 stats
->tx_bytes
= rte_stats
.obytes
;
2173 stats
->rx_errors
= rte_stats
.ierrors
;
2174 stats
->tx_errors
= rte_stats
.oerrors
;
2176 rte_spinlock_lock(&dev
->stats_lock
);
2177 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2178 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2179 rte_spinlock_unlock(&dev
->stats_lock
);
2181 /* These are the available DPDK counters for packets not received due to
2182 * local resource constraints in DPDK and NIC respectively. */
2183 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2184 stats
->rx_missed_errors
= rte_stats
.imissed
;
2186 ovs_mutex_unlock(&dev
->mutex
);
2192 netdev_dpdk_get_features(const struct netdev
*netdev
,
2193 enum netdev_features
*current
,
2194 enum netdev_features
*advertised
,
2195 enum netdev_features
*supported
,
2196 enum netdev_features
*peer
)
2198 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2199 struct rte_eth_link link
;
2201 ovs_mutex_lock(&dev
->mutex
);
2203 ovs_mutex_unlock(&dev
->mutex
);
2205 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2206 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2207 *current
= NETDEV_F_10MB_HD
;
2209 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2210 *current
= NETDEV_F_100MB_HD
;
2212 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2213 *current
= NETDEV_F_1GB_HD
;
2215 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2216 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2217 *current
= NETDEV_F_10MB_FD
;
2219 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2220 *current
= NETDEV_F_100MB_FD
;
2222 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2223 *current
= NETDEV_F_1GB_FD
;
2225 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2226 *current
= NETDEV_F_10GB_FD
;
2230 if (link
.link_autoneg
) {
2231 *current
|= NETDEV_F_AUTONEG
;
2234 *advertised
= *supported
= *peer
= 0;
2239 static struct ingress_policer
*
2240 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2242 struct ingress_policer
*policer
= NULL
;
2243 uint64_t rate_bytes
;
2244 uint64_t burst_bytes
;
2247 policer
= xmalloc(sizeof *policer
);
2248 rte_spinlock_init(&policer
->policer_lock
);
2250 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2251 rate_bytes
= rate
* 1000ULL / 8;
2252 burst_bytes
= burst
* 1000ULL / 8;
2254 policer
->app_srtcm_params
.cir
= rate_bytes
;
2255 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2256 policer
->app_srtcm_params
.ebs
= 0;
2257 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2258 &policer
->app_srtcm_params
);
2260 VLOG_ERR("Could not create rte meter for ingress policer");
2268 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2269 uint32_t policer_burst
)
2271 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2272 struct ingress_policer
*policer
;
2274 /* Force to 0 if no rate specified,
2275 * default to 8000 kbits if burst is 0,
2276 * else stick with user-specified value.
2278 policer_burst
= (!policer_rate
? 0
2279 : !policer_burst
? 8000
2282 ovs_mutex_lock(&dev
->mutex
);
2284 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2285 &dev
->ingress_policer
);
2287 if (dev
->policer_rate
== policer_rate
&&
2288 dev
->policer_burst
== policer_burst
) {
2289 /* Assume that settings haven't changed since we last set them. */
2290 ovs_mutex_unlock(&dev
->mutex
);
2294 /* Destroy any existing ingress policer for the device if one exists */
2296 ovsrcu_postpone(free
, policer
);
2299 if (policer_rate
!= 0) {
2300 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2304 ovsrcu_set(&dev
->ingress_policer
, policer
);
2305 dev
->policer_rate
= policer_rate
;
2306 dev
->policer_burst
= policer_burst
;
2307 ovs_mutex_unlock(&dev
->mutex
);
2313 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2315 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2317 ovs_mutex_lock(&dev
->mutex
);
2318 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2319 * postive integer to meet RFC 2863 recommendations.
2321 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2322 ovs_mutex_unlock(&dev
->mutex
);
2328 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2330 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2332 ovs_mutex_lock(&dev
->mutex
);
2333 check_link_status(dev
);
2334 *carrier
= dev
->link
.link_status
;
2336 ovs_mutex_unlock(&dev
->mutex
);
2342 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2344 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2346 ovs_mutex_lock(&dev
->mutex
);
2348 if (is_vhost_running(dev
)) {
2354 ovs_mutex_unlock(&dev
->mutex
);
2359 static long long int
2360 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2362 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2363 long long int carrier_resets
;
2365 ovs_mutex_lock(&dev
->mutex
);
2366 carrier_resets
= dev
->link_reset_cnt
;
2367 ovs_mutex_unlock(&dev
->mutex
);
2369 return carrier_resets
;
2373 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2374 long long int interval OVS_UNUSED
)
2380 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2381 enum netdev_flags off
, enum netdev_flags on
,
2382 enum netdev_flags
*old_flagsp
)
2383 OVS_REQUIRES(dev
->mutex
)
2385 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2389 *old_flagsp
= dev
->flags
;
2393 if (dev
->flags
== *old_flagsp
) {
2397 if (dev
->type
== DPDK_DEV_ETH
) {
2398 if (dev
->flags
& NETDEV_PROMISC
) {
2399 rte_eth_promiscuous_enable(dev
->port_id
);
2402 netdev_change_seq_changed(&dev
->up
);
2404 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2405 * running then change netdev's change_seq to trigger link state
2408 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2409 && is_vhost_running(dev
)) {
2410 netdev_change_seq_changed(&dev
->up
);
2412 /* Clear statistics if device is getting up. */
2413 if (NETDEV_UP
& on
) {
2414 rte_spinlock_lock(&dev
->stats_lock
);
2415 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2416 rte_spinlock_unlock(&dev
->stats_lock
);
2425 netdev_dpdk_update_flags(struct netdev
*netdev
,
2426 enum netdev_flags off
, enum netdev_flags on
,
2427 enum netdev_flags
*old_flagsp
)
2429 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2432 ovs_mutex_lock(&dev
->mutex
);
2433 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2434 ovs_mutex_unlock(&dev
->mutex
);
2440 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2442 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2443 struct rte_eth_dev_info dev_info
;
2445 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2449 ovs_mutex_lock(&dev
->mutex
);
2450 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2451 ovs_mutex_unlock(&dev
->mutex
);
2453 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2454 smap_add_format(args
, "numa_id", "%d",
2455 rte_eth_dev_socket_id(dev
->port_id
));
2456 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2457 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2458 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2459 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2460 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2461 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2462 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2463 dev_info
.max_hash_mac_addrs
);
2464 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2465 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2467 /* Querying the DPDK library for iftype may be done in future, pending
2468 * support; cf. RFC 3635 Section 3.2.4. */
2469 enum { IF_TYPE_ETHERNETCSMACD
= 6 };
2471 smap_add_format(args
, "if_type", "%"PRIu32
, IF_TYPE_ETHERNETCSMACD
);
2472 smap_add_format(args
, "if_descr", "%s %s", rte_version(),
2473 dev_info
.driver_name
);
2475 if (dev_info
.pci_dev
) {
2476 smap_add_format(args
, "pci-vendor_id", "0x%u",
2477 dev_info
.pci_dev
->id
.vendor_id
);
2478 smap_add_format(args
, "pci-device_id", "0x%x",
2479 dev_info
.pci_dev
->id
.device_id
);
2486 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2487 OVS_REQUIRES(dev
->mutex
)
2489 enum netdev_flags old_flags
;
2492 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2494 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2499 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2500 const char *argv
[], void *aux OVS_UNUSED
)
2504 if (!strcasecmp(argv
[argc
- 1], "up")) {
2506 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2509 unixctl_command_reply_error(conn
, "Invalid Admin State");
2514 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2516 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2517 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2519 ovs_mutex_lock(&dev
->mutex
);
2520 netdev_dpdk_set_admin_state__(dev
, up
);
2521 ovs_mutex_unlock(&dev
->mutex
);
2523 netdev_close(netdev
);
2525 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2526 netdev_close(netdev
);
2530 struct netdev_dpdk
*dev
;
2532 ovs_mutex_lock(&dpdk_mutex
);
2533 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2534 ovs_mutex_lock(&dev
->mutex
);
2535 netdev_dpdk_set_admin_state__(dev
, up
);
2536 ovs_mutex_unlock(&dev
->mutex
);
2538 ovs_mutex_unlock(&dpdk_mutex
);
2540 unixctl_command_reply(conn
, "OK");
2544 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2545 const char *argv
[], void *aux OVS_UNUSED
)
2549 dpdk_port_t port_id
;
2550 char devname
[RTE_ETH_NAME_MAX_LEN
];
2551 struct netdev_dpdk
*dev
;
2553 ovs_mutex_lock(&dpdk_mutex
);
2555 if (rte_eth_dev_get_port_by_name(argv
[1], &port_id
)) {
2556 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2560 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2562 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2563 "Remove it before detaching",
2564 argv
[1], netdev_get_name(&dev
->up
));
2568 rte_eth_dev_close(port_id
);
2570 ret
= rte_eth_dev_detach(port_id
, devname
);
2572 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2576 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2578 ovs_mutex_unlock(&dpdk_mutex
);
2579 unixctl_command_reply(conn
, response
);
2584 ovs_mutex_unlock(&dpdk_mutex
);
2585 unixctl_command_reply_error(conn
, response
);
2590 * Set virtqueue flags so that we do not receive interrupts.
2593 set_irq_status(int vid
)
2597 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2598 rte_vhost_enable_guest_notification(vid
, i
, 0);
2603 * Fixes mapping for vhost-user tx queues. Must be called after each
2604 * enabling/disabling of queues and n_txq modifications.
2607 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2608 OVS_REQUIRES(dev
->mutex
)
2610 int *enabled_queues
, n_enabled
= 0;
2611 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2613 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2615 for (i
= 0; i
< total_txqs
; i
++) {
2616 /* Enabled queues always mapped to themselves. */
2617 if (dev
->tx_q
[i
].map
== i
) {
2618 enabled_queues
[n_enabled
++] = i
;
2622 if (n_enabled
== 0 && total_txqs
!= 0) {
2623 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2628 for (i
= 0; i
< total_txqs
; i
++) {
2629 if (dev
->tx_q
[i
].map
!= i
) {
2630 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2631 k
= (k
+ 1) % n_enabled
;
2635 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2636 for (i
= 0; i
< total_txqs
; i
++) {
2637 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2640 free(enabled_queues
);
2644 * A new virtio-net device is added to a vhost port.
2649 struct netdev_dpdk
*dev
;
2650 bool exists
= false;
2652 char ifname
[IF_NAME_SZ
];
2654 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2656 ovs_mutex_lock(&dpdk_mutex
);
2657 /* Add device to the vhost port with the same name as that passed down. */
2658 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2659 ovs_mutex_lock(&dev
->mutex
);
2660 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2661 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
2663 /* Get NUMA information */
2664 newnode
= rte_vhost_get_numa_node(vid
);
2665 if (newnode
== -1) {
2667 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2670 newnode
= dev
->socket_id
;
2673 if (dev
->requested_n_txq
!= qp_num
2674 || dev
->requested_n_rxq
!= qp_num
2675 || dev
->requested_socket_id
!= newnode
) {
2676 dev
->requested_socket_id
= newnode
;
2677 dev
->requested_n_rxq
= qp_num
;
2678 dev
->requested_n_txq
= qp_num
;
2679 netdev_request_reconfigure(&dev
->up
);
2681 /* Reconfiguration not required. */
2682 dev
->vhost_reconfigured
= true;
2685 ovsrcu_index_set(&dev
->vid
, vid
);
2688 /* Disable notifications. */
2689 set_irq_status(vid
);
2690 netdev_change_seq_changed(&dev
->up
);
2691 ovs_mutex_unlock(&dev
->mutex
);
2694 ovs_mutex_unlock(&dev
->mutex
);
2696 ovs_mutex_unlock(&dpdk_mutex
);
2699 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2704 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2710 /* Clears mapping for all available queues of vhost interface. */
2712 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2713 OVS_REQUIRES(dev
->mutex
)
2717 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2718 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2723 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2724 * flag to stop any more packets from being sent or received to/from a VM and
2725 * ensure all currently queued packets have been sent/received before removing
2729 destroy_device(int vid
)
2731 struct netdev_dpdk
*dev
;
2732 bool exists
= false;
2733 char ifname
[IF_NAME_SZ
];
2735 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2737 ovs_mutex_lock(&dpdk_mutex
);
2738 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2739 if (netdev_dpdk_get_vid(dev
) == vid
) {
2741 ovs_mutex_lock(&dev
->mutex
);
2742 dev
->vhost_reconfigured
= false;
2743 ovsrcu_index_set(&dev
->vid
, -1);
2744 netdev_dpdk_txq_map_clear(dev
);
2746 netdev_change_seq_changed(&dev
->up
);
2747 ovs_mutex_unlock(&dev
->mutex
);
2753 ovs_mutex_unlock(&dpdk_mutex
);
2757 * Wait for other threads to quiesce after setting the 'virtio_dev'
2758 * to NULL, before returning.
2760 ovsrcu_synchronize();
2762 * As call to ovsrcu_synchronize() will end the quiescent state,
2763 * put thread back into quiescent state before returning.
2765 ovsrcu_quiesce_start();
2766 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2768 VLOG_INFO("vHost Device '%s' not found", ifname
);
2773 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2775 struct netdev_dpdk
*dev
;
2776 bool exists
= false;
2777 int qid
= queue_id
/ VIRTIO_QNUM
;
2778 char ifname
[IF_NAME_SZ
];
2780 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2782 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2786 ovs_mutex_lock(&dpdk_mutex
);
2787 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2788 ovs_mutex_lock(&dev
->mutex
);
2789 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2791 dev
->tx_q
[qid
].map
= qid
;
2793 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2795 netdev_dpdk_remap_txqs(dev
);
2797 ovs_mutex_unlock(&dev
->mutex
);
2800 ovs_mutex_unlock(&dev
->mutex
);
2802 ovs_mutex_unlock(&dpdk_mutex
);
2805 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2806 "changed to \'%s\'", queue_id
, qid
, ifname
,
2807 (enable
== 1) ? "enabled" : "disabled");
2809 VLOG_INFO("vHost Device '%s' not found", ifname
);
2817 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2819 return ovsrcu_index_get(&dev
->vid
);
2822 struct ingress_policer
*
2823 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2825 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2829 netdev_dpdk_class_init(void)
2831 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2833 /* This function can be called for different classes. The initialization
2834 * needs to be done only once */
2835 if (ovsthread_once_start(&once
)) {
2836 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2837 unixctl_command_register("netdev-dpdk/set-admin-state",
2838 "[netdev] up|down", 1, 2,
2839 netdev_dpdk_set_admin_state
, NULL
);
2841 unixctl_command_register("netdev-dpdk/detach",
2842 "pci address of device", 1, 1,
2843 netdev_dpdk_detach
, NULL
);
2845 ovsthread_once_done(&once
);
2855 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2856 dpdk_port_t
*eth_port_id
)
2858 struct dpdk_ring
*ring_pair
;
2862 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2867 /* XXX: Add support for multiquque ring. */
2868 ring_name
= xasprintf("%s_tx", dev_name
);
2870 /* Create single producer tx ring, netdev does explicit locking. */
2871 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2874 if (ring_pair
->cring_tx
== NULL
) {
2875 rte_free(ring_pair
);
2879 ring_name
= xasprintf("%s_rx", dev_name
);
2881 /* Create single consumer rx ring, netdev does explicit locking. */
2882 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2885 if (ring_pair
->cring_rx
== NULL
) {
2886 rte_free(ring_pair
);
2890 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2891 &ring_pair
->cring_tx
, 1, SOCKET0
);
2894 rte_free(ring_pair
);
2898 ring_pair
->user_port_id
= port_no
;
2899 ring_pair
->eth_port_id
= port_id
;
2900 *eth_port_id
= port_id
;
2902 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2908 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
2909 OVS_REQUIRES(dpdk_mutex
)
2911 struct dpdk_ring
*ring_pair
;
2912 unsigned int port_no
;
2915 /* Names always start with "dpdkr" */
2916 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2921 /* Look through our list to find the device */
2922 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2923 if (ring_pair
->user_port_id
== port_no
) {
2924 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2925 /* Really all that is needed */
2926 *eth_port_id
= ring_pair
->eth_port_id
;
2930 /* Need to create the device rings */
2931 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2935 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2936 struct dp_packet_batch
*batch
, bool may_steal
,
2937 bool concurrent_txq
)
2939 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2940 struct dp_packet
*packet
;
2942 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2943 * the rss hash field is clear. This is because the same mbuf may be
2944 * modified by the consumer of the ring and return into the datapath
2945 * without recalculating the RSS hash. */
2946 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
2947 dp_packet_mbuf_rss_flag_reset(packet
);
2950 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2955 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2957 dpdk_port_t port_no
= 0;
2960 ovs_mutex_lock(&dpdk_mutex
);
2962 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2967 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2968 rte_eth_dev_socket_id(port_no
));
2970 ovs_mutex_unlock(&dpdk_mutex
);
2977 * Initialize QoS configuration operations.
2980 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2983 rte_spinlock_init(&conf
->lock
);
2987 * Search existing QoS operations in qos_ops and compare each set of
2988 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2991 static const struct dpdk_qos_ops
*
2992 qos_lookup_name(const char *name
)
2994 const struct dpdk_qos_ops
*const *opsp
;
2996 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2997 const struct dpdk_qos_ops
*ops
= *opsp
;
2998 if (!strcmp(name
, ops
->qos_name
)) {
3006 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3009 const struct dpdk_qos_ops
*const *opsp
;
3011 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3012 const struct dpdk_qos_ops
*ops
= *opsp
;
3013 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3014 sset_add(types
, ops
->qos_name
);
3021 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3022 const char **typep
, struct smap
*details
)
3024 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3025 struct qos_conf
*qos_conf
;
3028 ovs_mutex_lock(&dev
->mutex
);
3029 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3031 *typep
= qos_conf
->ops
->qos_name
;
3032 error
= (qos_conf
->ops
->qos_get
3033 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3035 /* No QoS configuration set, return an empty string */
3038 ovs_mutex_unlock(&dev
->mutex
);
3044 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3045 const struct smap
*details
)
3047 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3048 const struct dpdk_qos_ops
*new_ops
= NULL
;
3049 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3052 ovs_mutex_lock(&dev
->mutex
);
3054 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3056 new_ops
= qos_lookup_name(type
);
3058 if (!new_ops
|| !new_ops
->qos_construct
) {
3059 new_qos_conf
= NULL
;
3060 if (type
&& type
[0]) {
3063 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3064 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3065 new_qos_conf
= qos_conf
;
3067 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3071 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3072 type
, netdev
->name
, rte_strerror(error
));
3075 if (new_qos_conf
!= qos_conf
) {
3076 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3078 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3082 ovs_mutex_unlock(&dev
->mutex
);
3087 /* egress-policer details */
3089 struct egress_policer
{
3090 struct qos_conf qos_conf
;
3091 struct rte_meter_srtcm_params app_srtcm_params
;
3092 struct rte_meter_srtcm egress_meter
;
3096 egress_policer_details_to_param(const struct smap
*details
,
3097 struct rte_meter_srtcm_params
*params
)
3099 memset(params
, 0, sizeof *params
);
3100 params
->cir
= smap_get_ullong(details
, "cir", 0);
3101 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3106 egress_policer_qos_construct(const struct smap
*details
,
3107 struct qos_conf
**conf
)
3109 struct egress_policer
*policer
;
3112 policer
= xmalloc(sizeof *policer
);
3113 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3114 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3115 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3116 &policer
->app_srtcm_params
);
3118 *conf
= &policer
->qos_conf
;
3129 egress_policer_qos_destruct(struct qos_conf
*conf
)
3131 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3137 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3139 struct egress_policer
*policer
=
3140 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3142 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3143 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3149 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3150 const struct smap
*details
)
3152 struct egress_policer
*policer
=
3153 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3154 struct rte_meter_srtcm_params params
;
3156 egress_policer_details_to_param(details
, ¶ms
);
3158 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3162 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3166 struct egress_policer
*policer
=
3167 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3169 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3170 pkt_cnt
, may_steal
);
3175 static const struct dpdk_qos_ops egress_policer_ops
= {
3176 "egress-policer", /* qos_name */
3177 egress_policer_qos_construct
,
3178 egress_policer_qos_destruct
,
3179 egress_policer_qos_get
,
3180 egress_policer_qos_is_equal
,
3185 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3187 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3190 ovs_mutex_lock(&dev
->mutex
);
3192 if (netdev
->n_txq
== dev
->requested_n_txq
3193 && netdev
->n_rxq
== dev
->requested_n_rxq
3194 && dev
->mtu
== dev
->requested_mtu
3195 && dev
->rxq_size
== dev
->requested_rxq_size
3196 && dev
->txq_size
== dev
->requested_txq_size
3197 && dev
->socket_id
== dev
->requested_socket_id
) {
3198 /* Reconfiguration is unnecessary */
3203 rte_eth_dev_stop(dev
->port_id
);
3205 err
= netdev_dpdk_mempool_configure(dev
);
3206 if (err
&& err
!= EEXIST
) {
3210 netdev
->n_txq
= dev
->requested_n_txq
;
3211 netdev
->n_rxq
= dev
->requested_n_rxq
;
3213 dev
->rxq_size
= dev
->requested_rxq_size
;
3214 dev
->txq_size
= dev
->requested_txq_size
;
3216 rte_free(dev
->tx_q
);
3217 err
= dpdk_eth_dev_init(dev
);
3218 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3223 netdev_change_seq_changed(netdev
);
3226 ovs_mutex_unlock(&dev
->mutex
);
3231 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3232 OVS_REQUIRES(dev
->mutex
)
3234 dev
->up
.n_txq
= dev
->requested_n_txq
;
3235 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3238 /* Enable TX queue 0 by default if it wasn't disabled. */
3239 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3240 dev
->tx_q
[0].map
= 0;
3243 netdev_dpdk_remap_txqs(dev
);
3245 err
= netdev_dpdk_mempool_configure(dev
);
3247 /* A new mempool was created. */
3248 netdev_change_seq_changed(&dev
->up
);
3249 } else if (err
!= EEXIST
){
3252 if (netdev_dpdk_get_vid(dev
) >= 0) {
3253 if (dev
->vhost_reconfigured
== false) {
3254 dev
->vhost_reconfigured
= true;
3255 /* Carrier status may need updating. */
3256 netdev_change_seq_changed(&dev
->up
);
3264 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3266 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3269 ovs_mutex_lock(&dev
->mutex
);
3270 err
= dpdk_vhost_reconfigure_helper(dev
);
3271 ovs_mutex_unlock(&dev
->mutex
);
3277 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3279 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3281 uint64_t vhost_flags
= 0;
3283 ovs_mutex_lock(&dev
->mutex
);
3285 /* Configure vHost client mode if requested and if the following criteria
3287 * 1. Device hasn't been registered yet.
3288 * 2. A path has been specified.
3290 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3291 && strlen(dev
->vhost_id
)) {
3292 /* Register client-mode device. */
3293 vhost_flags
|= RTE_VHOST_USER_CLIENT
;
3295 /* Enable IOMMU support, if explicitly requested. */
3296 if (dpdk_vhost_iommu_enabled()) {
3297 vhost_flags
|= RTE_VHOST_USER_IOMMU_SUPPORT
;
3299 err
= rte_vhost_driver_register(dev
->vhost_id
, vhost_flags
);
3301 VLOG_ERR("vhost-user device setup failure for device %s\n",
3305 /* Configuration successful */
3306 dev
->vhost_driver_flags
|= vhost_flags
;
3307 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3308 "using client socket '%s'",
3309 dev
->up
.name
, dev
->vhost_id
);
3312 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3313 &virtio_net_device_ops
);
3315 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3316 "vhost user client port: %s\n", dev
->up
.name
);
3320 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3321 1ULL << VIRTIO_NET_F_HOST_TSO4
3322 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3323 | 1ULL << VIRTIO_NET_F_CSUM
);
3325 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3326 "client port: %s\n", dev
->up
.name
);
3330 err
= rte_vhost_driver_start(dev
->vhost_id
);
3332 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3333 "client port: %s\n", dev
->up
.name
);
3338 err
= dpdk_vhost_reconfigure_helper(dev
);
3341 ovs_mutex_unlock(&dev
->mutex
);
3346 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3347 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3348 GET_CARRIER, GET_STATS, \
3349 GET_FEATURES, GET_STATUS, \
3350 RECONFIGURE, RXQ_RECV) \
3353 true, /* is_pmd */ \
3355 NULL, /* netdev_dpdk_run */ \
3356 NULL, /* netdev_dpdk_wait */ \
3358 netdev_dpdk_alloc, \
3361 netdev_dpdk_dealloc, \
3362 netdev_dpdk_get_config, \
3364 NULL, /* get_tunnel_config */ \
3365 NULL, /* build header */ \
3366 NULL, /* push header */ \
3367 NULL, /* pop header */ \
3368 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3372 NULL, /* send_wait */ \
3374 netdev_dpdk_set_etheraddr, \
3375 netdev_dpdk_get_etheraddr, \
3376 netdev_dpdk_get_mtu, \
3377 netdev_dpdk_set_mtu, \
3378 netdev_dpdk_get_ifindex, \
3380 netdev_dpdk_get_carrier_resets, \
3381 netdev_dpdk_set_miimon, \
3384 NULL, /* set_advertisements */ \
3385 NULL, /* get_pt_mode */ \
3387 netdev_dpdk_set_policing, \
3388 netdev_dpdk_get_qos_types, \
3389 NULL, /* get_qos_capabilities */ \
3390 netdev_dpdk_get_qos, \
3391 netdev_dpdk_set_qos, \
3392 NULL, /* get_queue */ \
3393 NULL, /* set_queue */ \
3394 NULL, /* delete_queue */ \
3395 NULL, /* get_queue_stats */ \
3396 NULL, /* queue_dump_start */ \
3397 NULL, /* queue_dump_next */ \
3398 NULL, /* queue_dump_done */ \
3399 NULL, /* dump_queue_stats */ \
3401 NULL, /* set_in4 */ \
3402 NULL, /* get_addr_list */ \
3403 NULL, /* add_router */ \
3404 NULL, /* get_next_hop */ \
3406 NULL, /* arp_lookup */ \
3408 netdev_dpdk_update_flags, \
3411 netdev_dpdk_rxq_alloc, \
3412 netdev_dpdk_rxq_construct, \
3413 netdev_dpdk_rxq_destruct, \
3414 netdev_dpdk_rxq_dealloc, \
3416 NULL, /* rx_wait */ \
3417 NULL, /* rxq_drain */ \
3421 static const struct netdev_class dpdk_class
=
3424 netdev_dpdk_class_init
,
3425 netdev_dpdk_construct
,
3426 netdev_dpdk_destruct
,
3427 netdev_dpdk_set_config
,
3428 netdev_dpdk_set_tx_multiq
,
3429 netdev_dpdk_eth_send
,
3430 netdev_dpdk_get_carrier
,
3431 netdev_dpdk_get_stats
,
3432 netdev_dpdk_get_features
,
3433 netdev_dpdk_get_status
,
3434 netdev_dpdk_reconfigure
,
3435 netdev_dpdk_rxq_recv
);
3437 static const struct netdev_class dpdk_ring_class
=
3440 netdev_dpdk_class_init
,
3441 netdev_dpdk_ring_construct
,
3442 netdev_dpdk_destruct
,
3443 netdev_dpdk_ring_set_config
,
3444 netdev_dpdk_set_tx_multiq
,
3445 netdev_dpdk_ring_send
,
3446 netdev_dpdk_get_carrier
,
3447 netdev_dpdk_get_stats
,
3448 netdev_dpdk_get_features
,
3449 netdev_dpdk_get_status
,
3450 netdev_dpdk_reconfigure
,
3451 netdev_dpdk_rxq_recv
);
3453 static const struct netdev_class dpdk_vhost_class
=
3457 netdev_dpdk_vhost_construct
,
3458 netdev_dpdk_vhost_destruct
,
3461 netdev_dpdk_vhost_send
,
3462 netdev_dpdk_vhost_get_carrier
,
3463 netdev_dpdk_vhost_get_stats
,
3466 netdev_dpdk_vhost_reconfigure
,
3467 netdev_dpdk_vhost_rxq_recv
);
3468 static const struct netdev_class dpdk_vhost_client_class
=
3470 "dpdkvhostuserclient",
3472 netdev_dpdk_vhost_client_construct
,
3473 netdev_dpdk_vhost_destruct
,
3474 netdev_dpdk_vhost_client_set_config
,
3476 netdev_dpdk_vhost_send
,
3477 netdev_dpdk_vhost_get_carrier
,
3478 netdev_dpdk_vhost_get_stats
,
3481 netdev_dpdk_vhost_client_reconfigure
,
3482 netdev_dpdk_vhost_rxq_recv
);
3485 netdev_dpdk_register(void)
3487 netdev_register_provider(&dpdk_class
);
3488 netdev_register_provider(&dpdk_ring_class
);
3489 netdev_register_provider(&dpdk_vhost_class
);
3490 netdev_register_provider(&dpdk_vhost_client_class
);