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 /* Max and min number of packets in the mempool. OVS tries to allocate a
95 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
96 * enough hugepages) we keep halving the number until the allocation succeeds
97 * or we reach MIN_NB_MBUF */
99 #define MAX_NB_MBUF (4096 * 64)
100 #define MIN_NB_MBUF (4096 * 4)
101 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
103 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
104 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/ MIN_NB_MBUF
)
107 /* The smallest possible NB_MBUF that we're going to try should be a multiple
108 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
109 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/ MIN_NB_MBUF
))
113 * DPDK XSTATS Counter names definition
115 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
116 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
117 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
118 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
119 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
120 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
121 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
123 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
124 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
125 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
126 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
127 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
128 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
129 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
131 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
132 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
133 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
134 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
135 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
136 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
137 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
138 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
142 /* Default size of Physical NIC RXQ */
143 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
144 /* Default size of Physical NIC TXQ */
145 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
146 /* Maximum size of Physical NIC Queues */
147 #define NIC_PORT_MAX_Q_SIZE 4096
149 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
150 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
151 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
152 * yet mapped to another queue. */
154 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
156 /* DPDK library uses uint16_t for port_id. */
157 typedef uint16_t dpdk_port_t
;
158 #define DPDK_PORT_ID_FMT "%"PRIu16
160 #define VHOST_ENQ_RETRY_NUM 8
161 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
163 static const struct rte_eth_conf port_conf
= {
165 .mq_mode
= ETH_MQ_RX_RSS
,
167 .header_split
= 0, /* Header Split disabled */
168 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
169 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
170 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
176 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
180 .mq_mode
= ETH_MQ_TX_NONE
,
185 * These callbacks allow virtio-net devices to be added to vhost ports when
186 * configuration has been fully completed.
188 static int new_device(int vid
);
189 static void destroy_device(int vid
);
190 static int vring_state_changed(int vid
, uint16_t queue_id
, int enable
);
191 static const struct vhost_device_ops virtio_net_device_ops
=
193 .new_device
= new_device
,
194 .destroy_device
= destroy_device
,
195 .vring_state_changed
= vring_state_changed
,
196 .features_changed
= NULL
199 enum { DPDK_RING_SIZE
= 256 };
200 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
201 enum { DRAIN_TSC
= 200000ULL };
208 /* Quality of Service */
210 /* An instance of a QoS configuration. Always associated with a particular
213 * Each QoS implementation subclasses this with whatever additional data it
217 const struct dpdk_qos_ops
*ops
;
221 /* A particular implementation of dpdk QoS operations.
223 * The functions below return 0 if successful or a positive errno value on
224 * failure, except where otherwise noted. All of them must be provided, except
225 * where otherwise noted.
227 struct dpdk_qos_ops
{
229 /* Name of the QoS type */
230 const char *qos_name
;
232 /* Called to construct a qos_conf object. The implementation should make
233 * the appropriate calls to configure QoS according to 'details'.
235 * The contents of 'details' should be documented as valid for 'ovs_name'
236 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
237 * (which is built as ovs-vswitchd.conf.db(8)).
239 * This function must return 0 if and only if it sets '*conf' to an
240 * initialized 'struct qos_conf'.
242 * For all QoS implementations it should always be non-null.
244 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
246 /* Destroys the data structures allocated by the implementation as part of
249 * For all QoS implementations it should always be non-null.
251 void (*qos_destruct
)(struct qos_conf
*conf
);
253 /* Retrieves details of 'conf' configuration into 'details'.
255 * The contents of 'details' should be documented as valid for 'ovs_name'
256 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
257 * (which is built as ovs-vswitchd.conf.db(8)).
259 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
261 /* Returns true if 'conf' is already configured according to 'details'.
263 * The contents of 'details' should be documented as valid for 'ovs_name'
264 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
265 * (which is built as ovs-vswitchd.conf.db(8)).
267 * For all QoS implementations it should always be non-null.
269 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
270 const struct smap
*details
);
272 /* Modify an array of rte_mbufs. The modification is specific to
273 * each qos implementation.
275 * The function should take and array of mbufs and an int representing
276 * the current number of mbufs present in the array.
278 * After the function has performed a qos modification to the array of
279 * mbufs it returns an int representing the number of mbufs now present in
280 * the array. This value is can then be passed to the port send function
281 * along with the modified array for transmission.
283 * For all QoS implementations it should always be non-null.
285 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
286 int pkt_cnt
, bool may_steal
);
289 /* dpdk_qos_ops for each type of user space QoS implementation */
290 static const struct dpdk_qos_ops egress_policer_ops
;
293 * Array of dpdk_qos_ops, contains pointer to all supported QoS
296 static const struct dpdk_qos_ops
*const qos_confs
[] = {
301 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
303 /* Contains all 'struct dpdk_dev's. */
304 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
305 = OVS_LIST_INITIALIZER(&dpdk_list
);
307 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
308 = OVS_MUTEX_INITIALIZER
;
310 static struct ovs_list dpdk_mp_list
OVS_GUARDED_BY(dpdk_mp_mutex
)
311 = OVS_LIST_INITIALIZER(&dpdk_mp_list
);
315 struct rte_mempool
*mp
;
319 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
323 /* There should be one 'struct dpdk_tx_queue' created for
325 struct dpdk_tx_queue
{
326 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
327 * from concurrent access. It is used only
328 * if the queue is shared among different
329 * pmd threads (see 'concurrent_txq'). */
330 int map
; /* Mapping of configured vhost-user queues
331 * to enabled by guest. */
334 /* dpdk has no way to remove dpdk ring ethernet devices
335 so we have to keep them around once they've been created
338 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
339 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
342 /* For the client rings */
343 struct rte_ring
*cring_tx
;
344 struct rte_ring
*cring_rx
;
345 unsigned int user_port_id
; /* User given port no, parsed from port name */
346 dpdk_port_t eth_port_id
; /* ethernet device port id */
347 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
350 struct ingress_policer
{
351 struct rte_meter_srtcm_params app_srtcm_params
;
352 struct rte_meter_srtcm in_policer
;
353 rte_spinlock_t policer_lock
;
356 enum dpdk_hw_ol_features
{
357 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
361 * In order to avoid confusion in variables names, following naming convention
362 * should be used, if possible:
364 * 'struct netdev' : 'netdev'
365 * 'struct netdev_dpdk' : 'dev'
366 * 'struct netdev_rxq' : 'rxq'
367 * 'struct netdev_rxq_dpdk' : 'rx'
370 * struct netdev *netdev = netdev_from_name(name);
371 * struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
373 * Also, 'netdev' should be used instead of 'dev->up', where 'netdev' was
378 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline0
,
381 /* If true, device was attached by rte_eth_dev_attach(). */
383 struct eth_addr hwaddr
;
388 enum dpdk_dev_type type
;
389 enum netdev_flags flags
;
390 char *devargs
; /* Device arguments for dpdk ports */
391 struct dpdk_tx_queue
*tx_q
;
392 struct rte_eth_link link
;
394 /* 4 pad bytes here. */
397 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline1
,
398 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
399 struct dpdk_mp
*dpdk_mp
;
401 /* virtio identifier for vhost devices */
404 /* True if vHost device is 'up' and has been reconfigured at least once */
405 bool vhost_reconfigured
;
406 /* 3 pad bytes here. */
409 PADDED_MEMBERS(CACHE_LINE_SIZE
,
410 /* Identifier used to distinguish vhost devices from each other. */
411 char vhost_id
[PATH_MAX
];
414 PADDED_MEMBERS(CACHE_LINE_SIZE
,
417 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
419 /* QoS configuration and lock for the device */
420 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
422 /* Ingress Policer */
423 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
424 uint32_t policer_rate
;
425 uint32_t policer_burst
;
428 PADDED_MEMBERS(CACHE_LINE_SIZE
,
429 struct netdev_stats stats
;
431 rte_spinlock_t stats_lock
;
432 /* 44 pad bytes here. */
435 PADDED_MEMBERS(CACHE_LINE_SIZE
,
436 /* The following properties cannot be changed when a device is running,
437 * so we remember the request and update them next time
438 * netdev_dpdk*_reconfigure() is called */
442 int requested_rxq_size
;
443 int requested_txq_size
;
445 /* Number of rx/tx descriptors for physical devices */
449 /* Socket ID detected when vHost device is brought up */
450 int requested_socket_id
;
452 /* Denotes whether vHost port is client/server mode */
453 uint64_t vhost_driver_flags
;
455 /* DPDK-ETH Flow control */
456 struct rte_eth_fc_conf fc_conf
;
458 /* DPDK-ETH hardware offload features,
459 * from the enum set 'dpdk_hw_ol_features' */
460 uint32_t hw_ol_features
;
463 PADDED_MEMBERS(CACHE_LINE_SIZE
,
464 /* Names of all XSTATS counters */
465 struct rte_eth_xstat_name
*rte_xstats_names
;
466 int rte_xstats_names_size
;
467 int rte_xstats_ids_size
;
468 uint64_t *rte_xstats_ids
;
472 struct netdev_rxq_dpdk
{
473 struct netdev_rxq up
;
477 static void netdev_dpdk_destruct(struct netdev
*netdev
);
478 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
480 static void netdev_dpdk_clear_xstats(struct netdev_dpdk
*dev
);
482 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
484 struct ingress_policer
*
485 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
488 is_dpdk_class(const struct netdev_class
*class)
490 return class->destruct
== netdev_dpdk_destruct
491 || class->destruct
== netdev_dpdk_vhost_destruct
;
494 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
495 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
496 * value, insufficient buffers are allocated to accomodate the packet in its
497 * entirety. Furthermore, certain drivers need to ensure that there is also
498 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
499 * frames). If the RX buffer is too small, then the driver enables scatter RX
500 * behaviour, which reduces performance. To prevent this, use a buffer size
501 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
504 dpdk_buf_size(int mtu
)
506 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
507 NETDEV_DPDK_MBUF_ALIGN
);
510 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
512 * Unlike xmalloc(), this function can return NULL on failure. */
514 dpdk_rte_mzalloc(size_t sz
)
516 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
520 free_dpdk_buf(struct dp_packet
*p
)
522 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
524 rte_pktmbuf_free(pkt
);
528 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
529 void *opaque_arg OVS_UNUSED
,
531 unsigned i OVS_UNUSED
)
533 struct rte_mbuf
*pkt
= _p
;
535 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
538 static struct dpdk_mp
*
539 dpdk_mp_create(int socket_id
, int mtu
)
545 dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
549 dmp
->socket_id
= socket_id
;
552 /* XXX: this is a really rough method of provisioning memory.
553 * It's impossible to determine what the exact memory requirements are
554 * when the number of ports and rxqs that utilize a particular mempool can
555 * change dynamically at runtime. For now, use this rough heurisitic.
557 if (mtu
>= ETHER_MTU
) {
558 mp_size
= MAX_NB_MBUF
;
560 mp_size
= MIN_NB_MBUF
;
564 mp_name
= xasprintf("ovs_mp_%d_%d_%u", dmp
->mtu
, dmp
->socket_id
,
567 dmp
->mp
= rte_pktmbuf_pool_create(mp_name
, mp_size
,
569 sizeof (struct dp_packet
)
570 - sizeof (struct rte_mbuf
),
572 - sizeof(struct dp_packet
),
575 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
580 /* rte_pktmbuf_pool_create has done some initialization of the
581 * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init
582 * initializes some OVS specific fields of dp_packet.
584 rte_mempool_obj_iter(dmp
->mp
, ovs_rte_pktmbuf_init
, NULL
);
587 } while (rte_errno
== ENOMEM
&& (mp_size
/= 2) >= MIN_NB_MBUF
);
593 static struct dpdk_mp
*
594 dpdk_mp_get(int socket_id
, int mtu
)
598 ovs_mutex_lock(&dpdk_mp_mutex
);
599 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
600 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
606 dmp
= dpdk_mp_create(socket_id
, mtu
);
608 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
613 ovs_mutex_unlock(&dpdk_mp_mutex
);
618 /* Release an existing mempool. */
620 dpdk_mp_put(struct dpdk_mp
*dmp
)
626 ovs_mutex_lock(&dpdk_mp_mutex
);
627 ovs_assert(dmp
->refcount
);
629 if (!--dmp
->refcount
) {
630 ovs_list_remove(&dmp
->list_node
);
631 rte_mempool_free(dmp
->mp
);
634 ovs_mutex_unlock(&dpdk_mp_mutex
);
637 /* Tries to allocate new mempool on requested_socket_id with
638 * mbuf size corresponding to requested_mtu.
639 * On success new configuration will be applied.
640 * On error, device will be left unchanged. */
642 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
643 OVS_REQUIRES(dev
->mutex
)
645 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
648 mp
= dpdk_mp_get(dev
->requested_socket_id
, FRAME_LEN_TO_MTU(buf_size
));
650 VLOG_ERR("Failed to create memory pool for netdev "
651 "%s, with MTU %d on socket %d: %s\n",
652 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
653 rte_strerror(rte_errno
));
656 dpdk_mp_put(dev
->dpdk_mp
);
658 dev
->mtu
= dev
->requested_mtu
;
659 dev
->socket_id
= dev
->requested_socket_id
;
660 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
667 check_link_status(struct netdev_dpdk
*dev
)
669 struct rte_eth_link link
;
671 rte_eth_link_get_nowait(dev
->port_id
, &link
);
673 if (dev
->link
.link_status
!= link
.link_status
) {
674 netdev_change_seq_changed(&dev
->up
);
676 dev
->link_reset_cnt
++;
678 if (dev
->link
.link_status
) {
680 "Port "DPDK_PORT_ID_FMT
" Link Up - speed %u Mbps - %s",
681 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
682 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
)
683 ? "full-duplex" : "half-duplex");
685 VLOG_DBG_RL(&rl
, "Port "DPDK_PORT_ID_FMT
" Link Down",
692 dpdk_watchdog(void *dummy OVS_UNUSED
)
694 struct netdev_dpdk
*dev
;
696 pthread_detach(pthread_self());
699 ovs_mutex_lock(&dpdk_mutex
);
700 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
701 ovs_mutex_lock(&dev
->mutex
);
702 if (dev
->type
== DPDK_DEV_ETH
) {
703 check_link_status(dev
);
705 ovs_mutex_unlock(&dev
->mutex
);
707 ovs_mutex_unlock(&dpdk_mutex
);
708 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
715 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
719 struct rte_eth_conf conf
= port_conf
;
721 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
723 if (dev
->mtu
> ETHER_MTU
) {
724 conf
.rxmode
.enable_scatter
= 1;
727 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
728 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
729 /* A device may report more queues than it makes available (this has
730 * been observed for Intel xl710, which reserves some of them for
731 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
732 * available. When this happens we can retry the configuration
733 * and request less queues */
734 while (n_rxq
&& n_txq
) {
736 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
739 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
741 VLOG_WARN("Interface %s eth_dev setup error %s\n",
742 dev
->up
.name
, rte_strerror(-diag
));
746 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
748 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
749 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
753 for (i
= 0; i
< n_txq
; i
++) {
754 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
755 dev
->socket_id
, NULL
);
757 VLOG_INFO("Interface %s unable to setup txq(%d): %s",
758 dev
->up
.name
, i
, rte_strerror(-diag
));
764 /* Retry with less tx queues */
769 for (i
= 0; i
< n_rxq
; i
++) {
770 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
771 dev
->socket_id
, NULL
,
774 VLOG_INFO("Interface %s unable to setup rxq(%d): %s",
775 dev
->up
.name
, i
, rte_strerror(-diag
));
781 /* Retry with less rx queues */
786 dev
->up
.n_rxq
= n_rxq
;
787 dev
->up
.n_txq
= n_txq
;
796 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
798 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
799 VLOG_WARN("Failed to enable flow control on device "DPDK_PORT_ID_FMT
,
805 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
806 OVS_REQUIRES(dev
->mutex
)
808 struct rte_pktmbuf_pool_private
*mbp_priv
;
809 struct rte_eth_dev_info info
;
810 struct ether_addr eth_addr
;
813 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
814 DEV_RX_OFFLOAD_TCP_CKSUM
|
815 DEV_RX_OFFLOAD_IPV4_CKSUM
;
817 rte_eth_dev_info_get(dev
->port_id
, &info
);
819 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
820 rx_chksm_offload_capa
) {
821 VLOG_WARN("Rx checksum offload is not supported on port "
822 DPDK_PORT_ID_FMT
, dev
->port_id
);
823 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
825 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
828 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
829 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
831 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
833 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
834 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
838 diag
= rte_eth_dev_start(dev
->port_id
);
840 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
841 rte_strerror(-diag
));
845 rte_eth_promiscuous_enable(dev
->port_id
);
846 rte_eth_allmulticast_enable(dev
->port_id
);
848 memset(ð_addr
, 0x0, sizeof(eth_addr
));
849 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
850 VLOG_INFO_RL(&rl
, "Port "DPDK_PORT_ID_FMT
": "ETH_ADDR_FMT
,
851 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
853 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
854 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
856 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
857 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
859 /* Get the Flow control configuration for DPDK-ETH */
860 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
862 VLOG_DBG("cannot get flow control parameters on port "DPDK_PORT_ID_FMT
863 ", err=%d", dev
->port_id
, diag
);
869 static struct netdev_dpdk
*
870 netdev_dpdk_cast(const struct netdev
*netdev
)
872 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
875 static struct netdev
*
876 netdev_dpdk_alloc(void)
878 struct netdev_dpdk
*dev
;
880 dev
= dpdk_rte_mzalloc(sizeof *dev
);
888 static struct dpdk_tx_queue
*
889 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
891 struct dpdk_tx_queue
*txqs
;
894 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
896 for (i
= 0; i
< n_txqs
; i
++) {
897 /* Initialize map for vhost devices. */
898 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
899 rte_spinlock_init(&txqs
[i
].tx_lock
);
907 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
908 enum dpdk_dev_type type
, int socket_id
)
909 OVS_REQUIRES(dpdk_mutex
)
911 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
913 ovs_mutex_init(&dev
->mutex
);
915 rte_spinlock_init(&dev
->stats_lock
);
917 /* If the 'sid' is negative, it means that the kernel fails
918 * to obtain the pci numa info. In that situation, always
920 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
921 dev
->requested_socket_id
= dev
->socket_id
;
922 dev
->port_id
= port_no
;
925 dev
->requested_mtu
= ETHER_MTU
;
926 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
927 ovsrcu_index_init(&dev
->vid
, -1);
928 dev
->vhost_reconfigured
= false;
929 dev
->attached
= false;
931 ovsrcu_init(&dev
->qos_conf
, NULL
);
933 ovsrcu_init(&dev
->ingress_policer
, NULL
);
934 dev
->policer_rate
= 0;
935 dev
->policer_burst
= 0;
939 dev
->requested_n_rxq
= NR_QUEUE
;
940 dev
->requested_n_txq
= NR_QUEUE
;
941 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
942 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
944 /* Initialize the flow control to NULL */
945 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
947 /* Initilize the hardware offload flags to 0 */
948 dev
->hw_ol_features
= 0;
950 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
952 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
954 netdev_request_reconfigure(netdev
);
956 dev
->rte_xstats_names
= NULL
;
957 dev
->rte_xstats_names_size
= 0;
959 dev
->rte_xstats_ids
= NULL
;
960 dev
->rte_xstats_ids_size
= 0;
965 /* dev_name must be the prefix followed by a positive decimal number.
966 * (no leading + or - signs are allowed) */
968 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
969 unsigned int *port_no
)
973 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
977 cport
= dev_name
+ strlen(prefix
);
979 if (str_to_uint(cport
, 10, port_no
)) {
987 vhost_common_construct(struct netdev
*netdev
)
988 OVS_REQUIRES(dpdk_mutex
)
990 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
991 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
993 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
998 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
999 DPDK_DEV_VHOST
, socket_id
);
1003 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
1005 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1006 const char *name
= netdev
->name
;
1009 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
1010 * the file system. '/' or '\' would traverse directories, so they're not
1011 * acceptable in 'name'. */
1012 if (strchr(name
, '/') || strchr(name
, '\\')) {
1013 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
1014 "A valid name must not include '/' or '\\'",
1019 ovs_mutex_lock(&dpdk_mutex
);
1020 /* Take the name of the vhost-user port and append it to the location where
1021 * the socket is to be created, then register the socket.
1023 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
1024 dpdk_get_vhost_sock_dir(), name
);
1026 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
1027 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
1029 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
1033 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
1034 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1035 dev
->vhost_id
, name
);
1038 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
1039 &virtio_net_device_ops
);
1041 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1042 "port: %s\n", name
);
1046 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1047 1ULL << VIRTIO_NET_F_HOST_TSO4
1048 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1049 | 1ULL << VIRTIO_NET_F_CSUM
);
1051 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1052 "port: %s\n", name
);
1056 err
= rte_vhost_driver_start(dev
->vhost_id
);
1058 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1059 "port: %s\n", name
);
1063 err
= vhost_common_construct(netdev
);
1065 VLOG_ERR("vhost_common_construct failed for vhost user "
1066 "port: %s\n", name
);
1070 ovs_mutex_unlock(&dpdk_mutex
);
1071 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1072 "please migrate to dpdkvhostuserclient ports.");
1077 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1081 ovs_mutex_lock(&dpdk_mutex
);
1082 err
= vhost_common_construct(netdev
);
1084 VLOG_ERR("vhost_common_construct failed for vhost user client"
1085 "port: %s\n", netdev
->name
);
1087 ovs_mutex_unlock(&dpdk_mutex
);
1092 netdev_dpdk_construct(struct netdev
*netdev
)
1096 ovs_mutex_lock(&dpdk_mutex
);
1097 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1098 DPDK_DEV_ETH
, SOCKET0
);
1099 ovs_mutex_unlock(&dpdk_mutex
);
1104 common_destruct(struct netdev_dpdk
*dev
)
1105 OVS_REQUIRES(dpdk_mutex
)
1106 OVS_EXCLUDED(dev
->mutex
)
1108 rte_free(dev
->tx_q
);
1109 dpdk_mp_put(dev
->dpdk_mp
);
1111 ovs_list_remove(&dev
->list_node
);
1112 free(ovsrcu_get_protected(struct ingress_policer
*,
1113 &dev
->ingress_policer
));
1114 ovs_mutex_destroy(&dev
->mutex
);
1118 netdev_dpdk_destruct(struct netdev
*netdev
)
1120 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1121 char devname
[RTE_ETH_NAME_MAX_LEN
];
1123 ovs_mutex_lock(&dpdk_mutex
);
1125 rte_eth_dev_stop(dev
->port_id
);
1127 if (dev
->attached
) {
1128 rte_eth_dev_close(dev
->port_id
);
1129 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1130 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1132 VLOG_INFO("Device '%s' has been detached", devname
);
1136 netdev_dpdk_clear_xstats(dev
);
1138 common_destruct(dev
);
1140 ovs_mutex_unlock(&dpdk_mutex
);
1143 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1144 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1145 * deadlock, none of the mutexes must be held while calling this function. */
1147 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1149 OVS_EXCLUDED(dpdk_mutex
)
1150 OVS_EXCLUDED(dev
->mutex
)
1152 return rte_vhost_driver_unregister(vhost_id
);
1156 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1158 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1161 ovs_mutex_lock(&dpdk_mutex
);
1163 /* Guest becomes an orphan if still attached. */
1164 if (netdev_dpdk_get_vid(dev
) >= 0
1165 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1166 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1168 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1169 "socket '%s' must be restarted.", dev
->vhost_id
);
1172 vhost_id
= xstrdup(dev
->vhost_id
);
1174 common_destruct(dev
);
1176 ovs_mutex_unlock(&dpdk_mutex
);
1182 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1183 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1184 netdev
->name
, vhost_id
);
1185 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1186 /* OVS server mode - remove this socket from list for deletion */
1187 fatal_signal_remove_file_to_unlink(vhost_id
);
1194 netdev_dpdk_dealloc(struct netdev
*netdev
)
1196 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1202 netdev_dpdk_clear_xstats(struct netdev_dpdk
*dev
)
1204 /* If statistics are already allocated, we have to
1205 * reconfigure, as port_id could have been changed. */
1206 if (dev
->rte_xstats_names
) {
1207 free(dev
->rte_xstats_names
);
1208 dev
->rte_xstats_names
= NULL
;
1209 dev
->rte_xstats_names_size
= 0;
1211 if (dev
->rte_xstats_ids
) {
1212 free(dev
->rte_xstats_ids
);
1213 dev
->rte_xstats_ids
= NULL
;
1214 dev
->rte_xstats_ids_size
= 0;
1219 netdev_dpdk_get_xstat_name(struct netdev_dpdk
*dev
, uint64_t id
)
1221 if (id
>= dev
->rte_xstats_names_size
) {
1224 return dev
->rte_xstats_names
[id
].name
;
1228 netdev_dpdk_configure_xstats(struct netdev_dpdk
*dev
)
1229 OVS_REQUIRES(dev
->mutex
)
1233 struct rte_eth_xstat
*rte_xstats
;
1238 /* Retrieving all XSTATS names. If something will go wrong
1239 * or amount of counters will be equal 0, rte_xstats_names
1240 * buffer will be marked as NULL, and any further xstats
1241 * query won't be performed (e.g. during netdev_dpdk_get_stats
1247 if (dev
->rte_xstats_names
== NULL
|| dev
->rte_xstats_ids
== NULL
) {
1248 dev
->rte_xstats_names_size
=
1249 rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
1251 if (dev
->rte_xstats_names_size
< 0) {
1252 VLOG_WARN("Cannot get XSTATS for port: "DPDK_PORT_ID_FMT
,
1254 dev
->rte_xstats_names_size
= 0;
1256 /* Reserve memory for xstats names and values */
1257 dev
->rte_xstats_names
= xcalloc(dev
->rte_xstats_names_size
,
1258 sizeof *dev
->rte_xstats_names
);
1260 if (dev
->rte_xstats_names
) {
1261 /* Retreive xstats names */
1263 rte_eth_xstats_get_names(dev
->port_id
,
1264 dev
->rte_xstats_names
,
1265 dev
->rte_xstats_names_size
);
1267 if (rte_xstats_len
< 0) {
1268 VLOG_WARN("Cannot get XSTATS names for port: "
1269 DPDK_PORT_ID_FMT
, dev
->port_id
);
1271 } else if (rte_xstats_len
!= dev
->rte_xstats_names_size
) {
1272 VLOG_WARN("XSTATS size doesn't match for port: "
1273 DPDK_PORT_ID_FMT
, dev
->port_id
);
1277 dev
->rte_xstats_ids
= xcalloc(dev
->rte_xstats_names_size
,
1280 /* We have to calculate number of counters */
1281 rte_xstats
= xmalloc(rte_xstats_len
* sizeof *rte_xstats
);
1282 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
1284 /* Retreive xstats values */
1285 if (rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
1286 rte_xstats_len
) > 0) {
1287 dev
->rte_xstats_ids_size
= 0;
1289 for (uint32_t i
= 0; i
< rte_xstats_len
; i
++) {
1290 id
= rte_xstats
[i
].id
;
1291 name
= netdev_dpdk_get_xstat_name(dev
, id
);
1292 /* We need to filter out everything except
1293 * dropped, error and management counters */
1294 if (string_ends_with(name
, "_errors") ||
1295 strstr(name
, "_management_") ||
1296 string_ends_with(name
, "_dropped")) {
1298 dev
->rte_xstats_ids
[xstats_no
] = id
;
1302 dev
->rte_xstats_ids_size
= xstats_no
;
1305 VLOG_WARN("Can't get XSTATS IDs for port: "
1306 DPDK_PORT_ID_FMT
, dev
->port_id
);
1313 /* Already configured */
1319 netdev_dpdk_clear_xstats(dev
);
1325 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1327 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1329 ovs_mutex_lock(&dev
->mutex
);
1331 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1332 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1333 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1334 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1335 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1337 if (dev
->type
== DPDK_DEV_ETH
) {
1338 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1339 dev
->requested_rxq_size
);
1340 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1342 smap_add_format(args
, "requested_txq_descriptors", "%d",
1343 dev
->requested_txq_size
);
1344 smap_add_format(args
, "configured_txq_descriptors", "%d",
1346 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1347 smap_add(args
, "rx_csum_offload", "true");
1349 smap_add(args
, "rx_csum_offload", "false");
1352 ovs_mutex_unlock(&dev
->mutex
);
1357 static struct netdev_dpdk
*
1358 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1359 OVS_REQUIRES(dpdk_mutex
)
1361 struct netdev_dpdk
*dev
;
1363 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1364 if (dev
->port_id
== port_id
) {
1373 netdev_dpdk_get_port_by_mac(const char *mac_str
)
1375 dpdk_port_t port_id
;
1376 struct eth_addr mac
, port_mac
;
1378 if (!eth_addr_from_string(mac_str
, &mac
)) {
1379 VLOG_ERR("invalid mac: %s", mac_str
);
1380 return DPDK_ETH_PORT_ID_INVALID
;
1383 RTE_ETH_FOREACH_DEV (port_id
) {
1384 struct ether_addr ea
;
1386 rte_eth_macaddr_get(port_id
, &ea
);
1387 memcpy(port_mac
.ea
, ea
.addr_bytes
, ETH_ADDR_LEN
);
1388 if (eth_addr_equals(mac
, port_mac
)) {
1393 return DPDK_ETH_PORT_ID_INVALID
;
1397 * Normally, a PCI id is enough for identifying a specific DPDK port.
1398 * However, for some NICs having multiple ports sharing the same PCI
1399 * id, using PCI id won't work then.
1401 * To fix that, here one more method is introduced: "class=eth,mac=$MAC".
1403 * Note that the compatibility is fully kept: user can still use the
1404 * PCI id for adding ports (when it's enough for them).
1407 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1408 const char *devargs
, char **errp
)
1411 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1413 if (strncmp(devargs
, "class=eth,mac=", 14) == 0) {
1414 new_port_id
= netdev_dpdk_get_port_by_mac(&devargs
[14]);
1416 name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1417 if (rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1418 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1419 /* Device not found in DPDK, attempt to attach it */
1420 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1421 /* Attach successful */
1422 dev
->attached
= true;
1423 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1425 /* Attach unsuccessful */
1426 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1432 if (new_port_id
== DPDK_ETH_PORT_ID_INVALID
) {
1433 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK", devargs
);
1440 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1441 OVS_REQUIRES(dev
->mutex
)
1445 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1446 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1447 dev
->requested_n_rxq
= new_n_rxq
;
1448 netdev_request_reconfigure(&dev
->up
);
1453 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1454 const char *flag
, int default_size
, int *new_size
)
1456 int queue_size
= smap_get_int(args
, flag
, default_size
);
1458 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1459 || !is_pow2(queue_size
)) {
1460 queue_size
= default_size
;
1463 if (queue_size
!= *new_size
) {
1464 *new_size
= queue_size
;
1465 netdev_request_reconfigure(netdev
);
1470 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1473 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1474 bool rx_fc_en
, tx_fc_en
, autoneg
;
1475 enum rte_eth_fc_mode fc_mode
;
1476 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1477 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1478 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1480 const char *new_devargs
;
1483 ovs_mutex_lock(&dpdk_mutex
);
1484 ovs_mutex_lock(&dev
->mutex
);
1486 dpdk_set_rxq_config(dev
, args
);
1488 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1489 NIC_PORT_DEFAULT_RXQ_SIZE
,
1490 &dev
->requested_rxq_size
);
1491 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1492 NIC_PORT_DEFAULT_TXQ_SIZE
,
1493 &dev
->requested_txq_size
);
1495 new_devargs
= smap_get(args
, "dpdk-devargs");
1497 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1498 /* The user requested a new device. If we return error, the caller
1499 * will delete this netdev and try to recreate it. */
1504 /* dpdk-devargs is required for device configuration */
1505 if (new_devargs
&& new_devargs
[0]) {
1506 /* Don't process dpdk-devargs if value is unchanged and port id
1508 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1509 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1510 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1513 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1515 } else if (new_port_id
== dev
->port_id
) {
1516 /* Already configured, do not reconfigure again */
1519 struct netdev_dpdk
*dup_dev
;
1521 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1523 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1524 "which is already in use by '%s'",
1525 netdev_get_name(netdev
), new_devargs
,
1526 netdev_get_name(&dup_dev
->up
));
1529 int sid
= rte_eth_dev_socket_id(new_port_id
);
1531 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1532 dev
->devargs
= xstrdup(new_devargs
);
1533 dev
->port_id
= new_port_id
;
1534 netdev_request_reconfigure(&dev
->up
);
1535 netdev_dpdk_clear_xstats(dev
);
1541 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1542 "The old 'dpdk<port_id>' names are not supported",
1543 netdev_get_name(netdev
));
1551 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1552 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1553 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1555 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1556 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1557 dev
->fc_conf
.mode
= fc_mode
;
1558 dev
->fc_conf
.autoneg
= autoneg
;
1559 dpdk_eth_flow_ctrl_setup(dev
);
1563 ovs_mutex_unlock(&dev
->mutex
);
1564 ovs_mutex_unlock(&dpdk_mutex
);
1570 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1571 char **errp OVS_UNUSED
)
1573 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1575 ovs_mutex_lock(&dev
->mutex
);
1576 dpdk_set_rxq_config(dev
, args
);
1577 ovs_mutex_unlock(&dev
->mutex
);
1583 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1584 const struct smap
*args
,
1585 char **errp OVS_UNUSED
)
1587 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1590 ovs_mutex_lock(&dev
->mutex
);
1591 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1592 path
= smap_get(args
, "vhost-server-path");
1593 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1594 strcpy(dev
->vhost_id
, path
);
1595 /* check zero copy configuration */
1596 if (smap_get_bool(args
, "dq-zero-copy", false)) {
1597 dev
->vhost_driver_flags
|= RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
1599 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
1601 netdev_request_reconfigure(netdev
);
1604 ovs_mutex_unlock(&dev
->mutex
);
1610 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1612 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1614 return dev
->socket_id
;
1617 /* Sets the number of tx queues for the dpdk interface. */
1619 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1621 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1623 ovs_mutex_lock(&dev
->mutex
);
1625 if (dev
->requested_n_txq
== n_txq
) {
1629 dev
->requested_n_txq
= n_txq
;
1630 netdev_request_reconfigure(netdev
);
1633 ovs_mutex_unlock(&dev
->mutex
);
1637 static struct netdev_rxq
*
1638 netdev_dpdk_rxq_alloc(void)
1640 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1649 static struct netdev_rxq_dpdk
*
1650 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1652 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1656 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1658 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1659 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1661 ovs_mutex_lock(&dev
->mutex
);
1662 rx
->port_id
= dev
->port_id
;
1663 ovs_mutex_unlock(&dev
->mutex
);
1669 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1674 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1676 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1681 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1682 * 'pkts', even in case of failure.
1684 * Returns the number of packets that weren't transmitted. */
1686 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1687 struct rte_mbuf
**pkts
, int cnt
)
1691 while (nb_tx
!= cnt
) {
1694 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1702 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1703 /* Free buffers, which we couldn't transmit, one at a time (each
1704 * packet could come from a different mempool) */
1707 for (i
= nb_tx
; i
< cnt
; i
++) {
1708 rte_pktmbuf_free(pkts
[i
]);
1716 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1717 struct rte_mbuf
*pkt
, uint64_t time
)
1719 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1721 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1726 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1727 struct rte_mbuf
**pkts
, int pkt_cnt
,
1732 struct rte_mbuf
*pkt
= NULL
;
1733 uint64_t current_time
= rte_rdtsc();
1735 for (i
= 0; i
< pkt_cnt
; i
++) {
1737 /* Handle current packet */
1738 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1745 rte_pktmbuf_free(pkt
);
1754 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1755 int pkt_cnt
, bool may_steal
)
1759 rte_spinlock_lock(&policer
->policer_lock
);
1760 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1761 pkt_cnt
, may_steal
);
1762 rte_spinlock_unlock(&policer
->policer_lock
);
1768 is_vhost_running(struct netdev_dpdk
*dev
)
1770 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1774 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1775 unsigned int packet_size
)
1777 /* Hard-coded search for the size bucket. */
1778 if (packet_size
< 256) {
1779 if (packet_size
>= 128) {
1780 stats
->rx_128_to_255_packets
++;
1781 } else if (packet_size
<= 64) {
1782 stats
->rx_1_to_64_packets
++;
1784 stats
->rx_65_to_127_packets
++;
1787 if (packet_size
>= 1523) {
1788 stats
->rx_1523_to_max_packets
++;
1789 } else if (packet_size
>= 1024) {
1790 stats
->rx_1024_to_1522_packets
++;
1791 } else if (packet_size
< 512) {
1792 stats
->rx_256_to_511_packets
++;
1794 stats
->rx_512_to_1023_packets
++;
1800 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1801 struct dp_packet
**packets
, int count
,
1805 unsigned int packet_size
;
1806 struct dp_packet
*packet
;
1808 stats
->rx_packets
+= count
;
1809 stats
->rx_dropped
+= dropped
;
1810 for (i
= 0; i
< count
; i
++) {
1811 packet
= packets
[i
];
1812 packet_size
= dp_packet_size(packet
);
1814 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1815 /* This only protects the following multicast counting from
1816 * too short packets, but it does not stop the packet from
1817 * further processing. */
1819 stats
->rx_length_errors
++;
1823 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1825 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1826 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1830 stats
->rx_bytes
+= packet_size
;
1835 * The receive path for the vhost port is the TX path out from guest.
1838 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1839 struct dp_packet_batch
*batch
)
1841 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1842 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1844 uint16_t dropped
= 0;
1845 int qid
= rxq
->queue_id
;
1846 int vid
= netdev_dpdk_get_vid(dev
);
1848 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
1849 || !(dev
->flags
& NETDEV_UP
))) {
1853 nb_rx
= rte_vhost_dequeue_burst(vid
, qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1855 (struct rte_mbuf
**) batch
->packets
,
1863 nb_rx
= ingress_policer_run(policer
,
1864 (struct rte_mbuf
**) batch
->packets
,
1869 rte_spinlock_lock(&dev
->stats_lock
);
1870 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1872 rte_spinlock_unlock(&dev
->stats_lock
);
1874 batch
->count
= nb_rx
;
1875 dp_packet_batch_init_packet_fields(batch
);
1881 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1883 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1884 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1885 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1889 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1893 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1894 (struct rte_mbuf
**) batch
->packets
,
1902 nb_rx
= ingress_policer_run(policer
,
1903 (struct rte_mbuf
**) batch
->packets
,
1908 /* Update stats to reflect dropped packets */
1909 if (OVS_UNLIKELY(dropped
)) {
1910 rte_spinlock_lock(&dev
->stats_lock
);
1911 dev
->stats
.rx_dropped
+= dropped
;
1912 rte_spinlock_unlock(&dev
->stats_lock
);
1915 batch
->count
= nb_rx
;
1916 dp_packet_batch_init_packet_fields(batch
);
1922 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1923 int cnt
, bool may_steal
)
1925 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1928 rte_spinlock_lock(&qos_conf
->lock
);
1929 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1930 rte_spinlock_unlock(&qos_conf
->lock
);
1937 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1942 struct rte_mbuf
*pkt
;
1944 for (i
= 0; i
< pkt_cnt
; i
++) {
1946 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1947 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1948 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1949 rte_pktmbuf_free(pkt
);
1953 if (OVS_UNLIKELY(i
!= cnt
)) {
1963 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1964 struct dp_packet
**packets
,
1969 int sent
= attempted
- dropped
;
1971 stats
->tx_packets
+= sent
;
1972 stats
->tx_dropped
+= dropped
;
1974 for (i
= 0; i
< sent
; i
++) {
1975 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1980 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1981 struct dp_packet
**pkts
, int cnt
)
1983 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1984 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1985 unsigned int total_pkts
= cnt
;
1986 unsigned int dropped
= 0;
1988 int vid
= netdev_dpdk_get_vid(dev
);
1990 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1992 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
|| qid
< 0
1993 || !(dev
->flags
& NETDEV_UP
))) {
1994 rte_spinlock_lock(&dev
->stats_lock
);
1995 dev
->stats
.tx_dropped
+= cnt
;
1996 rte_spinlock_unlock(&dev
->stats_lock
);
2000 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
2002 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
2003 /* Check has QoS has been configured for the netdev */
2004 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
2005 dropped
= total_pkts
- cnt
;
2008 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
2009 unsigned int tx_pkts
;
2011 tx_pkts
= rte_vhost_enqueue_burst(vid
, vhost_qid
, cur_pkts
, cnt
);
2012 if (OVS_LIKELY(tx_pkts
)) {
2013 /* Packets have been sent.*/
2015 /* Prepare for possible retry.*/
2016 cur_pkts
= &cur_pkts
[tx_pkts
];
2018 /* No packets sent - do not retry.*/
2021 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
2023 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2025 rte_spinlock_lock(&dev
->stats_lock
);
2026 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
2028 rte_spinlock_unlock(&dev
->stats_lock
);
2031 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
2032 dp_packet_delete(pkts
[i
]);
2036 /* Tx function. Transmit packets indefinitely */
2038 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
2039 OVS_NO_THREAD_SAFETY_ANALYSIS
2041 const size_t batch_cnt
= dp_packet_batch_size(batch
);
2042 #if !defined(__CHECKER__) && !defined(_WIN32)
2043 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
2045 /* Sparse or MSVC doesn't like variable length array. */
2046 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
2048 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2049 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
2050 uint32_t cnt
= batch_cnt
;
2051 uint32_t dropped
= 0;
2053 if (dev
->type
!= DPDK_DEV_VHOST
) {
2054 /* Check if QoS has been configured for this netdev. */
2055 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
2057 dropped
+= batch_cnt
- cnt
;
2062 for (uint32_t i
= 0; i
< cnt
; i
++) {
2063 struct dp_packet
*packet
= batch
->packets
[i
];
2064 uint32_t size
= dp_packet_size(packet
);
2066 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
2067 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
2068 size
, dev
->max_packet_len
);
2074 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
2075 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
2080 /* We have to do a copy for now */
2081 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
2082 dp_packet_data(packet
), size
);
2083 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
2088 if (OVS_LIKELY(txcnt
)) {
2089 if (dev
->type
== DPDK_DEV_VHOST
) {
2090 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
2093 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
2097 if (OVS_UNLIKELY(dropped
)) {
2098 rte_spinlock_lock(&dev
->stats_lock
);
2099 dev
->stats
.tx_dropped
+= dropped
;
2100 rte_spinlock_unlock(&dev
->stats_lock
);
2105 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
2106 struct dp_packet_batch
*batch
,
2107 bool concurrent_txq OVS_UNUSED
)
2110 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2111 dpdk_do_tx_copy(netdev
, qid
, batch
);
2112 dp_packet_delete_batch(batch
, true);
2114 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
2120 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
2121 struct dp_packet_batch
*batch
,
2122 bool concurrent_txq
)
2124 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
2125 dp_packet_delete_batch(batch
, true);
2129 if (OVS_UNLIKELY(concurrent_txq
)) {
2130 qid
= qid
% dev
->up
.n_txq
;
2131 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
2134 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2135 struct netdev
*netdev
= &dev
->up
;
2137 dpdk_do_tx_copy(netdev
, qid
, batch
);
2138 dp_packet_delete_batch(batch
, true);
2140 int tx_cnt
, dropped
;
2141 int batch_cnt
= dp_packet_batch_size(batch
);
2142 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
2144 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
2145 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
2146 dropped
= batch_cnt
- tx_cnt
;
2148 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
2150 if (OVS_UNLIKELY(dropped
)) {
2151 rte_spinlock_lock(&dev
->stats_lock
);
2152 dev
->stats
.tx_dropped
+= dropped
;
2153 rte_spinlock_unlock(&dev
->stats_lock
);
2157 if (OVS_UNLIKELY(concurrent_txq
)) {
2158 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2163 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
2164 struct dp_packet_batch
*batch
, bool concurrent_txq
)
2166 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2168 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
2173 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
2175 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2177 ovs_mutex_lock(&dev
->mutex
);
2178 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
2180 netdev_change_seq_changed(netdev
);
2182 ovs_mutex_unlock(&dev
->mutex
);
2188 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
2190 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2192 ovs_mutex_lock(&dev
->mutex
);
2194 ovs_mutex_unlock(&dev
->mutex
);
2200 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
2202 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2204 ovs_mutex_lock(&dev
->mutex
);
2206 ovs_mutex_unlock(&dev
->mutex
);
2212 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2214 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2216 /* XXX: Ensure that the overall frame length of the requested MTU does not
2217 * surpass the NETDEV_DPDK_MAX_PKT_LEN. DPDK device drivers differ in how
2218 * the L2 frame length is calculated for a given MTU when
2219 * rte_eth_dev_set_mtu(mtu) is called e.g. i40e driver includes 2 x vlan
2220 * headers, the em driver includes 1 x vlan header, the ixgbe driver does
2221 * not include vlan headers. As such we should use
2222 * MTU_TO_MAX_FRAME_LEN(mtu) which includes an additional 2 x vlan headers
2223 * (8 bytes) for comparison. This avoids a failure later with
2224 * rte_eth_dev_set_mtu(). This approach should be used until DPDK provides
2225 * a method to retrieve the upper bound MTU for a given device.
2227 if (MTU_TO_MAX_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2228 || mtu
< ETHER_MIN_MTU
) {
2229 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2233 ovs_mutex_lock(&dev
->mutex
);
2234 if (dev
->requested_mtu
!= mtu
) {
2235 dev
->requested_mtu
= mtu
;
2236 netdev_request_reconfigure(netdev
);
2238 ovs_mutex_unlock(&dev
->mutex
);
2244 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2247 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2248 struct netdev_stats
*stats
)
2250 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2252 ovs_mutex_lock(&dev
->mutex
);
2254 rte_spinlock_lock(&dev
->stats_lock
);
2255 /* Supported Stats */
2256 stats
->rx_packets
= dev
->stats
.rx_packets
;
2257 stats
->tx_packets
= dev
->stats
.tx_packets
;
2258 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2259 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2260 stats
->multicast
= dev
->stats
.multicast
;
2261 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2262 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2263 stats
->rx_errors
= dev
->stats
.rx_errors
;
2264 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2266 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2267 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2268 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2269 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2270 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2271 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2272 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2274 rte_spinlock_unlock(&dev
->stats_lock
);
2276 ovs_mutex_unlock(&dev
->mutex
);
2282 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2283 const struct rte_eth_xstat
*xstats
,
2284 const struct rte_eth_xstat_name
*names
,
2285 const unsigned int size
)
2287 for (unsigned int i
= 0; i
< size
; i
++) {
2288 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2289 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2290 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2291 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2292 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2293 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2294 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2295 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2296 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2297 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2298 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2299 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2300 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2301 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2302 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2303 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2304 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2305 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2306 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2307 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2308 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2309 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2310 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2311 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2312 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2313 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2314 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2315 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2316 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2317 stats
->multicast
= xstats
[i
].value
;
2318 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2319 stats
->tx_multicast_packets
= xstats
[i
].value
;
2320 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2321 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2322 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2323 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2324 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2325 stats
->rx_undersized_errors
= xstats
[i
].value
;
2326 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2327 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2328 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2329 stats
->rx_jabber_errors
= xstats
[i
].value
;
2335 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2337 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2338 struct rte_eth_stats rte_stats
;
2341 netdev_dpdk_get_carrier(netdev
, &gg
);
2342 ovs_mutex_lock(&dev
->mutex
);
2344 struct rte_eth_xstat
*rte_xstats
= NULL
;
2345 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2346 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2348 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2349 VLOG_ERR("Can't get ETH statistics for port: "DPDK_PORT_ID_FMT
,
2351 ovs_mutex_unlock(&dev
->mutex
);
2355 /* Get length of statistics */
2356 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2357 if (rte_xstats_len
< 0) {
2358 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2362 /* Reserve memory for xstats names and values */
2363 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2364 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2366 /* Retreive xstats names */
2367 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2370 if (rte_xstats_new_len
!= rte_xstats_len
) {
2371 VLOG_WARN("Cannot get XSTATS names for port: "DPDK_PORT_ID_FMT
,
2375 /* Retreive xstats values */
2376 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2377 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2379 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2380 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2383 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2389 free(rte_xstats_names
);
2391 stats
->rx_packets
= rte_stats
.ipackets
;
2392 stats
->tx_packets
= rte_stats
.opackets
;
2393 stats
->rx_bytes
= rte_stats
.ibytes
;
2394 stats
->tx_bytes
= rte_stats
.obytes
;
2395 stats
->rx_errors
= rte_stats
.ierrors
;
2396 stats
->tx_errors
= rte_stats
.oerrors
;
2398 rte_spinlock_lock(&dev
->stats_lock
);
2399 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2400 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2401 rte_spinlock_unlock(&dev
->stats_lock
);
2403 /* These are the available DPDK counters for packets not received due to
2404 * local resource constraints in DPDK and NIC respectively. */
2405 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2406 stats
->rx_missed_errors
= rte_stats
.imissed
;
2408 ovs_mutex_unlock(&dev
->mutex
);
2414 netdev_dpdk_get_custom_stats(const struct netdev
*netdev
,
2415 struct netdev_custom_stats
*custom_stats
)
2419 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2422 ovs_mutex_lock(&dev
->mutex
);
2424 if (netdev_dpdk_configure_xstats(dev
)) {
2425 uint64_t *values
= xcalloc(dev
->rte_xstats_ids_size
,
2429 rte_eth_xstats_get_by_id(dev
->port_id
, dev
->rte_xstats_ids
,
2430 values
, dev
->rte_xstats_ids_size
);
2432 if (rte_xstats_ret
> 0 &&
2433 rte_xstats_ret
<= dev
->rte_xstats_ids_size
) {
2435 custom_stats
->size
= rte_xstats_ret
;
2436 custom_stats
->counters
=
2437 (struct netdev_custom_counter
*) xcalloc(rte_xstats_ret
,
2438 sizeof(struct netdev_custom_counter
));
2440 for (i
= 0; i
< rte_xstats_ret
; i
++) {
2441 ovs_strlcpy(custom_stats
->counters
[i
].name
,
2442 netdev_dpdk_get_xstat_name(dev
,
2443 dev
->rte_xstats_ids
[i
]),
2444 NETDEV_CUSTOM_STATS_NAME_SIZE
);
2445 custom_stats
->counters
[i
].value
= values
[i
];
2448 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2450 custom_stats
->counters
= NULL
;
2451 custom_stats
->size
= 0;
2452 /* Let's clear statistics cache, so it will be
2454 netdev_dpdk_clear_xstats(dev
);
2460 ovs_mutex_unlock(&dev
->mutex
);
2466 netdev_dpdk_get_features(const struct netdev
*netdev
,
2467 enum netdev_features
*current
,
2468 enum netdev_features
*advertised
,
2469 enum netdev_features
*supported
,
2470 enum netdev_features
*peer
)
2472 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2473 struct rte_eth_link link
;
2475 ovs_mutex_lock(&dev
->mutex
);
2477 ovs_mutex_unlock(&dev
->mutex
);
2479 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2480 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2481 *current
= NETDEV_F_10MB_HD
;
2483 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2484 *current
= NETDEV_F_100MB_HD
;
2486 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2487 *current
= NETDEV_F_1GB_HD
;
2489 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2490 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2491 *current
= NETDEV_F_10MB_FD
;
2493 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2494 *current
= NETDEV_F_100MB_FD
;
2496 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2497 *current
= NETDEV_F_1GB_FD
;
2499 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2500 *current
= NETDEV_F_10GB_FD
;
2504 if (link
.link_autoneg
) {
2505 *current
|= NETDEV_F_AUTONEG
;
2508 *advertised
= *supported
= *peer
= 0;
2513 static struct ingress_policer
*
2514 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2516 struct ingress_policer
*policer
= NULL
;
2517 uint64_t rate_bytes
;
2518 uint64_t burst_bytes
;
2521 policer
= xmalloc(sizeof *policer
);
2522 rte_spinlock_init(&policer
->policer_lock
);
2524 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2525 rate_bytes
= rate
* 1000ULL / 8;
2526 burst_bytes
= burst
* 1000ULL / 8;
2528 policer
->app_srtcm_params
.cir
= rate_bytes
;
2529 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2530 policer
->app_srtcm_params
.ebs
= 0;
2531 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2532 &policer
->app_srtcm_params
);
2534 VLOG_ERR("Could not create rte meter for ingress policer");
2543 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2544 uint32_t policer_burst
)
2546 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2547 struct ingress_policer
*policer
;
2549 /* Force to 0 if no rate specified,
2550 * default to 8000 kbits if burst is 0,
2551 * else stick with user-specified value.
2553 policer_burst
= (!policer_rate
? 0
2554 : !policer_burst
? 8000
2557 ovs_mutex_lock(&dev
->mutex
);
2559 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2560 &dev
->ingress_policer
);
2562 if (dev
->policer_rate
== policer_rate
&&
2563 dev
->policer_burst
== policer_burst
) {
2564 /* Assume that settings haven't changed since we last set them. */
2565 ovs_mutex_unlock(&dev
->mutex
);
2569 /* Destroy any existing ingress policer for the device if one exists */
2571 ovsrcu_postpone(free
, policer
);
2574 if (policer_rate
!= 0) {
2575 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2579 ovsrcu_set(&dev
->ingress_policer
, policer
);
2580 dev
->policer_rate
= policer_rate
;
2581 dev
->policer_burst
= policer_burst
;
2582 ovs_mutex_unlock(&dev
->mutex
);
2588 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2590 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2592 ovs_mutex_lock(&dev
->mutex
);
2593 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2594 * postive integer to meet RFC 2863 recommendations.
2596 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2597 ovs_mutex_unlock(&dev
->mutex
);
2603 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2605 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2607 ovs_mutex_lock(&dev
->mutex
);
2608 check_link_status(dev
);
2609 *carrier
= dev
->link
.link_status
;
2611 ovs_mutex_unlock(&dev
->mutex
);
2617 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2619 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2621 ovs_mutex_lock(&dev
->mutex
);
2623 if (is_vhost_running(dev
)) {
2629 ovs_mutex_unlock(&dev
->mutex
);
2634 static long long int
2635 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2637 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2638 long long int carrier_resets
;
2640 ovs_mutex_lock(&dev
->mutex
);
2641 carrier_resets
= dev
->link_reset_cnt
;
2642 ovs_mutex_unlock(&dev
->mutex
);
2644 return carrier_resets
;
2648 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2649 long long int interval OVS_UNUSED
)
2655 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2656 enum netdev_flags off
, enum netdev_flags on
,
2657 enum netdev_flags
*old_flagsp
)
2658 OVS_REQUIRES(dev
->mutex
)
2660 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2664 *old_flagsp
= dev
->flags
;
2668 if (dev
->flags
== *old_flagsp
) {
2672 if (dev
->type
== DPDK_DEV_ETH
) {
2673 if (dev
->flags
& NETDEV_PROMISC
) {
2674 rte_eth_promiscuous_enable(dev
->port_id
);
2677 netdev_change_seq_changed(&dev
->up
);
2679 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2680 * running then change netdev's change_seq to trigger link state
2683 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2684 && is_vhost_running(dev
)) {
2685 netdev_change_seq_changed(&dev
->up
);
2687 /* Clear statistics if device is getting up. */
2688 if (NETDEV_UP
& on
) {
2689 rte_spinlock_lock(&dev
->stats_lock
);
2690 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2691 rte_spinlock_unlock(&dev
->stats_lock
);
2700 netdev_dpdk_update_flags(struct netdev
*netdev
,
2701 enum netdev_flags off
, enum netdev_flags on
,
2702 enum netdev_flags
*old_flagsp
)
2704 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2707 ovs_mutex_lock(&dev
->mutex
);
2708 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2709 ovs_mutex_unlock(&dev
->mutex
);
2715 netdev_dpdk_vhost_user_get_status(const struct netdev
*netdev
,
2718 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2720 ovs_mutex_lock(&dev
->mutex
);
2722 bool client_mode
= dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
;
2723 smap_add_format(args
, "mode", "%s", client_mode
? "client" : "server");
2725 int vid
= netdev_dpdk_get_vid(dev
);
2727 smap_add_format(args
, "status", "disconnected");
2728 ovs_mutex_unlock(&dev
->mutex
);
2731 smap_add_format(args
, "status", "connected");
2734 char socket_name
[PATH_MAX
];
2735 if (!rte_vhost_get_ifname(vid
, socket_name
, PATH_MAX
)) {
2736 smap_add_format(args
, "socket", "%s", socket_name
);
2740 if (!rte_vhost_get_negotiated_features(vid
, &features
)) {
2741 smap_add_format(args
, "features", "0x%016"PRIx64
, features
);
2745 if (!rte_vhost_get_mtu(vid
, &mtu
)) {
2746 smap_add_format(args
, "mtu", "%d", mtu
);
2749 int numa
= rte_vhost_get_numa_node(vid
);
2751 smap_add_format(args
, "numa", "%d", numa
);
2754 uint16_t vring_num
= rte_vhost_get_vring_num(vid
);
2756 smap_add_format(args
, "num_of_vrings", "%d", vring_num
);
2759 for (int i
= 0; i
< vring_num
; i
++) {
2760 struct rte_vhost_vring vring
;
2761 char vhost_vring
[16];
2763 rte_vhost_get_vhost_vring(vid
, i
, &vring
);
2764 snprintf(vhost_vring
, 16, "vring_%d_size", i
);
2765 smap_add_format(args
, vhost_vring
, "%d", vring
.size
);
2768 ovs_mutex_unlock(&dev
->mutex
);
2773 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2775 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2776 struct rte_eth_dev_info dev_info
;
2778 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2782 ovs_mutex_lock(&dev
->mutex
);
2783 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2784 ovs_mutex_unlock(&dev
->mutex
);
2786 smap_add_format(args
, "port_no", DPDK_PORT_ID_FMT
, dev
->port_id
);
2787 smap_add_format(args
, "numa_id", "%d",
2788 rte_eth_dev_socket_id(dev
->port_id
));
2789 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2790 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2791 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2792 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2793 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2794 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2795 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2796 dev_info
.max_hash_mac_addrs
);
2797 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2798 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2800 /* Querying the DPDK library for iftype may be done in future, pending
2801 * support; cf. RFC 3635 Section 3.2.4. */
2802 enum { IF_TYPE_ETHERNETCSMACD
= 6 };
2804 smap_add_format(args
, "if_type", "%"PRIu32
, IF_TYPE_ETHERNETCSMACD
);
2805 smap_add_format(args
, "if_descr", "%s %s", rte_version(),
2806 dev_info
.driver_name
);
2808 if (dev_info
.pci_dev
) {
2809 smap_add_format(args
, "pci-vendor_id", "0x%u",
2810 dev_info
.pci_dev
->id
.vendor_id
);
2811 smap_add_format(args
, "pci-device_id", "0x%x",
2812 dev_info
.pci_dev
->id
.device_id
);
2819 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2820 OVS_REQUIRES(dev
->mutex
)
2822 enum netdev_flags old_flags
;
2825 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2827 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2832 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2833 const char *argv
[], void *aux OVS_UNUSED
)
2837 if (!strcasecmp(argv
[argc
- 1], "up")) {
2839 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2842 unixctl_command_reply_error(conn
, "Invalid Admin State");
2847 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2849 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2850 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2852 ovs_mutex_lock(&dev
->mutex
);
2853 netdev_dpdk_set_admin_state__(dev
, up
);
2854 ovs_mutex_unlock(&dev
->mutex
);
2856 netdev_close(netdev
);
2858 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2859 netdev_close(netdev
);
2863 struct netdev_dpdk
*dev
;
2865 ovs_mutex_lock(&dpdk_mutex
);
2866 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2867 ovs_mutex_lock(&dev
->mutex
);
2868 netdev_dpdk_set_admin_state__(dev
, up
);
2869 ovs_mutex_unlock(&dev
->mutex
);
2871 ovs_mutex_unlock(&dpdk_mutex
);
2873 unixctl_command_reply(conn
, "OK");
2877 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2878 const char *argv
[], void *aux OVS_UNUSED
)
2882 dpdk_port_t port_id
;
2883 char devname
[RTE_ETH_NAME_MAX_LEN
];
2884 struct netdev_dpdk
*dev
;
2886 ovs_mutex_lock(&dpdk_mutex
);
2888 if (rte_eth_dev_get_port_by_name(argv
[1], &port_id
)) {
2889 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2893 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2895 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2896 "Remove it before detaching",
2897 argv
[1], netdev_get_name(&dev
->up
));
2901 rte_eth_dev_close(port_id
);
2903 ret
= rte_eth_dev_detach(port_id
, devname
);
2905 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2909 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2911 ovs_mutex_unlock(&dpdk_mutex
);
2912 unixctl_command_reply(conn
, response
);
2917 ovs_mutex_unlock(&dpdk_mutex
);
2918 unixctl_command_reply_error(conn
, response
);
2923 netdev_dpdk_get_mempool_info(struct unixctl_conn
*conn
,
2924 int argc
, const char *argv
[],
2925 void *aux OVS_UNUSED
)
2929 char *response
= NULL
;
2930 struct netdev
*netdev
= NULL
;
2933 netdev
= netdev_from_name(argv
[1]);
2934 if (!netdev
|| !is_dpdk_class(netdev
->netdev_class
)) {
2935 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2940 stream
= open_memstream(&response
, &size
);
2942 response
= xasprintf("Unable to open memstream: %s.",
2943 ovs_strerror(errno
));
2944 unixctl_command_reply_error(conn
, response
);
2949 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2951 ovs_mutex_lock(&dev
->mutex
);
2952 ovs_mutex_lock(&dpdk_mp_mutex
);
2954 rte_mempool_dump(stream
, dev
->dpdk_mp
->mp
);
2956 ovs_mutex_unlock(&dpdk_mp_mutex
);
2957 ovs_mutex_unlock(&dev
->mutex
);
2959 ovs_mutex_lock(&dpdk_mp_mutex
);
2960 rte_mempool_list_dump(stream
);
2961 ovs_mutex_unlock(&dpdk_mp_mutex
);
2966 unixctl_command_reply(conn
, response
);
2969 netdev_close(netdev
);
2973 * Set virtqueue flags so that we do not receive interrupts.
2976 set_irq_status(int vid
)
2980 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2981 rte_vhost_enable_guest_notification(vid
, i
, 0);
2986 * Fixes mapping for vhost-user tx queues. Must be called after each
2987 * enabling/disabling of queues and n_txq modifications.
2990 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2991 OVS_REQUIRES(dev
->mutex
)
2993 int *enabled_queues
, n_enabled
= 0;
2994 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2996 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2998 for (i
= 0; i
< total_txqs
; i
++) {
2999 /* Enabled queues always mapped to themselves. */
3000 if (dev
->tx_q
[i
].map
== i
) {
3001 enabled_queues
[n_enabled
++] = i
;
3005 if (n_enabled
== 0 && total_txqs
!= 0) {
3006 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
3011 for (i
= 0; i
< total_txqs
; i
++) {
3012 if (dev
->tx_q
[i
].map
!= i
) {
3013 dev
->tx_q
[i
].map
= enabled_queues
[k
];
3014 k
= (k
+ 1) % n_enabled
;
3018 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
3019 for (i
= 0; i
< total_txqs
; i
++) {
3020 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
3023 free(enabled_queues
);
3027 * A new virtio-net device is added to a vhost port.
3032 struct netdev_dpdk
*dev
;
3033 bool exists
= false;
3035 char ifname
[IF_NAME_SZ
];
3037 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3039 ovs_mutex_lock(&dpdk_mutex
);
3040 /* Add device to the vhost port with the same name as that passed down. */
3041 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
3042 ovs_mutex_lock(&dev
->mutex
);
3043 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3044 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
3046 /* Get NUMA information */
3047 newnode
= rte_vhost_get_numa_node(vid
);
3048 if (newnode
== -1) {
3050 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
3053 newnode
= dev
->socket_id
;
3056 if (dev
->requested_n_txq
!= qp_num
3057 || dev
->requested_n_rxq
!= qp_num
3058 || dev
->requested_socket_id
!= newnode
) {
3059 dev
->requested_socket_id
= newnode
;
3060 dev
->requested_n_rxq
= qp_num
;
3061 dev
->requested_n_txq
= qp_num
;
3062 netdev_request_reconfigure(&dev
->up
);
3064 /* Reconfiguration not required. */
3065 dev
->vhost_reconfigured
= true;
3068 ovsrcu_index_set(&dev
->vid
, vid
);
3071 /* Disable notifications. */
3072 set_irq_status(vid
);
3073 netdev_change_seq_changed(&dev
->up
);
3074 ovs_mutex_unlock(&dev
->mutex
);
3077 ovs_mutex_unlock(&dev
->mutex
);
3079 ovs_mutex_unlock(&dpdk_mutex
);
3082 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
3087 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
3093 /* Clears mapping for all available queues of vhost interface. */
3095 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
3096 OVS_REQUIRES(dev
->mutex
)
3100 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
3101 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
3106 * Remove a virtio-net device from the specific vhost port. Use dev->remove
3107 * flag to stop any more packets from being sent or received to/from a VM and
3108 * ensure all currently queued packets have been sent/received before removing
3112 destroy_device(int vid
)
3114 struct netdev_dpdk
*dev
;
3115 bool exists
= false;
3116 char ifname
[IF_NAME_SZ
];
3118 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3120 ovs_mutex_lock(&dpdk_mutex
);
3121 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3122 if (netdev_dpdk_get_vid(dev
) == vid
) {
3124 ovs_mutex_lock(&dev
->mutex
);
3125 dev
->vhost_reconfigured
= false;
3126 ovsrcu_index_set(&dev
->vid
, -1);
3127 netdev_dpdk_txq_map_clear(dev
);
3129 netdev_change_seq_changed(&dev
->up
);
3130 ovs_mutex_unlock(&dev
->mutex
);
3136 ovs_mutex_unlock(&dpdk_mutex
);
3140 * Wait for other threads to quiesce after setting the 'virtio_dev'
3141 * to NULL, before returning.
3143 ovsrcu_synchronize();
3145 * As call to ovsrcu_synchronize() will end the quiescent state,
3146 * put thread back into quiescent state before returning.
3148 ovsrcu_quiesce_start();
3149 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
3151 VLOG_INFO("vHost Device '%s' not found", ifname
);
3156 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
3158 struct netdev_dpdk
*dev
;
3159 bool exists
= false;
3160 int qid
= queue_id
/ VIRTIO_QNUM
;
3161 char ifname
[IF_NAME_SZ
];
3163 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3165 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
3169 ovs_mutex_lock(&dpdk_mutex
);
3170 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3171 ovs_mutex_lock(&dev
->mutex
);
3172 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3174 dev
->tx_q
[qid
].map
= qid
;
3176 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
3178 netdev_dpdk_remap_txqs(dev
);
3180 ovs_mutex_unlock(&dev
->mutex
);
3183 ovs_mutex_unlock(&dev
->mutex
);
3185 ovs_mutex_unlock(&dpdk_mutex
);
3188 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
3189 "changed to \'%s\'", queue_id
, qid
, ifname
,
3190 (enable
== 1) ? "enabled" : "disabled");
3192 VLOG_INFO("vHost Device '%s' not found", ifname
);
3200 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
3202 return ovsrcu_index_get(&dev
->vid
);
3205 struct ingress_policer
*
3206 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
3208 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
3212 netdev_dpdk_class_init(void)
3214 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3216 /* This function can be called for different classes. The initialization
3217 * needs to be done only once */
3218 if (ovsthread_once_start(&once
)) {
3219 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
3220 unixctl_command_register("netdev-dpdk/set-admin-state",
3221 "[netdev] up|down", 1, 2,
3222 netdev_dpdk_set_admin_state
, NULL
);
3224 unixctl_command_register("netdev-dpdk/detach",
3225 "pci address of device", 1, 1,
3226 netdev_dpdk_detach
, NULL
);
3228 unixctl_command_register("netdev-dpdk/get-mempool-info",
3230 netdev_dpdk_get_mempool_info
, NULL
);
3232 ovsthread_once_done(&once
);
3242 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
3243 dpdk_port_t
*eth_port_id
)
3245 struct dpdk_ring
*ring_pair
;
3249 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
3254 /* XXX: Add support for multiquque ring. */
3255 ring_name
= xasprintf("%s_tx", dev_name
);
3257 /* Create single producer tx ring, netdev does explicit locking. */
3258 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3261 if (ring_pair
->cring_tx
== NULL
) {
3262 rte_free(ring_pair
);
3266 ring_name
= xasprintf("%s_rx", dev_name
);
3268 /* Create single consumer rx ring, netdev does explicit locking. */
3269 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3272 if (ring_pair
->cring_rx
== NULL
) {
3273 rte_free(ring_pair
);
3277 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
3278 &ring_pair
->cring_tx
, 1, SOCKET0
);
3281 rte_free(ring_pair
);
3285 ring_pair
->user_port_id
= port_no
;
3286 ring_pair
->eth_port_id
= port_id
;
3287 *eth_port_id
= port_id
;
3289 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
3295 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
3296 OVS_REQUIRES(dpdk_mutex
)
3298 struct dpdk_ring
*ring_pair
;
3299 unsigned int port_no
;
3302 /* Names always start with "dpdkr" */
3303 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
3308 /* Look through our list to find the device */
3309 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
3310 if (ring_pair
->user_port_id
== port_no
) {
3311 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
3312 /* Really all that is needed */
3313 *eth_port_id
= ring_pair
->eth_port_id
;
3317 /* Need to create the device rings */
3318 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
3322 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
3323 struct dp_packet_batch
*batch
, bool concurrent_txq
)
3325 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3326 struct dp_packet
*packet
;
3328 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
3329 * the rss hash field is clear. This is because the same mbuf may be
3330 * modified by the consumer of the ring and return into the datapath
3331 * without recalculating the RSS hash. */
3332 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
3333 dp_packet_mbuf_rss_flag_reset(packet
);
3336 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
3341 netdev_dpdk_ring_construct(struct netdev
*netdev
)
3343 dpdk_port_t port_no
= 0;
3346 ovs_mutex_lock(&dpdk_mutex
);
3348 err
= dpdk_ring_open(netdev
->name
, &port_no
);
3353 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
3354 rte_eth_dev_socket_id(port_no
));
3356 ovs_mutex_unlock(&dpdk_mutex
);
3363 * Initialize QoS configuration operations.
3366 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
3369 rte_spinlock_init(&conf
->lock
);
3373 * Search existing QoS operations in qos_ops and compare each set of
3374 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
3377 static const struct dpdk_qos_ops
*
3378 qos_lookup_name(const char *name
)
3380 const struct dpdk_qos_ops
*const *opsp
;
3382 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3383 const struct dpdk_qos_ops
*ops
= *opsp
;
3384 if (!strcmp(name
, ops
->qos_name
)) {
3392 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3395 const struct dpdk_qos_ops
*const *opsp
;
3397 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3398 const struct dpdk_qos_ops
*ops
= *opsp
;
3399 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3400 sset_add(types
, ops
->qos_name
);
3407 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3408 const char **typep
, struct smap
*details
)
3410 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3411 struct qos_conf
*qos_conf
;
3414 ovs_mutex_lock(&dev
->mutex
);
3415 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3417 *typep
= qos_conf
->ops
->qos_name
;
3418 error
= (qos_conf
->ops
->qos_get
3419 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3421 /* No QoS configuration set, return an empty string */
3424 ovs_mutex_unlock(&dev
->mutex
);
3430 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3431 const struct smap
*details
)
3433 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3434 const struct dpdk_qos_ops
*new_ops
= NULL
;
3435 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3438 ovs_mutex_lock(&dev
->mutex
);
3440 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3442 new_ops
= qos_lookup_name(type
);
3444 if (!new_ops
|| !new_ops
->qos_construct
) {
3445 new_qos_conf
= NULL
;
3446 if (type
&& type
[0]) {
3449 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3450 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3451 new_qos_conf
= qos_conf
;
3453 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3457 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3458 type
, netdev
->name
, rte_strerror(error
));
3461 if (new_qos_conf
!= qos_conf
) {
3462 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3464 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3468 ovs_mutex_unlock(&dev
->mutex
);
3473 /* egress-policer details */
3475 struct egress_policer
{
3476 struct qos_conf qos_conf
;
3477 struct rte_meter_srtcm_params app_srtcm_params
;
3478 struct rte_meter_srtcm egress_meter
;
3482 egress_policer_details_to_param(const struct smap
*details
,
3483 struct rte_meter_srtcm_params
*params
)
3485 memset(params
, 0, sizeof *params
);
3486 params
->cir
= smap_get_ullong(details
, "cir", 0);
3487 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3492 egress_policer_qos_construct(const struct smap
*details
,
3493 struct qos_conf
**conf
)
3495 struct egress_policer
*policer
;
3498 policer
= xmalloc(sizeof *policer
);
3499 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3500 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3501 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3502 &policer
->app_srtcm_params
);
3504 *conf
= &policer
->qos_conf
;
3515 egress_policer_qos_destruct(struct qos_conf
*conf
)
3517 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3523 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3525 struct egress_policer
*policer
=
3526 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3528 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3529 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3535 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3536 const struct smap
*details
)
3538 struct egress_policer
*policer
=
3539 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3540 struct rte_meter_srtcm_params params
;
3542 egress_policer_details_to_param(details
, ¶ms
);
3544 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3548 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3552 struct egress_policer
*policer
=
3553 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3555 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3556 pkt_cnt
, may_steal
);
3561 static const struct dpdk_qos_ops egress_policer_ops
= {
3562 "egress-policer", /* qos_name */
3563 egress_policer_qos_construct
,
3564 egress_policer_qos_destruct
,
3565 egress_policer_qos_get
,
3566 egress_policer_qos_is_equal
,
3571 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3573 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3576 ovs_mutex_lock(&dev
->mutex
);
3578 if (netdev
->n_txq
== dev
->requested_n_txq
3579 && netdev
->n_rxq
== dev
->requested_n_rxq
3580 && dev
->mtu
== dev
->requested_mtu
3581 && dev
->rxq_size
== dev
->requested_rxq_size
3582 && dev
->txq_size
== dev
->requested_txq_size
3583 && dev
->socket_id
== dev
->requested_socket_id
) {
3584 /* Reconfiguration is unnecessary */
3589 rte_eth_dev_stop(dev
->port_id
);
3591 if (dev
->mtu
!= dev
->requested_mtu
3592 || dev
->socket_id
!= dev
->requested_socket_id
) {
3593 err
= netdev_dpdk_mempool_configure(dev
);
3599 netdev
->n_txq
= dev
->requested_n_txq
;
3600 netdev
->n_rxq
= dev
->requested_n_rxq
;
3602 dev
->rxq_size
= dev
->requested_rxq_size
;
3603 dev
->txq_size
= dev
->requested_txq_size
;
3605 rte_free(dev
->tx_q
);
3606 err
= dpdk_eth_dev_init(dev
);
3607 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3612 netdev_change_seq_changed(netdev
);
3615 ovs_mutex_unlock(&dev
->mutex
);
3620 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3621 OVS_REQUIRES(dev
->mutex
)
3623 dev
->up
.n_txq
= dev
->requested_n_txq
;
3624 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3627 /* Enable TX queue 0 by default if it wasn't disabled. */
3628 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3629 dev
->tx_q
[0].map
= 0;
3632 netdev_dpdk_remap_txqs(dev
);
3634 if (dev
->requested_socket_id
!= dev
->socket_id
3635 || dev
->requested_mtu
!= dev
->mtu
) {
3636 err
= netdev_dpdk_mempool_configure(dev
);
3640 netdev_change_seq_changed(&dev
->up
);
3644 if (netdev_dpdk_get_vid(dev
) >= 0) {
3645 if (dev
->vhost_reconfigured
== false) {
3646 dev
->vhost_reconfigured
= true;
3647 /* Carrier status may need updating. */
3648 netdev_change_seq_changed(&dev
->up
);
3656 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3658 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3661 ovs_mutex_lock(&dev
->mutex
);
3662 err
= dpdk_vhost_reconfigure_helper(dev
);
3663 ovs_mutex_unlock(&dev
->mutex
);
3669 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3671 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3673 uint64_t vhost_flags
= 0;
3676 ovs_mutex_lock(&dev
->mutex
);
3678 /* Configure vHost client mode if requested and if the following criteria
3680 * 1. Device hasn't been registered yet.
3681 * 2. A path has been specified.
3683 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3684 && strlen(dev
->vhost_id
)) {
3685 /* Register client-mode device. */
3686 vhost_flags
|= RTE_VHOST_USER_CLIENT
;
3688 /* Enable IOMMU support, if explicitly requested. */
3689 if (dpdk_vhost_iommu_enabled()) {
3690 vhost_flags
|= RTE_VHOST_USER_IOMMU_SUPPORT
;
3693 zc_enabled
= dev
->vhost_driver_flags
3694 & RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
3695 /* Enable zero copy flag, if requested */
3697 vhost_flags
|= RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
3700 err
= rte_vhost_driver_register(dev
->vhost_id
, vhost_flags
);
3702 VLOG_ERR("vhost-user device setup failure for device %s\n",
3706 /* Configuration successful */
3707 dev
->vhost_driver_flags
|= vhost_flags
;
3708 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3709 "using client socket '%s'",
3710 dev
->up
.name
, dev
->vhost_id
);
3712 VLOG_INFO("Zero copy enabled for vHost port %s", dev
->up
.name
);
3716 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3717 &virtio_net_device_ops
);
3719 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3720 "vhost user client port: %s\n", dev
->up
.name
);
3724 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3725 1ULL << VIRTIO_NET_F_HOST_TSO4
3726 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3727 | 1ULL << VIRTIO_NET_F_CSUM
);
3729 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3730 "client port: %s\n", dev
->up
.name
);
3734 err
= rte_vhost_driver_start(dev
->vhost_id
);
3736 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3737 "client port: %s\n", dev
->up
.name
);
3742 err
= dpdk_vhost_reconfigure_helper(dev
);
3745 ovs_mutex_unlock(&dev
->mutex
);
3750 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3751 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3752 GET_CARRIER, GET_STATS, \
3754 GET_FEATURES, GET_STATUS, \
3755 RECONFIGURE, RXQ_RECV) \
3758 true, /* is_pmd */ \
3760 NULL, /* netdev_dpdk_run */ \
3761 NULL, /* netdev_dpdk_wait */ \
3763 netdev_dpdk_alloc, \
3766 netdev_dpdk_dealloc, \
3767 netdev_dpdk_get_config, \
3769 NULL, /* get_tunnel_config */ \
3770 NULL, /* build header */ \
3771 NULL, /* push header */ \
3772 NULL, /* pop header */ \
3773 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3777 NULL, /* send_wait */ \
3779 netdev_dpdk_set_etheraddr, \
3780 netdev_dpdk_get_etheraddr, \
3781 netdev_dpdk_get_mtu, \
3782 netdev_dpdk_set_mtu, \
3783 netdev_dpdk_get_ifindex, \
3785 netdev_dpdk_get_carrier_resets, \
3786 netdev_dpdk_set_miimon, \
3790 NULL, /* set_advertisements */ \
3791 NULL, /* get_pt_mode */ \
3793 netdev_dpdk_set_policing, \
3794 netdev_dpdk_get_qos_types, \
3795 NULL, /* get_qos_capabilities */ \
3796 netdev_dpdk_get_qos, \
3797 netdev_dpdk_set_qos, \
3798 NULL, /* get_queue */ \
3799 NULL, /* set_queue */ \
3800 NULL, /* delete_queue */ \
3801 NULL, /* get_queue_stats */ \
3802 NULL, /* queue_dump_start */ \
3803 NULL, /* queue_dump_next */ \
3804 NULL, /* queue_dump_done */ \
3805 NULL, /* dump_queue_stats */ \
3807 NULL, /* set_in4 */ \
3808 NULL, /* get_addr_list */ \
3809 NULL, /* add_router */ \
3810 NULL, /* get_next_hop */ \
3812 NULL, /* arp_lookup */ \
3814 netdev_dpdk_update_flags, \
3817 netdev_dpdk_rxq_alloc, \
3818 netdev_dpdk_rxq_construct, \
3819 netdev_dpdk_rxq_destruct, \
3820 netdev_dpdk_rxq_dealloc, \
3822 NULL, /* rx_wait */ \
3823 NULL, /* rxq_drain */ \
3827 static const struct netdev_class dpdk_class
=
3830 netdev_dpdk_class_init
,
3831 netdev_dpdk_construct
,
3832 netdev_dpdk_destruct
,
3833 netdev_dpdk_set_config
,
3834 netdev_dpdk_set_tx_multiq
,
3835 netdev_dpdk_eth_send
,
3836 netdev_dpdk_get_carrier
,
3837 netdev_dpdk_get_stats
,
3838 netdev_dpdk_get_custom_stats
,
3839 netdev_dpdk_get_features
,
3840 netdev_dpdk_get_status
,
3841 netdev_dpdk_reconfigure
,
3842 netdev_dpdk_rxq_recv
);
3844 static const struct netdev_class dpdk_ring_class
=
3847 netdev_dpdk_class_init
,
3848 netdev_dpdk_ring_construct
,
3849 netdev_dpdk_destruct
,
3850 netdev_dpdk_ring_set_config
,
3851 netdev_dpdk_set_tx_multiq
,
3852 netdev_dpdk_ring_send
,
3853 netdev_dpdk_get_carrier
,
3854 netdev_dpdk_get_stats
,
3855 netdev_dpdk_get_custom_stats
,
3856 netdev_dpdk_get_features
,
3857 netdev_dpdk_get_status
,
3858 netdev_dpdk_reconfigure
,
3859 netdev_dpdk_rxq_recv
);
3861 static const struct netdev_class dpdk_vhost_class
=
3865 netdev_dpdk_vhost_construct
,
3866 netdev_dpdk_vhost_destruct
,
3869 netdev_dpdk_vhost_send
,
3870 netdev_dpdk_vhost_get_carrier
,
3871 netdev_dpdk_vhost_get_stats
,
3874 netdev_dpdk_vhost_user_get_status
,
3875 netdev_dpdk_vhost_reconfigure
,
3876 netdev_dpdk_vhost_rxq_recv
);
3877 static const struct netdev_class dpdk_vhost_client_class
=
3879 "dpdkvhostuserclient",
3881 netdev_dpdk_vhost_client_construct
,
3882 netdev_dpdk_vhost_destruct
,
3883 netdev_dpdk_vhost_client_set_config
,
3885 netdev_dpdk_vhost_send
,
3886 netdev_dpdk_vhost_get_carrier
,
3887 netdev_dpdk_vhost_get_stats
,
3890 netdev_dpdk_vhost_user_get_status
,
3891 netdev_dpdk_vhost_client_reconfigure
,
3892 netdev_dpdk_vhost_rxq_recv
);
3895 netdev_dpdk_register(void)
3897 netdev_register_provider(&dpdk_class
);
3898 netdev_register_provider(&dpdk_ring_class
);
3899 netdev_register_provider(&dpdk_vhost_class
);
3900 netdev_register_provider(&dpdk_vhost_client_class
);