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"
26 #include <rte_config.h>
27 #include <rte_cycles.h>
28 #include <rte_errno.h>
29 #include <rte_eth_ring.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
33 #include <rte_meter.h>
34 #include <rte_virtio_net.h>
37 #include "dp-packet.h"
39 #include "dpif-netdev.h"
40 #include "fatal-signal.h"
41 #include "netdev-provider.h"
42 #include "netdev-vport.h"
44 #include "openvswitch/dynamic-string.h"
45 #include "openvswitch/list.h"
46 #include "openvswitch/ofp-print.h"
47 #include "openvswitch/vlog.h"
49 #include "ovs-thread.h"
52 #include "openvswitch/shash.h"
55 #include "unaligned.h"
59 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
60 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
62 #define DPDK_PORT_WATCHDOG_INTERVAL 5
64 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
65 #define OVS_VPORT_DPDK "ovs_dpdk"
68 * need to reserve tons of extra space in the mbufs so we can align the
69 * DMA addresses to 4KB.
70 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
71 * performance for standard Ethernet MTU.
73 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
74 + (2 * VLAN_HEADER_LEN))
75 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
76 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
77 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
78 - ETHER_HDR_LEN - ETHER_CRC_LEN)
79 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
80 + sizeof(struct dp_packet) \
81 + RTE_PKTMBUF_HEADROOM), \
83 #define NETDEV_DPDK_MBUF_ALIGN 1024
84 #define NETDEV_DPDK_MAX_PKT_LEN 9728
86 /* Max and min number of packets in the mempool. OVS tries to allocate a
87 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
88 * enough hugepages) we keep halving the number until the allocation succeeds
89 * or we reach MIN_NB_MBUF */
91 #define MAX_NB_MBUF (4096 * 64)
92 #define MIN_NB_MBUF (4096 * 4)
93 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
95 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
96 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
) == 0);
98 /* The smallest possible NB_MBUF that we're going to try should be a multiple
99 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
100 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
))
104 * DPDK XSTATS Counter names definition
106 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
107 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
108 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
109 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
110 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
111 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
112 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
114 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
115 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
116 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
117 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
118 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
119 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
120 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
122 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
123 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
124 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
125 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
126 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
127 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
128 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
129 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
133 /* Default size of Physical NIC RXQ */
134 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
135 /* Default size of Physical NIC TXQ */
136 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
137 /* Maximum size of Physical NIC Queues */
138 #define NIC_PORT_MAX_Q_SIZE 4096
140 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
141 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
142 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
143 * yet mapped to another queue. */
145 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
147 /* DPDK library uses uint8_t for port_id. */
148 typedef uint8_t dpdk_port_t
;
150 #define VHOST_ENQ_RETRY_NUM 8
151 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
153 static const struct rte_eth_conf port_conf
= {
155 .mq_mode
= ETH_MQ_RX_RSS
,
157 .header_split
= 0, /* Header Split disabled */
158 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
159 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
160 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
166 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
170 .mq_mode
= ETH_MQ_TX_NONE
,
174 enum { DPDK_RING_SIZE
= 256 };
175 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
176 enum { DRAIN_TSC
= 200000ULL };
183 /* Quality of Service */
185 /* An instance of a QoS configuration. Always associated with a particular
188 * Each QoS implementation subclasses this with whatever additional data it
192 const struct dpdk_qos_ops
*ops
;
196 /* A particular implementation of dpdk QoS operations.
198 * The functions below return 0 if successful or a positive errno value on
199 * failure, except where otherwise noted. All of them must be provided, except
200 * where otherwise noted.
202 struct dpdk_qos_ops
{
204 /* Name of the QoS type */
205 const char *qos_name
;
207 /* Called to construct a qos_conf object. The implementation should make
208 * the appropriate calls to configure QoS according to 'details'.
210 * The contents of 'details' should be documented as valid for 'ovs_name'
211 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
212 * (which is built as ovs-vswitchd.conf.db(8)).
214 * This function must return 0 if and only if it sets '*conf' to an
215 * initialized 'struct qos_conf'.
217 * For all QoS implementations it should always be non-null.
219 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
221 /* Destroys the data structures allocated by the implementation as part of
224 * For all QoS implementations it should always be non-null.
226 void (*qos_destruct
)(struct qos_conf
*conf
);
228 /* Retrieves details of 'conf' configuration into 'details'.
230 * The contents of 'details' should be documented as valid for 'ovs_name'
231 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
232 * (which is built as ovs-vswitchd.conf.db(8)).
234 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
236 /* Returns true if 'conf' is already configured according to 'details'.
238 * The contents of 'details' should be documented as valid for 'ovs_name'
239 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
240 * (which is built as ovs-vswitchd.conf.db(8)).
242 * For all QoS implementations it should always be non-null.
244 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
245 const struct smap
*details
);
247 /* Modify an array of rte_mbufs. The modification is specific to
248 * each qos implementation.
250 * The function should take and array of mbufs and an int representing
251 * the current number of mbufs present in the array.
253 * After the function has performed a qos modification to the array of
254 * mbufs it returns an int representing the number of mbufs now present in
255 * the array. This value is can then be passed to the port send function
256 * along with the modified array for transmission.
258 * For all QoS implementations it should always be non-null.
260 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
264 /* dpdk_qos_ops for each type of user space QoS implementation */
265 static const struct dpdk_qos_ops egress_policer_ops
;
268 * Array of dpdk_qos_ops, contains pointer to all supported QoS
271 static const struct dpdk_qos_ops
*const qos_confs
[] = {
276 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
278 /* Contains all 'struct dpdk_dev's. */
279 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
280 = OVS_LIST_INITIALIZER(&dpdk_list
);
282 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
283 = OVS_MUTEX_INITIALIZER
;
285 static struct ovs_list dpdk_mp_list
OVS_GUARDED_BY(dpdk_mp_mutex
)
286 = OVS_LIST_INITIALIZER(&dpdk_mp_list
);
289 struct rte_mempool
*mp
;
293 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
296 /* There should be one 'struct dpdk_tx_queue' created for
298 struct dpdk_tx_queue
{
299 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
300 * from concurrent access. It is used only
301 * if the queue is shared among different
302 * pmd threads (see 'concurrent_txq'). */
303 int map
; /* Mapping of configured vhost-user queues
304 * to enabled by guest. */
307 /* dpdk has no way to remove dpdk ring ethernet devices
308 so we have to keep them around once they've been created
311 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
312 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
315 /* For the client rings */
316 struct rte_ring
*cring_tx
;
317 struct rte_ring
*cring_rx
;
318 unsigned int user_port_id
; /* User given port no, parsed from port name */
319 dpdk_port_t eth_port_id
; /* ethernet device port id */
320 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
323 struct ingress_policer
{
324 struct rte_meter_srtcm_params app_srtcm_params
;
325 struct rte_meter_srtcm in_policer
;
326 rte_spinlock_t policer_lock
;
329 enum dpdk_hw_ol_features
{
330 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
337 enum dpdk_dev_type type
;
339 struct dpdk_tx_queue
*tx_q
;
341 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
343 struct dpdk_mp
*dpdk_mp
;
347 struct netdev_stats stats
;
349 rte_spinlock_t stats_lock
;
351 struct eth_addr hwaddr
;
352 enum netdev_flags flags
;
354 struct rte_eth_link link
;
357 /* virtio identifier for vhost devices */
360 /* True if vHost device is 'up' and has been reconfigured at least once */
361 bool vhost_reconfigured
;
363 /* Identifier used to distinguish vhost devices from each other. */
364 char vhost_id
[PATH_MAX
];
366 /* Device arguments for dpdk ports */
369 /* If true, device was attached by rte_eth_dev_attach(). */
373 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
375 /* QoS configuration and lock for the device */
376 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
378 /* The following properties cannot be changed when a device is running,
379 * so we remember the request and update them next time
380 * netdev_dpdk*_reconfigure() is called */
384 int requested_rxq_size
;
385 int requested_txq_size
;
387 /* Number of rx/tx descriptors for physical devices */
391 /* Socket ID detected when vHost device is brought up */
392 int requested_socket_id
;
394 /* Denotes whether vHost port is client/server mode */
395 uint64_t vhost_driver_flags
;
397 /* Ingress Policer */
398 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
399 uint32_t policer_rate
;
400 uint32_t policer_burst
;
402 /* DPDK-ETH Flow control */
403 struct rte_eth_fc_conf fc_conf
;
405 /* DPDK-ETH hardware offload features,
406 * from the enum set 'dpdk_hw_ol_features' */
407 uint32_t hw_ol_features
;
410 struct netdev_rxq_dpdk
{
411 struct netdev_rxq up
;
415 static int netdev_dpdk_class_init(void);
416 static int netdev_dpdk_vhost_class_init(void);
418 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
420 struct ingress_policer
*
421 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
424 is_dpdk_class(const struct netdev_class
*class)
426 return class->init
== netdev_dpdk_class_init
427 || class->init
== netdev_dpdk_vhost_class_init
;
430 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
431 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
432 * value, insufficient buffers are allocated to accomodate the packet in its
433 * entirety. Furthermore, certain drivers need to ensure that there is also
434 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
435 * frames). If the RX buffer is too small, then the driver enables scatter RX
436 * behaviour, which reduces performance. To prevent this, use a buffer size
437 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
440 dpdk_buf_size(int mtu
)
442 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
443 NETDEV_DPDK_MBUF_ALIGN
);
446 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
448 * Unlike xmalloc(), this function can return NULL on failure. */
450 dpdk_rte_mzalloc(size_t sz
)
452 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
456 free_dpdk_buf(struct dp_packet
*p
)
458 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
460 rte_pktmbuf_free(pkt
);
464 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
465 void *opaque_arg OVS_UNUSED
,
467 unsigned i OVS_UNUSED
)
469 struct rte_mbuf
*pkt
= _p
;
471 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
474 static struct dpdk_mp
*
475 dpdk_mp_create(int socket_id
, int mtu
)
481 dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
485 dmp
->socket_id
= socket_id
;
488 /* XXX: this is a really rough method of provisioning memory.
489 * It's impossible to determine what the exact memory requirements are
490 * when the number of ports and rxqs that utilize a particular mempool can
491 * change dynamically at runtime. For now, use this rough heurisitic.
493 if (mtu
>= ETHER_MTU
) {
494 mp_size
= MAX_NB_MBUF
;
496 mp_size
= MIN_NB_MBUF
;
500 mp_name
= xasprintf("ovs_mp_%d_%d_%u", dmp
->mtu
, dmp
->socket_id
,
503 dmp
->mp
= rte_pktmbuf_pool_create(mp_name
, mp_size
,
505 sizeof (struct dp_packet
)
506 - sizeof (struct rte_mbuf
),
508 - sizeof(struct dp_packet
),
511 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
516 /* rte_pktmbuf_pool_create has done some initialization of the
517 * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init
518 * initializes some OVS specific fields of dp_packet.
520 rte_mempool_obj_iter(dmp
->mp
, ovs_rte_pktmbuf_init
, NULL
);
523 } while (rte_errno
== ENOMEM
&& (mp_size
/= 2) >= MIN_NB_MBUF
);
529 static struct dpdk_mp
*
530 dpdk_mp_get(int socket_id
, int mtu
)
534 ovs_mutex_lock(&dpdk_mp_mutex
);
535 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
536 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
542 dmp
= dpdk_mp_create(socket_id
, mtu
);
544 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
548 ovs_mutex_unlock(&dpdk_mp_mutex
);
554 dpdk_mp_put(struct dpdk_mp
*dmp
)
560 ovs_mutex_lock(&dpdk_mp_mutex
);
561 ovs_assert(dmp
->refcount
);
563 if (!--dmp
->refcount
) {
564 ovs_list_remove(&dmp
->list_node
);
565 rte_mempool_free(dmp
->mp
);
568 ovs_mutex_unlock(&dpdk_mp_mutex
);
571 /* Tries to allocate new mempool on requested_socket_id with
572 * mbuf size corresponding to requested_mtu.
573 * On success new configuration will be applied.
574 * On error, device will be left unchanged. */
576 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
577 OVS_REQUIRES(dev
->mutex
)
579 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
582 mp
= dpdk_mp_get(dev
->requested_socket_id
, FRAME_LEN_TO_MTU(buf_size
));
584 VLOG_ERR("Failed to create memory pool for netdev "
585 "%s, with MTU %d on socket %d: %s\n",
586 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
587 rte_strerror(rte_errno
));
590 dpdk_mp_put(dev
->dpdk_mp
);
592 dev
->mtu
= dev
->requested_mtu
;
593 dev
->socket_id
= dev
->requested_socket_id
;
594 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
601 check_link_status(struct netdev_dpdk
*dev
)
603 struct rte_eth_link link
;
605 rte_eth_link_get_nowait(dev
->port_id
, &link
);
607 if (dev
->link
.link_status
!= link
.link_status
) {
608 netdev_change_seq_changed(&dev
->up
);
610 dev
->link_reset_cnt
++;
612 if (dev
->link
.link_status
) {
613 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
614 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
615 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
616 ("full-duplex") : ("half-duplex"));
618 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
624 dpdk_watchdog(void *dummy OVS_UNUSED
)
626 struct netdev_dpdk
*dev
;
628 pthread_detach(pthread_self());
631 ovs_mutex_lock(&dpdk_mutex
);
632 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
633 ovs_mutex_lock(&dev
->mutex
);
634 if (dev
->type
== DPDK_DEV_ETH
) {
635 check_link_status(dev
);
637 ovs_mutex_unlock(&dev
->mutex
);
639 ovs_mutex_unlock(&dpdk_mutex
);
640 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
647 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
651 struct rte_eth_conf conf
= port_conf
;
653 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
655 if (dev
->mtu
> ETHER_MTU
) {
656 conf
.rxmode
.enable_scatter
= 1;
659 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
660 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
661 /* A device may report more queues than it makes available (this has
662 * been observed for Intel xl710, which reserves some of them for
663 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
664 * available. When this happens we can retry the configuration
665 * and request less queues */
666 while (n_rxq
&& n_txq
) {
668 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
671 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
673 VLOG_WARN("Interface %s eth_dev setup error %s\n",
674 dev
->up
.name
, rte_strerror(-diag
));
678 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
680 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
681 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
685 for (i
= 0; i
< n_txq
; i
++) {
686 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
687 dev
->socket_id
, NULL
);
689 VLOG_INFO("Interface %s txq(%d) setup error: %s",
690 dev
->up
.name
, i
, rte_strerror(-diag
));
696 /* Retry with less tx queues */
701 for (i
= 0; i
< n_rxq
; i
++) {
702 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
703 dev
->socket_id
, NULL
,
706 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
707 dev
->up
.name
, i
, rte_strerror(-diag
));
713 /* Retry with less rx queues */
718 dev
->up
.n_rxq
= n_rxq
;
719 dev
->up
.n_txq
= n_txq
;
728 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
730 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
731 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
737 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
738 OVS_REQUIRES(dev
->mutex
)
740 struct rte_pktmbuf_pool_private
*mbp_priv
;
741 struct rte_eth_dev_info info
;
742 struct ether_addr eth_addr
;
745 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
746 DEV_RX_OFFLOAD_TCP_CKSUM
|
747 DEV_RX_OFFLOAD_IPV4_CKSUM
;
749 rte_eth_dev_info_get(dev
->port_id
, &info
);
751 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
752 rx_chksm_offload_capa
) {
753 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
755 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
757 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
760 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
761 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
763 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
765 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
766 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
770 diag
= rte_eth_dev_start(dev
->port_id
);
772 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
773 rte_strerror(-diag
));
777 rte_eth_promiscuous_enable(dev
->port_id
);
778 rte_eth_allmulticast_enable(dev
->port_id
);
780 memset(ð_addr
, 0x0, sizeof(eth_addr
));
781 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
782 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
783 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
785 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
786 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
788 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
789 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
791 /* Get the Flow control configuration for DPDK-ETH */
792 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
794 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
801 static struct netdev_dpdk
*
802 netdev_dpdk_cast(const struct netdev
*netdev
)
804 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
807 static struct netdev
*
808 netdev_dpdk_alloc(void)
810 struct netdev_dpdk
*dev
;
812 dev
= dpdk_rte_mzalloc(sizeof *dev
);
820 static struct dpdk_tx_queue
*
821 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
823 struct dpdk_tx_queue
*txqs
;
826 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
828 for (i
= 0; i
< n_txqs
; i
++) {
829 /* Initialize map for vhost devices. */
830 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
831 rte_spinlock_init(&txqs
[i
].tx_lock
);
839 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
840 enum dpdk_dev_type type
, int socket_id
)
841 OVS_REQUIRES(dpdk_mutex
)
843 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
845 ovs_mutex_init(&dev
->mutex
);
847 rte_spinlock_init(&dev
->stats_lock
);
849 /* If the 'sid' is negative, it means that the kernel fails
850 * to obtain the pci numa info. In that situation, always
852 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
853 dev
->requested_socket_id
= dev
->socket_id
;
854 dev
->port_id
= port_no
;
857 dev
->requested_mtu
= ETHER_MTU
;
858 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
859 ovsrcu_index_init(&dev
->vid
, -1);
860 dev
->vhost_reconfigured
= false;
861 dev
->attached
= false;
863 ovsrcu_init(&dev
->qos_conf
, NULL
);
865 ovsrcu_init(&dev
->ingress_policer
, NULL
);
866 dev
->policer_rate
= 0;
867 dev
->policer_burst
= 0;
871 dev
->requested_n_rxq
= NR_QUEUE
;
872 dev
->requested_n_txq
= NR_QUEUE
;
873 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
874 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
876 /* Initialize the flow control to NULL */
877 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
879 /* Initilize the hardware offload flags to 0 */
880 dev
->hw_ol_features
= 0;
882 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
884 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
886 netdev_request_reconfigure(netdev
);
891 /* dev_name must be the prefix followed by a positive decimal number.
892 * (no leading + or - signs are allowed) */
894 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
895 unsigned int *port_no
)
899 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
903 cport
= dev_name
+ strlen(prefix
);
905 if (str_to_uint(cport
, 10, port_no
)) {
913 vhost_common_construct(struct netdev
*netdev
)
914 OVS_REQUIRES(dpdk_mutex
)
916 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
917 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
919 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
924 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
925 DPDK_DEV_VHOST
, socket_id
);
929 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
931 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
932 const char *name
= netdev
->name
;
935 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
936 * the file system. '/' or '\' would traverse directories, so they're not
937 * acceptable in 'name'. */
938 if (strchr(name
, '/') || strchr(name
, '\\')) {
939 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
940 "A valid name must not include '/' or '\\'",
945 ovs_mutex_lock(&dpdk_mutex
);
946 /* Take the name of the vhost-user port and append it to the location where
947 * the socket is to be created, then register the socket.
949 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
950 dpdk_get_vhost_sock_dir(), name
);
952 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
953 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
955 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
958 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
959 VLOG_INFO("Socket %s created for vhost-user port %s\n",
960 dev
->vhost_id
, name
);
962 err
= vhost_common_construct(netdev
);
964 ovs_mutex_unlock(&dpdk_mutex
);
965 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
966 "please migrate to dpdkvhostuserclient ports.");
971 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
975 ovs_mutex_lock(&dpdk_mutex
);
976 err
= vhost_common_construct(netdev
);
977 ovs_mutex_unlock(&dpdk_mutex
);
982 netdev_dpdk_construct(struct netdev
*netdev
)
986 ovs_mutex_lock(&dpdk_mutex
);
987 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
988 DPDK_DEV_ETH
, SOCKET0
);
989 ovs_mutex_unlock(&dpdk_mutex
);
994 common_destruct(struct netdev_dpdk
*dev
)
995 OVS_REQUIRES(dpdk_mutex
)
996 OVS_EXCLUDED(dev
->mutex
)
999 dpdk_mp_put(dev
->dpdk_mp
);
1001 ovs_list_remove(&dev
->list_node
);
1002 free(ovsrcu_get_protected(struct ingress_policer
*,
1003 &dev
->ingress_policer
));
1004 ovs_mutex_destroy(&dev
->mutex
);
1008 netdev_dpdk_destruct(struct netdev
*netdev
)
1010 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1011 char devname
[RTE_ETH_NAME_MAX_LEN
];
1013 ovs_mutex_lock(&dpdk_mutex
);
1015 rte_eth_dev_stop(dev
->port_id
);
1017 if (dev
->attached
) {
1018 rte_eth_dev_close(dev
->port_id
);
1019 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1020 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1022 VLOG_INFO("Device '%s' detached", devname
);
1027 common_destruct(dev
);
1029 ovs_mutex_unlock(&dpdk_mutex
);
1032 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1033 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1034 * deadlock, none of the mutexes must be held while calling this function. */
1036 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1038 OVS_EXCLUDED(dpdk_mutex
)
1039 OVS_EXCLUDED(dev
->mutex
)
1041 return rte_vhost_driver_unregister(vhost_id
);
1045 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1047 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1050 ovs_mutex_lock(&dpdk_mutex
);
1052 /* Guest becomes an orphan if still attached. */
1053 if (netdev_dpdk_get_vid(dev
) >= 0
1054 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1055 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1057 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1058 "socket '%s' must be restarted.", dev
->vhost_id
);
1061 vhost_id
= xstrdup(dev
->vhost_id
);
1063 common_destruct(dev
);
1065 ovs_mutex_unlock(&dpdk_mutex
);
1071 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1072 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1073 netdev
->name
, vhost_id
);
1074 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1075 /* OVS server mode - remove this socket from list for deletion */
1076 fatal_signal_remove_file_to_unlink(vhost_id
);
1083 netdev_dpdk_dealloc(struct netdev
*netdev
)
1085 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1091 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1093 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1095 ovs_mutex_lock(&dev
->mutex
);
1097 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1098 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1099 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1100 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1101 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1103 if (dev
->type
== DPDK_DEV_ETH
) {
1104 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1105 dev
->requested_rxq_size
);
1106 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1108 smap_add_format(args
, "requested_txq_descriptors", "%d",
1109 dev
->requested_txq_size
);
1110 smap_add_format(args
, "configured_txq_descriptors", "%d",
1112 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1113 smap_add(args
, "rx_csum_offload", "true");
1115 smap_add(args
, "rx_csum_offload", "false");
1118 ovs_mutex_unlock(&dev
->mutex
);
1123 static struct netdev_dpdk
*
1124 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1125 OVS_REQUIRES(dpdk_mutex
)
1127 struct netdev_dpdk
*dev
;
1129 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1130 if (dev
->port_id
== port_id
) {
1139 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1140 const char *devargs
, char **errp
)
1142 /* Get the name up to the first comma. */
1143 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1144 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1146 if (!rte_eth_dev_count()
1147 || rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1148 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1149 /* Device not found in DPDK, attempt to attach it */
1150 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1151 /* Attach successful */
1152 dev
->attached
= true;
1153 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1155 /* Attach unsuccessful */
1156 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1157 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1167 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1168 OVS_REQUIRES(dev
->mutex
)
1172 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1173 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1174 dev
->requested_n_rxq
= new_n_rxq
;
1175 netdev_request_reconfigure(&dev
->up
);
1180 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1181 const char *flag
, int default_size
, int *new_size
)
1183 int queue_size
= smap_get_int(args
, flag
, default_size
);
1185 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1186 || !is_pow2(queue_size
)) {
1187 queue_size
= default_size
;
1190 if (queue_size
!= *new_size
) {
1191 *new_size
= queue_size
;
1192 netdev_request_reconfigure(netdev
);
1197 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1200 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1201 bool rx_fc_en
, tx_fc_en
, autoneg
;
1202 enum rte_eth_fc_mode fc_mode
;
1203 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1204 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1205 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1207 const char *new_devargs
;
1210 ovs_mutex_lock(&dpdk_mutex
);
1211 ovs_mutex_lock(&dev
->mutex
);
1213 dpdk_set_rxq_config(dev
, args
);
1215 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1216 NIC_PORT_DEFAULT_RXQ_SIZE
,
1217 &dev
->requested_rxq_size
);
1218 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1219 NIC_PORT_DEFAULT_TXQ_SIZE
,
1220 &dev
->requested_txq_size
);
1222 new_devargs
= smap_get(args
, "dpdk-devargs");
1224 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1225 /* The user requested a new device. If we return error, the caller
1226 * will delete this netdev and try to recreate it. */
1231 /* dpdk-devargs is required for device configuration */
1232 if (new_devargs
&& new_devargs
[0]) {
1233 /* Don't process dpdk-devargs if value is unchanged and port id
1235 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1236 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1237 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1240 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1242 } else if (new_port_id
== dev
->port_id
) {
1243 /* Already configured, do not reconfigure again */
1246 struct netdev_dpdk
*dup_dev
;
1248 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1250 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1251 "which is already in use by '%s'",
1252 netdev_get_name(netdev
), new_devargs
,
1253 netdev_get_name(&dup_dev
->up
));
1256 int sid
= rte_eth_dev_socket_id(new_port_id
);
1258 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1259 dev
->devargs
= xstrdup(new_devargs
);
1260 dev
->port_id
= new_port_id
;
1261 netdev_request_reconfigure(&dev
->up
);
1267 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1268 "The old 'dpdk<port_id>' names are not supported",
1269 netdev_get_name(netdev
));
1277 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1278 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1279 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1281 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1282 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1283 dev
->fc_conf
.mode
= fc_mode
;
1284 dev
->fc_conf
.autoneg
= autoneg
;
1285 dpdk_eth_flow_ctrl_setup(dev
);
1289 ovs_mutex_unlock(&dev
->mutex
);
1290 ovs_mutex_unlock(&dpdk_mutex
);
1296 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1297 char **errp OVS_UNUSED
)
1299 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1301 ovs_mutex_lock(&dev
->mutex
);
1302 dpdk_set_rxq_config(dev
, args
);
1303 ovs_mutex_unlock(&dev
->mutex
);
1309 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1310 const struct smap
*args
,
1311 char **errp OVS_UNUSED
)
1313 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1316 ovs_mutex_lock(&dev
->mutex
);
1317 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1318 path
= smap_get(args
, "vhost-server-path");
1319 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1320 strcpy(dev
->vhost_id
, path
);
1321 netdev_request_reconfigure(netdev
);
1324 ovs_mutex_unlock(&dev
->mutex
);
1330 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1332 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1334 return dev
->socket_id
;
1337 /* Sets the number of tx queues for the dpdk interface. */
1339 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1341 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1343 ovs_mutex_lock(&dev
->mutex
);
1345 if (dev
->requested_n_txq
== n_txq
) {
1349 dev
->requested_n_txq
= n_txq
;
1350 netdev_request_reconfigure(netdev
);
1353 ovs_mutex_unlock(&dev
->mutex
);
1357 static struct netdev_rxq
*
1358 netdev_dpdk_rxq_alloc(void)
1360 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1369 static struct netdev_rxq_dpdk
*
1370 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1372 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1376 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1378 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1379 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1381 ovs_mutex_lock(&dev
->mutex
);
1382 rx
->port_id
= dev
->port_id
;
1383 ovs_mutex_unlock(&dev
->mutex
);
1389 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1394 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1396 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1401 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1402 * 'pkts', even in case of failure.
1404 * Returns the number of packets that weren't transmitted. */
1406 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1407 struct rte_mbuf
**pkts
, int cnt
)
1411 while (nb_tx
!= cnt
) {
1414 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1422 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1423 /* Free buffers, which we couldn't transmit, one at a time (each
1424 * packet could come from a different mempool) */
1427 for (i
= nb_tx
; i
< cnt
; i
++) {
1428 rte_pktmbuf_free(pkts
[i
]);
1436 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1437 struct rte_mbuf
*pkt
, uint64_t time
)
1439 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1441 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1446 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1447 struct rte_mbuf
**pkts
, int pkt_cnt
)
1451 struct rte_mbuf
*pkt
= NULL
;
1452 uint64_t current_time
= rte_rdtsc();
1454 for (i
= 0; i
< pkt_cnt
; i
++) {
1456 /* Handle current packet */
1457 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1463 rte_pktmbuf_free(pkt
);
1471 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1476 rte_spinlock_lock(&policer
->policer_lock
);
1477 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
, pkt_cnt
);
1478 rte_spinlock_unlock(&policer
->policer_lock
);
1484 is_vhost_running(struct netdev_dpdk
*dev
)
1486 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1490 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1491 unsigned int packet_size
)
1493 /* Hard-coded search for the size bucket. */
1494 if (packet_size
< 256) {
1495 if (packet_size
>= 128) {
1496 stats
->rx_128_to_255_packets
++;
1497 } else if (packet_size
<= 64) {
1498 stats
->rx_1_to_64_packets
++;
1500 stats
->rx_65_to_127_packets
++;
1503 if (packet_size
>= 1523) {
1504 stats
->rx_1523_to_max_packets
++;
1505 } else if (packet_size
>= 1024) {
1506 stats
->rx_1024_to_1522_packets
++;
1507 } else if (packet_size
< 512) {
1508 stats
->rx_256_to_511_packets
++;
1510 stats
->rx_512_to_1023_packets
++;
1516 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1517 struct dp_packet
**packets
, int count
,
1521 unsigned int packet_size
;
1522 struct dp_packet
*packet
;
1524 stats
->rx_packets
+= count
;
1525 stats
->rx_dropped
+= dropped
;
1526 for (i
= 0; i
< count
; i
++) {
1527 packet
= packets
[i
];
1528 packet_size
= dp_packet_size(packet
);
1530 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1531 /* This only protects the following multicast counting from
1532 * too short packets, but it does not stop the packet from
1533 * further processing. */
1535 stats
->rx_length_errors
++;
1539 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1541 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1542 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1546 stats
->rx_bytes
+= packet_size
;
1551 * The receive path for the vhost port is the TX path out from guest.
1554 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1555 struct dp_packet_batch
*batch
)
1557 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1558 int qid
= rxq
->queue_id
;
1559 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1561 uint16_t dropped
= 0;
1563 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1564 || !(dev
->flags
& NETDEV_UP
))) {
1568 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1569 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1571 (struct rte_mbuf
**) batch
->packets
,
1579 nb_rx
= ingress_policer_run(policer
,
1580 (struct rte_mbuf
**) batch
->packets
,
1585 rte_spinlock_lock(&dev
->stats_lock
);
1586 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1588 rte_spinlock_unlock(&dev
->stats_lock
);
1590 batch
->count
= (int) nb_rx
;
1595 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1597 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1598 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1599 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1603 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1607 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1608 (struct rte_mbuf
**) batch
->packets
,
1616 nb_rx
= ingress_policer_run(policer
,
1617 (struct rte_mbuf
**) batch
->packets
,
1622 /* Update stats to reflect dropped packets */
1623 if (OVS_UNLIKELY(dropped
)) {
1624 rte_spinlock_lock(&dev
->stats_lock
);
1625 dev
->stats
.rx_dropped
+= dropped
;
1626 rte_spinlock_unlock(&dev
->stats_lock
);
1629 batch
->count
= nb_rx
;
1635 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1638 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1641 rte_spinlock_lock(&qos_conf
->lock
);
1642 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
);
1643 rte_spinlock_unlock(&qos_conf
->lock
);
1650 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1655 struct rte_mbuf
*pkt
;
1657 for (i
= 0; i
< pkt_cnt
; i
++) {
1659 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1660 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1661 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1662 rte_pktmbuf_free(pkt
);
1666 if (OVS_UNLIKELY(i
!= cnt
)) {
1676 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1677 struct dp_packet
**packets
,
1682 int sent
= attempted
- dropped
;
1684 stats
->tx_packets
+= sent
;
1685 stats
->tx_dropped
+= dropped
;
1687 for (i
= 0; i
< sent
; i
++) {
1688 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1693 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1694 struct dp_packet
**pkts
, int cnt
)
1696 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1697 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1698 unsigned int total_pkts
= cnt
;
1699 unsigned int dropped
= 0;
1702 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1704 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1705 || !(dev
->flags
& NETDEV_UP
))) {
1706 rte_spinlock_lock(&dev
->stats_lock
);
1707 dev
->stats
.tx_dropped
+= cnt
;
1708 rte_spinlock_unlock(&dev
->stats_lock
);
1712 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1714 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1715 /* Check has QoS has been configured for the netdev */
1716 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
);
1717 dropped
= total_pkts
- cnt
;
1720 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1721 unsigned int tx_pkts
;
1723 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1724 vhost_qid
, cur_pkts
, cnt
);
1725 if (OVS_LIKELY(tx_pkts
)) {
1726 /* Packets have been sent.*/
1728 /* Prepare for possible retry.*/
1729 cur_pkts
= &cur_pkts
[tx_pkts
];
1731 /* No packets sent - do not retry.*/
1734 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1736 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1738 rte_spinlock_lock(&dev
->stats_lock
);
1739 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1741 rte_spinlock_unlock(&dev
->stats_lock
);
1744 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1745 dp_packet_delete(pkts
[i
]);
1749 /* Tx function. Transmit packets indefinitely */
1751 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1752 OVS_NO_THREAD_SAFETY_ANALYSIS
1754 #if !defined(__CHECKER__) && !defined(_WIN32)
1755 const size_t PKT_ARRAY_SIZE
= batch
->count
;
1757 /* Sparse or MSVC doesn't like variable length array. */
1758 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1760 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1761 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1766 dp_packet_batch_apply_cutlen(batch
);
1768 for (i
= 0; i
< batch
->count
; i
++) {
1769 int size
= dp_packet_size(batch
->packets
[i
]);
1771 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1772 VLOG_WARN_RL(&rl
, "Too big size %d max_packet_len %d",
1773 (int) size
, dev
->max_packet_len
);
1779 pkts
[newcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1781 if (!pkts
[newcnt
]) {
1782 dropped
+= batch
->count
- i
;
1786 /* We have to do a copy for now */
1787 memcpy(rte_pktmbuf_mtod(pkts
[newcnt
], void *),
1788 dp_packet_data(batch
->packets
[i
]), size
);
1790 rte_pktmbuf_data_len(pkts
[newcnt
]) = size
;
1791 rte_pktmbuf_pkt_len(pkts
[newcnt
]) = size
;
1796 if (dev
->type
== DPDK_DEV_VHOST
) {
1797 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1800 unsigned int qos_pkts
= newcnt
;
1802 /* Check if QoS has been configured for this netdev. */
1803 newcnt
= netdev_dpdk_qos_run(dev
, pkts
, newcnt
);
1805 dropped
+= qos_pkts
- newcnt
;
1806 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, newcnt
);
1809 if (OVS_UNLIKELY(dropped
)) {
1810 rte_spinlock_lock(&dev
->stats_lock
);
1811 dev
->stats
.tx_dropped
+= dropped
;
1812 rte_spinlock_unlock(&dev
->stats_lock
);
1817 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1818 struct dp_packet_batch
*batch
,
1819 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1822 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1823 dpdk_do_tx_copy(netdev
, qid
, batch
);
1824 dp_packet_delete_batch(batch
, may_steal
);
1826 dp_packet_batch_apply_cutlen(batch
);
1827 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1833 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1834 struct dp_packet_batch
*batch
, bool may_steal
,
1835 bool concurrent_txq
)
1837 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1838 dp_packet_delete_batch(batch
, may_steal
);
1842 if (OVS_UNLIKELY(concurrent_txq
)) {
1843 qid
= qid
% dev
->up
.n_txq
;
1844 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1847 if (OVS_UNLIKELY(!may_steal
||
1848 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1849 struct netdev
*netdev
= &dev
->up
;
1851 dpdk_do_tx_copy(netdev
, qid
, batch
);
1852 dp_packet_delete_batch(batch
, may_steal
);
1855 int cnt
= batch
->count
;
1856 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1858 dp_packet_batch_apply_cutlen(batch
);
1860 cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, cnt
);
1861 cnt
= netdev_dpdk_qos_run(dev
, pkts
, cnt
);
1862 dropped
= batch
->count
- cnt
;
1864 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, cnt
);
1866 if (OVS_UNLIKELY(dropped
)) {
1867 rte_spinlock_lock(&dev
->stats_lock
);
1868 dev
->stats
.tx_dropped
+= dropped
;
1869 rte_spinlock_unlock(&dev
->stats_lock
);
1873 if (OVS_UNLIKELY(concurrent_txq
)) {
1874 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1879 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1880 struct dp_packet_batch
*batch
, bool may_steal
,
1881 bool concurrent_txq
)
1883 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1885 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1890 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1892 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1894 ovs_mutex_lock(&dev
->mutex
);
1895 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1897 netdev_change_seq_changed(netdev
);
1899 ovs_mutex_unlock(&dev
->mutex
);
1905 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1907 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1909 ovs_mutex_lock(&dev
->mutex
);
1911 ovs_mutex_unlock(&dev
->mutex
);
1917 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1919 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1921 ovs_mutex_lock(&dev
->mutex
);
1923 ovs_mutex_unlock(&dev
->mutex
);
1929 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
1931 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1933 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
1934 || mtu
< ETHER_MIN_MTU
) {
1935 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
1939 ovs_mutex_lock(&dev
->mutex
);
1940 if (dev
->requested_mtu
!= mtu
) {
1941 dev
->requested_mtu
= mtu
;
1942 netdev_request_reconfigure(netdev
);
1944 ovs_mutex_unlock(&dev
->mutex
);
1950 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
1953 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
1954 struct netdev_stats
*stats
)
1956 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1958 ovs_mutex_lock(&dev
->mutex
);
1960 rte_spinlock_lock(&dev
->stats_lock
);
1961 /* Supported Stats */
1962 stats
->rx_packets
+= dev
->stats
.rx_packets
;
1963 stats
->tx_packets
+= dev
->stats
.tx_packets
;
1964 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
1965 stats
->tx_dropped
+= dev
->stats
.tx_dropped
;
1966 stats
->multicast
= dev
->stats
.multicast
;
1967 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
1968 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
1969 stats
->rx_errors
= dev
->stats
.rx_errors
;
1970 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
1972 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
1973 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
1974 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
1975 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
1976 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
1977 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
1978 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
1980 rte_spinlock_unlock(&dev
->stats_lock
);
1982 ovs_mutex_unlock(&dev
->mutex
);
1988 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
1989 const struct rte_eth_xstat
*xstats
,
1990 const struct rte_eth_xstat_name
*names
,
1991 const unsigned int size
)
1993 for (unsigned int i
= 0; i
< size
; i
++) {
1994 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
1995 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
1996 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
1997 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
1998 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
1999 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2000 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2001 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2002 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2003 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2004 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2005 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2006 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2007 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2008 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2009 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2010 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2011 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2012 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2013 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2014 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2015 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2016 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2017 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2018 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2019 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2020 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2021 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2022 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2023 stats
->multicast
= xstats
[i
].value
;
2024 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2025 stats
->tx_multicast_packets
= xstats
[i
].value
;
2026 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2027 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2028 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2029 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2030 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2031 stats
->rx_undersized_errors
= xstats
[i
].value
;
2032 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2033 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2034 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2035 stats
->rx_jabber_errors
= xstats
[i
].value
;
2041 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2043 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2044 struct rte_eth_stats rte_stats
;
2047 netdev_dpdk_get_carrier(netdev
, &gg
);
2048 ovs_mutex_lock(&dev
->mutex
);
2050 struct rte_eth_xstat
*rte_xstats
= NULL
;
2051 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2052 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2054 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2055 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2056 ovs_mutex_unlock(&dev
->mutex
);
2060 /* Get length of statistics */
2061 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2062 if (rte_xstats_len
< 0) {
2063 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2066 /* Reserve memory for xstats names and values */
2067 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2068 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2070 /* Retreive xstats names */
2071 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2074 if (rte_xstats_new_len
!= rte_xstats_len
) {
2075 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2078 /* Retreive xstats values */
2079 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2080 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2082 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2083 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2086 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2091 free(rte_xstats_names
);
2093 stats
->rx_packets
= rte_stats
.ipackets
;
2094 stats
->tx_packets
= rte_stats
.opackets
;
2095 stats
->rx_bytes
= rte_stats
.ibytes
;
2096 stats
->tx_bytes
= rte_stats
.obytes
;
2097 stats
->rx_errors
= rte_stats
.ierrors
;
2098 stats
->tx_errors
= rte_stats
.oerrors
;
2100 rte_spinlock_lock(&dev
->stats_lock
);
2101 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2102 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2103 rte_spinlock_unlock(&dev
->stats_lock
);
2105 /* These are the available DPDK counters for packets not received due to
2106 * local resource constraints in DPDK and NIC respectively. */
2107 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2108 stats
->rx_missed_errors
= rte_stats
.imissed
;
2110 ovs_mutex_unlock(&dev
->mutex
);
2116 netdev_dpdk_get_features(const struct netdev
*netdev
,
2117 enum netdev_features
*current
,
2118 enum netdev_features
*advertised
,
2119 enum netdev_features
*supported
,
2120 enum netdev_features
*peer
)
2122 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2123 struct rte_eth_link link
;
2125 ovs_mutex_lock(&dev
->mutex
);
2127 ovs_mutex_unlock(&dev
->mutex
);
2129 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2130 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2131 *current
= NETDEV_F_10MB_HD
;
2133 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2134 *current
= NETDEV_F_100MB_HD
;
2136 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2137 *current
= NETDEV_F_1GB_HD
;
2139 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2140 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2141 *current
= NETDEV_F_10MB_FD
;
2143 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2144 *current
= NETDEV_F_100MB_FD
;
2146 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2147 *current
= NETDEV_F_1GB_FD
;
2149 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2150 *current
= NETDEV_F_10GB_FD
;
2154 if (link
.link_autoneg
) {
2155 *current
|= NETDEV_F_AUTONEG
;
2158 *advertised
= *supported
= *peer
= 0;
2163 static struct ingress_policer
*
2164 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2166 struct ingress_policer
*policer
= NULL
;
2167 uint64_t rate_bytes
;
2168 uint64_t burst_bytes
;
2171 policer
= xmalloc(sizeof *policer
);
2172 rte_spinlock_init(&policer
->policer_lock
);
2174 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2175 rate_bytes
= rate
* 1000/8;
2176 burst_bytes
= burst
* 1000/8;
2178 policer
->app_srtcm_params
.cir
= rate_bytes
;
2179 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2180 policer
->app_srtcm_params
.ebs
= 0;
2181 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2182 &policer
->app_srtcm_params
);
2184 VLOG_ERR("Could not create rte meter for ingress policer");
2192 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2193 uint32_t policer_burst
)
2195 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2196 struct ingress_policer
*policer
;
2198 /* Force to 0 if no rate specified,
2199 * default to 8000 kbits if burst is 0,
2200 * else stick with user-specified value.
2202 policer_burst
= (!policer_rate
? 0
2203 : !policer_burst
? 8000
2206 ovs_mutex_lock(&dev
->mutex
);
2208 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2209 &dev
->ingress_policer
);
2211 if (dev
->policer_rate
== policer_rate
&&
2212 dev
->policer_burst
== policer_burst
) {
2213 /* Assume that settings haven't changed since we last set them. */
2214 ovs_mutex_unlock(&dev
->mutex
);
2218 /* Destroy any existing ingress policer for the device if one exists */
2220 ovsrcu_postpone(free
, policer
);
2223 if (policer_rate
!= 0) {
2224 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2228 ovsrcu_set(&dev
->ingress_policer
, policer
);
2229 dev
->policer_rate
= policer_rate
;
2230 dev
->policer_burst
= policer_burst
;
2231 ovs_mutex_unlock(&dev
->mutex
);
2237 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2239 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2241 ovs_mutex_lock(&dev
->mutex
);
2242 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2243 * postive integer to meet RFC 2863 recommendations.
2245 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2246 ovs_mutex_unlock(&dev
->mutex
);
2252 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2254 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2256 ovs_mutex_lock(&dev
->mutex
);
2257 check_link_status(dev
);
2258 *carrier
= dev
->link
.link_status
;
2260 ovs_mutex_unlock(&dev
->mutex
);
2266 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2268 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2270 ovs_mutex_lock(&dev
->mutex
);
2272 if (is_vhost_running(dev
)) {
2278 ovs_mutex_unlock(&dev
->mutex
);
2283 static long long int
2284 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2286 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2287 long long int carrier_resets
;
2289 ovs_mutex_lock(&dev
->mutex
);
2290 carrier_resets
= dev
->link_reset_cnt
;
2291 ovs_mutex_unlock(&dev
->mutex
);
2293 return carrier_resets
;
2297 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2298 long long int interval OVS_UNUSED
)
2304 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2305 enum netdev_flags off
, enum netdev_flags on
,
2306 enum netdev_flags
*old_flagsp
)
2307 OVS_REQUIRES(dev
->mutex
)
2309 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2313 *old_flagsp
= dev
->flags
;
2317 if (dev
->flags
== *old_flagsp
) {
2321 if (dev
->type
== DPDK_DEV_ETH
) {
2322 if (dev
->flags
& NETDEV_PROMISC
) {
2323 rte_eth_promiscuous_enable(dev
->port_id
);
2326 netdev_change_seq_changed(&dev
->up
);
2328 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2329 * running then change netdev's change_seq to trigger link state
2332 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2333 && is_vhost_running(dev
)) {
2334 netdev_change_seq_changed(&dev
->up
);
2336 /* Clear statistics if device is getting up. */
2337 if (NETDEV_UP
& on
) {
2338 rte_spinlock_lock(&dev
->stats_lock
);
2339 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2340 rte_spinlock_unlock(&dev
->stats_lock
);
2349 netdev_dpdk_update_flags(struct netdev
*netdev
,
2350 enum netdev_flags off
, enum netdev_flags on
,
2351 enum netdev_flags
*old_flagsp
)
2353 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2356 ovs_mutex_lock(&dev
->mutex
);
2357 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2358 ovs_mutex_unlock(&dev
->mutex
);
2364 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2366 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2367 struct rte_eth_dev_info dev_info
;
2369 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2373 ovs_mutex_lock(&dev
->mutex
);
2374 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2375 ovs_mutex_unlock(&dev
->mutex
);
2377 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2378 smap_add_format(args
, "numa_id", "%d",
2379 rte_eth_dev_socket_id(dev
->port_id
));
2380 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2381 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2382 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2383 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2384 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2385 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2386 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2387 dev_info
.max_hash_mac_addrs
);
2388 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2389 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2391 if (dev_info
.pci_dev
) {
2392 smap_add_format(args
, "pci-vendor_id", "0x%u",
2393 dev_info
.pci_dev
->id
.vendor_id
);
2394 smap_add_format(args
, "pci-device_id", "0x%x",
2395 dev_info
.pci_dev
->id
.device_id
);
2402 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2403 OVS_REQUIRES(dev
->mutex
)
2405 enum netdev_flags old_flags
;
2408 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2410 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2415 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2416 const char *argv
[], void *aux OVS_UNUSED
)
2420 if (!strcasecmp(argv
[argc
- 1], "up")) {
2422 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2425 unixctl_command_reply_error(conn
, "Invalid Admin State");
2430 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2431 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2432 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2434 ovs_mutex_lock(&dpdk_dev
->mutex
);
2435 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2436 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2438 netdev_close(netdev
);
2440 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2441 netdev_close(netdev
);
2445 struct netdev_dpdk
*netdev
;
2447 ovs_mutex_lock(&dpdk_mutex
);
2448 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2449 ovs_mutex_lock(&netdev
->mutex
);
2450 netdev_dpdk_set_admin_state__(netdev
, up
);
2451 ovs_mutex_unlock(&netdev
->mutex
);
2453 ovs_mutex_unlock(&dpdk_mutex
);
2455 unixctl_command_reply(conn
, "OK");
2459 * Set virtqueue flags so that we do not receive interrupts.
2462 set_irq_status(int vid
)
2467 for (i
= 0; i
< rte_vhost_get_queue_num(vid
); i
++) {
2468 idx
= i
* VIRTIO_QNUM
;
2469 rte_vhost_enable_guest_notification(vid
, idx
+ VIRTIO_RXQ
, 0);
2470 rte_vhost_enable_guest_notification(vid
, idx
+ VIRTIO_TXQ
, 0);
2475 * Fixes mapping for vhost-user tx queues. Must be called after each
2476 * enabling/disabling of queues and n_txq modifications.
2479 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2480 OVS_REQUIRES(dev
->mutex
)
2482 int *enabled_queues
, n_enabled
= 0;
2483 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2485 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2487 for (i
= 0; i
< total_txqs
; i
++) {
2488 /* Enabled queues always mapped to themselves. */
2489 if (dev
->tx_q
[i
].map
== i
) {
2490 enabled_queues
[n_enabled
++] = i
;
2494 if (n_enabled
== 0 && total_txqs
!= 0) {
2495 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2500 for (i
= 0; i
< total_txqs
; i
++) {
2501 if (dev
->tx_q
[i
].map
!= i
) {
2502 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2503 k
= (k
+ 1) % n_enabled
;
2507 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2508 for (i
= 0; i
< total_txqs
; i
++) {
2509 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2512 free(enabled_queues
);
2516 * A new virtio-net device is added to a vhost port.
2521 struct netdev_dpdk
*dev
;
2522 bool exists
= false;
2524 char ifname
[IF_NAME_SZ
];
2526 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2528 ovs_mutex_lock(&dpdk_mutex
);
2529 /* Add device to the vhost port with the same name as that passed down. */
2530 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2531 ovs_mutex_lock(&dev
->mutex
);
2532 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2533 uint32_t qp_num
= rte_vhost_get_queue_num(vid
);
2535 /* Get NUMA information */
2536 newnode
= rte_vhost_get_numa_node(vid
);
2537 if (newnode
== -1) {
2539 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2542 newnode
= dev
->socket_id
;
2545 if (dev
->requested_n_txq
!= qp_num
2546 || dev
->requested_n_rxq
!= qp_num
2547 || dev
->requested_socket_id
!= newnode
) {
2548 dev
->requested_socket_id
= newnode
;
2549 dev
->requested_n_rxq
= qp_num
;
2550 dev
->requested_n_txq
= qp_num
;
2551 netdev_request_reconfigure(&dev
->up
);
2553 /* Reconfiguration not required. */
2554 dev
->vhost_reconfigured
= true;
2557 ovsrcu_index_set(&dev
->vid
, vid
);
2560 /* Disable notifications. */
2561 set_irq_status(vid
);
2562 netdev_change_seq_changed(&dev
->up
);
2563 ovs_mutex_unlock(&dev
->mutex
);
2566 ovs_mutex_unlock(&dev
->mutex
);
2568 ovs_mutex_unlock(&dpdk_mutex
);
2571 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2576 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2582 /* Clears mapping for all available queues of vhost interface. */
2584 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2585 OVS_REQUIRES(dev
->mutex
)
2589 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2590 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2595 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2596 * flag to stop any more packets from being sent or received to/from a VM and
2597 * ensure all currently queued packets have been sent/received before removing
2601 destroy_device(int vid
)
2603 struct netdev_dpdk
*dev
;
2604 bool exists
= false;
2605 char ifname
[IF_NAME_SZ
];
2607 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2609 ovs_mutex_lock(&dpdk_mutex
);
2610 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2611 if (netdev_dpdk_get_vid(dev
) == vid
) {
2613 ovs_mutex_lock(&dev
->mutex
);
2614 dev
->vhost_reconfigured
= false;
2615 ovsrcu_index_set(&dev
->vid
, -1);
2616 netdev_dpdk_txq_map_clear(dev
);
2618 netdev_change_seq_changed(&dev
->up
);
2619 ovs_mutex_unlock(&dev
->mutex
);
2625 ovs_mutex_unlock(&dpdk_mutex
);
2629 * Wait for other threads to quiesce after setting the 'virtio_dev'
2630 * to NULL, before returning.
2632 ovsrcu_synchronize();
2634 * As call to ovsrcu_synchronize() will end the quiescent state,
2635 * put thread back into quiescent state before returning.
2637 ovsrcu_quiesce_start();
2638 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2640 VLOG_INFO("vHost Device '%s' not found", ifname
);
2645 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2647 struct netdev_dpdk
*dev
;
2648 bool exists
= false;
2649 int qid
= queue_id
/ VIRTIO_QNUM
;
2650 char ifname
[IF_NAME_SZ
];
2652 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2654 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2658 ovs_mutex_lock(&dpdk_mutex
);
2659 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2660 ovs_mutex_lock(&dev
->mutex
);
2661 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2663 dev
->tx_q
[qid
].map
= qid
;
2665 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2667 netdev_dpdk_remap_txqs(dev
);
2669 ovs_mutex_unlock(&dev
->mutex
);
2672 ovs_mutex_unlock(&dev
->mutex
);
2674 ovs_mutex_unlock(&dpdk_mutex
);
2677 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2678 "changed to \'%s\'", queue_id
, qid
, ifname
,
2679 (enable
== 1) ? "enabled" : "disabled");
2681 VLOG_INFO("vHost Device '%s' not found", ifname
);
2689 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2691 return ovsrcu_index_get(&dev
->vid
);
2694 struct ingress_policer
*
2695 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2697 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2701 * These callbacks allow virtio-net devices to be added to vhost ports when
2702 * configuration has been fully complete.
2704 static const struct virtio_net_device_ops virtio_net_device_ops
=
2706 .new_device
= new_device
,
2707 .destroy_device
= destroy_device
,
2708 .vring_state_changed
= vring_state_changed
2712 start_vhost_loop(void *dummy OVS_UNUSED
)
2714 pthread_detach(pthread_self());
2715 /* Put the vhost thread into quiescent state. */
2716 ovsrcu_quiesce_start();
2717 rte_vhost_driver_session_start();
2722 netdev_dpdk_class_init(void)
2724 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2726 /* This function can be called for different classes. The initialization
2727 * needs to be done only once */
2728 if (ovsthread_once_start(&once
)) {
2729 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2730 unixctl_command_register("netdev-dpdk/set-admin-state",
2731 "[netdev] up|down", 1, 2,
2732 netdev_dpdk_set_admin_state
, NULL
);
2734 ovsthread_once_done(&once
);
2741 netdev_dpdk_vhost_class_init(void)
2743 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2745 /* This function can be called for different classes. The initialization
2746 * needs to be done only once */
2747 if (ovsthread_once_start(&once
)) {
2748 rte_vhost_driver_callback_register(&virtio_net_device_ops
);
2749 rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_HOST_TSO4
2750 | 1ULL << VIRTIO_NET_F_HOST_TSO6
2751 | 1ULL << VIRTIO_NET_F_CSUM
);
2752 ovs_thread_create("vhost_thread", start_vhost_loop
, NULL
);
2754 ovsthread_once_done(&once
);
2763 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2764 dpdk_port_t
*eth_port_id
)
2766 struct dpdk_ring
*ring_pair
;
2770 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2775 /* XXX: Add support for multiquque ring. */
2776 ring_name
= xasprintf("%s_tx", dev_name
);
2778 /* Create single producer tx ring, netdev does explicit locking. */
2779 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2782 if (ring_pair
->cring_tx
== NULL
) {
2783 rte_free(ring_pair
);
2787 ring_name
= xasprintf("%s_rx", dev_name
);
2789 /* Create single consumer rx ring, netdev does explicit locking. */
2790 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2793 if (ring_pair
->cring_rx
== NULL
) {
2794 rte_free(ring_pair
);
2798 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2799 &ring_pair
->cring_tx
, 1, SOCKET0
);
2802 rte_free(ring_pair
);
2806 ring_pair
->user_port_id
= port_no
;
2807 ring_pair
->eth_port_id
= port_id
;
2808 *eth_port_id
= port_id
;
2810 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2816 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
2817 OVS_REQUIRES(dpdk_mutex
)
2819 struct dpdk_ring
*ring_pair
;
2820 unsigned int port_no
;
2823 /* Names always start with "dpdkr" */
2824 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2829 /* Look through our list to find the device */
2830 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2831 if (ring_pair
->user_port_id
== port_no
) {
2832 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2833 /* Really all that is needed */
2834 *eth_port_id
= ring_pair
->eth_port_id
;
2838 /* Need to create the device rings */
2839 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2843 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2844 struct dp_packet_batch
*batch
, bool may_steal
,
2845 bool concurrent_txq
)
2847 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2850 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2851 * the rss hash field is clear. This is because the same mbuf may be
2852 * modified by the consumer of the ring and return into the datapath
2853 * without recalculating the RSS hash. */
2854 for (i
= 0; i
< batch
->count
; i
++) {
2855 dp_packet_rss_invalidate(batch
->packets
[i
]);
2858 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2863 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2865 dpdk_port_t port_no
= 0;
2868 ovs_mutex_lock(&dpdk_mutex
);
2870 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2875 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2876 rte_eth_dev_socket_id(port_no
));
2878 ovs_mutex_unlock(&dpdk_mutex
);
2885 * Initialize QoS configuration operations.
2888 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2891 rte_spinlock_init(&conf
->lock
);
2895 * Search existing QoS operations in qos_ops and compare each set of
2896 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2899 static const struct dpdk_qos_ops
*
2900 qos_lookup_name(const char *name
)
2902 const struct dpdk_qos_ops
*const *opsp
;
2904 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2905 const struct dpdk_qos_ops
*ops
= *opsp
;
2906 if (!strcmp(name
, ops
->qos_name
)) {
2914 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2917 const struct dpdk_qos_ops
*const *opsp
;
2919 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2920 const struct dpdk_qos_ops
*ops
= *opsp
;
2921 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
2922 sset_add(types
, ops
->qos_name
);
2929 netdev_dpdk_get_qos(const struct netdev
*netdev
,
2930 const char **typep
, struct smap
*details
)
2932 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2933 struct qos_conf
*qos_conf
;
2936 ovs_mutex_lock(&dev
->mutex
);
2937 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
2939 *typep
= qos_conf
->ops
->qos_name
;
2940 error
= (qos_conf
->ops
->qos_get
2941 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
2943 /* No QoS configuration set, return an empty string */
2946 ovs_mutex_unlock(&dev
->mutex
);
2952 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
2953 const struct smap
*details
)
2955 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2956 const struct dpdk_qos_ops
*new_ops
= NULL
;
2957 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
2960 ovs_mutex_lock(&dev
->mutex
);
2962 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
2964 new_ops
= qos_lookup_name(type
);
2966 if (!new_ops
|| !new_ops
->qos_construct
) {
2967 new_qos_conf
= NULL
;
2968 if (type
&& type
[0]) {
2971 } else if (qos_conf
&& qos_conf
->ops
== new_ops
2972 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
2973 new_qos_conf
= qos_conf
;
2975 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
2979 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
2980 type
, netdev
->name
, rte_strerror(error
));
2983 if (new_qos_conf
!= qos_conf
) {
2984 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
2986 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
2990 ovs_mutex_unlock(&dev
->mutex
);
2995 /* egress-policer details */
2997 struct egress_policer
{
2998 struct qos_conf qos_conf
;
2999 struct rte_meter_srtcm_params app_srtcm_params
;
3000 struct rte_meter_srtcm egress_meter
;
3004 egress_policer_details_to_param(const struct smap
*details
,
3005 struct rte_meter_srtcm_params
*params
)
3007 memset(params
, 0, sizeof *params
);
3008 params
->cir
= smap_get_ullong(details
, "cir", 0);
3009 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3014 egress_policer_qos_construct(const struct smap
*details
,
3015 struct qos_conf
**conf
)
3017 struct egress_policer
*policer
;
3020 policer
= xmalloc(sizeof *policer
);
3021 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3022 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3023 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3024 &policer
->app_srtcm_params
);
3026 *conf
= &policer
->qos_conf
;
3037 egress_policer_qos_destruct(struct qos_conf
*conf
)
3039 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3045 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3047 struct egress_policer
*policer
=
3048 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3050 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3051 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3057 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3058 const struct smap
*details
)
3060 struct egress_policer
*policer
=
3061 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3062 struct rte_meter_srtcm_params params
;
3064 egress_policer_details_to_param(details
, ¶ms
);
3066 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3070 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
)
3073 struct egress_policer
*policer
=
3074 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3076 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
, pkt_cnt
);
3081 static const struct dpdk_qos_ops egress_policer_ops
= {
3082 "egress-policer", /* qos_name */
3083 egress_policer_qos_construct
,
3084 egress_policer_qos_destruct
,
3085 egress_policer_qos_get
,
3086 egress_policer_qos_is_equal
,
3091 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3093 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3096 ovs_mutex_lock(&dev
->mutex
);
3098 if (netdev
->n_txq
== dev
->requested_n_txq
3099 && netdev
->n_rxq
== dev
->requested_n_rxq
3100 && dev
->mtu
== dev
->requested_mtu
3101 && dev
->rxq_size
== dev
->requested_rxq_size
3102 && dev
->txq_size
== dev
->requested_txq_size
3103 && dev
->socket_id
== dev
->requested_socket_id
) {
3104 /* Reconfiguration is unnecessary */
3109 rte_eth_dev_stop(dev
->port_id
);
3111 if (dev
->mtu
!= dev
->requested_mtu
3112 || dev
->socket_id
!= dev
->requested_socket_id
) {
3113 err
= netdev_dpdk_mempool_configure(dev
);
3119 netdev
->n_txq
= dev
->requested_n_txq
;
3120 netdev
->n_rxq
= dev
->requested_n_rxq
;
3122 dev
->rxq_size
= dev
->requested_rxq_size
;
3123 dev
->txq_size
= dev
->requested_txq_size
;
3125 rte_free(dev
->tx_q
);
3126 err
= dpdk_eth_dev_init(dev
);
3127 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3132 netdev_change_seq_changed(netdev
);
3135 ovs_mutex_unlock(&dev
->mutex
);
3140 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3141 OVS_REQUIRES(dev
->mutex
)
3143 dev
->up
.n_txq
= dev
->requested_n_txq
;
3144 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3147 /* Enable TX queue 0 by default if it wasn't disabled. */
3148 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3149 dev
->tx_q
[0].map
= 0;
3152 netdev_dpdk_remap_txqs(dev
);
3154 if (dev
->requested_socket_id
!= dev
->socket_id
3155 || dev
->requested_mtu
!= dev
->mtu
) {
3156 err
= netdev_dpdk_mempool_configure(dev
);
3160 netdev_change_seq_changed(&dev
->up
);
3164 if (netdev_dpdk_get_vid(dev
) >= 0) {
3165 dev
->vhost_reconfigured
= true;
3172 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3174 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3177 ovs_mutex_lock(&dev
->mutex
);
3178 err
= dpdk_vhost_reconfigure_helper(dev
);
3179 ovs_mutex_unlock(&dev
->mutex
);
3185 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3187 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3190 ovs_mutex_lock(&dev
->mutex
);
3192 /* Configure vHost client mode if requested and if the following criteria
3194 * 1. Device hasn't been registered yet.
3195 * 2. A path has been specified.
3197 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3198 && strlen(dev
->vhost_id
)) {
3199 /* Register client-mode device */
3200 err
= rte_vhost_driver_register(dev
->vhost_id
,
3201 RTE_VHOST_USER_CLIENT
);
3203 VLOG_ERR("vhost-user device setup failure for device %s\n",
3207 /* Configuration successful */
3208 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3209 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3210 "using client socket '%s'",
3211 dev
->up
.name
, dev
->vhost_id
);
3215 err
= dpdk_vhost_reconfigure_helper(dev
);
3218 ovs_mutex_unlock(&dev
->mutex
);
3223 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3224 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3225 GET_CARRIER, GET_STATS, \
3226 GET_FEATURES, GET_STATUS, \
3227 RECONFIGURE, RXQ_RECV) \
3230 true, /* is_pmd */ \
3232 NULL, /* netdev_dpdk_run */ \
3233 NULL, /* netdev_dpdk_wait */ \
3235 netdev_dpdk_alloc, \
3238 netdev_dpdk_dealloc, \
3239 netdev_dpdk_get_config, \
3241 NULL, /* get_tunnel_config */ \
3242 NULL, /* build header */ \
3243 NULL, /* push header */ \
3244 NULL, /* pop header */ \
3245 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3249 NULL, /* send_wait */ \
3251 netdev_dpdk_set_etheraddr, \
3252 netdev_dpdk_get_etheraddr, \
3253 netdev_dpdk_get_mtu, \
3254 netdev_dpdk_set_mtu, \
3255 netdev_dpdk_get_ifindex, \
3257 netdev_dpdk_get_carrier_resets, \
3258 netdev_dpdk_set_miimon, \
3261 NULL, /* set_advertisements */ \
3262 NULL, /* get_pt_mode */ \
3264 netdev_dpdk_set_policing, \
3265 netdev_dpdk_get_qos_types, \
3266 NULL, /* get_qos_capabilities */ \
3267 netdev_dpdk_get_qos, \
3268 netdev_dpdk_set_qos, \
3269 NULL, /* get_queue */ \
3270 NULL, /* set_queue */ \
3271 NULL, /* delete_queue */ \
3272 NULL, /* get_queue_stats */ \
3273 NULL, /* queue_dump_start */ \
3274 NULL, /* queue_dump_next */ \
3275 NULL, /* queue_dump_done */ \
3276 NULL, /* dump_queue_stats */ \
3278 NULL, /* set_in4 */ \
3279 NULL, /* get_addr_list */ \
3280 NULL, /* add_router */ \
3281 NULL, /* get_next_hop */ \
3283 NULL, /* arp_lookup */ \
3285 netdev_dpdk_update_flags, \
3288 netdev_dpdk_rxq_alloc, \
3289 netdev_dpdk_rxq_construct, \
3290 netdev_dpdk_rxq_destruct, \
3291 netdev_dpdk_rxq_dealloc, \
3293 NULL, /* rx_wait */ \
3294 NULL, /* rxq_drain */ \
3298 static const struct netdev_class dpdk_class
=
3301 netdev_dpdk_class_init
,
3302 netdev_dpdk_construct
,
3303 netdev_dpdk_destruct
,
3304 netdev_dpdk_set_config
,
3305 netdev_dpdk_set_tx_multiq
,
3306 netdev_dpdk_eth_send
,
3307 netdev_dpdk_get_carrier
,
3308 netdev_dpdk_get_stats
,
3309 netdev_dpdk_get_features
,
3310 netdev_dpdk_get_status
,
3311 netdev_dpdk_reconfigure
,
3312 netdev_dpdk_rxq_recv
);
3314 static const struct netdev_class dpdk_ring_class
=
3317 netdev_dpdk_class_init
,
3318 netdev_dpdk_ring_construct
,
3319 netdev_dpdk_destruct
,
3320 netdev_dpdk_ring_set_config
,
3321 netdev_dpdk_set_tx_multiq
,
3322 netdev_dpdk_ring_send
,
3323 netdev_dpdk_get_carrier
,
3324 netdev_dpdk_get_stats
,
3325 netdev_dpdk_get_features
,
3326 netdev_dpdk_get_status
,
3327 netdev_dpdk_reconfigure
,
3328 netdev_dpdk_rxq_recv
);
3330 static const struct netdev_class dpdk_vhost_class
=
3333 netdev_dpdk_vhost_class_init
,
3334 netdev_dpdk_vhost_construct
,
3335 netdev_dpdk_vhost_destruct
,
3338 netdev_dpdk_vhost_send
,
3339 netdev_dpdk_vhost_get_carrier
,
3340 netdev_dpdk_vhost_get_stats
,
3343 netdev_dpdk_vhost_reconfigure
,
3344 netdev_dpdk_vhost_rxq_recv
);
3345 static const struct netdev_class dpdk_vhost_client_class
=
3347 "dpdkvhostuserclient",
3348 netdev_dpdk_vhost_class_init
,
3349 netdev_dpdk_vhost_client_construct
,
3350 netdev_dpdk_vhost_destruct
,
3351 netdev_dpdk_vhost_client_set_config
,
3353 netdev_dpdk_vhost_send
,
3354 netdev_dpdk_vhost_get_carrier
,
3355 netdev_dpdk_vhost_get_stats
,
3358 netdev_dpdk_vhost_client_reconfigure
,
3359 netdev_dpdk_vhost_rxq_recv
);
3362 netdev_dpdk_register(void)
3364 netdev_register_provider(&dpdk_class
);
3365 netdev_register_provider(&dpdk_ring_class
);
3366 netdev_register_provider(&dpdk_vhost_class
);
3367 netdev_register_provider(&dpdk_vhost_client_class
);