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_config.h>
30 #include <rte_cycles.h>
31 #include <rte_errno.h>
32 #include <rte_eth_ring.h>
33 #include <rte_ethdev.h>
34 #include <rte_malloc.h>
36 #include <rte_meter.h>
38 #include <rte_vhost.h>
41 #include "dp-packet.h"
43 #include "dpif-netdev.h"
44 #include "fatal-signal.h"
45 #include "netdev-provider.h"
46 #include "netdev-vport.h"
48 #include "openvswitch/dynamic-string.h"
49 #include "openvswitch/list.h"
50 #include "openvswitch/ofp-print.h"
51 #include "openvswitch/vlog.h"
53 #include "ovs-thread.h"
56 #include "openvswitch/shash.h"
59 #include "unaligned.h"
63 enum {VIRTIO_RXQ
, VIRTIO_TXQ
, VIRTIO_QNUM
};
65 VLOG_DEFINE_THIS_MODULE(netdev_dpdk
);
66 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
68 #define DPDK_PORT_WATCHDOG_INTERVAL 5
70 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
71 #define OVS_VPORT_DPDK "ovs_dpdk"
74 * need to reserve tons of extra space in the mbufs so we can align the
75 * DMA addresses to 4KB.
76 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
77 * performance for standard Ethernet MTU.
79 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
80 + (2 * VLAN_HEADER_LEN))
81 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
82 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
83 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
84 - ETHER_HDR_LEN - ETHER_CRC_LEN)
85 #define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
86 + sizeof(struct dp_packet) \
87 + RTE_PKTMBUF_HEADROOM), \
89 #define NETDEV_DPDK_MBUF_ALIGN 1024
90 #define NETDEV_DPDK_MAX_PKT_LEN 9728
92 /* Max and min number of packets in the mempool. OVS tries to allocate a
93 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
94 * enough hugepages) we keep halving the number until the allocation succeeds
95 * or we reach MIN_NB_MBUF */
97 #define MAX_NB_MBUF (4096 * 64)
98 #define MIN_NB_MBUF (4096 * 4)
99 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
101 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
102 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
) == 0);
104 /* The smallest possible NB_MBUF that we're going to try should be a multiple
105 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
106 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
))
110 * DPDK XSTATS Counter names definition
112 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
113 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
114 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
115 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
116 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
117 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
118 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
120 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
121 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
122 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
123 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
124 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
125 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
126 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
128 #define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
129 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
130 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
131 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
132 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
133 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
134 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
135 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
139 /* Default size of Physical NIC RXQ */
140 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
141 /* Default size of Physical NIC TXQ */
142 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
143 /* Maximum size of Physical NIC Queues */
144 #define NIC_PORT_MAX_Q_SIZE 4096
146 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
147 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
148 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
149 * yet mapped to another queue. */
151 #define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
153 /* DPDK library uses uint8_t for port_id. */
154 typedef uint8_t dpdk_port_t
;
156 #define VHOST_ENQ_RETRY_NUM 8
157 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
159 static const struct rte_eth_conf port_conf
= {
161 .mq_mode
= ETH_MQ_RX_RSS
,
163 .header_split
= 0, /* Header Split disabled */
164 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
165 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
166 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
172 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
176 .mq_mode
= ETH_MQ_TX_NONE
,
181 * These callbacks allow virtio-net devices to be added to vhost ports when
182 * configuration has been fully completed.
184 static int new_device(int vid
);
185 static void destroy_device(int vid
);
186 static int vring_state_changed(int vid
, uint16_t queue_id
, int enable
);
187 static const struct vhost_device_ops virtio_net_device_ops
=
189 .new_device
= new_device
,
190 .destroy_device
= destroy_device
,
191 .vring_state_changed
= vring_state_changed
,
192 .features_changed
= NULL
195 enum { DPDK_RING_SIZE
= 256 };
196 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
197 enum { DRAIN_TSC
= 200000ULL };
204 /* Quality of Service */
206 /* An instance of a QoS configuration. Always associated with a particular
209 * Each QoS implementation subclasses this with whatever additional data it
213 const struct dpdk_qos_ops
*ops
;
217 /* A particular implementation of dpdk QoS operations.
219 * The functions below return 0 if successful or a positive errno value on
220 * failure, except where otherwise noted. All of them must be provided, except
221 * where otherwise noted.
223 struct dpdk_qos_ops
{
225 /* Name of the QoS type */
226 const char *qos_name
;
228 /* Called to construct a qos_conf object. The implementation should make
229 * the appropriate calls to configure QoS according to 'details'.
231 * The contents of 'details' should be documented as valid for 'ovs_name'
232 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
233 * (which is built as ovs-vswitchd.conf.db(8)).
235 * This function must return 0 if and only if it sets '*conf' to an
236 * initialized 'struct qos_conf'.
238 * For all QoS implementations it should always be non-null.
240 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
242 /* Destroys the data structures allocated by the implementation as part of
245 * For all QoS implementations it should always be non-null.
247 void (*qos_destruct
)(struct qos_conf
*conf
);
249 /* Retrieves details of 'conf' configuration into 'details'.
251 * The contents of 'details' should be documented as valid for 'ovs_name'
252 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
253 * (which is built as ovs-vswitchd.conf.db(8)).
255 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
257 /* Returns true if 'conf' is already configured according to 'details'.
259 * The contents of 'details' should be documented as valid for 'ovs_name'
260 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
261 * (which is built as ovs-vswitchd.conf.db(8)).
263 * For all QoS implementations it should always be non-null.
265 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
266 const struct smap
*details
);
268 /* Modify an array of rte_mbufs. The modification is specific to
269 * each qos implementation.
271 * The function should take and array of mbufs and an int representing
272 * the current number of mbufs present in the array.
274 * After the function has performed a qos modification to the array of
275 * mbufs it returns an int representing the number of mbufs now present in
276 * the array. This value is can then be passed to the port send function
277 * along with the modified array for transmission.
279 * For all QoS implementations it should always be non-null.
281 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
282 int pkt_cnt
, bool may_steal
);
285 /* dpdk_qos_ops for each type of user space QoS implementation */
286 static const struct dpdk_qos_ops egress_policer_ops
;
289 * Array of dpdk_qos_ops, contains pointer to all supported QoS
292 static const struct dpdk_qos_ops
*const qos_confs
[] = {
297 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
299 /* Contains all 'struct dpdk_dev's. */
300 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
301 = OVS_LIST_INITIALIZER(&dpdk_list
);
303 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
304 = OVS_MUTEX_INITIALIZER
;
307 struct rte_mempool
*mp
;
310 char if_name
[IFNAMSIZ
];
312 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
315 /* There should be one 'struct dpdk_tx_queue' created for
317 struct dpdk_tx_queue
{
318 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
319 * from concurrent access. It is used only
320 * if the queue is shared among different
321 * pmd threads (see 'concurrent_txq'). */
322 int map
; /* Mapping of configured vhost-user queues
323 * to enabled by guest. */
326 /* dpdk has no way to remove dpdk ring ethernet devices
327 so we have to keep them around once they've been created
330 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
331 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
334 /* For the client rings */
335 struct rte_ring
*cring_tx
;
336 struct rte_ring
*cring_rx
;
337 unsigned int user_port_id
; /* User given port no, parsed from port name */
338 dpdk_port_t eth_port_id
; /* ethernet device port id */
339 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
342 struct ingress_policer
{
343 struct rte_meter_srtcm_params app_srtcm_params
;
344 struct rte_meter_srtcm in_policer
;
345 rte_spinlock_t policer_lock
;
348 enum dpdk_hw_ol_features
{
349 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
356 enum dpdk_dev_type type
;
358 struct dpdk_tx_queue
*tx_q
;
360 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
362 struct dpdk_mp
*dpdk_mp
;
366 struct netdev_stats stats
;
368 rte_spinlock_t stats_lock
;
370 struct eth_addr hwaddr
;
371 enum netdev_flags flags
;
373 struct rte_eth_link link
;
376 /* virtio identifier for vhost devices */
379 /* True if vHost device is 'up' and has been reconfigured at least once */
380 bool vhost_reconfigured
;
382 /* Identifier used to distinguish vhost devices from each other. */
383 char vhost_id
[PATH_MAX
];
385 /* Device arguments for dpdk ports */
388 /* If true, device was attached by rte_eth_dev_attach(). */
392 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
394 /* QoS configuration and lock for the device */
395 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
397 /* The following properties cannot be changed when a device is running,
398 * so we remember the request and update them next time
399 * netdev_dpdk*_reconfigure() is called */
403 int requested_rxq_size
;
404 int requested_txq_size
;
406 /* Number of rx/tx descriptors for physical devices */
410 /* Socket ID detected when vHost device is brought up */
411 int requested_socket_id
;
413 /* Denotes whether vHost port is client/server mode */
414 uint64_t vhost_driver_flags
;
416 /* Ingress Policer */
417 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
418 uint32_t policer_rate
;
419 uint32_t policer_burst
;
421 /* DPDK-ETH Flow control */
422 struct rte_eth_fc_conf fc_conf
;
424 /* DPDK-ETH hardware offload features,
425 * from the enum set 'dpdk_hw_ol_features' */
426 uint32_t hw_ol_features
;
429 struct netdev_rxq_dpdk
{
430 struct netdev_rxq up
;
434 static void netdev_dpdk_destruct(struct netdev
*netdev
);
435 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
437 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
439 struct ingress_policer
*
440 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
443 is_dpdk_class(const struct netdev_class
*class)
445 return class->destruct
== netdev_dpdk_destruct
446 || class->destruct
== netdev_dpdk_vhost_destruct
;
449 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
450 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
451 * value, insufficient buffers are allocated to accomodate the packet in its
452 * entirety. Furthermore, certain drivers need to ensure that there is also
453 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
454 * frames). If the RX buffer is too small, then the driver enables scatter RX
455 * behaviour, which reduces performance. To prevent this, use a buffer size
456 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
459 dpdk_buf_size(int mtu
)
461 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
462 NETDEV_DPDK_MBUF_ALIGN
);
465 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
467 * Unlike xmalloc(), this function can return NULL on failure. */
469 dpdk_rte_mzalloc(size_t sz
)
471 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
475 free_dpdk_buf(struct dp_packet
*p
)
477 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
479 rte_pktmbuf_free(pkt
);
483 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
484 void *opaque_arg OVS_UNUSED
,
486 unsigned i OVS_UNUSED
)
488 struct rte_mbuf
*pkt
= _p
;
490 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
494 * Full DPDK memory pool name must be unique
495 * and cannot be longer than RTE_MEMPOOL_NAMESIZE
498 dpdk_mp_name(struct dpdk_mp
*dmp
)
500 uint32_t h
= hash_string(dmp
->if_name
, 0);
501 char *mp_name
= xcalloc(RTE_MEMPOOL_NAMESIZE
, sizeof *mp_name
);
502 int ret
= snprintf(mp_name
, RTE_MEMPOOL_NAMESIZE
, "ovs_%x_%d_%u",
503 h
, dmp
->mtu
, dmp
->mp_size
);
504 if (ret
< 0 || ret
>= RTE_MEMPOOL_NAMESIZE
) {
510 static struct dpdk_mp
*
511 dpdk_mp_create(struct netdev_dpdk
*dev
, int mtu
)
513 struct dpdk_mp
*dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
517 dmp
->socket_id
= dev
->requested_socket_id
;
519 ovs_strzcpy(dmp
->if_name
, dev
->up
.name
, IFNAMSIZ
);
522 * XXX: rough estimation of memory required for port:
523 * <packets required to fill the device rxqs>
524 * + <packets that could be stuck on other ports txqs>
525 * + <packets in the pmd threads>
526 * + <additional memory for corner cases>
528 dmp
->mp_size
= dev
->requested_n_rxq
* dev
->requested_rxq_size
529 + dev
->requested_n_txq
* dev
->requested_txq_size
530 + MIN(RTE_MAX_LCORE
, dev
->requested_n_rxq
) * NETDEV_MAX_BURST
533 bool mp_exists
= false;
536 char *mp_name
= dpdk_mp_name(dmp
);
538 VLOG_DBG("Requesting a mempool of %u mbufs for netdev %s "
539 "with %d Rx and %d Tx queues.",
540 dmp
->mp_size
, dev
->up
.name
,
541 dev
->requested_n_rxq
, dev
->requested_n_txq
);
543 dmp
->mp
= rte_pktmbuf_pool_create(mp_name
, dmp
->mp_size
,
545 sizeof (struct dp_packet
)
546 - sizeof (struct rte_mbuf
),
548 - sizeof(struct dp_packet
),
551 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", mp_name
,
553 } else if (rte_errno
== EEXIST
) {
554 /* A mempool with the same name already exists. We just
555 * retrieve its pointer to be returned to the caller. */
556 dmp
->mp
= rte_mempool_lookup(mp_name
);
557 VLOG_DBG("A mempool with name %s already exists at %p.",
559 /* As the mempool create returned EEXIST we can expect the
560 * lookup has returned a valid pointer. If for some reason
561 * that's not the case we keep track of it. */
564 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
565 mp_name
, dmp
->mp_size
);
569 /* rte_pktmbuf_pool_create has done some initialization of the
570 * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init
571 * initializes some OVS specific fields of dp_packet.
573 rte_mempool_obj_iter(dmp
->mp
, ovs_rte_pktmbuf_init
, NULL
);
576 } while (!mp_exists
&&
577 (rte_errno
== ENOMEM
&& (dmp
->mp_size
/= 2) >= MIN_NB_MBUF
));
583 static struct dpdk_mp
*
584 dpdk_mp_get(struct netdev_dpdk
*dev
, int mtu
)
588 ovs_mutex_lock(&dpdk_mp_mutex
);
589 dmp
= dpdk_mp_create(dev
, mtu
);
590 ovs_mutex_unlock(&dpdk_mp_mutex
);
596 dpdk_mp_put(struct dpdk_mp
*dmp
)
604 ovs_mutex_lock(&dpdk_mp_mutex
);
605 mp_name
= dpdk_mp_name(dmp
);
606 VLOG_DBG("Releasing \"%s\" mempool", mp_name
);
608 rte_mempool_free(dmp
->mp
);
610 ovs_mutex_unlock(&dpdk_mp_mutex
);
613 /* Tries to allocate new mempool on requested_socket_id with
614 * mbuf size corresponding to requested_mtu.
615 * On success new configuration will be applied.
616 * On error, device will be left unchanged. */
618 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
619 OVS_REQUIRES(dev
->mutex
)
621 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
624 mp
= dpdk_mp_get(dev
, FRAME_LEN_TO_MTU(buf_size
));
626 VLOG_ERR("Failed to create memory pool for netdev "
627 "%s, with MTU %d on socket %d: %s\n",
628 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
629 rte_strerror(rte_errno
));
632 dpdk_mp_put(dev
->dpdk_mp
);
634 dev
->mtu
= dev
->requested_mtu
;
635 dev
->socket_id
= dev
->requested_socket_id
;
636 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
643 check_link_status(struct netdev_dpdk
*dev
)
645 struct rte_eth_link link
;
647 rte_eth_link_get_nowait(dev
->port_id
, &link
);
649 if (dev
->link
.link_status
!= link
.link_status
) {
650 netdev_change_seq_changed(&dev
->up
);
652 dev
->link_reset_cnt
++;
654 if (dev
->link
.link_status
) {
655 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
656 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
657 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
658 ("full-duplex") : ("half-duplex"));
660 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
666 dpdk_watchdog(void *dummy OVS_UNUSED
)
668 struct netdev_dpdk
*dev
;
670 pthread_detach(pthread_self());
673 ovs_mutex_lock(&dpdk_mutex
);
674 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
675 ovs_mutex_lock(&dev
->mutex
);
676 if (dev
->type
== DPDK_DEV_ETH
) {
677 check_link_status(dev
);
679 ovs_mutex_unlock(&dev
->mutex
);
681 ovs_mutex_unlock(&dpdk_mutex
);
682 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
689 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
693 struct rte_eth_conf conf
= port_conf
;
695 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
697 if (dev
->mtu
> ETHER_MTU
) {
698 conf
.rxmode
.enable_scatter
= 1;
701 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
702 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
703 /* A device may report more queues than it makes available (this has
704 * been observed for Intel xl710, which reserves some of them for
705 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
706 * available. When this happens we can retry the configuration
707 * and request less queues */
708 while (n_rxq
&& n_txq
) {
710 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
713 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
715 VLOG_WARN("Interface %s eth_dev setup error %s\n",
716 dev
->up
.name
, rte_strerror(-diag
));
720 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
722 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
723 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
727 for (i
= 0; i
< n_txq
; i
++) {
728 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
729 dev
->socket_id
, NULL
);
731 VLOG_INFO("Interface %s txq(%d) setup error: %s",
732 dev
->up
.name
, i
, rte_strerror(-diag
));
738 /* Retry with less tx queues */
743 for (i
= 0; i
< n_rxq
; i
++) {
744 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
745 dev
->socket_id
, NULL
,
748 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
749 dev
->up
.name
, i
, rte_strerror(-diag
));
755 /* Retry with less rx queues */
760 dev
->up
.n_rxq
= n_rxq
;
761 dev
->up
.n_txq
= n_txq
;
770 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
772 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
773 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
779 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
780 OVS_REQUIRES(dev
->mutex
)
782 struct rte_pktmbuf_pool_private
*mbp_priv
;
783 struct rte_eth_dev_info info
;
784 struct ether_addr eth_addr
;
787 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
788 DEV_RX_OFFLOAD_TCP_CKSUM
|
789 DEV_RX_OFFLOAD_IPV4_CKSUM
;
791 rte_eth_dev_info_get(dev
->port_id
, &info
);
793 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
794 rx_chksm_offload_capa
) {
795 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
797 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
799 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
802 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
803 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
805 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
807 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
808 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
812 diag
= rte_eth_dev_start(dev
->port_id
);
814 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
815 rte_strerror(-diag
));
819 rte_eth_promiscuous_enable(dev
->port_id
);
820 rte_eth_allmulticast_enable(dev
->port_id
);
822 memset(ð_addr
, 0x0, sizeof(eth_addr
));
823 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
824 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
825 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
827 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
828 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
830 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
831 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
833 /* Get the Flow control configuration for DPDK-ETH */
834 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
836 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
843 static struct netdev_dpdk
*
844 netdev_dpdk_cast(const struct netdev
*netdev
)
846 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
849 static struct netdev
*
850 netdev_dpdk_alloc(void)
852 struct netdev_dpdk
*dev
;
854 dev
= dpdk_rte_mzalloc(sizeof *dev
);
862 static struct dpdk_tx_queue
*
863 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
865 struct dpdk_tx_queue
*txqs
;
868 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
870 for (i
= 0; i
< n_txqs
; i
++) {
871 /* Initialize map for vhost devices. */
872 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
873 rte_spinlock_init(&txqs
[i
].tx_lock
);
881 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
882 enum dpdk_dev_type type
, int socket_id
)
883 OVS_REQUIRES(dpdk_mutex
)
885 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
887 ovs_mutex_init(&dev
->mutex
);
889 rte_spinlock_init(&dev
->stats_lock
);
891 /* If the 'sid' is negative, it means that the kernel fails
892 * to obtain the pci numa info. In that situation, always
894 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
895 dev
->requested_socket_id
= dev
->socket_id
;
896 dev
->port_id
= port_no
;
899 dev
->requested_mtu
= ETHER_MTU
;
900 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
901 ovsrcu_index_init(&dev
->vid
, -1);
902 dev
->vhost_reconfigured
= false;
903 dev
->attached
= false;
905 ovsrcu_init(&dev
->qos_conf
, NULL
);
907 ovsrcu_init(&dev
->ingress_policer
, NULL
);
908 dev
->policer_rate
= 0;
909 dev
->policer_burst
= 0;
913 dev
->requested_n_rxq
= NR_QUEUE
;
914 dev
->requested_n_txq
= NR_QUEUE
;
915 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
916 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
918 /* Initialize the flow control to NULL */
919 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
921 /* Initilize the hardware offload flags to 0 */
922 dev
->hw_ol_features
= 0;
924 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
926 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
928 netdev_request_reconfigure(netdev
);
933 /* dev_name must be the prefix followed by a positive decimal number.
934 * (no leading + or - signs are allowed) */
936 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
937 unsigned int *port_no
)
941 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
945 cport
= dev_name
+ strlen(prefix
);
947 if (str_to_uint(cport
, 10, port_no
)) {
955 vhost_common_construct(struct netdev
*netdev
)
956 OVS_REQUIRES(dpdk_mutex
)
958 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
959 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
961 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
966 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
967 DPDK_DEV_VHOST
, socket_id
);
971 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
973 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
974 const char *name
= netdev
->name
;
977 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
978 * the file system. '/' or '\' would traverse directories, so they're not
979 * acceptable in 'name'. */
980 if (strchr(name
, '/') || strchr(name
, '\\')) {
981 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
982 "A valid name must not include '/' or '\\'",
987 ovs_mutex_lock(&dpdk_mutex
);
988 /* Take the name of the vhost-user port and append it to the location where
989 * the socket is to be created, then register the socket.
991 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
992 dpdk_get_vhost_sock_dir(), name
);
994 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
995 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
997 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
1001 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
1002 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1003 dev
->vhost_id
, name
);
1006 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
1007 &virtio_net_device_ops
);
1009 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1010 "port: %s\n", name
);
1014 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
1015 1ULL << VIRTIO_NET_F_HOST_TSO4
1016 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1017 | 1ULL << VIRTIO_NET_F_CSUM
);
1019 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1020 "port: %s\n", name
);
1024 err
= rte_vhost_driver_start(dev
->vhost_id
);
1026 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1027 "port: %s\n", name
);
1031 err
= vhost_common_construct(netdev
);
1033 VLOG_ERR("vhost_common_construct failed for vhost user "
1034 "port: %s\n", name
);
1038 ovs_mutex_unlock(&dpdk_mutex
);
1039 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1040 "please migrate to dpdkvhostuserclient ports.");
1045 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1049 ovs_mutex_lock(&dpdk_mutex
);
1050 err
= vhost_common_construct(netdev
);
1052 VLOG_ERR("vhost_common_construct failed for vhost user client"
1053 "port: %s\n", netdev
->name
);
1055 ovs_mutex_unlock(&dpdk_mutex
);
1060 netdev_dpdk_construct(struct netdev
*netdev
)
1064 ovs_mutex_lock(&dpdk_mutex
);
1065 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1066 DPDK_DEV_ETH
, SOCKET0
);
1067 ovs_mutex_unlock(&dpdk_mutex
);
1072 common_destruct(struct netdev_dpdk
*dev
)
1073 OVS_REQUIRES(dpdk_mutex
)
1074 OVS_EXCLUDED(dev
->mutex
)
1076 rte_free(dev
->tx_q
);
1077 dpdk_mp_put(dev
->dpdk_mp
);
1079 ovs_list_remove(&dev
->list_node
);
1080 free(ovsrcu_get_protected(struct ingress_policer
*,
1081 &dev
->ingress_policer
));
1082 ovs_mutex_destroy(&dev
->mutex
);
1086 netdev_dpdk_destruct(struct netdev
*netdev
)
1088 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1089 char devname
[RTE_ETH_NAME_MAX_LEN
];
1091 ovs_mutex_lock(&dpdk_mutex
);
1093 rte_eth_dev_stop(dev
->port_id
);
1095 if (dev
->attached
) {
1096 rte_eth_dev_close(dev
->port_id
);
1097 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1098 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1100 VLOG_INFO("Device '%s' has been detached", devname
);
1105 common_destruct(dev
);
1107 ovs_mutex_unlock(&dpdk_mutex
);
1110 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1111 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1112 * deadlock, none of the mutexes must be held while calling this function. */
1114 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1116 OVS_EXCLUDED(dpdk_mutex
)
1117 OVS_EXCLUDED(dev
->mutex
)
1119 return rte_vhost_driver_unregister(vhost_id
);
1123 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1125 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1128 ovs_mutex_lock(&dpdk_mutex
);
1130 /* Guest becomes an orphan if still attached. */
1131 if (netdev_dpdk_get_vid(dev
) >= 0
1132 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1133 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1135 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1136 "socket '%s' must be restarted.", dev
->vhost_id
);
1139 vhost_id
= xstrdup(dev
->vhost_id
);
1141 common_destruct(dev
);
1143 ovs_mutex_unlock(&dpdk_mutex
);
1149 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1150 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1151 netdev
->name
, vhost_id
);
1152 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1153 /* OVS server mode - remove this socket from list for deletion */
1154 fatal_signal_remove_file_to_unlink(vhost_id
);
1161 netdev_dpdk_dealloc(struct netdev
*netdev
)
1163 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1169 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1171 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1173 ovs_mutex_lock(&dev
->mutex
);
1175 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1176 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1177 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1178 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1179 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1181 if (dev
->type
== DPDK_DEV_ETH
) {
1182 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1183 dev
->requested_rxq_size
);
1184 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1186 smap_add_format(args
, "requested_txq_descriptors", "%d",
1187 dev
->requested_txq_size
);
1188 smap_add_format(args
, "configured_txq_descriptors", "%d",
1190 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1191 smap_add(args
, "rx_csum_offload", "true");
1193 smap_add(args
, "rx_csum_offload", "false");
1196 ovs_mutex_unlock(&dev
->mutex
);
1201 static struct netdev_dpdk
*
1202 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1203 OVS_REQUIRES(dpdk_mutex
)
1205 struct netdev_dpdk
*dev
;
1207 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1208 if (dev
->port_id
== port_id
) {
1217 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1218 const char *devargs
, char **errp
)
1220 /* Get the name up to the first comma. */
1221 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1222 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1224 if (!rte_eth_dev_count()
1225 || rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1226 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1227 /* Device not found in DPDK, attempt to attach it */
1228 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1229 /* Attach successful */
1230 dev
->attached
= true;
1231 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1233 /* Attach unsuccessful */
1234 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1235 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1245 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1246 OVS_REQUIRES(dev
->mutex
)
1250 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1251 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1252 dev
->requested_n_rxq
= new_n_rxq
;
1253 netdev_request_reconfigure(&dev
->up
);
1258 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1259 const char *flag
, int default_size
, int *new_size
)
1261 int queue_size
= smap_get_int(args
, flag
, default_size
);
1263 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1264 || !is_pow2(queue_size
)) {
1265 queue_size
= default_size
;
1268 if (queue_size
!= *new_size
) {
1269 *new_size
= queue_size
;
1270 netdev_request_reconfigure(netdev
);
1275 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1278 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1279 bool rx_fc_en
, tx_fc_en
, autoneg
;
1280 enum rte_eth_fc_mode fc_mode
;
1281 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1282 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1283 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1285 const char *new_devargs
;
1288 ovs_mutex_lock(&dpdk_mutex
);
1289 ovs_mutex_lock(&dev
->mutex
);
1291 dpdk_set_rxq_config(dev
, args
);
1293 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1294 NIC_PORT_DEFAULT_RXQ_SIZE
,
1295 &dev
->requested_rxq_size
);
1296 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1297 NIC_PORT_DEFAULT_TXQ_SIZE
,
1298 &dev
->requested_txq_size
);
1300 new_devargs
= smap_get(args
, "dpdk-devargs");
1302 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1303 /* The user requested a new device. If we return error, the caller
1304 * will delete this netdev and try to recreate it. */
1309 /* dpdk-devargs is required for device configuration */
1310 if (new_devargs
&& new_devargs
[0]) {
1311 /* Don't process dpdk-devargs if value is unchanged and port id
1313 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1314 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1315 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1318 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1320 } else if (new_port_id
== dev
->port_id
) {
1321 /* Already configured, do not reconfigure again */
1324 struct netdev_dpdk
*dup_dev
;
1326 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1328 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1329 "which is already in use by '%s'",
1330 netdev_get_name(netdev
), new_devargs
,
1331 netdev_get_name(&dup_dev
->up
));
1334 int sid
= rte_eth_dev_socket_id(new_port_id
);
1336 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1337 dev
->devargs
= xstrdup(new_devargs
);
1338 dev
->port_id
= new_port_id
;
1339 netdev_request_reconfigure(&dev
->up
);
1345 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1346 "The old 'dpdk<port_id>' names are not supported",
1347 netdev_get_name(netdev
));
1355 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1356 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1357 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1359 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1360 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1361 dev
->fc_conf
.mode
= fc_mode
;
1362 dev
->fc_conf
.autoneg
= autoneg
;
1363 dpdk_eth_flow_ctrl_setup(dev
);
1367 ovs_mutex_unlock(&dev
->mutex
);
1368 ovs_mutex_unlock(&dpdk_mutex
);
1374 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1375 char **errp OVS_UNUSED
)
1377 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1379 ovs_mutex_lock(&dev
->mutex
);
1380 dpdk_set_rxq_config(dev
, args
);
1381 ovs_mutex_unlock(&dev
->mutex
);
1387 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1388 const struct smap
*args
,
1389 char **errp OVS_UNUSED
)
1391 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1394 ovs_mutex_lock(&dev
->mutex
);
1395 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1396 path
= smap_get(args
, "vhost-server-path");
1397 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1398 strcpy(dev
->vhost_id
, path
);
1399 netdev_request_reconfigure(netdev
);
1402 ovs_mutex_unlock(&dev
->mutex
);
1408 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1410 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1412 return dev
->socket_id
;
1415 /* Sets the number of tx queues for the dpdk interface. */
1417 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1419 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1421 ovs_mutex_lock(&dev
->mutex
);
1423 if (dev
->requested_n_txq
== n_txq
) {
1427 dev
->requested_n_txq
= n_txq
;
1428 netdev_request_reconfigure(netdev
);
1431 ovs_mutex_unlock(&dev
->mutex
);
1435 static struct netdev_rxq
*
1436 netdev_dpdk_rxq_alloc(void)
1438 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1447 static struct netdev_rxq_dpdk
*
1448 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1450 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1454 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1456 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1457 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1459 ovs_mutex_lock(&dev
->mutex
);
1460 rx
->port_id
= dev
->port_id
;
1461 ovs_mutex_unlock(&dev
->mutex
);
1467 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1472 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1474 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1479 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1480 * 'pkts', even in case of failure.
1482 * Returns the number of packets that weren't transmitted. */
1484 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1485 struct rte_mbuf
**pkts
, int cnt
)
1489 while (nb_tx
!= cnt
) {
1492 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1500 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1501 /* Free buffers, which we couldn't transmit, one at a time (each
1502 * packet could come from a different mempool) */
1505 for (i
= nb_tx
; i
< cnt
; i
++) {
1506 rte_pktmbuf_free(pkts
[i
]);
1514 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1515 struct rte_mbuf
*pkt
, uint64_t time
)
1517 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1519 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1524 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1525 struct rte_mbuf
**pkts
, int pkt_cnt
,
1530 struct rte_mbuf
*pkt
= NULL
;
1531 uint64_t current_time
= rte_rdtsc();
1533 for (i
= 0; i
< pkt_cnt
; i
++) {
1535 /* Handle current packet */
1536 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1543 rte_pktmbuf_free(pkt
);
1552 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1553 int pkt_cnt
, bool may_steal
)
1557 rte_spinlock_lock(&policer
->policer_lock
);
1558 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
,
1559 pkt_cnt
, may_steal
);
1560 rte_spinlock_unlock(&policer
->policer_lock
);
1566 is_vhost_running(struct netdev_dpdk
*dev
)
1568 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1572 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1573 unsigned int packet_size
)
1575 /* Hard-coded search for the size bucket. */
1576 if (packet_size
< 256) {
1577 if (packet_size
>= 128) {
1578 stats
->rx_128_to_255_packets
++;
1579 } else if (packet_size
<= 64) {
1580 stats
->rx_1_to_64_packets
++;
1582 stats
->rx_65_to_127_packets
++;
1585 if (packet_size
>= 1523) {
1586 stats
->rx_1523_to_max_packets
++;
1587 } else if (packet_size
>= 1024) {
1588 stats
->rx_1024_to_1522_packets
++;
1589 } else if (packet_size
< 512) {
1590 stats
->rx_256_to_511_packets
++;
1592 stats
->rx_512_to_1023_packets
++;
1598 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1599 struct dp_packet
**packets
, int count
,
1603 unsigned int packet_size
;
1604 struct dp_packet
*packet
;
1606 stats
->rx_packets
+= count
;
1607 stats
->rx_dropped
+= dropped
;
1608 for (i
= 0; i
< count
; i
++) {
1609 packet
= packets
[i
];
1610 packet_size
= dp_packet_size(packet
);
1612 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1613 /* This only protects the following multicast counting from
1614 * too short packets, but it does not stop the packet from
1615 * further processing. */
1617 stats
->rx_length_errors
++;
1621 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1623 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1624 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1628 stats
->rx_bytes
+= packet_size
;
1633 * The receive path for the vhost port is the TX path out from guest.
1636 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1637 struct dp_packet_batch
*batch
)
1639 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1640 int qid
= rxq
->queue_id
;
1641 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1643 uint16_t dropped
= 0;
1645 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1646 || !(dev
->flags
& NETDEV_UP
))) {
1650 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1651 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1653 (struct rte_mbuf
**) batch
->packets
,
1661 nb_rx
= ingress_policer_run(policer
,
1662 (struct rte_mbuf
**) batch
->packets
,
1667 rte_spinlock_lock(&dev
->stats_lock
);
1668 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1670 rte_spinlock_unlock(&dev
->stats_lock
);
1672 dp_packet_batch_init_cutlen(batch
);
1673 batch
->count
= (int) nb_rx
;
1678 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1680 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1681 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1682 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1686 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1690 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1691 (struct rte_mbuf
**) batch
->packets
,
1699 nb_rx
= ingress_policer_run(policer
,
1700 (struct rte_mbuf
**) batch
->packets
,
1705 /* Update stats to reflect dropped packets */
1706 if (OVS_UNLIKELY(dropped
)) {
1707 rte_spinlock_lock(&dev
->stats_lock
);
1708 dev
->stats
.rx_dropped
+= dropped
;
1709 rte_spinlock_unlock(&dev
->stats_lock
);
1712 dp_packet_batch_init_cutlen(batch
);
1713 batch
->count
= nb_rx
;
1719 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1720 int cnt
, bool may_steal
)
1722 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1725 rte_spinlock_lock(&qos_conf
->lock
);
1726 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
, may_steal
);
1727 rte_spinlock_unlock(&qos_conf
->lock
);
1734 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1739 struct rte_mbuf
*pkt
;
1741 for (i
= 0; i
< pkt_cnt
; i
++) {
1743 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1744 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1745 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1746 rte_pktmbuf_free(pkt
);
1750 if (OVS_UNLIKELY(i
!= cnt
)) {
1760 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1761 struct dp_packet
**packets
,
1766 int sent
= attempted
- dropped
;
1768 stats
->tx_packets
+= sent
;
1769 stats
->tx_dropped
+= dropped
;
1771 for (i
= 0; i
< sent
; i
++) {
1772 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1777 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1778 struct dp_packet
**pkts
, int cnt
)
1780 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1781 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1782 unsigned int total_pkts
= cnt
;
1783 unsigned int dropped
= 0;
1786 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1788 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1789 || !(dev
->flags
& NETDEV_UP
))) {
1790 rte_spinlock_lock(&dev
->stats_lock
);
1791 dev
->stats
.tx_dropped
+= cnt
;
1792 rte_spinlock_unlock(&dev
->stats_lock
);
1796 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1798 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1799 /* Check has QoS has been configured for the netdev */
1800 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
, true);
1801 dropped
= total_pkts
- cnt
;
1804 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1805 unsigned int tx_pkts
;
1807 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1808 vhost_qid
, cur_pkts
, cnt
);
1809 if (OVS_LIKELY(tx_pkts
)) {
1810 /* Packets have been sent.*/
1812 /* Prepare for possible retry.*/
1813 cur_pkts
= &cur_pkts
[tx_pkts
];
1815 /* No packets sent - do not retry.*/
1818 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1820 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1822 rte_spinlock_lock(&dev
->stats_lock
);
1823 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1825 rte_spinlock_unlock(&dev
->stats_lock
);
1828 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1829 dp_packet_delete(pkts
[i
]);
1833 /* Tx function. Transmit packets indefinitely */
1835 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1836 OVS_NO_THREAD_SAFETY_ANALYSIS
1838 const size_t batch_cnt
= dp_packet_batch_size(batch
);
1839 #if !defined(__CHECKER__) && !defined(_WIN32)
1840 const size_t PKT_ARRAY_SIZE
= batch_cnt
;
1842 /* Sparse or MSVC doesn't like variable length array. */
1843 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1845 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1846 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1847 uint32_t cnt
= batch_cnt
;
1848 uint32_t dropped
= 0;
1850 if (dev
->type
!= DPDK_DEV_VHOST
) {
1851 /* Check if QoS has been configured for this netdev. */
1852 cnt
= netdev_dpdk_qos_run(dev
, (struct rte_mbuf
**) batch
->packets
,
1854 dropped
+= batch_cnt
- cnt
;
1857 dp_packet_batch_apply_cutlen(batch
);
1861 for (uint32_t i
= 0; i
< cnt
; i
++) {
1862 struct dp_packet
*packet
= batch
->packets
[i
];
1863 uint32_t size
= dp_packet_size(packet
);
1865 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1866 VLOG_WARN_RL(&rl
, "Too big size %u max_packet_len %d",
1867 size
, dev
->max_packet_len
);
1873 pkts
[txcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1874 if (OVS_UNLIKELY(!pkts
[txcnt
])) {
1879 /* We have to do a copy for now */
1880 memcpy(rte_pktmbuf_mtod(pkts
[txcnt
], void *),
1881 dp_packet_data(packet
), size
);
1882 dp_packet_set_size((struct dp_packet
*)pkts
[txcnt
], size
);
1887 if (OVS_LIKELY(txcnt
)) {
1888 if (dev
->type
== DPDK_DEV_VHOST
) {
1889 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1892 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, txcnt
);
1896 if (OVS_UNLIKELY(dropped
)) {
1897 rte_spinlock_lock(&dev
->stats_lock
);
1898 dev
->stats
.tx_dropped
+= dropped
;
1899 rte_spinlock_unlock(&dev
->stats_lock
);
1904 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1905 struct dp_packet_batch
*batch
,
1906 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1909 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1910 dpdk_do_tx_copy(netdev
, qid
, batch
);
1911 dp_packet_delete_batch(batch
, may_steal
);
1913 dp_packet_batch_apply_cutlen(batch
);
1914 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1920 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1921 struct dp_packet_batch
*batch
, bool may_steal
,
1922 bool concurrent_txq
)
1924 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1925 dp_packet_delete_batch(batch
, may_steal
);
1929 if (OVS_UNLIKELY(concurrent_txq
)) {
1930 qid
= qid
% dev
->up
.n_txq
;
1931 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1934 if (OVS_UNLIKELY(!may_steal
||
1935 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1936 struct netdev
*netdev
= &dev
->up
;
1938 dpdk_do_tx_copy(netdev
, qid
, batch
);
1939 dp_packet_delete_batch(batch
, may_steal
);
1941 int tx_cnt
, dropped
;
1942 int batch_cnt
= dp_packet_batch_size(batch
);
1943 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1945 dp_packet_batch_apply_cutlen(batch
);
1947 tx_cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, batch_cnt
);
1948 tx_cnt
= netdev_dpdk_qos_run(dev
, pkts
, tx_cnt
, true);
1949 dropped
= batch_cnt
- tx_cnt
;
1951 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, tx_cnt
);
1953 if (OVS_UNLIKELY(dropped
)) {
1954 rte_spinlock_lock(&dev
->stats_lock
);
1955 dev
->stats
.tx_dropped
+= dropped
;
1956 rte_spinlock_unlock(&dev
->stats_lock
);
1960 if (OVS_UNLIKELY(concurrent_txq
)) {
1961 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1966 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1967 struct dp_packet_batch
*batch
, bool may_steal
,
1968 bool concurrent_txq
)
1970 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1972 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1977 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1979 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1981 ovs_mutex_lock(&dev
->mutex
);
1982 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1984 netdev_change_seq_changed(netdev
);
1986 ovs_mutex_unlock(&dev
->mutex
);
1992 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1994 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1996 ovs_mutex_lock(&dev
->mutex
);
1998 ovs_mutex_unlock(&dev
->mutex
);
2004 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
2006 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2008 ovs_mutex_lock(&dev
->mutex
);
2010 ovs_mutex_unlock(&dev
->mutex
);
2016 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
2018 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2020 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
2021 || mtu
< ETHER_MIN_MTU
) {
2022 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
2026 ovs_mutex_lock(&dev
->mutex
);
2027 if (dev
->requested_mtu
!= mtu
) {
2028 dev
->requested_mtu
= mtu
;
2029 netdev_request_reconfigure(netdev
);
2031 ovs_mutex_unlock(&dev
->mutex
);
2037 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2040 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2041 struct netdev_stats
*stats
)
2043 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2045 ovs_mutex_lock(&dev
->mutex
);
2047 rte_spinlock_lock(&dev
->stats_lock
);
2048 /* Supported Stats */
2049 stats
->rx_packets
= dev
->stats
.rx_packets
;
2050 stats
->tx_packets
= dev
->stats
.tx_packets
;
2051 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2052 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2053 stats
->multicast
= dev
->stats
.multicast
;
2054 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2055 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2056 stats
->rx_errors
= dev
->stats
.rx_errors
;
2057 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2059 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2060 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2061 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2062 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2063 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2064 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2065 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2067 rte_spinlock_unlock(&dev
->stats_lock
);
2069 ovs_mutex_unlock(&dev
->mutex
);
2075 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2076 const struct rte_eth_xstat
*xstats
,
2077 const struct rte_eth_xstat_name
*names
,
2078 const unsigned int size
)
2080 for (unsigned int i
= 0; i
< size
; i
++) {
2081 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2082 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2083 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2084 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2085 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2086 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2087 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2088 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2089 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2090 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2091 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2092 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2093 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2094 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2095 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2096 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2097 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2098 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2099 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2100 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2101 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2102 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2103 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2104 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2105 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2106 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2107 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2108 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2109 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2110 stats
->multicast
= xstats
[i
].value
;
2111 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2112 stats
->tx_multicast_packets
= xstats
[i
].value
;
2113 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2114 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2115 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2116 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2117 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2118 stats
->rx_undersized_errors
= xstats
[i
].value
;
2119 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2120 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2121 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2122 stats
->rx_jabber_errors
= xstats
[i
].value
;
2128 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2130 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2131 struct rte_eth_stats rte_stats
;
2134 netdev_dpdk_get_carrier(netdev
, &gg
);
2135 ovs_mutex_lock(&dev
->mutex
);
2137 struct rte_eth_xstat
*rte_xstats
= NULL
;
2138 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2139 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2141 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2142 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2143 ovs_mutex_unlock(&dev
->mutex
);
2147 /* Get length of statistics */
2148 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2149 if (rte_xstats_len
< 0) {
2150 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2153 /* Reserve memory for xstats names and values */
2154 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2155 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2157 /* Retreive xstats names */
2158 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2161 if (rte_xstats_new_len
!= rte_xstats_len
) {
2162 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2165 /* Retreive xstats values */
2166 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2167 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2169 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2170 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2173 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2178 free(rte_xstats_names
);
2180 stats
->rx_packets
= rte_stats
.ipackets
;
2181 stats
->tx_packets
= rte_stats
.opackets
;
2182 stats
->rx_bytes
= rte_stats
.ibytes
;
2183 stats
->tx_bytes
= rte_stats
.obytes
;
2184 stats
->rx_errors
= rte_stats
.ierrors
;
2185 stats
->tx_errors
= rte_stats
.oerrors
;
2187 rte_spinlock_lock(&dev
->stats_lock
);
2188 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2189 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2190 rte_spinlock_unlock(&dev
->stats_lock
);
2192 /* These are the available DPDK counters for packets not received due to
2193 * local resource constraints in DPDK and NIC respectively. */
2194 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2195 stats
->rx_missed_errors
= rte_stats
.imissed
;
2197 ovs_mutex_unlock(&dev
->mutex
);
2203 netdev_dpdk_get_features(const struct netdev
*netdev
,
2204 enum netdev_features
*current
,
2205 enum netdev_features
*advertised
,
2206 enum netdev_features
*supported
,
2207 enum netdev_features
*peer
)
2209 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2210 struct rte_eth_link link
;
2212 ovs_mutex_lock(&dev
->mutex
);
2214 ovs_mutex_unlock(&dev
->mutex
);
2216 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2217 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2218 *current
= NETDEV_F_10MB_HD
;
2220 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2221 *current
= NETDEV_F_100MB_HD
;
2223 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2224 *current
= NETDEV_F_1GB_HD
;
2226 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2227 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2228 *current
= NETDEV_F_10MB_FD
;
2230 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2231 *current
= NETDEV_F_100MB_FD
;
2233 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2234 *current
= NETDEV_F_1GB_FD
;
2236 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2237 *current
= NETDEV_F_10GB_FD
;
2241 if (link
.link_autoneg
) {
2242 *current
|= NETDEV_F_AUTONEG
;
2245 *advertised
= *supported
= *peer
= 0;
2250 static struct ingress_policer
*
2251 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2253 struct ingress_policer
*policer
= NULL
;
2254 uint64_t rate_bytes
;
2255 uint64_t burst_bytes
;
2258 policer
= xmalloc(sizeof *policer
);
2259 rte_spinlock_init(&policer
->policer_lock
);
2261 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2262 rate_bytes
= rate
* 1000ULL / 8;
2263 burst_bytes
= burst
* 1000ULL / 8;
2265 policer
->app_srtcm_params
.cir
= rate_bytes
;
2266 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2267 policer
->app_srtcm_params
.ebs
= 0;
2268 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2269 &policer
->app_srtcm_params
);
2271 VLOG_ERR("Could not create rte meter for ingress policer");
2279 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2280 uint32_t policer_burst
)
2282 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2283 struct ingress_policer
*policer
;
2285 /* Force to 0 if no rate specified,
2286 * default to 8000 kbits if burst is 0,
2287 * else stick with user-specified value.
2289 policer_burst
= (!policer_rate
? 0
2290 : !policer_burst
? 8000
2293 ovs_mutex_lock(&dev
->mutex
);
2295 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2296 &dev
->ingress_policer
);
2298 if (dev
->policer_rate
== policer_rate
&&
2299 dev
->policer_burst
== policer_burst
) {
2300 /* Assume that settings haven't changed since we last set them. */
2301 ovs_mutex_unlock(&dev
->mutex
);
2305 /* Destroy any existing ingress policer for the device if one exists */
2307 ovsrcu_postpone(free
, policer
);
2310 if (policer_rate
!= 0) {
2311 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2315 ovsrcu_set(&dev
->ingress_policer
, policer
);
2316 dev
->policer_rate
= policer_rate
;
2317 dev
->policer_burst
= policer_burst
;
2318 ovs_mutex_unlock(&dev
->mutex
);
2324 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2326 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2328 ovs_mutex_lock(&dev
->mutex
);
2329 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2330 * postive integer to meet RFC 2863 recommendations.
2332 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2333 ovs_mutex_unlock(&dev
->mutex
);
2339 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2341 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2343 ovs_mutex_lock(&dev
->mutex
);
2344 check_link_status(dev
);
2345 *carrier
= dev
->link
.link_status
;
2347 ovs_mutex_unlock(&dev
->mutex
);
2353 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2355 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2357 ovs_mutex_lock(&dev
->mutex
);
2359 if (is_vhost_running(dev
)) {
2365 ovs_mutex_unlock(&dev
->mutex
);
2370 static long long int
2371 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2373 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2374 long long int carrier_resets
;
2376 ovs_mutex_lock(&dev
->mutex
);
2377 carrier_resets
= dev
->link_reset_cnt
;
2378 ovs_mutex_unlock(&dev
->mutex
);
2380 return carrier_resets
;
2384 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2385 long long int interval OVS_UNUSED
)
2391 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2392 enum netdev_flags off
, enum netdev_flags on
,
2393 enum netdev_flags
*old_flagsp
)
2394 OVS_REQUIRES(dev
->mutex
)
2396 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2400 *old_flagsp
= dev
->flags
;
2404 if (dev
->flags
== *old_flagsp
) {
2408 if (dev
->type
== DPDK_DEV_ETH
) {
2409 if (dev
->flags
& NETDEV_PROMISC
) {
2410 rte_eth_promiscuous_enable(dev
->port_id
);
2413 netdev_change_seq_changed(&dev
->up
);
2415 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2416 * running then change netdev's change_seq to trigger link state
2419 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2420 && is_vhost_running(dev
)) {
2421 netdev_change_seq_changed(&dev
->up
);
2423 /* Clear statistics if device is getting up. */
2424 if (NETDEV_UP
& on
) {
2425 rte_spinlock_lock(&dev
->stats_lock
);
2426 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2427 rte_spinlock_unlock(&dev
->stats_lock
);
2436 netdev_dpdk_update_flags(struct netdev
*netdev
,
2437 enum netdev_flags off
, enum netdev_flags on
,
2438 enum netdev_flags
*old_flagsp
)
2440 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2443 ovs_mutex_lock(&dev
->mutex
);
2444 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2445 ovs_mutex_unlock(&dev
->mutex
);
2451 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2453 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2454 struct rte_eth_dev_info dev_info
;
2456 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2460 ovs_mutex_lock(&dev
->mutex
);
2461 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2462 ovs_mutex_unlock(&dev
->mutex
);
2464 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2465 smap_add_format(args
, "numa_id", "%d",
2466 rte_eth_dev_socket_id(dev
->port_id
));
2467 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2468 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2469 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2470 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2471 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2472 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2473 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2474 dev_info
.max_hash_mac_addrs
);
2475 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2476 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2478 if (dev_info
.pci_dev
) {
2479 smap_add_format(args
, "pci-vendor_id", "0x%u",
2480 dev_info
.pci_dev
->id
.vendor_id
);
2481 smap_add_format(args
, "pci-device_id", "0x%x",
2482 dev_info
.pci_dev
->id
.device_id
);
2489 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2490 OVS_REQUIRES(dev
->mutex
)
2492 enum netdev_flags old_flags
;
2495 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2497 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2502 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2503 const char *argv
[], void *aux OVS_UNUSED
)
2507 if (!strcasecmp(argv
[argc
- 1], "up")) {
2509 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2512 unixctl_command_reply_error(conn
, "Invalid Admin State");
2517 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2518 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2519 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2521 ovs_mutex_lock(&dpdk_dev
->mutex
);
2522 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2523 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2525 netdev_close(netdev
);
2527 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2528 netdev_close(netdev
);
2532 struct netdev_dpdk
*netdev
;
2534 ovs_mutex_lock(&dpdk_mutex
);
2535 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2536 ovs_mutex_lock(&netdev
->mutex
);
2537 netdev_dpdk_set_admin_state__(netdev
, up
);
2538 ovs_mutex_unlock(&netdev
->mutex
);
2540 ovs_mutex_unlock(&dpdk_mutex
);
2542 unixctl_command_reply(conn
, "OK");
2546 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2547 const char *argv
[], void *aux OVS_UNUSED
)
2552 char devname
[RTE_ETH_NAME_MAX_LEN
];
2553 struct netdev_dpdk
*dev
;
2555 ovs_mutex_lock(&dpdk_mutex
);
2557 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv
[1],
2559 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2563 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2565 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2566 "Remove it before detaching",
2567 argv
[1], netdev_get_name(&dev
->up
));
2571 rte_eth_dev_close(port_id
);
2573 ret
= rte_eth_dev_detach(port_id
, devname
);
2575 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2579 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2581 ovs_mutex_unlock(&dpdk_mutex
);
2582 unixctl_command_reply(conn
, response
);
2587 ovs_mutex_unlock(&dpdk_mutex
);
2588 unixctl_command_reply_error(conn
, response
);
2593 * Set virtqueue flags so that we do not receive interrupts.
2596 set_irq_status(int vid
)
2600 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2601 rte_vhost_enable_guest_notification(vid
, i
, 0);
2606 * Fixes mapping for vhost-user tx queues. Must be called after each
2607 * enabling/disabling of queues and n_txq modifications.
2610 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2611 OVS_REQUIRES(dev
->mutex
)
2613 int *enabled_queues
, n_enabled
= 0;
2614 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2616 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2618 for (i
= 0; i
< total_txqs
; i
++) {
2619 /* Enabled queues always mapped to themselves. */
2620 if (dev
->tx_q
[i
].map
== i
) {
2621 enabled_queues
[n_enabled
++] = i
;
2625 if (n_enabled
== 0 && total_txqs
!= 0) {
2626 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2631 for (i
= 0; i
< total_txqs
; i
++) {
2632 if (dev
->tx_q
[i
].map
!= i
) {
2633 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2634 k
= (k
+ 1) % n_enabled
;
2638 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2639 for (i
= 0; i
< total_txqs
; i
++) {
2640 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2643 free(enabled_queues
);
2647 * A new virtio-net device is added to a vhost port.
2652 struct netdev_dpdk
*dev
;
2653 bool exists
= false;
2655 char ifname
[IF_NAME_SZ
];
2657 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2659 ovs_mutex_lock(&dpdk_mutex
);
2660 /* Add device to the vhost port with the same name as that passed down. */
2661 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2662 ovs_mutex_lock(&dev
->mutex
);
2663 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2664 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
2666 /* Get NUMA information */
2667 newnode
= rte_vhost_get_numa_node(vid
);
2668 if (newnode
== -1) {
2670 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2673 newnode
= dev
->socket_id
;
2676 if (dev
->requested_n_txq
!= qp_num
2677 || dev
->requested_n_rxq
!= qp_num
2678 || dev
->requested_socket_id
!= newnode
) {
2679 dev
->requested_socket_id
= newnode
;
2680 dev
->requested_n_rxq
= qp_num
;
2681 dev
->requested_n_txq
= qp_num
;
2682 netdev_request_reconfigure(&dev
->up
);
2684 /* Reconfiguration not required. */
2685 dev
->vhost_reconfigured
= true;
2688 ovsrcu_index_set(&dev
->vid
, vid
);
2691 /* Disable notifications. */
2692 set_irq_status(vid
);
2693 netdev_change_seq_changed(&dev
->up
);
2694 ovs_mutex_unlock(&dev
->mutex
);
2697 ovs_mutex_unlock(&dev
->mutex
);
2699 ovs_mutex_unlock(&dpdk_mutex
);
2702 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2707 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2713 /* Clears mapping for all available queues of vhost interface. */
2715 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2716 OVS_REQUIRES(dev
->mutex
)
2720 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2721 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2726 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2727 * flag to stop any more packets from being sent or received to/from a VM and
2728 * ensure all currently queued packets have been sent/received before removing
2732 destroy_device(int vid
)
2734 struct netdev_dpdk
*dev
;
2735 bool exists
= false;
2736 char ifname
[IF_NAME_SZ
];
2738 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2740 ovs_mutex_lock(&dpdk_mutex
);
2741 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2742 if (netdev_dpdk_get_vid(dev
) == vid
) {
2744 ovs_mutex_lock(&dev
->mutex
);
2745 dev
->vhost_reconfigured
= false;
2746 ovsrcu_index_set(&dev
->vid
, -1);
2747 netdev_dpdk_txq_map_clear(dev
);
2749 netdev_change_seq_changed(&dev
->up
);
2750 ovs_mutex_unlock(&dev
->mutex
);
2756 ovs_mutex_unlock(&dpdk_mutex
);
2760 * Wait for other threads to quiesce after setting the 'virtio_dev'
2761 * to NULL, before returning.
2763 ovsrcu_synchronize();
2765 * As call to ovsrcu_synchronize() will end the quiescent state,
2766 * put thread back into quiescent state before returning.
2768 ovsrcu_quiesce_start();
2769 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2771 VLOG_INFO("vHost Device '%s' not found", ifname
);
2776 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2778 struct netdev_dpdk
*dev
;
2779 bool exists
= false;
2780 int qid
= queue_id
/ VIRTIO_QNUM
;
2781 char ifname
[IF_NAME_SZ
];
2783 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2785 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2789 ovs_mutex_lock(&dpdk_mutex
);
2790 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2791 ovs_mutex_lock(&dev
->mutex
);
2792 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2794 dev
->tx_q
[qid
].map
= qid
;
2796 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2798 netdev_dpdk_remap_txqs(dev
);
2800 ovs_mutex_unlock(&dev
->mutex
);
2803 ovs_mutex_unlock(&dev
->mutex
);
2805 ovs_mutex_unlock(&dpdk_mutex
);
2808 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2809 "changed to \'%s\'", queue_id
, qid
, ifname
,
2810 (enable
== 1) ? "enabled" : "disabled");
2812 VLOG_INFO("vHost Device '%s' not found", ifname
);
2820 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2822 return ovsrcu_index_get(&dev
->vid
);
2825 struct ingress_policer
*
2826 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2828 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2832 netdev_dpdk_class_init(void)
2834 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2836 /* This function can be called for different classes. The initialization
2837 * needs to be done only once */
2838 if (ovsthread_once_start(&once
)) {
2839 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2840 unixctl_command_register("netdev-dpdk/set-admin-state",
2841 "[netdev] up|down", 1, 2,
2842 netdev_dpdk_set_admin_state
, NULL
);
2844 unixctl_command_register("netdev-dpdk/detach",
2845 "pci address of device", 1, 1,
2846 netdev_dpdk_detach
, NULL
);
2848 ovsthread_once_done(&once
);
2858 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2859 dpdk_port_t
*eth_port_id
)
2861 struct dpdk_ring
*ring_pair
;
2865 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2870 /* XXX: Add support for multiquque ring. */
2871 ring_name
= xasprintf("%s_tx", dev_name
);
2873 /* Create single producer tx ring, netdev does explicit locking. */
2874 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2877 if (ring_pair
->cring_tx
== NULL
) {
2878 rte_free(ring_pair
);
2882 ring_name
= xasprintf("%s_rx", dev_name
);
2884 /* Create single consumer rx ring, netdev does explicit locking. */
2885 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2888 if (ring_pair
->cring_rx
== NULL
) {
2889 rte_free(ring_pair
);
2893 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2894 &ring_pair
->cring_tx
, 1, SOCKET0
);
2897 rte_free(ring_pair
);
2901 ring_pair
->user_port_id
= port_no
;
2902 ring_pair
->eth_port_id
= port_id
;
2903 *eth_port_id
= port_id
;
2905 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2911 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
2912 OVS_REQUIRES(dpdk_mutex
)
2914 struct dpdk_ring
*ring_pair
;
2915 unsigned int port_no
;
2918 /* Names always start with "dpdkr" */
2919 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2924 /* Look through our list to find the device */
2925 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2926 if (ring_pair
->user_port_id
== port_no
) {
2927 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2928 /* Really all that is needed */
2929 *eth_port_id
= ring_pair
->eth_port_id
;
2933 /* Need to create the device rings */
2934 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2938 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2939 struct dp_packet_batch
*batch
, bool may_steal
,
2940 bool concurrent_txq
)
2942 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2943 struct dp_packet
*packet
;
2945 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2946 * the rss hash field is clear. This is because the same mbuf may be
2947 * modified by the consumer of the ring and return into the datapath
2948 * without recalculating the RSS hash. */
2949 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
2950 dp_packet_mbuf_rss_flag_reset(packet
);
2953 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2958 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2960 dpdk_port_t port_no
= 0;
2963 ovs_mutex_lock(&dpdk_mutex
);
2965 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2970 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2971 rte_eth_dev_socket_id(port_no
));
2973 ovs_mutex_unlock(&dpdk_mutex
);
2980 * Initialize QoS configuration operations.
2983 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2986 rte_spinlock_init(&conf
->lock
);
2990 * Search existing QoS operations in qos_ops and compare each set of
2991 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2994 static const struct dpdk_qos_ops
*
2995 qos_lookup_name(const char *name
)
2997 const struct dpdk_qos_ops
*const *opsp
;
2999 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3000 const struct dpdk_qos_ops
*ops
= *opsp
;
3001 if (!strcmp(name
, ops
->qos_name
)) {
3009 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
3012 const struct dpdk_qos_ops
*const *opsp
;
3014 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
3015 const struct dpdk_qos_ops
*ops
= *opsp
;
3016 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
3017 sset_add(types
, ops
->qos_name
);
3024 netdev_dpdk_get_qos(const struct netdev
*netdev
,
3025 const char **typep
, struct smap
*details
)
3027 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3028 struct qos_conf
*qos_conf
;
3031 ovs_mutex_lock(&dev
->mutex
);
3032 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3034 *typep
= qos_conf
->ops
->qos_name
;
3035 error
= (qos_conf
->ops
->qos_get
3036 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3038 /* No QoS configuration set, return an empty string */
3041 ovs_mutex_unlock(&dev
->mutex
);
3047 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3048 const struct smap
*details
)
3050 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3051 const struct dpdk_qos_ops
*new_ops
= NULL
;
3052 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3055 ovs_mutex_lock(&dev
->mutex
);
3057 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3059 new_ops
= qos_lookup_name(type
);
3061 if (!new_ops
|| !new_ops
->qos_construct
) {
3062 new_qos_conf
= NULL
;
3063 if (type
&& type
[0]) {
3066 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3067 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3068 new_qos_conf
= qos_conf
;
3070 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3074 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3075 type
, netdev
->name
, rte_strerror(error
));
3078 if (new_qos_conf
!= qos_conf
) {
3079 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3081 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3085 ovs_mutex_unlock(&dev
->mutex
);
3090 /* egress-policer details */
3092 struct egress_policer
{
3093 struct qos_conf qos_conf
;
3094 struct rte_meter_srtcm_params app_srtcm_params
;
3095 struct rte_meter_srtcm egress_meter
;
3099 egress_policer_details_to_param(const struct smap
*details
,
3100 struct rte_meter_srtcm_params
*params
)
3102 memset(params
, 0, sizeof *params
);
3103 params
->cir
= smap_get_ullong(details
, "cir", 0);
3104 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3109 egress_policer_qos_construct(const struct smap
*details
,
3110 struct qos_conf
**conf
)
3112 struct egress_policer
*policer
;
3115 policer
= xmalloc(sizeof *policer
);
3116 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3117 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3118 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3119 &policer
->app_srtcm_params
);
3121 *conf
= &policer
->qos_conf
;
3132 egress_policer_qos_destruct(struct qos_conf
*conf
)
3134 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3140 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3142 struct egress_policer
*policer
=
3143 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3145 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3146 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3152 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3153 const struct smap
*details
)
3155 struct egress_policer
*policer
=
3156 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3157 struct rte_meter_srtcm_params params
;
3159 egress_policer_details_to_param(details
, ¶ms
);
3161 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3165 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
,
3169 struct egress_policer
*policer
=
3170 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3172 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
,
3173 pkt_cnt
, may_steal
);
3178 static const struct dpdk_qos_ops egress_policer_ops
= {
3179 "egress-policer", /* qos_name */
3180 egress_policer_qos_construct
,
3181 egress_policer_qos_destruct
,
3182 egress_policer_qos_get
,
3183 egress_policer_qos_is_equal
,
3188 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3190 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3193 ovs_mutex_lock(&dev
->mutex
);
3195 if (netdev
->n_txq
== dev
->requested_n_txq
3196 && netdev
->n_rxq
== dev
->requested_n_rxq
3197 && dev
->mtu
== dev
->requested_mtu
3198 && dev
->rxq_size
== dev
->requested_rxq_size
3199 && dev
->txq_size
== dev
->requested_txq_size
3200 && dev
->socket_id
== dev
->requested_socket_id
) {
3201 /* Reconfiguration is unnecessary */
3206 rte_eth_dev_stop(dev
->port_id
);
3208 err
= netdev_dpdk_mempool_configure(dev
);
3213 netdev
->n_txq
= dev
->requested_n_txq
;
3214 netdev
->n_rxq
= dev
->requested_n_rxq
;
3216 dev
->rxq_size
= dev
->requested_rxq_size
;
3217 dev
->txq_size
= dev
->requested_txq_size
;
3219 rte_free(dev
->tx_q
);
3220 err
= dpdk_eth_dev_init(dev
);
3221 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3226 netdev_change_seq_changed(netdev
);
3229 ovs_mutex_unlock(&dev
->mutex
);
3234 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3235 OVS_REQUIRES(dev
->mutex
)
3237 dev
->up
.n_txq
= dev
->requested_n_txq
;
3238 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3241 /* Enable TX queue 0 by default if it wasn't disabled. */
3242 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3243 dev
->tx_q
[0].map
= 0;
3246 netdev_dpdk_remap_txqs(dev
);
3248 err
= netdev_dpdk_mempool_configure(dev
);
3252 netdev_change_seq_changed(&dev
->up
);
3255 if (netdev_dpdk_get_vid(dev
) >= 0) {
3256 if (dev
->vhost_reconfigured
== false) {
3257 dev
->vhost_reconfigured
= true;
3258 /* Carrier status may need updating. */
3259 netdev_change_seq_changed(&dev
->up
);
3267 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3269 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3272 ovs_mutex_lock(&dev
->mutex
);
3273 err
= dpdk_vhost_reconfigure_helper(dev
);
3274 ovs_mutex_unlock(&dev
->mutex
);
3280 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3282 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3285 ovs_mutex_lock(&dev
->mutex
);
3287 /* Configure vHost client mode if requested and if the following criteria
3289 * 1. Device hasn't been registered yet.
3290 * 2. A path has been specified.
3292 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3293 && strlen(dev
->vhost_id
)) {
3294 /* Register client-mode device */
3295 err
= rte_vhost_driver_register(dev
->vhost_id
,
3296 RTE_VHOST_USER_CLIENT
);
3298 VLOG_ERR("vhost-user device setup failure for device %s\n",
3302 /* Configuration successful */
3303 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3304 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3305 "using client socket '%s'",
3306 dev
->up
.name
, dev
->vhost_id
);
3309 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3310 &virtio_net_device_ops
);
3312 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3313 "vhost user client port: %s\n", dev
->up
.name
);
3317 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3318 1ULL << VIRTIO_NET_F_HOST_TSO4
3319 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3320 | 1ULL << VIRTIO_NET_F_CSUM
);
3322 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3323 "client port: %s\n", dev
->up
.name
);
3327 err
= rte_vhost_driver_start(dev
->vhost_id
);
3329 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3330 "client port: %s\n", dev
->up
.name
);
3335 err
= dpdk_vhost_reconfigure_helper(dev
);
3338 ovs_mutex_unlock(&dev
->mutex
);
3343 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3344 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3345 GET_CARRIER, GET_STATS, \
3346 GET_FEATURES, GET_STATUS, \
3347 RECONFIGURE, RXQ_RECV) \
3350 true, /* is_pmd */ \
3352 NULL, /* netdev_dpdk_run */ \
3353 NULL, /* netdev_dpdk_wait */ \
3355 netdev_dpdk_alloc, \
3358 netdev_dpdk_dealloc, \
3359 netdev_dpdk_get_config, \
3361 NULL, /* get_tunnel_config */ \
3362 NULL, /* build header */ \
3363 NULL, /* push header */ \
3364 NULL, /* pop header */ \
3365 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3369 NULL, /* send_wait */ \
3371 netdev_dpdk_set_etheraddr, \
3372 netdev_dpdk_get_etheraddr, \
3373 netdev_dpdk_get_mtu, \
3374 netdev_dpdk_set_mtu, \
3375 netdev_dpdk_get_ifindex, \
3377 netdev_dpdk_get_carrier_resets, \
3378 netdev_dpdk_set_miimon, \
3381 NULL, /* set_advertisements */ \
3382 NULL, /* get_pt_mode */ \
3384 netdev_dpdk_set_policing, \
3385 netdev_dpdk_get_qos_types, \
3386 NULL, /* get_qos_capabilities */ \
3387 netdev_dpdk_get_qos, \
3388 netdev_dpdk_set_qos, \
3389 NULL, /* get_queue */ \
3390 NULL, /* set_queue */ \
3391 NULL, /* delete_queue */ \
3392 NULL, /* get_queue_stats */ \
3393 NULL, /* queue_dump_start */ \
3394 NULL, /* queue_dump_next */ \
3395 NULL, /* queue_dump_done */ \
3396 NULL, /* dump_queue_stats */ \
3398 NULL, /* set_in4 */ \
3399 NULL, /* get_addr_list */ \
3400 NULL, /* add_router */ \
3401 NULL, /* get_next_hop */ \
3403 NULL, /* arp_lookup */ \
3405 netdev_dpdk_update_flags, \
3408 netdev_dpdk_rxq_alloc, \
3409 netdev_dpdk_rxq_construct, \
3410 netdev_dpdk_rxq_destruct, \
3411 netdev_dpdk_rxq_dealloc, \
3413 NULL, /* rx_wait */ \
3414 NULL, /* rxq_drain */ \
3418 static const struct netdev_class dpdk_class
=
3421 netdev_dpdk_class_init
,
3422 netdev_dpdk_construct
,
3423 netdev_dpdk_destruct
,
3424 netdev_dpdk_set_config
,
3425 netdev_dpdk_set_tx_multiq
,
3426 netdev_dpdk_eth_send
,
3427 netdev_dpdk_get_carrier
,
3428 netdev_dpdk_get_stats
,
3429 netdev_dpdk_get_features
,
3430 netdev_dpdk_get_status
,
3431 netdev_dpdk_reconfigure
,
3432 netdev_dpdk_rxq_recv
);
3434 static const struct netdev_class dpdk_ring_class
=
3437 netdev_dpdk_class_init
,
3438 netdev_dpdk_ring_construct
,
3439 netdev_dpdk_destruct
,
3440 netdev_dpdk_ring_set_config
,
3441 netdev_dpdk_set_tx_multiq
,
3442 netdev_dpdk_ring_send
,
3443 netdev_dpdk_get_carrier
,
3444 netdev_dpdk_get_stats
,
3445 netdev_dpdk_get_features
,
3446 netdev_dpdk_get_status
,
3447 netdev_dpdk_reconfigure
,
3448 netdev_dpdk_rxq_recv
);
3450 static const struct netdev_class dpdk_vhost_class
=
3454 netdev_dpdk_vhost_construct
,
3455 netdev_dpdk_vhost_destruct
,
3458 netdev_dpdk_vhost_send
,
3459 netdev_dpdk_vhost_get_carrier
,
3460 netdev_dpdk_vhost_get_stats
,
3463 netdev_dpdk_vhost_reconfigure
,
3464 netdev_dpdk_vhost_rxq_recv
);
3465 static const struct netdev_class dpdk_vhost_client_class
=
3467 "dpdkvhostuserclient",
3469 netdev_dpdk_vhost_client_construct
,
3470 netdev_dpdk_vhost_destruct
,
3471 netdev_dpdk_vhost_client_set_config
,
3473 netdev_dpdk_vhost_send
,
3474 netdev_dpdk_vhost_get_carrier
,
3475 netdev_dpdk_vhost_get_stats
,
3478 netdev_dpdk_vhost_client_reconfigure
,
3479 netdev_dpdk_vhost_rxq_recv
);
3482 netdev_dpdk_register(void)
3484 netdev_register_provider(&dpdk_class
);
3485 netdev_register_provider(&dpdk_ring_class
);
3486 netdev_register_provider(&dpdk_vhost_class
);
3487 netdev_register_provider(&dpdk_vhost_client_class
);