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
);
594 dpdk_mp_full(const struct rte_mempool
*mp
) OVS_REQUIRES(dpdk_mp_mutex
)
597 /* This logic is needed because rte_mempool_full() is not guaranteed to
598 * be atomic and mbufs could be moved from mempool cache --> mempool ring
599 * during the call. However, as no mbufs will be taken from the mempool
600 * at this time, we can work around it by also checking the ring entries
601 * separately and ensuring that they have not changed.
603 ring_count
= rte_mempool_ops_get_count(mp
);
604 if (rte_mempool_full(mp
) && rte_mempool_ops_get_count(mp
) == ring_count
) {
611 /* Free unused mempools. */
613 dpdk_mp_sweep(void) OVS_REQUIRES(dpdk_mp_mutex
)
615 struct dpdk_mp
*dmp
, *next
;
617 LIST_FOR_EACH_SAFE (dmp
, next
, list_node
, &dpdk_mp_list
) {
618 if (!dmp
->refcount
&& dpdk_mp_full(dmp
->mp
)) {
619 VLOG_DBG("Freeing mempool \"%s\"", dmp
->mp
->name
);
620 ovs_list_remove(&dmp
->list_node
);
621 rte_mempool_free(dmp
->mp
);
627 static struct dpdk_mp
*
628 dpdk_mp_get(int socket_id
, int mtu
)
633 ovs_mutex_lock(&dpdk_mp_mutex
);
634 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
635 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
636 VLOG_DBG("Reusing mempool \"%s\"", dmp
->mp
->name
);
642 /* Sweep mempools after reuse or before create. */
646 dmp
= dpdk_mp_create(socket_id
, mtu
);
648 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
652 ovs_mutex_unlock(&dpdk_mp_mutex
);
657 /* Decrement reference to a mempool. */
659 dpdk_mp_put(struct dpdk_mp
*dmp
)
665 ovs_mutex_lock(&dpdk_mp_mutex
);
666 ovs_assert(dmp
->refcount
);
668 ovs_mutex_unlock(&dpdk_mp_mutex
);
671 /* Tries to allocate new mempool on requested_socket_id with
672 * mbuf size corresponding to requested_mtu.
673 * On success new configuration will be applied.
674 * On error, device will be left unchanged. */
676 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
677 OVS_REQUIRES(dev
->mutex
)
679 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
682 mp
= dpdk_mp_get(dev
->requested_socket_id
, FRAME_LEN_TO_MTU(buf_size
));
684 VLOG_ERR("Failed to create memory pool for netdev "
685 "%s, with MTU %d on socket %d: %s\n",
686 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
687 rte_strerror(rte_errno
));
690 dpdk_mp_put(dev
->dpdk_mp
);
692 dev
->mtu
= dev
->requested_mtu
;
693 dev
->socket_id
= dev
->requested_socket_id
;
694 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
701 check_link_status(struct netdev_dpdk
*dev
)
703 struct rte_eth_link link
;
705 rte_eth_link_get_nowait(dev
->port_id
, &link
);
707 if (dev
->link
.link_status
!= link
.link_status
) {
708 netdev_change_seq_changed(&dev
->up
);
710 dev
->link_reset_cnt
++;
712 if (dev
->link
.link_status
) {
714 "Port "DPDK_PORT_ID_FMT
" Link Up - speed %u Mbps - %s",
715 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
716 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
)
717 ? "full-duplex" : "half-duplex");
719 VLOG_DBG_RL(&rl
, "Port "DPDK_PORT_ID_FMT
" Link Down",
726 dpdk_watchdog(void *dummy OVS_UNUSED
)
728 struct netdev_dpdk
*dev
;
730 pthread_detach(pthread_self());
733 ovs_mutex_lock(&dpdk_mutex
);
734 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
735 ovs_mutex_lock(&dev
->mutex
);
736 if (dev
->type
== DPDK_DEV_ETH
) {
737 check_link_status(dev
);
739 ovs_mutex_unlock(&dev
->mutex
);
741 ovs_mutex_unlock(&dpdk_mutex
);
742 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
749 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
753 struct rte_eth_conf conf
= port_conf
;
755 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
757 if (dev
->mtu
> ETHER_MTU
) {
758 conf
.rxmode
.enable_scatter
= 1;
761 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
762 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
763 /* A device may report more queues than it makes available (this has
764 * been observed for Intel xl710, which reserves some of them for
765 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
766 * available. When this happens we can retry the configuration
767 * and request less queues */
768 while (n_rxq
&& n_txq
) {
770 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
773 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
775 VLOG_WARN("Interface %s eth_dev setup error %s\n",
776 dev
->up
.name
, rte_strerror(-diag
));
780 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
782 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
783 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
787 for (i
= 0; i
< n_txq
; i
++) {
788 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
789 dev
->socket_id
, NULL
);
791 VLOG_INFO("Interface %s unable to setup txq(%d): %s",
792 dev
->up
.name
, i
, rte_strerror(-diag
));
798 /* Retry with less tx queues */
803 for (i
= 0; i
< n_rxq
; i
++) {
804 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
805 dev
->socket_id
, NULL
,
808 VLOG_INFO("Interface %s unable to setup rxq(%d): %s",
809 dev
->up
.name
, i
, rte_strerror(-diag
));
815 /* Retry with less rx queues */
820 dev
->up
.n_rxq
= n_rxq
;
821 dev
->up
.n_txq
= n_txq
;
830 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
832 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
833 VLOG_WARN("Failed to enable flow control on device "DPDK_PORT_ID_FMT
,
839 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
840 OVS_REQUIRES(dev
->mutex
)
842 struct rte_pktmbuf_pool_private
*mbp_priv
;
843 struct rte_eth_dev_info info
;
844 struct ether_addr eth_addr
;
847 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
848 DEV_RX_OFFLOAD_TCP_CKSUM
|
849 DEV_RX_OFFLOAD_IPV4_CKSUM
;
851 rte_eth_dev_info_get(dev
->port_id
, &info
);
853 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
854 rx_chksm_offload_capa
) {
855 VLOG_WARN("Rx checksum offload is not supported on port "
856 DPDK_PORT_ID_FMT
, dev
->port_id
);
857 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
859 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
862 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
863 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
865 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
867 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
868 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
872 diag
= rte_eth_dev_start(dev
->port_id
);
874 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
875 rte_strerror(-diag
));
879 rte_eth_promiscuous_enable(dev
->port_id
);
880 rte_eth_allmulticast_enable(dev
->port_id
);
882 memset(ð_addr
, 0x0, sizeof(eth_addr
));
883 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
884 VLOG_INFO_RL(&rl
, "Port "DPDK_PORT_ID_FMT
": "ETH_ADDR_FMT
,
885 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
887 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
888 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
890 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
891 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
893 /* Get the Flow control configuration for DPDK-ETH */
894 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
896 VLOG_DBG("cannot get flow control parameters on port "DPDK_PORT_ID_FMT
897 ", err=%d", dev
->port_id
, diag
);
903 static struct netdev_dpdk
*
904 netdev_dpdk_cast(const struct netdev
*netdev
)
906 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
909 static struct netdev
*
910 netdev_dpdk_alloc(void)
912 struct netdev_dpdk
*dev
;
914 dev
= dpdk_rte_mzalloc(sizeof *dev
);
922 static struct dpdk_tx_queue
*
923 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
925 struct dpdk_tx_queue
*txqs
;
928 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
930 for (i
= 0; i
< n_txqs
; i
++) {
931 /* Initialize map for vhost devices. */
932 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
933 rte_spinlock_init(&txqs
[i
].tx_lock
);
941 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
942 enum dpdk_dev_type type
, int socket_id
)
943 OVS_REQUIRES(dpdk_mutex
)
945 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
947 ovs_mutex_init(&dev
->mutex
);
949 rte_spinlock_init(&dev
->stats_lock
);
951 /* If the 'sid' is negative, it means that the kernel fails
952 * to obtain the pci numa info. In that situation, always
954 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
955 dev
->requested_socket_id
= dev
->socket_id
;
956 dev
->port_id
= port_no
;
959 dev
->requested_mtu
= ETHER_MTU
;
960 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
961 ovsrcu_index_init(&dev
->vid
, -1);
962 dev
->vhost_reconfigured
= false;
963 dev
->attached
= false;
965 ovsrcu_init(&dev
->qos_conf
, NULL
);
967 ovsrcu_init(&dev
->ingress_policer
, NULL
);
968 dev
->policer_rate
= 0;
969 dev
->policer_burst
= 0;
973 dev
->requested_n_rxq
= NR_QUEUE
;
974 dev
->requested_n_txq
= NR_QUEUE
;
975 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
976 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
978 /* Initialize the flow control to NULL */
979 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
981 /* Initilize the hardware offload flags to 0 */
982 dev
->hw_ol_features
= 0;
984 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
986 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
988 netdev_request_reconfigure(netdev
);
990 dev
->rte_xstats_names
= NULL
;
991 dev
->rte_xstats_names_size
= 0;
993 dev
->rte_xstats_ids
= NULL
;
994 dev
->rte_xstats_ids_size
= 0;
999 /* dev_name must be the prefix followed by a positive decimal number.
1000 * (no leading + or - signs are allowed) */
1002 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
1003 unsigned int *port_no
)
1007 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
1011 cport
= dev_name
+ strlen(prefix
);
1013 if (str_to_uint(cport
, 10, port_no
)) {
1021 vhost_common_construct(struct netdev
*netdev
)
1022 OVS_REQUIRES(dpdk_mutex
)
1024 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
1025 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1027 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
1032 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1033 DPDK_DEV_VHOST
, socket_id
);
1037 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
1039 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1040 const char *name
= netdev
->name
;
1043 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
1044 * the file system. '/' or '\' would traverse directories, so they're not
1045 * acceptable in 'name'. */
1046 if (strchr(name
, '/') || strchr(name
, '\\')) {
1047 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
1048 "A valid name must not include '/' or '\\'",
1053 ovs_mutex_lock(&dpdk_mutex
);
1054 /* Take the name of the vhost-user port and append it to the location where
1055 * the socket is to be created, then register the socket.
1057 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
1058 dpdk_get_vhost_sock_dir(), name
);
1060 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
1061 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
1063 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
1067 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
1068 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1069 dev
->vhost_id
, name
);
1072 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
1073 &virtio_net_device_ops
);
1075 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1076 "port: %s\n", name
);
1080 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1081 1ULL << VIRTIO_NET_F_HOST_TSO4
1082 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1083 | 1ULL << VIRTIO_NET_F_CSUM
);
1085 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1086 "port: %s\n", name
);
1090 err
= rte_vhost_driver_start(dev
->vhost_id
);
1092 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1093 "port: %s\n", name
);
1097 err
= vhost_common_construct(netdev
);
1099 VLOG_ERR("vhost_common_construct failed for vhost user "
1100 "port: %s\n", name
);
1104 ovs_mutex_unlock(&dpdk_mutex
);
1105 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1106 "please migrate to dpdkvhostuserclient ports.");
1111 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1115 ovs_mutex_lock(&dpdk_mutex
);
1116 err
= vhost_common_construct(netdev
);
1118 VLOG_ERR("vhost_common_construct failed for vhost user client"
1119 "port: %s\n", netdev
->name
);
1121 ovs_mutex_unlock(&dpdk_mutex
);
1126 netdev_dpdk_construct(struct netdev
*netdev
)
1130 ovs_mutex_lock(&dpdk_mutex
);
1131 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1132 DPDK_DEV_ETH
, SOCKET0
);
1133 ovs_mutex_unlock(&dpdk_mutex
);
1138 common_destruct(struct netdev_dpdk
*dev
)
1139 OVS_REQUIRES(dpdk_mutex
)
1140 OVS_EXCLUDED(dev
->mutex
)
1142 rte_free(dev
->tx_q
);
1143 dpdk_mp_put(dev
->dpdk_mp
);
1145 ovs_list_remove(&dev
->list_node
);
1146 free(ovsrcu_get_protected(struct ingress_policer
*,
1147 &dev
->ingress_policer
));
1148 ovs_mutex_destroy(&dev
->mutex
);
1152 netdev_dpdk_destruct(struct netdev
*netdev
)
1154 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1155 char devname
[RTE_ETH_NAME_MAX_LEN
];
1157 ovs_mutex_lock(&dpdk_mutex
);
1159 rte_eth_dev_stop(dev
->port_id
);
1161 if (dev
->attached
) {
1162 rte_eth_dev_close(dev
->port_id
);
1163 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1164 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1166 VLOG_INFO("Device '%s' has been detached", devname
);
1170 netdev_dpdk_clear_xstats(dev
);
1172 common_destruct(dev
);
1174 ovs_mutex_unlock(&dpdk_mutex
);
1177 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1178 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1179 * deadlock, none of the mutexes must be held while calling this function. */
1181 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1183 OVS_EXCLUDED(dpdk_mutex
)
1184 OVS_EXCLUDED(dev
->mutex
)
1186 return rte_vhost_driver_unregister(vhost_id
);
1190 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1192 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1195 ovs_mutex_lock(&dpdk_mutex
);
1197 /* Guest becomes an orphan if still attached. */
1198 if (netdev_dpdk_get_vid(dev
) >= 0
1199 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1200 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1202 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1203 "socket '%s' must be restarted.", dev
->vhost_id
);
1206 vhost_id
= xstrdup(dev
->vhost_id
);
1208 common_destruct(dev
);
1210 ovs_mutex_unlock(&dpdk_mutex
);
1216 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1217 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1218 netdev
->name
, vhost_id
);
1219 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1220 /* OVS server mode - remove this socket from list for deletion */
1221 fatal_signal_remove_file_to_unlink(vhost_id
);
1228 netdev_dpdk_dealloc(struct netdev
*netdev
)
1230 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1236 netdev_dpdk_clear_xstats(struct netdev_dpdk
*dev
)
1238 /* If statistics are already allocated, we have to
1239 * reconfigure, as port_id could have been changed. */
1240 if (dev
->rte_xstats_names
) {
1241 free(dev
->rte_xstats_names
);
1242 dev
->rte_xstats_names
= NULL
;
1243 dev
->rte_xstats_names_size
= 0;
1245 if (dev
->rte_xstats_ids
) {
1246 free(dev
->rte_xstats_ids
);
1247 dev
->rte_xstats_ids
= NULL
;
1248 dev
->rte_xstats_ids_size
= 0;
1253 netdev_dpdk_get_xstat_name(struct netdev_dpdk
*dev
, uint64_t id
)
1255 if (id
>= dev
->rte_xstats_names_size
) {
1258 return dev
->rte_xstats_names
[id
].name
;
1262 netdev_dpdk_configure_xstats(struct netdev_dpdk
*dev
)
1263 OVS_REQUIRES(dev
->mutex
)
1267 struct rte_eth_xstat
*rte_xstats
;
1272 /* Retrieving all XSTATS names. If something will go wrong
1273 * or amount of counters will be equal 0, rte_xstats_names
1274 * buffer will be marked as NULL, and any further xstats
1275 * query won't be performed (e.g. during netdev_dpdk_get_stats
1281 if (dev
->rte_xstats_names
== NULL
|| dev
->rte_xstats_ids
== NULL
) {
1282 dev
->rte_xstats_names_size
=
1283 rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
1285 if (dev
->rte_xstats_names_size
< 0) {
1286 VLOG_WARN("Cannot get XSTATS for port: "DPDK_PORT_ID_FMT
,
1288 dev
->rte_xstats_names_size
= 0;
1290 /* Reserve memory for xstats names and values */
1291 dev
->rte_xstats_names
= xcalloc(dev
->rte_xstats_names_size
,
1292 sizeof *dev
->rte_xstats_names
);
1294 if (dev
->rte_xstats_names
) {
1295 /* Retreive xstats names */
1297 rte_eth_xstats_get_names(dev
->port_id
,
1298 dev
->rte_xstats_names
,
1299 dev
->rte_xstats_names_size
);
1301 if (rte_xstats_len
< 0) {
1302 VLOG_WARN("Cannot get XSTATS names for port: "
1303 DPDK_PORT_ID_FMT
, dev
->port_id
);
1305 } else if (rte_xstats_len
!= dev
->rte_xstats_names_size
) {
1306 VLOG_WARN("XSTATS size doesn't match for port: "
1307 DPDK_PORT_ID_FMT
, dev
->port_id
);
1311 dev
->rte_xstats_ids
= xcalloc(dev
->rte_xstats_names_size
,
1314 /* We have to calculate number of counters */
1315 rte_xstats
= xmalloc(rte_xstats_len
* sizeof *rte_xstats
);
1316 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
1318 /* Retreive xstats values */
1319 if (rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
1320 rte_xstats_len
) > 0) {
1321 dev
->rte_xstats_ids_size
= 0;
1323 for (uint32_t i
= 0; i
< rte_xstats_len
; i
++) {
1324 id
= rte_xstats
[i
].id
;
1325 name
= netdev_dpdk_get_xstat_name(dev
, id
);
1326 /* We need to filter out everything except
1327 * dropped, error and management counters */
1328 if (string_ends_with(name
, "_errors") ||
1329 strstr(name
, "_management_") ||
1330 string_ends_with(name
, "_dropped")) {
1332 dev
->rte_xstats_ids
[xstats_no
] = id
;
1336 dev
->rte_xstats_ids_size
= xstats_no
;
1339 VLOG_WARN("Can't get XSTATS IDs for port: "
1340 DPDK_PORT_ID_FMT
, dev
->port_id
);
1347 /* Already configured */
1353 netdev_dpdk_clear_xstats(dev
);
1359 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1361 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1363 ovs_mutex_lock(&dev
->mutex
);
1365 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1366 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1367 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1368 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1369 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1371 if (dev
->type
== DPDK_DEV_ETH
) {
1372 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1373 dev
->requested_rxq_size
);
1374 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1376 smap_add_format(args
, "requested_txq_descriptors", "%d",
1377 dev
->requested_txq_size
);
1378 smap_add_format(args
, "configured_txq_descriptors", "%d",
1380 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1381 smap_add(args
, "rx_csum_offload", "true");
1383 smap_add(args
, "rx_csum_offload", "false");
1386 ovs_mutex_unlock(&dev
->mutex
);
1391 static struct netdev_dpdk
*
1392 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1393 OVS_REQUIRES(dpdk_mutex
)
1395 struct netdev_dpdk
*dev
;
1397 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1398 if (dev
->port_id
== port_id
) {
1407 netdev_dpdk_get_port_by_mac(const char *mac_str
)
1409 dpdk_port_t port_id
;
1410 struct eth_addr mac
, port_mac
;
1412 if (!eth_addr_from_string(mac_str
, &mac
)) {
1413 VLOG_ERR("invalid mac: %s", mac_str
);
1414 return DPDK_ETH_PORT_ID_INVALID
;
1417 RTE_ETH_FOREACH_DEV (port_id
) {
1418 struct ether_addr ea
;
1420 rte_eth_macaddr_get(port_id
, &ea
);
1421 memcpy(port_mac
.ea
, ea
.addr_bytes
, ETH_ADDR_LEN
);
1422 if (eth_addr_equals(mac
, port_mac
)) {
1427 return DPDK_ETH_PORT_ID_INVALID
;
1431 * Normally, a PCI id is enough for identifying a specific DPDK port.
1432 * However, for some NICs having multiple ports sharing the same PCI
1433 * id, using PCI id won't work then.
1435 * To fix that, here one more method is introduced: "class=eth,mac=$MAC".
1437 * Note that the compatibility is fully kept: user can still use the
1438 * PCI id for adding ports (when it's enough for them).
1441 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1442 const char *devargs
, char **errp
)
1445 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1447 if (strncmp(devargs
, "class=eth,mac=", 14) == 0) {
1448 new_port_id
= netdev_dpdk_get_port_by_mac(&devargs
[14]);
1450 name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1451 if (rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1452 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1453 /* Device not found in DPDK, attempt to attach it */
1454 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1455 /* Attach successful */
1456 dev
->attached
= true;
1457 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1459 /* Attach unsuccessful */
1460 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1466 if (new_port_id
== DPDK_ETH_PORT_ID_INVALID
) {
1467 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK", devargs
);
1474 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1475 OVS_REQUIRES(dev
->mutex
)
1479 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1480 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1481 dev
->requested_n_rxq
= new_n_rxq
;
1482 netdev_request_reconfigure(&dev
->up
);
1487 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1488 const char *flag
, int default_size
, int *new_size
)
1490 int queue_size
= smap_get_int(args
, flag
, default_size
);
1492 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1493 || !is_pow2(queue_size
)) {
1494 queue_size
= default_size
;
1497 if (queue_size
!= *new_size
) {
1498 *new_size
= queue_size
;
1499 netdev_request_reconfigure(netdev
);
1504 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1507 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1508 bool rx_fc_en
, tx_fc_en
, autoneg
;
1509 enum rte_eth_fc_mode fc_mode
;
1510 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1511 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1512 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1514 const char *new_devargs
;
1517 ovs_mutex_lock(&dpdk_mutex
);
1518 ovs_mutex_lock(&dev
->mutex
);
1520 dpdk_set_rxq_config(dev
, args
);
1522 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1523 NIC_PORT_DEFAULT_RXQ_SIZE
,
1524 &dev
->requested_rxq_size
);
1525 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1526 NIC_PORT_DEFAULT_TXQ_SIZE
,
1527 &dev
->requested_txq_size
);
1529 new_devargs
= smap_get(args
, "dpdk-devargs");
1531 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1532 /* The user requested a new device. If we return error, the caller
1533 * will delete this netdev and try to recreate it. */
1538 /* dpdk-devargs is required for device configuration */
1539 if (new_devargs
&& new_devargs
[0]) {
1540 /* Don't process dpdk-devargs if value is unchanged and port id
1542 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1543 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1544 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1547 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1549 } else if (new_port_id
== dev
->port_id
) {
1550 /* Already configured, do not reconfigure again */
1553 struct netdev_dpdk
*dup_dev
;
1555 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1557 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1558 "which is already in use by '%s'",
1559 netdev_get_name(netdev
), new_devargs
,
1560 netdev_get_name(&dup_dev
->up
));
1563 int sid
= rte_eth_dev_socket_id(new_port_id
);
1565 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1566 dev
->devargs
= xstrdup(new_devargs
);
1567 dev
->port_id
= new_port_id
;
1568 netdev_request_reconfigure(&dev
->up
);
1569 netdev_dpdk_clear_xstats(dev
);
1575 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1576 "The old 'dpdk<port_id>' names are not supported",
1577 netdev_get_name(netdev
));
1585 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1586 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1587 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1589 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1590 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1591 dev
->fc_conf
.mode
= fc_mode
;
1592 dev
->fc_conf
.autoneg
= autoneg
;
1593 dpdk_eth_flow_ctrl_setup(dev
);
1597 ovs_mutex_unlock(&dev
->mutex
);
1598 ovs_mutex_unlock(&dpdk_mutex
);
1604 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1605 char **errp OVS_UNUSED
)
1607 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1609 ovs_mutex_lock(&dev
->mutex
);
1610 dpdk_set_rxq_config(dev
, args
);
1611 ovs_mutex_unlock(&dev
->mutex
);
1617 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1618 const struct smap
*args
,
1619 char **errp OVS_UNUSED
)
1621 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1624 ovs_mutex_lock(&dev
->mutex
);
1625 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1626 path
= smap_get(args
, "vhost-server-path");
1627 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1628 strcpy(dev
->vhost_id
, path
);
1629 /* check zero copy configuration */
1630 if (smap_get_bool(args
, "dq-zero-copy", false)) {
1631 dev
->vhost_driver_flags
|= RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
1633 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
1635 netdev_request_reconfigure(netdev
);
1638 ovs_mutex_unlock(&dev
->mutex
);
1644 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1646 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1648 return dev
->socket_id
;
1651 /* Sets the number of tx queues for the dpdk interface. */
1653 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1655 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1657 ovs_mutex_lock(&dev
->mutex
);
1659 if (dev
->requested_n_txq
== n_txq
) {
1663 dev
->requested_n_txq
= n_txq
;
1664 netdev_request_reconfigure(netdev
);
1667 ovs_mutex_unlock(&dev
->mutex
);
1671 static struct netdev_rxq
*
1672 netdev_dpdk_rxq_alloc(void)
1674 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1683 static struct netdev_rxq_dpdk
*
1684 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1686 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1690 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1692 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1693 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1695 ovs_mutex_lock(&dev
->mutex
);
1696 rx
->port_id
= dev
->port_id
;
1697 ovs_mutex_unlock(&dev
->mutex
);
1703 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1708 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1710 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1715 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1716 * 'pkts', even in case of failure.
1718 * Returns the number of packets that weren't transmitted. */
1720 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1721 struct rte_mbuf
**pkts
, int cnt
)
1725 while (nb_tx
!= cnt
) {
1728 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1736 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1737 /* Free buffers, which we couldn't transmit, one at a time (each
1738 * packet could come from a different mempool) */
1741 for (i
= nb_tx
; i
< cnt
; i
++) {
1742 rte_pktmbuf_free(pkts
[i
]);
1750 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1751 struct rte_mbuf
*pkt
, uint64_t time
)
1753 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1755 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1760 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1761 struct rte_mbuf
**pkts
, int pkt_cnt
,
1766 struct rte_mbuf
*pkt
= NULL
;
1767 uint64_t current_time
= rte_rdtsc();
1769 for (i
= 0; i
< pkt_cnt
; i
++) {
1771 /* Handle current packet */
1772 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1779 rte_pktmbuf_free(pkt
);
1788 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1789 int pkt_cnt
, bool may_steal
)
1793 rte_spinlock_lock(&policer
->policer_lock
);
1794 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1795 pkt_cnt
, may_steal
);
1796 rte_spinlock_unlock(&policer
->policer_lock
);
1802 is_vhost_running(struct netdev_dpdk
*dev
)
1804 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1808 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1809 unsigned int packet_size
)
1811 /* Hard-coded search for the size bucket. */
1812 if (packet_size
< 256) {
1813 if (packet_size
>= 128) {
1814 stats
->rx_128_to_255_packets
++;
1815 } else if (packet_size
<= 64) {
1816 stats
->rx_1_to_64_packets
++;
1818 stats
->rx_65_to_127_packets
++;
1821 if (packet_size
>= 1523) {
1822 stats
->rx_1523_to_max_packets
++;
1823 } else if (packet_size
>= 1024) {
1824 stats
->rx_1024_to_1522_packets
++;
1825 } else if (packet_size
< 512) {
1826 stats
->rx_256_to_511_packets
++;
1828 stats
->rx_512_to_1023_packets
++;
1834 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1835 struct dp_packet
**packets
, int count
,
1839 unsigned int packet_size
;
1840 struct dp_packet
*packet
;
1842 stats
->rx_packets
+= count
;
1843 stats
->rx_dropped
+= dropped
;
1844 for (i
= 0; i
< count
; i
++) {
1845 packet
= packets
[i
];
1846 packet_size
= dp_packet_size(packet
);
1848 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1849 /* This only protects the following multicast counting from
1850 * too short packets, but it does not stop the packet from
1851 * further processing. */
1853 stats
->rx_length_errors
++;
1857 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1859 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1860 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1864 stats
->rx_bytes
+= packet_size
;
1869 * The receive path for the vhost port is the TX path out from guest.
1872 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1873 struct dp_packet_batch
*batch
)
1875 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1876 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1878 uint16_t dropped
= 0;
1879 int qid
= rxq
->queue_id
;
1880 int vid
= netdev_dpdk_get_vid(dev
);
1882 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
1883 || !(dev
->flags
& NETDEV_UP
))) {
1887 nb_rx
= rte_vhost_dequeue_burst(vid
, qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1889 (struct rte_mbuf
**) batch
->packets
,
1897 nb_rx
= ingress_policer_run(policer
,
1898 (struct rte_mbuf
**) batch
->packets
,
1903 rte_spinlock_lock(&dev
->stats_lock
);
1904 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1906 rte_spinlock_unlock(&dev
->stats_lock
);
1908 batch
->count
= nb_rx
;
1909 dp_packet_batch_init_packet_fields(batch
);
1915 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1917 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1918 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1919 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1923 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1927 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1928 (struct rte_mbuf
**) batch
->packets
,
1936 nb_rx
= ingress_policer_run(policer
,
1937 (struct rte_mbuf
**) batch
->packets
,
1942 /* Update stats to reflect dropped packets */
1943 if (OVS_UNLIKELY(dropped
)) {
1944 rte_spinlock_lock(&dev
->stats_lock
);
1945 dev
->stats
.rx_dropped
+= dropped
;
1946 rte_spinlock_unlock(&dev
->stats_lock
);
1949 batch
->count
= nb_rx
;
1950 dp_packet_batch_init_packet_fields(batch
);
1956 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1957 int cnt
, bool may_steal
)
1959 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1962 rte_spinlock_lock(&qos_conf
->lock
);
1963 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1964 rte_spinlock_unlock(&qos_conf
->lock
);
1971 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1976 struct rte_mbuf
*pkt
;
1978 for (i
= 0; i
< pkt_cnt
; i
++) {
1980 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1981 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1982 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1983 rte_pktmbuf_free(pkt
);
1987 if (OVS_UNLIKELY(i
!= cnt
)) {
1997 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1998 struct dp_packet
**packets
,
2003 int sent
= attempted
- dropped
;
2005 stats
->tx_packets
+= sent
;
2006 stats
->tx_dropped
+= dropped
;
2008 for (i
= 0; i
< sent
; i
++) {
2009 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
2014 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
2015 struct dp_packet
**pkts
, int cnt
)
2017 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2018 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
2019 unsigned int total_pkts
= cnt
;
2020 unsigned int dropped
= 0;
2022 int vid
= netdev_dpdk_get_vid(dev
);
2024 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
2026 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
|| qid
< 0
2027 || !(dev
->flags
& NETDEV_UP
))) {
2028 rte_spinlock_lock(&dev
->stats_lock
);
2029 dev
->stats
.tx_dropped
+= cnt
;
2030 rte_spinlock_unlock(&dev
->stats_lock
);
2034 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
2036 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
2037 /* Check has QoS has been configured for the netdev */
2038 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
2039 dropped
= total_pkts
- cnt
;
2042 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
2043 unsigned int tx_pkts
;
2045 tx_pkts
= rte_vhost_enqueue_burst(vid
, vhost_qid
, cur_pkts
, cnt
);
2046 if (OVS_LIKELY(tx_pkts
)) {
2047 /* Packets have been sent.*/
2049 /* Prepare for possible retry.*/
2050 cur_pkts
= &cur_pkts
[tx_pkts
];
2052 /* No packets sent - do not retry.*/
2055 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
2057 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2059 rte_spinlock_lock(&dev
->stats_lock
);
2060 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
2062 rte_spinlock_unlock(&dev
->stats_lock
);
2065 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
2066 dp_packet_delete(pkts
[i
]);
2070 /* Tx function. Transmit packets indefinitely */
2072 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
2073 OVS_NO_THREAD_SAFETY_ANALYSIS
2075 const size_t batch_cnt
= dp_packet_batch_size(batch
);
2076 #if !defined(__CHECKER__) && !defined(_WIN32)
2077 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
2079 /* Sparse or MSVC doesn't like variable length array. */
2080 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
2082 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2083 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
2084 uint32_t cnt
= batch_cnt
;
2085 uint32_t dropped
= 0;
2087 if (dev
->type
!= DPDK_DEV_VHOST
) {
2088 /* Check if QoS has been configured for this netdev. */
2089 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
2091 dropped
+= batch_cnt
- cnt
;
2096 for (uint32_t i
= 0; i
< cnt
; i
++) {
2097 struct dp_packet
*packet
= batch
->packets
[i
];
2098 uint32_t size
= dp_packet_size(packet
);
2100 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
2101 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
2102 size
, dev
->max_packet_len
);
2108 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
2109 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
2114 /* We have to do a copy for now */
2115 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
2116 dp_packet_data(packet
), size
);
2117 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
2122 if (OVS_LIKELY(txcnt
)) {
2123 if (dev
->type
== DPDK_DEV_VHOST
) {
2124 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
2127 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
2131 if (OVS_UNLIKELY(dropped
)) {
2132 rte_spinlock_lock(&dev
->stats_lock
);
2133 dev
->stats
.tx_dropped
+= dropped
;
2134 rte_spinlock_unlock(&dev
->stats_lock
);
2139 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
2140 struct dp_packet_batch
*batch
,
2141 bool concurrent_txq OVS_UNUSED
)
2144 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2145 dpdk_do_tx_copy(netdev
, qid
, batch
);
2146 dp_packet_delete_batch(batch
, true);
2148 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
2154 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
2155 struct dp_packet_batch
*batch
,
2156 bool concurrent_txq
)
2158 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
2159 dp_packet_delete_batch(batch
, true);
2163 if (OVS_UNLIKELY(concurrent_txq
)) {
2164 qid
= qid
% dev
->up
.n_txq
;
2165 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
2168 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2169 struct netdev
*netdev
= &dev
->up
;
2171 dpdk_do_tx_copy(netdev
, qid
, batch
);
2172 dp_packet_delete_batch(batch
, true);
2174 int tx_cnt
, dropped
;
2175 int batch_cnt
= dp_packet_batch_size(batch
);
2176 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
2178 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
2179 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
2180 dropped
= batch_cnt
- tx_cnt
;
2182 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
2184 if (OVS_UNLIKELY(dropped
)) {
2185 rte_spinlock_lock(&dev
->stats_lock
);
2186 dev
->stats
.tx_dropped
+= dropped
;
2187 rte_spinlock_unlock(&dev
->stats_lock
);
2191 if (OVS_UNLIKELY(concurrent_txq
)) {
2192 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2197 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
2198 struct dp_packet_batch
*batch
, bool concurrent_txq
)
2200 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2202 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
2207 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
2209 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2211 ovs_mutex_lock(&dev
->mutex
);
2212 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
2214 netdev_change_seq_changed(netdev
);
2216 ovs_mutex_unlock(&dev
->mutex
);
2222 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
2224 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2226 ovs_mutex_lock(&dev
->mutex
);
2228 ovs_mutex_unlock(&dev
->mutex
);
2234 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
2236 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2238 ovs_mutex_lock(&dev
->mutex
);
2240 ovs_mutex_unlock(&dev
->mutex
);
2246 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2248 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2250 /* XXX: Ensure that the overall frame length of the requested MTU does not
2251 * surpass the NETDEV_DPDK_MAX_PKT_LEN. DPDK device drivers differ in how
2252 * the L2 frame length is calculated for a given MTU when
2253 * rte_eth_dev_set_mtu(mtu) is called e.g. i40e driver includes 2 x vlan
2254 * headers, the em driver includes 1 x vlan header, the ixgbe driver does
2255 * not include vlan headers. As such we should use
2256 * MTU_TO_MAX_FRAME_LEN(mtu) which includes an additional 2 x vlan headers
2257 * (8 bytes) for comparison. This avoids a failure later with
2258 * rte_eth_dev_set_mtu(). This approach should be used until DPDK provides
2259 * a method to retrieve the upper bound MTU for a given device.
2261 if (MTU_TO_MAX_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2262 || mtu
< ETHER_MIN_MTU
) {
2263 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2267 ovs_mutex_lock(&dev
->mutex
);
2268 if (dev
->requested_mtu
!= mtu
) {
2269 dev
->requested_mtu
= mtu
;
2270 netdev_request_reconfigure(netdev
);
2272 ovs_mutex_unlock(&dev
->mutex
);
2278 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2281 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2282 struct netdev_stats
*stats
)
2284 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2286 ovs_mutex_lock(&dev
->mutex
);
2288 rte_spinlock_lock(&dev
->stats_lock
);
2289 /* Supported Stats */
2290 stats
->rx_packets
= dev
->stats
.rx_packets
;
2291 stats
->tx_packets
= dev
->stats
.tx_packets
;
2292 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2293 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2294 stats
->multicast
= dev
->stats
.multicast
;
2295 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2296 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2297 stats
->rx_errors
= dev
->stats
.rx_errors
;
2298 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2300 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2301 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2302 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2303 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2304 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2305 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2306 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2308 rte_spinlock_unlock(&dev
->stats_lock
);
2310 ovs_mutex_unlock(&dev
->mutex
);
2316 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2317 const struct rte_eth_xstat
*xstats
,
2318 const struct rte_eth_xstat_name
*names
,
2319 const unsigned int size
)
2321 for (unsigned int i
= 0; i
< size
; i
++) {
2322 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2323 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2324 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2325 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2326 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2327 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2328 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2329 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2330 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2331 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2332 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2333 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2334 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2335 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2336 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2337 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2338 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2339 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2340 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2341 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2342 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2343 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2344 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2345 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2346 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2347 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2348 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2349 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2350 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2351 stats
->multicast
= xstats
[i
].value
;
2352 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2353 stats
->tx_multicast_packets
= xstats
[i
].value
;
2354 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2355 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2356 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2357 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2358 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2359 stats
->rx_undersized_errors
= xstats
[i
].value
;
2360 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2361 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2362 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2363 stats
->rx_jabber_errors
= xstats
[i
].value
;
2369 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2371 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2372 struct rte_eth_stats rte_stats
;
2375 netdev_dpdk_get_carrier(netdev
, &gg
);
2376 ovs_mutex_lock(&dev
->mutex
);
2378 struct rte_eth_xstat
*rte_xstats
= NULL
;
2379 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2380 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2382 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2383 VLOG_ERR("Can't get ETH statistics for port: "DPDK_PORT_ID_FMT
,
2385 ovs_mutex_unlock(&dev
->mutex
);
2389 /* Get length of statistics */
2390 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2391 if (rte_xstats_len
< 0) {
2392 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2396 /* Reserve memory for xstats names and values */
2397 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2398 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2400 /* Retreive xstats names */
2401 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2404 if (rte_xstats_new_len
!= rte_xstats_len
) {
2405 VLOG_WARN("Cannot get XSTATS names for port: "DPDK_PORT_ID_FMT
,
2409 /* Retreive xstats values */
2410 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2411 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2413 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2414 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2417 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2423 free(rte_xstats_names
);
2425 stats
->rx_packets
= rte_stats
.ipackets
;
2426 stats
->tx_packets
= rte_stats
.opackets
;
2427 stats
->rx_bytes
= rte_stats
.ibytes
;
2428 stats
->tx_bytes
= rte_stats
.obytes
;
2429 stats
->rx_errors
= rte_stats
.ierrors
;
2430 stats
->tx_errors
= rte_stats
.oerrors
;
2432 rte_spinlock_lock(&dev
->stats_lock
);
2433 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2434 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2435 rte_spinlock_unlock(&dev
->stats_lock
);
2437 /* These are the available DPDK counters for packets not received due to
2438 * local resource constraints in DPDK and NIC respectively. */
2439 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2440 stats
->rx_missed_errors
= rte_stats
.imissed
;
2442 ovs_mutex_unlock(&dev
->mutex
);
2448 netdev_dpdk_get_custom_stats(const struct netdev
*netdev
,
2449 struct netdev_custom_stats
*custom_stats
)
2453 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2456 ovs_mutex_lock(&dev
->mutex
);
2458 if (netdev_dpdk_configure_xstats(dev
)) {
2459 uint64_t *values
= xcalloc(dev
->rte_xstats_ids_size
,
2463 rte_eth_xstats_get_by_id(dev
->port_id
, dev
->rte_xstats_ids
,
2464 values
, dev
->rte_xstats_ids_size
);
2466 if (rte_xstats_ret
> 0 &&
2467 rte_xstats_ret
<= dev
->rte_xstats_ids_size
) {
2469 custom_stats
->size
= rte_xstats_ret
;
2470 custom_stats
->counters
=
2471 (struct netdev_custom_counter
*) xcalloc(rte_xstats_ret
,
2472 sizeof(struct netdev_custom_counter
));
2474 for (i
= 0; i
< rte_xstats_ret
; i
++) {
2475 ovs_strlcpy(custom_stats
->counters
[i
].name
,
2476 netdev_dpdk_get_xstat_name(dev
,
2477 dev
->rte_xstats_ids
[i
]),
2478 NETDEV_CUSTOM_STATS_NAME_SIZE
);
2479 custom_stats
->counters
[i
].value
= values
[i
];
2482 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2484 custom_stats
->counters
= NULL
;
2485 custom_stats
->size
= 0;
2486 /* Let's clear statistics cache, so it will be
2488 netdev_dpdk_clear_xstats(dev
);
2494 ovs_mutex_unlock(&dev
->mutex
);
2500 netdev_dpdk_get_features(const struct netdev
*netdev
,
2501 enum netdev_features
*current
,
2502 enum netdev_features
*advertised
,
2503 enum netdev_features
*supported
,
2504 enum netdev_features
*peer
)
2506 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2507 struct rte_eth_link link
;
2509 ovs_mutex_lock(&dev
->mutex
);
2511 ovs_mutex_unlock(&dev
->mutex
);
2513 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2514 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2515 *current
= NETDEV_F_10MB_HD
;
2517 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2518 *current
= NETDEV_F_100MB_HD
;
2520 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2521 *current
= NETDEV_F_1GB_HD
;
2523 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2524 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2525 *current
= NETDEV_F_10MB_FD
;
2527 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2528 *current
= NETDEV_F_100MB_FD
;
2530 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2531 *current
= NETDEV_F_1GB_FD
;
2533 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2534 *current
= NETDEV_F_10GB_FD
;
2538 if (link
.link_autoneg
) {
2539 *current
|= NETDEV_F_AUTONEG
;
2542 *advertised
= *supported
= *peer
= 0;
2547 static struct ingress_policer
*
2548 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2550 struct ingress_policer
*policer
= NULL
;
2551 uint64_t rate_bytes
;
2552 uint64_t burst_bytes
;
2555 policer
= xmalloc(sizeof *policer
);
2556 rte_spinlock_init(&policer
->policer_lock
);
2558 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2559 rate_bytes
= rate
* 1000ULL / 8;
2560 burst_bytes
= burst
* 1000ULL / 8;
2562 policer
->app_srtcm_params
.cir
= rate_bytes
;
2563 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2564 policer
->app_srtcm_params
.ebs
= 0;
2565 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2566 &policer
->app_srtcm_params
);
2568 VLOG_ERR("Could not create rte meter for ingress policer");
2577 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2578 uint32_t policer_burst
)
2580 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2581 struct ingress_policer
*policer
;
2583 /* Force to 0 if no rate specified,
2584 * default to 8000 kbits if burst is 0,
2585 * else stick with user-specified value.
2587 policer_burst
= (!policer_rate
? 0
2588 : !policer_burst
? 8000
2591 ovs_mutex_lock(&dev
->mutex
);
2593 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2594 &dev
->ingress_policer
);
2596 if (dev
->policer_rate
== policer_rate
&&
2597 dev
->policer_burst
== policer_burst
) {
2598 /* Assume that settings haven't changed since we last set them. */
2599 ovs_mutex_unlock(&dev
->mutex
);
2603 /* Destroy any existing ingress policer for the device if one exists */
2605 ovsrcu_postpone(free
, policer
);
2608 if (policer_rate
!= 0) {
2609 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2613 ovsrcu_set(&dev
->ingress_policer
, policer
);
2614 dev
->policer_rate
= policer_rate
;
2615 dev
->policer_burst
= policer_burst
;
2616 ovs_mutex_unlock(&dev
->mutex
);
2622 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2624 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2626 ovs_mutex_lock(&dev
->mutex
);
2627 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2628 * postive integer to meet RFC 2863 recommendations.
2630 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2631 ovs_mutex_unlock(&dev
->mutex
);
2637 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2639 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2641 ovs_mutex_lock(&dev
->mutex
);
2642 check_link_status(dev
);
2643 *carrier
= dev
->link
.link_status
;
2645 ovs_mutex_unlock(&dev
->mutex
);
2651 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2653 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2655 ovs_mutex_lock(&dev
->mutex
);
2657 if (is_vhost_running(dev
)) {
2663 ovs_mutex_unlock(&dev
->mutex
);
2668 static long long int
2669 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2671 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2672 long long int carrier_resets
;
2674 ovs_mutex_lock(&dev
->mutex
);
2675 carrier_resets
= dev
->link_reset_cnt
;
2676 ovs_mutex_unlock(&dev
->mutex
);
2678 return carrier_resets
;
2682 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2683 long long int interval OVS_UNUSED
)
2689 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2690 enum netdev_flags off
, enum netdev_flags on
,
2691 enum netdev_flags
*old_flagsp
)
2692 OVS_REQUIRES(dev
->mutex
)
2694 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2698 *old_flagsp
= dev
->flags
;
2702 if (dev
->flags
== *old_flagsp
) {
2706 if (dev
->type
== DPDK_DEV_ETH
) {
2707 if (dev
->flags
& NETDEV_PROMISC
) {
2708 rte_eth_promiscuous_enable(dev
->port_id
);
2711 netdev_change_seq_changed(&dev
->up
);
2713 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2714 * running then change netdev's change_seq to trigger link state
2717 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2718 && is_vhost_running(dev
)) {
2719 netdev_change_seq_changed(&dev
->up
);
2721 /* Clear statistics if device is getting up. */
2722 if (NETDEV_UP
& on
) {
2723 rte_spinlock_lock(&dev
->stats_lock
);
2724 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2725 rte_spinlock_unlock(&dev
->stats_lock
);
2734 netdev_dpdk_update_flags(struct netdev
*netdev
,
2735 enum netdev_flags off
, enum netdev_flags on
,
2736 enum netdev_flags
*old_flagsp
)
2738 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2741 ovs_mutex_lock(&dev
->mutex
);
2742 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2743 ovs_mutex_unlock(&dev
->mutex
);
2749 netdev_dpdk_vhost_user_get_status(const struct netdev
*netdev
,
2752 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2754 ovs_mutex_lock(&dev
->mutex
);
2756 bool client_mode
= dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
;
2757 smap_add_format(args
, "mode", "%s", client_mode
? "client" : "server");
2759 int vid
= netdev_dpdk_get_vid(dev
);
2761 smap_add_format(args
, "status", "disconnected");
2762 ovs_mutex_unlock(&dev
->mutex
);
2765 smap_add_format(args
, "status", "connected");
2768 char socket_name
[PATH_MAX
];
2769 if (!rte_vhost_get_ifname(vid
, socket_name
, PATH_MAX
)) {
2770 smap_add_format(args
, "socket", "%s", socket_name
);
2774 if (!rte_vhost_get_negotiated_features(vid
, &features
)) {
2775 smap_add_format(args
, "features", "0x%016"PRIx64
, features
);
2779 if (!rte_vhost_get_mtu(vid
, &mtu
)) {
2780 smap_add_format(args
, "mtu", "%d", mtu
);
2783 int numa
= rte_vhost_get_numa_node(vid
);
2785 smap_add_format(args
, "numa", "%d", numa
);
2788 uint16_t vring_num
= rte_vhost_get_vring_num(vid
);
2790 smap_add_format(args
, "num_of_vrings", "%d", vring_num
);
2793 for (int i
= 0; i
< vring_num
; i
++) {
2794 struct rte_vhost_vring vring
;
2795 char vhost_vring
[16];
2797 rte_vhost_get_vhost_vring(vid
, i
, &vring
);
2798 snprintf(vhost_vring
, 16, "vring_%d_size", i
);
2799 smap_add_format(args
, vhost_vring
, "%d", vring
.size
);
2802 ovs_mutex_unlock(&dev
->mutex
);
2807 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2809 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2810 struct rte_eth_dev_info dev_info
;
2812 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2816 ovs_mutex_lock(&dev
->mutex
);
2817 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2818 ovs_mutex_unlock(&dev
->mutex
);
2820 smap_add_format(args
, "port_no", DPDK_PORT_ID_FMT
, dev
->port_id
);
2821 smap_add_format(args
, "numa_id", "%d",
2822 rte_eth_dev_socket_id(dev
->port_id
));
2823 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2824 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2825 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2826 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2827 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2828 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2829 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2830 dev_info
.max_hash_mac_addrs
);
2831 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2832 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2834 /* Querying the DPDK library for iftype may be done in future, pending
2835 * support; cf. RFC 3635 Section 3.2.4. */
2836 enum { IF_TYPE_ETHERNETCSMACD
= 6 };
2838 smap_add_format(args
, "if_type", "%"PRIu32
, IF_TYPE_ETHERNETCSMACD
);
2839 smap_add_format(args
, "if_descr", "%s %s", rte_version(),
2840 dev_info
.driver_name
);
2842 if (dev_info
.pci_dev
) {
2843 smap_add_format(args
, "pci-vendor_id", "0x%u",
2844 dev_info
.pci_dev
->id
.vendor_id
);
2845 smap_add_format(args
, "pci-device_id", "0x%x",
2846 dev_info
.pci_dev
->id
.device_id
);
2853 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2854 OVS_REQUIRES(dev
->mutex
)
2856 enum netdev_flags old_flags
;
2859 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2861 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2866 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2867 const char *argv
[], void *aux OVS_UNUSED
)
2871 if (!strcasecmp(argv
[argc
- 1], "up")) {
2873 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2876 unixctl_command_reply_error(conn
, "Invalid Admin State");
2881 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2883 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2884 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2886 ovs_mutex_lock(&dev
->mutex
);
2887 netdev_dpdk_set_admin_state__(dev
, up
);
2888 ovs_mutex_unlock(&dev
->mutex
);
2890 netdev_close(netdev
);
2892 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2893 netdev_close(netdev
);
2897 struct netdev_dpdk
*dev
;
2899 ovs_mutex_lock(&dpdk_mutex
);
2900 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2901 ovs_mutex_lock(&dev
->mutex
);
2902 netdev_dpdk_set_admin_state__(dev
, up
);
2903 ovs_mutex_unlock(&dev
->mutex
);
2905 ovs_mutex_unlock(&dpdk_mutex
);
2907 unixctl_command_reply(conn
, "OK");
2911 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2912 const char *argv
[], void *aux OVS_UNUSED
)
2916 dpdk_port_t port_id
;
2917 char devname
[RTE_ETH_NAME_MAX_LEN
];
2918 struct netdev_dpdk
*dev
;
2920 ovs_mutex_lock(&dpdk_mutex
);
2922 if (rte_eth_dev_get_port_by_name(argv
[1], &port_id
)) {
2923 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2927 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2929 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2930 "Remove it before detaching",
2931 argv
[1], netdev_get_name(&dev
->up
));
2935 rte_eth_dev_close(port_id
);
2937 ret
= rte_eth_dev_detach(port_id
, devname
);
2939 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2943 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2945 ovs_mutex_unlock(&dpdk_mutex
);
2946 unixctl_command_reply(conn
, response
);
2951 ovs_mutex_unlock(&dpdk_mutex
);
2952 unixctl_command_reply_error(conn
, response
);
2957 netdev_dpdk_get_mempool_info(struct unixctl_conn
*conn
,
2958 int argc
, const char *argv
[],
2959 void *aux OVS_UNUSED
)
2963 char *response
= NULL
;
2964 struct netdev
*netdev
= NULL
;
2967 netdev
= netdev_from_name(argv
[1]);
2968 if (!netdev
|| !is_dpdk_class(netdev
->netdev_class
)) {
2969 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2974 stream
= open_memstream(&response
, &size
);
2976 response
= xasprintf("Unable to open memstream: %s.",
2977 ovs_strerror(errno
));
2978 unixctl_command_reply_error(conn
, response
);
2983 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2985 ovs_mutex_lock(&dev
->mutex
);
2986 ovs_mutex_lock(&dpdk_mp_mutex
);
2988 rte_mempool_dump(stream
, dev
->dpdk_mp
->mp
);
2990 ovs_mutex_unlock(&dpdk_mp_mutex
);
2991 ovs_mutex_unlock(&dev
->mutex
);
2993 ovs_mutex_lock(&dpdk_mp_mutex
);
2994 rte_mempool_list_dump(stream
);
2995 ovs_mutex_unlock(&dpdk_mp_mutex
);
3000 unixctl_command_reply(conn
, response
);
3003 netdev_close(netdev
);
3007 * Set virtqueue flags so that we do not receive interrupts.
3010 set_irq_status(int vid
)
3014 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
3015 rte_vhost_enable_guest_notification(vid
, i
, 0);
3020 * Fixes mapping for vhost-user tx queues. Must be called after each
3021 * enabling/disabling of queues and n_txq modifications.
3024 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
3025 OVS_REQUIRES(dev
->mutex
)
3027 int *enabled_queues
, n_enabled
= 0;
3028 int i
, k
, total_txqs
= dev
->up
.n_txq
;
3030 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
3032 for (i
= 0; i
< total_txqs
; i
++) {
3033 /* Enabled queues always mapped to themselves. */
3034 if (dev
->tx_q
[i
].map
== i
) {
3035 enabled_queues
[n_enabled
++] = i
;
3039 if (n_enabled
== 0 && total_txqs
!= 0) {
3040 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
3045 for (i
= 0; i
< total_txqs
; i
++) {
3046 if (dev
->tx_q
[i
].map
!= i
) {
3047 dev
->tx_q
[i
].map
= enabled_queues
[k
];
3048 k
= (k
+ 1) % n_enabled
;
3052 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
3053 for (i
= 0; i
< total_txqs
; i
++) {
3054 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
3057 free(enabled_queues
);
3061 * A new virtio-net device is added to a vhost port.
3066 struct netdev_dpdk
*dev
;
3067 bool exists
= false;
3069 char ifname
[IF_NAME_SZ
];
3071 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3073 ovs_mutex_lock(&dpdk_mutex
);
3074 /* Add device to the vhost port with the same name as that passed down. */
3075 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
3076 ovs_mutex_lock(&dev
->mutex
);
3077 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3078 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
3080 /* Get NUMA information */
3081 newnode
= rte_vhost_get_numa_node(vid
);
3082 if (newnode
== -1) {
3084 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
3087 newnode
= dev
->socket_id
;
3090 if (dev
->requested_n_txq
!= qp_num
3091 || dev
->requested_n_rxq
!= qp_num
3092 || dev
->requested_socket_id
!= newnode
) {
3093 dev
->requested_socket_id
= newnode
;
3094 dev
->requested_n_rxq
= qp_num
;
3095 dev
->requested_n_txq
= qp_num
;
3096 netdev_request_reconfigure(&dev
->up
);
3098 /* Reconfiguration not required. */
3099 dev
->vhost_reconfigured
= true;
3102 ovsrcu_index_set(&dev
->vid
, vid
);
3105 /* Disable notifications. */
3106 set_irq_status(vid
);
3107 netdev_change_seq_changed(&dev
->up
);
3108 ovs_mutex_unlock(&dev
->mutex
);
3111 ovs_mutex_unlock(&dev
->mutex
);
3113 ovs_mutex_unlock(&dpdk_mutex
);
3116 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
3121 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
3127 /* Clears mapping for all available queues of vhost interface. */
3129 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
3130 OVS_REQUIRES(dev
->mutex
)
3134 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
3135 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
3140 * Remove a virtio-net device from the specific vhost port. Use dev->remove
3141 * flag to stop any more packets from being sent or received to/from a VM and
3142 * ensure all currently queued packets have been sent/received before removing
3146 destroy_device(int vid
)
3148 struct netdev_dpdk
*dev
;
3149 bool exists
= false;
3150 char ifname
[IF_NAME_SZ
];
3152 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3154 ovs_mutex_lock(&dpdk_mutex
);
3155 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3156 if (netdev_dpdk_get_vid(dev
) == vid
) {
3158 ovs_mutex_lock(&dev
->mutex
);
3159 dev
->vhost_reconfigured
= false;
3160 ovsrcu_index_set(&dev
->vid
, -1);
3161 netdev_dpdk_txq_map_clear(dev
);
3163 netdev_change_seq_changed(&dev
->up
);
3164 ovs_mutex_unlock(&dev
->mutex
);
3170 ovs_mutex_unlock(&dpdk_mutex
);
3174 * Wait for other threads to quiesce after setting the 'virtio_dev'
3175 * to NULL, before returning.
3177 ovsrcu_synchronize();
3179 * As call to ovsrcu_synchronize() will end the quiescent state,
3180 * put thread back into quiescent state before returning.
3182 ovsrcu_quiesce_start();
3183 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
3185 VLOG_INFO("vHost Device '%s' not found", ifname
);
3190 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
3192 struct netdev_dpdk
*dev
;
3193 bool exists
= false;
3194 int qid
= queue_id
/ VIRTIO_QNUM
;
3195 char ifname
[IF_NAME_SZ
];
3197 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3199 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
3203 ovs_mutex_lock(&dpdk_mutex
);
3204 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3205 ovs_mutex_lock(&dev
->mutex
);
3206 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3208 dev
->tx_q
[qid
].map
= qid
;
3210 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
3212 netdev_dpdk_remap_txqs(dev
);
3214 ovs_mutex_unlock(&dev
->mutex
);
3217 ovs_mutex_unlock(&dev
->mutex
);
3219 ovs_mutex_unlock(&dpdk_mutex
);
3222 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
3223 "changed to \'%s\'", queue_id
, qid
, ifname
,
3224 (enable
== 1) ? "enabled" : "disabled");
3226 VLOG_INFO("vHost Device '%s' not found", ifname
);
3234 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
3236 return ovsrcu_index_get(&dev
->vid
);
3239 struct ingress_policer
*
3240 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
3242 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
3246 netdev_dpdk_class_init(void)
3248 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3250 /* This function can be called for different classes. The initialization
3251 * needs to be done only once */
3252 if (ovsthread_once_start(&once
)) {
3253 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
3254 unixctl_command_register("netdev-dpdk/set-admin-state",
3255 "[netdev] up|down", 1, 2,
3256 netdev_dpdk_set_admin_state
, NULL
);
3258 unixctl_command_register("netdev-dpdk/detach",
3259 "pci address of device", 1, 1,
3260 netdev_dpdk_detach
, NULL
);
3262 unixctl_command_register("netdev-dpdk/get-mempool-info",
3264 netdev_dpdk_get_mempool_info
, NULL
);
3266 ovsthread_once_done(&once
);
3276 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
3277 dpdk_port_t
*eth_port_id
)
3279 struct dpdk_ring
*ring_pair
;
3283 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
3288 /* XXX: Add support for multiquque ring. */
3289 ring_name
= xasprintf("%s_tx", dev_name
);
3291 /* Create single producer tx ring, netdev does explicit locking. */
3292 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3295 if (ring_pair
->cring_tx
== NULL
) {
3296 rte_free(ring_pair
);
3300 ring_name
= xasprintf("%s_rx", dev_name
);
3302 /* Create single consumer rx ring, netdev does explicit locking. */
3303 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3306 if (ring_pair
->cring_rx
== NULL
) {
3307 rte_free(ring_pair
);
3311 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
3312 &ring_pair
->cring_tx
, 1, SOCKET0
);
3315 rte_free(ring_pair
);
3319 ring_pair
->user_port_id
= port_no
;
3320 ring_pair
->eth_port_id
= port_id
;
3321 *eth_port_id
= port_id
;
3323 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
3329 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
3330 OVS_REQUIRES(dpdk_mutex
)
3332 struct dpdk_ring
*ring_pair
;
3333 unsigned int port_no
;
3336 /* Names always start with "dpdkr" */
3337 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
3342 /* Look through our list to find the device */
3343 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
3344 if (ring_pair
->user_port_id
== port_no
) {
3345 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
3346 /* Really all that is needed */
3347 *eth_port_id
= ring_pair
->eth_port_id
;
3351 /* Need to create the device rings */
3352 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
3356 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
3357 struct dp_packet_batch
*batch
, bool concurrent_txq
)
3359 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3360 struct dp_packet
*packet
;
3362 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
3363 * the rss hash field is clear. This is because the same mbuf may be
3364 * modified by the consumer of the ring and return into the datapath
3365 * without recalculating the RSS hash. */
3366 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
3367 dp_packet_mbuf_rss_flag_reset(packet
);
3370 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
3375 netdev_dpdk_ring_construct(struct netdev
*netdev
)
3377 dpdk_port_t port_no
= 0;
3380 ovs_mutex_lock(&dpdk_mutex
);
3382 err
= dpdk_ring_open(netdev
->name
, &port_no
);
3387 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
3388 rte_eth_dev_socket_id(port_no
));
3390 ovs_mutex_unlock(&dpdk_mutex
);
3397 * Initialize QoS configuration operations.
3400 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
3403 rte_spinlock_init(&conf
->lock
);
3407 * Search existing QoS operations in qos_ops and compare each set of
3408 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
3411 static const struct dpdk_qos_ops
*
3412 qos_lookup_name(const char *name
)
3414 const struct dpdk_qos_ops
*const *opsp
;
3416 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3417 const struct dpdk_qos_ops
*ops
= *opsp
;
3418 if (!strcmp(name
, ops
->qos_name
)) {
3426 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3429 const struct dpdk_qos_ops
*const *opsp
;
3431 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3432 const struct dpdk_qos_ops
*ops
= *opsp
;
3433 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3434 sset_add(types
, ops
->qos_name
);
3441 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3442 const char **typep
, struct smap
*details
)
3444 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3445 struct qos_conf
*qos_conf
;
3448 ovs_mutex_lock(&dev
->mutex
);
3449 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3451 *typep
= qos_conf
->ops
->qos_name
;
3452 error
= (qos_conf
->ops
->qos_get
3453 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3455 /* No QoS configuration set, return an empty string */
3458 ovs_mutex_unlock(&dev
->mutex
);
3464 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3465 const struct smap
*details
)
3467 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3468 const struct dpdk_qos_ops
*new_ops
= NULL
;
3469 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3472 ovs_mutex_lock(&dev
->mutex
);
3474 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3476 new_ops
= qos_lookup_name(type
);
3478 if (!new_ops
|| !new_ops
->qos_construct
) {
3479 new_qos_conf
= NULL
;
3480 if (type
&& type
[0]) {
3483 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3484 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3485 new_qos_conf
= qos_conf
;
3487 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3491 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3492 type
, netdev
->name
, rte_strerror(error
));
3495 if (new_qos_conf
!= qos_conf
) {
3496 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3498 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3502 ovs_mutex_unlock(&dev
->mutex
);
3507 /* egress-policer details */
3509 struct egress_policer
{
3510 struct qos_conf qos_conf
;
3511 struct rte_meter_srtcm_params app_srtcm_params
;
3512 struct rte_meter_srtcm egress_meter
;
3516 egress_policer_details_to_param(const struct smap
*details
,
3517 struct rte_meter_srtcm_params
*params
)
3519 memset(params
, 0, sizeof *params
);
3520 params
->cir
= smap_get_ullong(details
, "cir", 0);
3521 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3526 egress_policer_qos_construct(const struct smap
*details
,
3527 struct qos_conf
**conf
)
3529 struct egress_policer
*policer
;
3532 policer
= xmalloc(sizeof *policer
);
3533 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3534 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3535 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3536 &policer
->app_srtcm_params
);
3538 *conf
= &policer
->qos_conf
;
3549 egress_policer_qos_destruct(struct qos_conf
*conf
)
3551 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3557 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3559 struct egress_policer
*policer
=
3560 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3562 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3563 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3569 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3570 const struct smap
*details
)
3572 struct egress_policer
*policer
=
3573 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3574 struct rte_meter_srtcm_params params
;
3576 egress_policer_details_to_param(details
, ¶ms
);
3578 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3582 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3586 struct egress_policer
*policer
=
3587 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3589 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3590 pkt_cnt
, may_steal
);
3595 static const struct dpdk_qos_ops egress_policer_ops
= {
3596 "egress-policer", /* qos_name */
3597 egress_policer_qos_construct
,
3598 egress_policer_qos_destruct
,
3599 egress_policer_qos_get
,
3600 egress_policer_qos_is_equal
,
3605 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3607 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3610 ovs_mutex_lock(&dev
->mutex
);
3612 if (netdev
->n_txq
== dev
->requested_n_txq
3613 && netdev
->n_rxq
== dev
->requested_n_rxq
3614 && dev
->mtu
== dev
->requested_mtu
3615 && dev
->rxq_size
== dev
->requested_rxq_size
3616 && dev
->txq_size
== dev
->requested_txq_size
3617 && dev
->socket_id
== dev
->requested_socket_id
) {
3618 /* Reconfiguration is unnecessary */
3623 rte_eth_dev_stop(dev
->port_id
);
3625 if (dev
->mtu
!= dev
->requested_mtu
3626 || dev
->socket_id
!= dev
->requested_socket_id
) {
3627 err
= netdev_dpdk_mempool_configure(dev
);
3633 netdev
->n_txq
= dev
->requested_n_txq
;
3634 netdev
->n_rxq
= dev
->requested_n_rxq
;
3636 dev
->rxq_size
= dev
->requested_rxq_size
;
3637 dev
->txq_size
= dev
->requested_txq_size
;
3639 rte_free(dev
->tx_q
);
3640 err
= dpdk_eth_dev_init(dev
);
3641 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3646 netdev_change_seq_changed(netdev
);
3649 ovs_mutex_unlock(&dev
->mutex
);
3654 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3655 OVS_REQUIRES(dev
->mutex
)
3657 dev
->up
.n_txq
= dev
->requested_n_txq
;
3658 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3661 /* Enable TX queue 0 by default if it wasn't disabled. */
3662 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3663 dev
->tx_q
[0].map
= 0;
3666 netdev_dpdk_remap_txqs(dev
);
3668 if (dev
->requested_socket_id
!= dev
->socket_id
3669 || dev
->requested_mtu
!= dev
->mtu
) {
3670 err
= netdev_dpdk_mempool_configure(dev
);
3674 netdev_change_seq_changed(&dev
->up
);
3678 if (netdev_dpdk_get_vid(dev
) >= 0) {
3679 if (dev
->vhost_reconfigured
== false) {
3680 dev
->vhost_reconfigured
= true;
3681 /* Carrier status may need updating. */
3682 netdev_change_seq_changed(&dev
->up
);
3690 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3692 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3695 ovs_mutex_lock(&dev
->mutex
);
3696 err
= dpdk_vhost_reconfigure_helper(dev
);
3697 ovs_mutex_unlock(&dev
->mutex
);
3703 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3705 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3707 uint64_t vhost_flags
= 0;
3710 ovs_mutex_lock(&dev
->mutex
);
3712 /* Configure vHost client mode if requested and if the following criteria
3714 * 1. Device hasn't been registered yet.
3715 * 2. A path has been specified.
3717 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3718 && strlen(dev
->vhost_id
)) {
3719 /* Register client-mode device. */
3720 vhost_flags
|= RTE_VHOST_USER_CLIENT
;
3722 /* Enable IOMMU support, if explicitly requested. */
3723 if (dpdk_vhost_iommu_enabled()) {
3724 vhost_flags
|= RTE_VHOST_USER_IOMMU_SUPPORT
;
3727 zc_enabled
= dev
->vhost_driver_flags
3728 & RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
3729 /* Enable zero copy flag, if requested */
3731 vhost_flags
|= RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
3734 err
= rte_vhost_driver_register(dev
->vhost_id
, vhost_flags
);
3736 VLOG_ERR("vhost-user device setup failure for device %s\n",
3740 /* Configuration successful */
3741 dev
->vhost_driver_flags
|= vhost_flags
;
3742 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3743 "using client socket '%s'",
3744 dev
->up
.name
, dev
->vhost_id
);
3746 VLOG_INFO("Zero copy enabled for vHost port %s", dev
->up
.name
);
3750 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3751 &virtio_net_device_ops
);
3753 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3754 "vhost user client port: %s\n", dev
->up
.name
);
3758 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3759 1ULL << VIRTIO_NET_F_HOST_TSO4
3760 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3761 | 1ULL << VIRTIO_NET_F_CSUM
);
3763 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3764 "client port: %s\n", dev
->up
.name
);
3768 err
= rte_vhost_driver_start(dev
->vhost_id
);
3770 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3771 "client port: %s\n", dev
->up
.name
);
3776 err
= dpdk_vhost_reconfigure_helper(dev
);
3779 ovs_mutex_unlock(&dev
->mutex
);
3784 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3785 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3786 GET_CARRIER, GET_STATS, \
3788 GET_FEATURES, GET_STATUS, \
3789 RECONFIGURE, RXQ_RECV) \
3792 true, /* is_pmd */ \
3794 NULL, /* netdev_dpdk_run */ \
3795 NULL, /* netdev_dpdk_wait */ \
3797 netdev_dpdk_alloc, \
3800 netdev_dpdk_dealloc, \
3801 netdev_dpdk_get_config, \
3803 NULL, /* get_tunnel_config */ \
3804 NULL, /* build header */ \
3805 NULL, /* push header */ \
3806 NULL, /* pop header */ \
3807 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3811 NULL, /* send_wait */ \
3813 netdev_dpdk_set_etheraddr, \
3814 netdev_dpdk_get_etheraddr, \
3815 netdev_dpdk_get_mtu, \
3816 netdev_dpdk_set_mtu, \
3817 netdev_dpdk_get_ifindex, \
3819 netdev_dpdk_get_carrier_resets, \
3820 netdev_dpdk_set_miimon, \
3824 NULL, /* set_advertisements */ \
3825 NULL, /* get_pt_mode */ \
3827 netdev_dpdk_set_policing, \
3828 netdev_dpdk_get_qos_types, \
3829 NULL, /* get_qos_capabilities */ \
3830 netdev_dpdk_get_qos, \
3831 netdev_dpdk_set_qos, \
3832 NULL, /* get_queue */ \
3833 NULL, /* set_queue */ \
3834 NULL, /* delete_queue */ \
3835 NULL, /* get_queue_stats */ \
3836 NULL, /* queue_dump_start */ \
3837 NULL, /* queue_dump_next */ \
3838 NULL, /* queue_dump_done */ \
3839 NULL, /* dump_queue_stats */ \
3841 NULL, /* set_in4 */ \
3842 NULL, /* get_addr_list */ \
3843 NULL, /* add_router */ \
3844 NULL, /* get_next_hop */ \
3846 NULL, /* arp_lookup */ \
3848 netdev_dpdk_update_flags, \
3851 netdev_dpdk_rxq_alloc, \
3852 netdev_dpdk_rxq_construct, \
3853 netdev_dpdk_rxq_destruct, \
3854 netdev_dpdk_rxq_dealloc, \
3856 NULL, /* rx_wait */ \
3857 NULL, /* rxq_drain */ \
3861 static const struct netdev_class dpdk_class
=
3864 netdev_dpdk_class_init
,
3865 netdev_dpdk_construct
,
3866 netdev_dpdk_destruct
,
3867 netdev_dpdk_set_config
,
3868 netdev_dpdk_set_tx_multiq
,
3869 netdev_dpdk_eth_send
,
3870 netdev_dpdk_get_carrier
,
3871 netdev_dpdk_get_stats
,
3872 netdev_dpdk_get_custom_stats
,
3873 netdev_dpdk_get_features
,
3874 netdev_dpdk_get_status
,
3875 netdev_dpdk_reconfigure
,
3876 netdev_dpdk_rxq_recv
);
3878 static const struct netdev_class dpdk_ring_class
=
3881 netdev_dpdk_class_init
,
3882 netdev_dpdk_ring_construct
,
3883 netdev_dpdk_destruct
,
3884 netdev_dpdk_ring_set_config
,
3885 netdev_dpdk_set_tx_multiq
,
3886 netdev_dpdk_ring_send
,
3887 netdev_dpdk_get_carrier
,
3888 netdev_dpdk_get_stats
,
3889 netdev_dpdk_get_custom_stats
,
3890 netdev_dpdk_get_features
,
3891 netdev_dpdk_get_status
,
3892 netdev_dpdk_reconfigure
,
3893 netdev_dpdk_rxq_recv
);
3895 static const struct netdev_class dpdk_vhost_class
=
3899 netdev_dpdk_vhost_construct
,
3900 netdev_dpdk_vhost_destruct
,
3903 netdev_dpdk_vhost_send
,
3904 netdev_dpdk_vhost_get_carrier
,
3905 netdev_dpdk_vhost_get_stats
,
3908 netdev_dpdk_vhost_user_get_status
,
3909 netdev_dpdk_vhost_reconfigure
,
3910 netdev_dpdk_vhost_rxq_recv
);
3911 static const struct netdev_class dpdk_vhost_client_class
=
3913 "dpdkvhostuserclient",
3915 netdev_dpdk_vhost_client_construct
,
3916 netdev_dpdk_vhost_destruct
,
3917 netdev_dpdk_vhost_client_set_config
,
3919 netdev_dpdk_vhost_send
,
3920 netdev_dpdk_vhost_get_carrier
,
3921 netdev_dpdk_vhost_get_stats
,
3924 netdev_dpdk_vhost_user_get_status
,
3925 netdev_dpdk_vhost_client_reconfigure
,
3926 netdev_dpdk_vhost_rxq_recv
);
3929 netdev_dpdk_register(void)
3931 netdev_register_provider(&dpdk_class
);
3932 netdev_register_provider(&dpdk_ring_class
);
3933 netdev_register_provider(&dpdk_vhost_class
);
3934 netdev_register_provider(&dpdk_vhost_client_class
);