2 * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "netdev-dpdk.h"
26 #include <rte_config.h>
27 #include <rte_cycles.h>
28 #include <rte_errno.h>
29 #include <rte_eth_ring.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
33 #include <rte_meter.h>
34 #include <rte_virtio_net.h>
37 #include "dp-packet.h"
39 #include "dpif-netdev.h"
40 #include "fatal-signal.h"
41 #include "netdev-provider.h"
42 #include "netdev-vport.h"
44 #include "openvswitch/dynamic-string.h"
45 #include "openvswitch/list.h"
46 #include "openvswitch/ofp-print.h"
47 #include "openvswitch/vlog.h"
49 #include "ovs-thread.h"
52 #include "openvswitch/shash.h"
55 #include "unaligned.h"
59 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
60 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
62 #define DPDK_PORT_WATCHDOG_INTERVAL 5
64 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
65 #define OVS_VPORT_DPDK "ovs_dpdk"
68 * need to reserve tons of extra space in the mbufs so we can align the
69 * DMA addresses to 4KB.
70 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
71 * performance for standard Ethernet MTU.
73 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
74 + (2 * VLAN_HEADER_LEN))
75 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
76 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
77 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
78 - ETHER_HDR_LEN - ETHER_CRC_LEN)
79 #define MBUF_SIZE(mtu) (MTU_TO_MAX_FRAME_LEN(mtu) \
80 + sizeof(struct dp_packet) \
81 + RTE_PKTMBUF_HEADROOM)
82 #define NETDEV_DPDK_MBUF_ALIGN 1024
83 #define NETDEV_DPDK_MAX_PKT_LEN 9728
85 /* Max and min number of packets in the mempool. OVS tries to allocate a
86 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
87 * enough hugepages) we keep halving the number until the allocation succeeds
88 * or we reach MIN_NB_MBUF */
90 #define MAX_NB_MBUF (4096 * 64)
91 #define MIN_NB_MBUF (4096 * 4)
92 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
94 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
95 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
) == 0);
97 /* The smallest possible NB_MBUF that we're going to try should be a multiple
98 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
99 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
))
103 * DPDK XSTATS Counter names definition
105 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
106 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
107 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
108 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
109 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
110 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
111 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
113 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
114 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
115 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
116 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
117 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
118 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
119 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
121 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
122 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
123 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
124 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
125 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
126 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
127 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
128 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
132 /* Default size of Physical NIC RXQ */
133 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
134 /* Default size of Physical NIC TXQ */
135 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
136 /* Maximum size of Physical NIC Queues */
137 #define NIC_PORT_MAX_Q_SIZE 4096
139 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
140 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
141 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
142 * yet mapped to another queue. */
144 #define VHOST_ENQ_RETRY_NUM 8
145 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
147 static const struct rte_eth_conf port_conf
= {
149 .mq_mode
= ETH_MQ_RX_RSS
,
151 .header_split
= 0, /* Header Split disabled */
152 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
153 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
154 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
160 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
164 .mq_mode
= ETH_MQ_TX_NONE
,
168 enum { DPDK_RING_SIZE
= 256 };
169 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
170 enum { DRAIN_TSC
= 200000ULL };
177 /* Quality of Service */
179 /* An instance of a QoS configuration. Always associated with a particular
182 * Each QoS implementation subclasses this with whatever additional data it
186 const struct dpdk_qos_ops
*ops
;
190 /* A particular implementation of dpdk QoS operations.
192 * The functions below return 0 if successful or a positive errno value on
193 * failure, except where otherwise noted. All of them must be provided, except
194 * where otherwise noted.
196 struct dpdk_qos_ops
{
198 /* Name of the QoS type */
199 const char *qos_name
;
201 /* Called to construct a qos_conf object. The implementation should make
202 * the appropriate calls to configure QoS according to 'details'.
204 * The contents of 'details' should be documented as valid for 'ovs_name'
205 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
206 * (which is built as ovs-vswitchd.conf.db(8)).
208 * This function must return 0 if and only if it sets '*conf' to an
209 * initialized 'struct qos_conf'.
211 * For all QoS implementations it should always be non-null.
213 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
215 /* Destroys the data structures allocated by the implementation as part of
218 * For all QoS implementations it should always be non-null.
220 void (*qos_destruct
)(struct qos_conf
*conf
);
222 /* Retrieves details of 'conf' configuration into '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 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
230 /* Returns true if 'conf' is already configured according to 'details'.
232 * The contents of 'details' should be documented as valid for 'ovs_name'
233 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
234 * (which is built as ovs-vswitchd.conf.db(8)).
236 * For all QoS implementations it should always be non-null.
238 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
239 const struct smap
*details
);
241 /* Modify an array of rte_mbufs. The modification is specific to
242 * each qos implementation.
244 * The function should take and array of mbufs and an int representing
245 * the current number of mbufs present in the array.
247 * After the function has performed a qos modification to the array of
248 * mbufs it returns an int representing the number of mbufs now present in
249 * the array. This value is can then be passed to the port send function
250 * along with the modified array for transmission.
252 * For all QoS implementations it should always be non-null.
254 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
258 /* dpdk_qos_ops for each type of user space QoS implementation */
259 static const struct dpdk_qos_ops egress_policer_ops
;
262 * Array of dpdk_qos_ops, contains pointer to all supported QoS
265 static const struct dpdk_qos_ops
*const qos_confs
[] = {
270 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
272 /* Contains all 'struct dpdk_dev's. */
273 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
274 = OVS_LIST_INITIALIZER(&dpdk_list
);
276 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
277 = OVS_MUTEX_INITIALIZER
;
279 static struct ovs_list dpdk_mp_list
OVS_GUARDED_BY(dpdk_mp_mutex
)
280 = OVS_LIST_INITIALIZER(&dpdk_mp_list
);
283 struct rte_mempool
*mp
;
287 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
290 /* There should be one 'struct dpdk_tx_queue' created for
292 struct dpdk_tx_queue
{
293 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
294 * from concurrent access. It is used only
295 * if the queue is shared among different
296 * pmd threads (see 'concurrent_txq'). */
297 int map
; /* Mapping of configured vhost-user queues
298 * to enabled by guest. */
301 /* dpdk has no way to remove dpdk ring ethernet devices
302 so we have to keep them around once they've been created
305 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
306 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
309 /* For the client rings */
310 struct rte_ring
*cring_tx
;
311 struct rte_ring
*cring_rx
;
312 unsigned int user_port_id
; /* User given port no, parsed from port name */
313 int eth_port_id
; /* ethernet device port id */
314 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
317 struct ingress_policer
{
318 struct rte_meter_srtcm_params app_srtcm_params
;
319 struct rte_meter_srtcm in_policer
;
320 rte_spinlock_t policer_lock
;
323 enum dpdk_hw_ol_features
{
324 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
331 enum dpdk_dev_type type
;
333 struct dpdk_tx_queue
*tx_q
;
335 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
337 struct dpdk_mp
*dpdk_mp
;
341 struct netdev_stats stats
;
343 rte_spinlock_t stats_lock
;
345 struct eth_addr hwaddr
;
346 enum netdev_flags flags
;
348 struct rte_eth_link link
;
351 /* virtio identifier for vhost devices */
354 /* True if vHost device is 'up' and has been reconfigured at least once */
355 bool vhost_reconfigured
;
357 /* Identifier used to distinguish vhost devices from each other. */
358 char vhost_id
[PATH_MAX
];
360 /* Device arguments for dpdk ports */
364 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
366 /* QoS configuration and lock for the device */
367 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
369 /* The following properties cannot be changed when a device is running,
370 * so we remember the request and update them next time
371 * netdev_dpdk*_reconfigure() is called */
375 int requested_rxq_size
;
376 int requested_txq_size
;
378 /* Number of rx/tx descriptors for physical devices */
382 /* Socket ID detected when vHost device is brought up */
383 int requested_socket_id
;
385 /* Denotes whether vHost port is client/server mode */
386 uint64_t vhost_driver_flags
;
388 /* Ingress Policer */
389 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
390 uint32_t policer_rate
;
391 uint32_t policer_burst
;
393 /* DPDK-ETH Flow control */
394 struct rte_eth_fc_conf fc_conf
;
396 /* DPDK-ETH hardware offload features,
397 * from the enum set 'dpdk_hw_ol_features' */
398 uint32_t hw_ol_features
;
401 struct netdev_rxq_dpdk
{
402 struct netdev_rxq up
;
406 static int netdev_dpdk_class_init(void);
407 static int netdev_dpdk_vhost_class_init(void);
409 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
411 struct ingress_policer
*
412 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
415 is_dpdk_class(const struct netdev_class
*class)
417 return class->init
== netdev_dpdk_class_init
418 || class->init
== netdev_dpdk_vhost_class_init
;
421 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
422 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
423 * value, insufficient buffers are allocated to accomodate the packet in its
424 * entirety. Furthermore, certain drivers need to ensure that there is also
425 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
426 * frames). If the RX buffer is too small, then the driver enables scatter RX
427 * behaviour, which reduces performance. To prevent this, use a buffer size
428 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
431 dpdk_buf_size(int mtu
)
433 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
434 NETDEV_DPDK_MBUF_ALIGN
);
437 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
439 * Unlike xmalloc(), this function can return NULL on failure. */
441 dpdk_rte_mzalloc(size_t sz
)
443 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
447 free_dpdk_buf(struct dp_packet
*p
)
449 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
451 rte_pktmbuf_free(pkt
);
455 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
456 void *opaque_arg OVS_UNUSED
,
458 unsigned i OVS_UNUSED
)
460 struct rte_mbuf
*pkt
= _p
;
462 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
465 static struct dpdk_mp
*
466 dpdk_mp_create(int socket_id
, int mtu
)
472 dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
476 dmp
->socket_id
= socket_id
;
479 /* XXX: this is a really rough method of provisioning memory.
480 * It's impossible to determine what the exact memory requirements are
481 * when the number of ports and rxqs that utilize a particular mempool can
482 * change dynamically at runtime. For now, use this rough heurisitic.
484 if (mtu
>= ETHER_MTU
) {
485 mp_size
= MAX_NB_MBUF
;
487 mp_size
= MIN_NB_MBUF
;
491 mp_name
= xasprintf("ovs_mp_%d_%d_%u", dmp
->mtu
, dmp
->socket_id
,
494 dmp
->mp
= rte_pktmbuf_pool_create(mp_name
, mp_size
,
496 sizeof (struct dp_packet
)
497 - sizeof (struct rte_mbuf
),
499 - sizeof(struct dp_packet
),
502 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
507 /* rte_pktmbuf_pool_create has done some initialization of the
508 * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init
509 * initializes some OVS specific fields of dp_packet.
511 rte_mempool_obj_iter(dmp
->mp
, ovs_rte_pktmbuf_init
, NULL
);
514 } while (rte_errno
== ENOMEM
&& (mp_size
/= 2) >= MIN_NB_MBUF
);
520 static struct dpdk_mp
*
521 dpdk_mp_get(int socket_id
, int mtu
)
525 ovs_mutex_lock(&dpdk_mp_mutex
);
526 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
527 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
533 dmp
= dpdk_mp_create(socket_id
, mtu
);
535 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
539 ovs_mutex_unlock(&dpdk_mp_mutex
);
545 dpdk_mp_put(struct dpdk_mp
*dmp
)
551 ovs_mutex_lock(&dpdk_mp_mutex
);
552 ovs_assert(dmp
->refcount
);
554 if (!--dmp
->refcount
) {
555 ovs_list_remove(&dmp
->list_node
);
556 rte_mempool_free(dmp
->mp
);
559 ovs_mutex_unlock(&dpdk_mp_mutex
);
562 /* Tries to allocate new mempool on requested_socket_id with
563 * mbuf size corresponding to requested_mtu.
564 * On success new configuration will be applied.
565 * On error, device will be left unchanged. */
567 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
568 OVS_REQUIRES(dev
->mutex
)
570 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
573 mp
= dpdk_mp_get(dev
->requested_socket_id
, FRAME_LEN_TO_MTU(buf_size
));
575 VLOG_ERR("Failed to create memory pool for netdev "
576 "%s, with MTU %d on socket %d: %s\n",
577 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
578 rte_strerror(rte_errno
));
581 dpdk_mp_put(dev
->dpdk_mp
);
583 dev
->mtu
= dev
->requested_mtu
;
584 dev
->socket_id
= dev
->requested_socket_id
;
585 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
592 check_link_status(struct netdev_dpdk
*dev
)
594 struct rte_eth_link link
;
596 rte_eth_link_get_nowait(dev
->port_id
, &link
);
598 if (dev
->link
.link_status
!= link
.link_status
) {
599 netdev_change_seq_changed(&dev
->up
);
601 dev
->link_reset_cnt
++;
603 if (dev
->link
.link_status
) {
604 VLOG_DBG_RL(&rl
, "Port %d Link Up - speed %u Mbps - %s",
605 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
606 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
607 ("full-duplex") : ("half-duplex"));
609 VLOG_DBG_RL(&rl
, "Port %d Link Down", dev
->port_id
);
615 dpdk_watchdog(void *dummy OVS_UNUSED
)
617 struct netdev_dpdk
*dev
;
619 pthread_detach(pthread_self());
622 ovs_mutex_lock(&dpdk_mutex
);
623 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
624 ovs_mutex_lock(&dev
->mutex
);
625 if (dev
->type
== DPDK_DEV_ETH
) {
626 check_link_status(dev
);
628 ovs_mutex_unlock(&dev
->mutex
);
630 ovs_mutex_unlock(&dpdk_mutex
);
631 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
638 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
642 struct rte_eth_conf conf
= port_conf
;
644 if (dev
->mtu
> ETHER_MTU
) {
645 conf
.rxmode
.jumbo_frame
= 1;
646 conf
.rxmode
.max_rx_pkt_len
= dev
->max_packet_len
;
648 conf
.rxmode
.jumbo_frame
= 0;
649 conf
.rxmode
.max_rx_pkt_len
= 0;
651 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
652 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
653 /* A device may report more queues than it makes available (this has
654 * been observed for Intel xl710, which reserves some of them for
655 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
656 * available. When this happens we can retry the configuration
657 * and request less queues */
658 while (n_rxq
&& n_txq
) {
660 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
663 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
665 VLOG_WARN("Interface %s eth_dev setup error %s\n",
666 dev
->up
.name
, rte_strerror(-diag
));
670 for (i
= 0; i
< n_txq
; i
++) {
671 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
672 dev
->socket_id
, NULL
);
674 VLOG_INFO("Interface %s txq(%d) setup error: %s",
675 dev
->up
.name
, i
, rte_strerror(-diag
));
681 /* Retry with less tx queues */
686 for (i
= 0; i
< n_rxq
; i
++) {
687 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
688 dev
->socket_id
, NULL
,
691 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
692 dev
->up
.name
, i
, rte_strerror(-diag
));
698 /* Retry with less rx queues */
703 dev
->up
.n_rxq
= n_rxq
;
704 dev
->up
.n_txq
= n_txq
;
713 dpdk_eth_checksum_offload_configure(struct netdev_dpdk
*dev
)
714 OVS_REQUIRES(dev
->mutex
)
716 struct rte_eth_dev_info info
;
717 bool rx_csum_ol_flag
= false;
718 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
719 DEV_RX_OFFLOAD_TCP_CKSUM
|
720 DEV_RX_OFFLOAD_IPV4_CKSUM
;
721 rte_eth_dev_info_get(dev
->port_id
, &info
);
722 rx_csum_ol_flag
= (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
724 if (rx_csum_ol_flag
&&
725 (info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
726 rx_chksm_offload_capa
) {
727 VLOG_WARN_ONCE("Rx checksum offload is not supported on device %d",
729 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
732 netdev_request_reconfigure(&dev
->up
);
736 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
738 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
739 VLOG_WARN("Failed to enable flow control on device %d", dev
->port_id
);
744 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
745 OVS_REQUIRES(dev
->mutex
)
747 struct rte_pktmbuf_pool_private
*mbp_priv
;
748 struct rte_eth_dev_info info
;
749 struct ether_addr eth_addr
;
753 rte_eth_dev_info_get(dev
->port_id
, &info
);
755 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
756 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
758 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
760 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
761 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
765 diag
= rte_eth_dev_start(dev
->port_id
);
767 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
768 rte_strerror(-diag
));
772 rte_eth_promiscuous_enable(dev
->port_id
);
773 rte_eth_allmulticast_enable(dev
->port_id
);
775 memset(ð_addr
, 0x0, sizeof(eth_addr
));
776 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
777 VLOG_INFO_RL(&rl
, "Port %d: "ETH_ADDR_FMT
,
778 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
780 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
781 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
783 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
784 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
786 /* Get the Flow control configuration for DPDK-ETH */
787 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
789 VLOG_DBG("cannot get flow control parameters on port=%d, err=%d",
796 static struct netdev_dpdk
*
797 netdev_dpdk_cast(const struct netdev
*netdev
)
799 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
802 static struct netdev
*
803 netdev_dpdk_alloc(void)
805 struct netdev_dpdk
*dev
;
807 dev
= dpdk_rte_mzalloc(sizeof *dev
);
815 static struct dpdk_tx_queue
*
816 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
818 struct dpdk_tx_queue
*txqs
;
821 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
823 for (i
= 0; i
< n_txqs
; i
++) {
824 /* Initialize map for vhost devices. */
825 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
826 rte_spinlock_init(&txqs
[i
].tx_lock
);
834 common_construct(struct netdev
*netdev
, unsigned int port_no
,
835 enum dpdk_dev_type type
, int socket_id
)
836 OVS_REQUIRES(dpdk_mutex
)
838 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
840 ovs_mutex_init(&dev
->mutex
);
842 rte_spinlock_init(&dev
->stats_lock
);
844 /* If the 'sid' is negative, it means that the kernel fails
845 * to obtain the pci numa info. In that situation, always
847 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
848 dev
->requested_socket_id
= dev
->socket_id
;
849 dev
->port_id
= port_no
;
852 dev
->requested_mtu
= ETHER_MTU
;
853 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
854 ovsrcu_index_init(&dev
->vid
, -1);
855 dev
->vhost_reconfigured
= false;
857 ovsrcu_init(&dev
->qos_conf
, NULL
);
859 ovsrcu_init(&dev
->ingress_policer
, NULL
);
860 dev
->policer_rate
= 0;
861 dev
->policer_burst
= 0;
865 dev
->requested_n_rxq
= NR_QUEUE
;
866 dev
->requested_n_txq
= NR_QUEUE
;
867 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
868 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
870 /* Initialize the flow control to NULL */
871 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
873 /* Initilize the hardware offload flags to 0 */
874 dev
->hw_ol_features
= 0;
876 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
878 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
880 netdev_request_reconfigure(netdev
);
885 /* dev_name must be the prefix followed by a positive decimal number.
886 * (no leading + or - signs are allowed) */
888 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
889 unsigned int *port_no
)
893 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
897 cport
= dev_name
+ strlen(prefix
);
899 if (str_to_uint(cport
, 10, port_no
)) {
907 vhost_common_construct(struct netdev
*netdev
)
908 OVS_REQUIRES(dpdk_mutex
)
910 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
911 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
913 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
918 return common_construct(netdev
, -1, DPDK_DEV_VHOST
, socket_id
);
922 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
924 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
925 const char *name
= netdev
->name
;
928 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
929 * the file system. '/' or '\' would traverse directories, so they're not
930 * acceptable in 'name'. */
931 if (strchr(name
, '/') || strchr(name
, '\\')) {
932 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
933 "A valid name must not include '/' or '\\'",
938 ovs_mutex_lock(&dpdk_mutex
);
939 /* Take the name of the vhost-user port and append it to the location where
940 * the socket is to be created, then register the socket.
942 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
943 dpdk_get_vhost_sock_dir(), name
);
945 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
946 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
948 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
951 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
952 VLOG_INFO("Socket %s created for vhost-user port %s\n",
953 dev
->vhost_id
, name
);
955 err
= vhost_common_construct(netdev
);
957 ovs_mutex_unlock(&dpdk_mutex
);
962 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
966 ovs_mutex_lock(&dpdk_mutex
);
967 err
= vhost_common_construct(netdev
);
968 ovs_mutex_unlock(&dpdk_mutex
);
973 netdev_dpdk_construct(struct netdev
*netdev
)
977 ovs_mutex_lock(&dpdk_mutex
);
978 err
= common_construct(netdev
, -1, DPDK_DEV_ETH
, SOCKET0
);
979 ovs_mutex_unlock(&dpdk_mutex
);
984 common_destruct(struct netdev_dpdk
*dev
)
985 OVS_REQUIRES(dpdk_mutex
)
986 OVS_EXCLUDED(dev
->mutex
)
989 dpdk_mp_put(dev
->dpdk_mp
);
991 ovs_list_remove(&dev
->list_node
);
992 free(ovsrcu_get_protected(struct ingress_policer
*,
993 &dev
->ingress_policer
));
994 ovs_mutex_destroy(&dev
->mutex
);
998 netdev_dpdk_destruct(struct netdev
*netdev
)
1000 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1002 ovs_mutex_lock(&dpdk_mutex
);
1004 rte_eth_dev_stop(dev
->port_id
);
1006 common_destruct(dev
);
1008 ovs_mutex_unlock(&dpdk_mutex
);
1011 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1012 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1013 * deadlock, none of the mutexes must be held while calling this function. */
1015 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1017 OVS_EXCLUDED(dpdk_mutex
)
1018 OVS_EXCLUDED(dev
->mutex
)
1020 return rte_vhost_driver_unregister(vhost_id
);
1024 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1026 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1029 ovs_mutex_lock(&dpdk_mutex
);
1031 /* Guest becomes an orphan if still attached. */
1032 if (netdev_dpdk_get_vid(dev
) >= 0
1033 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1034 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1036 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1037 "socket '%s' must be restarted.", dev
->vhost_id
);
1040 vhost_id
= xstrdup(dev
->vhost_id
);
1042 common_destruct(dev
);
1044 ovs_mutex_unlock(&dpdk_mutex
);
1050 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1051 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1052 netdev
->name
, vhost_id
);
1053 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1054 /* OVS server mode - remove this socket from list for deletion */
1055 fatal_signal_remove_file_to_unlink(vhost_id
);
1062 netdev_dpdk_dealloc(struct netdev
*netdev
)
1064 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1070 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1072 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1074 ovs_mutex_lock(&dev
->mutex
);
1076 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1077 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1078 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1079 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1080 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1082 if (dev
->type
== DPDK_DEV_ETH
) {
1083 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1084 dev
->requested_rxq_size
);
1085 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1087 smap_add_format(args
, "requested_txq_descriptors", "%d",
1088 dev
->requested_txq_size
);
1089 smap_add_format(args
, "configured_txq_descriptors", "%d",
1091 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1092 smap_add(args
, "rx_csum_offload", "true");
1095 ovs_mutex_unlock(&dev
->mutex
);
1100 static struct netdev_dpdk
*
1101 netdev_dpdk_lookup_by_port_id(int port_id
)
1102 OVS_REQUIRES(dpdk_mutex
)
1104 struct netdev_dpdk
*dev
;
1106 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1107 if (dev
->port_id
== port_id
) {
1116 netdev_dpdk_process_devargs(const char *devargs
, char **errp
)
1118 uint8_t new_port_id
= UINT8_MAX
;
1120 if (!rte_eth_dev_count()
1121 || rte_eth_dev_get_port_by_name(devargs
, &new_port_id
)
1122 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1123 /* Device not found in DPDK, attempt to attach it */
1124 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1125 /* Attach successful */
1126 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1128 /* Attach unsuccessful */
1129 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1139 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1140 OVS_REQUIRES(dev
->mutex
)
1144 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1145 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1146 dev
->requested_n_rxq
= new_n_rxq
;
1147 netdev_request_reconfigure(&dev
->up
);
1152 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1153 const char *flag
, int default_size
, int *new_size
)
1155 int queue_size
= smap_get_int(args
, flag
, default_size
);
1157 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1158 || !is_pow2(queue_size
)) {
1159 queue_size
= default_size
;
1162 if (queue_size
!= *new_size
) {
1163 *new_size
= queue_size
;
1164 netdev_request_reconfigure(netdev
);
1169 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1172 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1173 bool rx_fc_en
, tx_fc_en
, autoneg
;
1174 enum rte_eth_fc_mode fc_mode
;
1175 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1176 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1177 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1181 const char *new_devargs
;
1184 ovs_mutex_lock(&dpdk_mutex
);
1185 ovs_mutex_lock(&dev
->mutex
);
1187 dpdk_set_rxq_config(dev
, args
);
1189 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1190 NIC_PORT_DEFAULT_RXQ_SIZE
,
1191 &dev
->requested_rxq_size
);
1192 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1193 NIC_PORT_DEFAULT_TXQ_SIZE
,
1194 &dev
->requested_txq_size
);
1196 new_devargs
= smap_get(args
, "dpdk-devargs");
1198 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1199 /* The user requested a new device. If we return error, the caller
1200 * will delete this netdev and try to recreate it. */
1205 /* dpdk-devargs is required for device configuration */
1206 if (new_devargs
&& new_devargs
[0]) {
1207 /* Don't process dpdk-devargs if value is unchanged and port id
1209 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1210 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1211 int new_port_id
= netdev_dpdk_process_devargs(new_devargs
, errp
);
1212 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1214 } else if (new_port_id
== dev
->port_id
) {
1215 /* Already configured, do not reconfigure again */
1218 struct netdev_dpdk
*dup_dev
;
1219 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1221 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1222 "which is already in use by '%s'",
1223 netdev_get_name(netdev
), new_devargs
,
1224 netdev_get_name(&dup_dev
->up
));
1227 int sid
= rte_eth_dev_socket_id(new_port_id
);
1228 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1229 dev
->devargs
= xstrdup(new_devargs
);
1230 dev
->port_id
= new_port_id
;
1231 netdev_request_reconfigure(&dev
->up
);
1237 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1238 "The old 'dpdk<port_id>' names are not supported",
1239 netdev_get_name(netdev
));
1247 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1248 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1249 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1251 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1252 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1253 dev
->fc_conf
.mode
= fc_mode
;
1254 dev
->fc_conf
.autoneg
= autoneg
;
1255 dpdk_eth_flow_ctrl_setup(dev
);
1258 /* Rx checksum offload configuration */
1259 /* By default the Rx checksum offload is ON */
1260 rx_chksm_ofld
= smap_get_bool(args
, "rx-checksum-offload", true);
1261 temp_flag
= (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
)
1263 if (temp_flag
!= rx_chksm_ofld
) {
1264 dev
->hw_ol_features
^= NETDEV_RX_CHECKSUM_OFFLOAD
;
1265 dpdk_eth_checksum_offload_configure(dev
);
1269 ovs_mutex_unlock(&dev
->mutex
);
1270 ovs_mutex_unlock(&dpdk_mutex
);
1276 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1277 char **errp OVS_UNUSED
)
1279 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1281 ovs_mutex_lock(&dev
->mutex
);
1282 dpdk_set_rxq_config(dev
, args
);
1283 ovs_mutex_unlock(&dev
->mutex
);
1289 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1290 const struct smap
*args
,
1291 char **errp OVS_UNUSED
)
1293 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1296 ovs_mutex_lock(&dev
->mutex
);
1297 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1298 path
= smap_get(args
, "vhost-server-path");
1299 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1300 strcpy(dev
->vhost_id
, path
);
1301 netdev_request_reconfigure(netdev
);
1304 ovs_mutex_unlock(&dev
->mutex
);
1310 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1312 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1314 return dev
->socket_id
;
1317 /* Sets the number of tx queues for the dpdk interface. */
1319 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1321 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1323 ovs_mutex_lock(&dev
->mutex
);
1325 if (dev
->requested_n_txq
== n_txq
) {
1329 dev
->requested_n_txq
= n_txq
;
1330 netdev_request_reconfigure(netdev
);
1333 ovs_mutex_unlock(&dev
->mutex
);
1337 static struct netdev_rxq
*
1338 netdev_dpdk_rxq_alloc(void)
1340 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1349 static struct netdev_rxq_dpdk
*
1350 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1352 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1356 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1358 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1359 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1361 ovs_mutex_lock(&dev
->mutex
);
1362 rx
->port_id
= dev
->port_id
;
1363 ovs_mutex_unlock(&dev
->mutex
);
1369 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1374 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1376 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1381 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1382 * 'pkts', even in case of failure.
1384 * Returns the number of packets that weren't transmitted. */
1386 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1387 struct rte_mbuf
**pkts
, int cnt
)
1391 while (nb_tx
!= cnt
) {
1394 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1402 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1403 /* Free buffers, which we couldn't transmit, one at a time (each
1404 * packet could come from a different mempool) */
1407 for (i
= nb_tx
; i
< cnt
; i
++) {
1408 rte_pktmbuf_free(pkts
[i
]);
1416 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1417 struct rte_mbuf
*pkt
, uint64_t time
)
1419 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1421 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1426 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1427 struct rte_mbuf
**pkts
, int pkt_cnt
)
1431 struct rte_mbuf
*pkt
= NULL
;
1432 uint64_t current_time
= rte_rdtsc();
1434 for (i
= 0; i
< pkt_cnt
; i
++) {
1436 /* Handle current packet */
1437 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1443 rte_pktmbuf_free(pkt
);
1451 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1456 rte_spinlock_lock(&policer
->policer_lock
);
1457 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
, pkt_cnt
);
1458 rte_spinlock_unlock(&policer
->policer_lock
);
1464 is_vhost_running(struct netdev_dpdk
*dev
)
1466 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1470 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1471 unsigned int packet_size
)
1473 /* Hard-coded search for the size bucket. */
1474 if (packet_size
< 256) {
1475 if (packet_size
>= 128) {
1476 stats
->rx_128_to_255_packets
++;
1477 } else if (packet_size
<= 64) {
1478 stats
->rx_1_to_64_packets
++;
1480 stats
->rx_65_to_127_packets
++;
1483 if (packet_size
>= 1523) {
1484 stats
->rx_1523_to_max_packets
++;
1485 } else if (packet_size
>= 1024) {
1486 stats
->rx_1024_to_1522_packets
++;
1487 } else if (packet_size
< 512) {
1488 stats
->rx_256_to_511_packets
++;
1490 stats
->rx_512_to_1023_packets
++;
1496 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1497 struct dp_packet
**packets
, int count
,
1501 unsigned int packet_size
;
1502 struct dp_packet
*packet
;
1504 stats
->rx_packets
+= count
;
1505 stats
->rx_dropped
+= dropped
;
1506 for (i
= 0; i
< count
; i
++) {
1507 packet
= packets
[i
];
1508 packet_size
= dp_packet_size(packet
);
1510 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1511 /* This only protects the following multicast counting from
1512 * too short packets, but it does not stop the packet from
1513 * further processing. */
1515 stats
->rx_length_errors
++;
1519 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1521 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1522 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1526 stats
->rx_bytes
+= packet_size
;
1531 * The receive path for the vhost port is the TX path out from guest.
1534 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1535 struct dp_packet_batch
*batch
)
1537 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1538 int qid
= rxq
->queue_id
;
1539 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1541 uint16_t dropped
= 0;
1543 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1544 || !(dev
->flags
& NETDEV_UP
))) {
1548 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1549 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1551 (struct rte_mbuf
**) batch
->packets
,
1559 nb_rx
= ingress_policer_run(policer
,
1560 (struct rte_mbuf
**) batch
->packets
,
1565 rte_spinlock_lock(&dev
->stats_lock
);
1566 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1568 rte_spinlock_unlock(&dev
->stats_lock
);
1570 batch
->count
= (int) nb_rx
;
1575 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1577 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1578 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1579 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1583 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1587 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1588 (struct rte_mbuf
**) batch
->packets
,
1596 nb_rx
= ingress_policer_run(policer
,
1597 (struct rte_mbuf
**) batch
->packets
,
1602 /* Update stats to reflect dropped packets */
1603 if (OVS_UNLIKELY(dropped
)) {
1604 rte_spinlock_lock(&dev
->stats_lock
);
1605 dev
->stats
.rx_dropped
+= dropped
;
1606 rte_spinlock_unlock(&dev
->stats_lock
);
1609 batch
->count
= nb_rx
;
1615 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1618 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1621 rte_spinlock_lock(&qos_conf
->lock
);
1622 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
);
1623 rte_spinlock_unlock(&qos_conf
->lock
);
1630 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1635 struct rte_mbuf
*pkt
;
1637 for (i
= 0; i
< pkt_cnt
; i
++) {
1639 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1640 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1641 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1642 rte_pktmbuf_free(pkt
);
1646 if (OVS_UNLIKELY(i
!= cnt
)) {
1656 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1657 struct dp_packet
**packets
,
1662 int sent
= attempted
- dropped
;
1664 stats
->tx_packets
+= sent
;
1665 stats
->tx_dropped
+= dropped
;
1667 for (i
= 0; i
< sent
; i
++) {
1668 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1673 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1674 struct dp_packet
**pkts
, int cnt
)
1676 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1677 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1678 unsigned int total_pkts
= cnt
;
1679 unsigned int dropped
= 0;
1682 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1684 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1685 || !(dev
->flags
& NETDEV_UP
))) {
1686 rte_spinlock_lock(&dev
->stats_lock
);
1687 dev
->stats
.tx_dropped
+= cnt
;
1688 rte_spinlock_unlock(&dev
->stats_lock
);
1692 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1694 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1695 /* Check has QoS has been configured for the netdev */
1696 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
);
1697 dropped
= total_pkts
- cnt
;
1700 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1701 unsigned int tx_pkts
;
1703 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1704 vhost_qid
, cur_pkts
, cnt
);
1705 if (OVS_LIKELY(tx_pkts
)) {
1706 /* Packets have been sent.*/
1708 /* Prepare for possible retry.*/
1709 cur_pkts
= &cur_pkts
[tx_pkts
];
1711 /* No packets sent - do not retry.*/
1714 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1716 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1718 rte_spinlock_lock(&dev
->stats_lock
);
1719 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1721 rte_spinlock_unlock(&dev
->stats_lock
);
1724 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1725 dp_packet_delete(pkts
[i
]);
1729 /* Tx function. Transmit packets indefinitely */
1731 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1732 OVS_NO_THREAD_SAFETY_ANALYSIS
1734 #if !defined(__CHECKER__) && !defined(_WIN32)
1735 const size_t PKT_ARRAY_SIZE
= batch
->count
;
1737 /* Sparse or MSVC doesn't like variable length array. */
1738 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1740 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1741 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1746 dp_packet_batch_apply_cutlen(batch
);
1748 for (i
= 0; i
< batch
->count
; i
++) {
1749 int size
= dp_packet_size(batch
->packets
[i
]);
1751 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1752 VLOG_WARN_RL(&rl
, "Too big size %d max_packet_len %d",
1753 (int) size
, dev
->max_packet_len
);
1759 pkts
[newcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1761 if (!pkts
[newcnt
]) {
1762 dropped
+= batch
->count
- i
;
1766 /* We have to do a copy for now */
1767 memcpy(rte_pktmbuf_mtod(pkts
[newcnt
], void *),
1768 dp_packet_data(batch
->packets
[i
]), size
);
1770 rte_pktmbuf_data_len(pkts
[newcnt
]) = size
;
1771 rte_pktmbuf_pkt_len(pkts
[newcnt
]) = size
;
1776 if (dev
->type
== DPDK_DEV_VHOST
) {
1777 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1780 unsigned int qos_pkts
= newcnt
;
1782 /* Check if QoS has been configured for this netdev. */
1783 newcnt
= netdev_dpdk_qos_run(dev
, pkts
, newcnt
);
1785 dropped
+= qos_pkts
- newcnt
;
1786 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, newcnt
);
1789 if (OVS_UNLIKELY(dropped
)) {
1790 rte_spinlock_lock(&dev
->stats_lock
);
1791 dev
->stats
.tx_dropped
+= dropped
;
1792 rte_spinlock_unlock(&dev
->stats_lock
);
1797 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1798 struct dp_packet_batch
*batch
,
1799 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1802 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1803 dpdk_do_tx_copy(netdev
, qid
, batch
);
1804 dp_packet_delete_batch(batch
, may_steal
);
1806 dp_packet_batch_apply_cutlen(batch
);
1807 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1813 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1814 struct dp_packet_batch
*batch
, bool may_steal
,
1815 bool concurrent_txq
)
1817 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1818 dp_packet_delete_batch(batch
, may_steal
);
1822 if (OVS_UNLIKELY(concurrent_txq
)) {
1823 qid
= qid
% dev
->up
.n_txq
;
1824 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1827 if (OVS_UNLIKELY(!may_steal
||
1828 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1829 struct netdev
*netdev
= &dev
->up
;
1831 dpdk_do_tx_copy(netdev
, qid
, batch
);
1832 dp_packet_delete_batch(batch
, may_steal
);
1835 int cnt
= batch
->count
;
1836 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1838 dp_packet_batch_apply_cutlen(batch
);
1840 cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, cnt
);
1841 cnt
= netdev_dpdk_qos_run(dev
, pkts
, cnt
);
1842 dropped
= batch
->count
- cnt
;
1844 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, cnt
);
1846 if (OVS_UNLIKELY(dropped
)) {
1847 rte_spinlock_lock(&dev
->stats_lock
);
1848 dev
->stats
.tx_dropped
+= dropped
;
1849 rte_spinlock_unlock(&dev
->stats_lock
);
1853 if (OVS_UNLIKELY(concurrent_txq
)) {
1854 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1859 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1860 struct dp_packet_batch
*batch
, bool may_steal
,
1861 bool concurrent_txq
)
1863 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1865 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1870 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1872 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1874 ovs_mutex_lock(&dev
->mutex
);
1875 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1877 netdev_change_seq_changed(netdev
);
1879 ovs_mutex_unlock(&dev
->mutex
);
1885 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1887 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1889 ovs_mutex_lock(&dev
->mutex
);
1891 ovs_mutex_unlock(&dev
->mutex
);
1897 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1899 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1901 ovs_mutex_lock(&dev
->mutex
);
1903 ovs_mutex_unlock(&dev
->mutex
);
1909 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
1911 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1913 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
1914 || mtu
< ETHER_MIN_MTU
) {
1915 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
1919 ovs_mutex_lock(&dev
->mutex
);
1920 if (dev
->requested_mtu
!= mtu
) {
1921 dev
->requested_mtu
= mtu
;
1922 netdev_request_reconfigure(netdev
);
1924 ovs_mutex_unlock(&dev
->mutex
);
1930 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
1933 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
1934 struct netdev_stats
*stats
)
1936 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1938 ovs_mutex_lock(&dev
->mutex
);
1940 rte_spinlock_lock(&dev
->stats_lock
);
1941 /* Supported Stats */
1942 stats
->rx_packets
+= dev
->stats
.rx_packets
;
1943 stats
->tx_packets
+= dev
->stats
.tx_packets
;
1944 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
1945 stats
->tx_dropped
+= dev
->stats
.tx_dropped
;
1946 stats
->multicast
= dev
->stats
.multicast
;
1947 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
1948 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
1949 stats
->rx_errors
= dev
->stats
.rx_errors
;
1950 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
1952 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
1953 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
1954 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
1955 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
1956 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
1957 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
1958 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
1960 rte_spinlock_unlock(&dev
->stats_lock
);
1962 ovs_mutex_unlock(&dev
->mutex
);
1968 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
1969 const struct rte_eth_xstat
*xstats
,
1970 const struct rte_eth_xstat_name
*names
,
1971 const unsigned int size
)
1973 for (unsigned int i
= 0; i
< size
; i
++) {
1974 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
1975 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
1976 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
1977 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
1978 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
1979 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
1980 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
1981 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
1982 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
1983 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
1984 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
1985 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
1986 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
1987 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
1988 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
1989 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
1990 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
1991 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
1992 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
1993 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
1994 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
1995 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
1996 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
1997 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
1998 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
1999 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2000 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2001 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2002 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2003 stats
->multicast
= xstats
[i
].value
;
2004 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2005 stats
->tx_multicast_packets
= xstats
[i
].value
;
2006 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2007 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2008 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2009 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2010 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2011 stats
->rx_undersized_errors
= xstats
[i
].value
;
2012 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2013 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2014 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2015 stats
->rx_jabber_errors
= xstats
[i
].value
;
2021 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2023 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2024 struct rte_eth_stats rte_stats
;
2027 netdev_dpdk_get_carrier(netdev
, &gg
);
2028 ovs_mutex_lock(&dev
->mutex
);
2030 struct rte_eth_xstat
*rte_xstats
= NULL
;
2031 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2032 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2034 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2035 VLOG_ERR("Can't get ETH statistics for port: %i.", dev
->port_id
);
2036 ovs_mutex_unlock(&dev
->mutex
);
2040 /* Get length of statistics */
2041 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2042 if (rte_xstats_len
< 0) {
2043 VLOG_WARN("Cannot get XSTATS values for port: %i", dev
->port_id
);
2046 /* Reserve memory for xstats names and values */
2047 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2048 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2050 /* Retreive xstats names */
2051 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2054 if (rte_xstats_new_len
!= rte_xstats_len
) {
2055 VLOG_WARN("Cannot get XSTATS names for port: %i.", dev
->port_id
);
2058 /* Retreive xstats values */
2059 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2060 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2062 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2063 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2066 VLOG_WARN("Cannot get XSTATS values for port: %i.", dev
->port_id
);
2071 free(rte_xstats_names
);
2073 stats
->rx_packets
= rte_stats
.ipackets
;
2074 stats
->tx_packets
= rte_stats
.opackets
;
2075 stats
->rx_bytes
= rte_stats
.ibytes
;
2076 stats
->tx_bytes
= rte_stats
.obytes
;
2077 stats
->rx_errors
= rte_stats
.ierrors
;
2078 stats
->tx_errors
= rte_stats
.oerrors
;
2080 rte_spinlock_lock(&dev
->stats_lock
);
2081 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2082 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2083 rte_spinlock_unlock(&dev
->stats_lock
);
2085 /* These are the available DPDK counters for packets not received due to
2086 * local resource constraints in DPDK and NIC respectively. */
2087 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2088 stats
->rx_missed_errors
= rte_stats
.imissed
;
2090 ovs_mutex_unlock(&dev
->mutex
);
2096 netdev_dpdk_get_features(const struct netdev
*netdev
,
2097 enum netdev_features
*current
,
2098 enum netdev_features
*advertised
,
2099 enum netdev_features
*supported
,
2100 enum netdev_features
*peer
)
2102 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2103 struct rte_eth_link link
;
2105 ovs_mutex_lock(&dev
->mutex
);
2107 ovs_mutex_unlock(&dev
->mutex
);
2109 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2110 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2111 *current
= NETDEV_F_10MB_HD
;
2113 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2114 *current
= NETDEV_F_100MB_HD
;
2116 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2117 *current
= NETDEV_F_1GB_HD
;
2119 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2120 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2121 *current
= NETDEV_F_10MB_FD
;
2123 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2124 *current
= NETDEV_F_100MB_FD
;
2126 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2127 *current
= NETDEV_F_1GB_FD
;
2129 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2130 *current
= NETDEV_F_10GB_FD
;
2134 if (link
.link_autoneg
) {
2135 *current
|= NETDEV_F_AUTONEG
;
2138 *advertised
= *supported
= *peer
= 0;
2143 static struct ingress_policer
*
2144 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2146 struct ingress_policer
*policer
= NULL
;
2147 uint64_t rate_bytes
;
2148 uint64_t burst_bytes
;
2151 policer
= xmalloc(sizeof *policer
);
2152 rte_spinlock_init(&policer
->policer_lock
);
2154 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2155 rate_bytes
= rate
* 1000/8;
2156 burst_bytes
= burst
* 1000/8;
2158 policer
->app_srtcm_params
.cir
= rate_bytes
;
2159 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2160 policer
->app_srtcm_params
.ebs
= 0;
2161 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2162 &policer
->app_srtcm_params
);
2164 VLOG_ERR("Could not create rte meter for ingress policer");
2172 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2173 uint32_t policer_burst
)
2175 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2176 struct ingress_policer
*policer
;
2178 /* Force to 0 if no rate specified,
2179 * default to 8000 kbits if burst is 0,
2180 * else stick with user-specified value.
2182 policer_burst
= (!policer_rate
? 0
2183 : !policer_burst
? 8000
2186 ovs_mutex_lock(&dev
->mutex
);
2188 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2189 &dev
->ingress_policer
);
2191 if (dev
->policer_rate
== policer_rate
&&
2192 dev
->policer_burst
== policer_burst
) {
2193 /* Assume that settings haven't changed since we last set them. */
2194 ovs_mutex_unlock(&dev
->mutex
);
2198 /* Destroy any existing ingress policer for the device if one exists */
2200 ovsrcu_postpone(free
, policer
);
2203 if (policer_rate
!= 0) {
2204 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2208 ovsrcu_set(&dev
->ingress_policer
, policer
);
2209 dev
->policer_rate
= policer_rate
;
2210 dev
->policer_burst
= policer_burst
;
2211 ovs_mutex_unlock(&dev
->mutex
);
2217 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2219 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2221 ovs_mutex_lock(&dev
->mutex
);
2222 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2223 * postive integer to meet RFC 2863 recommendations.
2225 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2226 ovs_mutex_unlock(&dev
->mutex
);
2232 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2234 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2236 ovs_mutex_lock(&dev
->mutex
);
2237 check_link_status(dev
);
2238 *carrier
= dev
->link
.link_status
;
2240 ovs_mutex_unlock(&dev
->mutex
);
2246 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2248 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2250 ovs_mutex_lock(&dev
->mutex
);
2252 if (is_vhost_running(dev
)) {
2258 ovs_mutex_unlock(&dev
->mutex
);
2263 static long long int
2264 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2266 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2267 long long int carrier_resets
;
2269 ovs_mutex_lock(&dev
->mutex
);
2270 carrier_resets
= dev
->link_reset_cnt
;
2271 ovs_mutex_unlock(&dev
->mutex
);
2273 return carrier_resets
;
2277 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2278 long long int interval OVS_UNUSED
)
2284 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2285 enum netdev_flags off
, enum netdev_flags on
,
2286 enum netdev_flags
*old_flagsp
)
2287 OVS_REQUIRES(dev
->mutex
)
2289 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2293 *old_flagsp
= dev
->flags
;
2297 if (dev
->flags
== *old_flagsp
) {
2301 if (dev
->type
== DPDK_DEV_ETH
) {
2302 if (dev
->flags
& NETDEV_PROMISC
) {
2303 rte_eth_promiscuous_enable(dev
->port_id
);
2306 netdev_change_seq_changed(&dev
->up
);
2308 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2309 * running then change netdev's change_seq to trigger link state
2312 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2313 && is_vhost_running(dev
)) {
2314 netdev_change_seq_changed(&dev
->up
);
2316 /* Clear statistics if device is getting up. */
2317 if (NETDEV_UP
& on
) {
2318 rte_spinlock_lock(&dev
->stats_lock
);
2319 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2320 rte_spinlock_unlock(&dev
->stats_lock
);
2329 netdev_dpdk_update_flags(struct netdev
*netdev
,
2330 enum netdev_flags off
, enum netdev_flags on
,
2331 enum netdev_flags
*old_flagsp
)
2333 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2336 ovs_mutex_lock(&dev
->mutex
);
2337 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2338 ovs_mutex_unlock(&dev
->mutex
);
2344 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2346 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2347 struct rte_eth_dev_info dev_info
;
2349 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2353 ovs_mutex_lock(&dev
->mutex
);
2354 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2355 ovs_mutex_unlock(&dev
->mutex
);
2357 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2358 smap_add_format(args
, "numa_id", "%d",
2359 rte_eth_dev_socket_id(dev
->port_id
));
2360 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2361 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2362 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2363 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2364 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2365 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2366 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2367 dev_info
.max_hash_mac_addrs
);
2368 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2369 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2371 if (dev_info
.pci_dev
) {
2372 smap_add_format(args
, "pci-vendor_id", "0x%u",
2373 dev_info
.pci_dev
->id
.vendor_id
);
2374 smap_add_format(args
, "pci-device_id", "0x%x",
2375 dev_info
.pci_dev
->id
.device_id
);
2382 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2383 OVS_REQUIRES(dev
->mutex
)
2385 enum netdev_flags old_flags
;
2388 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2390 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2395 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2396 const char *argv
[], void *aux OVS_UNUSED
)
2400 if (!strcasecmp(argv
[argc
- 1], "up")) {
2402 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2405 unixctl_command_reply_error(conn
, "Invalid Admin State");
2410 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2411 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2412 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2414 ovs_mutex_lock(&dpdk_dev
->mutex
);
2415 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2416 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2418 netdev_close(netdev
);
2420 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2421 netdev_close(netdev
);
2425 struct netdev_dpdk
*netdev
;
2427 ovs_mutex_lock(&dpdk_mutex
);
2428 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2429 ovs_mutex_lock(&netdev
->mutex
);
2430 netdev_dpdk_set_admin_state__(netdev
, up
);
2431 ovs_mutex_unlock(&netdev
->mutex
);
2433 ovs_mutex_unlock(&dpdk_mutex
);
2435 unixctl_command_reply(conn
, "OK");
2439 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2440 const char *argv
[], void *aux OVS_UNUSED
)
2445 char devname
[RTE_ETH_NAME_MAX_LEN
];
2446 struct netdev_dpdk
*dev
;
2448 ovs_mutex_lock(&dpdk_mutex
);
2450 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv
[1],
2452 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2456 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2458 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2459 "Remove it before detaching",
2460 argv
[1], netdev_get_name(&dev
->up
));
2464 rte_eth_dev_close(port_id
);
2466 ret
= rte_eth_dev_detach(port_id
, devname
);
2468 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2472 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2474 ovs_mutex_unlock(&dpdk_mutex
);
2475 unixctl_command_reply(conn
, response
);
2480 ovs_mutex_unlock(&dpdk_mutex
);
2481 unixctl_command_reply_error(conn
, response
);
2486 * Set virtqueue flags so that we do not receive interrupts.
2489 set_irq_status(int vid
)
2494 for (i
= 0; i
< rte_vhost_get_queue_num(vid
); i
++) {
2495 idx
= i
* VIRTIO_QNUM
;
2496 rte_vhost_enable_guest_notification(vid
, idx
+ VIRTIO_RXQ
, 0);
2497 rte_vhost_enable_guest_notification(vid
, idx
+ VIRTIO_TXQ
, 0);
2502 * Fixes mapping for vhost-user tx queues. Must be called after each
2503 * enabling/disabling of queues and n_txq modifications.
2506 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2507 OVS_REQUIRES(dev
->mutex
)
2509 int *enabled_queues
, n_enabled
= 0;
2510 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2512 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2514 for (i
= 0; i
< total_txqs
; i
++) {
2515 /* Enabled queues always mapped to themselves. */
2516 if (dev
->tx_q
[i
].map
== i
) {
2517 enabled_queues
[n_enabled
++] = i
;
2521 if (n_enabled
== 0 && total_txqs
!= 0) {
2522 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2527 for (i
= 0; i
< total_txqs
; i
++) {
2528 if (dev
->tx_q
[i
].map
!= i
) {
2529 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2530 k
= (k
+ 1) % n_enabled
;
2534 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2535 for (i
= 0; i
< total_txqs
; i
++) {
2536 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2539 free(enabled_queues
);
2543 * A new virtio-net device is added to a vhost port.
2548 struct netdev_dpdk
*dev
;
2549 bool exists
= false;
2551 char ifname
[IF_NAME_SZ
];
2553 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2555 ovs_mutex_lock(&dpdk_mutex
);
2556 /* Add device to the vhost port with the same name as that passed down. */
2557 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2558 ovs_mutex_lock(&dev
->mutex
);
2559 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2560 uint32_t qp_num
= rte_vhost_get_queue_num(vid
);
2562 /* Get NUMA information */
2563 newnode
= rte_vhost_get_numa_node(vid
);
2564 if (newnode
== -1) {
2566 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2569 newnode
= dev
->socket_id
;
2572 if (dev
->requested_n_txq
!= qp_num
2573 || dev
->requested_n_rxq
!= qp_num
2574 || dev
->requested_socket_id
!= newnode
) {
2575 dev
->requested_socket_id
= newnode
;
2576 dev
->requested_n_rxq
= qp_num
;
2577 dev
->requested_n_txq
= qp_num
;
2578 netdev_request_reconfigure(&dev
->up
);
2580 /* Reconfiguration not required. */
2581 dev
->vhost_reconfigured
= true;
2584 ovsrcu_index_set(&dev
->vid
, vid
);
2587 /* Disable notifications. */
2588 set_irq_status(vid
);
2589 netdev_change_seq_changed(&dev
->up
);
2590 ovs_mutex_unlock(&dev
->mutex
);
2593 ovs_mutex_unlock(&dev
->mutex
);
2595 ovs_mutex_unlock(&dpdk_mutex
);
2598 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2603 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2609 /* Clears mapping for all available queues of vhost interface. */
2611 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2612 OVS_REQUIRES(dev
->mutex
)
2616 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2617 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2622 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2623 * flag to stop any more packets from being sent or received to/from a VM and
2624 * ensure all currently queued packets have been sent/received before removing
2628 destroy_device(int vid
)
2630 struct netdev_dpdk
*dev
;
2631 bool exists
= false;
2632 char ifname
[IF_NAME_SZ
];
2634 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2636 ovs_mutex_lock(&dpdk_mutex
);
2637 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2638 if (netdev_dpdk_get_vid(dev
) == vid
) {
2640 ovs_mutex_lock(&dev
->mutex
);
2641 dev
->vhost_reconfigured
= false;
2642 ovsrcu_index_set(&dev
->vid
, -1);
2643 netdev_dpdk_txq_map_clear(dev
);
2645 netdev_change_seq_changed(&dev
->up
);
2646 ovs_mutex_unlock(&dev
->mutex
);
2652 ovs_mutex_unlock(&dpdk_mutex
);
2656 * Wait for other threads to quiesce after setting the 'virtio_dev'
2657 * to NULL, before returning.
2659 ovsrcu_synchronize();
2661 * As call to ovsrcu_synchronize() will end the quiescent state,
2662 * put thread back into quiescent state before returning.
2664 ovsrcu_quiesce_start();
2665 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2667 VLOG_INFO("vHost Device '%s' not found", ifname
);
2672 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2674 struct netdev_dpdk
*dev
;
2675 bool exists
= false;
2676 int qid
= queue_id
/ VIRTIO_QNUM
;
2677 char ifname
[IF_NAME_SZ
];
2679 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2681 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2685 ovs_mutex_lock(&dpdk_mutex
);
2686 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2687 ovs_mutex_lock(&dev
->mutex
);
2688 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2690 dev
->tx_q
[qid
].map
= qid
;
2692 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2694 netdev_dpdk_remap_txqs(dev
);
2696 ovs_mutex_unlock(&dev
->mutex
);
2699 ovs_mutex_unlock(&dev
->mutex
);
2701 ovs_mutex_unlock(&dpdk_mutex
);
2704 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2705 "changed to \'%s\'", queue_id
, qid
, ifname
,
2706 (enable
== 1) ? "enabled" : "disabled");
2708 VLOG_INFO("vHost Device '%s' not found", ifname
);
2716 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2718 return ovsrcu_index_get(&dev
->vid
);
2721 struct ingress_policer
*
2722 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2724 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2728 * These callbacks allow virtio-net devices to be added to vhost ports when
2729 * configuration has been fully complete.
2731 static const struct virtio_net_device_ops virtio_net_device_ops
=
2733 .new_device
= new_device
,
2734 .destroy_device
= destroy_device
,
2735 .vring_state_changed
= vring_state_changed
2739 start_vhost_loop(void *dummy OVS_UNUSED
)
2741 pthread_detach(pthread_self());
2742 /* Put the vhost thread into quiescent state. */
2743 ovsrcu_quiesce_start();
2744 rte_vhost_driver_session_start();
2749 netdev_dpdk_class_init(void)
2751 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2753 /* This function can be called for different classes. The initialization
2754 * needs to be done only once */
2755 if (ovsthread_once_start(&once
)) {
2756 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2757 unixctl_command_register("netdev-dpdk/set-admin-state",
2758 "[netdev] up|down", 1, 2,
2759 netdev_dpdk_set_admin_state
, NULL
);
2760 unixctl_command_register("netdev-dpdk/detach",
2761 "pci address of device", 1, 1,
2762 netdev_dpdk_detach
, NULL
);
2764 ovsthread_once_done(&once
);
2771 netdev_dpdk_vhost_class_init(void)
2773 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2775 /* This function can be called for different classes. The initialization
2776 * needs to be done only once */
2777 if (ovsthread_once_start(&once
)) {
2778 rte_vhost_driver_callback_register(&virtio_net_device_ops
);
2779 rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_HOST_TSO4
2780 | 1ULL << VIRTIO_NET_F_HOST_TSO6
2781 | 1ULL << VIRTIO_NET_F_CSUM
);
2782 ovs_thread_create("vhost_thread", start_vhost_loop
, NULL
);
2784 ovsthread_once_done(&once
);
2793 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2794 unsigned int *eth_port_id
)
2796 struct dpdk_ring
*ring_pair
;
2800 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2805 /* XXX: Add support for multiquque ring. */
2806 ring_name
= xasprintf("%s_tx", dev_name
);
2808 /* Create single producer tx ring, netdev does explicit locking. */
2809 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2812 if (ring_pair
->cring_tx
== NULL
) {
2813 rte_free(ring_pair
);
2817 ring_name
= xasprintf("%s_rx", dev_name
);
2819 /* Create single consumer rx ring, netdev does explicit locking. */
2820 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2823 if (ring_pair
->cring_rx
== NULL
) {
2824 rte_free(ring_pair
);
2828 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2829 &ring_pair
->cring_tx
, 1, SOCKET0
);
2832 rte_free(ring_pair
);
2836 ring_pair
->user_port_id
= port_no
;
2837 ring_pair
->eth_port_id
= port_id
;
2838 *eth_port_id
= port_id
;
2840 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2846 dpdk_ring_open(const char dev_name
[], unsigned int *eth_port_id
)
2847 OVS_REQUIRES(dpdk_mutex
)
2849 struct dpdk_ring
*ring_pair
;
2850 unsigned int port_no
;
2853 /* Names always start with "dpdkr" */
2854 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2859 /* Look through our list to find the device */
2860 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2861 if (ring_pair
->user_port_id
== port_no
) {
2862 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2863 /* Really all that is needed */
2864 *eth_port_id
= ring_pair
->eth_port_id
;
2868 /* Need to create the device rings */
2869 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2873 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2874 struct dp_packet_batch
*batch
, bool may_steal
,
2875 bool concurrent_txq
)
2877 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2880 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2881 * the rss hash field is clear. This is because the same mbuf may be
2882 * modified by the consumer of the ring and return into the datapath
2883 * without recalculating the RSS hash. */
2884 for (i
= 0; i
< batch
->count
; i
++) {
2885 dp_packet_rss_invalidate(batch
->packets
[i
]);
2888 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2893 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2895 unsigned int port_no
= 0;
2898 ovs_mutex_lock(&dpdk_mutex
);
2900 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2905 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2906 rte_eth_dev_socket_id(port_no
));
2908 ovs_mutex_unlock(&dpdk_mutex
);
2915 * Initialize QoS configuration operations.
2918 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2921 rte_spinlock_init(&conf
->lock
);
2925 * Search existing QoS operations in qos_ops and compare each set of
2926 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2929 static const struct dpdk_qos_ops
*
2930 qos_lookup_name(const char *name
)
2932 const struct dpdk_qos_ops
*const *opsp
;
2934 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2935 const struct dpdk_qos_ops
*ops
= *opsp
;
2936 if (!strcmp(name
, ops
->qos_name
)) {
2944 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2947 const struct dpdk_qos_ops
*const *opsp
;
2949 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2950 const struct dpdk_qos_ops
*ops
= *opsp
;
2951 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
2952 sset_add(types
, ops
->qos_name
);
2959 netdev_dpdk_get_qos(const struct netdev
*netdev
,
2960 const char **typep
, struct smap
*details
)
2962 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2963 struct qos_conf
*qos_conf
;
2966 ovs_mutex_lock(&dev
->mutex
);
2967 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
2969 *typep
= qos_conf
->ops
->qos_name
;
2970 error
= (qos_conf
->ops
->qos_get
2971 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
2973 /* No QoS configuration set, return an empty string */
2976 ovs_mutex_unlock(&dev
->mutex
);
2982 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
2983 const struct smap
*details
)
2985 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2986 const struct dpdk_qos_ops
*new_ops
= NULL
;
2987 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
2990 ovs_mutex_lock(&dev
->mutex
);
2992 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
2994 new_ops
= qos_lookup_name(type
);
2996 if (!new_ops
|| !new_ops
->qos_construct
) {
2997 new_qos_conf
= NULL
;
2998 if (type
&& type
[0]) {
3001 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3002 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3003 new_qos_conf
= qos_conf
;
3005 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3009 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3010 type
, netdev
->name
, rte_strerror(error
));
3013 if (new_qos_conf
!= qos_conf
) {
3014 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3016 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3020 ovs_mutex_unlock(&dev
->mutex
);
3025 /* egress-policer details */
3027 struct egress_policer
{
3028 struct qos_conf qos_conf
;
3029 struct rte_meter_srtcm_params app_srtcm_params
;
3030 struct rte_meter_srtcm egress_meter
;
3034 egress_policer_details_to_param(const struct smap
*details
,
3035 struct rte_meter_srtcm_params
*params
)
3037 memset(params
, 0, sizeof *params
);
3038 params
->cir
= smap_get_ullong(details
, "cir", 0);
3039 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3044 egress_policer_qos_construct(const struct smap
*details
,
3045 struct qos_conf
**conf
)
3047 struct egress_policer
*policer
;
3050 policer
= xmalloc(sizeof *policer
);
3051 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3052 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3053 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3054 &policer
->app_srtcm_params
);
3056 *conf
= &policer
->qos_conf
;
3067 egress_policer_qos_destruct(struct qos_conf
*conf
)
3069 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3075 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3077 struct egress_policer
*policer
=
3078 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3080 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3081 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3087 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3088 const struct smap
*details
)
3090 struct egress_policer
*policer
=
3091 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3092 struct rte_meter_srtcm_params params
;
3094 egress_policer_details_to_param(details
, ¶ms
);
3096 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3100 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
)
3103 struct egress_policer
*policer
=
3104 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3106 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
, pkt_cnt
);
3111 static const struct dpdk_qos_ops egress_policer_ops
= {
3112 "egress-policer", /* qos_name */
3113 egress_policer_qos_construct
,
3114 egress_policer_qos_destruct
,
3115 egress_policer_qos_get
,
3116 egress_policer_qos_is_equal
,
3121 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3123 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3126 ovs_mutex_lock(&dev
->mutex
);
3128 if (netdev
->n_txq
== dev
->requested_n_txq
3129 && netdev
->n_rxq
== dev
->requested_n_rxq
3130 && dev
->mtu
== dev
->requested_mtu
3131 && dev
->rxq_size
== dev
->requested_rxq_size
3132 && dev
->txq_size
== dev
->requested_txq_size
3133 && dev
->socket_id
== dev
->requested_socket_id
) {
3134 /* Reconfiguration is unnecessary */
3139 rte_eth_dev_stop(dev
->port_id
);
3141 if (dev
->mtu
!= dev
->requested_mtu
3142 || dev
->socket_id
!= dev
->requested_socket_id
) {
3143 err
= netdev_dpdk_mempool_configure(dev
);
3149 netdev
->n_txq
= dev
->requested_n_txq
;
3150 netdev
->n_rxq
= dev
->requested_n_rxq
;
3152 dev
->rxq_size
= dev
->requested_rxq_size
;
3153 dev
->txq_size
= dev
->requested_txq_size
;
3155 rte_free(dev
->tx_q
);
3156 err
= dpdk_eth_dev_init(dev
);
3157 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3162 netdev_change_seq_changed(netdev
);
3165 ovs_mutex_unlock(&dev
->mutex
);
3170 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3171 OVS_REQUIRES(dev
->mutex
)
3173 dev
->up
.n_txq
= dev
->requested_n_txq
;
3174 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3177 /* Enable TX queue 0 by default if it wasn't disabled. */
3178 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3179 dev
->tx_q
[0].map
= 0;
3182 netdev_dpdk_remap_txqs(dev
);
3184 if (dev
->requested_socket_id
!= dev
->socket_id
3185 || dev
->requested_mtu
!= dev
->mtu
) {
3186 err
= netdev_dpdk_mempool_configure(dev
);
3190 netdev_change_seq_changed(&dev
->up
);
3194 if (netdev_dpdk_get_vid(dev
) >= 0) {
3195 dev
->vhost_reconfigured
= true;
3202 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3204 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3207 ovs_mutex_lock(&dev
->mutex
);
3208 err
= dpdk_vhost_reconfigure_helper(dev
);
3209 ovs_mutex_unlock(&dev
->mutex
);
3215 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3217 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3220 ovs_mutex_lock(&dev
->mutex
);
3222 /* Configure vHost client mode if requested and if the following criteria
3224 * 1. Device hasn't been registered yet.
3225 * 2. A path has been specified.
3227 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3228 && strlen(dev
->vhost_id
)) {
3229 /* Register client-mode device */
3230 err
= rte_vhost_driver_register(dev
->vhost_id
,
3231 RTE_VHOST_USER_CLIENT
);
3233 VLOG_ERR("vhost-user device setup failure for device %s\n",
3237 /* Configuration successful */
3238 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3239 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3240 "using client socket '%s'",
3241 dev
->up
.name
, dev
->vhost_id
);
3245 err
= dpdk_vhost_reconfigure_helper(dev
);
3248 ovs_mutex_unlock(&dev
->mutex
);
3253 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3254 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3255 GET_CARRIER, GET_STATS, \
3256 GET_FEATURES, GET_STATUS, \
3257 RECONFIGURE, RXQ_RECV) \
3260 true, /* is_pmd */ \
3262 NULL, /* netdev_dpdk_run */ \
3263 NULL, /* netdev_dpdk_wait */ \
3265 netdev_dpdk_alloc, \
3268 netdev_dpdk_dealloc, \
3269 netdev_dpdk_get_config, \
3271 NULL, /* get_tunnel_config */ \
3272 NULL, /* build header */ \
3273 NULL, /* push header */ \
3274 NULL, /* pop header */ \
3275 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3279 NULL, /* send_wait */ \
3281 netdev_dpdk_set_etheraddr, \
3282 netdev_dpdk_get_etheraddr, \
3283 netdev_dpdk_get_mtu, \
3284 netdev_dpdk_set_mtu, \
3285 netdev_dpdk_get_ifindex, \
3287 netdev_dpdk_get_carrier_resets, \
3288 netdev_dpdk_set_miimon, \
3291 NULL, /* set_advertisements */ \
3293 netdev_dpdk_set_policing, \
3294 netdev_dpdk_get_qos_types, \
3295 NULL, /* get_qos_capabilities */ \
3296 netdev_dpdk_get_qos, \
3297 netdev_dpdk_set_qos, \
3298 NULL, /* get_queue */ \
3299 NULL, /* set_queue */ \
3300 NULL, /* delete_queue */ \
3301 NULL, /* get_queue_stats */ \
3302 NULL, /* queue_dump_start */ \
3303 NULL, /* queue_dump_next */ \
3304 NULL, /* queue_dump_done */ \
3305 NULL, /* dump_queue_stats */ \
3307 NULL, /* set_in4 */ \
3308 NULL, /* get_addr_list */ \
3309 NULL, /* add_router */ \
3310 NULL, /* get_next_hop */ \
3312 NULL, /* arp_lookup */ \
3314 netdev_dpdk_update_flags, \
3317 netdev_dpdk_rxq_alloc, \
3318 netdev_dpdk_rxq_construct, \
3319 netdev_dpdk_rxq_destruct, \
3320 netdev_dpdk_rxq_dealloc, \
3322 NULL, /* rx_wait */ \
3323 NULL, /* rxq_drain */ \
3326 static const struct netdev_class dpdk_class
=
3329 netdev_dpdk_class_init
,
3330 netdev_dpdk_construct
,
3331 netdev_dpdk_destruct
,
3332 netdev_dpdk_set_config
,
3333 netdev_dpdk_set_tx_multiq
,
3334 netdev_dpdk_eth_send
,
3335 netdev_dpdk_get_carrier
,
3336 netdev_dpdk_get_stats
,
3337 netdev_dpdk_get_features
,
3338 netdev_dpdk_get_status
,
3339 netdev_dpdk_reconfigure
,
3340 netdev_dpdk_rxq_recv
);
3342 static const struct netdev_class dpdk_ring_class
=
3345 netdev_dpdk_class_init
,
3346 netdev_dpdk_ring_construct
,
3347 netdev_dpdk_destruct
,
3348 netdev_dpdk_ring_set_config
,
3349 netdev_dpdk_set_tx_multiq
,
3350 netdev_dpdk_ring_send
,
3351 netdev_dpdk_get_carrier
,
3352 netdev_dpdk_get_stats
,
3353 netdev_dpdk_get_features
,
3354 netdev_dpdk_get_status
,
3355 netdev_dpdk_reconfigure
,
3356 netdev_dpdk_rxq_recv
);
3358 static const struct netdev_class dpdk_vhost_class
=
3361 netdev_dpdk_vhost_class_init
,
3362 netdev_dpdk_vhost_construct
,
3363 netdev_dpdk_vhost_destruct
,
3366 netdev_dpdk_vhost_send
,
3367 netdev_dpdk_vhost_get_carrier
,
3368 netdev_dpdk_vhost_get_stats
,
3371 netdev_dpdk_vhost_reconfigure
,
3372 netdev_dpdk_vhost_rxq_recv
);
3373 static const struct netdev_class dpdk_vhost_client_class
=
3375 "dpdkvhostuserclient",
3376 netdev_dpdk_vhost_class_init
,
3377 netdev_dpdk_vhost_client_construct
,
3378 netdev_dpdk_vhost_destruct
,
3379 netdev_dpdk_vhost_client_set_config
,
3381 netdev_dpdk_vhost_send
,
3382 netdev_dpdk_vhost_get_carrier
,
3383 netdev_dpdk_vhost_get_stats
,
3386 netdev_dpdk_vhost_client_reconfigure
,
3387 netdev_dpdk_vhost_rxq_recv
);
3390 netdev_dpdk_register(void)
3392 netdev_register_provider(&dpdk_class
);
3393 netdev_register_provider(&dpdk_ring_class
);
3394 netdev_register_provider(&dpdk_vhost_class
);
3395 netdev_register_provider(&dpdk_vhost_client_class
);