2 * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "netdev-dpdk.h"
25 #include <linux/virtio_net.h>
26 #include <sys/socket.h>
29 #include <rte_bus_pci.h>
30 #include <rte_config.h>
31 #include <rte_cycles.h>
32 #include <rte_errno.h>
33 #include <rte_eth_ring.h>
34 #include <rte_ethdev.h>
35 #include <rte_malloc.h>
37 #include <rte_meter.h>
39 #include <rte_vhost.h>
40 #include <rte_version.h>
43 #include "dp-packet.h"
45 #include "dpif-netdev.h"
46 #include "fatal-signal.h"
47 #include "netdev-provider.h"
48 #include "netdev-vport.h"
50 #include "openvswitch/dynamic-string.h"
51 #include "openvswitch/list.h"
52 #include "openvswitch/ofp-print.h"
53 #include "openvswitch/vlog.h"
55 #include "ovs-thread.h"
58 #include "openvswitch/shash.h"
61 #include "unaligned.h"
65 enum {VIRTIO_RXQ
, VIRTIO_TXQ
, VIRTIO_QNUM
};
67 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
68 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
70 #define DPDK_PORT_WATCHDOG_INTERVAL 5
72 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
73 #define OVS_VPORT_DPDK "ovs_dpdk"
76 * need to reserve tons of extra space in the mbufs so we can align the
77 * DMA addresses to 4KB.
78 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
79 * performance for standard Ethernet MTU.
81 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
82 + (2 * VLAN_HEADER_LEN))
83 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
84 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
85 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
86 - ETHER_HDR_LEN - ETHER_CRC_LEN)
87 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
88 + sizeof(struct dp_packet) \
89 + RTE_PKTMBUF_HEADROOM), \
91 #define NETDEV_DPDK_MBUF_ALIGN 1024
92 #define NETDEV_DPDK_MAX_PKT_LEN 9728
94 /* Min number of packets in the mempool. OVS tries to allocate a mempool with
95 * roughly estimated number of mbufs: if this fails (because the system doesn't
96 * have enough hugepages) we keep halving the number until the allocation
97 * succeeds or we reach MIN_NB_MBUF */
98 #define MIN_NB_MBUF (4096 * 4)
99 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
102 * DPDK XSTATS Counter names definition
104 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
105 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
106 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
107 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
108 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
109 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
110 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
112 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
113 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
114 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
115 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
116 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
117 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
118 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
120 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
121 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
122 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
123 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
124 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
125 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
126 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
127 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
131 /* Default size of Physical NIC RXQ */
132 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
133 /* Default size of Physical NIC TXQ */
134 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
135 /* Maximum size of Physical NIC Queues */
136 #define NIC_PORT_MAX_Q_SIZE 4096
138 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
139 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
140 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
141 * yet mapped to another queue. */
143 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
145 /* DPDK library uses uint16_t for port_id. */
146 typedef uint16_t dpdk_port_t
;
148 #define VHOST_ENQ_RETRY_NUM 8
149 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
151 static const struct rte_eth_conf port_conf
= {
153 .mq_mode
= ETH_MQ_RX_RSS
,
155 .header_split
= 0, /* Header Split disabled */
156 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
157 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
158 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
164 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
168 .mq_mode
= ETH_MQ_TX_NONE
,
173 * These callbacks allow virtio-net devices to be added to vhost ports when
174 * configuration has been fully completed.
176 static int new_device(int vid
);
177 static void destroy_device(int vid
);
178 static int vring_state_changed(int vid
, uint16_t queue_id
, int enable
);
179 static const struct vhost_device_ops virtio_net_device_ops
=
181 .new_device
= new_device
,
182 .destroy_device
= destroy_device
,
183 .vring_state_changed
= vring_state_changed
,
184 .features_changed
= NULL
187 enum { DPDK_RING_SIZE
= 256 };
188 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
189 enum { DRAIN_TSC
= 200000ULL };
196 /* Quality of Service */
198 /* An instance of a QoS configuration. Always associated with a particular
201 * Each QoS implementation subclasses this with whatever additional data it
205 const struct dpdk_qos_ops
*ops
;
209 /* A particular implementation of dpdk QoS operations.
211 * The functions below return 0 if successful or a positive errno value on
212 * failure, except where otherwise noted. All of them must be provided, except
213 * where otherwise noted.
215 struct dpdk_qos_ops
{
217 /* Name of the QoS type */
218 const char *qos_name
;
220 /* Called to construct a qos_conf object. The implementation should make
221 * the appropriate calls to configure QoS according to 'details'.
223 * The contents of 'details' should be documented as valid for 'ovs_name'
224 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
225 * (which is built as ovs-vswitchd.conf.db(8)).
227 * This function must return 0 if and only if it sets '*conf' to an
228 * initialized 'struct qos_conf'.
230 * For all QoS implementations it should always be non-null.
232 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
234 /* Destroys the data structures allocated by the implementation as part of
237 * For all QoS implementations it should always be non-null.
239 void (*qos_destruct
)(struct qos_conf
*conf
);
241 /* Retrieves details of 'conf' configuration into 'details'.
243 * The contents of 'details' should be documented as valid for 'ovs_name'
244 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
245 * (which is built as ovs-vswitchd.conf.db(8)).
247 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
249 /* Returns true if 'conf' is already configured according to 'details'.
251 * The contents of 'details' should be documented as valid for 'ovs_name'
252 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
253 * (which is built as ovs-vswitchd.conf.db(8)).
255 * For all QoS implementations it should always be non-null.
257 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
258 const struct smap
*details
);
260 /* Modify an array of rte_mbufs. The modification is specific to
261 * each qos implementation.
263 * The function should take and array of mbufs and an int representing
264 * the current number of mbufs present in the array.
266 * After the function has performed a qos modification to the array of
267 * mbufs it returns an int representing the number of mbufs now present in
268 * the array. This value is can then be passed to the port send function
269 * along with the modified array for transmission.
271 * For all QoS implementations it should always be non-null.
273 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
274 int pkt_cnt
, bool may_steal
);
277 /* dpdk_qos_ops for each type of user space QoS implementation */
278 static const struct dpdk_qos_ops egress_policer_ops
;
281 * Array of dpdk_qos_ops, contains pointer to all supported QoS
284 static const struct dpdk_qos_ops
*const qos_confs
[] = {
289 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
291 /* Contains all 'struct dpdk_dev's. */
292 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
293 = OVS_LIST_INITIALIZER(&dpdk_list
);
295 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
296 = OVS_MUTEX_INITIALIZER
;
298 /* There should be one 'struct dpdk_tx_queue' created for
300 struct dpdk_tx_queue
{
301 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
302 * from concurrent access. It is used only
303 * if the queue is shared among different
304 * pmd threads (see 'concurrent_txq'). */
305 int map
; /* Mapping of configured vhost-user queues
306 * to enabled by guest. */
309 /* dpdk has no way to remove dpdk ring ethernet devices
310 so we have to keep them around once they've been created
313 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
314 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
317 /* For the client rings */
318 struct rte_ring
*cring_tx
;
319 struct rte_ring
*cring_rx
;
320 unsigned int user_port_id
; /* User given port no, parsed from port name */
321 dpdk_port_t eth_port_id
; /* ethernet device port id */
322 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
325 struct ingress_policer
{
326 struct rte_meter_srtcm_params app_srtcm_params
;
327 struct rte_meter_srtcm in_policer
;
328 rte_spinlock_t policer_lock
;
331 enum dpdk_hw_ol_features
{
332 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
336 * In order to avoid confusion in variables names, following naming convention
337 * should be used, if possible:
339 * 'struct netdev' : 'netdev'
340 * 'struct netdev_dpdk' : 'dev'
341 * 'struct netdev_rxq' : 'rxq'
342 * 'struct netdev_rxq_dpdk' : 'rx'
345 * struct netdev *netdev = netdev_from_name(name);
346 * struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
348 * Also, 'netdev' should be used instead of 'dev->up', where 'netdev' was
353 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline0
,
356 /* If true, device was attached by rte_eth_dev_attach(). */
358 struct eth_addr hwaddr
;
363 enum dpdk_dev_type type
;
364 enum netdev_flags flags
;
365 char *devargs
; /* Device arguments for dpdk ports */
366 struct dpdk_tx_queue
*tx_q
;
367 struct rte_eth_link link
;
369 /* 4 pad bytes here. */
372 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline1
,
373 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
374 struct rte_mempool
*mp
;
376 /* virtio identifier for vhost devices */
379 /* True if vHost device is 'up' and has been reconfigured at least once */
380 bool vhost_reconfigured
;
381 /* 3 pad bytes here. */
384 PADDED_MEMBERS(CACHE_LINE_SIZE
,
385 /* Identifier used to distinguish vhost devices from each other. */
386 char vhost_id
[PATH_MAX
];
389 PADDED_MEMBERS(CACHE_LINE_SIZE
,
392 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
394 /* QoS configuration and lock for the device */
395 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
397 /* Ingress Policer */
398 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
399 uint32_t policer_rate
;
400 uint32_t policer_burst
;
403 PADDED_MEMBERS(CACHE_LINE_SIZE
,
404 struct netdev_stats stats
;
406 rte_spinlock_t stats_lock
;
407 /* 44 pad bytes here. */
410 PADDED_MEMBERS(CACHE_LINE_SIZE
,
411 /* The following properties cannot be changed when a device is running,
412 * so we remember the request and update them next time
413 * netdev_dpdk*_reconfigure() is called */
417 int requested_rxq_size
;
418 int requested_txq_size
;
420 /* Number of rx/tx descriptors for physical devices */
424 /* Socket ID detected when vHost device is brought up */
425 int requested_socket_id
;
427 /* Denotes whether vHost port is client/server mode */
428 uint64_t vhost_driver_flags
;
430 /* DPDK-ETH Flow control */
431 struct rte_eth_fc_conf fc_conf
;
433 /* DPDK-ETH hardware offload features,
434 * from the enum set 'dpdk_hw_ol_features' */
435 uint32_t hw_ol_features
;
438 PADDED_MEMBERS(CACHE_LINE_SIZE
,
439 /* Names of all XSTATS counters */
440 struct rte_eth_xstat_name
*rte_xstats_names
;
441 int rte_xstats_names_size
;
442 int rte_xstats_ids_size
;
443 uint64_t *rte_xstats_ids
;
447 struct netdev_rxq_dpdk
{
448 struct netdev_rxq up
;
452 static void netdev_dpdk_destruct(struct netdev
*netdev
);
453 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
455 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
457 struct ingress_policer
*
458 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
461 is_dpdk_class(const struct netdev_class
*class)
463 return class->destruct
== netdev_dpdk_destruct
464 || class->destruct
== netdev_dpdk_vhost_destruct
;
467 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
468 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
469 * value, insufficient buffers are allocated to accomodate the packet in its
470 * entirety. Furthermore, certain drivers need to ensure that there is also
471 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
472 * frames). If the RX buffer is too small, then the driver enables scatter RX
473 * behaviour, which reduces performance. To prevent this, use a buffer size
474 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
477 dpdk_buf_size(int mtu
)
479 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
480 NETDEV_DPDK_MBUF_ALIGN
);
483 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
485 * Unlike xmalloc(), this function can return NULL on failure. */
487 dpdk_rte_mzalloc(size_t sz
)
489 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
493 free_dpdk_buf(struct dp_packet
*p
)
495 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
497 rte_pktmbuf_free(pkt
);
501 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
502 void *opaque_arg OVS_UNUSED
,
504 unsigned i OVS_UNUSED
)
506 struct rte_mbuf
*pkt
= _p
;
508 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
511 /* Returns a valid pointer when either of the following is true:
512 * - a new mempool was just created;
513 * - a matching mempool already exists. */
514 static struct rte_mempool
*
515 dpdk_mp_create(struct netdev_dpdk
*dev
, int mtu
)
517 char mp_name
[RTE_MEMPOOL_NAMESIZE
];
518 const char *netdev_name
= netdev_get_name(&dev
->up
);
519 int socket_id
= dev
->requested_socket_id
;
521 uint32_t hash
= hash_string(netdev_name
, 0);
522 struct rte_mempool
*mp
= NULL
;
525 * XXX: rough estimation of number of mbufs required for this port:
526 * <packets required to fill the device rxqs>
527 * + <packets that could be stuck on other ports txqs>
528 * + <packets in the pmd threads>
529 * + <additional memory for corner cases>
531 n_mbufs
= dev
->requested_n_rxq
* dev
->requested_rxq_size
532 + dev
->requested_n_txq
* dev
->requested_txq_size
533 + MIN(RTE_MAX_LCORE
, dev
->requested_n_rxq
) * NETDEV_MAX_BURST
536 ovs_mutex_lock(&dpdk_mp_mutex
);
538 /* Full DPDK memory pool name must be unique and cannot be
539 * longer than RTE_MEMPOOL_NAMESIZE. */
540 int ret
= snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
,
541 "ovs%08x%02d%05d%07u",
542 hash
, socket_id
, mtu
, n_mbufs
);
543 if (ret
< 0 || ret
>= RTE_MEMPOOL_NAMESIZE
) {
544 VLOG_DBG("snprintf returned %d. "
545 "Failed to generate a mempool name for \"%s\". "
546 "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.",
547 ret
, netdev_name
, hash
, socket_id
, mtu
, n_mbufs
);
551 VLOG_DBG("Port %s: Requesting a mempool of %u mbufs "
552 "on socket %d for %d Rx and %d Tx queues.",
553 netdev_name
, n_mbufs
, socket_id
,
554 dev
->requested_n_rxq
, dev
->requested_n_txq
);
556 mp
= rte_pktmbuf_pool_create(mp_name
, n_mbufs
, MP_CACHE_SZ
,
557 sizeof (struct dp_packet
) - sizeof (struct rte_mbuf
),
558 MBUF_SIZE(mtu
) - sizeof(struct dp_packet
), socket_id
);
561 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
563 /* rte_pktmbuf_pool_create has done some initialization of the
564 * rte_mbuf part of each dp_packet. Some OvS specific fields
565 * of the packet still need to be initialized by
566 * ovs_rte_pktmbuf_init. */
567 rte_mempool_obj_iter(mp
, ovs_rte_pktmbuf_init
, NULL
);
568 } else if (rte_errno
== EEXIST
) {
569 /* A mempool with the same name already exists. We just
570 * retrieve its pointer to be returned to the caller. */
571 mp
= rte_mempool_lookup(mp_name
);
572 /* As the mempool create returned EEXIST we can expect the
573 * lookup has returned a valid pointer. If for some reason
574 * that's not the case we keep track of it. */
575 VLOG_DBG("A mempool with name \"%s\" already exists at %p.",
578 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
581 } while (!mp
&& rte_errno
== ENOMEM
&& (n_mbufs
/= 2) >= MIN_NB_MBUF
);
583 ovs_mutex_unlock(&dpdk_mp_mutex
);
587 /* Release an existing mempool. */
589 dpdk_mp_free(struct rte_mempool
*mp
)
595 ovs_mutex_lock(&dpdk_mp_mutex
);
596 VLOG_DBG("Releasing \"%s\" mempool", mp
->name
);
597 rte_mempool_free(mp
);
598 ovs_mutex_unlock(&dpdk_mp_mutex
);
601 /* Tries to allocate a new mempool - or re-use an existing one where
602 * appropriate - on requested_socket_id with a size determined by
603 * requested_mtu and requested Rx/Tx queues.
604 * On success - or when re-using an existing mempool - the new configuration
606 * On error, device will be left unchanged. */
608 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
609 OVS_REQUIRES(dev
->mutex
)
611 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
612 struct rte_mempool
*mp
;
615 mp
= dpdk_mp_create(dev
, FRAME_LEN_TO_MTU(buf_size
));
617 VLOG_ERR("Failed to create memory pool for netdev "
618 "%s, with MTU %d on socket %d: %s\n",
619 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
620 rte_strerror(rte_errno
));
623 /* If a new MTU was requested and its rounded value equals the one
624 * that is currently used, then the existing mempool is returned. */
626 /* A new mempool was created, release the previous one. */
627 dpdk_mp_free(dev
->mp
);
632 dev
->mtu
= dev
->requested_mtu
;
633 dev
->socket_id
= dev
->requested_socket_id
;
634 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
641 check_link_status(struct netdev_dpdk
*dev
)
643 struct rte_eth_link link
;
645 rte_eth_link_get_nowait(dev
->port_id
, &link
);
647 if (dev
->link
.link_status
!= link
.link_status
) {
648 netdev_change_seq_changed(&dev
->up
);
650 dev
->link_reset_cnt
++;
652 if (dev
->link
.link_status
) {
653 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
654 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
655 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
656 ("full-duplex") : ("half-duplex"));
658 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
664 dpdk_watchdog(void *dummy OVS_UNUSED
)
666 struct netdev_dpdk
*dev
;
668 pthread_detach(pthread_self());
671 ovs_mutex_lock(&dpdk_mutex
);
672 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
673 ovs_mutex_lock(&dev
->mutex
);
674 if (dev
->type
== DPDK_DEV_ETH
) {
675 check_link_status(dev
);
677 ovs_mutex_unlock(&dev
->mutex
);
679 ovs_mutex_unlock(&dpdk_mutex
);
680 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
687 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
691 struct rte_eth_conf conf
= port_conf
;
693 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
695 if (dev
->mtu
> ETHER_MTU
) {
696 conf
.rxmode
.enable_scatter
= 1;
699 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
700 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
701 /* A device may report more queues than it makes available (this has
702 * been observed for Intel xl710, which reserves some of them for
703 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
704 * available. When this happens we can retry the configuration
705 * and request less queues */
706 while (n_rxq
&& n_txq
) {
708 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
711 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
713 VLOG_WARN("Interface %s eth_dev setup error %s\n",
714 dev
->up
.name
, rte_strerror(-diag
));
718 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
720 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
721 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
725 for (i
= 0; i
< n_txq
; i
++) {
726 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
727 dev
->socket_id
, NULL
);
729 VLOG_INFO("Interface %s txq(%d) setup error: %s",
730 dev
->up
.name
, i
, rte_strerror(-diag
));
736 /* Retry with less tx queues */
741 for (i
= 0; i
< n_rxq
; i
++) {
742 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
743 dev
->socket_id
, NULL
, dev
->mp
);
745 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
746 dev
->up
.name
, i
, rte_strerror(-diag
));
752 /* Retry with less rx queues */
757 dev
->up
.n_rxq
= n_rxq
;
758 dev
->up
.n_txq
= n_txq
;
767 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
769 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
770 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
776 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
777 OVS_REQUIRES(dev
->mutex
)
779 struct rte_pktmbuf_pool_private
*mbp_priv
;
780 struct rte_eth_dev_info info
;
781 struct ether_addr eth_addr
;
784 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
785 DEV_RX_OFFLOAD_TCP_CKSUM
|
786 DEV_RX_OFFLOAD_IPV4_CKSUM
;
788 rte_eth_dev_info_get(dev
->port_id
, &info
);
790 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
791 rx_chksm_offload_capa
) {
792 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
794 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
796 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
799 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
800 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
802 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
804 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
805 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
809 diag
= rte_eth_dev_start(dev
->port_id
);
811 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
812 rte_strerror(-diag
));
816 rte_eth_promiscuous_enable(dev
->port_id
);
817 rte_eth_allmulticast_enable(dev
->port_id
);
819 memset(ð_addr
, 0x0, sizeof(eth_addr
));
820 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
821 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
822 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
824 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
825 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
827 mbp_priv
= rte_mempool_get_priv(dev
->mp
);
828 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
830 /* Get the Flow control configuration for DPDK-ETH */
831 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
833 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
840 static struct netdev_dpdk
*
841 netdev_dpdk_cast(const struct netdev
*netdev
)
843 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
846 static struct netdev
*
847 netdev_dpdk_alloc(void)
849 struct netdev_dpdk
*dev
;
851 dev
= dpdk_rte_mzalloc(sizeof *dev
);
859 static struct dpdk_tx_queue
*
860 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
862 struct dpdk_tx_queue
*txqs
;
865 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
867 for (i
= 0; i
< n_txqs
; i
++) {
868 /* Initialize map for vhost devices. */
869 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
870 rte_spinlock_init(&txqs
[i
].tx_lock
);
878 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
879 enum dpdk_dev_type type
, int socket_id
)
880 OVS_REQUIRES(dpdk_mutex
)
882 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
884 ovs_mutex_init(&dev
->mutex
);
886 rte_spinlock_init(&dev
->stats_lock
);
888 /* If the 'sid' is negative, it means that the kernel fails
889 * to obtain the pci numa info. In that situation, always
891 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
892 dev
->requested_socket_id
= dev
->socket_id
;
893 dev
->port_id
= port_no
;
896 dev
->requested_mtu
= ETHER_MTU
;
897 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
898 ovsrcu_index_init(&dev
->vid
, -1);
899 dev
->vhost_reconfigured
= false;
900 dev
->attached
= false;
902 ovsrcu_init(&dev
->qos_conf
, NULL
);
904 ovsrcu_init(&dev
->ingress_policer
, NULL
);
905 dev
->policer_rate
= 0;
906 dev
->policer_burst
= 0;
910 dev
->requested_n_rxq
= NR_QUEUE
;
911 dev
->requested_n_txq
= NR_QUEUE
;
912 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
913 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
915 /* Initialize the flow control to NULL */
916 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
918 /* Initilize the hardware offload flags to 0 */
919 dev
->hw_ol_features
= 0;
921 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
923 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
925 netdev_request_reconfigure(netdev
);
927 dev
->rte_xstats_names
= NULL
;
928 dev
->rte_xstats_names_size
= 0;
930 dev
->rte_xstats_ids
= NULL
;
931 dev
->rte_xstats_ids_size
= 0;
936 /* dev_name must be the prefix followed by a positive decimal number.
937 * (no leading + or - signs are allowed) */
939 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
940 unsigned int *port_no
)
944 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
948 cport
= dev_name
+ strlen(prefix
);
950 if (str_to_uint(cport
, 10, port_no
)) {
958 vhost_common_construct(struct netdev
*netdev
)
959 OVS_REQUIRES(dpdk_mutex
)
961 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
962 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
964 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
969 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
970 DPDK_DEV_VHOST
, socket_id
);
974 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
976 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
977 const char *name
= netdev
->name
;
980 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
981 * the file system. '/' or '\' would traverse directories, so they're not
982 * acceptable in 'name'. */
983 if (strchr(name
, '/') || strchr(name
, '\\')) {
984 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
985 "A valid name must not include '/' or '\\'",
990 ovs_mutex_lock(&dpdk_mutex
);
991 /* Take the name of the vhost-user port and append it to the location where
992 * the socket is to be created, then register the socket.
994 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
995 dpdk_get_vhost_sock_dir(), name
);
997 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
998 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
1000 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
1004 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
1005 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1006 dev
->vhost_id
, name
);
1009 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
1010 &virtio_net_device_ops
);
1012 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1013 "port: %s\n", name
);
1017 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1018 1ULL << VIRTIO_NET_F_HOST_TSO4
1019 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1020 | 1ULL << VIRTIO_NET_F_CSUM
);
1022 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1023 "port: %s\n", name
);
1027 err
= rte_vhost_driver_start(dev
->vhost_id
);
1029 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1030 "port: %s\n", name
);
1034 err
= vhost_common_construct(netdev
);
1036 VLOG_ERR("vhost_common_construct failed for vhost user "
1037 "port: %s\n", name
);
1041 ovs_mutex_unlock(&dpdk_mutex
);
1042 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1043 "please migrate to dpdkvhostuserclient ports.");
1048 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1052 ovs_mutex_lock(&dpdk_mutex
);
1053 err
= vhost_common_construct(netdev
);
1055 VLOG_ERR("vhost_common_construct failed for vhost user client"
1056 "port: %s\n", netdev
->name
);
1058 ovs_mutex_unlock(&dpdk_mutex
);
1063 netdev_dpdk_construct(struct netdev
*netdev
)
1067 ovs_mutex_lock(&dpdk_mutex
);
1068 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1069 DPDK_DEV_ETH
, SOCKET0
);
1070 ovs_mutex_unlock(&dpdk_mutex
);
1075 common_destruct(struct netdev_dpdk
*dev
)
1076 OVS_REQUIRES(dpdk_mutex
)
1077 OVS_EXCLUDED(dev
->mutex
)
1079 rte_free(dev
->tx_q
);
1080 dpdk_mp_free(dev
->mp
);
1082 ovs_list_remove(&dev
->list_node
);
1083 free(ovsrcu_get_protected(struct ingress_policer
*,
1084 &dev
->ingress_policer
));
1085 ovs_mutex_destroy(&dev
->mutex
);
1089 netdev_dpdk_destruct(struct netdev
*netdev
)
1091 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1092 char devname
[RTE_ETH_NAME_MAX_LEN
];
1094 ovs_mutex_lock(&dpdk_mutex
);
1096 rte_eth_dev_stop(dev
->port_id
);
1098 if (dev
->attached
) {
1099 rte_eth_dev_close(dev
->port_id
);
1100 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1101 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1103 VLOG_INFO("Device '%s' has been detached", devname
);
1108 common_destruct(dev
);
1110 ovs_mutex_unlock(&dpdk_mutex
);
1113 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1114 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1115 * deadlock, none of the mutexes must be held while calling this function. */
1117 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1119 OVS_EXCLUDED(dpdk_mutex
)
1120 OVS_EXCLUDED(dev
->mutex
)
1122 return rte_vhost_driver_unregister(vhost_id
);
1126 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1128 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1131 ovs_mutex_lock(&dpdk_mutex
);
1133 /* Guest becomes an orphan if still attached. */
1134 if (netdev_dpdk_get_vid(dev
) >= 0
1135 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1136 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1138 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1139 "socket '%s' must be restarted.", dev
->vhost_id
);
1142 vhost_id
= xstrdup(dev
->vhost_id
);
1144 common_destruct(dev
);
1146 ovs_mutex_unlock(&dpdk_mutex
);
1152 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1153 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1154 netdev
->name
, vhost_id
);
1155 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1156 /* OVS server mode - remove this socket from list for deletion */
1157 fatal_signal_remove_file_to_unlink(vhost_id
);
1164 netdev_dpdk_dealloc(struct netdev
*netdev
)
1166 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1172 netdev_dpdk_clear_xstats(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
1174 /* If statistics are already allocated, we have to
1175 * reconfigure, as port_id could have been changed. */
1176 if (dev
->rte_xstats_names
) {
1177 free(dev
->rte_xstats_names
);
1178 dev
->rte_xstats_names
= NULL
;
1179 dev
->rte_xstats_names_size
= 0;
1181 if (dev
->rte_xstats_ids
) {
1182 free(dev
->rte_xstats_ids
);
1183 dev
->rte_xstats_ids
= NULL
;
1184 dev
->rte_xstats_ids_size
= 0;
1189 netdev_dpdk_get_xstat_name(struct netdev_dpdk
*dev
, uint64_t id
)
1191 if (id
>= dev
->rte_xstats_names_size
) {
1194 return dev
->rte_xstats_names
[id
].name
;
1198 netdev_dpdk_configure_xstats(struct netdev_dpdk
*dev
)
1199 OVS_REQUIRES(dev
->mutex
)
1203 struct rte_eth_xstat
*rte_xstats
;
1208 /* Retrieving all XSTATS names. If something will go wrong
1209 * or amount of counters will be equal 0, rte_xstats_names
1210 * buffer will be marked as NULL, and any further xstats
1211 * query won't be performed (e.g. during netdev_dpdk_get_stats
1217 if (dev
->rte_xstats_names
== NULL
|| dev
->rte_xstats_ids
== NULL
) {
1218 dev
->rte_xstats_names_size
=
1219 rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
1221 if (dev
->rte_xstats_names_size
< 0) {
1222 VLOG_WARN("Cannot get XSTATS for port: %"PRIu8
, dev
->port_id
);
1223 dev
->rte_xstats_names_size
= 0;
1225 /* Reserve memory for xstats names and values */
1226 dev
->rte_xstats_names
= xcalloc(dev
->rte_xstats_names_size
,
1227 sizeof *dev
->rte_xstats_names
);
1229 if (dev
->rte_xstats_names
) {
1230 /* Retreive xstats names */
1232 rte_eth_xstats_get_names(dev
->port_id
,
1233 dev
->rte_xstats_names
,
1234 dev
->rte_xstats_names_size
);
1236 if (rte_xstats_len
< 0) {
1237 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
,
1240 } else if (rte_xstats_len
!= dev
->rte_xstats_names_size
) {
1241 VLOG_WARN("XSTATS size doesn't match for port: %"PRIu8
,
1246 dev
->rte_xstats_ids
= xcalloc(dev
->rte_xstats_names_size
,
1249 /* We have to calculate number of counters */
1250 rte_xstats
= xmalloc(rte_xstats_len
* sizeof *rte_xstats
);
1251 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
1253 /* Retreive xstats values */
1254 if (rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
1255 rte_xstats_len
) > 0) {
1256 dev
->rte_xstats_ids_size
= 0;
1258 for (uint32_t i
= 0; i
< rte_xstats_len
; i
++) {
1259 id
= rte_xstats
[i
].id
;
1260 name
= netdev_dpdk_get_xstat_name(dev
, id
);
1261 /* We need to filter out everything except
1262 * dropped, error and management counters */
1263 if (string_ends_with(name
, "_errors") ||
1264 strstr(name
, "_management_") ||
1265 string_ends_with(name
, "_dropped")) {
1267 dev
->rte_xstats_ids
[xstats_no
] = id
;
1271 dev
->rte_xstats_ids_size
= xstats_no
;
1274 VLOG_WARN("Can't get XSTATS IDs for port: %"PRIu8
,
1280 /* Already configured */
1286 netdev_dpdk_clear_xstats(dev
);
1292 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1294 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1296 ovs_mutex_lock(&dev
->mutex
);
1298 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1299 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1300 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1301 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1302 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1304 if (dev
->type
== DPDK_DEV_ETH
) {
1305 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1306 dev
->requested_rxq_size
);
1307 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1309 smap_add_format(args
, "requested_txq_descriptors", "%d",
1310 dev
->requested_txq_size
);
1311 smap_add_format(args
, "configured_txq_descriptors", "%d",
1313 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1314 smap_add(args
, "rx_csum_offload", "true");
1316 smap_add(args
, "rx_csum_offload", "false");
1319 ovs_mutex_unlock(&dev
->mutex
);
1324 static struct netdev_dpdk
*
1325 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1326 OVS_REQUIRES(dpdk_mutex
)
1328 struct netdev_dpdk
*dev
;
1330 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1331 if (dev
->port_id
== port_id
) {
1340 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1341 const char *devargs
, char **errp
)
1343 /* Get the name up to the first comma. */
1344 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1345 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1347 if (rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1348 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1349 /* Device not found in DPDK, attempt to attach it */
1350 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1351 /* Attach successful */
1352 dev
->attached
= true;
1353 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1355 /* Attach unsuccessful */
1356 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1357 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1367 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1368 OVS_REQUIRES(dev
->mutex
)
1372 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1373 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1374 dev
->requested_n_rxq
= new_n_rxq
;
1375 netdev_request_reconfigure(&dev
->up
);
1380 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1381 const char *flag
, int default_size
, int *new_size
)
1383 int queue_size
= smap_get_int(args
, flag
, default_size
);
1385 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1386 || !is_pow2(queue_size
)) {
1387 queue_size
= default_size
;
1390 if (queue_size
!= *new_size
) {
1391 *new_size
= queue_size
;
1392 netdev_request_reconfigure(netdev
);
1397 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1400 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1401 bool rx_fc_en
, tx_fc_en
, autoneg
;
1402 enum rte_eth_fc_mode fc_mode
;
1403 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1404 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1405 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1407 const char *new_devargs
;
1410 ovs_mutex_lock(&dpdk_mutex
);
1411 ovs_mutex_lock(&dev
->mutex
);
1413 dpdk_set_rxq_config(dev
, args
);
1415 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1416 NIC_PORT_DEFAULT_RXQ_SIZE
,
1417 &dev
->requested_rxq_size
);
1418 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1419 NIC_PORT_DEFAULT_TXQ_SIZE
,
1420 &dev
->requested_txq_size
);
1422 new_devargs
= smap_get(args
, "dpdk-devargs");
1424 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1425 /* The user requested a new device. If we return error, the caller
1426 * will delete this netdev and try to recreate it. */
1431 /* dpdk-devargs is required for device configuration */
1432 if (new_devargs
&& new_devargs
[0]) {
1433 /* Don't process dpdk-devargs if value is unchanged and port id
1435 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1436 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1437 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1440 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1442 } else if (new_port_id
== dev
->port_id
) {
1443 /* Already configured, do not reconfigure again */
1446 struct netdev_dpdk
*dup_dev
;
1448 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1450 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1451 "which is already in use by '%s'",
1452 netdev_get_name(netdev
), new_devargs
,
1453 netdev_get_name(&dup_dev
->up
));
1456 int sid
= rte_eth_dev_socket_id(new_port_id
);
1458 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1459 dev
->devargs
= xstrdup(new_devargs
);
1460 dev
->port_id
= new_port_id
;
1461 netdev_request_reconfigure(&dev
->up
);
1462 netdev_dpdk_clear_xstats(dev
);
1468 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1469 "The old 'dpdk<port_id>' names are not supported",
1470 netdev_get_name(netdev
));
1478 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1479 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1480 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1482 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1483 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1484 dev
->fc_conf
.mode
= fc_mode
;
1485 dev
->fc_conf
.autoneg
= autoneg
;
1486 dpdk_eth_flow_ctrl_setup(dev
);
1490 ovs_mutex_unlock(&dev
->mutex
);
1491 ovs_mutex_unlock(&dpdk_mutex
);
1497 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1498 char **errp OVS_UNUSED
)
1500 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1502 ovs_mutex_lock(&dev
->mutex
);
1503 dpdk_set_rxq_config(dev
, args
);
1504 ovs_mutex_unlock(&dev
->mutex
);
1510 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1511 const struct smap
*args
,
1512 char **errp OVS_UNUSED
)
1514 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1517 ovs_mutex_lock(&dev
->mutex
);
1518 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1519 path
= smap_get(args
, "vhost-server-path");
1520 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1521 strcpy(dev
->vhost_id
, path
);
1522 netdev_request_reconfigure(netdev
);
1525 ovs_mutex_unlock(&dev
->mutex
);
1531 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1533 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1535 return dev
->socket_id
;
1538 /* Sets the number of tx queues for the dpdk interface. */
1540 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1542 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1544 ovs_mutex_lock(&dev
->mutex
);
1546 if (dev
->requested_n_txq
== n_txq
) {
1550 dev
->requested_n_txq
= n_txq
;
1551 netdev_request_reconfigure(netdev
);
1554 ovs_mutex_unlock(&dev
->mutex
);
1558 static struct netdev_rxq
*
1559 netdev_dpdk_rxq_alloc(void)
1561 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1570 static struct netdev_rxq_dpdk
*
1571 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1573 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1577 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1579 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1580 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1582 ovs_mutex_lock(&dev
->mutex
);
1583 rx
->port_id
= dev
->port_id
;
1584 ovs_mutex_unlock(&dev
->mutex
);
1590 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1595 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1597 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1602 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1603 * 'pkts', even in case of failure.
1605 * Returns the number of packets that weren't transmitted. */
1607 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1608 struct rte_mbuf
**pkts
, int cnt
)
1612 while (nb_tx
!= cnt
) {
1615 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1623 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1624 /* Free buffers, which we couldn't transmit, one at a time (each
1625 * packet could come from a different mempool) */
1628 for (i
= nb_tx
; i
< cnt
; i
++) {
1629 rte_pktmbuf_free(pkts
[i
]);
1637 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1638 struct rte_mbuf
*pkt
, uint64_t time
)
1640 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1642 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1647 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1648 struct rte_mbuf
**pkts
, int pkt_cnt
,
1653 struct rte_mbuf
*pkt
= NULL
;
1654 uint64_t current_time
= rte_rdtsc();
1656 for (i
= 0; i
< pkt_cnt
; i
++) {
1658 /* Handle current packet */
1659 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1666 rte_pktmbuf_free(pkt
);
1675 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1676 int pkt_cnt
, bool may_steal
)
1680 rte_spinlock_lock(&policer
->policer_lock
);
1681 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1682 pkt_cnt
, may_steal
);
1683 rte_spinlock_unlock(&policer
->policer_lock
);
1689 is_vhost_running(struct netdev_dpdk
*dev
)
1691 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1695 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1696 unsigned int packet_size
)
1698 /* Hard-coded search for the size bucket. */
1699 if (packet_size
< 256) {
1700 if (packet_size
>= 128) {
1701 stats
->rx_128_to_255_packets
++;
1702 } else if (packet_size
<= 64) {
1703 stats
->rx_1_to_64_packets
++;
1705 stats
->rx_65_to_127_packets
++;
1708 if (packet_size
>= 1523) {
1709 stats
->rx_1523_to_max_packets
++;
1710 } else if (packet_size
>= 1024) {
1711 stats
->rx_1024_to_1522_packets
++;
1712 } else if (packet_size
< 512) {
1713 stats
->rx_256_to_511_packets
++;
1715 stats
->rx_512_to_1023_packets
++;
1721 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1722 struct dp_packet
**packets
, int count
,
1726 unsigned int packet_size
;
1727 struct dp_packet
*packet
;
1729 stats
->rx_packets
+= count
;
1730 stats
->rx_dropped
+= dropped
;
1731 for (i
= 0; i
< count
; i
++) {
1732 packet
= packets
[i
];
1733 packet_size
= dp_packet_size(packet
);
1735 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1736 /* This only protects the following multicast counting from
1737 * too short packets, but it does not stop the packet from
1738 * further processing. */
1740 stats
->rx_length_errors
++;
1744 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1746 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1747 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1751 stats
->rx_bytes
+= packet_size
;
1756 * The receive path for the vhost port is the TX path out from guest.
1759 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1760 struct dp_packet_batch
*batch
)
1762 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1763 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1765 uint16_t dropped
= 0;
1766 int qid
= rxq
->queue_id
;
1767 int vid
= netdev_dpdk_get_vid(dev
);
1769 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
1770 || !(dev
->flags
& NETDEV_UP
))) {
1774 nb_rx
= rte_vhost_dequeue_burst(vid
, qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1776 (struct rte_mbuf
**) batch
->packets
,
1784 nb_rx
= ingress_policer_run(policer
,
1785 (struct rte_mbuf
**) batch
->packets
,
1790 rte_spinlock_lock(&dev
->stats_lock
);
1791 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1793 rte_spinlock_unlock(&dev
->stats_lock
);
1795 batch
->count
= nb_rx
;
1796 dp_packet_batch_init_packet_fields(batch
);
1802 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1804 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1805 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1806 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1810 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1814 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1815 (struct rte_mbuf
**) batch
->packets
,
1823 nb_rx
= ingress_policer_run(policer
,
1824 (struct rte_mbuf
**) batch
->packets
,
1829 /* Update stats to reflect dropped packets */
1830 if (OVS_UNLIKELY(dropped
)) {
1831 rte_spinlock_lock(&dev
->stats_lock
);
1832 dev
->stats
.rx_dropped
+= dropped
;
1833 rte_spinlock_unlock(&dev
->stats_lock
);
1836 batch
->count
= nb_rx
;
1837 dp_packet_batch_init_packet_fields(batch
);
1843 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1844 int cnt
, bool may_steal
)
1846 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1849 rte_spinlock_lock(&qos_conf
->lock
);
1850 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1851 rte_spinlock_unlock(&qos_conf
->lock
);
1858 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1863 struct rte_mbuf
*pkt
;
1865 for (i
= 0; i
< pkt_cnt
; i
++) {
1867 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1868 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1869 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1870 rte_pktmbuf_free(pkt
);
1874 if (OVS_UNLIKELY(i
!= cnt
)) {
1884 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1885 struct dp_packet
**packets
,
1890 int sent
= attempted
- dropped
;
1892 stats
->tx_packets
+= sent
;
1893 stats
->tx_dropped
+= dropped
;
1895 for (i
= 0; i
< sent
; i
++) {
1896 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1901 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1902 struct dp_packet
**pkts
, int cnt
)
1904 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1905 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1906 unsigned int total_pkts
= cnt
;
1907 unsigned int dropped
= 0;
1909 int vid
= netdev_dpdk_get_vid(dev
);
1911 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1913 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
|| qid
< 0
1914 || !(dev
->flags
& NETDEV_UP
))) {
1915 rte_spinlock_lock(&dev
->stats_lock
);
1916 dev
->stats
.tx_dropped
+= cnt
;
1917 rte_spinlock_unlock(&dev
->stats_lock
);
1921 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1923 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1924 /* Check has QoS has been configured for the netdev */
1925 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
1926 dropped
= total_pkts
- cnt
;
1929 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1930 unsigned int tx_pkts
;
1932 tx_pkts
= rte_vhost_enqueue_burst(vid
, vhost_qid
, cur_pkts
, cnt
);
1933 if (OVS_LIKELY(tx_pkts
)) {
1934 /* Packets have been sent.*/
1936 /* Prepare for possible retry.*/
1937 cur_pkts
= &cur_pkts
[tx_pkts
];
1939 /* No packets sent - do not retry.*/
1942 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1944 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1946 rte_spinlock_lock(&dev
->stats_lock
);
1947 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1949 rte_spinlock_unlock(&dev
->stats_lock
);
1952 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1953 dp_packet_delete(pkts
[i
]);
1957 /* Tx function. Transmit packets indefinitely */
1959 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1960 OVS_NO_THREAD_SAFETY_ANALYSIS
1962 const size_t batch_cnt
= dp_packet_batch_size(batch
);
1963 #if !defined(__CHECKER__) && !defined(_WIN32)
1964 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
1966 /* Sparse or MSVC doesn't like variable length array. */
1967 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1969 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1970 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1971 uint32_t cnt
= batch_cnt
;
1972 uint32_t dropped
= 0;
1974 if (dev
->type
!= DPDK_DEV_VHOST
) {
1975 /* Check if QoS has been configured for this netdev. */
1976 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
1978 dropped
+= batch_cnt
- cnt
;
1983 for (uint32_t i
= 0; i
< cnt
; i
++) {
1984 struct dp_packet
*packet
= batch
->packets
[i
];
1985 uint32_t size
= dp_packet_size(packet
);
1987 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1988 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
1989 size
, dev
->max_packet_len
);
1995 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->mp
);
1996 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
2001 /* We have to do a copy for now */
2002 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
2003 dp_packet_data(packet
), size
);
2004 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
2009 if (OVS_LIKELY(txcnt
)) {
2010 if (dev
->type
== DPDK_DEV_VHOST
) {
2011 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
2014 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
2018 if (OVS_UNLIKELY(dropped
)) {
2019 rte_spinlock_lock(&dev
->stats_lock
);
2020 dev
->stats
.tx_dropped
+= dropped
;
2021 rte_spinlock_unlock(&dev
->stats_lock
);
2026 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
2027 struct dp_packet_batch
*batch
,
2028 bool concurrent_txq OVS_UNUSED
)
2031 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2032 dpdk_do_tx_copy(netdev
, qid
, batch
);
2033 dp_packet_delete_batch(batch
, true);
2035 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
2041 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
2042 struct dp_packet_batch
*batch
,
2043 bool concurrent_txq
)
2045 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
2046 dp_packet_delete_batch(batch
, true);
2050 if (OVS_UNLIKELY(concurrent_txq
)) {
2051 qid
= qid
% dev
->up
.n_txq
;
2052 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
2055 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2056 struct netdev
*netdev
= &dev
->up
;
2058 dpdk_do_tx_copy(netdev
, qid
, batch
);
2059 dp_packet_delete_batch(batch
, true);
2061 int tx_cnt
, dropped
;
2062 int batch_cnt
= dp_packet_batch_size(batch
);
2063 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
2065 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
2066 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
2067 dropped
= batch_cnt
- tx_cnt
;
2069 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
2071 if (OVS_UNLIKELY(dropped
)) {
2072 rte_spinlock_lock(&dev
->stats_lock
);
2073 dev
->stats
.tx_dropped
+= dropped
;
2074 rte_spinlock_unlock(&dev
->stats_lock
);
2078 if (OVS_UNLIKELY(concurrent_txq
)) {
2079 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2084 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
2085 struct dp_packet_batch
*batch
, bool concurrent_txq
)
2087 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2089 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
2094 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
2096 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2098 ovs_mutex_lock(&dev
->mutex
);
2099 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
2101 netdev_change_seq_changed(netdev
);
2103 ovs_mutex_unlock(&dev
->mutex
);
2109 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
2111 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2113 ovs_mutex_lock(&dev
->mutex
);
2115 ovs_mutex_unlock(&dev
->mutex
);
2121 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
2123 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2125 ovs_mutex_lock(&dev
->mutex
);
2127 ovs_mutex_unlock(&dev
->mutex
);
2133 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2135 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2137 /* XXX: Ensure that the overall frame length of the requested MTU does not
2138 * surpass the NETDEV_DPDK_MAX_PKT_LEN. DPDK device drivers differ in how
2139 * the L2 frame length is calculated for a given MTU when
2140 * rte_eth_dev_set_mtu(mtu) is called e.g. i40e driver includes 2 x vlan
2141 * headers, the em driver includes 1 x vlan header, the ixgbe driver does
2142 * not include vlan headers. As such we should use
2143 * MTU_TO_MAX_FRAME_LEN(mtu) which includes an additional 2 x vlan headers
2144 * (8 bytes) for comparison. This avoids a failure later with
2145 * rte_eth_dev_set_mtu(). This approach should be used until DPDK provides
2146 * a method to retrieve the upper bound MTU for a given device.
2148 if (MTU_TO_MAX_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2149 || mtu
< ETHER_MIN_MTU
) {
2150 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2154 ovs_mutex_lock(&dev
->mutex
);
2155 if (dev
->requested_mtu
!= mtu
) {
2156 dev
->requested_mtu
= mtu
;
2157 netdev_request_reconfigure(netdev
);
2159 ovs_mutex_unlock(&dev
->mutex
);
2165 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2168 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2169 struct netdev_stats
*stats
)
2171 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2173 ovs_mutex_lock(&dev
->mutex
);
2175 rte_spinlock_lock(&dev
->stats_lock
);
2176 /* Supported Stats */
2177 stats
->rx_packets
= dev
->stats
.rx_packets
;
2178 stats
->tx_packets
= dev
->stats
.tx_packets
;
2179 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2180 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2181 stats
->multicast
= dev
->stats
.multicast
;
2182 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2183 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2184 stats
->rx_errors
= dev
->stats
.rx_errors
;
2185 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2187 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2188 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2189 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2190 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2191 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2192 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2193 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2195 rte_spinlock_unlock(&dev
->stats_lock
);
2197 ovs_mutex_unlock(&dev
->mutex
);
2203 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2204 const struct rte_eth_xstat
*xstats
,
2205 const struct rte_eth_xstat_name
*names
,
2206 const unsigned int size
)
2208 for (unsigned int i
= 0; i
< size
; i
++) {
2209 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2210 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2211 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2212 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2213 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2214 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2215 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2216 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2217 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2218 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2219 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2220 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2221 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2222 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2223 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2224 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2225 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2226 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2227 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2228 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2229 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2230 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2231 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2232 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2233 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2234 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2235 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2236 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2237 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2238 stats
->multicast
= xstats
[i
].value
;
2239 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2240 stats
->tx_multicast_packets
= xstats
[i
].value
;
2241 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2242 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2243 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2244 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2245 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2246 stats
->rx_undersized_errors
= xstats
[i
].value
;
2247 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2248 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2249 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2250 stats
->rx_jabber_errors
= xstats
[i
].value
;
2256 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2258 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2259 struct rte_eth_stats rte_stats
;
2262 netdev_dpdk_get_carrier(netdev
, &gg
);
2263 ovs_mutex_lock(&dev
->mutex
);
2265 struct rte_eth_xstat
*rte_xstats
= NULL
;
2266 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2267 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2269 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2270 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2271 ovs_mutex_unlock(&dev
->mutex
);
2275 /* Get length of statistics */
2276 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2277 if (rte_xstats_len
< 0) {
2278 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2281 /* Reserve memory for xstats names and values */
2282 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2283 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2285 /* Retreive xstats names */
2286 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2289 if (rte_xstats_new_len
!= rte_xstats_len
) {
2290 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2293 /* Retreive xstats values */
2294 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2295 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2297 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2298 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2301 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2306 free(rte_xstats_names
);
2308 stats
->rx_packets
= rte_stats
.ipackets
;
2309 stats
->tx_packets
= rte_stats
.opackets
;
2310 stats
->rx_bytes
= rte_stats
.ibytes
;
2311 stats
->tx_bytes
= rte_stats
.obytes
;
2312 stats
->rx_errors
= rte_stats
.ierrors
;
2313 stats
->tx_errors
= rte_stats
.oerrors
;
2315 rte_spinlock_lock(&dev
->stats_lock
);
2316 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2317 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2318 rte_spinlock_unlock(&dev
->stats_lock
);
2320 /* These are the available DPDK counters for packets not received due to
2321 * local resource constraints in DPDK and NIC respectively. */
2322 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2323 stats
->rx_missed_errors
= rte_stats
.imissed
;
2325 ovs_mutex_unlock(&dev
->mutex
);
2331 netdev_dpdk_get_custom_stats(const struct netdev
*netdev
,
2332 struct netdev_custom_stats
*custom_stats
)
2336 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2339 ovs_mutex_lock(&dev
->mutex
);
2341 if (netdev_dpdk_configure_xstats(dev
)) {
2342 uint64_t *values
= xcalloc(dev
->rte_xstats_ids_size
,
2346 rte_eth_xstats_get_by_id(dev
->port_id
, dev
->rte_xstats_ids
,
2347 values
, dev
->rte_xstats_ids_size
);
2349 if (rte_xstats_ret
> 0 &&
2350 rte_xstats_ret
<= dev
->rte_xstats_ids_size
) {
2352 custom_stats
->size
= rte_xstats_ret
;
2353 custom_stats
->counters
=
2354 (struct netdev_custom_counter
*) xcalloc(rte_xstats_ret
,
2355 sizeof(struct netdev_custom_counter
));
2357 for (i
= 0; i
< rte_xstats_ret
; i
++) {
2358 ovs_strlcpy(custom_stats
->counters
[i
].name
,
2359 netdev_dpdk_get_xstat_name(dev
,
2360 dev
->rte_xstats_ids
[i
]),
2361 NETDEV_CUSTOM_STATS_NAME_SIZE
);
2362 custom_stats
->counters
[i
].value
= values
[i
];
2365 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
,
2367 custom_stats
->counters
= NULL
;
2368 custom_stats
->size
= 0;
2369 /* Let's clear statistics cache, so it will be
2371 netdev_dpdk_clear_xstats(dev
);
2375 ovs_mutex_unlock(&dev
->mutex
);
2381 netdev_dpdk_get_features(const struct netdev
*netdev
,
2382 enum netdev_features
*current
,
2383 enum netdev_features
*advertised
,
2384 enum netdev_features
*supported
,
2385 enum netdev_features
*peer
)
2387 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2388 struct rte_eth_link link
;
2390 ovs_mutex_lock(&dev
->mutex
);
2392 ovs_mutex_unlock(&dev
->mutex
);
2394 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2395 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2396 *current
= NETDEV_F_10MB_HD
;
2398 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2399 *current
= NETDEV_F_100MB_HD
;
2401 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2402 *current
= NETDEV_F_1GB_HD
;
2404 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2405 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2406 *current
= NETDEV_F_10MB_FD
;
2408 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2409 *current
= NETDEV_F_100MB_FD
;
2411 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2412 *current
= NETDEV_F_1GB_FD
;
2414 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2415 *current
= NETDEV_F_10GB_FD
;
2419 if (link
.link_autoneg
) {
2420 *current
|= NETDEV_F_AUTONEG
;
2423 *advertised
= *supported
= *peer
= 0;
2428 static struct ingress_policer
*
2429 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2431 struct ingress_policer
*policer
= NULL
;
2432 uint64_t rate_bytes
;
2433 uint64_t burst_bytes
;
2436 policer
= xmalloc(sizeof *policer
);
2437 rte_spinlock_init(&policer
->policer_lock
);
2439 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2440 rate_bytes
= rate
* 1000ULL / 8;
2441 burst_bytes
= burst
* 1000ULL / 8;
2443 policer
->app_srtcm_params
.cir
= rate_bytes
;
2444 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2445 policer
->app_srtcm_params
.ebs
= 0;
2446 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2447 &policer
->app_srtcm_params
);
2449 VLOG_ERR("Could not create rte meter for ingress policer");
2458 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2459 uint32_t policer_burst
)
2461 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2462 struct ingress_policer
*policer
;
2464 /* Force to 0 if no rate specified,
2465 * default to 8000 kbits if burst is 0,
2466 * else stick with user-specified value.
2468 policer_burst
= (!policer_rate
? 0
2469 : !policer_burst
? 8000
2472 ovs_mutex_lock(&dev
->mutex
);
2474 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2475 &dev
->ingress_policer
);
2477 if (dev
->policer_rate
== policer_rate
&&
2478 dev
->policer_burst
== policer_burst
) {
2479 /* Assume that settings haven't changed since we last set them. */
2480 ovs_mutex_unlock(&dev
->mutex
);
2484 /* Destroy any existing ingress policer for the device if one exists */
2486 ovsrcu_postpone(free
, policer
);
2489 if (policer_rate
!= 0) {
2490 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2494 ovsrcu_set(&dev
->ingress_policer
, policer
);
2495 dev
->policer_rate
= policer_rate
;
2496 dev
->policer_burst
= policer_burst
;
2497 ovs_mutex_unlock(&dev
->mutex
);
2503 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2505 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2507 ovs_mutex_lock(&dev
->mutex
);
2508 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2509 * postive integer to meet RFC 2863 recommendations.
2511 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2512 ovs_mutex_unlock(&dev
->mutex
);
2518 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2520 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2522 ovs_mutex_lock(&dev
->mutex
);
2523 check_link_status(dev
);
2524 *carrier
= dev
->link
.link_status
;
2526 ovs_mutex_unlock(&dev
->mutex
);
2532 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2534 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2536 ovs_mutex_lock(&dev
->mutex
);
2538 if (is_vhost_running(dev
)) {
2544 ovs_mutex_unlock(&dev
->mutex
);
2549 static long long int
2550 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2552 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2553 long long int carrier_resets
;
2555 ovs_mutex_lock(&dev
->mutex
);
2556 carrier_resets
= dev
->link_reset_cnt
;
2557 ovs_mutex_unlock(&dev
->mutex
);
2559 return carrier_resets
;
2563 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2564 long long int interval OVS_UNUSED
)
2570 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2571 enum netdev_flags off
, enum netdev_flags on
,
2572 enum netdev_flags
*old_flagsp
)
2573 OVS_REQUIRES(dev
->mutex
)
2575 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2579 *old_flagsp
= dev
->flags
;
2583 if (dev
->flags
== *old_flagsp
) {
2587 if (dev
->type
== DPDK_DEV_ETH
) {
2588 if (dev
->flags
& NETDEV_PROMISC
) {
2589 rte_eth_promiscuous_enable(dev
->port_id
);
2592 netdev_change_seq_changed(&dev
->up
);
2594 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2595 * running then change netdev's change_seq to trigger link state
2598 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2599 && is_vhost_running(dev
)) {
2600 netdev_change_seq_changed(&dev
->up
);
2602 /* Clear statistics if device is getting up. */
2603 if (NETDEV_UP
& on
) {
2604 rte_spinlock_lock(&dev
->stats_lock
);
2605 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2606 rte_spinlock_unlock(&dev
->stats_lock
);
2615 netdev_dpdk_update_flags(struct netdev
*netdev
,
2616 enum netdev_flags off
, enum netdev_flags on
,
2617 enum netdev_flags
*old_flagsp
)
2619 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2622 ovs_mutex_lock(&dev
->mutex
);
2623 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2624 ovs_mutex_unlock(&dev
->mutex
);
2630 netdev_dpdk_vhost_user_get_status(const struct netdev
*netdev
,
2633 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2635 ovs_mutex_lock(&dev
->mutex
);
2637 bool client_mode
= dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
;
2638 smap_add_format(args
, "mode", "%s", client_mode
? "client" : "server");
2640 int vid
= netdev_dpdk_get_vid(dev
);
2642 smap_add_format(args
, "status", "disconnected");
2643 ovs_mutex_unlock(&dev
->mutex
);
2646 smap_add_format(args
, "status", "connected");
2649 char socket_name
[PATH_MAX
];
2650 if (!rte_vhost_get_ifname(vid
, socket_name
, PATH_MAX
)) {
2651 smap_add_format(args
, "socket", "%s", socket_name
);
2655 if (!rte_vhost_get_negotiated_features(vid
, &features
)) {
2656 smap_add_format(args
, "features", "0x%016"PRIx64
, features
);
2660 if (!rte_vhost_get_mtu(vid
, &mtu
)) {
2661 smap_add_format(args
, "mtu", "%d", mtu
);
2664 int numa
= rte_vhost_get_numa_node(vid
);
2666 smap_add_format(args
, "numa", "%d", numa
);
2669 uint16_t vring_num
= rte_vhost_get_vring_num(vid
);
2671 smap_add_format(args
, "num_of_vrings", "%d", vring_num
);
2674 for (int i
= 0; i
< vring_num
; i
++) {
2675 struct rte_vhost_vring vring
;
2676 char vhost_vring
[16];
2678 rte_vhost_get_vhost_vring(vid
, i
, &vring
);
2679 snprintf(vhost_vring
, 16, "vring_%d_size", i
);
2680 smap_add_format(args
, vhost_vring
, "%d", vring
.size
);
2683 ovs_mutex_unlock(&dev
->mutex
);
2688 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2690 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2691 struct rte_eth_dev_info dev_info
;
2693 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2697 ovs_mutex_lock(&dev
->mutex
);
2698 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2699 ovs_mutex_unlock(&dev
->mutex
);
2701 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2702 smap_add_format(args
, "numa_id", "%d",
2703 rte_eth_dev_socket_id(dev
->port_id
));
2704 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2705 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2706 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2707 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2708 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2709 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2710 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2711 dev_info
.max_hash_mac_addrs
);
2712 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2713 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2715 /* Querying the DPDK library for iftype may be done in future, pending
2716 * support; cf. RFC 3635 Section 3.2.4. */
2717 enum { IF_TYPE_ETHERNETCSMACD
= 6 };
2719 smap_add_format(args
, "if_type", "%"PRIu32
, IF_TYPE_ETHERNETCSMACD
);
2720 smap_add_format(args
, "if_descr", "%s %s", rte_version(),
2721 dev_info
.driver_name
);
2723 if (dev_info
.pci_dev
) {
2724 smap_add_format(args
, "pci-vendor_id", "0x%u",
2725 dev_info
.pci_dev
->id
.vendor_id
);
2726 smap_add_format(args
, "pci-device_id", "0x%x",
2727 dev_info
.pci_dev
->id
.device_id
);
2734 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2735 OVS_REQUIRES(dev
->mutex
)
2737 enum netdev_flags old_flags
;
2740 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2742 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2747 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2748 const char *argv
[], void *aux OVS_UNUSED
)
2752 if (!strcasecmp(argv
[argc
- 1], "up")) {
2754 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2757 unixctl_command_reply_error(conn
, "Invalid Admin State");
2762 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2764 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2765 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2767 ovs_mutex_lock(&dev
->mutex
);
2768 netdev_dpdk_set_admin_state__(dev
, up
);
2769 ovs_mutex_unlock(&dev
->mutex
);
2771 netdev_close(netdev
);
2773 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2774 netdev_close(netdev
);
2778 struct netdev_dpdk
*dev
;
2780 ovs_mutex_lock(&dpdk_mutex
);
2781 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2782 ovs_mutex_lock(&dev
->mutex
);
2783 netdev_dpdk_set_admin_state__(dev
, up
);
2784 ovs_mutex_unlock(&dev
->mutex
);
2786 ovs_mutex_unlock(&dpdk_mutex
);
2788 unixctl_command_reply(conn
, "OK");
2792 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2793 const char *argv
[], void *aux OVS_UNUSED
)
2797 dpdk_port_t port_id
;
2798 char devname
[RTE_ETH_NAME_MAX_LEN
];
2799 struct netdev_dpdk
*dev
;
2801 ovs_mutex_lock(&dpdk_mutex
);
2803 if (rte_eth_dev_get_port_by_name(argv
[1], &port_id
)) {
2804 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2808 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2810 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2811 "Remove it before detaching",
2812 argv
[1], netdev_get_name(&dev
->up
));
2816 rte_eth_dev_close(port_id
);
2818 ret
= rte_eth_dev_detach(port_id
, devname
);
2820 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2824 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2826 ovs_mutex_unlock(&dpdk_mutex
);
2827 unixctl_command_reply(conn
, response
);
2832 ovs_mutex_unlock(&dpdk_mutex
);
2833 unixctl_command_reply_error(conn
, response
);
2838 netdev_dpdk_get_mempool_info(struct unixctl_conn
*conn
,
2839 int argc
, const char *argv
[],
2840 void *aux OVS_UNUSED
)
2844 char *response
= NULL
;
2845 struct netdev
*netdev
= NULL
;
2848 netdev
= netdev_from_name(argv
[1]);
2849 if (!netdev
|| !is_dpdk_class(netdev
->netdev_class
)) {
2850 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2855 stream
= open_memstream(&response
, &size
);
2857 response
= xasprintf("Unable to open memstream: %s.",
2858 ovs_strerror(errno
));
2859 unixctl_command_reply_error(conn
, response
);
2864 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2866 ovs_mutex_lock(&dev
->mutex
);
2867 ovs_mutex_lock(&dpdk_mp_mutex
);
2869 rte_mempool_dump(stream
, dev
->mp
);
2871 ovs_mutex_unlock(&dpdk_mp_mutex
);
2872 ovs_mutex_unlock(&dev
->mutex
);
2874 ovs_mutex_lock(&dpdk_mp_mutex
);
2875 rte_mempool_list_dump(stream
);
2876 ovs_mutex_unlock(&dpdk_mp_mutex
);
2881 unixctl_command_reply(conn
, response
);
2884 netdev_close(netdev
);
2888 * Set virtqueue flags so that we do not receive interrupts.
2891 set_irq_status(int vid
)
2895 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2896 rte_vhost_enable_guest_notification(vid
, i
, 0);
2901 * Fixes mapping for vhost-user tx queues. Must be called after each
2902 * enabling/disabling of queues and n_txq modifications.
2905 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2906 OVS_REQUIRES(dev
->mutex
)
2908 int *enabled_queues
, n_enabled
= 0;
2909 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2911 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2913 for (i
= 0; i
< total_txqs
; i
++) {
2914 /* Enabled queues always mapped to themselves. */
2915 if (dev
->tx_q
[i
].map
== i
) {
2916 enabled_queues
[n_enabled
++] = i
;
2920 if (n_enabled
== 0 && total_txqs
!= 0) {
2921 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2926 for (i
= 0; i
< total_txqs
; i
++) {
2927 if (dev
->tx_q
[i
].map
!= i
) {
2928 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2929 k
= (k
+ 1) % n_enabled
;
2933 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2934 for (i
= 0; i
< total_txqs
; i
++) {
2935 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2938 free(enabled_queues
);
2942 * A new virtio-net device is added to a vhost port.
2947 struct netdev_dpdk
*dev
;
2948 bool exists
= false;
2950 char ifname
[IF_NAME_SZ
];
2952 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2954 ovs_mutex_lock(&dpdk_mutex
);
2955 /* Add device to the vhost port with the same name as that passed down. */
2956 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2957 ovs_mutex_lock(&dev
->mutex
);
2958 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2959 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
2961 /* Get NUMA information */
2962 newnode
= rte_vhost_get_numa_node(vid
);
2963 if (newnode
== -1) {
2965 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2968 newnode
= dev
->socket_id
;
2971 if (dev
->requested_n_txq
!= qp_num
2972 || dev
->requested_n_rxq
!= qp_num
2973 || dev
->requested_socket_id
!= newnode
) {
2974 dev
->requested_socket_id
= newnode
;
2975 dev
->requested_n_rxq
= qp_num
;
2976 dev
->requested_n_txq
= qp_num
;
2977 netdev_request_reconfigure(&dev
->up
);
2979 /* Reconfiguration not required. */
2980 dev
->vhost_reconfigured
= true;
2983 ovsrcu_index_set(&dev
->vid
, vid
);
2986 /* Disable notifications. */
2987 set_irq_status(vid
);
2988 netdev_change_seq_changed(&dev
->up
);
2989 ovs_mutex_unlock(&dev
->mutex
);
2992 ovs_mutex_unlock(&dev
->mutex
);
2994 ovs_mutex_unlock(&dpdk_mutex
);
2997 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
3002 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
3008 /* Clears mapping for all available queues of vhost interface. */
3010 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
3011 OVS_REQUIRES(dev
->mutex
)
3015 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
3016 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
3021 * Remove a virtio-net device from the specific vhost port. Use dev->remove
3022 * flag to stop any more packets from being sent or received to/from a VM and
3023 * ensure all currently queued packets have been sent/received before removing
3027 destroy_device(int vid
)
3029 struct netdev_dpdk
*dev
;
3030 bool exists
= false;
3031 char ifname
[IF_NAME_SZ
];
3033 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3035 ovs_mutex_lock(&dpdk_mutex
);
3036 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3037 if (netdev_dpdk_get_vid(dev
) == vid
) {
3039 ovs_mutex_lock(&dev
->mutex
);
3040 dev
->vhost_reconfigured
= false;
3041 ovsrcu_index_set(&dev
->vid
, -1);
3042 netdev_dpdk_txq_map_clear(dev
);
3044 netdev_change_seq_changed(&dev
->up
);
3045 ovs_mutex_unlock(&dev
->mutex
);
3051 ovs_mutex_unlock(&dpdk_mutex
);
3055 * Wait for other threads to quiesce after setting the 'virtio_dev'
3056 * to NULL, before returning.
3058 ovsrcu_synchronize();
3060 * As call to ovsrcu_synchronize() will end the quiescent state,
3061 * put thread back into quiescent state before returning.
3063 ovsrcu_quiesce_start();
3064 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
3066 VLOG_INFO("vHost Device '%s' not found", ifname
);
3071 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
3073 struct netdev_dpdk
*dev
;
3074 bool exists
= false;
3075 int qid
= queue_id
/ VIRTIO_QNUM
;
3076 char ifname
[IF_NAME_SZ
];
3078 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3080 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
3084 ovs_mutex_lock(&dpdk_mutex
);
3085 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3086 ovs_mutex_lock(&dev
->mutex
);
3087 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3089 dev
->tx_q
[qid
].map
= qid
;
3091 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
3093 netdev_dpdk_remap_txqs(dev
);
3095 ovs_mutex_unlock(&dev
->mutex
);
3098 ovs_mutex_unlock(&dev
->mutex
);
3100 ovs_mutex_unlock(&dpdk_mutex
);
3103 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
3104 "changed to \'%s\'", queue_id
, qid
, ifname
,
3105 (enable
== 1) ? "enabled" : "disabled");
3107 VLOG_INFO("vHost Device '%s' not found", ifname
);
3115 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
3117 return ovsrcu_index_get(&dev
->vid
);
3120 struct ingress_policer
*
3121 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
3123 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
3127 netdev_dpdk_class_init(void)
3129 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3131 /* This function can be called for different classes. The initialization
3132 * needs to be done only once */
3133 if (ovsthread_once_start(&once
)) {
3134 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
3135 unixctl_command_register("netdev-dpdk/set-admin-state",
3136 "[netdev] up|down", 1, 2,
3137 netdev_dpdk_set_admin_state
, NULL
);
3139 unixctl_command_register("netdev-dpdk/detach",
3140 "pci address of device", 1, 1,
3141 netdev_dpdk_detach
, NULL
);
3143 unixctl_command_register("netdev-dpdk/get-mempool-info",
3145 netdev_dpdk_get_mempool_info
, NULL
);
3147 ovsthread_once_done(&once
);
3157 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
3158 dpdk_port_t
*eth_port_id
)
3160 struct dpdk_ring
*ring_pair
;
3164 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
3169 /* XXX: Add support for multiquque ring. */
3170 ring_name
= xasprintf("%s_tx", dev_name
);
3172 /* Create single producer tx ring, netdev does explicit locking. */
3173 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3176 if (ring_pair
->cring_tx
== NULL
) {
3177 rte_free(ring_pair
);
3181 ring_name
= xasprintf("%s_rx", dev_name
);
3183 /* Create single consumer rx ring, netdev does explicit locking. */
3184 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3187 if (ring_pair
->cring_rx
== NULL
) {
3188 rte_free(ring_pair
);
3192 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
3193 &ring_pair
->cring_tx
, 1, SOCKET0
);
3196 rte_free(ring_pair
);
3200 ring_pair
->user_port_id
= port_no
;
3201 ring_pair
->eth_port_id
= port_id
;
3202 *eth_port_id
= port_id
;
3204 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
3210 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
3211 OVS_REQUIRES(dpdk_mutex
)
3213 struct dpdk_ring
*ring_pair
;
3214 unsigned int port_no
;
3217 /* Names always start with "dpdkr" */
3218 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
3223 /* Look through our list to find the device */
3224 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
3225 if (ring_pair
->user_port_id
== port_no
) {
3226 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
3227 /* Really all that is needed */
3228 *eth_port_id
= ring_pair
->eth_port_id
;
3232 /* Need to create the device rings */
3233 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
3237 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
3238 struct dp_packet_batch
*batch
, bool concurrent_txq
)
3240 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3241 struct dp_packet
*packet
;
3243 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
3244 * the rss hash field is clear. This is because the same mbuf may be
3245 * modified by the consumer of the ring and return into the datapath
3246 * without recalculating the RSS hash. */
3247 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
3248 dp_packet_mbuf_rss_flag_reset(packet
);
3251 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
3256 netdev_dpdk_ring_construct(struct netdev
*netdev
)
3258 dpdk_port_t port_no
= 0;
3261 ovs_mutex_lock(&dpdk_mutex
);
3263 err
= dpdk_ring_open(netdev
->name
, &port_no
);
3268 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
3269 rte_eth_dev_socket_id(port_no
));
3271 ovs_mutex_unlock(&dpdk_mutex
);
3278 * Initialize QoS configuration operations.
3281 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
3284 rte_spinlock_init(&conf
->lock
);
3288 * Search existing QoS operations in qos_ops and compare each set of
3289 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
3292 static const struct dpdk_qos_ops
*
3293 qos_lookup_name(const char *name
)
3295 const struct dpdk_qos_ops
*const *opsp
;
3297 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3298 const struct dpdk_qos_ops
*ops
= *opsp
;
3299 if (!strcmp(name
, ops
->qos_name
)) {
3307 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3310 const struct dpdk_qos_ops
*const *opsp
;
3312 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3313 const struct dpdk_qos_ops
*ops
= *opsp
;
3314 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3315 sset_add(types
, ops
->qos_name
);
3322 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3323 const char **typep
, struct smap
*details
)
3325 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3326 struct qos_conf
*qos_conf
;
3329 ovs_mutex_lock(&dev
->mutex
);
3330 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3332 *typep
= qos_conf
->ops
->qos_name
;
3333 error
= (qos_conf
->ops
->qos_get
3334 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3336 /* No QoS configuration set, return an empty string */
3339 ovs_mutex_unlock(&dev
->mutex
);
3345 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3346 const struct smap
*details
)
3348 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3349 const struct dpdk_qos_ops
*new_ops
= NULL
;
3350 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3353 ovs_mutex_lock(&dev
->mutex
);
3355 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3357 new_ops
= qos_lookup_name(type
);
3359 if (!new_ops
|| !new_ops
->qos_construct
) {
3360 new_qos_conf
= NULL
;
3361 if (type
&& type
[0]) {
3364 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3365 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3366 new_qos_conf
= qos_conf
;
3368 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3372 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3373 type
, netdev
->name
, rte_strerror(error
));
3376 if (new_qos_conf
!= qos_conf
) {
3377 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3379 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3383 ovs_mutex_unlock(&dev
->mutex
);
3388 /* egress-policer details */
3390 struct egress_policer
{
3391 struct qos_conf qos_conf
;
3392 struct rte_meter_srtcm_params app_srtcm_params
;
3393 struct rte_meter_srtcm egress_meter
;
3397 egress_policer_details_to_param(const struct smap
*details
,
3398 struct rte_meter_srtcm_params
*params
)
3400 memset(params
, 0, sizeof *params
);
3401 params
->cir
= smap_get_ullong(details
, "cir", 0);
3402 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3407 egress_policer_qos_construct(const struct smap
*details
,
3408 struct qos_conf
**conf
)
3410 struct egress_policer
*policer
;
3413 policer
= xmalloc(sizeof *policer
);
3414 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3415 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3416 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3417 &policer
->app_srtcm_params
);
3419 *conf
= &policer
->qos_conf
;
3430 egress_policer_qos_destruct(struct qos_conf
*conf
)
3432 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3438 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3440 struct egress_policer
*policer
=
3441 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3443 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3444 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3450 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3451 const struct smap
*details
)
3453 struct egress_policer
*policer
=
3454 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3455 struct rte_meter_srtcm_params params
;
3457 egress_policer_details_to_param(details
, ¶ms
);
3459 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3463 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3467 struct egress_policer
*policer
=
3468 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3470 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3471 pkt_cnt
, may_steal
);
3476 static const struct dpdk_qos_ops egress_policer_ops
= {
3477 "egress-policer", /* qos_name */
3478 egress_policer_qos_construct
,
3479 egress_policer_qos_destruct
,
3480 egress_policer_qos_get
,
3481 egress_policer_qos_is_equal
,
3486 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3488 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3491 ovs_mutex_lock(&dev
->mutex
);
3493 if (netdev
->n_txq
== dev
->requested_n_txq
3494 && netdev
->n_rxq
== dev
->requested_n_rxq
3495 && dev
->mtu
== dev
->requested_mtu
3496 && dev
->rxq_size
== dev
->requested_rxq_size
3497 && dev
->txq_size
== dev
->requested_txq_size
3498 && dev
->socket_id
== dev
->requested_socket_id
) {
3499 /* Reconfiguration is unnecessary */
3504 rte_eth_dev_stop(dev
->port_id
);
3506 err
= netdev_dpdk_mempool_configure(dev
);
3507 if (err
&& err
!= EEXIST
) {
3511 netdev
->n_txq
= dev
->requested_n_txq
;
3512 netdev
->n_rxq
= dev
->requested_n_rxq
;
3514 dev
->rxq_size
= dev
->requested_rxq_size
;
3515 dev
->txq_size
= dev
->requested_txq_size
;
3517 rte_free(dev
->tx_q
);
3518 err
= dpdk_eth_dev_init(dev
);
3519 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3524 netdev_change_seq_changed(netdev
);
3527 ovs_mutex_unlock(&dev
->mutex
);
3532 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3533 OVS_REQUIRES(dev
->mutex
)
3535 dev
->up
.n_txq
= dev
->requested_n_txq
;
3536 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3539 /* Enable TX queue 0 by default if it wasn't disabled. */
3540 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3541 dev
->tx_q
[0].map
= 0;
3544 netdev_dpdk_remap_txqs(dev
);
3546 err
= netdev_dpdk_mempool_configure(dev
);
3548 /* A new mempool was created. */
3549 netdev_change_seq_changed(&dev
->up
);
3550 } else if (err
!= EEXIST
){
3553 if (netdev_dpdk_get_vid(dev
) >= 0) {
3554 if (dev
->vhost_reconfigured
== false) {
3555 dev
->vhost_reconfigured
= true;
3556 /* Carrier status may need updating. */
3557 netdev_change_seq_changed(&dev
->up
);
3565 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3567 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3570 ovs_mutex_lock(&dev
->mutex
);
3571 err
= dpdk_vhost_reconfigure_helper(dev
);
3572 ovs_mutex_unlock(&dev
->mutex
);
3578 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3580 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3582 uint64_t vhost_flags
= 0;
3584 ovs_mutex_lock(&dev
->mutex
);
3586 /* Configure vHost client mode if requested and if the following criteria
3588 * 1. Device hasn't been registered yet.
3589 * 2. A path has been specified.
3591 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3592 && strlen(dev
->vhost_id
)) {
3593 /* Register client-mode device. */
3594 vhost_flags
|= RTE_VHOST_USER_CLIENT
;
3596 /* Enable IOMMU support, if explicitly requested. */
3597 if (dpdk_vhost_iommu_enabled()) {
3598 vhost_flags
|= RTE_VHOST_USER_IOMMU_SUPPORT
;
3600 err
= rte_vhost_driver_register(dev
->vhost_id
, vhost_flags
);
3602 VLOG_ERR("vhost-user device setup failure for device %s\n",
3606 /* Configuration successful */
3607 dev
->vhost_driver_flags
|= vhost_flags
;
3608 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3609 "using client socket '%s'",
3610 dev
->up
.name
, dev
->vhost_id
);
3613 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3614 &virtio_net_device_ops
);
3616 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3617 "vhost user client port: %s\n", dev
->up
.name
);
3621 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3622 1ULL << VIRTIO_NET_F_HOST_TSO4
3623 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3624 | 1ULL << VIRTIO_NET_F_CSUM
);
3626 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3627 "client port: %s\n", dev
->up
.name
);
3631 err
= rte_vhost_driver_start(dev
->vhost_id
);
3633 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3634 "client port: %s\n", dev
->up
.name
);
3639 err
= dpdk_vhost_reconfigure_helper(dev
);
3642 ovs_mutex_unlock(&dev
->mutex
);
3647 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3648 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3649 GET_CARRIER, GET_STATS, \
3651 GET_FEATURES, GET_STATUS, \
3652 RECONFIGURE, RXQ_RECV) \
3655 true, /* is_pmd */ \
3657 NULL, /* netdev_dpdk_run */ \
3658 NULL, /* netdev_dpdk_wait */ \
3660 netdev_dpdk_alloc, \
3663 netdev_dpdk_dealloc, \
3664 netdev_dpdk_get_config, \
3666 NULL, /* get_tunnel_config */ \
3667 NULL, /* build header */ \
3668 NULL, /* push header */ \
3669 NULL, /* pop header */ \
3670 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3674 NULL, /* send_wait */ \
3676 netdev_dpdk_set_etheraddr, \
3677 netdev_dpdk_get_etheraddr, \
3678 netdev_dpdk_get_mtu, \
3679 netdev_dpdk_set_mtu, \
3680 netdev_dpdk_get_ifindex, \
3682 netdev_dpdk_get_carrier_resets, \
3683 netdev_dpdk_set_miimon, \
3687 NULL, /* set_advertisements */ \
3688 NULL, /* get_pt_mode */ \
3690 netdev_dpdk_set_policing, \
3691 netdev_dpdk_get_qos_types, \
3692 NULL, /* get_qos_capabilities */ \
3693 netdev_dpdk_get_qos, \
3694 netdev_dpdk_set_qos, \
3695 NULL, /* get_queue */ \
3696 NULL, /* set_queue */ \
3697 NULL, /* delete_queue */ \
3698 NULL, /* get_queue_stats */ \
3699 NULL, /* queue_dump_start */ \
3700 NULL, /* queue_dump_next */ \
3701 NULL, /* queue_dump_done */ \
3702 NULL, /* dump_queue_stats */ \
3704 NULL, /* set_in4 */ \
3705 NULL, /* get_addr_list */ \
3706 NULL, /* add_router */ \
3707 NULL, /* get_next_hop */ \
3709 NULL, /* arp_lookup */ \
3711 netdev_dpdk_update_flags, \
3714 netdev_dpdk_rxq_alloc, \
3715 netdev_dpdk_rxq_construct, \
3716 netdev_dpdk_rxq_destruct, \
3717 netdev_dpdk_rxq_dealloc, \
3719 NULL, /* rx_wait */ \
3720 NULL, /* rxq_drain */ \
3724 static const struct netdev_class dpdk_class
=
3727 netdev_dpdk_class_init
,
3728 netdev_dpdk_construct
,
3729 netdev_dpdk_destruct
,
3730 netdev_dpdk_set_config
,
3731 netdev_dpdk_set_tx_multiq
,
3732 netdev_dpdk_eth_send
,
3733 netdev_dpdk_get_carrier
,
3734 netdev_dpdk_get_stats
,
3735 netdev_dpdk_get_custom_stats
,
3736 netdev_dpdk_get_features
,
3737 netdev_dpdk_get_status
,
3738 netdev_dpdk_reconfigure
,
3739 netdev_dpdk_rxq_recv
);
3741 static const struct netdev_class dpdk_ring_class
=
3744 netdev_dpdk_class_init
,
3745 netdev_dpdk_ring_construct
,
3746 netdev_dpdk_destruct
,
3747 netdev_dpdk_ring_set_config
,
3748 netdev_dpdk_set_tx_multiq
,
3749 netdev_dpdk_ring_send
,
3750 netdev_dpdk_get_carrier
,
3751 netdev_dpdk_get_stats
,
3752 netdev_dpdk_get_custom_stats
,
3753 netdev_dpdk_get_features
,
3754 netdev_dpdk_get_status
,
3755 netdev_dpdk_reconfigure
,
3756 netdev_dpdk_rxq_recv
);
3758 static const struct netdev_class dpdk_vhost_class
=
3762 netdev_dpdk_vhost_construct
,
3763 netdev_dpdk_vhost_destruct
,
3766 netdev_dpdk_vhost_send
,
3767 netdev_dpdk_vhost_get_carrier
,
3768 netdev_dpdk_vhost_get_stats
,
3771 netdev_dpdk_vhost_user_get_status
,
3772 netdev_dpdk_vhost_reconfigure
,
3773 netdev_dpdk_vhost_rxq_recv
);
3774 static const struct netdev_class dpdk_vhost_client_class
=
3776 "dpdkvhostuserclient",
3778 netdev_dpdk_vhost_client_construct
,
3779 netdev_dpdk_vhost_destruct
,
3780 netdev_dpdk_vhost_client_set_config
,
3782 netdev_dpdk_vhost_send
,
3783 netdev_dpdk_vhost_get_carrier
,
3784 netdev_dpdk_vhost_get_stats
,
3787 netdev_dpdk_vhost_user_get_status
,
3788 netdev_dpdk_vhost_client_reconfigure
,
3789 netdev_dpdk_vhost_rxq_recv
);
3792 netdev_dpdk_register(void)
3794 netdev_register_provider(&dpdk_class
);
3795 netdev_register_provider(&dpdk_ring_class
);
3796 netdev_register_provider(&dpdk_vhost_class
);
3797 netdev_register_provider(&dpdk_vhost_client_class
);