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
;
307 struct rte_mempool
*mp
;
310 char if_name
[IFNAMSIZ
];
312 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
315 /* There should be one 'struct dpdk_tx_queue' created for
317 struct dpdk_tx_queue
{
318 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
319 * from concurrent access. It is used only
320 * if the queue is shared among different
321 * pmd threads (see 'concurrent_txq'). */
322 int map
; /* Mapping of configured vhost-user queues
323 * to enabled by guest. */
326 /* dpdk has no way to remove dpdk ring ethernet devices
327 so we have to keep them around once they've been created
330 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
331 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
334 /* For the client rings */
335 struct rte_ring
*cring_tx
;
336 struct rte_ring
*cring_rx
;
337 unsigned int user_port_id
; /* User given port no, parsed from port name */
338 dpdk_port_t eth_port_id
; /* ethernet device port id */
339 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
342 struct ingress_policer
{
343 struct rte_meter_srtcm_params app_srtcm_params
;
344 struct rte_meter_srtcm in_policer
;
345 rte_spinlock_t policer_lock
;
348 enum dpdk_hw_ol_features
{
349 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
356 enum dpdk_dev_type type
;
358 struct dpdk_tx_queue
*tx_q
;
360 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
362 struct dpdk_mp
*dpdk_mp
;
366 struct netdev_stats stats
;
368 rte_spinlock_t stats_lock
;
370 struct eth_addr hwaddr
;
371 enum netdev_flags flags
;
373 struct rte_eth_link link
;
376 /* virtio identifier for vhost devices */
379 /* True if vHost device is 'up' and has been reconfigured at least once */
380 bool vhost_reconfigured
;
382 /* Identifier used to distinguish vhost devices from each other. */
383 char vhost_id
[PATH_MAX
];
385 /* Device arguments for dpdk ports */
388 /* If true, device was attached by rte_eth_dev_attach(). */
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 /* The following properties cannot be changed when a device is running,
398 * so we remember the request and update them next time
399 * netdev_dpdk*_reconfigure() is called */
403 int requested_rxq_size
;
404 int requested_txq_size
;
406 /* Number of rx/tx descriptors for physical devices */
410 /* Socket ID detected when vHost device is brought up */
411 int requested_socket_id
;
413 /* Denotes whether vHost port is client/server mode */
414 uint64_t vhost_driver_flags
;
416 /* Ingress Policer */
417 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
418 uint32_t policer_rate
;
419 uint32_t policer_burst
;
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
;
429 struct netdev_rxq_dpdk
{
430 struct netdev_rxq up
;
434 static void netdev_dpdk_destruct(struct netdev
*netdev
);
435 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
437 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
439 struct ingress_policer
*
440 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
443 is_dpdk_class(const struct netdev_class
*class)
445 return class->destruct
== netdev_dpdk_destruct
446 || class->destruct
== netdev_dpdk_vhost_destruct
;
449 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
450 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
451 * value, insufficient buffers are allocated to accomodate the packet in its
452 * entirety. Furthermore, certain drivers need to ensure that there is also
453 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
454 * frames). If the RX buffer is too small, then the driver enables scatter RX
455 * behaviour, which reduces performance. To prevent this, use a buffer size
456 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
459 dpdk_buf_size(int mtu
)
461 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
462 NETDEV_DPDK_MBUF_ALIGN
);
465 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
467 * Unlike xmalloc(), this function can return NULL on failure. */
469 dpdk_rte_mzalloc(size_t sz
)
471 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
475 free_dpdk_buf(struct dp_packet
*p
)
477 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
479 rte_pktmbuf_free(pkt
);
483 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
484 void *opaque_arg OVS_UNUSED
,
486 unsigned i OVS_UNUSED
)
488 struct rte_mbuf
*pkt
= _p
;
490 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
494 * Full DPDK memory pool name must be unique
495 * and cannot be longer than RTE_MEMPOOL_NAMESIZE
498 dpdk_mp_name(struct dpdk_mp
*dmp
)
500 uint32_t h
= hash_string(dmp
->if_name
, 0);
501 char *mp_name
= xcalloc(RTE_MEMPOOL_NAMESIZE
, sizeof *mp_name
);
502 int ret
= snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
, "ovs_%x_%d_%u",
503 h
, dmp
->mtu
, dmp
->mp_size
);
504 if (ret
< 0 || ret
>= RTE_MEMPOOL_NAMESIZE
) {
510 static struct dpdk_mp
*
511 dpdk_mp_create(struct netdev_dpdk
*dev
, int mtu
, bool *mp_exists
)
513 struct dpdk_mp
*dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
518 dmp
->socket_id
= dev
->requested_socket_id
;
520 ovs_strzcpy(dmp
->if_name
, dev
->up
.name
, IFNAMSIZ
);
523 * XXX: rough estimation of memory required for port:
524 * <packets required to fill the device rxqs>
525 * + <packets that could be stuck on other ports txqs>
526 * + <packets in the pmd threads>
527 * + <additional memory for corner cases>
529 dmp
->mp_size
= dev
->requested_n_rxq
* dev
->requested_rxq_size
530 + dev
->requested_n_txq
* dev
->requested_txq_size
531 + MIN(RTE_MAX_LCORE
, dev
->requested_n_rxq
) * NETDEV_MAX_BURST
535 char *mp_name
= dpdk_mp_name(dmp
);
537 VLOG_DBG("Requesting a mempool of %u mbufs for netdev %s "
538 "with %d Rx and %d Tx queues.",
539 dmp
->mp_size
, dev
->up
.name
,
540 dev
->requested_n_rxq
, dev
->requested_n_txq
);
542 dmp
->mp
= rte_pktmbuf_pool_create(mp_name
, dmp
->mp_size
,
544 sizeof (struct dp_packet
)
545 - sizeof (struct rte_mbuf
),
547 - sizeof(struct dp_packet
),
550 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", mp_name
,
552 } else if (rte_errno
== EEXIST
) {
553 /* A mempool with the same name already exists. We just
554 * retrieve its pointer to be returned to the caller. */
555 dmp
->mp
= rte_mempool_lookup(mp_name
);
556 VLOG_DBG("A mempool with name %s already exists at %p.",
558 /* As the mempool create returned EEXIST we can expect the
559 * lookup has returned a valid pointer. If for some reason
560 * that's not the case we keep track of it. */
563 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
564 mp_name
, dmp
->mp_size
);
568 /* rte_pktmbuf_pool_create has done some initialization of the
569 * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init
570 * initializes some OVS specific fields of dp_packet.
572 rte_mempool_obj_iter(dmp
->mp
, ovs_rte_pktmbuf_init
, NULL
);
575 } while (!(*mp_exists
) &&
576 (rte_errno
== ENOMEM
&& (dmp
->mp_size
/= 2) >= MIN_NB_MBUF
));
582 /* Returns a valid pointer when either of the following is true:
583 * - a new mempool was just created;
584 * - a matching mempool already exists. */
585 static struct dpdk_mp
*
586 dpdk_mp_get(struct netdev_dpdk
*dev
, int mtu
, bool *mp_exists
)
590 ovs_mutex_lock(&dpdk_mp_mutex
);
591 dmp
= dpdk_mp_create(dev
, mtu
, mp_exists
);
592 ovs_mutex_unlock(&dpdk_mp_mutex
);
598 dpdk_mp_put(struct dpdk_mp
*dmp
)
606 ovs_mutex_lock(&dpdk_mp_mutex
);
607 mp_name
= dpdk_mp_name(dmp
);
608 VLOG_DBG("Releasing \"%s\" mempool", mp_name
);
610 rte_mempool_free(dmp
->mp
);
612 ovs_mutex_unlock(&dpdk_mp_mutex
);
615 /* Tries to allocate a new mempool - or re-use an existing one where
616 * appropriate - on requested_socket_id with a size determined by
617 * requested_mtu and requested Rx/Tx queues.
618 * On success - or when re-using an existing mempool - the new configuration
620 * On error, device will be left unchanged. */
622 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
623 OVS_REQUIRES(dev
->mutex
)
625 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
629 mp
= dpdk_mp_get(dev
, FRAME_LEN_TO_MTU(buf_size
), &mp_exists
);
631 VLOG_ERR("Failed to create memory pool for netdev "
632 "%s, with MTU %d on socket %d: %s\n",
633 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
634 rte_strerror(rte_errno
));
636 } else if (mp_exists
) {
637 /* If a new MTU was requested and its rounded value equals the one
638 * that is currently used, then the existing mempool is returned.
639 * Update dev with the new values. */
640 dev
->mtu
= dev
->requested_mtu
;
641 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
644 dpdk_mp_put(dev
->dpdk_mp
);
646 dev
->mtu
= dev
->requested_mtu
;
647 dev
->socket_id
= dev
->requested_socket_id
;
648 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
655 check_link_status(struct netdev_dpdk
*dev
)
657 struct rte_eth_link link
;
659 rte_eth_link_get_nowait(dev
->port_id
, &link
);
661 if (dev
->link
.link_status
!= link
.link_status
) {
662 netdev_change_seq_changed(&dev
->up
);
664 dev
->link_reset_cnt
++;
666 if (dev
->link
.link_status
) {
667 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
668 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
669 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
670 ("full-duplex") : ("half-duplex"));
672 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
678 dpdk_watchdog(void *dummy OVS_UNUSED
)
680 struct netdev_dpdk
*dev
;
682 pthread_detach(pthread_self());
685 ovs_mutex_lock(&dpdk_mutex
);
686 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
687 ovs_mutex_lock(&dev
->mutex
);
688 if (dev
->type
== DPDK_DEV_ETH
) {
689 check_link_status(dev
);
691 ovs_mutex_unlock(&dev
->mutex
);
693 ovs_mutex_unlock(&dpdk_mutex
);
694 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
701 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
705 struct rte_eth_conf conf
= port_conf
;
707 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
709 if (dev
->mtu
> ETHER_MTU
) {
710 conf
.rxmode
.enable_scatter
= 1;
713 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
714 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
715 /* A device may report more queues than it makes available (this has
716 * been observed for Intel xl710, which reserves some of them for
717 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
718 * available. When this happens we can retry the configuration
719 * and request less queues */
720 while (n_rxq
&& n_txq
) {
722 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
725 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
727 VLOG_WARN("Interface %s eth_dev setup error %s\n",
728 dev
->up
.name
, rte_strerror(-diag
));
732 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
734 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
735 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
739 for (i
= 0; i
< n_txq
; i
++) {
740 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
741 dev
->socket_id
, NULL
);
743 VLOG_INFO("Interface %s txq(%d) setup error: %s",
744 dev
->up
.name
, i
, rte_strerror(-diag
));
750 /* Retry with less tx queues */
755 for (i
= 0; i
< n_rxq
; i
++) {
756 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
757 dev
->socket_id
, NULL
,
760 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
761 dev
->up
.name
, i
, rte_strerror(-diag
));
767 /* Retry with less rx queues */
772 dev
->up
.n_rxq
= n_rxq
;
773 dev
->up
.n_txq
= n_txq
;
782 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
784 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
785 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
791 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
792 OVS_REQUIRES(dev
->mutex
)
794 struct rte_pktmbuf_pool_private
*mbp_priv
;
795 struct rte_eth_dev_info info
;
796 struct ether_addr eth_addr
;
799 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
800 DEV_RX_OFFLOAD_TCP_CKSUM
|
801 DEV_RX_OFFLOAD_IPV4_CKSUM
;
803 rte_eth_dev_info_get(dev
->port_id
, &info
);
805 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
806 rx_chksm_offload_capa
) {
807 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
809 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
811 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
814 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
815 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
817 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
819 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
820 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
824 diag
= rte_eth_dev_start(dev
->port_id
);
826 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
827 rte_strerror(-diag
));
831 rte_eth_promiscuous_enable(dev
->port_id
);
832 rte_eth_allmulticast_enable(dev
->port_id
);
834 memset(ð_addr
, 0x0, sizeof(eth_addr
));
835 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
836 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
837 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
839 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
840 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
842 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
843 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
845 /* Get the Flow control configuration for DPDK-ETH */
846 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
848 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
855 static struct netdev_dpdk
*
856 netdev_dpdk_cast(const struct netdev
*netdev
)
858 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
861 static struct netdev
*
862 netdev_dpdk_alloc(void)
864 struct netdev_dpdk
*dev
;
866 dev
= dpdk_rte_mzalloc(sizeof *dev
);
874 static struct dpdk_tx_queue
*
875 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
877 struct dpdk_tx_queue
*txqs
;
880 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
882 for (i
= 0; i
< n_txqs
; i
++) {
883 /* Initialize map for vhost devices. */
884 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
885 rte_spinlock_init(&txqs
[i
].tx_lock
);
893 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
894 enum dpdk_dev_type type
, int socket_id
)
895 OVS_REQUIRES(dpdk_mutex
)
897 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
899 ovs_mutex_init(&dev
->mutex
);
901 rte_spinlock_init(&dev
->stats_lock
);
903 /* If the 'sid' is negative, it means that the kernel fails
904 * to obtain the pci numa info. In that situation, always
906 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
907 dev
->requested_socket_id
= dev
->socket_id
;
908 dev
->port_id
= port_no
;
911 dev
->requested_mtu
= ETHER_MTU
;
912 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
913 ovsrcu_index_init(&dev
->vid
, -1);
914 dev
->vhost_reconfigured
= false;
915 dev
->attached
= false;
917 ovsrcu_init(&dev
->qos_conf
, NULL
);
919 ovsrcu_init(&dev
->ingress_policer
, NULL
);
920 dev
->policer_rate
= 0;
921 dev
->policer_burst
= 0;
925 dev
->requested_n_rxq
= NR_QUEUE
;
926 dev
->requested_n_txq
= NR_QUEUE
;
927 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
928 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
930 /* Initialize the flow control to NULL */
931 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
933 /* Initilize the hardware offload flags to 0 */
934 dev
->hw_ol_features
= 0;
936 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
938 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
940 netdev_request_reconfigure(netdev
);
945 /* dev_name must be the prefix followed by a positive decimal number.
946 * (no leading + or - signs are allowed) */
948 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
949 unsigned int *port_no
)
953 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
957 cport
= dev_name
+ strlen(prefix
);
959 if (str_to_uint(cport
, 10, port_no
)) {
967 vhost_common_construct(struct netdev
*netdev
)
968 OVS_REQUIRES(dpdk_mutex
)
970 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
971 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
973 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
978 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
979 DPDK_DEV_VHOST
, socket_id
);
983 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
985 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
986 const char *name
= netdev
->name
;
989 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
990 * the file system. '/' or '\' would traverse directories, so they're not
991 * acceptable in 'name'. */
992 if (strchr(name
, '/') || strchr(name
, '\\')) {
993 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
994 "A valid name must not include '/' or '\\'",
999 ovs_mutex_lock(&dpdk_mutex
);
1000 /* Take the name of the vhost-user port and append it to the location where
1001 * the socket is to be created, then register the socket.
1003 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
1004 dpdk_get_vhost_sock_dir(), name
);
1006 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
1007 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
1009 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
1013 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
1014 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1015 dev
->vhost_id
, name
);
1018 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
1019 &virtio_net_device_ops
);
1021 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1022 "port: %s\n", name
);
1026 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1027 1ULL << VIRTIO_NET_F_HOST_TSO4
1028 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1029 | 1ULL << VIRTIO_NET_F_CSUM
);
1031 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1032 "port: %s\n", name
);
1036 err
= rte_vhost_driver_start(dev
->vhost_id
);
1038 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1039 "port: %s\n", name
);
1043 err
= vhost_common_construct(netdev
);
1045 VLOG_ERR("vhost_common_construct failed for vhost user "
1046 "port: %s\n", name
);
1050 ovs_mutex_unlock(&dpdk_mutex
);
1051 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1052 "please migrate to dpdkvhostuserclient ports.");
1057 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1061 ovs_mutex_lock(&dpdk_mutex
);
1062 err
= vhost_common_construct(netdev
);
1064 VLOG_ERR("vhost_common_construct failed for vhost user client"
1065 "port: %s\n", netdev
->name
);
1067 ovs_mutex_unlock(&dpdk_mutex
);
1072 netdev_dpdk_construct(struct netdev
*netdev
)
1076 ovs_mutex_lock(&dpdk_mutex
);
1077 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1078 DPDK_DEV_ETH
, SOCKET0
);
1079 ovs_mutex_unlock(&dpdk_mutex
);
1084 common_destruct(struct netdev_dpdk
*dev
)
1085 OVS_REQUIRES(dpdk_mutex
)
1086 OVS_EXCLUDED(dev
->mutex
)
1088 rte_free(dev
->tx_q
);
1089 dpdk_mp_put(dev
->dpdk_mp
);
1091 ovs_list_remove(&dev
->list_node
);
1092 free(ovsrcu_get_protected(struct ingress_policer
*,
1093 &dev
->ingress_policer
));
1094 ovs_mutex_destroy(&dev
->mutex
);
1098 netdev_dpdk_destruct(struct netdev
*netdev
)
1100 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1101 char devname
[RTE_ETH_NAME_MAX_LEN
];
1103 ovs_mutex_lock(&dpdk_mutex
);
1105 rte_eth_dev_stop(dev
->port_id
);
1107 if (dev
->attached
) {
1108 rte_eth_dev_close(dev
->port_id
);
1109 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1110 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1112 VLOG_INFO("Device '%s' has been detached", devname
);
1117 common_destruct(dev
);
1119 ovs_mutex_unlock(&dpdk_mutex
);
1122 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1123 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1124 * deadlock, none of the mutexes must be held while calling this function. */
1126 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1128 OVS_EXCLUDED(dpdk_mutex
)
1129 OVS_EXCLUDED(dev
->mutex
)
1131 return rte_vhost_driver_unregister(vhost_id
);
1135 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1137 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1140 ovs_mutex_lock(&dpdk_mutex
);
1142 /* Guest becomes an orphan if still attached. */
1143 if (netdev_dpdk_get_vid(dev
) >= 0
1144 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1145 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1147 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1148 "socket '%s' must be restarted.", dev
->vhost_id
);
1151 vhost_id
= xstrdup(dev
->vhost_id
);
1153 common_destruct(dev
);
1155 ovs_mutex_unlock(&dpdk_mutex
);
1161 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1162 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1163 netdev
->name
, vhost_id
);
1164 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1165 /* OVS server mode - remove this socket from list for deletion */
1166 fatal_signal_remove_file_to_unlink(vhost_id
);
1173 netdev_dpdk_dealloc(struct netdev
*netdev
)
1175 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1181 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1183 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1185 ovs_mutex_lock(&dev
->mutex
);
1187 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1188 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1189 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1190 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1191 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1193 if (dev
->type
== DPDK_DEV_ETH
) {
1194 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1195 dev
->requested_rxq_size
);
1196 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1198 smap_add_format(args
, "requested_txq_descriptors", "%d",
1199 dev
->requested_txq_size
);
1200 smap_add_format(args
, "configured_txq_descriptors", "%d",
1202 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1203 smap_add(args
, "rx_csum_offload", "true");
1205 smap_add(args
, "rx_csum_offload", "false");
1208 ovs_mutex_unlock(&dev
->mutex
);
1213 static struct netdev_dpdk
*
1214 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1215 OVS_REQUIRES(dpdk_mutex
)
1217 struct netdev_dpdk
*dev
;
1219 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1220 if (dev
->port_id
== port_id
) {
1229 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1230 const char *devargs
, char **errp
)
1232 /* Get the name up to the first comma. */
1233 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1234 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1236 if (!rte_eth_dev_count()
1237 || rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1238 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1239 /* Device not found in DPDK, attempt to attach it */
1240 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1241 /* Attach successful */
1242 dev
->attached
= true;
1243 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1245 /* Attach unsuccessful */
1246 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1247 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1257 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1258 OVS_REQUIRES(dev
->mutex
)
1262 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1263 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1264 dev
->requested_n_rxq
= new_n_rxq
;
1265 netdev_request_reconfigure(&dev
->up
);
1270 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1271 const char *flag
, int default_size
, int *new_size
)
1273 int queue_size
= smap_get_int(args
, flag
, default_size
);
1275 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1276 || !is_pow2(queue_size
)) {
1277 queue_size
= default_size
;
1280 if (queue_size
!= *new_size
) {
1281 *new_size
= queue_size
;
1282 netdev_request_reconfigure(netdev
);
1287 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1290 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1291 bool rx_fc_en
, tx_fc_en
, autoneg
;
1292 enum rte_eth_fc_mode fc_mode
;
1293 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1294 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1295 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1297 const char *new_devargs
;
1300 ovs_mutex_lock(&dpdk_mutex
);
1301 ovs_mutex_lock(&dev
->mutex
);
1303 dpdk_set_rxq_config(dev
, args
);
1305 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1306 NIC_PORT_DEFAULT_RXQ_SIZE
,
1307 &dev
->requested_rxq_size
);
1308 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1309 NIC_PORT_DEFAULT_TXQ_SIZE
,
1310 &dev
->requested_txq_size
);
1312 new_devargs
= smap_get(args
, "dpdk-devargs");
1314 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1315 /* The user requested a new device. If we return error, the caller
1316 * will delete this netdev and try to recreate it. */
1321 /* dpdk-devargs is required for device configuration */
1322 if (new_devargs
&& new_devargs
[0]) {
1323 /* Don't process dpdk-devargs if value is unchanged and port id
1325 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1326 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1327 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1330 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1332 } else if (new_port_id
== dev
->port_id
) {
1333 /* Already configured, do not reconfigure again */
1336 struct netdev_dpdk
*dup_dev
;
1338 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1340 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1341 "which is already in use by '%s'",
1342 netdev_get_name(netdev
), new_devargs
,
1343 netdev_get_name(&dup_dev
->up
));
1346 int sid
= rte_eth_dev_socket_id(new_port_id
);
1348 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1349 dev
->devargs
= xstrdup(new_devargs
);
1350 dev
->port_id
= new_port_id
;
1351 netdev_request_reconfigure(&dev
->up
);
1357 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1358 "The old 'dpdk<port_id>' names are not supported",
1359 netdev_get_name(netdev
));
1367 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1368 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1369 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1371 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1372 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1373 dev
->fc_conf
.mode
= fc_mode
;
1374 dev
->fc_conf
.autoneg
= autoneg
;
1375 dpdk_eth_flow_ctrl_setup(dev
);
1379 ovs_mutex_unlock(&dev
->mutex
);
1380 ovs_mutex_unlock(&dpdk_mutex
);
1386 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1387 char **errp OVS_UNUSED
)
1389 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1391 ovs_mutex_lock(&dev
->mutex
);
1392 dpdk_set_rxq_config(dev
, args
);
1393 ovs_mutex_unlock(&dev
->mutex
);
1399 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1400 const struct smap
*args
,
1401 char **errp OVS_UNUSED
)
1403 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1406 ovs_mutex_lock(&dev
->mutex
);
1407 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1408 path
= smap_get(args
, "vhost-server-path");
1409 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1410 strcpy(dev
->vhost_id
, path
);
1411 netdev_request_reconfigure(netdev
);
1414 ovs_mutex_unlock(&dev
->mutex
);
1420 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1422 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1424 return dev
->socket_id
;
1427 /* Sets the number of tx queues for the dpdk interface. */
1429 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1431 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1433 ovs_mutex_lock(&dev
->mutex
);
1435 if (dev
->requested_n_txq
== n_txq
) {
1439 dev
->requested_n_txq
= n_txq
;
1440 netdev_request_reconfigure(netdev
);
1443 ovs_mutex_unlock(&dev
->mutex
);
1447 static struct netdev_rxq
*
1448 netdev_dpdk_rxq_alloc(void)
1450 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1459 static struct netdev_rxq_dpdk
*
1460 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1462 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1466 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1468 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1469 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1471 ovs_mutex_lock(&dev
->mutex
);
1472 rx
->port_id
= dev
->port_id
;
1473 ovs_mutex_unlock(&dev
->mutex
);
1479 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1484 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1486 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1491 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1492 * 'pkts', even in case of failure.
1494 * Returns the number of packets that weren't transmitted. */
1496 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1497 struct rte_mbuf
**pkts
, int cnt
)
1501 while (nb_tx
!= cnt
) {
1504 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1512 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1513 /* Free buffers, which we couldn't transmit, one at a time (each
1514 * packet could come from a different mempool) */
1517 for (i
= nb_tx
; i
< cnt
; i
++) {
1518 rte_pktmbuf_free(pkts
[i
]);
1526 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1527 struct rte_mbuf
*pkt
, uint64_t time
)
1529 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1531 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1536 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1537 struct rte_mbuf
**pkts
, int pkt_cnt
,
1542 struct rte_mbuf
*pkt
= NULL
;
1543 uint64_t current_time
= rte_rdtsc();
1545 for (i
= 0; i
< pkt_cnt
; i
++) {
1547 /* Handle current packet */
1548 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1555 rte_pktmbuf_free(pkt
);
1564 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1565 int pkt_cnt
, bool may_steal
)
1569 rte_spinlock_lock(&policer
->policer_lock
);
1570 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1571 pkt_cnt
, may_steal
);
1572 rte_spinlock_unlock(&policer
->policer_lock
);
1578 is_vhost_running(struct netdev_dpdk
*dev
)
1580 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1584 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1585 unsigned int packet_size
)
1587 /* Hard-coded search for the size bucket. */
1588 if (packet_size
< 256) {
1589 if (packet_size
>= 128) {
1590 stats
->rx_128_to_255_packets
++;
1591 } else if (packet_size
<= 64) {
1592 stats
->rx_1_to_64_packets
++;
1594 stats
->rx_65_to_127_packets
++;
1597 if (packet_size
>= 1523) {
1598 stats
->rx_1523_to_max_packets
++;
1599 } else if (packet_size
>= 1024) {
1600 stats
->rx_1024_to_1522_packets
++;
1601 } else if (packet_size
< 512) {
1602 stats
->rx_256_to_511_packets
++;
1604 stats
->rx_512_to_1023_packets
++;
1610 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1611 struct dp_packet
**packets
, int count
,
1615 unsigned int packet_size
;
1616 struct dp_packet
*packet
;
1618 stats
->rx_packets
+= count
;
1619 stats
->rx_dropped
+= dropped
;
1620 for (i
= 0; i
< count
; i
++) {
1621 packet
= packets
[i
];
1622 packet_size
= dp_packet_size(packet
);
1624 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1625 /* This only protects the following multicast counting from
1626 * too short packets, but it does not stop the packet from
1627 * further processing. */
1629 stats
->rx_length_errors
++;
1633 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1635 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1636 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1640 stats
->rx_bytes
+= packet_size
;
1645 * The receive path for the vhost port is the TX path out from guest.
1648 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1649 struct dp_packet_batch
*batch
)
1651 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1652 int qid
= rxq
->queue_id
;
1653 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1655 uint16_t dropped
= 0;
1657 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1658 || !(dev
->flags
& NETDEV_UP
))) {
1662 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1663 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1665 (struct rte_mbuf
**) batch
->packets
,
1673 nb_rx
= ingress_policer_run(policer
,
1674 (struct rte_mbuf
**) batch
->packets
,
1679 rte_spinlock_lock(&dev
->stats_lock
);
1680 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1682 rte_spinlock_unlock(&dev
->stats_lock
);
1684 batch
->count
= nb_rx
;
1685 dp_packet_batch_init_packet_fields(batch
);
1691 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1693 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1694 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1695 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1699 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1703 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1704 (struct rte_mbuf
**) batch
->packets
,
1712 nb_rx
= ingress_policer_run(policer
,
1713 (struct rte_mbuf
**) batch
->packets
,
1718 /* Update stats to reflect dropped packets */
1719 if (OVS_UNLIKELY(dropped
)) {
1720 rte_spinlock_lock(&dev
->stats_lock
);
1721 dev
->stats
.rx_dropped
+= dropped
;
1722 rte_spinlock_unlock(&dev
->stats_lock
);
1725 batch
->count
= nb_rx
;
1726 dp_packet_batch_init_packet_fields(batch
);
1732 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1733 int cnt
, bool may_steal
)
1735 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1738 rte_spinlock_lock(&qos_conf
->lock
);
1739 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1740 rte_spinlock_unlock(&qos_conf
->lock
);
1747 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1752 struct rte_mbuf
*pkt
;
1754 for (i
= 0; i
< pkt_cnt
; i
++) {
1756 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1757 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1758 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1759 rte_pktmbuf_free(pkt
);
1763 if (OVS_UNLIKELY(i
!= cnt
)) {
1773 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1774 struct dp_packet
**packets
,
1779 int sent
= attempted
- dropped
;
1781 stats
->tx_packets
+= sent
;
1782 stats
->tx_dropped
+= dropped
;
1784 for (i
= 0; i
< sent
; i
++) {
1785 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1790 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1791 struct dp_packet
**pkts
, int cnt
)
1793 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1794 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1795 unsigned int total_pkts
= cnt
;
1796 unsigned int dropped
= 0;
1799 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1801 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1802 || !(dev
->flags
& NETDEV_UP
))) {
1803 rte_spinlock_lock(&dev
->stats_lock
);
1804 dev
->stats
.tx_dropped
+= cnt
;
1805 rte_spinlock_unlock(&dev
->stats_lock
);
1809 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1811 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1812 /* Check has QoS has been configured for the netdev */
1813 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
1814 dropped
= total_pkts
- cnt
;
1817 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1818 unsigned int tx_pkts
;
1820 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1821 vhost_qid
, cur_pkts
, cnt
);
1822 if (OVS_LIKELY(tx_pkts
)) {
1823 /* Packets have been sent.*/
1825 /* Prepare for possible retry.*/
1826 cur_pkts
= &cur_pkts
[tx_pkts
];
1828 /* No packets sent - do not retry.*/
1831 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1833 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1835 rte_spinlock_lock(&dev
->stats_lock
);
1836 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1838 rte_spinlock_unlock(&dev
->stats_lock
);
1841 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1842 dp_packet_delete(pkts
[i
]);
1846 /* Tx function. Transmit packets indefinitely */
1848 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1849 OVS_NO_THREAD_SAFETY_ANALYSIS
1851 const size_t batch_cnt
= dp_packet_batch_size(batch
);
1852 #if !defined(__CHECKER__) && !defined(_WIN32)
1853 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
1855 /* Sparse or MSVC doesn't like variable length array. */
1856 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1858 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1859 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1860 uint32_t cnt
= batch_cnt
;
1861 uint32_t dropped
= 0;
1863 if (dev
->type
!= DPDK_DEV_VHOST
) {
1864 /* Check if QoS has been configured for this netdev. */
1865 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
1867 dropped
+= batch_cnt
- cnt
;
1870 dp_packet_batch_apply_cutlen(batch
);
1874 for (uint32_t i
= 0; i
< cnt
; i
++) {
1875 struct dp_packet
*packet
= batch
->packets
[i
];
1876 uint32_t size
= dp_packet_size(packet
);
1878 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1879 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
1880 size
, dev
->max_packet_len
);
1886 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1887 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
1892 /* We have to do a copy for now */
1893 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
1894 dp_packet_data(packet
), size
);
1895 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
1900 if (OVS_LIKELY(txcnt
)) {
1901 if (dev
->type
== DPDK_DEV_VHOST
) {
1902 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1905 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
1909 if (OVS_UNLIKELY(dropped
)) {
1910 rte_spinlock_lock(&dev
->stats_lock
);
1911 dev
->stats
.tx_dropped
+= dropped
;
1912 rte_spinlock_unlock(&dev
->stats_lock
);
1917 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1918 struct dp_packet_batch
*batch
,
1919 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1922 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1923 dpdk_do_tx_copy(netdev
, qid
, batch
);
1924 dp_packet_delete_batch(batch
, may_steal
);
1926 dp_packet_batch_apply_cutlen(batch
);
1927 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1933 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1934 struct dp_packet_batch
*batch
, bool may_steal
,
1935 bool concurrent_txq
)
1937 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1938 dp_packet_delete_batch(batch
, may_steal
);
1942 if (OVS_UNLIKELY(concurrent_txq
)) {
1943 qid
= qid
% dev
->up
.n_txq
;
1944 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1947 if (OVS_UNLIKELY(!may_steal
||
1948 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1949 struct netdev
*netdev
= &dev
->up
;
1951 dpdk_do_tx_copy(netdev
, qid
, batch
);
1952 dp_packet_delete_batch(batch
, may_steal
);
1954 int tx_cnt
, dropped
;
1955 int batch_cnt
= dp_packet_batch_size(batch
);
1956 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1958 dp_packet_batch_apply_cutlen(batch
);
1960 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
1961 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
1962 dropped
= batch_cnt
- tx_cnt
;
1964 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
1966 if (OVS_UNLIKELY(dropped
)) {
1967 rte_spinlock_lock(&dev
->stats_lock
);
1968 dev
->stats
.tx_dropped
+= dropped
;
1969 rte_spinlock_unlock(&dev
->stats_lock
);
1973 if (OVS_UNLIKELY(concurrent_txq
)) {
1974 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1979 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1980 struct dp_packet_batch
*batch
, bool may_steal
,
1981 bool concurrent_txq
)
1983 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1985 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1990 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1992 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1994 ovs_mutex_lock(&dev
->mutex
);
1995 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1997 netdev_change_seq_changed(netdev
);
1999 ovs_mutex_unlock(&dev
->mutex
);
2005 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
2007 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2009 ovs_mutex_lock(&dev
->mutex
);
2011 ovs_mutex_unlock(&dev
->mutex
);
2017 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
2019 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2021 ovs_mutex_lock(&dev
->mutex
);
2023 ovs_mutex_unlock(&dev
->mutex
);
2029 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2031 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2033 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2034 || mtu
< ETHER_MIN_MTU
) {
2035 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2039 ovs_mutex_lock(&dev
->mutex
);
2040 if (dev
->requested_mtu
!= mtu
) {
2041 dev
->requested_mtu
= mtu
;
2042 netdev_request_reconfigure(netdev
);
2044 ovs_mutex_unlock(&dev
->mutex
);
2050 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2053 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2054 struct netdev_stats
*stats
)
2056 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2058 ovs_mutex_lock(&dev
->mutex
);
2060 rte_spinlock_lock(&dev
->stats_lock
);
2061 /* Supported Stats */
2062 stats
->rx_packets
= dev
->stats
.rx_packets
;
2063 stats
->tx_packets
= dev
->stats
.tx_packets
;
2064 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2065 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2066 stats
->multicast
= dev
->stats
.multicast
;
2067 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2068 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2069 stats
->rx_errors
= dev
->stats
.rx_errors
;
2070 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2072 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2073 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2074 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2075 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2076 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2077 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2078 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2080 rte_spinlock_unlock(&dev
->stats_lock
);
2082 ovs_mutex_unlock(&dev
->mutex
);
2088 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2089 const struct rte_eth_xstat
*xstats
,
2090 const struct rte_eth_xstat_name
*names
,
2091 const unsigned int size
)
2093 for (unsigned int i
= 0; i
< size
; i
++) {
2094 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2095 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2096 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2097 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2098 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2099 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2100 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2101 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2102 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2103 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2104 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2105 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2106 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2107 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2108 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2109 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2110 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2111 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2112 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2113 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2114 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2115 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2116 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2117 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2118 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2119 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2120 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2121 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2122 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2123 stats
->multicast
= xstats
[i
].value
;
2124 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2125 stats
->tx_multicast_packets
= xstats
[i
].value
;
2126 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2127 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2128 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2129 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2130 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2131 stats
->rx_undersized_errors
= xstats
[i
].value
;
2132 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2133 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2134 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2135 stats
->rx_jabber_errors
= xstats
[i
].value
;
2141 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2143 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2144 struct rte_eth_stats rte_stats
;
2147 netdev_dpdk_get_carrier(netdev
, &gg
);
2148 ovs_mutex_lock(&dev
->mutex
);
2150 struct rte_eth_xstat
*rte_xstats
= NULL
;
2151 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2152 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2154 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2155 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2156 ovs_mutex_unlock(&dev
->mutex
);
2160 /* Get length of statistics */
2161 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2162 if (rte_xstats_len
< 0) {
2163 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2166 /* Reserve memory for xstats names and values */
2167 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2168 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2170 /* Retreive xstats names */
2171 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2174 if (rte_xstats_new_len
!= rte_xstats_len
) {
2175 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2178 /* Retreive xstats values */
2179 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2180 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2182 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2183 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2186 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2191 free(rte_xstats_names
);
2193 stats
->rx_packets
= rte_stats
.ipackets
;
2194 stats
->tx_packets
= rte_stats
.opackets
;
2195 stats
->rx_bytes
= rte_stats
.ibytes
;
2196 stats
->tx_bytes
= rte_stats
.obytes
;
2197 stats
->rx_errors
= rte_stats
.ierrors
;
2198 stats
->tx_errors
= rte_stats
.oerrors
;
2200 rte_spinlock_lock(&dev
->stats_lock
);
2201 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2202 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2203 rte_spinlock_unlock(&dev
->stats_lock
);
2205 /* These are the available DPDK counters for packets not received due to
2206 * local resource constraints in DPDK and NIC respectively. */
2207 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2208 stats
->rx_missed_errors
= rte_stats
.imissed
;
2210 ovs_mutex_unlock(&dev
->mutex
);
2216 netdev_dpdk_get_features(const struct netdev
*netdev
,
2217 enum netdev_features
*current
,
2218 enum netdev_features
*advertised
,
2219 enum netdev_features
*supported
,
2220 enum netdev_features
*peer
)
2222 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2223 struct rte_eth_link link
;
2225 ovs_mutex_lock(&dev
->mutex
);
2227 ovs_mutex_unlock(&dev
->mutex
);
2229 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2230 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2231 *current
= NETDEV_F_10MB_HD
;
2233 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2234 *current
= NETDEV_F_100MB_HD
;
2236 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2237 *current
= NETDEV_F_1GB_HD
;
2239 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2240 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2241 *current
= NETDEV_F_10MB_FD
;
2243 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2244 *current
= NETDEV_F_100MB_FD
;
2246 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2247 *current
= NETDEV_F_1GB_FD
;
2249 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2250 *current
= NETDEV_F_10GB_FD
;
2254 if (link
.link_autoneg
) {
2255 *current
|= NETDEV_F_AUTONEG
;
2258 *advertised
= *supported
= *peer
= 0;
2263 static struct ingress_policer
*
2264 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2266 struct ingress_policer
*policer
= NULL
;
2267 uint64_t rate_bytes
;
2268 uint64_t burst_bytes
;
2271 policer
= xmalloc(sizeof *policer
);
2272 rte_spinlock_init(&policer
->policer_lock
);
2274 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2275 rate_bytes
= rate
* 1000ULL / 8;
2276 burst_bytes
= burst
* 1000ULL / 8;
2278 policer
->app_srtcm_params
.cir
= rate_bytes
;
2279 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2280 policer
->app_srtcm_params
.ebs
= 0;
2281 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2282 &policer
->app_srtcm_params
);
2284 VLOG_ERR("Could not create rte meter for ingress policer");
2292 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2293 uint32_t policer_burst
)
2295 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2296 struct ingress_policer
*policer
;
2298 /* Force to 0 if no rate specified,
2299 * default to 8000 kbits if burst is 0,
2300 * else stick with user-specified value.
2302 policer_burst
= (!policer_rate
? 0
2303 : !policer_burst
? 8000
2306 ovs_mutex_lock(&dev
->mutex
);
2308 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2309 &dev
->ingress_policer
);
2311 if (dev
->policer_rate
== policer_rate
&&
2312 dev
->policer_burst
== policer_burst
) {
2313 /* Assume that settings haven't changed since we last set them. */
2314 ovs_mutex_unlock(&dev
->mutex
);
2318 /* Destroy any existing ingress policer for the device if one exists */
2320 ovsrcu_postpone(free
, policer
);
2323 if (policer_rate
!= 0) {
2324 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2328 ovsrcu_set(&dev
->ingress_policer
, policer
);
2329 dev
->policer_rate
= policer_rate
;
2330 dev
->policer_burst
= policer_burst
;
2331 ovs_mutex_unlock(&dev
->mutex
);
2337 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2339 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2341 ovs_mutex_lock(&dev
->mutex
);
2342 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2343 * postive integer to meet RFC 2863 recommendations.
2345 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2346 ovs_mutex_unlock(&dev
->mutex
);
2352 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2354 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2356 ovs_mutex_lock(&dev
->mutex
);
2357 check_link_status(dev
);
2358 *carrier
= dev
->link
.link_status
;
2360 ovs_mutex_unlock(&dev
->mutex
);
2366 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2368 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2370 ovs_mutex_lock(&dev
->mutex
);
2372 if (is_vhost_running(dev
)) {
2378 ovs_mutex_unlock(&dev
->mutex
);
2383 static long long int
2384 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2386 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2387 long long int carrier_resets
;
2389 ovs_mutex_lock(&dev
->mutex
);
2390 carrier_resets
= dev
->link_reset_cnt
;
2391 ovs_mutex_unlock(&dev
->mutex
);
2393 return carrier_resets
;
2397 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2398 long long int interval OVS_UNUSED
)
2404 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2405 enum netdev_flags off
, enum netdev_flags on
,
2406 enum netdev_flags
*old_flagsp
)
2407 OVS_REQUIRES(dev
->mutex
)
2409 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2413 *old_flagsp
= dev
->flags
;
2417 if (dev
->flags
== *old_flagsp
) {
2421 if (dev
->type
== DPDK_DEV_ETH
) {
2422 if (dev
->flags
& NETDEV_PROMISC
) {
2423 rte_eth_promiscuous_enable(dev
->port_id
);
2426 netdev_change_seq_changed(&dev
->up
);
2428 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2429 * running then change netdev's change_seq to trigger link state
2432 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2433 && is_vhost_running(dev
)) {
2434 netdev_change_seq_changed(&dev
->up
);
2436 /* Clear statistics if device is getting up. */
2437 if (NETDEV_UP
& on
) {
2438 rte_spinlock_lock(&dev
->stats_lock
);
2439 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2440 rte_spinlock_unlock(&dev
->stats_lock
);
2449 netdev_dpdk_update_flags(struct netdev
*netdev
,
2450 enum netdev_flags off
, enum netdev_flags on
,
2451 enum netdev_flags
*old_flagsp
)
2453 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2456 ovs_mutex_lock(&dev
->mutex
);
2457 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2458 ovs_mutex_unlock(&dev
->mutex
);
2464 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2466 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2467 struct rte_eth_dev_info dev_info
;
2469 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2473 ovs_mutex_lock(&dev
->mutex
);
2474 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2475 ovs_mutex_unlock(&dev
->mutex
);
2477 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2478 smap_add_format(args
, "numa_id", "%d",
2479 rte_eth_dev_socket_id(dev
->port_id
));
2480 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2481 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2482 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2483 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2484 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2485 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2486 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2487 dev_info
.max_hash_mac_addrs
);
2488 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2489 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2491 if (dev_info
.pci_dev
) {
2492 smap_add_format(args
, "pci-vendor_id", "0x%u",
2493 dev_info
.pci_dev
->id
.vendor_id
);
2494 smap_add_format(args
, "pci-device_id", "0x%x",
2495 dev_info
.pci_dev
->id
.device_id
);
2502 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2503 OVS_REQUIRES(dev
->mutex
)
2505 enum netdev_flags old_flags
;
2508 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2510 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2515 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2516 const char *argv
[], void *aux OVS_UNUSED
)
2520 if (!strcasecmp(argv
[argc
- 1], "up")) {
2522 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2525 unixctl_command_reply_error(conn
, "Invalid Admin State");
2530 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2531 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2532 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2534 ovs_mutex_lock(&dpdk_dev
->mutex
);
2535 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2536 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2538 netdev_close(netdev
);
2540 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2541 netdev_close(netdev
);
2545 struct netdev_dpdk
*netdev
;
2547 ovs_mutex_lock(&dpdk_mutex
);
2548 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2549 ovs_mutex_lock(&netdev
->mutex
);
2550 netdev_dpdk_set_admin_state__(netdev
, up
);
2551 ovs_mutex_unlock(&netdev
->mutex
);
2553 ovs_mutex_unlock(&dpdk_mutex
);
2555 unixctl_command_reply(conn
, "OK");
2559 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2560 const char *argv
[], void *aux OVS_UNUSED
)
2565 char devname
[RTE_ETH_NAME_MAX_LEN
];
2566 struct netdev_dpdk
*dev
;
2568 ovs_mutex_lock(&dpdk_mutex
);
2570 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv
[1],
2572 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2576 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2578 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2579 "Remove it before detaching",
2580 argv
[1], netdev_get_name(&dev
->up
));
2584 rte_eth_dev_close(port_id
);
2586 ret
= rte_eth_dev_detach(port_id
, devname
);
2588 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2592 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2594 ovs_mutex_unlock(&dpdk_mutex
);
2595 unixctl_command_reply(conn
, response
);
2600 ovs_mutex_unlock(&dpdk_mutex
);
2601 unixctl_command_reply_error(conn
, response
);
2606 * Set virtqueue flags so that we do not receive interrupts.
2609 set_irq_status(int vid
)
2613 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2614 rte_vhost_enable_guest_notification(vid
, i
, 0);
2619 * Fixes mapping for vhost-user tx queues. Must be called after each
2620 * enabling/disabling of queues and n_txq modifications.
2623 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2624 OVS_REQUIRES(dev
->mutex
)
2626 int *enabled_queues
, n_enabled
= 0;
2627 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2629 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2631 for (i
= 0; i
< total_txqs
; i
++) {
2632 /* Enabled queues always mapped to themselves. */
2633 if (dev
->tx_q
[i
].map
== i
) {
2634 enabled_queues
[n_enabled
++] = i
;
2638 if (n_enabled
== 0 && total_txqs
!= 0) {
2639 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2644 for (i
= 0; i
< total_txqs
; i
++) {
2645 if (dev
->tx_q
[i
].map
!= i
) {
2646 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2647 k
= (k
+ 1) % n_enabled
;
2651 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2652 for (i
= 0; i
< total_txqs
; i
++) {
2653 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2656 free(enabled_queues
);
2660 * A new virtio-net device is added to a vhost port.
2665 struct netdev_dpdk
*dev
;
2666 bool exists
= false;
2668 char ifname
[IF_NAME_SZ
];
2670 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2672 ovs_mutex_lock(&dpdk_mutex
);
2673 /* Add device to the vhost port with the same name as that passed down. */
2674 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2675 ovs_mutex_lock(&dev
->mutex
);
2676 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2677 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
2679 /* Get NUMA information */
2680 newnode
= rte_vhost_get_numa_node(vid
);
2681 if (newnode
== -1) {
2683 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2686 newnode
= dev
->socket_id
;
2689 if (dev
->requested_n_txq
!= qp_num
2690 || dev
->requested_n_rxq
!= qp_num
2691 || dev
->requested_socket_id
!= newnode
) {
2692 dev
->requested_socket_id
= newnode
;
2693 dev
->requested_n_rxq
= qp_num
;
2694 dev
->requested_n_txq
= qp_num
;
2695 netdev_request_reconfigure(&dev
->up
);
2697 /* Reconfiguration not required. */
2698 dev
->vhost_reconfigured
= true;
2701 ovsrcu_index_set(&dev
->vid
, vid
);
2704 /* Disable notifications. */
2705 set_irq_status(vid
);
2706 netdev_change_seq_changed(&dev
->up
);
2707 ovs_mutex_unlock(&dev
->mutex
);
2710 ovs_mutex_unlock(&dev
->mutex
);
2712 ovs_mutex_unlock(&dpdk_mutex
);
2715 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2720 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2726 /* Clears mapping for all available queues of vhost interface. */
2728 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2729 OVS_REQUIRES(dev
->mutex
)
2733 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2734 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2739 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2740 * flag to stop any more packets from being sent or received to/from a VM and
2741 * ensure all currently queued packets have been sent/received before removing
2745 destroy_device(int vid
)
2747 struct netdev_dpdk
*dev
;
2748 bool exists
= false;
2749 char ifname
[IF_NAME_SZ
];
2751 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2753 ovs_mutex_lock(&dpdk_mutex
);
2754 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2755 if (netdev_dpdk_get_vid(dev
) == vid
) {
2757 ovs_mutex_lock(&dev
->mutex
);
2758 dev
->vhost_reconfigured
= false;
2759 ovsrcu_index_set(&dev
->vid
, -1);
2760 netdev_dpdk_txq_map_clear(dev
);
2762 netdev_change_seq_changed(&dev
->up
);
2763 ovs_mutex_unlock(&dev
->mutex
);
2769 ovs_mutex_unlock(&dpdk_mutex
);
2773 * Wait for other threads to quiesce after setting the 'virtio_dev'
2774 * to NULL, before returning.
2776 ovsrcu_synchronize();
2778 * As call to ovsrcu_synchronize() will end the quiescent state,
2779 * put thread back into quiescent state before returning.
2781 ovsrcu_quiesce_start();
2782 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2784 VLOG_INFO("vHost Device '%s' not found", ifname
);
2789 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2791 struct netdev_dpdk
*dev
;
2792 bool exists
= false;
2793 int qid
= queue_id
/ VIRTIO_QNUM
;
2794 char ifname
[IF_NAME_SZ
];
2796 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2798 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2802 ovs_mutex_lock(&dpdk_mutex
);
2803 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2804 ovs_mutex_lock(&dev
->mutex
);
2805 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2807 dev
->tx_q
[qid
].map
= qid
;
2809 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2811 netdev_dpdk_remap_txqs(dev
);
2813 ovs_mutex_unlock(&dev
->mutex
);
2816 ovs_mutex_unlock(&dev
->mutex
);
2818 ovs_mutex_unlock(&dpdk_mutex
);
2821 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2822 "changed to \'%s\'", queue_id
, qid
, ifname
,
2823 (enable
== 1) ? "enabled" : "disabled");
2825 VLOG_INFO("vHost Device '%s' not found", ifname
);
2833 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2835 return ovsrcu_index_get(&dev
->vid
);
2838 struct ingress_policer
*
2839 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2841 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2845 netdev_dpdk_class_init(void)
2847 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2849 /* This function can be called for different classes. The initialization
2850 * needs to be done only once */
2851 if (ovsthread_once_start(&once
)) {
2852 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2853 unixctl_command_register("netdev-dpdk/set-admin-state",
2854 "[netdev] up|down", 1, 2,
2855 netdev_dpdk_set_admin_state
, NULL
);
2857 unixctl_command_register("netdev-dpdk/detach",
2858 "pci address of device", 1, 1,
2859 netdev_dpdk_detach
, NULL
);
2861 ovsthread_once_done(&once
);
2871 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2872 dpdk_port_t
*eth_port_id
)
2874 struct dpdk_ring
*ring_pair
;
2878 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2883 /* XXX: Add support for multiquque ring. */
2884 ring_name
= xasprintf("%s_tx", dev_name
);
2886 /* Create single producer tx ring, netdev does explicit locking. */
2887 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2890 if (ring_pair
->cring_tx
== NULL
) {
2891 rte_free(ring_pair
);
2895 ring_name
= xasprintf("%s_rx", dev_name
);
2897 /* Create single consumer rx ring, netdev does explicit locking. */
2898 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2901 if (ring_pair
->cring_rx
== NULL
) {
2902 rte_free(ring_pair
);
2906 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2907 &ring_pair
->cring_tx
, 1, SOCKET0
);
2910 rte_free(ring_pair
);
2914 ring_pair
->user_port_id
= port_no
;
2915 ring_pair
->eth_port_id
= port_id
;
2916 *eth_port_id
= port_id
;
2918 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2924 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
2925 OVS_REQUIRES(dpdk_mutex
)
2927 struct dpdk_ring
*ring_pair
;
2928 unsigned int port_no
;
2931 /* Names always start with "dpdkr" */
2932 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2937 /* Look through our list to find the device */
2938 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2939 if (ring_pair
->user_port_id
== port_no
) {
2940 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2941 /* Really all that is needed */
2942 *eth_port_id
= ring_pair
->eth_port_id
;
2946 /* Need to create the device rings */
2947 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2951 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2952 struct dp_packet_batch
*batch
, bool may_steal
,
2953 bool concurrent_txq
)
2955 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2956 struct dp_packet
*packet
;
2958 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2959 * the rss hash field is clear. This is because the same mbuf may be
2960 * modified by the consumer of the ring and return into the datapath
2961 * without recalculating the RSS hash. */
2962 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
2963 dp_packet_mbuf_rss_flag_reset(packet
);
2966 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2971 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2973 dpdk_port_t port_no
= 0;
2976 ovs_mutex_lock(&dpdk_mutex
);
2978 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2983 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2984 rte_eth_dev_socket_id(port_no
));
2986 ovs_mutex_unlock(&dpdk_mutex
);
2993 * Initialize QoS configuration operations.
2996 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2999 rte_spinlock_init(&conf
->lock
);
3003 * Search existing QoS operations in qos_ops and compare each set of
3004 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
3007 static const struct dpdk_qos_ops
*
3008 qos_lookup_name(const char *name
)
3010 const struct dpdk_qos_ops
*const *opsp
;
3012 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3013 const struct dpdk_qos_ops
*ops
= *opsp
;
3014 if (!strcmp(name
, ops
->qos_name
)) {
3022 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3025 const struct dpdk_qos_ops
*const *opsp
;
3027 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3028 const struct dpdk_qos_ops
*ops
= *opsp
;
3029 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3030 sset_add(types
, ops
->qos_name
);
3037 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3038 const char **typep
, struct smap
*details
)
3040 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3041 struct qos_conf
*qos_conf
;
3044 ovs_mutex_lock(&dev
->mutex
);
3045 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3047 *typep
= qos_conf
->ops
->qos_name
;
3048 error
= (qos_conf
->ops
->qos_get
3049 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3051 /* No QoS configuration set, return an empty string */
3054 ovs_mutex_unlock(&dev
->mutex
);
3060 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3061 const struct smap
*details
)
3063 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3064 const struct dpdk_qos_ops
*new_ops
= NULL
;
3065 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3068 ovs_mutex_lock(&dev
->mutex
);
3070 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3072 new_ops
= qos_lookup_name(type
);
3074 if (!new_ops
|| !new_ops
->qos_construct
) {
3075 new_qos_conf
= NULL
;
3076 if (type
&& type
[0]) {
3079 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3080 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3081 new_qos_conf
= qos_conf
;
3083 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3087 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3088 type
, netdev
->name
, rte_strerror(error
));
3091 if (new_qos_conf
!= qos_conf
) {
3092 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3094 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3098 ovs_mutex_unlock(&dev
->mutex
);
3103 /* egress-policer details */
3105 struct egress_policer
{
3106 struct qos_conf qos_conf
;
3107 struct rte_meter_srtcm_params app_srtcm_params
;
3108 struct rte_meter_srtcm egress_meter
;
3112 egress_policer_details_to_param(const struct smap
*details
,
3113 struct rte_meter_srtcm_params
*params
)
3115 memset(params
, 0, sizeof *params
);
3116 params
->cir
= smap_get_ullong(details
, "cir", 0);
3117 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3122 egress_policer_qos_construct(const struct smap
*details
,
3123 struct qos_conf
**conf
)
3125 struct egress_policer
*policer
;
3128 policer
= xmalloc(sizeof *policer
);
3129 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3130 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3131 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3132 &policer
->app_srtcm_params
);
3134 *conf
= &policer
->qos_conf
;
3145 egress_policer_qos_destruct(struct qos_conf
*conf
)
3147 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3153 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3155 struct egress_policer
*policer
=
3156 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3158 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3159 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3165 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3166 const struct smap
*details
)
3168 struct egress_policer
*policer
=
3169 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3170 struct rte_meter_srtcm_params params
;
3172 egress_policer_details_to_param(details
, ¶ms
);
3174 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3178 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3182 struct egress_policer
*policer
=
3183 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3185 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3186 pkt_cnt
, may_steal
);
3191 static const struct dpdk_qos_ops egress_policer_ops
= {
3192 "egress-policer", /* qos_name */
3193 egress_policer_qos_construct
,
3194 egress_policer_qos_destruct
,
3195 egress_policer_qos_get
,
3196 egress_policer_qos_is_equal
,
3201 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3203 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3206 ovs_mutex_lock(&dev
->mutex
);
3208 if (netdev
->n_txq
== dev
->requested_n_txq
3209 && netdev
->n_rxq
== dev
->requested_n_rxq
3210 && dev
->mtu
== dev
->requested_mtu
3211 && dev
->rxq_size
== dev
->requested_rxq_size
3212 && dev
->txq_size
== dev
->requested_txq_size
3213 && dev
->socket_id
== dev
->requested_socket_id
) {
3214 /* Reconfiguration is unnecessary */
3219 rte_eth_dev_stop(dev
->port_id
);
3221 err
= netdev_dpdk_mempool_configure(dev
);
3222 if (err
&& err
!= EEXIST
) {
3226 netdev
->n_txq
= dev
->requested_n_txq
;
3227 netdev
->n_rxq
= dev
->requested_n_rxq
;
3229 dev
->rxq_size
= dev
->requested_rxq_size
;
3230 dev
->txq_size
= dev
->requested_txq_size
;
3232 rte_free(dev
->tx_q
);
3233 err
= dpdk_eth_dev_init(dev
);
3234 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3239 netdev_change_seq_changed(netdev
);
3242 ovs_mutex_unlock(&dev
->mutex
);
3247 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3248 OVS_REQUIRES(dev
->mutex
)
3250 dev
->up
.n_txq
= dev
->requested_n_txq
;
3251 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3254 /* Enable TX queue 0 by default if it wasn't disabled. */
3255 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3256 dev
->tx_q
[0].map
= 0;
3259 netdev_dpdk_remap_txqs(dev
);
3261 err
= netdev_dpdk_mempool_configure(dev
);
3263 /* A new mempool was created. */
3264 netdev_change_seq_changed(&dev
->up
);
3265 } else if (err
!= EEXIST
){
3268 if (netdev_dpdk_get_vid(dev
) >= 0) {
3269 if (dev
->vhost_reconfigured
== false) {
3270 dev
->vhost_reconfigured
= true;
3271 /* Carrier status may need updating. */
3272 netdev_change_seq_changed(&dev
->up
);
3280 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3282 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3285 ovs_mutex_lock(&dev
->mutex
);
3286 err
= dpdk_vhost_reconfigure_helper(dev
);
3287 ovs_mutex_unlock(&dev
->mutex
);
3293 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3295 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3298 ovs_mutex_lock(&dev
->mutex
);
3300 /* Configure vHost client mode if requested and if the following criteria
3302 * 1. Device hasn't been registered yet.
3303 * 2. A path has been specified.
3305 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3306 && strlen(dev
->vhost_id
)) {
3307 /* Register client-mode device */
3308 err
= rte_vhost_driver_register(dev
->vhost_id
,
3309 RTE_VHOST_USER_CLIENT
);
3311 VLOG_ERR("vhost-user device setup failure for device %s\n",
3315 /* Configuration successful */
3316 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3317 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3318 "using client socket '%s'",
3319 dev
->up
.name
, dev
->vhost_id
);
3322 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3323 &virtio_net_device_ops
);
3325 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3326 "vhost user client port: %s\n", dev
->up
.name
);
3330 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3331 1ULL << VIRTIO_NET_F_HOST_TSO4
3332 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3333 | 1ULL << VIRTIO_NET_F_CSUM
);
3335 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3336 "client port: %s\n", dev
->up
.name
);
3340 err
= rte_vhost_driver_start(dev
->vhost_id
);
3342 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3343 "client port: %s\n", dev
->up
.name
);
3348 err
= dpdk_vhost_reconfigure_helper(dev
);
3351 ovs_mutex_unlock(&dev
->mutex
);
3356 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3357 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3358 GET_CARRIER, GET_STATS, \
3359 GET_FEATURES, GET_STATUS, \
3360 RECONFIGURE, RXQ_RECV) \
3363 true, /* is_pmd */ \
3365 NULL, /* netdev_dpdk_run */ \
3366 NULL, /* netdev_dpdk_wait */ \
3368 netdev_dpdk_alloc, \
3371 netdev_dpdk_dealloc, \
3372 netdev_dpdk_get_config, \
3374 NULL, /* get_tunnel_config */ \
3375 NULL, /* build header */ \
3376 NULL, /* push header */ \
3377 NULL, /* pop header */ \
3378 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3382 NULL, /* send_wait */ \
3384 netdev_dpdk_set_etheraddr, \
3385 netdev_dpdk_get_etheraddr, \
3386 netdev_dpdk_get_mtu, \
3387 netdev_dpdk_set_mtu, \
3388 netdev_dpdk_get_ifindex, \
3390 netdev_dpdk_get_carrier_resets, \
3391 netdev_dpdk_set_miimon, \
3394 NULL, /* set_advertisements */ \
3395 NULL, /* get_pt_mode */ \
3397 netdev_dpdk_set_policing, \
3398 netdev_dpdk_get_qos_types, \
3399 NULL, /* get_qos_capabilities */ \
3400 netdev_dpdk_get_qos, \
3401 netdev_dpdk_set_qos, \
3402 NULL, /* get_queue */ \
3403 NULL, /* set_queue */ \
3404 NULL, /* delete_queue */ \
3405 NULL, /* get_queue_stats */ \
3406 NULL, /* queue_dump_start */ \
3407 NULL, /* queue_dump_next */ \
3408 NULL, /* queue_dump_done */ \
3409 NULL, /* dump_queue_stats */ \
3411 NULL, /* set_in4 */ \
3412 NULL, /* get_addr_list */ \
3413 NULL, /* add_router */ \
3414 NULL, /* get_next_hop */ \
3416 NULL, /* arp_lookup */ \
3418 netdev_dpdk_update_flags, \
3421 netdev_dpdk_rxq_alloc, \
3422 netdev_dpdk_rxq_construct, \
3423 netdev_dpdk_rxq_destruct, \
3424 netdev_dpdk_rxq_dealloc, \
3426 NULL, /* rx_wait */ \
3427 NULL, /* rxq_drain */ \
3431 static const struct netdev_class dpdk_class
=
3434 netdev_dpdk_class_init
,
3435 netdev_dpdk_construct
,
3436 netdev_dpdk_destruct
,
3437 netdev_dpdk_set_config
,
3438 netdev_dpdk_set_tx_multiq
,
3439 netdev_dpdk_eth_send
,
3440 netdev_dpdk_get_carrier
,
3441 netdev_dpdk_get_stats
,
3442 netdev_dpdk_get_features
,
3443 netdev_dpdk_get_status
,
3444 netdev_dpdk_reconfigure
,
3445 netdev_dpdk_rxq_recv
);
3447 static const struct netdev_class dpdk_ring_class
=
3450 netdev_dpdk_class_init
,
3451 netdev_dpdk_ring_construct
,
3452 netdev_dpdk_destruct
,
3453 netdev_dpdk_ring_set_config
,
3454 netdev_dpdk_set_tx_multiq
,
3455 netdev_dpdk_ring_send
,
3456 netdev_dpdk_get_carrier
,
3457 netdev_dpdk_get_stats
,
3458 netdev_dpdk_get_features
,
3459 netdev_dpdk_get_status
,
3460 netdev_dpdk_reconfigure
,
3461 netdev_dpdk_rxq_recv
);
3463 static const struct netdev_class dpdk_vhost_class
=
3467 netdev_dpdk_vhost_construct
,
3468 netdev_dpdk_vhost_destruct
,
3471 netdev_dpdk_vhost_send
,
3472 netdev_dpdk_vhost_get_carrier
,
3473 netdev_dpdk_vhost_get_stats
,
3476 netdev_dpdk_vhost_reconfigure
,
3477 netdev_dpdk_vhost_rxq_recv
);
3478 static const struct netdev_class dpdk_vhost_client_class
=
3480 "dpdkvhostuserclient",
3482 netdev_dpdk_vhost_client_construct
,
3483 netdev_dpdk_vhost_destruct
,
3484 netdev_dpdk_vhost_client_set_config
,
3486 netdev_dpdk_vhost_send
,
3487 netdev_dpdk_vhost_get_carrier
,
3488 netdev_dpdk_vhost_get_stats
,
3491 netdev_dpdk_vhost_client_reconfigure
,
3492 netdev_dpdk_vhost_rxq_recv
);
3495 netdev_dpdk_register(void)
3497 netdev_register_provider(&dpdk_class
);
3498 netdev_register_provider(&dpdk_ring_class
);
3499 netdev_register_provider(&dpdk_vhost_class
);
3500 netdev_register_provider(&dpdk_vhost_client_class
);