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_config.h>
30 #include <rte_cycles.h>
31 #include <rte_errno.h>
32 #include <rte_eth_ring.h>
33 #include <rte_ethdev.h>
34 #include <rte_malloc.h>
36 #include <rte_meter.h>
38 #include <rte_vhost.h>
41 #include "dp-packet.h"
43 #include "dpif-netdev.h"
44 #include "fatal-signal.h"
45 #include "netdev-provider.h"
46 #include "netdev-vport.h"
48 #include "openvswitch/dynamic-string.h"
49 #include "openvswitch/list.h"
50 #include "openvswitch/ofp-print.h"
51 #include "openvswitch/vlog.h"
53 #include "ovs-thread.h"
56 #include "openvswitch/shash.h"
59 #include "unaligned.h"
63 enum {VIRTIO_RXQ
, VIRTIO_TXQ
, VIRTIO_QNUM
};
65 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
66 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
68 #define DPDK_PORT_WATCHDOG_INTERVAL 5
70 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
71 #define OVS_VPORT_DPDK "ovs_dpdk"
74 * need to reserve tons of extra space in the mbufs so we can align the
75 * DMA addresses to 4KB.
76 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
77 * performance for standard Ethernet MTU.
79 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
80 + (2 * VLAN_HEADER_LEN))
81 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
82 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
83 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
84 - ETHER_HDR_LEN - ETHER_CRC_LEN)
85 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
86 + sizeof(struct dp_packet) \
87 + RTE_PKTMBUF_HEADROOM), \
89 #define NETDEV_DPDK_MBUF_ALIGN 1024
90 #define NETDEV_DPDK_MAX_PKT_LEN 9728
92 /* Max and min number of packets in the mempool. OVS tries to allocate a
93 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
94 * enough hugepages) we keep halving the number until the allocation succeeds
95 * or we reach MIN_NB_MBUF */
97 #define MAX_NB_MBUF (4096 * 64)
98 #define MIN_NB_MBUF (4096 * 4)
99 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
101 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
102 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
) == 0);
104 /* The smallest possible NB_MBUF that we're going to try should be a multiple
105 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
106 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
))
110 * DPDK XSTATS Counter names definition
112 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
113 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
114 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
115 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
116 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
117 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
118 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
120 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
121 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
122 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
123 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
124 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
125 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
126 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
128 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
129 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
130 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
131 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
132 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
133 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
134 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
135 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
139 /* Default size of Physical NIC RXQ */
140 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
141 /* Default size of Physical NIC TXQ */
142 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
143 /* Maximum size of Physical NIC Queues */
144 #define NIC_PORT_MAX_Q_SIZE 4096
146 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
147 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
148 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
149 * yet mapped to another queue. */
151 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
153 /* DPDK library uses uint8_t for port_id. */
154 typedef uint8_t dpdk_port_t
;
156 #define VHOST_ENQ_RETRY_NUM 8
157 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
159 static const struct rte_eth_conf port_conf
= {
161 .mq_mode
= ETH_MQ_RX_RSS
,
163 .header_split
= 0, /* Header Split disabled */
164 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
165 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
166 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
172 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
176 .mq_mode
= ETH_MQ_TX_NONE
,
181 * These callbacks allow virtio-net devices to be added to vhost ports when
182 * configuration has been fully completed.
184 static int new_device(int vid
);
185 static void destroy_device(int vid
);
186 static int vring_state_changed(int vid
, uint16_t queue_id
, int enable
);
187 static const struct vhost_device_ops virtio_net_device_ops
=
189 .new_device
= new_device
,
190 .destroy_device
= destroy_device
,
191 .vring_state_changed
= vring_state_changed
,
192 .features_changed
= NULL
195 enum { DPDK_RING_SIZE
= 256 };
196 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
197 enum { DRAIN_TSC
= 200000ULL };
204 /* Quality of Service */
206 /* An instance of a QoS configuration. Always associated with a particular
209 * Each QoS implementation subclasses this with whatever additional data it
213 const struct dpdk_qos_ops
*ops
;
217 /* A particular implementation of dpdk QoS operations.
219 * The functions below return 0 if successful or a positive errno value on
220 * failure, except where otherwise noted. All of them must be provided, except
221 * where otherwise noted.
223 struct dpdk_qos_ops
{
225 /* Name of the QoS type */
226 const char *qos_name
;
228 /* Called to construct a qos_conf object. The implementation should make
229 * the appropriate calls to configure QoS according to 'details'.
231 * The contents of 'details' should be documented as valid for 'ovs_name'
232 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
233 * (which is built as ovs-vswitchd.conf.db(8)).
235 * This function must return 0 if and only if it sets '*conf' to an
236 * initialized 'struct qos_conf'.
238 * For all QoS implementations it should always be non-null.
240 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
242 /* Destroys the data structures allocated by the implementation as part of
245 * For all QoS implementations it should always be non-null.
247 void (*qos_destruct
)(struct qos_conf
*conf
);
249 /* Retrieves details of 'conf' configuration into '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 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
257 /* Returns true if 'conf' is already configured according to 'details'.
259 * The contents of 'details' should be documented as valid for 'ovs_name'
260 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
261 * (which is built as ovs-vswitchd.conf.db(8)).
263 * For all QoS implementations it should always be non-null.
265 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
266 const struct smap
*details
);
268 /* Modify an array of rte_mbufs. The modification is specific to
269 * each qos implementation.
271 * The function should take and array of mbufs and an int representing
272 * the current number of mbufs present in the array.
274 * After the function has performed a qos modification to the array of
275 * mbufs it returns an int representing the number of mbufs now present in
276 * the array. This value is can then be passed to the port send function
277 * along with the modified array for transmission.
279 * For all QoS implementations it should always be non-null.
281 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
282 int pkt_cnt
, bool may_steal
);
285 /* dpdk_qos_ops for each type of user space QoS implementation */
286 static const struct dpdk_qos_ops egress_policer_ops
;
289 * Array of dpdk_qos_ops, contains pointer to all supported QoS
292 static const struct dpdk_qos_ops
*const qos_confs
[] = {
297 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
299 /* Contains all 'struct dpdk_dev's. */
300 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
301 = OVS_LIST_INITIALIZER(&dpdk_list
);
303 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
304 = OVS_MUTEX_INITIALIZER
;
306 /* There should be one 'struct dpdk_tx_queue' created for
308 struct dpdk_tx_queue
{
309 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
310 * from concurrent access. It is used only
311 * if the queue is shared among different
312 * pmd threads (see 'concurrent_txq'). */
313 int map
; /* Mapping of configured vhost-user queues
314 * to enabled by guest. */
317 /* dpdk has no way to remove dpdk ring ethernet devices
318 so we have to keep them around once they've been created
321 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
322 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
325 /* For the client rings */
326 struct rte_ring
*cring_tx
;
327 struct rte_ring
*cring_rx
;
328 unsigned int user_port_id
; /* User given port no, parsed from port name */
329 dpdk_port_t eth_port_id
; /* ethernet device port id */
330 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
333 struct ingress_policer
{
334 struct rte_meter_srtcm_params app_srtcm_params
;
335 struct rte_meter_srtcm in_policer
;
336 rte_spinlock_t policer_lock
;
339 enum dpdk_hw_ol_features
{
340 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
344 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline0
,
347 /* If true, device was attached by rte_eth_dev_attach(). */
349 struct eth_addr hwaddr
;
354 enum dpdk_dev_type type
;
355 enum netdev_flags flags
;
356 char *devargs
; /* Device arguments for dpdk ports */
357 struct dpdk_tx_queue
*tx_q
;
358 struct rte_eth_link link
;
360 /* 4 pad bytes here. */
363 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline1
,
364 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
365 struct rte_mempool
*mp
;
367 /* virtio identifier for vhost devices */
370 /* True if vHost device is 'up' and has been reconfigured at least once */
371 bool vhost_reconfigured
;
372 /* 3 pad bytes here. */
375 PADDED_MEMBERS(CACHE_LINE_SIZE
,
376 /* Identifier used to distinguish vhost devices from each other. */
377 char vhost_id
[PATH_MAX
];
380 PADDED_MEMBERS(CACHE_LINE_SIZE
,
383 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
385 /* QoS configuration and lock for the device */
386 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
388 /* Ingress Policer */
389 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
390 uint32_t policer_rate
;
391 uint32_t policer_burst
;
394 PADDED_MEMBERS(CACHE_LINE_SIZE
,
395 struct netdev_stats stats
;
397 rte_spinlock_t stats_lock
;
398 /* 44 pad bytes here. */
401 PADDED_MEMBERS(CACHE_LINE_SIZE
,
402 /* The following properties cannot be changed when a device is running,
403 * so we remember the request and update them next time
404 * netdev_dpdk*_reconfigure() is called */
408 int requested_rxq_size
;
409 int requested_txq_size
;
411 /* Number of rx/tx descriptors for physical devices */
415 /* Socket ID detected when vHost device is brought up */
416 int requested_socket_id
;
418 /* Denotes whether vHost port is client/server mode */
419 uint64_t vhost_driver_flags
;
421 /* DPDK-ETH Flow control */
422 struct rte_eth_fc_conf fc_conf
;
424 /* DPDK-ETH hardware offload features,
425 * from the enum set 'dpdk_hw_ol_features' */
426 uint32_t hw_ol_features
;
430 struct netdev_rxq_dpdk
{
431 struct netdev_rxq up
;
435 static void netdev_dpdk_destruct(struct netdev
*netdev
);
436 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
438 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
440 struct ingress_policer
*
441 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
444 is_dpdk_class(const struct netdev_class
*class)
446 return class->destruct
== netdev_dpdk_destruct
447 || class->destruct
== netdev_dpdk_vhost_destruct
;
450 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
451 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
452 * value, insufficient buffers are allocated to accomodate the packet in its
453 * entirety. Furthermore, certain drivers need to ensure that there is also
454 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
455 * frames). If the RX buffer is too small, then the driver enables scatter RX
456 * behaviour, which reduces performance. To prevent this, use a buffer size
457 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
460 dpdk_buf_size(int mtu
)
462 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
463 NETDEV_DPDK_MBUF_ALIGN
);
466 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
468 * Unlike xmalloc(), this function can return NULL on failure. */
470 dpdk_rte_mzalloc(size_t sz
)
472 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
476 free_dpdk_buf(struct dp_packet
*p
)
478 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
480 rte_pktmbuf_free(pkt
);
484 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
485 void *opaque_arg OVS_UNUSED
,
487 unsigned i OVS_UNUSED
)
489 struct rte_mbuf
*pkt
= _p
;
491 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
494 /* Returns a valid pointer when either of the following is true:
495 * - a new mempool was just created;
496 * - a matching mempool already exists. */
497 static struct rte_mempool
*
498 dpdk_mp_create(struct netdev_dpdk
*dev
, int mtu
)
500 char mp_name
[RTE_MEMPOOL_NAMESIZE
];
501 const char *netdev_name
= netdev_get_name(&dev
->up
);
502 int socket_id
= dev
->requested_socket_id
;
504 uint32_t hash
= hash_string(netdev_name
, 0);
505 struct rte_mempool
*mp
= NULL
;
508 * XXX: rough estimation of number of mbufs required for this port:
509 * <packets required to fill the device rxqs>
510 * + <packets that could be stuck on other ports txqs>
511 * + <packets in the pmd threads>
512 * + <additional memory for corner cases>
514 n_mbufs
= dev
->requested_n_rxq
* dev
->requested_rxq_size
515 + dev
->requested_n_txq
* dev
->requested_txq_size
516 + MIN(RTE_MAX_LCORE
, dev
->requested_n_rxq
) * NETDEV_MAX_BURST
519 ovs_mutex_lock(&dpdk_mp_mutex
);
521 /* Full DPDK memory pool name must be unique and cannot be
522 * longer than RTE_MEMPOOL_NAMESIZE. */
523 int ret
= snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
, "ovs_%x_%d_%d_%u",
524 hash
, socket_id
, mtu
, n_mbufs
);
525 if (ret
< 0 || ret
>= RTE_MEMPOOL_NAMESIZE
) {
526 VLOG_DBG("snprintf returned %d. "
527 "Failed to generate a mempool name for \"%s\". "
528 "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.",
529 ret
, netdev_name
, hash
, socket_id
, mtu
, n_mbufs
);
533 VLOG_DBG("Port %s: Requesting a mempool of %u mbufs "
534 "on socket %d for %d Rx and %d Tx queues.",
535 netdev_name
, n_mbufs
, socket_id
,
536 dev
->requested_n_rxq
, dev
->requested_n_txq
);
538 mp
= rte_pktmbuf_pool_create(mp_name
, n_mbufs
, MP_CACHE_SZ
,
539 sizeof (struct dp_packet
) - sizeof (struct rte_mbuf
),
540 MBUF_SIZE(mtu
) - sizeof(struct dp_packet
), socket_id
);
543 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
545 /* rte_pktmbuf_pool_create has done some initialization of the
546 * rte_mbuf part of each dp_packet. Some OvS specific fields
547 * of the packet still need to be initialized by
548 * ovs_rte_pktmbuf_init. */
549 rte_mempool_obj_iter(mp
, ovs_rte_pktmbuf_init
, NULL
);
550 } else if (rte_errno
== EEXIST
) {
551 /* A mempool with the same name already exists. We just
552 * retrieve its pointer to be returned to the caller. */
553 mp
= rte_mempool_lookup(mp_name
);
554 /* As the mempool create returned EEXIST we can expect the
555 * lookup has returned a valid pointer. If for some reason
556 * that's not the case we keep track of it. */
557 VLOG_DBG("A mempool with name \"%s\" already exists at %p.",
560 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
563 } while (!mp
&& rte_errno
== ENOMEM
&& (n_mbufs
/= 2) >= MIN_NB_MBUF
);
565 ovs_mutex_unlock(&dpdk_mp_mutex
);
569 /* Release an existing mempool. */
571 dpdk_mp_free(struct rte_mempool
*mp
)
577 ovs_mutex_lock(&dpdk_mp_mutex
);
578 VLOG_DBG("Releasing \"%s\" mempool", mp
->name
);
579 rte_mempool_free(mp
);
580 ovs_mutex_unlock(&dpdk_mp_mutex
);
583 /* Tries to allocate a new mempool - or re-use an existing one where
584 * appropriate - on requested_socket_id with a size determined by
585 * requested_mtu and requested Rx/Tx queues.
586 * On success - or when re-using an existing mempool - the new configuration
588 * On error, device will be left unchanged. */
590 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
591 OVS_REQUIRES(dev
->mutex
)
593 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
594 struct rte_mempool
*mp
;
597 mp
= dpdk_mp_create(dev
, FRAME_LEN_TO_MTU(buf_size
));
599 VLOG_ERR("Failed to create memory pool for netdev "
600 "%s, with MTU %d on socket %d: %s\n",
601 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
602 rte_strerror(rte_errno
));
605 /* If a new MTU was requested and its rounded value equals the one
606 * that is currently used, then the existing mempool is returned. */
608 /* A new mempool was created, release the previous one. */
609 dpdk_mp_free(dev
->mp
);
614 dev
->mtu
= dev
->requested_mtu
;
615 dev
->socket_id
= dev
->requested_socket_id
;
616 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
623 check_link_status(struct netdev_dpdk
*dev
)
625 struct rte_eth_link link
;
627 rte_eth_link_get_nowait(dev
->port_id
, &link
);
629 if (dev
->link
.link_status
!= link
.link_status
) {
630 netdev_change_seq_changed(&dev
->up
);
632 dev
->link_reset_cnt
++;
634 if (dev
->link
.link_status
) {
635 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
636 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
637 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
638 ("full-duplex") : ("half-duplex"));
640 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
646 dpdk_watchdog(void *dummy OVS_UNUSED
)
648 struct netdev_dpdk
*dev
;
650 pthread_detach(pthread_self());
653 ovs_mutex_lock(&dpdk_mutex
);
654 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
655 ovs_mutex_lock(&dev
->mutex
);
656 if (dev
->type
== DPDK_DEV_ETH
) {
657 check_link_status(dev
);
659 ovs_mutex_unlock(&dev
->mutex
);
661 ovs_mutex_unlock(&dpdk_mutex
);
662 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
669 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
673 struct rte_eth_conf conf
= port_conf
;
675 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
677 if (dev
->mtu
> ETHER_MTU
) {
678 conf
.rxmode
.enable_scatter
= 1;
681 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
682 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
683 /* A device may report more queues than it makes available (this has
684 * been observed for Intel xl710, which reserves some of them for
685 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
686 * available. When this happens we can retry the configuration
687 * and request less queues */
688 while (n_rxq
&& n_txq
) {
690 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
693 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
695 VLOG_WARN("Interface %s eth_dev setup error %s\n",
696 dev
->up
.name
, rte_strerror(-diag
));
700 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
702 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
703 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
707 for (i
= 0; i
< n_txq
; i
++) {
708 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
709 dev
->socket_id
, NULL
);
711 VLOG_INFO("Interface %s txq(%d) setup error: %s",
712 dev
->up
.name
, i
, rte_strerror(-diag
));
718 /* Retry with less tx queues */
723 for (i
= 0; i
< n_rxq
; i
++) {
724 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
725 dev
->socket_id
, NULL
, dev
->mp
);
727 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
728 dev
->up
.name
, i
, rte_strerror(-diag
));
734 /* Retry with less rx queues */
739 dev
->up
.n_rxq
= n_rxq
;
740 dev
->up
.n_txq
= n_txq
;
749 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
751 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
752 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
758 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
759 OVS_REQUIRES(dev
->mutex
)
761 struct rte_pktmbuf_pool_private
*mbp_priv
;
762 struct rte_eth_dev_info info
;
763 struct ether_addr eth_addr
;
766 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
767 DEV_RX_OFFLOAD_TCP_CKSUM
|
768 DEV_RX_OFFLOAD_IPV4_CKSUM
;
770 rte_eth_dev_info_get(dev
->port_id
, &info
);
772 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
773 rx_chksm_offload_capa
) {
774 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
776 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
778 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
781 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
782 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
784 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
786 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
787 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
791 diag
= rte_eth_dev_start(dev
->port_id
);
793 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
794 rte_strerror(-diag
));
798 rte_eth_promiscuous_enable(dev
->port_id
);
799 rte_eth_allmulticast_enable(dev
->port_id
);
801 memset(ð_addr
, 0x0, sizeof(eth_addr
));
802 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
803 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
804 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
806 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
807 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
809 mbp_priv
= rte_mempool_get_priv(dev
->mp
);
810 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
812 /* Get the Flow control configuration for DPDK-ETH */
813 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
815 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
822 static struct netdev_dpdk
*
823 netdev_dpdk_cast(const struct netdev
*netdev
)
825 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
828 static struct netdev
*
829 netdev_dpdk_alloc(void)
831 struct netdev_dpdk
*dev
;
833 dev
= dpdk_rte_mzalloc(sizeof *dev
);
841 static struct dpdk_tx_queue
*
842 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
844 struct dpdk_tx_queue
*txqs
;
847 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
849 for (i
= 0; i
< n_txqs
; i
++) {
850 /* Initialize map for vhost devices. */
851 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
852 rte_spinlock_init(&txqs
[i
].tx_lock
);
860 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
861 enum dpdk_dev_type type
, int socket_id
)
862 OVS_REQUIRES(dpdk_mutex
)
864 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
866 ovs_mutex_init(&dev
->mutex
);
868 rte_spinlock_init(&dev
->stats_lock
);
870 /* If the 'sid' is negative, it means that the kernel fails
871 * to obtain the pci numa info. In that situation, always
873 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
874 dev
->requested_socket_id
= dev
->socket_id
;
875 dev
->port_id
= port_no
;
878 dev
->requested_mtu
= ETHER_MTU
;
879 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
880 ovsrcu_index_init(&dev
->vid
, -1);
881 dev
->vhost_reconfigured
= false;
882 dev
->attached
= false;
884 ovsrcu_init(&dev
->qos_conf
, NULL
);
886 ovsrcu_init(&dev
->ingress_policer
, NULL
);
887 dev
->policer_rate
= 0;
888 dev
->policer_burst
= 0;
892 dev
->requested_n_rxq
= NR_QUEUE
;
893 dev
->requested_n_txq
= NR_QUEUE
;
894 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
895 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
897 /* Initialize the flow control to NULL */
898 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
900 /* Initilize the hardware offload flags to 0 */
901 dev
->hw_ol_features
= 0;
903 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
905 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
907 netdev_request_reconfigure(netdev
);
912 /* dev_name must be the prefix followed by a positive decimal number.
913 * (no leading + or - signs are allowed) */
915 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
916 unsigned int *port_no
)
920 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
924 cport
= dev_name
+ strlen(prefix
);
926 if (str_to_uint(cport
, 10, port_no
)) {
934 vhost_common_construct(struct netdev
*netdev
)
935 OVS_REQUIRES(dpdk_mutex
)
937 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
938 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
940 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
945 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
946 DPDK_DEV_VHOST
, socket_id
);
950 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
952 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
953 const char *name
= netdev
->name
;
956 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
957 * the file system. '/' or '\' would traverse directories, so they're not
958 * acceptable in 'name'. */
959 if (strchr(name
, '/') || strchr(name
, '\\')) {
960 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
961 "A valid name must not include '/' or '\\'",
966 ovs_mutex_lock(&dpdk_mutex
);
967 /* Take the name of the vhost-user port and append it to the location where
968 * the socket is to be created, then register the socket.
970 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
971 dpdk_get_vhost_sock_dir(), name
);
973 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
974 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
976 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
980 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
981 VLOG_INFO("Socket %s created for vhost-user port %s\n",
982 dev
->vhost_id
, name
);
985 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
986 &virtio_net_device_ops
);
988 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
993 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
994 1ULL << VIRTIO_NET_F_HOST_TSO4
995 | 1ULL << VIRTIO_NET_F_HOST_TSO6
996 | 1ULL << VIRTIO_NET_F_CSUM
);
998 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1003 err
= rte_vhost_driver_start(dev
->vhost_id
);
1005 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1006 "port: %s\n", name
);
1010 err
= vhost_common_construct(netdev
);
1012 VLOG_ERR("vhost_common_construct failed for vhost user "
1013 "port: %s\n", name
);
1017 ovs_mutex_unlock(&dpdk_mutex
);
1018 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1019 "please migrate to dpdkvhostuserclient ports.");
1024 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1028 ovs_mutex_lock(&dpdk_mutex
);
1029 err
= vhost_common_construct(netdev
);
1031 VLOG_ERR("vhost_common_construct failed for vhost user client"
1032 "port: %s\n", netdev
->name
);
1034 ovs_mutex_unlock(&dpdk_mutex
);
1039 netdev_dpdk_construct(struct netdev
*netdev
)
1043 ovs_mutex_lock(&dpdk_mutex
);
1044 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1045 DPDK_DEV_ETH
, SOCKET0
);
1046 ovs_mutex_unlock(&dpdk_mutex
);
1051 common_destruct(struct netdev_dpdk
*dev
)
1052 OVS_REQUIRES(dpdk_mutex
)
1053 OVS_EXCLUDED(dev
->mutex
)
1055 rte_free(dev
->tx_q
);
1056 dpdk_mp_free(dev
->mp
);
1058 ovs_list_remove(&dev
->list_node
);
1059 free(ovsrcu_get_protected(struct ingress_policer
*,
1060 &dev
->ingress_policer
));
1061 ovs_mutex_destroy(&dev
->mutex
);
1065 netdev_dpdk_destruct(struct netdev
*netdev
)
1067 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1068 char devname
[RTE_ETH_NAME_MAX_LEN
];
1070 ovs_mutex_lock(&dpdk_mutex
);
1072 rte_eth_dev_stop(dev
->port_id
);
1074 if (dev
->attached
) {
1075 rte_eth_dev_close(dev
->port_id
);
1076 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1077 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1079 VLOG_INFO("Device '%s' has been detached", devname
);
1084 common_destruct(dev
);
1086 ovs_mutex_unlock(&dpdk_mutex
);
1089 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1090 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1091 * deadlock, none of the mutexes must be held while calling this function. */
1093 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1095 OVS_EXCLUDED(dpdk_mutex
)
1096 OVS_EXCLUDED(dev
->mutex
)
1098 return rte_vhost_driver_unregister(vhost_id
);
1102 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1104 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1107 ovs_mutex_lock(&dpdk_mutex
);
1109 /* Guest becomes an orphan if still attached. */
1110 if (netdev_dpdk_get_vid(dev
) >= 0
1111 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1112 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1114 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1115 "socket '%s' must be restarted.", dev
->vhost_id
);
1118 vhost_id
= xstrdup(dev
->vhost_id
);
1120 common_destruct(dev
);
1122 ovs_mutex_unlock(&dpdk_mutex
);
1128 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1129 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1130 netdev
->name
, vhost_id
);
1131 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1132 /* OVS server mode - remove this socket from list for deletion */
1133 fatal_signal_remove_file_to_unlink(vhost_id
);
1140 netdev_dpdk_dealloc(struct netdev
*netdev
)
1142 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1148 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1150 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1152 ovs_mutex_lock(&dev
->mutex
);
1154 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1155 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1156 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1157 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1158 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1160 if (dev
->type
== DPDK_DEV_ETH
) {
1161 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1162 dev
->requested_rxq_size
);
1163 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1165 smap_add_format(args
, "requested_txq_descriptors", "%d",
1166 dev
->requested_txq_size
);
1167 smap_add_format(args
, "configured_txq_descriptors", "%d",
1169 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1170 smap_add(args
, "rx_csum_offload", "true");
1172 smap_add(args
, "rx_csum_offload", "false");
1175 ovs_mutex_unlock(&dev
->mutex
);
1180 static struct netdev_dpdk
*
1181 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1182 OVS_REQUIRES(dpdk_mutex
)
1184 struct netdev_dpdk
*dev
;
1186 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1187 if (dev
->port_id
== port_id
) {
1196 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1197 const char *devargs
, char **errp
)
1199 /* Get the name up to the first comma. */
1200 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1201 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1203 if (!rte_eth_dev_count()
1204 || rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1205 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1206 /* Device not found in DPDK, attempt to attach it */
1207 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1208 /* Attach successful */
1209 dev
->attached
= true;
1210 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1212 /* Attach unsuccessful */
1213 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1214 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1224 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1225 OVS_REQUIRES(dev
->mutex
)
1229 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1230 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1231 dev
->requested_n_rxq
= new_n_rxq
;
1232 netdev_request_reconfigure(&dev
->up
);
1237 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1238 const char *flag
, int default_size
, int *new_size
)
1240 int queue_size
= smap_get_int(args
, flag
, default_size
);
1242 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1243 || !is_pow2(queue_size
)) {
1244 queue_size
= default_size
;
1247 if (queue_size
!= *new_size
) {
1248 *new_size
= queue_size
;
1249 netdev_request_reconfigure(netdev
);
1254 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1257 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1258 bool rx_fc_en
, tx_fc_en
, autoneg
;
1259 enum rte_eth_fc_mode fc_mode
;
1260 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1261 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1262 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1264 const char *new_devargs
;
1267 ovs_mutex_lock(&dpdk_mutex
);
1268 ovs_mutex_lock(&dev
->mutex
);
1270 dpdk_set_rxq_config(dev
, args
);
1272 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1273 NIC_PORT_DEFAULT_RXQ_SIZE
,
1274 &dev
->requested_rxq_size
);
1275 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1276 NIC_PORT_DEFAULT_TXQ_SIZE
,
1277 &dev
->requested_txq_size
);
1279 new_devargs
= smap_get(args
, "dpdk-devargs");
1281 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1282 /* The user requested a new device. If we return error, the caller
1283 * will delete this netdev and try to recreate it. */
1288 /* dpdk-devargs is required for device configuration */
1289 if (new_devargs
&& new_devargs
[0]) {
1290 /* Don't process dpdk-devargs if value is unchanged and port id
1292 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1293 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1294 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1297 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1299 } else if (new_port_id
== dev
->port_id
) {
1300 /* Already configured, do not reconfigure again */
1303 struct netdev_dpdk
*dup_dev
;
1305 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1307 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1308 "which is already in use by '%s'",
1309 netdev_get_name(netdev
), new_devargs
,
1310 netdev_get_name(&dup_dev
->up
));
1313 int sid
= rte_eth_dev_socket_id(new_port_id
);
1315 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1316 dev
->devargs
= xstrdup(new_devargs
);
1317 dev
->port_id
= new_port_id
;
1318 netdev_request_reconfigure(&dev
->up
);
1324 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1325 "The old 'dpdk<port_id>' names are not supported",
1326 netdev_get_name(netdev
));
1334 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1335 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1336 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1338 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1339 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1340 dev
->fc_conf
.mode
= fc_mode
;
1341 dev
->fc_conf
.autoneg
= autoneg
;
1342 dpdk_eth_flow_ctrl_setup(dev
);
1346 ovs_mutex_unlock(&dev
->mutex
);
1347 ovs_mutex_unlock(&dpdk_mutex
);
1353 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1354 char **errp OVS_UNUSED
)
1356 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1358 ovs_mutex_lock(&dev
->mutex
);
1359 dpdk_set_rxq_config(dev
, args
);
1360 ovs_mutex_unlock(&dev
->mutex
);
1366 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1367 const struct smap
*args
,
1368 char **errp OVS_UNUSED
)
1370 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1373 ovs_mutex_lock(&dev
->mutex
);
1374 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1375 path
= smap_get(args
, "vhost-server-path");
1376 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1377 strcpy(dev
->vhost_id
, path
);
1378 netdev_request_reconfigure(netdev
);
1381 ovs_mutex_unlock(&dev
->mutex
);
1387 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1389 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1391 return dev
->socket_id
;
1394 /* Sets the number of tx queues for the dpdk interface. */
1396 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1398 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1400 ovs_mutex_lock(&dev
->mutex
);
1402 if (dev
->requested_n_txq
== n_txq
) {
1406 dev
->requested_n_txq
= n_txq
;
1407 netdev_request_reconfigure(netdev
);
1410 ovs_mutex_unlock(&dev
->mutex
);
1414 static struct netdev_rxq
*
1415 netdev_dpdk_rxq_alloc(void)
1417 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1426 static struct netdev_rxq_dpdk
*
1427 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1429 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1433 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1435 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1436 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1438 ovs_mutex_lock(&dev
->mutex
);
1439 rx
->port_id
= dev
->port_id
;
1440 ovs_mutex_unlock(&dev
->mutex
);
1446 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1451 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1453 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1458 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1459 * 'pkts', even in case of failure.
1461 * Returns the number of packets that weren't transmitted. */
1463 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1464 struct rte_mbuf
**pkts
, int cnt
)
1468 while (nb_tx
!= cnt
) {
1471 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1479 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1480 /* Free buffers, which we couldn't transmit, one at a time (each
1481 * packet could come from a different mempool) */
1484 for (i
= nb_tx
; i
< cnt
; i
++) {
1485 rte_pktmbuf_free(pkts
[i
]);
1493 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1494 struct rte_mbuf
*pkt
, uint64_t time
)
1496 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1498 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1503 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1504 struct rte_mbuf
**pkts
, int pkt_cnt
,
1509 struct rte_mbuf
*pkt
= NULL
;
1510 uint64_t current_time
= rte_rdtsc();
1512 for (i
= 0; i
< pkt_cnt
; i
++) {
1514 /* Handle current packet */
1515 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1522 rte_pktmbuf_free(pkt
);
1531 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1532 int pkt_cnt
, bool may_steal
)
1536 rte_spinlock_lock(&policer
->policer_lock
);
1537 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1538 pkt_cnt
, may_steal
);
1539 rte_spinlock_unlock(&policer
->policer_lock
);
1545 is_vhost_running(struct netdev_dpdk
*dev
)
1547 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1551 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1552 unsigned int packet_size
)
1554 /* Hard-coded search for the size bucket. */
1555 if (packet_size
< 256) {
1556 if (packet_size
>= 128) {
1557 stats
->rx_128_to_255_packets
++;
1558 } else if (packet_size
<= 64) {
1559 stats
->rx_1_to_64_packets
++;
1561 stats
->rx_65_to_127_packets
++;
1564 if (packet_size
>= 1523) {
1565 stats
->rx_1523_to_max_packets
++;
1566 } else if (packet_size
>= 1024) {
1567 stats
->rx_1024_to_1522_packets
++;
1568 } else if (packet_size
< 512) {
1569 stats
->rx_256_to_511_packets
++;
1571 stats
->rx_512_to_1023_packets
++;
1577 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1578 struct dp_packet
**packets
, int count
,
1582 unsigned int packet_size
;
1583 struct dp_packet
*packet
;
1585 stats
->rx_packets
+= count
;
1586 stats
->rx_dropped
+= dropped
;
1587 for (i
= 0; i
< count
; i
++) {
1588 packet
= packets
[i
];
1589 packet_size
= dp_packet_size(packet
);
1591 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1592 /* This only protects the following multicast counting from
1593 * too short packets, but it does not stop the packet from
1594 * further processing. */
1596 stats
->rx_length_errors
++;
1600 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1602 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1603 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1607 stats
->rx_bytes
+= packet_size
;
1612 * The receive path for the vhost port is the TX path out from guest.
1615 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1616 struct dp_packet_batch
*batch
)
1618 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1619 int qid
= rxq
->queue_id
;
1620 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1622 uint16_t dropped
= 0;
1624 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1625 || !(dev
->flags
& NETDEV_UP
))) {
1629 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1630 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1632 (struct rte_mbuf
**) batch
->packets
,
1640 nb_rx
= ingress_policer_run(policer
,
1641 (struct rte_mbuf
**) batch
->packets
,
1646 rte_spinlock_lock(&dev
->stats_lock
);
1647 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1649 rte_spinlock_unlock(&dev
->stats_lock
);
1651 batch
->count
= nb_rx
;
1652 dp_packet_batch_init_packet_fields(batch
);
1658 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1660 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1661 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1662 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1666 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1670 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1671 (struct rte_mbuf
**) batch
->packets
,
1679 nb_rx
= ingress_policer_run(policer
,
1680 (struct rte_mbuf
**) batch
->packets
,
1685 /* Update stats to reflect dropped packets */
1686 if (OVS_UNLIKELY(dropped
)) {
1687 rte_spinlock_lock(&dev
->stats_lock
);
1688 dev
->stats
.rx_dropped
+= dropped
;
1689 rte_spinlock_unlock(&dev
->stats_lock
);
1692 batch
->count
= nb_rx
;
1693 dp_packet_batch_init_packet_fields(batch
);
1699 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1700 int cnt
, bool may_steal
)
1702 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1705 rte_spinlock_lock(&qos_conf
->lock
);
1706 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1707 rte_spinlock_unlock(&qos_conf
->lock
);
1714 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1719 struct rte_mbuf
*pkt
;
1721 for (i
= 0; i
< pkt_cnt
; i
++) {
1723 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1724 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1725 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1726 rte_pktmbuf_free(pkt
);
1730 if (OVS_UNLIKELY(i
!= cnt
)) {
1740 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1741 struct dp_packet
**packets
,
1746 int sent
= attempted
- dropped
;
1748 stats
->tx_packets
+= sent
;
1749 stats
->tx_dropped
+= dropped
;
1751 for (i
= 0; i
< sent
; i
++) {
1752 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1757 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1758 struct dp_packet
**pkts
, int cnt
)
1760 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1761 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1762 unsigned int total_pkts
= cnt
;
1763 unsigned int dropped
= 0;
1766 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1768 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1769 || !(dev
->flags
& NETDEV_UP
))) {
1770 rte_spinlock_lock(&dev
->stats_lock
);
1771 dev
->stats
.tx_dropped
+= cnt
;
1772 rte_spinlock_unlock(&dev
->stats_lock
);
1776 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1778 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1779 /* Check has QoS has been configured for the netdev */
1780 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
1781 dropped
= total_pkts
- cnt
;
1784 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1785 unsigned int tx_pkts
;
1787 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1788 vhost_qid
, cur_pkts
, cnt
);
1789 if (OVS_LIKELY(tx_pkts
)) {
1790 /* Packets have been sent.*/
1792 /* Prepare for possible retry.*/
1793 cur_pkts
= &cur_pkts
[tx_pkts
];
1795 /* No packets sent - do not retry.*/
1798 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1800 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1802 rte_spinlock_lock(&dev
->stats_lock
);
1803 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1805 rte_spinlock_unlock(&dev
->stats_lock
);
1808 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1809 dp_packet_delete(pkts
[i
]);
1813 /* Tx function. Transmit packets indefinitely */
1815 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1816 OVS_NO_THREAD_SAFETY_ANALYSIS
1818 const size_t batch_cnt
= dp_packet_batch_size(batch
);
1819 #if !defined(__CHECKER__) && !defined(_WIN32)
1820 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
1822 /* Sparse or MSVC doesn't like variable length array. */
1823 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1825 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1826 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1827 uint32_t cnt
= batch_cnt
;
1828 uint32_t dropped
= 0;
1830 if (dev
->type
!= DPDK_DEV_VHOST
) {
1831 /* Check if QoS has been configured for this netdev. */
1832 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
1834 dropped
+= batch_cnt
- cnt
;
1837 dp_packet_batch_apply_cutlen(batch
);
1841 for (uint32_t i
= 0; i
< cnt
; i
++) {
1842 struct dp_packet
*packet
= batch
->packets
[i
];
1843 uint32_t size
= dp_packet_size(packet
);
1845 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1846 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
1847 size
, dev
->max_packet_len
);
1853 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->mp
);
1854 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
1859 /* We have to do a copy for now */
1860 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
1861 dp_packet_data(packet
), size
);
1862 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
1867 if (OVS_LIKELY(txcnt
)) {
1868 if (dev
->type
== DPDK_DEV_VHOST
) {
1869 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1872 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
1876 if (OVS_UNLIKELY(dropped
)) {
1877 rte_spinlock_lock(&dev
->stats_lock
);
1878 dev
->stats
.tx_dropped
+= dropped
;
1879 rte_spinlock_unlock(&dev
->stats_lock
);
1884 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1885 struct dp_packet_batch
*batch
,
1886 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1889 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1890 dpdk_do_tx_copy(netdev
, qid
, batch
);
1891 dp_packet_delete_batch(batch
, may_steal
);
1893 dp_packet_batch_apply_cutlen(batch
);
1894 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1900 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1901 struct dp_packet_batch
*batch
, bool may_steal
,
1902 bool concurrent_txq
)
1904 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1905 dp_packet_delete_batch(batch
, may_steal
);
1909 if (OVS_UNLIKELY(concurrent_txq
)) {
1910 qid
= qid
% dev
->up
.n_txq
;
1911 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1914 if (OVS_UNLIKELY(!may_steal
||
1915 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1916 struct netdev
*netdev
= &dev
->up
;
1918 dpdk_do_tx_copy(netdev
, qid
, batch
);
1919 dp_packet_delete_batch(batch
, may_steal
);
1921 int tx_cnt
, dropped
;
1922 int batch_cnt
= dp_packet_batch_size(batch
);
1923 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1925 dp_packet_batch_apply_cutlen(batch
);
1927 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
1928 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
1929 dropped
= batch_cnt
- tx_cnt
;
1931 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
1933 if (OVS_UNLIKELY(dropped
)) {
1934 rte_spinlock_lock(&dev
->stats_lock
);
1935 dev
->stats
.tx_dropped
+= dropped
;
1936 rte_spinlock_unlock(&dev
->stats_lock
);
1940 if (OVS_UNLIKELY(concurrent_txq
)) {
1941 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1946 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1947 struct dp_packet_batch
*batch
, bool may_steal
,
1948 bool concurrent_txq
)
1950 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1952 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1957 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1959 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1961 ovs_mutex_lock(&dev
->mutex
);
1962 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1964 netdev_change_seq_changed(netdev
);
1966 ovs_mutex_unlock(&dev
->mutex
);
1972 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1974 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1976 ovs_mutex_lock(&dev
->mutex
);
1978 ovs_mutex_unlock(&dev
->mutex
);
1984 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1986 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1988 ovs_mutex_lock(&dev
->mutex
);
1990 ovs_mutex_unlock(&dev
->mutex
);
1996 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
1998 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2000 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2001 || mtu
< ETHER_MIN_MTU
) {
2002 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2006 ovs_mutex_lock(&dev
->mutex
);
2007 if (dev
->requested_mtu
!= mtu
) {
2008 dev
->requested_mtu
= mtu
;
2009 netdev_request_reconfigure(netdev
);
2011 ovs_mutex_unlock(&dev
->mutex
);
2017 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2020 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2021 struct netdev_stats
*stats
)
2023 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2025 ovs_mutex_lock(&dev
->mutex
);
2027 rte_spinlock_lock(&dev
->stats_lock
);
2028 /* Supported Stats */
2029 stats
->rx_packets
= dev
->stats
.rx_packets
;
2030 stats
->tx_packets
= dev
->stats
.tx_packets
;
2031 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2032 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2033 stats
->multicast
= dev
->stats
.multicast
;
2034 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2035 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2036 stats
->rx_errors
= dev
->stats
.rx_errors
;
2037 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2039 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2040 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2041 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2042 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2043 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2044 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2045 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2047 rte_spinlock_unlock(&dev
->stats_lock
);
2049 ovs_mutex_unlock(&dev
->mutex
);
2055 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2056 const struct rte_eth_xstat
*xstats
,
2057 const struct rte_eth_xstat_name
*names
,
2058 const unsigned int size
)
2060 for (unsigned int i
= 0; i
< size
; i
++) {
2061 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2062 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2063 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2064 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2065 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2066 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2067 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2068 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2069 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2070 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2071 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2072 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2073 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2074 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2075 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2076 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2077 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2078 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2079 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2080 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2081 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2082 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2083 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2084 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2085 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2086 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2087 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2088 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2089 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2090 stats
->multicast
= xstats
[i
].value
;
2091 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2092 stats
->tx_multicast_packets
= xstats
[i
].value
;
2093 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2094 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2095 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2096 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2097 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2098 stats
->rx_undersized_errors
= xstats
[i
].value
;
2099 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2100 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2101 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2102 stats
->rx_jabber_errors
= xstats
[i
].value
;
2108 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2110 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2111 struct rte_eth_stats rte_stats
;
2114 netdev_dpdk_get_carrier(netdev
, &gg
);
2115 ovs_mutex_lock(&dev
->mutex
);
2117 struct rte_eth_xstat
*rte_xstats
= NULL
;
2118 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2119 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2121 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2122 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2123 ovs_mutex_unlock(&dev
->mutex
);
2127 /* Get length of statistics */
2128 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2129 if (rte_xstats_len
< 0) {
2130 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2133 /* Reserve memory for xstats names and values */
2134 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2135 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2137 /* Retreive xstats names */
2138 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2141 if (rte_xstats_new_len
!= rte_xstats_len
) {
2142 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2145 /* Retreive xstats values */
2146 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2147 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2149 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2150 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2153 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2158 free(rte_xstats_names
);
2160 stats
->rx_packets
= rte_stats
.ipackets
;
2161 stats
->tx_packets
= rte_stats
.opackets
;
2162 stats
->rx_bytes
= rte_stats
.ibytes
;
2163 stats
->tx_bytes
= rte_stats
.obytes
;
2164 stats
->rx_errors
= rte_stats
.ierrors
;
2165 stats
->tx_errors
= rte_stats
.oerrors
;
2167 rte_spinlock_lock(&dev
->stats_lock
);
2168 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2169 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2170 rte_spinlock_unlock(&dev
->stats_lock
);
2172 /* These are the available DPDK counters for packets not received due to
2173 * local resource constraints in DPDK and NIC respectively. */
2174 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2175 stats
->rx_missed_errors
= rte_stats
.imissed
;
2177 ovs_mutex_unlock(&dev
->mutex
);
2183 netdev_dpdk_get_features(const struct netdev
*netdev
,
2184 enum netdev_features
*current
,
2185 enum netdev_features
*advertised
,
2186 enum netdev_features
*supported
,
2187 enum netdev_features
*peer
)
2189 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2190 struct rte_eth_link link
;
2192 ovs_mutex_lock(&dev
->mutex
);
2194 ovs_mutex_unlock(&dev
->mutex
);
2196 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2197 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2198 *current
= NETDEV_F_10MB_HD
;
2200 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2201 *current
= NETDEV_F_100MB_HD
;
2203 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2204 *current
= NETDEV_F_1GB_HD
;
2206 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2207 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2208 *current
= NETDEV_F_10MB_FD
;
2210 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2211 *current
= NETDEV_F_100MB_FD
;
2213 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2214 *current
= NETDEV_F_1GB_FD
;
2216 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2217 *current
= NETDEV_F_10GB_FD
;
2221 if (link
.link_autoneg
) {
2222 *current
|= NETDEV_F_AUTONEG
;
2225 *advertised
= *supported
= *peer
= 0;
2230 static struct ingress_policer
*
2231 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2233 struct ingress_policer
*policer
= NULL
;
2234 uint64_t rate_bytes
;
2235 uint64_t burst_bytes
;
2238 policer
= xmalloc(sizeof *policer
);
2239 rte_spinlock_init(&policer
->policer_lock
);
2241 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2242 rate_bytes
= rate
* 1000ULL / 8;
2243 burst_bytes
= burst
* 1000ULL / 8;
2245 policer
->app_srtcm_params
.cir
= rate_bytes
;
2246 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2247 policer
->app_srtcm_params
.ebs
= 0;
2248 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2249 &policer
->app_srtcm_params
);
2251 VLOG_ERR("Could not create rte meter for ingress policer");
2259 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2260 uint32_t policer_burst
)
2262 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2263 struct ingress_policer
*policer
;
2265 /* Force to 0 if no rate specified,
2266 * default to 8000 kbits if burst is 0,
2267 * else stick with user-specified value.
2269 policer_burst
= (!policer_rate
? 0
2270 : !policer_burst
? 8000
2273 ovs_mutex_lock(&dev
->mutex
);
2275 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2276 &dev
->ingress_policer
);
2278 if (dev
->policer_rate
== policer_rate
&&
2279 dev
->policer_burst
== policer_burst
) {
2280 /* Assume that settings haven't changed since we last set them. */
2281 ovs_mutex_unlock(&dev
->mutex
);
2285 /* Destroy any existing ingress policer for the device if one exists */
2287 ovsrcu_postpone(free
, policer
);
2290 if (policer_rate
!= 0) {
2291 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2295 ovsrcu_set(&dev
->ingress_policer
, policer
);
2296 dev
->policer_rate
= policer_rate
;
2297 dev
->policer_burst
= policer_burst
;
2298 ovs_mutex_unlock(&dev
->mutex
);
2304 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2306 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2308 ovs_mutex_lock(&dev
->mutex
);
2309 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2310 * postive integer to meet RFC 2863 recommendations.
2312 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2313 ovs_mutex_unlock(&dev
->mutex
);
2319 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2321 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2323 ovs_mutex_lock(&dev
->mutex
);
2324 check_link_status(dev
);
2325 *carrier
= dev
->link
.link_status
;
2327 ovs_mutex_unlock(&dev
->mutex
);
2333 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2335 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2337 ovs_mutex_lock(&dev
->mutex
);
2339 if (is_vhost_running(dev
)) {
2345 ovs_mutex_unlock(&dev
->mutex
);
2350 static long long int
2351 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2353 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2354 long long int carrier_resets
;
2356 ovs_mutex_lock(&dev
->mutex
);
2357 carrier_resets
= dev
->link_reset_cnt
;
2358 ovs_mutex_unlock(&dev
->mutex
);
2360 return carrier_resets
;
2364 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2365 long long int interval OVS_UNUSED
)
2371 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2372 enum netdev_flags off
, enum netdev_flags on
,
2373 enum netdev_flags
*old_flagsp
)
2374 OVS_REQUIRES(dev
->mutex
)
2376 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2380 *old_flagsp
= dev
->flags
;
2384 if (dev
->flags
== *old_flagsp
) {
2388 if (dev
->type
== DPDK_DEV_ETH
) {
2389 if (dev
->flags
& NETDEV_PROMISC
) {
2390 rte_eth_promiscuous_enable(dev
->port_id
);
2393 netdev_change_seq_changed(&dev
->up
);
2395 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2396 * running then change netdev's change_seq to trigger link state
2399 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2400 && is_vhost_running(dev
)) {
2401 netdev_change_seq_changed(&dev
->up
);
2403 /* Clear statistics if device is getting up. */
2404 if (NETDEV_UP
& on
) {
2405 rte_spinlock_lock(&dev
->stats_lock
);
2406 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2407 rte_spinlock_unlock(&dev
->stats_lock
);
2416 netdev_dpdk_update_flags(struct netdev
*netdev
,
2417 enum netdev_flags off
, enum netdev_flags on
,
2418 enum netdev_flags
*old_flagsp
)
2420 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2423 ovs_mutex_lock(&dev
->mutex
);
2424 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2425 ovs_mutex_unlock(&dev
->mutex
);
2431 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2433 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2434 struct rte_eth_dev_info dev_info
;
2436 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2440 ovs_mutex_lock(&dev
->mutex
);
2441 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2442 ovs_mutex_unlock(&dev
->mutex
);
2444 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2445 smap_add_format(args
, "numa_id", "%d",
2446 rte_eth_dev_socket_id(dev
->port_id
));
2447 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2448 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2449 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2450 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2451 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2452 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2453 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2454 dev_info
.max_hash_mac_addrs
);
2455 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2456 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2458 if (dev_info
.pci_dev
) {
2459 smap_add_format(args
, "pci-vendor_id", "0x%u",
2460 dev_info
.pci_dev
->id
.vendor_id
);
2461 smap_add_format(args
, "pci-device_id", "0x%x",
2462 dev_info
.pci_dev
->id
.device_id
);
2469 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2470 OVS_REQUIRES(dev
->mutex
)
2472 enum netdev_flags old_flags
;
2475 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2477 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2482 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2483 const char *argv
[], void *aux OVS_UNUSED
)
2487 if (!strcasecmp(argv
[argc
- 1], "up")) {
2489 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2492 unixctl_command_reply_error(conn
, "Invalid Admin State");
2497 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2498 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2499 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2501 ovs_mutex_lock(&dpdk_dev
->mutex
);
2502 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2503 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2505 netdev_close(netdev
);
2507 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2508 netdev_close(netdev
);
2512 struct netdev_dpdk
*netdev
;
2514 ovs_mutex_lock(&dpdk_mutex
);
2515 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2516 ovs_mutex_lock(&netdev
->mutex
);
2517 netdev_dpdk_set_admin_state__(netdev
, up
);
2518 ovs_mutex_unlock(&netdev
->mutex
);
2520 ovs_mutex_unlock(&dpdk_mutex
);
2522 unixctl_command_reply(conn
, "OK");
2526 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2527 const char *argv
[], void *aux OVS_UNUSED
)
2531 dpdk_port_t port_id
;
2532 char devname
[RTE_ETH_NAME_MAX_LEN
];
2533 struct netdev_dpdk
*dev
;
2535 ovs_mutex_lock(&dpdk_mutex
);
2537 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv
[1],
2539 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2543 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2545 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2546 "Remove it before detaching",
2547 argv
[1], netdev_get_name(&dev
->up
));
2551 rte_eth_dev_close(port_id
);
2553 ret
= rte_eth_dev_detach(port_id
, devname
);
2555 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2559 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2561 ovs_mutex_unlock(&dpdk_mutex
);
2562 unixctl_command_reply(conn
, response
);
2567 ovs_mutex_unlock(&dpdk_mutex
);
2568 unixctl_command_reply_error(conn
, response
);
2573 * Set virtqueue flags so that we do not receive interrupts.
2576 set_irq_status(int vid
)
2580 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2581 rte_vhost_enable_guest_notification(vid
, i
, 0);
2586 * Fixes mapping for vhost-user tx queues. Must be called after each
2587 * enabling/disabling of queues and n_txq modifications.
2590 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2591 OVS_REQUIRES(dev
->mutex
)
2593 int *enabled_queues
, n_enabled
= 0;
2594 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2596 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2598 for (i
= 0; i
< total_txqs
; i
++) {
2599 /* Enabled queues always mapped to themselves. */
2600 if (dev
->tx_q
[i
].map
== i
) {
2601 enabled_queues
[n_enabled
++] = i
;
2605 if (n_enabled
== 0 && total_txqs
!= 0) {
2606 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2611 for (i
= 0; i
< total_txqs
; i
++) {
2612 if (dev
->tx_q
[i
].map
!= i
) {
2613 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2614 k
= (k
+ 1) % n_enabled
;
2618 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2619 for (i
= 0; i
< total_txqs
; i
++) {
2620 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2623 free(enabled_queues
);
2627 * A new virtio-net device is added to a vhost port.
2632 struct netdev_dpdk
*dev
;
2633 bool exists
= false;
2635 char ifname
[IF_NAME_SZ
];
2637 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2639 ovs_mutex_lock(&dpdk_mutex
);
2640 /* Add device to the vhost port with the same name as that passed down. */
2641 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2642 ovs_mutex_lock(&dev
->mutex
);
2643 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2644 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
2646 /* Get NUMA information */
2647 newnode
= rte_vhost_get_numa_node(vid
);
2648 if (newnode
== -1) {
2650 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2653 newnode
= dev
->socket_id
;
2656 if (dev
->requested_n_txq
!= qp_num
2657 || dev
->requested_n_rxq
!= qp_num
2658 || dev
->requested_socket_id
!= newnode
) {
2659 dev
->requested_socket_id
= newnode
;
2660 dev
->requested_n_rxq
= qp_num
;
2661 dev
->requested_n_txq
= qp_num
;
2662 netdev_request_reconfigure(&dev
->up
);
2664 /* Reconfiguration not required. */
2665 dev
->vhost_reconfigured
= true;
2668 ovsrcu_index_set(&dev
->vid
, vid
);
2671 /* Disable notifications. */
2672 set_irq_status(vid
);
2673 netdev_change_seq_changed(&dev
->up
);
2674 ovs_mutex_unlock(&dev
->mutex
);
2677 ovs_mutex_unlock(&dev
->mutex
);
2679 ovs_mutex_unlock(&dpdk_mutex
);
2682 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2687 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2693 /* Clears mapping for all available queues of vhost interface. */
2695 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2696 OVS_REQUIRES(dev
->mutex
)
2700 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2701 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2706 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2707 * flag to stop any more packets from being sent or received to/from a VM and
2708 * ensure all currently queued packets have been sent/received before removing
2712 destroy_device(int vid
)
2714 struct netdev_dpdk
*dev
;
2715 bool exists
= false;
2716 char ifname
[IF_NAME_SZ
];
2718 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2720 ovs_mutex_lock(&dpdk_mutex
);
2721 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2722 if (netdev_dpdk_get_vid(dev
) == vid
) {
2724 ovs_mutex_lock(&dev
->mutex
);
2725 dev
->vhost_reconfigured
= false;
2726 ovsrcu_index_set(&dev
->vid
, -1);
2727 netdev_dpdk_txq_map_clear(dev
);
2729 netdev_change_seq_changed(&dev
->up
);
2730 ovs_mutex_unlock(&dev
->mutex
);
2736 ovs_mutex_unlock(&dpdk_mutex
);
2740 * Wait for other threads to quiesce after setting the 'virtio_dev'
2741 * to NULL, before returning.
2743 ovsrcu_synchronize();
2745 * As call to ovsrcu_synchronize() will end the quiescent state,
2746 * put thread back into quiescent state before returning.
2748 ovsrcu_quiesce_start();
2749 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2751 VLOG_INFO("vHost Device '%s' not found", ifname
);
2756 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2758 struct netdev_dpdk
*dev
;
2759 bool exists
= false;
2760 int qid
= queue_id
/ VIRTIO_QNUM
;
2761 char ifname
[IF_NAME_SZ
];
2763 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2765 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2769 ovs_mutex_lock(&dpdk_mutex
);
2770 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2771 ovs_mutex_lock(&dev
->mutex
);
2772 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2774 dev
->tx_q
[qid
].map
= qid
;
2776 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2778 netdev_dpdk_remap_txqs(dev
);
2780 ovs_mutex_unlock(&dev
->mutex
);
2783 ovs_mutex_unlock(&dev
->mutex
);
2785 ovs_mutex_unlock(&dpdk_mutex
);
2788 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2789 "changed to \'%s\'", queue_id
, qid
, ifname
,
2790 (enable
== 1) ? "enabled" : "disabled");
2792 VLOG_INFO("vHost Device '%s' not found", ifname
);
2800 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2802 return ovsrcu_index_get(&dev
->vid
);
2805 struct ingress_policer
*
2806 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2808 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2812 netdev_dpdk_class_init(void)
2814 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2816 /* This function can be called for different classes. The initialization
2817 * needs to be done only once */
2818 if (ovsthread_once_start(&once
)) {
2819 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2820 unixctl_command_register("netdev-dpdk/set-admin-state",
2821 "[netdev] up|down", 1, 2,
2822 netdev_dpdk_set_admin_state
, NULL
);
2824 unixctl_command_register("netdev-dpdk/detach",
2825 "pci address of device", 1, 1,
2826 netdev_dpdk_detach
, NULL
);
2828 ovsthread_once_done(&once
);
2838 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2839 dpdk_port_t
*eth_port_id
)
2841 struct dpdk_ring
*ring_pair
;
2845 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2850 /* XXX: Add support for multiquque ring. */
2851 ring_name
= xasprintf("%s_tx", dev_name
);
2853 /* Create single producer tx ring, netdev does explicit locking. */
2854 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2857 if (ring_pair
->cring_tx
== NULL
) {
2858 rte_free(ring_pair
);
2862 ring_name
= xasprintf("%s_rx", dev_name
);
2864 /* Create single consumer rx ring, netdev does explicit locking. */
2865 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2868 if (ring_pair
->cring_rx
== NULL
) {
2869 rte_free(ring_pair
);
2873 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2874 &ring_pair
->cring_tx
, 1, SOCKET0
);
2877 rte_free(ring_pair
);
2881 ring_pair
->user_port_id
= port_no
;
2882 ring_pair
->eth_port_id
= port_id
;
2883 *eth_port_id
= port_id
;
2885 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2891 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
2892 OVS_REQUIRES(dpdk_mutex
)
2894 struct dpdk_ring
*ring_pair
;
2895 unsigned int port_no
;
2898 /* Names always start with "dpdkr" */
2899 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2904 /* Look through our list to find the device */
2905 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2906 if (ring_pair
->user_port_id
== port_no
) {
2907 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2908 /* Really all that is needed */
2909 *eth_port_id
= ring_pair
->eth_port_id
;
2913 /* Need to create the device rings */
2914 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2918 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2919 struct dp_packet_batch
*batch
, bool may_steal
,
2920 bool concurrent_txq
)
2922 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2923 struct dp_packet
*packet
;
2925 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2926 * the rss hash field is clear. This is because the same mbuf may be
2927 * modified by the consumer of the ring and return into the datapath
2928 * without recalculating the RSS hash. */
2929 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
2930 dp_packet_mbuf_rss_flag_reset(packet
);
2933 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2938 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2940 dpdk_port_t port_no
= 0;
2943 ovs_mutex_lock(&dpdk_mutex
);
2945 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2950 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2951 rte_eth_dev_socket_id(port_no
));
2953 ovs_mutex_unlock(&dpdk_mutex
);
2960 * Initialize QoS configuration operations.
2963 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2966 rte_spinlock_init(&conf
->lock
);
2970 * Search existing QoS operations in qos_ops and compare each set of
2971 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2974 static const struct dpdk_qos_ops
*
2975 qos_lookup_name(const char *name
)
2977 const struct dpdk_qos_ops
*const *opsp
;
2979 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2980 const struct dpdk_qos_ops
*ops
= *opsp
;
2981 if (!strcmp(name
, ops
->qos_name
)) {
2989 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2992 const struct dpdk_qos_ops
*const *opsp
;
2994 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2995 const struct dpdk_qos_ops
*ops
= *opsp
;
2996 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
2997 sset_add(types
, ops
->qos_name
);
3004 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3005 const char **typep
, struct smap
*details
)
3007 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3008 struct qos_conf
*qos_conf
;
3011 ovs_mutex_lock(&dev
->mutex
);
3012 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3014 *typep
= qos_conf
->ops
->qos_name
;
3015 error
= (qos_conf
->ops
->qos_get
3016 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3018 /* No QoS configuration set, return an empty string */
3021 ovs_mutex_unlock(&dev
->mutex
);
3027 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3028 const struct smap
*details
)
3030 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3031 const struct dpdk_qos_ops
*new_ops
= NULL
;
3032 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3035 ovs_mutex_lock(&dev
->mutex
);
3037 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3039 new_ops
= qos_lookup_name(type
);
3041 if (!new_ops
|| !new_ops
->qos_construct
) {
3042 new_qos_conf
= NULL
;
3043 if (type
&& type
[0]) {
3046 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3047 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3048 new_qos_conf
= qos_conf
;
3050 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3054 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3055 type
, netdev
->name
, rte_strerror(error
));
3058 if (new_qos_conf
!= qos_conf
) {
3059 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3061 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3065 ovs_mutex_unlock(&dev
->mutex
);
3070 /* egress-policer details */
3072 struct egress_policer
{
3073 struct qos_conf qos_conf
;
3074 struct rte_meter_srtcm_params app_srtcm_params
;
3075 struct rte_meter_srtcm egress_meter
;
3079 egress_policer_details_to_param(const struct smap
*details
,
3080 struct rte_meter_srtcm_params
*params
)
3082 memset(params
, 0, sizeof *params
);
3083 params
->cir
= smap_get_ullong(details
, "cir", 0);
3084 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3089 egress_policer_qos_construct(const struct smap
*details
,
3090 struct qos_conf
**conf
)
3092 struct egress_policer
*policer
;
3095 policer
= xmalloc(sizeof *policer
);
3096 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3097 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3098 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3099 &policer
->app_srtcm_params
);
3101 *conf
= &policer
->qos_conf
;
3112 egress_policer_qos_destruct(struct qos_conf
*conf
)
3114 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3120 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3122 struct egress_policer
*policer
=
3123 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3125 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3126 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3132 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3133 const struct smap
*details
)
3135 struct egress_policer
*policer
=
3136 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3137 struct rte_meter_srtcm_params params
;
3139 egress_policer_details_to_param(details
, ¶ms
);
3141 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3145 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3149 struct egress_policer
*policer
=
3150 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3152 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3153 pkt_cnt
, may_steal
);
3158 static const struct dpdk_qos_ops egress_policer_ops
= {
3159 "egress-policer", /* qos_name */
3160 egress_policer_qos_construct
,
3161 egress_policer_qos_destruct
,
3162 egress_policer_qos_get
,
3163 egress_policer_qos_is_equal
,
3168 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3170 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3173 ovs_mutex_lock(&dev
->mutex
);
3175 if (netdev
->n_txq
== dev
->requested_n_txq
3176 && netdev
->n_rxq
== dev
->requested_n_rxq
3177 && dev
->mtu
== dev
->requested_mtu
3178 && dev
->rxq_size
== dev
->requested_rxq_size
3179 && dev
->txq_size
== dev
->requested_txq_size
3180 && dev
->socket_id
== dev
->requested_socket_id
) {
3181 /* Reconfiguration is unnecessary */
3186 rte_eth_dev_stop(dev
->port_id
);
3188 err
= netdev_dpdk_mempool_configure(dev
);
3189 if (err
&& err
!= EEXIST
) {
3193 netdev
->n_txq
= dev
->requested_n_txq
;
3194 netdev
->n_rxq
= dev
->requested_n_rxq
;
3196 dev
->rxq_size
= dev
->requested_rxq_size
;
3197 dev
->txq_size
= dev
->requested_txq_size
;
3199 rte_free(dev
->tx_q
);
3200 err
= dpdk_eth_dev_init(dev
);
3201 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3206 netdev_change_seq_changed(netdev
);
3209 ovs_mutex_unlock(&dev
->mutex
);
3214 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3215 OVS_REQUIRES(dev
->mutex
)
3217 dev
->up
.n_txq
= dev
->requested_n_txq
;
3218 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3221 /* Enable TX queue 0 by default if it wasn't disabled. */
3222 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3223 dev
->tx_q
[0].map
= 0;
3226 netdev_dpdk_remap_txqs(dev
);
3228 err
= netdev_dpdk_mempool_configure(dev
);
3230 /* A new mempool was created. */
3231 netdev_change_seq_changed(&dev
->up
);
3232 } else if (err
!= EEXIST
){
3235 if (netdev_dpdk_get_vid(dev
) >= 0) {
3236 if (dev
->vhost_reconfigured
== false) {
3237 dev
->vhost_reconfigured
= true;
3238 /* Carrier status may need updating. */
3239 netdev_change_seq_changed(&dev
->up
);
3247 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3249 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3252 ovs_mutex_lock(&dev
->mutex
);
3253 err
= dpdk_vhost_reconfigure_helper(dev
);
3254 ovs_mutex_unlock(&dev
->mutex
);
3260 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3262 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3265 ovs_mutex_lock(&dev
->mutex
);
3267 /* Configure vHost client mode if requested and if the following criteria
3269 * 1. Device hasn't been registered yet.
3270 * 2. A path has been specified.
3272 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3273 && strlen(dev
->vhost_id
)) {
3274 /* Register client-mode device */
3275 err
= rte_vhost_driver_register(dev
->vhost_id
,
3276 RTE_VHOST_USER_CLIENT
);
3278 VLOG_ERR("vhost-user device setup failure for device %s\n",
3282 /* Configuration successful */
3283 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3284 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3285 "using client socket '%s'",
3286 dev
->up
.name
, dev
->vhost_id
);
3289 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3290 &virtio_net_device_ops
);
3292 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3293 "vhost user client port: %s\n", dev
->up
.name
);
3297 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3298 1ULL << VIRTIO_NET_F_HOST_TSO4
3299 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3300 | 1ULL << VIRTIO_NET_F_CSUM
);
3302 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3303 "client port: %s\n", dev
->up
.name
);
3307 err
= rte_vhost_driver_start(dev
->vhost_id
);
3309 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3310 "client port: %s\n", dev
->up
.name
);
3315 err
= dpdk_vhost_reconfigure_helper(dev
);
3318 ovs_mutex_unlock(&dev
->mutex
);
3323 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3324 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3325 GET_CARRIER, GET_STATS, \
3326 GET_FEATURES, GET_STATUS, \
3327 RECONFIGURE, RXQ_RECV) \
3330 true, /* is_pmd */ \
3332 NULL, /* netdev_dpdk_run */ \
3333 NULL, /* netdev_dpdk_wait */ \
3335 netdev_dpdk_alloc, \
3338 netdev_dpdk_dealloc, \
3339 netdev_dpdk_get_config, \
3341 NULL, /* get_tunnel_config */ \
3342 NULL, /* build header */ \
3343 NULL, /* push header */ \
3344 NULL, /* pop header */ \
3345 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3349 NULL, /* send_wait */ \
3351 netdev_dpdk_set_etheraddr, \
3352 netdev_dpdk_get_etheraddr, \
3353 netdev_dpdk_get_mtu, \
3354 netdev_dpdk_set_mtu, \
3355 netdev_dpdk_get_ifindex, \
3357 netdev_dpdk_get_carrier_resets, \
3358 netdev_dpdk_set_miimon, \
3361 NULL, /* set_advertisements */ \
3362 NULL, /* get_pt_mode */ \
3364 netdev_dpdk_set_policing, \
3365 netdev_dpdk_get_qos_types, \
3366 NULL, /* get_qos_capabilities */ \
3367 netdev_dpdk_get_qos, \
3368 netdev_dpdk_set_qos, \
3369 NULL, /* get_queue */ \
3370 NULL, /* set_queue */ \
3371 NULL, /* delete_queue */ \
3372 NULL, /* get_queue_stats */ \
3373 NULL, /* queue_dump_start */ \
3374 NULL, /* queue_dump_next */ \
3375 NULL, /* queue_dump_done */ \
3376 NULL, /* dump_queue_stats */ \
3378 NULL, /* set_in4 */ \
3379 NULL, /* get_addr_list */ \
3380 NULL, /* add_router */ \
3381 NULL, /* get_next_hop */ \
3383 NULL, /* arp_lookup */ \
3385 netdev_dpdk_update_flags, \
3388 netdev_dpdk_rxq_alloc, \
3389 netdev_dpdk_rxq_construct, \
3390 netdev_dpdk_rxq_destruct, \
3391 netdev_dpdk_rxq_dealloc, \
3393 NULL, /* rx_wait */ \
3394 NULL, /* rxq_drain */ \
3398 static const struct netdev_class dpdk_class
=
3401 netdev_dpdk_class_init
,
3402 netdev_dpdk_construct
,
3403 netdev_dpdk_destruct
,
3404 netdev_dpdk_set_config
,
3405 netdev_dpdk_set_tx_multiq
,
3406 netdev_dpdk_eth_send
,
3407 netdev_dpdk_get_carrier
,
3408 netdev_dpdk_get_stats
,
3409 netdev_dpdk_get_features
,
3410 netdev_dpdk_get_status
,
3411 netdev_dpdk_reconfigure
,
3412 netdev_dpdk_rxq_recv
);
3414 static const struct netdev_class dpdk_ring_class
=
3417 netdev_dpdk_class_init
,
3418 netdev_dpdk_ring_construct
,
3419 netdev_dpdk_destruct
,
3420 netdev_dpdk_ring_set_config
,
3421 netdev_dpdk_set_tx_multiq
,
3422 netdev_dpdk_ring_send
,
3423 netdev_dpdk_get_carrier
,
3424 netdev_dpdk_get_stats
,
3425 netdev_dpdk_get_features
,
3426 netdev_dpdk_get_status
,
3427 netdev_dpdk_reconfigure
,
3428 netdev_dpdk_rxq_recv
);
3430 static const struct netdev_class dpdk_vhost_class
=
3434 netdev_dpdk_vhost_construct
,
3435 netdev_dpdk_vhost_destruct
,
3438 netdev_dpdk_vhost_send
,
3439 netdev_dpdk_vhost_get_carrier
,
3440 netdev_dpdk_vhost_get_stats
,
3443 netdev_dpdk_vhost_reconfigure
,
3444 netdev_dpdk_vhost_rxq_recv
);
3445 static const struct netdev_class dpdk_vhost_client_class
=
3447 "dpdkvhostuserclient",
3449 netdev_dpdk_vhost_client_construct
,
3450 netdev_dpdk_vhost_destruct
,
3451 netdev_dpdk_vhost_client_set_config
,
3453 netdev_dpdk_vhost_send
,
3454 netdev_dpdk_vhost_get_carrier
,
3455 netdev_dpdk_vhost_get_stats
,
3458 netdev_dpdk_vhost_client_reconfigure
,
3459 netdev_dpdk_vhost_rxq_recv
);
3462 netdev_dpdk_register(void)
3464 netdev_register_provider(&dpdk_class
);
3465 netdev_register_provider(&dpdk_ring_class
);
3466 netdev_register_provider(&dpdk_vhost_class
);
3467 netdev_register_provider(&dpdk_vhost_client_class
);