2 * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "netdev-dpdk.h"
25 #include <linux/virtio_net.h>
26 #include <sys/socket.h>
29 #include <rte_bus_pci.h>
30 #include <rte_config.h>
31 #include <rte_cycles.h>
32 #include <rte_errno.h>
33 #include <rte_eth_ring.h>
34 #include <rte_ethdev.h>
35 #include <rte_malloc.h>
37 #include <rte_meter.h>
39 #include <rte_vhost.h>
40 #include <rte_version.h>
43 #include "dp-packet.h"
45 #include "dpif-netdev.h"
46 #include "fatal-signal.h"
47 #include "netdev-provider.h"
48 #include "netdev-vport.h"
50 #include "openvswitch/dynamic-string.h"
51 #include "openvswitch/list.h"
52 #include "openvswitch/ofp-print.h"
53 #include "openvswitch/vlog.h"
55 #include "ovs-thread.h"
58 #include "openvswitch/shash.h"
61 #include "unaligned.h"
65 enum {VIRTIO_RXQ
, VIRTIO_TXQ
, VIRTIO_QNUM
};
67 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
68 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
70 #define DPDK_PORT_WATCHDOG_INTERVAL 5
72 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
73 #define OVS_VPORT_DPDK "ovs_dpdk"
76 * need to reserve tons of extra space in the mbufs so we can align the
77 * DMA addresses to 4KB.
78 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
79 * performance for standard Ethernet MTU.
81 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
82 + (2 * VLAN_HEADER_LEN))
83 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
84 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
85 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
86 - ETHER_HDR_LEN - ETHER_CRC_LEN)
87 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
88 + sizeof(struct dp_packet) \
89 + RTE_PKTMBUF_HEADROOM), \
91 #define NETDEV_DPDK_MBUF_ALIGN 1024
92 #define NETDEV_DPDK_MAX_PKT_LEN 9728
94 /* Min number of packets in the mempool. OVS tries to allocate a mempool with
95 * roughly estimated number of mbufs: if this fails (because the system doesn't
96 * have enough hugepages) we keep halving the number until the allocation
97 * succeeds or we reach MIN_NB_MBUF */
98 #define MIN_NB_MBUF (4096 * 4)
99 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
102 * DPDK XSTATS Counter names definition
104 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
105 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
106 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
107 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
108 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
109 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
110 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
112 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
113 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
114 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
115 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
116 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
117 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
118 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
120 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
121 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
122 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
123 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
124 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
125 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
126 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
127 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
131 /* Default size of Physical NIC RXQ */
132 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
133 /* Default size of Physical NIC TXQ */
134 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
135 /* Maximum size of Physical NIC Queues */
136 #define NIC_PORT_MAX_Q_SIZE 4096
138 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
139 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
140 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
141 * yet mapped to another queue. */
143 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
145 /* DPDK library uses uint16_t for port_id. */
146 typedef uint16_t dpdk_port_t
;
147 #define DPDK_PORT_ID_FMT "%"PRIu16
149 #define VHOST_ENQ_RETRY_NUM 8
150 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
152 static const struct rte_eth_conf port_conf
= {
154 .mq_mode
= ETH_MQ_RX_RSS
,
156 .header_split
= 0, /* Header Split disabled */
157 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
158 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
159 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
165 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
169 .mq_mode
= ETH_MQ_TX_NONE
,
174 * These callbacks allow virtio-net devices to be added to vhost ports when
175 * configuration has been fully completed.
177 static int new_device(int vid
);
178 static void destroy_device(int vid
);
179 static int vring_state_changed(int vid
, uint16_t queue_id
, int enable
);
180 static const struct vhost_device_ops virtio_net_device_ops
=
182 .new_device
= new_device
,
183 .destroy_device
= destroy_device
,
184 .vring_state_changed
= vring_state_changed
,
185 .features_changed
= NULL
188 enum { DPDK_RING_SIZE
= 256 };
189 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
190 enum { DRAIN_TSC
= 200000ULL };
197 /* Quality of Service */
199 /* An instance of a QoS configuration. Always associated with a particular
202 * Each QoS implementation subclasses this with whatever additional data it
206 const struct dpdk_qos_ops
*ops
;
210 /* A particular implementation of dpdk QoS operations.
212 * The functions below return 0 if successful or a positive errno value on
213 * failure, except where otherwise noted. All of them must be provided, except
214 * where otherwise noted.
216 struct dpdk_qos_ops
{
218 /* Name of the QoS type */
219 const char *qos_name
;
221 /* Called to construct a qos_conf object. The implementation should make
222 * the appropriate calls to configure QoS according to 'details'.
224 * The contents of 'details' should be documented as valid for 'ovs_name'
225 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
226 * (which is built as ovs-vswitchd.conf.db(8)).
228 * This function must return 0 if and only if it sets '*conf' to an
229 * initialized 'struct qos_conf'.
231 * For all QoS implementations it should always be non-null.
233 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
235 /* Destroys the data structures allocated by the implementation as part of
238 * For all QoS implementations it should always be non-null.
240 void (*qos_destruct
)(struct qos_conf
*conf
);
242 /* Retrieves details of 'conf' configuration into 'details'.
244 * The contents of 'details' should be documented as valid for 'ovs_name'
245 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
246 * (which is built as ovs-vswitchd.conf.db(8)).
248 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
250 /* Returns true if 'conf' is already configured according to 'details'.
252 * The contents of 'details' should be documented as valid for 'ovs_name'
253 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
254 * (which is built as ovs-vswitchd.conf.db(8)).
256 * For all QoS implementations it should always be non-null.
258 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
259 const struct smap
*details
);
261 /* Modify an array of rte_mbufs. The modification is specific to
262 * each qos implementation.
264 * The function should take and array of mbufs and an int representing
265 * the current number of mbufs present in the array.
267 * After the function has performed a qos modification to the array of
268 * mbufs it returns an int representing the number of mbufs now present in
269 * the array. This value is can then be passed to the port send function
270 * along with the modified array for transmission.
272 * For all QoS implementations it should always be non-null.
274 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
275 int pkt_cnt
, bool may_steal
);
278 /* dpdk_qos_ops for each type of user space QoS implementation */
279 static const struct dpdk_qos_ops egress_policer_ops
;
282 * Array of dpdk_qos_ops, contains pointer to all supported QoS
285 static const struct dpdk_qos_ops
*const qos_confs
[] = {
290 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
292 /* Contains all 'struct dpdk_dev's. */
293 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
294 = OVS_LIST_INITIALIZER(&dpdk_list
);
296 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
297 = OVS_MUTEX_INITIALIZER
;
299 /* There should be one 'struct dpdk_tx_queue' created for
301 struct dpdk_tx_queue
{
302 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
303 * from concurrent access. It is used only
304 * if the queue is shared among different
305 * pmd threads (see 'concurrent_txq'). */
306 int map
; /* Mapping of configured vhost-user queues
307 * to enabled by guest. */
310 /* dpdk has no way to remove dpdk ring ethernet devices
311 so we have to keep them around once they've been created
314 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
315 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
318 /* For the client rings */
319 struct rte_ring
*cring_tx
;
320 struct rte_ring
*cring_rx
;
321 unsigned int user_port_id
; /* User given port no, parsed from port name */
322 dpdk_port_t eth_port_id
; /* ethernet device port id */
323 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
326 struct ingress_policer
{
327 struct rte_meter_srtcm_params app_srtcm_params
;
328 struct rte_meter_srtcm in_policer
;
329 rte_spinlock_t policer_lock
;
332 enum dpdk_hw_ol_features
{
333 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
337 * In order to avoid confusion in variables names, following naming convention
338 * should be used, if possible:
340 * 'struct netdev' : 'netdev'
341 * 'struct netdev_dpdk' : 'dev'
342 * 'struct netdev_rxq' : 'rxq'
343 * 'struct netdev_rxq_dpdk' : 'rx'
346 * struct netdev *netdev = netdev_from_name(name);
347 * struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
349 * Also, 'netdev' should be used instead of 'dev->up', where 'netdev' was
354 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline0
,
357 /* If true, device was attached by rte_eth_dev_attach(). */
359 struct eth_addr hwaddr
;
364 enum dpdk_dev_type type
;
365 enum netdev_flags flags
;
366 char *devargs
; /* Device arguments for dpdk ports */
367 struct dpdk_tx_queue
*tx_q
;
368 struct rte_eth_link link
;
370 /* 4 pad bytes here. */
373 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE
, cacheline1
,
374 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
375 struct rte_mempool
*mp
;
377 /* virtio identifier for vhost devices */
380 /* True if vHost device is 'up' and has been reconfigured at least once */
381 bool vhost_reconfigured
;
382 /* 3 pad bytes here. */
385 PADDED_MEMBERS(CACHE_LINE_SIZE
,
386 /* Identifier used to distinguish vhost devices from each other. */
387 char vhost_id
[PATH_MAX
];
390 PADDED_MEMBERS(CACHE_LINE_SIZE
,
393 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
395 /* QoS configuration and lock for the device */
396 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
398 /* Ingress Policer */
399 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
400 uint32_t policer_rate
;
401 uint32_t policer_burst
;
404 PADDED_MEMBERS(CACHE_LINE_SIZE
,
405 struct netdev_stats stats
;
407 rte_spinlock_t stats_lock
;
408 /* 44 pad bytes here. */
411 PADDED_MEMBERS(CACHE_LINE_SIZE
,
412 /* The following properties cannot be changed when a device is running,
413 * so we remember the request and update them next time
414 * netdev_dpdk*_reconfigure() is called */
418 int requested_rxq_size
;
419 int requested_txq_size
;
421 /* Number of rx/tx descriptors for physical devices */
425 /* Socket ID detected when vHost device is brought up */
426 int requested_socket_id
;
428 /* Denotes whether vHost port is client/server mode */
429 uint64_t vhost_driver_flags
;
431 /* DPDK-ETH Flow control */
432 struct rte_eth_fc_conf fc_conf
;
434 /* DPDK-ETH hardware offload features,
435 * from the enum set 'dpdk_hw_ol_features' */
436 uint32_t hw_ol_features
;
439 PADDED_MEMBERS(CACHE_LINE_SIZE
,
440 /* Names of all XSTATS counters */
441 struct rte_eth_xstat_name
*rte_xstats_names
;
442 int rte_xstats_names_size
;
443 int rte_xstats_ids_size
;
444 uint64_t *rte_xstats_ids
;
448 struct netdev_rxq_dpdk
{
449 struct netdev_rxq up
;
453 static void netdev_dpdk_destruct(struct netdev
*netdev
);
454 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
456 static void netdev_dpdk_clear_xstats(struct netdev_dpdk
*dev
);
458 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
460 struct ingress_policer
*
461 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
464 is_dpdk_class(const struct netdev_class
*class)
466 return class->destruct
== netdev_dpdk_destruct
467 || class->destruct
== netdev_dpdk_vhost_destruct
;
470 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
471 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
472 * value, insufficient buffers are allocated to accomodate the packet in its
473 * entirety. Furthermore, certain drivers need to ensure that there is also
474 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
475 * frames). If the RX buffer is too small, then the driver enables scatter RX
476 * behaviour, which reduces performance. To prevent this, use a buffer size
477 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
480 dpdk_buf_size(int mtu
)
482 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
483 NETDEV_DPDK_MBUF_ALIGN
);
486 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
488 * Unlike xmalloc(), this function can return NULL on failure. */
490 dpdk_rte_mzalloc(size_t sz
)
492 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
496 free_dpdk_buf(struct dp_packet
*p
)
498 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
500 rte_pktmbuf_free(pkt
);
504 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
505 void *opaque_arg OVS_UNUSED
,
507 unsigned i OVS_UNUSED
)
509 struct rte_mbuf
*pkt
= _p
;
511 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
514 /* Returns a valid pointer when either of the following is true:
515 * - a new mempool was just created;
516 * - a matching mempool already exists. */
517 static struct rte_mempool
*
518 dpdk_mp_create(struct netdev_dpdk
*dev
, int mtu
)
520 char mp_name
[RTE_MEMPOOL_NAMESIZE
];
521 const char *netdev_name
= netdev_get_name(&dev
->up
);
522 int socket_id
= dev
->requested_socket_id
;
524 uint32_t hash
= hash_string(netdev_name
, 0);
525 struct rte_mempool
*mp
= NULL
;
528 * XXX: rough estimation of number of mbufs required for this port:
529 * <packets required to fill the device rxqs>
530 * + <packets that could be stuck on other ports txqs>
531 * + <packets in the pmd threads>
532 * + <additional memory for corner cases>
534 n_mbufs
= dev
->requested_n_rxq
* dev
->requested_rxq_size
535 + dev
->requested_n_txq
* dev
->requested_txq_size
536 + MIN(RTE_MAX_LCORE
, dev
->requested_n_rxq
) * NETDEV_MAX_BURST
539 ovs_mutex_lock(&dpdk_mp_mutex
);
541 /* Full DPDK memory pool name must be unique and cannot be
542 * longer than RTE_MEMPOOL_NAMESIZE. */
543 int ret
= snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
,
544 "ovs%08x%02d%05d%07u",
545 hash
, socket_id
, mtu
, n_mbufs
);
546 if (ret
< 0 || ret
>= RTE_MEMPOOL_NAMESIZE
) {
547 VLOG_DBG("snprintf returned %d. "
548 "Failed to generate a mempool name for \"%s\". "
549 "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.",
550 ret
, netdev_name
, hash
, socket_id
, mtu
, n_mbufs
);
554 VLOG_DBG("Port %s: Requesting a mempool of %u mbufs "
555 "on socket %d for %d Rx and %d Tx queues.",
556 netdev_name
, n_mbufs
, socket_id
,
557 dev
->requested_n_rxq
, dev
->requested_n_txq
);
559 mp
= rte_pktmbuf_pool_create(mp_name
, n_mbufs
, MP_CACHE_SZ
,
560 sizeof (struct dp_packet
) - sizeof (struct rte_mbuf
),
561 MBUF_SIZE(mtu
) - sizeof(struct dp_packet
), socket_id
);
564 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
566 /* rte_pktmbuf_pool_create has done some initialization of the
567 * rte_mbuf part of each dp_packet. Some OvS specific fields
568 * of the packet still need to be initialized by
569 * ovs_rte_pktmbuf_init. */
570 rte_mempool_obj_iter(mp
, ovs_rte_pktmbuf_init
, NULL
);
571 } else if (rte_errno
== EEXIST
) {
572 /* A mempool with the same name already exists. We just
573 * retrieve its pointer to be returned to the caller. */
574 mp
= rte_mempool_lookup(mp_name
);
575 /* As the mempool create returned EEXIST we can expect the
576 * lookup has returned a valid pointer. If for some reason
577 * that's not the case we keep track of it. */
578 VLOG_DBG("A mempool with name \"%s\" already exists at %p.",
581 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
584 } while (!mp
&& rte_errno
== ENOMEM
&& (n_mbufs
/= 2) >= MIN_NB_MBUF
);
586 ovs_mutex_unlock(&dpdk_mp_mutex
);
590 /* Release an existing mempool. */
592 dpdk_mp_free(struct rte_mempool
*mp
)
598 ovs_mutex_lock(&dpdk_mp_mutex
);
599 VLOG_DBG("Releasing \"%s\" mempool", mp
->name
);
600 rte_mempool_free(mp
);
601 ovs_mutex_unlock(&dpdk_mp_mutex
);
604 /* Tries to allocate a new mempool - or re-use an existing one where
605 * appropriate - on requested_socket_id with a size determined by
606 * requested_mtu and requested Rx/Tx queues.
607 * On success - or when re-using an existing mempool - the new configuration
609 * On error, device will be left unchanged. */
611 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
612 OVS_REQUIRES(dev
->mutex
)
614 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
615 struct rte_mempool
*mp
;
618 mp
= dpdk_mp_create(dev
, FRAME_LEN_TO_MTU(buf_size
));
620 VLOG_ERR("Failed to create memory pool for netdev "
621 "%s, with MTU %d on socket %d: %s\n",
622 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
623 rte_strerror(rte_errno
));
626 /* If a new MTU was requested and its rounded value equals the one
627 * that is currently used, then the existing mempool is returned. */
629 /* A new mempool was created, release the previous one. */
630 dpdk_mp_free(dev
->mp
);
635 dev
->mtu
= dev
->requested_mtu
;
636 dev
->socket_id
= dev
->requested_socket_id
;
637 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
644 check_link_status(struct netdev_dpdk
*dev
)
646 struct rte_eth_link link
;
648 rte_eth_link_get_nowait(dev
->port_id
, &link
);
650 if (dev
->link
.link_status
!= link
.link_status
) {
651 netdev_change_seq_changed(&dev
->up
);
653 dev
->link_reset_cnt
++;
655 if (dev
->link
.link_status
) {
657 "Port "DPDK_PORT_ID_FMT
" Link Up - speed %u Mbps - %s",
658 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
659 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
)
660 ? "full-duplex" : "half-duplex");
662 VLOG_DBG_RL(&rl
, "Port "DPDK_PORT_ID_FMT
" Link Down",
669 dpdk_watchdog(void *dummy OVS_UNUSED
)
671 struct netdev_dpdk
*dev
;
673 pthread_detach(pthread_self());
676 ovs_mutex_lock(&dpdk_mutex
);
677 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
678 ovs_mutex_lock(&dev
->mutex
);
679 if (dev
->type
== DPDK_DEV_ETH
) {
680 check_link_status(dev
);
682 ovs_mutex_unlock(&dev
->mutex
);
684 ovs_mutex_unlock(&dpdk_mutex
);
685 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
692 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
696 struct rte_eth_conf conf
= port_conf
;
698 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
700 if (dev
->mtu
> ETHER_MTU
) {
701 conf
.rxmode
.enable_scatter
= 1;
704 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
705 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
706 /* A device may report more queues than it makes available (this has
707 * been observed for Intel xl710, which reserves some of them for
708 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
709 * available. When this happens we can retry the configuration
710 * and request less queues */
711 while (n_rxq
&& n_txq
) {
713 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
716 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
718 VLOG_WARN("Interface %s eth_dev setup error %s\n",
719 dev
->up
.name
, rte_strerror(-diag
));
723 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
725 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
726 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
730 for (i
= 0; i
< n_txq
; i
++) {
731 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
732 dev
->socket_id
, NULL
);
734 VLOG_INFO("Interface %s txq(%d) setup error: %s",
735 dev
->up
.name
, i
, rte_strerror(-diag
));
741 /* Retry with less tx queues */
746 for (i
= 0; i
< n_rxq
; i
++) {
747 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
748 dev
->socket_id
, NULL
, dev
->mp
);
750 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
751 dev
->up
.name
, i
, rte_strerror(-diag
));
757 /* Retry with less rx queues */
762 dev
->up
.n_rxq
= n_rxq
;
763 dev
->up
.n_txq
= n_txq
;
772 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
774 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
775 VLOG_WARN("Failed to enable flow control on device "DPDK_PORT_ID_FMT
,
781 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
782 OVS_REQUIRES(dev
->mutex
)
784 struct rte_pktmbuf_pool_private
*mbp_priv
;
785 struct rte_eth_dev_info info
;
786 struct ether_addr eth_addr
;
789 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
790 DEV_RX_OFFLOAD_TCP_CKSUM
|
791 DEV_RX_OFFLOAD_IPV4_CKSUM
;
793 rte_eth_dev_info_get(dev
->port_id
, &info
);
795 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
796 rx_chksm_offload_capa
) {
797 VLOG_WARN("Rx checksum offload is not supported on port "
798 DPDK_PORT_ID_FMT
, dev
->port_id
);
799 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
801 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
804 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
805 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
807 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
809 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
810 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
814 diag
= rte_eth_dev_start(dev
->port_id
);
816 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
817 rte_strerror(-diag
));
821 rte_eth_promiscuous_enable(dev
->port_id
);
822 rte_eth_allmulticast_enable(dev
->port_id
);
824 memset(ð_addr
, 0x0, sizeof(eth_addr
));
825 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
826 VLOG_INFO_RL(&rl
, "Port "DPDK_PORT_ID_FMT
": "ETH_ADDR_FMT
,
827 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
829 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
830 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
832 mbp_priv
= rte_mempool_get_priv(dev
->mp
);
833 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
835 /* Get the Flow control configuration for DPDK-ETH */
836 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
838 VLOG_DBG("cannot get flow control parameters on port "DPDK_PORT_ID_FMT
839 ", err=%d", dev
->port_id
, diag
);
845 static struct netdev_dpdk
*
846 netdev_dpdk_cast(const struct netdev
*netdev
)
848 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
851 static struct netdev
*
852 netdev_dpdk_alloc(void)
854 struct netdev_dpdk
*dev
;
856 dev
= dpdk_rte_mzalloc(sizeof *dev
);
864 static struct dpdk_tx_queue
*
865 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
867 struct dpdk_tx_queue
*txqs
;
870 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
872 for (i
= 0; i
< n_txqs
; i
++) {
873 /* Initialize map for vhost devices. */
874 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
875 rte_spinlock_init(&txqs
[i
].tx_lock
);
883 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
884 enum dpdk_dev_type type
, int socket_id
)
885 OVS_REQUIRES(dpdk_mutex
)
887 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
889 ovs_mutex_init(&dev
->mutex
);
891 rte_spinlock_init(&dev
->stats_lock
);
893 /* If the 'sid' is negative, it means that the kernel fails
894 * to obtain the pci numa info. In that situation, always
896 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
897 dev
->requested_socket_id
= dev
->socket_id
;
898 dev
->port_id
= port_no
;
901 dev
->requested_mtu
= ETHER_MTU
;
902 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
903 ovsrcu_index_init(&dev
->vid
, -1);
904 dev
->vhost_reconfigured
= false;
905 dev
->attached
= false;
907 ovsrcu_init(&dev
->qos_conf
, NULL
);
909 ovsrcu_init(&dev
->ingress_policer
, NULL
);
910 dev
->policer_rate
= 0;
911 dev
->policer_burst
= 0;
915 dev
->requested_n_rxq
= NR_QUEUE
;
916 dev
->requested_n_txq
= NR_QUEUE
;
917 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
918 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
920 /* Initialize the flow control to NULL */
921 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
923 /* Initilize the hardware offload flags to 0 */
924 dev
->hw_ol_features
= 0;
926 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
928 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
930 netdev_request_reconfigure(netdev
);
932 dev
->rte_xstats_names
= NULL
;
933 dev
->rte_xstats_names_size
= 0;
935 dev
->rte_xstats_ids
= NULL
;
936 dev
->rte_xstats_ids_size
= 0;
941 /* dev_name must be the prefix followed by a positive decimal number.
942 * (no leading + or - signs are allowed) */
944 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
945 unsigned int *port_no
)
949 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
953 cport
= dev_name
+ strlen(prefix
);
955 if (str_to_uint(cport
, 10, port_no
)) {
963 vhost_common_construct(struct netdev
*netdev
)
964 OVS_REQUIRES(dpdk_mutex
)
966 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
967 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
969 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
974 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
975 DPDK_DEV_VHOST
, socket_id
);
979 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
981 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
982 const char *name
= netdev
->name
;
985 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
986 * the file system. '/' or '\' would traverse directories, so they're not
987 * acceptable in 'name'. */
988 if (strchr(name
, '/') || strchr(name
, '\\')) {
989 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
990 "A valid name must not include '/' or '\\'",
995 ovs_mutex_lock(&dpdk_mutex
);
996 /* Take the name of the vhost-user port and append it to the location where
997 * the socket is to be created, then register the socket.
999 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
1000 dpdk_get_vhost_sock_dir(), name
);
1002 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
1003 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
1005 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
1009 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
1010 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1011 dev
->vhost_id
, name
);
1014 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
1015 &virtio_net_device_ops
);
1017 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1018 "port: %s\n", name
);
1022 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1023 1ULL << VIRTIO_NET_F_HOST_TSO4
1024 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1025 | 1ULL << VIRTIO_NET_F_CSUM
);
1027 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1028 "port: %s\n", name
);
1032 err
= rte_vhost_driver_start(dev
->vhost_id
);
1034 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1035 "port: %s\n", name
);
1039 err
= vhost_common_construct(netdev
);
1041 VLOG_ERR("vhost_common_construct failed for vhost user "
1042 "port: %s\n", name
);
1046 ovs_mutex_unlock(&dpdk_mutex
);
1047 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1048 "please migrate to dpdkvhostuserclient ports.");
1053 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1057 ovs_mutex_lock(&dpdk_mutex
);
1058 err
= vhost_common_construct(netdev
);
1060 VLOG_ERR("vhost_common_construct failed for vhost user client"
1061 "port: %s\n", netdev
->name
);
1063 ovs_mutex_unlock(&dpdk_mutex
);
1068 netdev_dpdk_construct(struct netdev
*netdev
)
1072 ovs_mutex_lock(&dpdk_mutex
);
1073 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1074 DPDK_DEV_ETH
, SOCKET0
);
1075 ovs_mutex_unlock(&dpdk_mutex
);
1080 common_destruct(struct netdev_dpdk
*dev
)
1081 OVS_REQUIRES(dpdk_mutex
)
1082 OVS_EXCLUDED(dev
->mutex
)
1084 rte_free(dev
->tx_q
);
1085 dpdk_mp_free(dev
->mp
);
1087 ovs_list_remove(&dev
->list_node
);
1088 free(ovsrcu_get_protected(struct ingress_policer
*,
1089 &dev
->ingress_policer
));
1090 ovs_mutex_destroy(&dev
->mutex
);
1094 netdev_dpdk_destruct(struct netdev
*netdev
)
1096 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1097 char devname
[RTE_ETH_NAME_MAX_LEN
];
1099 ovs_mutex_lock(&dpdk_mutex
);
1101 rte_eth_dev_stop(dev
->port_id
);
1103 if (dev
->attached
) {
1104 rte_eth_dev_close(dev
->port_id
);
1105 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1106 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1108 VLOG_INFO("Device '%s' has been detached", devname
);
1112 netdev_dpdk_clear_xstats(dev
);
1114 common_destruct(dev
);
1116 ovs_mutex_unlock(&dpdk_mutex
);
1119 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1120 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1121 * deadlock, none of the mutexes must be held while calling this function. */
1123 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1125 OVS_EXCLUDED(dpdk_mutex
)
1126 OVS_EXCLUDED(dev
->mutex
)
1128 return rte_vhost_driver_unregister(vhost_id
);
1132 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1134 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1137 ovs_mutex_lock(&dpdk_mutex
);
1139 /* Guest becomes an orphan if still attached. */
1140 if (netdev_dpdk_get_vid(dev
) >= 0
1141 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1142 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1144 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1145 "socket '%s' must be restarted.", dev
->vhost_id
);
1148 vhost_id
= xstrdup(dev
->vhost_id
);
1150 common_destruct(dev
);
1152 ovs_mutex_unlock(&dpdk_mutex
);
1158 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1159 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1160 netdev
->name
, vhost_id
);
1161 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1162 /* OVS server mode - remove this socket from list for deletion */
1163 fatal_signal_remove_file_to_unlink(vhost_id
);
1170 netdev_dpdk_dealloc(struct netdev
*netdev
)
1172 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1178 netdev_dpdk_clear_xstats(struct netdev_dpdk
*dev
)
1180 /* If statistics are already allocated, we have to
1181 * reconfigure, as port_id could have been changed. */
1182 if (dev
->rte_xstats_names
) {
1183 free(dev
->rte_xstats_names
);
1184 dev
->rte_xstats_names
= NULL
;
1185 dev
->rte_xstats_names_size
= 0;
1187 if (dev
->rte_xstats_ids
) {
1188 free(dev
->rte_xstats_ids
);
1189 dev
->rte_xstats_ids
= NULL
;
1190 dev
->rte_xstats_ids_size
= 0;
1195 netdev_dpdk_get_xstat_name(struct netdev_dpdk
*dev
, uint64_t id
)
1197 if (id
>= dev
->rte_xstats_names_size
) {
1200 return dev
->rte_xstats_names
[id
].name
;
1204 netdev_dpdk_configure_xstats(struct netdev_dpdk
*dev
)
1205 OVS_REQUIRES(dev
->mutex
)
1209 struct rte_eth_xstat
*rte_xstats
;
1214 /* Retrieving all XSTATS names. If something will go wrong
1215 * or amount of counters will be equal 0, rte_xstats_names
1216 * buffer will be marked as NULL, and any further xstats
1217 * query won't be performed (e.g. during netdev_dpdk_get_stats
1223 if (dev
->rte_xstats_names
== NULL
|| dev
->rte_xstats_ids
== NULL
) {
1224 dev
->rte_xstats_names_size
=
1225 rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
1227 if (dev
->rte_xstats_names_size
< 0) {
1228 VLOG_WARN("Cannot get XSTATS for port: "DPDK_PORT_ID_FMT
,
1230 dev
->rte_xstats_names_size
= 0;
1232 /* Reserve memory for xstats names and values */
1233 dev
->rte_xstats_names
= xcalloc(dev
->rte_xstats_names_size
,
1234 sizeof *dev
->rte_xstats_names
);
1236 if (dev
->rte_xstats_names
) {
1237 /* Retreive xstats names */
1239 rte_eth_xstats_get_names(dev
->port_id
,
1240 dev
->rte_xstats_names
,
1241 dev
->rte_xstats_names_size
);
1243 if (rte_xstats_len
< 0) {
1244 VLOG_WARN("Cannot get XSTATS names for port: "
1245 DPDK_PORT_ID_FMT
, dev
->port_id
);
1247 } else if (rte_xstats_len
!= dev
->rte_xstats_names_size
) {
1248 VLOG_WARN("XSTATS size doesn't match for port: "
1249 DPDK_PORT_ID_FMT
, dev
->port_id
);
1253 dev
->rte_xstats_ids
= xcalloc(dev
->rte_xstats_names_size
,
1256 /* We have to calculate number of counters */
1257 rte_xstats
= xmalloc(rte_xstats_len
* sizeof *rte_xstats
);
1258 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
1260 /* Retreive xstats values */
1261 if (rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
1262 rte_xstats_len
) > 0) {
1263 dev
->rte_xstats_ids_size
= 0;
1265 for (uint32_t i
= 0; i
< rte_xstats_len
; i
++) {
1266 id
= rte_xstats
[i
].id
;
1267 name
= netdev_dpdk_get_xstat_name(dev
, id
);
1268 /* We need to filter out everything except
1269 * dropped, error and management counters */
1270 if (string_ends_with(name
, "_errors") ||
1271 strstr(name
, "_management_") ||
1272 string_ends_with(name
, "_dropped")) {
1274 dev
->rte_xstats_ids
[xstats_no
] = id
;
1278 dev
->rte_xstats_ids_size
= xstats_no
;
1281 VLOG_WARN("Can't get XSTATS IDs for port: "
1282 DPDK_PORT_ID_FMT
, dev
->port_id
);
1289 /* Already configured */
1295 netdev_dpdk_clear_xstats(dev
);
1301 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1303 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1305 ovs_mutex_lock(&dev
->mutex
);
1307 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1308 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1309 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1310 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1311 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1313 if (dev
->type
== DPDK_DEV_ETH
) {
1314 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1315 dev
->requested_rxq_size
);
1316 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1318 smap_add_format(args
, "requested_txq_descriptors", "%d",
1319 dev
->requested_txq_size
);
1320 smap_add_format(args
, "configured_txq_descriptors", "%d",
1322 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1323 smap_add(args
, "rx_csum_offload", "true");
1325 smap_add(args
, "rx_csum_offload", "false");
1328 ovs_mutex_unlock(&dev
->mutex
);
1333 static struct netdev_dpdk
*
1334 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1335 OVS_REQUIRES(dpdk_mutex
)
1337 struct netdev_dpdk
*dev
;
1339 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1340 if (dev
->port_id
== port_id
) {
1349 netdev_dpdk_get_port_by_mac(const char *mac_str
)
1351 dpdk_port_t port_id
;
1352 struct eth_addr mac
, port_mac
;
1354 if (!eth_addr_from_string(mac_str
, &mac
)) {
1355 VLOG_ERR("invalid mac: %s", mac_str
);
1356 return DPDK_ETH_PORT_ID_INVALID
;
1359 RTE_ETH_FOREACH_DEV (port_id
) {
1360 struct ether_addr ea
;
1362 rte_eth_macaddr_get(port_id
, &ea
);
1363 memcpy(port_mac
.ea
, ea
.addr_bytes
, ETH_ADDR_LEN
);
1364 if (eth_addr_equals(mac
, port_mac
)) {
1369 return DPDK_ETH_PORT_ID_INVALID
;
1373 * Normally, a PCI id is enough for identifying a specific DPDK port.
1374 * However, for some NICs having multiple ports sharing the same PCI
1375 * id, using PCI id won't work then.
1377 * To fix that, here one more method is introduced: "class=eth,mac=$MAC".
1379 * Note that the compatibility is fully kept: user can still use the
1380 * PCI id for adding ports (when it's enough for them).
1383 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1384 const char *devargs
, char **errp
)
1387 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1389 if (strncmp(devargs
, "class=eth,mac=", 14) == 0) {
1390 new_port_id
= netdev_dpdk_get_port_by_mac(&devargs
[14]);
1392 name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1393 if (rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1394 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1395 /* Device not found in DPDK, attempt to attach it */
1396 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1397 /* Attach successful */
1398 dev
->attached
= true;
1399 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1401 /* Attach unsuccessful */
1402 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1408 if (new_port_id
== DPDK_ETH_PORT_ID_INVALID
) {
1409 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK", devargs
);
1416 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1417 OVS_REQUIRES(dev
->mutex
)
1421 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1422 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1423 dev
->requested_n_rxq
= new_n_rxq
;
1424 netdev_request_reconfigure(&dev
->up
);
1429 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1430 const char *flag
, int default_size
, int *new_size
)
1432 int queue_size
= smap_get_int(args
, flag
, default_size
);
1434 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1435 || !is_pow2(queue_size
)) {
1436 queue_size
= default_size
;
1439 if (queue_size
!= *new_size
) {
1440 *new_size
= queue_size
;
1441 netdev_request_reconfigure(netdev
);
1446 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1449 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1450 bool rx_fc_en
, tx_fc_en
, autoneg
;
1451 enum rte_eth_fc_mode fc_mode
;
1452 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1453 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1454 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1456 const char *new_devargs
;
1459 ovs_mutex_lock(&dpdk_mutex
);
1460 ovs_mutex_lock(&dev
->mutex
);
1462 dpdk_set_rxq_config(dev
, args
);
1464 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1465 NIC_PORT_DEFAULT_RXQ_SIZE
,
1466 &dev
->requested_rxq_size
);
1467 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1468 NIC_PORT_DEFAULT_TXQ_SIZE
,
1469 &dev
->requested_txq_size
);
1471 new_devargs
= smap_get(args
, "dpdk-devargs");
1473 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1474 /* The user requested a new device. If we return error, the caller
1475 * will delete this netdev and try to recreate it. */
1480 /* dpdk-devargs is required for device configuration */
1481 if (new_devargs
&& new_devargs
[0]) {
1482 /* Don't process dpdk-devargs if value is unchanged and port id
1484 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1485 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1486 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1489 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1491 } else if (new_port_id
== dev
->port_id
) {
1492 /* Already configured, do not reconfigure again */
1495 struct netdev_dpdk
*dup_dev
;
1497 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1499 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1500 "which is already in use by '%s'",
1501 netdev_get_name(netdev
), new_devargs
,
1502 netdev_get_name(&dup_dev
->up
));
1505 int sid
= rte_eth_dev_socket_id(new_port_id
);
1507 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1508 dev
->devargs
= xstrdup(new_devargs
);
1509 dev
->port_id
= new_port_id
;
1510 netdev_request_reconfigure(&dev
->up
);
1511 netdev_dpdk_clear_xstats(dev
);
1517 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1518 "The old 'dpdk<port_id>' names are not supported",
1519 netdev_get_name(netdev
));
1527 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1528 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1529 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1531 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1532 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1533 dev
->fc_conf
.mode
= fc_mode
;
1534 dev
->fc_conf
.autoneg
= autoneg
;
1535 dpdk_eth_flow_ctrl_setup(dev
);
1539 ovs_mutex_unlock(&dev
->mutex
);
1540 ovs_mutex_unlock(&dpdk_mutex
);
1546 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1547 char **errp OVS_UNUSED
)
1549 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1551 ovs_mutex_lock(&dev
->mutex
);
1552 dpdk_set_rxq_config(dev
, args
);
1553 ovs_mutex_unlock(&dev
->mutex
);
1559 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1560 const struct smap
*args
,
1561 char **errp OVS_UNUSED
)
1563 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1566 ovs_mutex_lock(&dev
->mutex
);
1567 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1568 path
= smap_get(args
, "vhost-server-path");
1569 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1570 strcpy(dev
->vhost_id
, path
);
1571 /* check zero copy configuration */
1572 if (smap_get_bool(args
, "dq-zero-copy", false)) {
1573 dev
->vhost_driver_flags
|= RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
1575 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
1577 netdev_request_reconfigure(netdev
);
1580 ovs_mutex_unlock(&dev
->mutex
);
1586 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1588 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1590 return dev
->socket_id
;
1593 /* Sets the number of tx queues for the dpdk interface. */
1595 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1597 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1599 ovs_mutex_lock(&dev
->mutex
);
1601 if (dev
->requested_n_txq
== n_txq
) {
1605 dev
->requested_n_txq
= n_txq
;
1606 netdev_request_reconfigure(netdev
);
1609 ovs_mutex_unlock(&dev
->mutex
);
1613 static struct netdev_rxq
*
1614 netdev_dpdk_rxq_alloc(void)
1616 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1625 static struct netdev_rxq_dpdk
*
1626 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1628 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1632 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1634 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1635 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1637 ovs_mutex_lock(&dev
->mutex
);
1638 rx
->port_id
= dev
->port_id
;
1639 ovs_mutex_unlock(&dev
->mutex
);
1645 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1650 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1652 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1657 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1658 * 'pkts', even in case of failure.
1660 * Returns the number of packets that weren't transmitted. */
1662 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1663 struct rte_mbuf
**pkts
, int cnt
)
1667 while (nb_tx
!= cnt
) {
1670 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1678 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1679 /* Free buffers, which we couldn't transmit, one at a time (each
1680 * packet could come from a different mempool) */
1683 for (i
= nb_tx
; i
< cnt
; i
++) {
1684 rte_pktmbuf_free(pkts
[i
]);
1692 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1693 struct rte_mbuf
*pkt
, uint64_t time
)
1695 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1697 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1702 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1703 struct rte_mbuf
**pkts
, int pkt_cnt
,
1708 struct rte_mbuf
*pkt
= NULL
;
1709 uint64_t current_time
= rte_rdtsc();
1711 for (i
= 0; i
< pkt_cnt
; i
++) {
1713 /* Handle current packet */
1714 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1721 rte_pktmbuf_free(pkt
);
1730 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1731 int pkt_cnt
, bool may_steal
)
1735 rte_spinlock_lock(&policer
->policer_lock
);
1736 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1737 pkt_cnt
, may_steal
);
1738 rte_spinlock_unlock(&policer
->policer_lock
);
1744 is_vhost_running(struct netdev_dpdk
*dev
)
1746 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1750 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1751 unsigned int packet_size
)
1753 /* Hard-coded search for the size bucket. */
1754 if (packet_size
< 256) {
1755 if (packet_size
>= 128) {
1756 stats
->rx_128_to_255_packets
++;
1757 } else if (packet_size
<= 64) {
1758 stats
->rx_1_to_64_packets
++;
1760 stats
->rx_65_to_127_packets
++;
1763 if (packet_size
>= 1523) {
1764 stats
->rx_1523_to_max_packets
++;
1765 } else if (packet_size
>= 1024) {
1766 stats
->rx_1024_to_1522_packets
++;
1767 } else if (packet_size
< 512) {
1768 stats
->rx_256_to_511_packets
++;
1770 stats
->rx_512_to_1023_packets
++;
1776 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1777 struct dp_packet
**packets
, int count
,
1781 unsigned int packet_size
;
1782 struct dp_packet
*packet
;
1784 stats
->rx_packets
+= count
;
1785 stats
->rx_dropped
+= dropped
;
1786 for (i
= 0; i
< count
; i
++) {
1787 packet
= packets
[i
];
1788 packet_size
= dp_packet_size(packet
);
1790 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1791 /* This only protects the following multicast counting from
1792 * too short packets, but it does not stop the packet from
1793 * further processing. */
1795 stats
->rx_length_errors
++;
1799 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1801 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1802 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1806 stats
->rx_bytes
+= packet_size
;
1811 * The receive path for the vhost port is the TX path out from guest.
1814 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1815 struct dp_packet_batch
*batch
)
1817 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1818 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1820 uint16_t dropped
= 0;
1821 int qid
= rxq
->queue_id
;
1822 int vid
= netdev_dpdk_get_vid(dev
);
1824 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
1825 || !(dev
->flags
& NETDEV_UP
))) {
1829 nb_rx
= rte_vhost_dequeue_burst(vid
, qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1831 (struct rte_mbuf
**) batch
->packets
,
1839 nb_rx
= ingress_policer_run(policer
,
1840 (struct rte_mbuf
**) batch
->packets
,
1845 rte_spinlock_lock(&dev
->stats_lock
);
1846 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1848 rte_spinlock_unlock(&dev
->stats_lock
);
1850 batch
->count
= nb_rx
;
1851 dp_packet_batch_init_packet_fields(batch
);
1857 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1859 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1860 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1861 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1865 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1869 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1870 (struct rte_mbuf
**) batch
->packets
,
1878 nb_rx
= ingress_policer_run(policer
,
1879 (struct rte_mbuf
**) batch
->packets
,
1884 /* Update stats to reflect dropped packets */
1885 if (OVS_UNLIKELY(dropped
)) {
1886 rte_spinlock_lock(&dev
->stats_lock
);
1887 dev
->stats
.rx_dropped
+= dropped
;
1888 rte_spinlock_unlock(&dev
->stats_lock
);
1891 batch
->count
= nb_rx
;
1892 dp_packet_batch_init_packet_fields(batch
);
1898 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1899 int cnt
, bool may_steal
)
1901 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1904 rte_spinlock_lock(&qos_conf
->lock
);
1905 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1906 rte_spinlock_unlock(&qos_conf
->lock
);
1913 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1918 struct rte_mbuf
*pkt
;
1920 for (i
= 0; i
< pkt_cnt
; i
++) {
1922 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1923 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1924 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1925 rte_pktmbuf_free(pkt
);
1929 if (OVS_UNLIKELY(i
!= cnt
)) {
1939 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1940 struct dp_packet
**packets
,
1945 int sent
= attempted
- dropped
;
1947 stats
->tx_packets
+= sent
;
1948 stats
->tx_dropped
+= dropped
;
1950 for (i
= 0; i
< sent
; i
++) {
1951 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1956 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1957 struct dp_packet
**pkts
, int cnt
)
1959 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1960 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1961 unsigned int total_pkts
= cnt
;
1962 unsigned int dropped
= 0;
1964 int vid
= netdev_dpdk_get_vid(dev
);
1966 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1968 if (OVS_UNLIKELY(vid
< 0 || !dev
->vhost_reconfigured
|| qid
< 0
1969 || !(dev
->flags
& NETDEV_UP
))) {
1970 rte_spinlock_lock(&dev
->stats_lock
);
1971 dev
->stats
.tx_dropped
+= cnt
;
1972 rte_spinlock_unlock(&dev
->stats_lock
);
1976 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1978 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1979 /* Check has QoS has been configured for the netdev */
1980 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
1981 dropped
= total_pkts
- cnt
;
1984 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1985 unsigned int tx_pkts
;
1987 tx_pkts
= rte_vhost_enqueue_burst(vid
, vhost_qid
, cur_pkts
, cnt
);
1988 if (OVS_LIKELY(tx_pkts
)) {
1989 /* Packets have been sent.*/
1991 /* Prepare for possible retry.*/
1992 cur_pkts
= &cur_pkts
[tx_pkts
];
1994 /* No packets sent - do not retry.*/
1997 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1999 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2001 rte_spinlock_lock(&dev
->stats_lock
);
2002 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
2004 rte_spinlock_unlock(&dev
->stats_lock
);
2007 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
2008 dp_packet_delete(pkts
[i
]);
2012 /* Tx function. Transmit packets indefinitely */
2014 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
2015 OVS_NO_THREAD_SAFETY_ANALYSIS
2017 const size_t batch_cnt
= dp_packet_batch_size(batch
);
2018 #if !defined(__CHECKER__) && !defined(_WIN32)
2019 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
2021 /* Sparse or MSVC doesn't like variable length array. */
2022 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
2024 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2025 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
2026 uint32_t cnt
= batch_cnt
;
2027 uint32_t dropped
= 0;
2029 if (dev
->type
!= DPDK_DEV_VHOST
) {
2030 /* Check if QoS has been configured for this netdev. */
2031 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
2033 dropped
+= batch_cnt
- cnt
;
2038 for (uint32_t i
= 0; i
< cnt
; i
++) {
2039 struct dp_packet
*packet
= batch
->packets
[i
];
2040 uint32_t size
= dp_packet_size(packet
);
2042 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
2043 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
2044 size
, dev
->max_packet_len
);
2050 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->mp
);
2051 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
2056 /* We have to do a copy for now */
2057 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
2058 dp_packet_data(packet
), size
);
2059 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
2064 if (OVS_LIKELY(txcnt
)) {
2065 if (dev
->type
== DPDK_DEV_VHOST
) {
2066 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
2069 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
2073 if (OVS_UNLIKELY(dropped
)) {
2074 rte_spinlock_lock(&dev
->stats_lock
);
2075 dev
->stats
.tx_dropped
+= dropped
;
2076 rte_spinlock_unlock(&dev
->stats_lock
);
2081 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
2082 struct dp_packet_batch
*batch
,
2083 bool concurrent_txq OVS_UNUSED
)
2086 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2087 dpdk_do_tx_copy(netdev
, qid
, batch
);
2088 dp_packet_delete_batch(batch
, true);
2090 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
2096 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
2097 struct dp_packet_batch
*batch
,
2098 bool concurrent_txq
)
2100 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
2101 dp_packet_delete_batch(batch
, true);
2105 if (OVS_UNLIKELY(concurrent_txq
)) {
2106 qid
= qid
% dev
->up
.n_txq
;
2107 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
2110 if (OVS_UNLIKELY(batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
2111 struct netdev
*netdev
= &dev
->up
;
2113 dpdk_do_tx_copy(netdev
, qid
, batch
);
2114 dp_packet_delete_batch(batch
, true);
2116 int tx_cnt
, dropped
;
2117 int batch_cnt
= dp_packet_batch_size(batch
);
2118 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
2120 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
2121 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
2122 dropped
= batch_cnt
- tx_cnt
;
2124 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
2126 if (OVS_UNLIKELY(dropped
)) {
2127 rte_spinlock_lock(&dev
->stats_lock
);
2128 dev
->stats
.tx_dropped
+= dropped
;
2129 rte_spinlock_unlock(&dev
->stats_lock
);
2133 if (OVS_UNLIKELY(concurrent_txq
)) {
2134 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
2139 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
2140 struct dp_packet_batch
*batch
, bool concurrent_txq
)
2142 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2144 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
2149 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
2151 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2153 ovs_mutex_lock(&dev
->mutex
);
2154 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
2156 netdev_change_seq_changed(netdev
);
2158 ovs_mutex_unlock(&dev
->mutex
);
2164 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
2166 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2168 ovs_mutex_lock(&dev
->mutex
);
2170 ovs_mutex_unlock(&dev
->mutex
);
2176 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
2178 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2180 ovs_mutex_lock(&dev
->mutex
);
2182 ovs_mutex_unlock(&dev
->mutex
);
2188 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2190 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2192 /* XXX: Ensure that the overall frame length of the requested MTU does not
2193 * surpass the NETDEV_DPDK_MAX_PKT_LEN. DPDK device drivers differ in how
2194 * the L2 frame length is calculated for a given MTU when
2195 * rte_eth_dev_set_mtu(mtu) is called e.g. i40e driver includes 2 x vlan
2196 * headers, the em driver includes 1 x vlan header, the ixgbe driver does
2197 * not include vlan headers. As such we should use
2198 * MTU_TO_MAX_FRAME_LEN(mtu) which includes an additional 2 x vlan headers
2199 * (8 bytes) for comparison. This avoids a failure later with
2200 * rte_eth_dev_set_mtu(). This approach should be used until DPDK provides
2201 * a method to retrieve the upper bound MTU for a given device.
2203 if (MTU_TO_MAX_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2204 || mtu
< ETHER_MIN_MTU
) {
2205 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2209 ovs_mutex_lock(&dev
->mutex
);
2210 if (dev
->requested_mtu
!= mtu
) {
2211 dev
->requested_mtu
= mtu
;
2212 netdev_request_reconfigure(netdev
);
2214 ovs_mutex_unlock(&dev
->mutex
);
2220 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2223 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2224 struct netdev_stats
*stats
)
2226 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2228 ovs_mutex_lock(&dev
->mutex
);
2230 rte_spinlock_lock(&dev
->stats_lock
);
2231 /* Supported Stats */
2232 stats
->rx_packets
= dev
->stats
.rx_packets
;
2233 stats
->tx_packets
= dev
->stats
.tx_packets
;
2234 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2235 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2236 stats
->multicast
= dev
->stats
.multicast
;
2237 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2238 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2239 stats
->rx_errors
= dev
->stats
.rx_errors
;
2240 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2242 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2243 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2244 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2245 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2246 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2247 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2248 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2250 rte_spinlock_unlock(&dev
->stats_lock
);
2252 ovs_mutex_unlock(&dev
->mutex
);
2258 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2259 const struct rte_eth_xstat
*xstats
,
2260 const struct rte_eth_xstat_name
*names
,
2261 const unsigned int size
)
2263 for (unsigned int i
= 0; i
< size
; i
++) {
2264 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2265 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2266 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2267 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2268 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2269 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2270 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2271 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2272 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2273 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2274 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2275 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2276 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2277 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2278 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2279 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2280 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2281 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2282 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2283 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2284 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2285 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2286 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2287 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2288 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2289 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2290 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2291 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2292 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2293 stats
->multicast
= xstats
[i
].value
;
2294 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2295 stats
->tx_multicast_packets
= xstats
[i
].value
;
2296 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2297 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2298 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2299 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2300 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2301 stats
->rx_undersized_errors
= xstats
[i
].value
;
2302 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2303 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2304 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2305 stats
->rx_jabber_errors
= xstats
[i
].value
;
2311 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2313 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2314 struct rte_eth_stats rte_stats
;
2317 netdev_dpdk_get_carrier(netdev
, &gg
);
2318 ovs_mutex_lock(&dev
->mutex
);
2320 struct rte_eth_xstat
*rte_xstats
= NULL
;
2321 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2322 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2324 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2325 VLOG_ERR("Can't get ETH statistics for port: "DPDK_PORT_ID_FMT
,
2327 ovs_mutex_unlock(&dev
->mutex
);
2331 /* Get length of statistics */
2332 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2333 if (rte_xstats_len
< 0) {
2334 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2338 /* Reserve memory for xstats names and values */
2339 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2340 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2342 /* Retreive xstats names */
2343 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2346 if (rte_xstats_new_len
!= rte_xstats_len
) {
2347 VLOG_WARN("Cannot get XSTATS names for port: "DPDK_PORT_ID_FMT
,
2351 /* Retreive xstats values */
2352 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2353 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2355 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2356 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2359 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2365 free(rte_xstats_names
);
2367 stats
->rx_packets
= rte_stats
.ipackets
;
2368 stats
->tx_packets
= rte_stats
.opackets
;
2369 stats
->rx_bytes
= rte_stats
.ibytes
;
2370 stats
->tx_bytes
= rte_stats
.obytes
;
2371 stats
->rx_errors
= rte_stats
.ierrors
;
2372 stats
->tx_errors
= rte_stats
.oerrors
;
2374 rte_spinlock_lock(&dev
->stats_lock
);
2375 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2376 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2377 rte_spinlock_unlock(&dev
->stats_lock
);
2379 /* These are the available DPDK counters for packets not received due to
2380 * local resource constraints in DPDK and NIC respectively. */
2381 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2382 stats
->rx_missed_errors
= rte_stats
.imissed
;
2384 ovs_mutex_unlock(&dev
->mutex
);
2390 netdev_dpdk_get_custom_stats(const struct netdev
*netdev
,
2391 struct netdev_custom_stats
*custom_stats
)
2395 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2398 ovs_mutex_lock(&dev
->mutex
);
2400 if (netdev_dpdk_configure_xstats(dev
)) {
2401 uint64_t *values
= xcalloc(dev
->rte_xstats_ids_size
,
2405 rte_eth_xstats_get_by_id(dev
->port_id
, dev
->rte_xstats_ids
,
2406 values
, dev
->rte_xstats_ids_size
);
2408 if (rte_xstats_ret
> 0 &&
2409 rte_xstats_ret
<= dev
->rte_xstats_ids_size
) {
2411 custom_stats
->size
= rte_xstats_ret
;
2412 custom_stats
->counters
=
2413 (struct netdev_custom_counter
*) xcalloc(rte_xstats_ret
,
2414 sizeof(struct netdev_custom_counter
));
2416 for (i
= 0; i
< rte_xstats_ret
; i
++) {
2417 ovs_strlcpy(custom_stats
->counters
[i
].name
,
2418 netdev_dpdk_get_xstat_name(dev
,
2419 dev
->rte_xstats_ids
[i
]),
2420 NETDEV_CUSTOM_STATS_NAME_SIZE
);
2421 custom_stats
->counters
[i
].value
= values
[i
];
2424 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT
,
2426 custom_stats
->counters
= NULL
;
2427 custom_stats
->size
= 0;
2428 /* Let's clear statistics cache, so it will be
2430 netdev_dpdk_clear_xstats(dev
);
2436 ovs_mutex_unlock(&dev
->mutex
);
2442 netdev_dpdk_get_features(const struct netdev
*netdev
,
2443 enum netdev_features
*current
,
2444 enum netdev_features
*advertised
,
2445 enum netdev_features
*supported
,
2446 enum netdev_features
*peer
)
2448 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2449 struct rte_eth_link link
;
2451 ovs_mutex_lock(&dev
->mutex
);
2453 ovs_mutex_unlock(&dev
->mutex
);
2455 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2456 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2457 *current
= NETDEV_F_10MB_HD
;
2459 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2460 *current
= NETDEV_F_100MB_HD
;
2462 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2463 *current
= NETDEV_F_1GB_HD
;
2465 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2466 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2467 *current
= NETDEV_F_10MB_FD
;
2469 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2470 *current
= NETDEV_F_100MB_FD
;
2472 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2473 *current
= NETDEV_F_1GB_FD
;
2475 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2476 *current
= NETDEV_F_10GB_FD
;
2480 if (link
.link_autoneg
) {
2481 *current
|= NETDEV_F_AUTONEG
;
2484 *advertised
= *supported
= *peer
= 0;
2489 static struct ingress_policer
*
2490 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2492 struct ingress_policer
*policer
= NULL
;
2493 uint64_t rate_bytes
;
2494 uint64_t burst_bytes
;
2497 policer
= xmalloc(sizeof *policer
);
2498 rte_spinlock_init(&policer
->policer_lock
);
2500 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2501 rate_bytes
= rate
* 1000ULL / 8;
2502 burst_bytes
= burst
* 1000ULL / 8;
2504 policer
->app_srtcm_params
.cir
= rate_bytes
;
2505 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2506 policer
->app_srtcm_params
.ebs
= 0;
2507 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2508 &policer
->app_srtcm_params
);
2510 VLOG_ERR("Could not create rte meter for ingress policer");
2519 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2520 uint32_t policer_burst
)
2522 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2523 struct ingress_policer
*policer
;
2525 /* Force to 0 if no rate specified,
2526 * default to 8000 kbits if burst is 0,
2527 * else stick with user-specified value.
2529 policer_burst
= (!policer_rate
? 0
2530 : !policer_burst
? 8000
2533 ovs_mutex_lock(&dev
->mutex
);
2535 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2536 &dev
->ingress_policer
);
2538 if (dev
->policer_rate
== policer_rate
&&
2539 dev
->policer_burst
== policer_burst
) {
2540 /* Assume that settings haven't changed since we last set them. */
2541 ovs_mutex_unlock(&dev
->mutex
);
2545 /* Destroy any existing ingress policer for the device if one exists */
2547 ovsrcu_postpone(free
, policer
);
2550 if (policer_rate
!= 0) {
2551 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2555 ovsrcu_set(&dev
->ingress_policer
, policer
);
2556 dev
->policer_rate
= policer_rate
;
2557 dev
->policer_burst
= policer_burst
;
2558 ovs_mutex_unlock(&dev
->mutex
);
2564 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2566 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2568 ovs_mutex_lock(&dev
->mutex
);
2569 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2570 * postive integer to meet RFC 2863 recommendations.
2572 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2573 ovs_mutex_unlock(&dev
->mutex
);
2579 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2581 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2583 ovs_mutex_lock(&dev
->mutex
);
2584 check_link_status(dev
);
2585 *carrier
= dev
->link
.link_status
;
2587 ovs_mutex_unlock(&dev
->mutex
);
2593 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2595 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2597 ovs_mutex_lock(&dev
->mutex
);
2599 if (is_vhost_running(dev
)) {
2605 ovs_mutex_unlock(&dev
->mutex
);
2610 static long long int
2611 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2613 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2614 long long int carrier_resets
;
2616 ovs_mutex_lock(&dev
->mutex
);
2617 carrier_resets
= dev
->link_reset_cnt
;
2618 ovs_mutex_unlock(&dev
->mutex
);
2620 return carrier_resets
;
2624 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2625 long long int interval OVS_UNUSED
)
2631 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2632 enum netdev_flags off
, enum netdev_flags on
,
2633 enum netdev_flags
*old_flagsp
)
2634 OVS_REQUIRES(dev
->mutex
)
2636 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2640 *old_flagsp
= dev
->flags
;
2644 if (dev
->flags
== *old_flagsp
) {
2648 if (dev
->type
== DPDK_DEV_ETH
) {
2649 if (dev
->flags
& NETDEV_PROMISC
) {
2650 rte_eth_promiscuous_enable(dev
->port_id
);
2653 netdev_change_seq_changed(&dev
->up
);
2655 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2656 * running then change netdev's change_seq to trigger link state
2659 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2660 && is_vhost_running(dev
)) {
2661 netdev_change_seq_changed(&dev
->up
);
2663 /* Clear statistics if device is getting up. */
2664 if (NETDEV_UP
& on
) {
2665 rte_spinlock_lock(&dev
->stats_lock
);
2666 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2667 rte_spinlock_unlock(&dev
->stats_lock
);
2676 netdev_dpdk_update_flags(struct netdev
*netdev
,
2677 enum netdev_flags off
, enum netdev_flags on
,
2678 enum netdev_flags
*old_flagsp
)
2680 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2683 ovs_mutex_lock(&dev
->mutex
);
2684 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2685 ovs_mutex_unlock(&dev
->mutex
);
2691 netdev_dpdk_vhost_user_get_status(const struct netdev
*netdev
,
2694 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2696 ovs_mutex_lock(&dev
->mutex
);
2698 bool client_mode
= dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
;
2699 smap_add_format(args
, "mode", "%s", client_mode
? "client" : "server");
2701 int vid
= netdev_dpdk_get_vid(dev
);
2703 smap_add_format(args
, "status", "disconnected");
2704 ovs_mutex_unlock(&dev
->mutex
);
2707 smap_add_format(args
, "status", "connected");
2710 char socket_name
[PATH_MAX
];
2711 if (!rte_vhost_get_ifname(vid
, socket_name
, PATH_MAX
)) {
2712 smap_add_format(args
, "socket", "%s", socket_name
);
2716 if (!rte_vhost_get_negotiated_features(vid
, &features
)) {
2717 smap_add_format(args
, "features", "0x%016"PRIx64
, features
);
2721 if (!rte_vhost_get_mtu(vid
, &mtu
)) {
2722 smap_add_format(args
, "mtu", "%d", mtu
);
2725 int numa
= rte_vhost_get_numa_node(vid
);
2727 smap_add_format(args
, "numa", "%d", numa
);
2730 uint16_t vring_num
= rte_vhost_get_vring_num(vid
);
2732 smap_add_format(args
, "num_of_vrings", "%d", vring_num
);
2735 for (int i
= 0; i
< vring_num
; i
++) {
2736 struct rte_vhost_vring vring
;
2737 char vhost_vring
[16];
2739 rte_vhost_get_vhost_vring(vid
, i
, &vring
);
2740 snprintf(vhost_vring
, 16, "vring_%d_size", i
);
2741 smap_add_format(args
, vhost_vring
, "%d", vring
.size
);
2744 ovs_mutex_unlock(&dev
->mutex
);
2749 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2751 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2752 struct rte_eth_dev_info dev_info
;
2754 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2758 ovs_mutex_lock(&dev
->mutex
);
2759 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2760 ovs_mutex_unlock(&dev
->mutex
);
2762 smap_add_format(args
, "port_no", DPDK_PORT_ID_FMT
, dev
->port_id
);
2763 smap_add_format(args
, "numa_id", "%d",
2764 rte_eth_dev_socket_id(dev
->port_id
));
2765 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2766 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2767 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2768 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2769 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2770 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2771 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2772 dev_info
.max_hash_mac_addrs
);
2773 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2774 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2776 /* Querying the DPDK library for iftype may be done in future, pending
2777 * support; cf. RFC 3635 Section 3.2.4. */
2778 enum { IF_TYPE_ETHERNETCSMACD
= 6 };
2780 smap_add_format(args
, "if_type", "%"PRIu32
, IF_TYPE_ETHERNETCSMACD
);
2781 smap_add_format(args
, "if_descr", "%s %s", rte_version(),
2782 dev_info
.driver_name
);
2784 if (dev_info
.pci_dev
) {
2785 smap_add_format(args
, "pci-vendor_id", "0x%u",
2786 dev_info
.pci_dev
->id
.vendor_id
);
2787 smap_add_format(args
, "pci-device_id", "0x%x",
2788 dev_info
.pci_dev
->id
.device_id
);
2795 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2796 OVS_REQUIRES(dev
->mutex
)
2798 enum netdev_flags old_flags
;
2801 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2803 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2808 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2809 const char *argv
[], void *aux OVS_UNUSED
)
2813 if (!strcasecmp(argv
[argc
- 1], "up")) {
2815 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2818 unixctl_command_reply_error(conn
, "Invalid Admin State");
2823 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2825 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2826 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2828 ovs_mutex_lock(&dev
->mutex
);
2829 netdev_dpdk_set_admin_state__(dev
, up
);
2830 ovs_mutex_unlock(&dev
->mutex
);
2832 netdev_close(netdev
);
2834 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2835 netdev_close(netdev
);
2839 struct netdev_dpdk
*dev
;
2841 ovs_mutex_lock(&dpdk_mutex
);
2842 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2843 ovs_mutex_lock(&dev
->mutex
);
2844 netdev_dpdk_set_admin_state__(dev
, up
);
2845 ovs_mutex_unlock(&dev
->mutex
);
2847 ovs_mutex_unlock(&dpdk_mutex
);
2849 unixctl_command_reply(conn
, "OK");
2853 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2854 const char *argv
[], void *aux OVS_UNUSED
)
2858 dpdk_port_t port_id
;
2859 char devname
[RTE_ETH_NAME_MAX_LEN
];
2860 struct netdev_dpdk
*dev
;
2862 ovs_mutex_lock(&dpdk_mutex
);
2864 if (rte_eth_dev_get_port_by_name(argv
[1], &port_id
)) {
2865 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2869 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2871 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2872 "Remove it before detaching",
2873 argv
[1], netdev_get_name(&dev
->up
));
2877 rte_eth_dev_close(port_id
);
2879 ret
= rte_eth_dev_detach(port_id
, devname
);
2881 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2885 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2887 ovs_mutex_unlock(&dpdk_mutex
);
2888 unixctl_command_reply(conn
, response
);
2893 ovs_mutex_unlock(&dpdk_mutex
);
2894 unixctl_command_reply_error(conn
, response
);
2899 netdev_dpdk_get_mempool_info(struct unixctl_conn
*conn
,
2900 int argc
, const char *argv
[],
2901 void *aux OVS_UNUSED
)
2905 char *response
= NULL
;
2906 struct netdev
*netdev
= NULL
;
2909 netdev
= netdev_from_name(argv
[1]);
2910 if (!netdev
|| !is_dpdk_class(netdev
->netdev_class
)) {
2911 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2916 stream
= open_memstream(&response
, &size
);
2918 response
= xasprintf("Unable to open memstream: %s.",
2919 ovs_strerror(errno
));
2920 unixctl_command_reply_error(conn
, response
);
2925 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2927 ovs_mutex_lock(&dev
->mutex
);
2928 ovs_mutex_lock(&dpdk_mp_mutex
);
2930 rte_mempool_dump(stream
, dev
->mp
);
2932 ovs_mutex_unlock(&dpdk_mp_mutex
);
2933 ovs_mutex_unlock(&dev
->mutex
);
2935 ovs_mutex_lock(&dpdk_mp_mutex
);
2936 rte_mempool_list_dump(stream
);
2937 ovs_mutex_unlock(&dpdk_mp_mutex
);
2942 unixctl_command_reply(conn
, response
);
2945 netdev_close(netdev
);
2949 * Set virtqueue flags so that we do not receive interrupts.
2952 set_irq_status(int vid
)
2956 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2957 rte_vhost_enable_guest_notification(vid
, i
, 0);
2962 * Fixes mapping for vhost-user tx queues. Must be called after each
2963 * enabling/disabling of queues and n_txq modifications.
2966 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2967 OVS_REQUIRES(dev
->mutex
)
2969 int *enabled_queues
, n_enabled
= 0;
2970 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2972 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2974 for (i
= 0; i
< total_txqs
; i
++) {
2975 /* Enabled queues always mapped to themselves. */
2976 if (dev
->tx_q
[i
].map
== i
) {
2977 enabled_queues
[n_enabled
++] = i
;
2981 if (n_enabled
== 0 && total_txqs
!= 0) {
2982 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2987 for (i
= 0; i
< total_txqs
; i
++) {
2988 if (dev
->tx_q
[i
].map
!= i
) {
2989 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2990 k
= (k
+ 1) % n_enabled
;
2994 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2995 for (i
= 0; i
< total_txqs
; i
++) {
2996 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2999 free(enabled_queues
);
3003 * A new virtio-net device is added to a vhost port.
3008 struct netdev_dpdk
*dev
;
3009 bool exists
= false;
3011 char ifname
[IF_NAME_SZ
];
3013 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3015 ovs_mutex_lock(&dpdk_mutex
);
3016 /* Add device to the vhost port with the same name as that passed down. */
3017 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
3018 ovs_mutex_lock(&dev
->mutex
);
3019 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3020 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
3022 /* Get NUMA information */
3023 newnode
= rte_vhost_get_numa_node(vid
);
3024 if (newnode
== -1) {
3026 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
3029 newnode
= dev
->socket_id
;
3032 if (dev
->requested_n_txq
!= qp_num
3033 || dev
->requested_n_rxq
!= qp_num
3034 || dev
->requested_socket_id
!= newnode
) {
3035 dev
->requested_socket_id
= newnode
;
3036 dev
->requested_n_rxq
= qp_num
;
3037 dev
->requested_n_txq
= qp_num
;
3038 netdev_request_reconfigure(&dev
->up
);
3040 /* Reconfiguration not required. */
3041 dev
->vhost_reconfigured
= true;
3044 ovsrcu_index_set(&dev
->vid
, vid
);
3047 /* Disable notifications. */
3048 set_irq_status(vid
);
3049 netdev_change_seq_changed(&dev
->up
);
3050 ovs_mutex_unlock(&dev
->mutex
);
3053 ovs_mutex_unlock(&dev
->mutex
);
3055 ovs_mutex_unlock(&dpdk_mutex
);
3058 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
3063 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
3069 /* Clears mapping for all available queues of vhost interface. */
3071 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
3072 OVS_REQUIRES(dev
->mutex
)
3076 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
3077 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
3082 * Remove a virtio-net device from the specific vhost port. Use dev->remove
3083 * flag to stop any more packets from being sent or received to/from a VM and
3084 * ensure all currently queued packets have been sent/received before removing
3088 destroy_device(int vid
)
3090 struct netdev_dpdk
*dev
;
3091 bool exists
= false;
3092 char ifname
[IF_NAME_SZ
];
3094 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3096 ovs_mutex_lock(&dpdk_mutex
);
3097 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3098 if (netdev_dpdk_get_vid(dev
) == vid
) {
3100 ovs_mutex_lock(&dev
->mutex
);
3101 dev
->vhost_reconfigured
= false;
3102 ovsrcu_index_set(&dev
->vid
, -1);
3103 netdev_dpdk_txq_map_clear(dev
);
3105 netdev_change_seq_changed(&dev
->up
);
3106 ovs_mutex_unlock(&dev
->mutex
);
3112 ovs_mutex_unlock(&dpdk_mutex
);
3116 * Wait for other threads to quiesce after setting the 'virtio_dev'
3117 * to NULL, before returning.
3119 ovsrcu_synchronize();
3121 * As call to ovsrcu_synchronize() will end the quiescent state,
3122 * put thread back into quiescent state before returning.
3124 ovsrcu_quiesce_start();
3125 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
3127 VLOG_INFO("vHost Device '%s' not found", ifname
);
3132 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
3134 struct netdev_dpdk
*dev
;
3135 bool exists
= false;
3136 int qid
= queue_id
/ VIRTIO_QNUM
;
3137 char ifname
[IF_NAME_SZ
];
3139 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
3141 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
3145 ovs_mutex_lock(&dpdk_mutex
);
3146 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
3147 ovs_mutex_lock(&dev
->mutex
);
3148 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
3150 dev
->tx_q
[qid
].map
= qid
;
3152 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
3154 netdev_dpdk_remap_txqs(dev
);
3156 ovs_mutex_unlock(&dev
->mutex
);
3159 ovs_mutex_unlock(&dev
->mutex
);
3161 ovs_mutex_unlock(&dpdk_mutex
);
3164 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
3165 "changed to \'%s\'", queue_id
, qid
, ifname
,
3166 (enable
== 1) ? "enabled" : "disabled");
3168 VLOG_INFO("vHost Device '%s' not found", ifname
);
3176 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
3178 return ovsrcu_index_get(&dev
->vid
);
3181 struct ingress_policer
*
3182 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
3184 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
3188 netdev_dpdk_class_init(void)
3190 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3192 /* This function can be called for different classes. The initialization
3193 * needs to be done only once */
3194 if (ovsthread_once_start(&once
)) {
3195 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
3196 unixctl_command_register("netdev-dpdk/set-admin-state",
3197 "[netdev] up|down", 1, 2,
3198 netdev_dpdk_set_admin_state
, NULL
);
3200 unixctl_command_register("netdev-dpdk/detach",
3201 "pci address of device", 1, 1,
3202 netdev_dpdk_detach
, NULL
);
3204 unixctl_command_register("netdev-dpdk/get-mempool-info",
3206 netdev_dpdk_get_mempool_info
, NULL
);
3208 ovsthread_once_done(&once
);
3218 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
3219 dpdk_port_t
*eth_port_id
)
3221 struct dpdk_ring
*ring_pair
;
3225 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
3230 /* XXX: Add support for multiquque ring. */
3231 ring_name
= xasprintf("%s_tx", dev_name
);
3233 /* Create single producer tx ring, netdev does explicit locking. */
3234 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3237 if (ring_pair
->cring_tx
== NULL
) {
3238 rte_free(ring_pair
);
3242 ring_name
= xasprintf("%s_rx", dev_name
);
3244 /* Create single consumer rx ring, netdev does explicit locking. */
3245 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
3248 if (ring_pair
->cring_rx
== NULL
) {
3249 rte_free(ring_pair
);
3253 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
3254 &ring_pair
->cring_tx
, 1, SOCKET0
);
3257 rte_free(ring_pair
);
3261 ring_pair
->user_port_id
= port_no
;
3262 ring_pair
->eth_port_id
= port_id
;
3263 *eth_port_id
= port_id
;
3265 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
3271 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
3272 OVS_REQUIRES(dpdk_mutex
)
3274 struct dpdk_ring
*ring_pair
;
3275 unsigned int port_no
;
3278 /* Names always start with "dpdkr" */
3279 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
3284 /* Look through our list to find the device */
3285 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
3286 if (ring_pair
->user_port_id
== port_no
) {
3287 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
3288 /* Really all that is needed */
3289 *eth_port_id
= ring_pair
->eth_port_id
;
3293 /* Need to create the device rings */
3294 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
3298 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
3299 struct dp_packet_batch
*batch
, bool concurrent_txq
)
3301 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3302 struct dp_packet
*packet
;
3304 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
3305 * the rss hash field is clear. This is because the same mbuf may be
3306 * modified by the consumer of the ring and return into the datapath
3307 * without recalculating the RSS hash. */
3308 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
3309 dp_packet_mbuf_rss_flag_reset(packet
);
3312 netdev_dpdk_send__(dev
, qid
, batch
, concurrent_txq
);
3317 netdev_dpdk_ring_construct(struct netdev
*netdev
)
3319 dpdk_port_t port_no
= 0;
3322 ovs_mutex_lock(&dpdk_mutex
);
3324 err
= dpdk_ring_open(netdev
->name
, &port_no
);
3329 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
3330 rte_eth_dev_socket_id(port_no
));
3332 ovs_mutex_unlock(&dpdk_mutex
);
3339 * Initialize QoS configuration operations.
3342 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
3345 rte_spinlock_init(&conf
->lock
);
3349 * Search existing QoS operations in qos_ops and compare each set of
3350 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
3353 static const struct dpdk_qos_ops
*
3354 qos_lookup_name(const char *name
)
3356 const struct dpdk_qos_ops
*const *opsp
;
3358 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3359 const struct dpdk_qos_ops
*ops
= *opsp
;
3360 if (!strcmp(name
, ops
->qos_name
)) {
3368 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3371 const struct dpdk_qos_ops
*const *opsp
;
3373 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3374 const struct dpdk_qos_ops
*ops
= *opsp
;
3375 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3376 sset_add(types
, ops
->qos_name
);
3383 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3384 const char **typep
, struct smap
*details
)
3386 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3387 struct qos_conf
*qos_conf
;
3390 ovs_mutex_lock(&dev
->mutex
);
3391 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3393 *typep
= qos_conf
->ops
->qos_name
;
3394 error
= (qos_conf
->ops
->qos_get
3395 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3397 /* No QoS configuration set, return an empty string */
3400 ovs_mutex_unlock(&dev
->mutex
);
3406 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3407 const struct smap
*details
)
3409 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3410 const struct dpdk_qos_ops
*new_ops
= NULL
;
3411 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3414 ovs_mutex_lock(&dev
->mutex
);
3416 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3418 new_ops
= qos_lookup_name(type
);
3420 if (!new_ops
|| !new_ops
->qos_construct
) {
3421 new_qos_conf
= NULL
;
3422 if (type
&& type
[0]) {
3425 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3426 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3427 new_qos_conf
= qos_conf
;
3429 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3433 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3434 type
, netdev
->name
, rte_strerror(error
));
3437 if (new_qos_conf
!= qos_conf
) {
3438 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3440 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3444 ovs_mutex_unlock(&dev
->mutex
);
3449 /* egress-policer details */
3451 struct egress_policer
{
3452 struct qos_conf qos_conf
;
3453 struct rte_meter_srtcm_params app_srtcm_params
;
3454 struct rte_meter_srtcm egress_meter
;
3458 egress_policer_details_to_param(const struct smap
*details
,
3459 struct rte_meter_srtcm_params
*params
)
3461 memset(params
, 0, sizeof *params
);
3462 params
->cir
= smap_get_ullong(details
, "cir", 0);
3463 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3468 egress_policer_qos_construct(const struct smap
*details
,
3469 struct qos_conf
**conf
)
3471 struct egress_policer
*policer
;
3474 policer
= xmalloc(sizeof *policer
);
3475 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3476 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3477 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3478 &policer
->app_srtcm_params
);
3480 *conf
= &policer
->qos_conf
;
3491 egress_policer_qos_destruct(struct qos_conf
*conf
)
3493 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3499 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3501 struct egress_policer
*policer
=
3502 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3504 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3505 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3511 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3512 const struct smap
*details
)
3514 struct egress_policer
*policer
=
3515 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3516 struct rte_meter_srtcm_params params
;
3518 egress_policer_details_to_param(details
, ¶ms
);
3520 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3524 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3528 struct egress_policer
*policer
=
3529 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3531 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3532 pkt_cnt
, may_steal
);
3537 static const struct dpdk_qos_ops egress_policer_ops
= {
3538 "egress-policer", /* qos_name */
3539 egress_policer_qos_construct
,
3540 egress_policer_qos_destruct
,
3541 egress_policer_qos_get
,
3542 egress_policer_qos_is_equal
,
3547 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3549 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3552 ovs_mutex_lock(&dev
->mutex
);
3554 if (netdev
->n_txq
== dev
->requested_n_txq
3555 && netdev
->n_rxq
== dev
->requested_n_rxq
3556 && dev
->mtu
== dev
->requested_mtu
3557 && dev
->rxq_size
== dev
->requested_rxq_size
3558 && dev
->txq_size
== dev
->requested_txq_size
3559 && dev
->socket_id
== dev
->requested_socket_id
) {
3560 /* Reconfiguration is unnecessary */
3565 rte_eth_dev_stop(dev
->port_id
);
3567 err
= netdev_dpdk_mempool_configure(dev
);
3568 if (err
&& err
!= EEXIST
) {
3572 netdev
->n_txq
= dev
->requested_n_txq
;
3573 netdev
->n_rxq
= dev
->requested_n_rxq
;
3575 dev
->rxq_size
= dev
->requested_rxq_size
;
3576 dev
->txq_size
= dev
->requested_txq_size
;
3578 rte_free(dev
->tx_q
);
3579 err
= dpdk_eth_dev_init(dev
);
3580 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3585 netdev_change_seq_changed(netdev
);
3588 ovs_mutex_unlock(&dev
->mutex
);
3593 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3594 OVS_REQUIRES(dev
->mutex
)
3596 dev
->up
.n_txq
= dev
->requested_n_txq
;
3597 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3600 /* Enable TX queue 0 by default if it wasn't disabled. */
3601 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3602 dev
->tx_q
[0].map
= 0;
3605 netdev_dpdk_remap_txqs(dev
);
3607 err
= netdev_dpdk_mempool_configure(dev
);
3609 /* A new mempool was created. */
3610 netdev_change_seq_changed(&dev
->up
);
3611 } else if (err
!= EEXIST
){
3614 if (netdev_dpdk_get_vid(dev
) >= 0) {
3615 if (dev
->vhost_reconfigured
== false) {
3616 dev
->vhost_reconfigured
= true;
3617 /* Carrier status may need updating. */
3618 netdev_change_seq_changed(&dev
->up
);
3626 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3628 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3631 ovs_mutex_lock(&dev
->mutex
);
3632 err
= dpdk_vhost_reconfigure_helper(dev
);
3633 ovs_mutex_unlock(&dev
->mutex
);
3639 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3641 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3643 uint64_t vhost_flags
= 0;
3646 ovs_mutex_lock(&dev
->mutex
);
3648 /* Configure vHost client mode if requested and if the following criteria
3650 * 1. Device hasn't been registered yet.
3651 * 2. A path has been specified.
3653 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3654 && strlen(dev
->vhost_id
)) {
3655 /* Register client-mode device. */
3656 vhost_flags
|= RTE_VHOST_USER_CLIENT
;
3658 /* Enable IOMMU support, if explicitly requested. */
3659 if (dpdk_vhost_iommu_enabled()) {
3660 vhost_flags
|= RTE_VHOST_USER_IOMMU_SUPPORT
;
3663 zc_enabled
= dev
->vhost_driver_flags
3664 & RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
3665 /* Enable zero copy flag, if requested */
3667 vhost_flags
|= RTE_VHOST_USER_DEQUEUE_ZERO_COPY
;
3670 err
= rte_vhost_driver_register(dev
->vhost_id
, vhost_flags
);
3672 VLOG_ERR("vhost-user device setup failure for device %s\n",
3676 /* Configuration successful */
3677 dev
->vhost_driver_flags
|= vhost_flags
;
3678 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3679 "using client socket '%s'",
3680 dev
->up
.name
, dev
->vhost_id
);
3682 VLOG_INFO("Zero copy enabled for vHost port %s", dev
->up
.name
);
3686 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3687 &virtio_net_device_ops
);
3689 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3690 "vhost user client port: %s\n", dev
->up
.name
);
3694 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3695 1ULL << VIRTIO_NET_F_HOST_TSO4
3696 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3697 | 1ULL << VIRTIO_NET_F_CSUM
);
3699 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3700 "client port: %s\n", dev
->up
.name
);
3704 err
= rte_vhost_driver_start(dev
->vhost_id
);
3706 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3707 "client port: %s\n", dev
->up
.name
);
3712 err
= dpdk_vhost_reconfigure_helper(dev
);
3715 ovs_mutex_unlock(&dev
->mutex
);
3720 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3721 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3722 GET_CARRIER, GET_STATS, \
3724 GET_FEATURES, GET_STATUS, \
3725 RECONFIGURE, RXQ_RECV) \
3728 true, /* is_pmd */ \
3730 NULL, /* netdev_dpdk_run */ \
3731 NULL, /* netdev_dpdk_wait */ \
3733 netdev_dpdk_alloc, \
3736 netdev_dpdk_dealloc, \
3737 netdev_dpdk_get_config, \
3739 NULL, /* get_tunnel_config */ \
3740 NULL, /* build header */ \
3741 NULL, /* push header */ \
3742 NULL, /* pop header */ \
3743 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3747 NULL, /* send_wait */ \
3749 netdev_dpdk_set_etheraddr, \
3750 netdev_dpdk_get_etheraddr, \
3751 netdev_dpdk_get_mtu, \
3752 netdev_dpdk_set_mtu, \
3753 netdev_dpdk_get_ifindex, \
3755 netdev_dpdk_get_carrier_resets, \
3756 netdev_dpdk_set_miimon, \
3760 NULL, /* set_advertisements */ \
3761 NULL, /* get_pt_mode */ \
3763 netdev_dpdk_set_policing, \
3764 netdev_dpdk_get_qos_types, \
3765 NULL, /* get_qos_capabilities */ \
3766 netdev_dpdk_get_qos, \
3767 netdev_dpdk_set_qos, \
3768 NULL, /* get_queue */ \
3769 NULL, /* set_queue */ \
3770 NULL, /* delete_queue */ \
3771 NULL, /* get_queue_stats */ \
3772 NULL, /* queue_dump_start */ \
3773 NULL, /* queue_dump_next */ \
3774 NULL, /* queue_dump_done */ \
3775 NULL, /* dump_queue_stats */ \
3777 NULL, /* set_in4 */ \
3778 NULL, /* get_addr_list */ \
3779 NULL, /* add_router */ \
3780 NULL, /* get_next_hop */ \
3782 NULL, /* arp_lookup */ \
3784 netdev_dpdk_update_flags, \
3787 netdev_dpdk_rxq_alloc, \
3788 netdev_dpdk_rxq_construct, \
3789 netdev_dpdk_rxq_destruct, \
3790 netdev_dpdk_rxq_dealloc, \
3792 NULL, /* rx_wait */ \
3793 NULL, /* rxq_drain */ \
3797 static const struct netdev_class dpdk_class
=
3800 netdev_dpdk_class_init
,
3801 netdev_dpdk_construct
,
3802 netdev_dpdk_destruct
,
3803 netdev_dpdk_set_config
,
3804 netdev_dpdk_set_tx_multiq
,
3805 netdev_dpdk_eth_send
,
3806 netdev_dpdk_get_carrier
,
3807 netdev_dpdk_get_stats
,
3808 netdev_dpdk_get_custom_stats
,
3809 netdev_dpdk_get_features
,
3810 netdev_dpdk_get_status
,
3811 netdev_dpdk_reconfigure
,
3812 netdev_dpdk_rxq_recv
);
3814 static const struct netdev_class dpdk_ring_class
=
3817 netdev_dpdk_class_init
,
3818 netdev_dpdk_ring_construct
,
3819 netdev_dpdk_destruct
,
3820 netdev_dpdk_ring_set_config
,
3821 netdev_dpdk_set_tx_multiq
,
3822 netdev_dpdk_ring_send
,
3823 netdev_dpdk_get_carrier
,
3824 netdev_dpdk_get_stats
,
3825 netdev_dpdk_get_custom_stats
,
3826 netdev_dpdk_get_features
,
3827 netdev_dpdk_get_status
,
3828 netdev_dpdk_reconfigure
,
3829 netdev_dpdk_rxq_recv
);
3831 static const struct netdev_class dpdk_vhost_class
=
3835 netdev_dpdk_vhost_construct
,
3836 netdev_dpdk_vhost_destruct
,
3839 netdev_dpdk_vhost_send
,
3840 netdev_dpdk_vhost_get_carrier
,
3841 netdev_dpdk_vhost_get_stats
,
3844 netdev_dpdk_vhost_user_get_status
,
3845 netdev_dpdk_vhost_reconfigure
,
3846 netdev_dpdk_vhost_rxq_recv
);
3847 static const struct netdev_class dpdk_vhost_client_class
=
3849 "dpdkvhostuserclient",
3851 netdev_dpdk_vhost_client_construct
,
3852 netdev_dpdk_vhost_destruct
,
3853 netdev_dpdk_vhost_client_set_config
,
3855 netdev_dpdk_vhost_send
,
3856 netdev_dpdk_vhost_get_carrier
,
3857 netdev_dpdk_vhost_get_stats
,
3860 netdev_dpdk_vhost_user_get_status
,
3861 netdev_dpdk_vhost_client_reconfigure
,
3862 netdev_dpdk_vhost_rxq_recv
);
3865 netdev_dpdk_register(void)
3867 netdev_register_provider(&dpdk_class
);
3868 netdev_register_provider(&dpdk_ring_class
);
3869 netdev_register_provider(&dpdk_vhost_class
);
3870 netdev_register_provider(&dpdk_vhost_client_class
);