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
,
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
;
306 static struct ovs_list dpdk_mp_list
OVS_GUARDED_BY(dpdk_mp_mutex
)
307 = OVS_LIST_INITIALIZER(&dpdk_mp_list
);
310 struct rte_mempool
*mp
;
314 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
317 /* There should be one 'struct dpdk_tx_queue' created for
319 struct dpdk_tx_queue
{
320 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
321 * from concurrent access. It is used only
322 * if the queue is shared among different
323 * pmd threads (see 'concurrent_txq'). */
324 int map
; /* Mapping of configured vhost-user queues
325 * to enabled by guest. */
328 /* dpdk has no way to remove dpdk ring ethernet devices
329 so we have to keep them around once they've been created
332 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
333 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
336 /* For the client rings */
337 struct rte_ring
*cring_tx
;
338 struct rte_ring
*cring_rx
;
339 unsigned int user_port_id
; /* User given port no, parsed from port name */
340 dpdk_port_t eth_port_id
; /* ethernet device port id */
341 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
344 struct ingress_policer
{
345 struct rte_meter_srtcm_params app_srtcm_params
;
346 struct rte_meter_srtcm in_policer
;
347 rte_spinlock_t policer_lock
;
350 enum dpdk_hw_ol_features
{
351 NETDEV_RX_CHECKSUM_OFFLOAD
= 1 << 0,
358 enum dpdk_dev_type type
;
360 struct dpdk_tx_queue
*tx_q
;
362 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
364 struct dpdk_mp
*dpdk_mp
;
368 struct netdev_stats stats
;
370 rte_spinlock_t stats_lock
;
372 struct eth_addr hwaddr
;
373 enum netdev_flags flags
;
375 struct rte_eth_link link
;
378 /* virtio identifier for vhost devices */
381 /* True if vHost device is 'up' and has been reconfigured at least once */
382 bool vhost_reconfigured
;
384 /* Identifier used to distinguish vhost devices from each other. */
385 char vhost_id
[PATH_MAX
];
387 /* Device arguments for dpdk ports */
390 /* If true, device was attached by rte_eth_dev_attach(). */
394 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
396 /* QoS configuration and lock for the device */
397 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
399 /* The following properties cannot be changed when a device is running,
400 * so we remember the request and update them next time
401 * netdev_dpdk*_reconfigure() is called */
405 int requested_rxq_size
;
406 int requested_txq_size
;
408 /* Number of rx/tx descriptors for physical devices */
412 /* Socket ID detected when vHost device is brought up */
413 int requested_socket_id
;
415 /* Denotes whether vHost port is client/server mode */
416 uint64_t vhost_driver_flags
;
418 /* Ingress Policer */
419 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
420 uint32_t policer_rate
;
421 uint32_t policer_burst
;
423 /* DPDK-ETH Flow control */
424 struct rte_eth_fc_conf fc_conf
;
426 /* DPDK-ETH hardware offload features,
427 * from the enum set 'dpdk_hw_ol_features' */
428 uint32_t hw_ol_features
;
431 struct netdev_rxq_dpdk
{
432 struct netdev_rxq up
;
436 static void netdev_dpdk_destruct(struct netdev
*netdev
);
437 static void netdev_dpdk_vhost_destruct(struct netdev
*netdev
);
439 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
441 struct ingress_policer
*
442 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
445 is_dpdk_class(const struct netdev_class
*class)
447 return class->destruct
== netdev_dpdk_destruct
448 || class->destruct
== netdev_dpdk_vhost_destruct
;
451 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
452 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
453 * value, insufficient buffers are allocated to accomodate the packet in its
454 * entirety. Furthermore, certain drivers need to ensure that there is also
455 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
456 * frames). If the RX buffer is too small, then the driver enables scatter RX
457 * behaviour, which reduces performance. To prevent this, use a buffer size
458 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
461 dpdk_buf_size(int mtu
)
463 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
464 NETDEV_DPDK_MBUF_ALIGN
);
467 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
469 * Unlike xmalloc(), this function can return NULL on failure. */
471 dpdk_rte_mzalloc(size_t sz
)
473 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
477 free_dpdk_buf(struct dp_packet
*p
)
479 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
481 rte_pktmbuf_free(pkt
);
485 ovs_rte_pktmbuf_init(struct rte_mempool
*mp OVS_UNUSED
,
486 void *opaque_arg OVS_UNUSED
,
488 unsigned i OVS_UNUSED
)
490 struct rte_mbuf
*pkt
= _p
;
492 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
495 static struct dpdk_mp
*
496 dpdk_mp_create(int socket_id
, int mtu
)
502 dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
506 dmp
->socket_id
= socket_id
;
509 /* XXX: this is a really rough method of provisioning memory.
510 * It's impossible to determine what the exact memory requirements are
511 * when the number of ports and rxqs that utilize a particular mempool can
512 * change dynamically at runtime. For now, use this rough heurisitic.
514 if (mtu
>= ETHER_MTU
) {
515 mp_size
= MAX_NB_MBUF
;
517 mp_size
= MIN_NB_MBUF
;
521 mp_name
= xasprintf("ovs_mp_%d_%d_%u", dmp
->mtu
, dmp
->socket_id
,
524 dmp
->mp
= rte_pktmbuf_pool_create(mp_name
, mp_size
,
526 sizeof (struct dp_packet
)
527 - sizeof (struct rte_mbuf
),
529 - sizeof(struct dp_packet
),
532 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
537 /* rte_pktmbuf_pool_create has done some initialization of the
538 * rte_mbuf part of each dp_packet, while ovs_rte_pktmbuf_init
539 * initializes some OVS specific fields of dp_packet.
541 rte_mempool_obj_iter(dmp
->mp
, ovs_rte_pktmbuf_init
, NULL
);
544 } while (rte_errno
== ENOMEM
&& (mp_size
/= 2) >= MIN_NB_MBUF
);
550 static struct dpdk_mp
*
551 dpdk_mp_get(int socket_id
, int mtu
)
555 ovs_mutex_lock(&dpdk_mp_mutex
);
556 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
557 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
563 dmp
= dpdk_mp_create(socket_id
, mtu
);
565 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
569 ovs_mutex_unlock(&dpdk_mp_mutex
);
575 dpdk_mp_put(struct dpdk_mp
*dmp
)
581 ovs_mutex_lock(&dpdk_mp_mutex
);
582 ovs_assert(dmp
->refcount
);
584 if (!--dmp
->refcount
) {
585 ovs_list_remove(&dmp
->list_node
);
586 rte_mempool_free(dmp
->mp
);
589 ovs_mutex_unlock(&dpdk_mp_mutex
);
592 /* Tries to allocate new mempool on requested_socket_id with
593 * mbuf size corresponding to requested_mtu.
594 * On success new configuration will be applied.
595 * On error, device will be left unchanged. */
597 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
598 OVS_REQUIRES(dev
->mutex
)
600 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
603 mp
= dpdk_mp_get(dev
->requested_socket_id
, FRAME_LEN_TO_MTU(buf_size
));
605 VLOG_ERR("Failed to create memory pool for netdev "
606 "%s, with MTU %d on socket %d: %s\n",
607 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
,
608 rte_strerror(rte_errno
));
611 dpdk_mp_put(dev
->dpdk_mp
);
613 dev
->mtu
= dev
->requested_mtu
;
614 dev
->socket_id
= dev
->requested_socket_id
;
615 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
622 check_link_status(struct netdev_dpdk
*dev
)
624 struct rte_eth_link link
;
626 rte_eth_link_get_nowait(dev
->port_id
, &link
);
628 if (dev
->link
.link_status
!= link
.link_status
) {
629 netdev_change_seq_changed(&dev
->up
);
631 dev
->link_reset_cnt
++;
633 if (dev
->link
.link_status
) {
634 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Up - speed %u Mbps - %s",
635 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
636 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
637 ("full-duplex") : ("half-duplex"));
639 VLOG_DBG_RL(&rl
, "Port %"PRIu8
" Link Down", dev
->port_id
);
645 dpdk_watchdog(void *dummy OVS_UNUSED
)
647 struct netdev_dpdk
*dev
;
649 pthread_detach(pthread_self());
652 ovs_mutex_lock(&dpdk_mutex
);
653 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
654 ovs_mutex_lock(&dev
->mutex
);
655 if (dev
->type
== DPDK_DEV_ETH
) {
656 check_link_status(dev
);
658 ovs_mutex_unlock(&dev
->mutex
);
660 ovs_mutex_unlock(&dpdk_mutex
);
661 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
668 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
672 struct rte_eth_conf conf
= port_conf
;
674 /* For some NICs (e.g. Niantic), scatter_rx mode needs to be explicitly
676 if (dev
->mtu
> ETHER_MTU
) {
677 conf
.rxmode
.enable_scatter
= 1;
680 conf
.rxmode
.hw_ip_checksum
= (dev
->hw_ol_features
&
681 NETDEV_RX_CHECKSUM_OFFLOAD
) != 0;
682 /* A device may report more queues than it makes available (this has
683 * been observed for Intel xl710, which reserves some of them for
684 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
685 * available. When this happens we can retry the configuration
686 * and request less queues */
687 while (n_rxq
&& n_txq
) {
689 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
692 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
694 VLOG_WARN("Interface %s eth_dev setup error %s\n",
695 dev
->up
.name
, rte_strerror(-diag
));
699 diag
= rte_eth_dev_set_mtu(dev
->port_id
, dev
->mtu
);
701 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
702 dev
->up
.name
, dev
->mtu
, rte_strerror(-diag
));
706 for (i
= 0; i
< n_txq
; i
++) {
707 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
708 dev
->socket_id
, NULL
);
710 VLOG_INFO("Interface %s txq(%d) setup error: %s",
711 dev
->up
.name
, i
, rte_strerror(-diag
));
717 /* Retry with less tx queues */
722 for (i
= 0; i
< n_rxq
; i
++) {
723 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
724 dev
->socket_id
, NULL
,
727 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
728 dev
->up
.name
, i
, rte_strerror(-diag
));
734 /* Retry with less rx queues */
739 dev
->up
.n_rxq
= n_rxq
;
740 dev
->up
.n_txq
= n_txq
;
749 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
751 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
752 VLOG_WARN("Failed to enable flow control on device %"PRIu8
,
758 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
759 OVS_REQUIRES(dev
->mutex
)
761 struct rte_pktmbuf_pool_private
*mbp_priv
;
762 struct rte_eth_dev_info info
;
763 struct ether_addr eth_addr
;
766 uint32_t rx_chksm_offload_capa
= DEV_RX_OFFLOAD_UDP_CKSUM
|
767 DEV_RX_OFFLOAD_TCP_CKSUM
|
768 DEV_RX_OFFLOAD_IPV4_CKSUM
;
770 rte_eth_dev_info_get(dev
->port_id
, &info
);
772 if ((info
.rx_offload_capa
& rx_chksm_offload_capa
) !=
773 rx_chksm_offload_capa
) {
774 VLOG_WARN("Rx checksum offload is not supported on port %"PRIu8
,
776 dev
->hw_ol_features
&= ~NETDEV_RX_CHECKSUM_OFFLOAD
;
778 dev
->hw_ol_features
|= NETDEV_RX_CHECKSUM_OFFLOAD
;
781 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
782 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
784 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
786 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
787 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
791 diag
= rte_eth_dev_start(dev
->port_id
);
793 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
794 rte_strerror(-diag
));
798 rte_eth_promiscuous_enable(dev
->port_id
);
799 rte_eth_allmulticast_enable(dev
->port_id
);
801 memset(ð_addr
, 0x0, sizeof(eth_addr
));
802 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
803 VLOG_INFO_RL(&rl
, "Port %"PRIu8
": "ETH_ADDR_FMT
,
804 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
806 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
807 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
809 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
810 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
812 /* Get the Flow control configuration for DPDK-ETH */
813 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
815 VLOG_DBG("cannot get flow control parameters on port=%"PRIu8
", err=%d",
822 static struct netdev_dpdk
*
823 netdev_dpdk_cast(const struct netdev
*netdev
)
825 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
828 static struct netdev
*
829 netdev_dpdk_alloc(void)
831 struct netdev_dpdk
*dev
;
833 dev
= dpdk_rte_mzalloc(sizeof *dev
);
841 static struct dpdk_tx_queue
*
842 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
844 struct dpdk_tx_queue
*txqs
;
847 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
849 for (i
= 0; i
< n_txqs
; i
++) {
850 /* Initialize map for vhost devices. */
851 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
852 rte_spinlock_init(&txqs
[i
].tx_lock
);
860 common_construct(struct netdev
*netdev
, dpdk_port_t port_no
,
861 enum dpdk_dev_type type
, int socket_id
)
862 OVS_REQUIRES(dpdk_mutex
)
864 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
866 ovs_mutex_init(&dev
->mutex
);
868 rte_spinlock_init(&dev
->stats_lock
);
870 /* If the 'sid' is negative, it means that the kernel fails
871 * to obtain the pci numa info. In that situation, always
873 dev
->socket_id
= socket_id
< 0 ? SOCKET0
: socket_id
;
874 dev
->requested_socket_id
= dev
->socket_id
;
875 dev
->port_id
= port_no
;
878 dev
->requested_mtu
= ETHER_MTU
;
879 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
880 ovsrcu_index_init(&dev
->vid
, -1);
881 dev
->vhost_reconfigured
= false;
882 dev
->attached
= false;
884 ovsrcu_init(&dev
->qos_conf
, NULL
);
886 ovsrcu_init(&dev
->ingress_policer
, NULL
);
887 dev
->policer_rate
= 0;
888 dev
->policer_burst
= 0;
892 dev
->requested_n_rxq
= NR_QUEUE
;
893 dev
->requested_n_txq
= NR_QUEUE
;
894 dev
->requested_rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
895 dev
->requested_txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
897 /* Initialize the flow control to NULL */
898 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
900 /* Initilize the hardware offload flags to 0 */
901 dev
->hw_ol_features
= 0;
903 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
905 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
907 netdev_request_reconfigure(netdev
);
912 /* dev_name must be the prefix followed by a positive decimal number.
913 * (no leading + or - signs are allowed) */
915 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
916 unsigned int *port_no
)
920 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
924 cport
= dev_name
+ strlen(prefix
);
926 if (str_to_uint(cport
, 10, port_no
)) {
934 vhost_common_construct(struct netdev
*netdev
)
935 OVS_REQUIRES(dpdk_mutex
)
937 int socket_id
= rte_lcore_to_socket_id(rte_get_master_lcore());
938 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
940 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
945 return common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
946 DPDK_DEV_VHOST
, socket_id
);
950 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
952 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
953 const char *name
= netdev
->name
;
956 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
957 * the file system. '/' or '\' would traverse directories, so they're not
958 * acceptable in 'name'. */
959 if (strchr(name
, '/') || strchr(name
, '\\')) {
960 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
961 "A valid name must not include '/' or '\\'",
966 ovs_mutex_lock(&dpdk_mutex
);
967 /* Take the name of the vhost-user port and append it to the location where
968 * the socket is to be created, then register the socket.
970 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
971 dpdk_get_vhost_sock_dir(), name
);
973 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
974 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
976 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
980 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
981 VLOG_INFO("Socket %s created for vhost-user port %s\n",
982 dev
->vhost_id
, name
);
985 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
986 &virtio_net_device_ops
);
988 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
993 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
994 1ULL << VIRTIO_NET_F_HOST_TSO4
995 | 1ULL << VIRTIO_NET_F_HOST_TSO6
996 | 1ULL << VIRTIO_NET_F_CSUM
);
998 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1003 err
= rte_vhost_driver_start(dev
->vhost_id
);
1005 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1006 "port: %s\n", name
);
1010 err
= vhost_common_construct(netdev
);
1012 VLOG_ERR("vhost_common_construct failed for vhost user "
1013 "port: %s\n", name
);
1017 ovs_mutex_unlock(&dpdk_mutex
);
1018 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1019 "please migrate to dpdkvhostuserclient ports.");
1024 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
1028 ovs_mutex_lock(&dpdk_mutex
);
1029 err
= vhost_common_construct(netdev
);
1031 VLOG_ERR("vhost_common_construct failed for vhost user client"
1032 "port: %s\n", netdev
->name
);
1034 ovs_mutex_unlock(&dpdk_mutex
);
1039 netdev_dpdk_construct(struct netdev
*netdev
)
1043 ovs_mutex_lock(&dpdk_mutex
);
1044 err
= common_construct(netdev
, DPDK_ETH_PORT_ID_INVALID
,
1045 DPDK_DEV_ETH
, SOCKET0
);
1046 ovs_mutex_unlock(&dpdk_mutex
);
1051 common_destruct(struct netdev_dpdk
*dev
)
1052 OVS_REQUIRES(dpdk_mutex
)
1053 OVS_EXCLUDED(dev
->mutex
)
1055 rte_free(dev
->tx_q
);
1056 dpdk_mp_put(dev
->dpdk_mp
);
1058 ovs_list_remove(&dev
->list_node
);
1059 free(ovsrcu_get_protected(struct ingress_policer
*,
1060 &dev
->ingress_policer
));
1061 ovs_mutex_destroy(&dev
->mutex
);
1065 netdev_dpdk_destruct(struct netdev
*netdev
)
1067 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1068 char devname
[RTE_ETH_NAME_MAX_LEN
];
1070 ovs_mutex_lock(&dpdk_mutex
);
1072 rte_eth_dev_stop(dev
->port_id
);
1074 if (dev
->attached
) {
1075 rte_eth_dev_close(dev
->port_id
);
1076 if (rte_eth_dev_detach(dev
->port_id
, devname
) < 0) {
1077 VLOG_ERR("Device '%s' can not be detached", dev
->devargs
);
1079 VLOG_INFO("Device '%s' has been detached", devname
);
1084 common_destruct(dev
);
1086 ovs_mutex_unlock(&dpdk_mutex
);
1089 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
1090 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1091 * deadlock, none of the mutexes must be held while calling this function. */
1093 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1095 OVS_EXCLUDED(dpdk_mutex
)
1096 OVS_EXCLUDED(dev
->mutex
)
1098 return rte_vhost_driver_unregister(vhost_id
);
1102 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1104 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1107 ovs_mutex_lock(&dpdk_mutex
);
1109 /* Guest becomes an orphan if still attached. */
1110 if (netdev_dpdk_get_vid(dev
) >= 0
1111 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1112 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1114 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1115 "socket '%s' must be restarted.", dev
->vhost_id
);
1118 vhost_id
= xstrdup(dev
->vhost_id
);
1120 common_destruct(dev
);
1122 ovs_mutex_unlock(&dpdk_mutex
);
1128 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1129 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1130 netdev
->name
, vhost_id
);
1131 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1132 /* OVS server mode - remove this socket from list for deletion */
1133 fatal_signal_remove_file_to_unlink(vhost_id
);
1140 netdev_dpdk_dealloc(struct netdev
*netdev
)
1142 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1148 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1150 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1152 ovs_mutex_lock(&dev
->mutex
);
1154 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1155 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1156 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1157 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1158 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1160 if (dev
->type
== DPDK_DEV_ETH
) {
1161 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1162 dev
->requested_rxq_size
);
1163 smap_add_format(args
, "configured_rxq_descriptors", "%d",
1165 smap_add_format(args
, "requested_txq_descriptors", "%d",
1166 dev
->requested_txq_size
);
1167 smap_add_format(args
, "configured_txq_descriptors", "%d",
1169 if (dev
->hw_ol_features
& NETDEV_RX_CHECKSUM_OFFLOAD
) {
1170 smap_add(args
, "rx_csum_offload", "true");
1172 smap_add(args
, "rx_csum_offload", "false");
1175 ovs_mutex_unlock(&dev
->mutex
);
1180 static struct netdev_dpdk
*
1181 netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id
)
1182 OVS_REQUIRES(dpdk_mutex
)
1184 struct netdev_dpdk
*dev
;
1186 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
1187 if (dev
->port_id
== port_id
) {
1196 netdev_dpdk_process_devargs(struct netdev_dpdk
*dev
,
1197 const char *devargs
, char **errp
)
1199 /* Get the name up to the first comma. */
1200 char *name
= xmemdup0(devargs
, strcspn(devargs
, ","));
1201 dpdk_port_t new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1203 if (!rte_eth_dev_count()
1204 || rte_eth_dev_get_port_by_name(name
, &new_port_id
)
1205 || !rte_eth_dev_is_valid_port(new_port_id
)) {
1206 /* Device not found in DPDK, attempt to attach it */
1207 if (!rte_eth_dev_attach(devargs
, &new_port_id
)) {
1208 /* Attach successful */
1209 dev
->attached
= true;
1210 VLOG_INFO("Device '%s' attached to DPDK", devargs
);
1212 /* Attach unsuccessful */
1213 new_port_id
= DPDK_ETH_PORT_ID_INVALID
;
1214 VLOG_WARN_BUF(errp
, "Error attaching device '%s' to DPDK",
1224 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1225 OVS_REQUIRES(dev
->mutex
)
1229 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", NR_QUEUE
), 1);
1230 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1231 dev
->requested_n_rxq
= new_n_rxq
;
1232 netdev_request_reconfigure(&dev
->up
);
1237 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1238 const char *flag
, int default_size
, int *new_size
)
1240 int queue_size
= smap_get_int(args
, flag
, default_size
);
1242 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1243 || !is_pow2(queue_size
)) {
1244 queue_size
= default_size
;
1247 if (queue_size
!= *new_size
) {
1248 *new_size
= queue_size
;
1249 netdev_request_reconfigure(netdev
);
1254 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
,
1257 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1258 bool rx_fc_en
, tx_fc_en
, autoneg
;
1259 enum rte_eth_fc_mode fc_mode
;
1260 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1261 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1262 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1264 const char *new_devargs
;
1267 ovs_mutex_lock(&dpdk_mutex
);
1268 ovs_mutex_lock(&dev
->mutex
);
1270 dpdk_set_rxq_config(dev
, args
);
1272 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1273 NIC_PORT_DEFAULT_RXQ_SIZE
,
1274 &dev
->requested_rxq_size
);
1275 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1276 NIC_PORT_DEFAULT_TXQ_SIZE
,
1277 &dev
->requested_txq_size
);
1279 new_devargs
= smap_get(args
, "dpdk-devargs");
1281 if (dev
->devargs
&& strcmp(new_devargs
, dev
->devargs
)) {
1282 /* The user requested a new device. If we return error, the caller
1283 * will delete this netdev and try to recreate it. */
1288 /* dpdk-devargs is required for device configuration */
1289 if (new_devargs
&& new_devargs
[0]) {
1290 /* Don't process dpdk-devargs if value is unchanged and port id
1292 if (!(dev
->devargs
&& !strcmp(dev
->devargs
, new_devargs
)
1293 && rte_eth_dev_is_valid_port(dev
->port_id
))) {
1294 dpdk_port_t new_port_id
= netdev_dpdk_process_devargs(dev
,
1297 if (!rte_eth_dev_is_valid_port(new_port_id
)) {
1299 } else if (new_port_id
== dev
->port_id
) {
1300 /* Already configured, do not reconfigure again */
1303 struct netdev_dpdk
*dup_dev
;
1305 dup_dev
= netdev_dpdk_lookup_by_port_id(new_port_id
);
1307 VLOG_WARN_BUF(errp
, "'%s' is trying to use device '%s' "
1308 "which is already in use by '%s'",
1309 netdev_get_name(netdev
), new_devargs
,
1310 netdev_get_name(&dup_dev
->up
));
1313 int sid
= rte_eth_dev_socket_id(new_port_id
);
1315 dev
->requested_socket_id
= sid
< 0 ? SOCKET0
: sid
;
1316 dev
->devargs
= xstrdup(new_devargs
);
1317 dev
->port_id
= new_port_id
;
1318 netdev_request_reconfigure(&dev
->up
);
1324 VLOG_WARN_BUF(errp
, "'%s' is missing 'options:dpdk-devargs'. "
1325 "The old 'dpdk<port_id>' names are not supported",
1326 netdev_get_name(netdev
));
1334 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1335 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1336 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1338 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1339 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1340 dev
->fc_conf
.mode
= fc_mode
;
1341 dev
->fc_conf
.autoneg
= autoneg
;
1342 dpdk_eth_flow_ctrl_setup(dev
);
1346 ovs_mutex_unlock(&dev
->mutex
);
1347 ovs_mutex_unlock(&dpdk_mutex
);
1353 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
,
1354 char **errp OVS_UNUSED
)
1356 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1358 ovs_mutex_lock(&dev
->mutex
);
1359 dpdk_set_rxq_config(dev
, args
);
1360 ovs_mutex_unlock(&dev
->mutex
);
1366 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1367 const struct smap
*args
,
1368 char **errp OVS_UNUSED
)
1370 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1373 ovs_mutex_lock(&dev
->mutex
);
1374 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1375 path
= smap_get(args
, "vhost-server-path");
1376 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1377 strcpy(dev
->vhost_id
, path
);
1378 netdev_request_reconfigure(netdev
);
1381 ovs_mutex_unlock(&dev
->mutex
);
1387 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1389 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1391 return dev
->socket_id
;
1394 /* Sets the number of tx queues for the dpdk interface. */
1396 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1398 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1400 ovs_mutex_lock(&dev
->mutex
);
1402 if (dev
->requested_n_txq
== n_txq
) {
1406 dev
->requested_n_txq
= n_txq
;
1407 netdev_request_reconfigure(netdev
);
1410 ovs_mutex_unlock(&dev
->mutex
);
1414 static struct netdev_rxq
*
1415 netdev_dpdk_rxq_alloc(void)
1417 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1426 static struct netdev_rxq_dpdk
*
1427 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1429 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1433 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1435 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1436 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1438 ovs_mutex_lock(&dev
->mutex
);
1439 rx
->port_id
= dev
->port_id
;
1440 ovs_mutex_unlock(&dev
->mutex
);
1446 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1451 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1453 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1458 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1459 * 'pkts', even in case of failure.
1461 * Returns the number of packets that weren't transmitted. */
1463 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1464 struct rte_mbuf
**pkts
, int cnt
)
1468 while (nb_tx
!= cnt
) {
1471 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1479 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1480 /* Free buffers, which we couldn't transmit, one at a time (each
1481 * packet could come from a different mempool) */
1484 for (i
= nb_tx
; i
< cnt
; i
++) {
1485 rte_pktmbuf_free(pkts
[i
]);
1493 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1494 struct rte_mbuf
*pkt
, uint64_t time
)
1496 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1498 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1503 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1504 struct rte_mbuf
**pkts
, int pkt_cnt
)
1508 struct rte_mbuf
*pkt
= NULL
;
1509 uint64_t current_time
= rte_rdtsc();
1511 for (i
= 0; i
< pkt_cnt
; i
++) {
1513 /* Handle current packet */
1514 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1520 rte_pktmbuf_free(pkt
);
1528 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1533 rte_spinlock_lock(&policer
->policer_lock
);
1534 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
, pkt_cnt
);
1535 rte_spinlock_unlock(&policer
->policer_lock
);
1541 is_vhost_running(struct netdev_dpdk
*dev
)
1543 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1547 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1548 unsigned int packet_size
)
1550 /* Hard-coded search for the size bucket. */
1551 if (packet_size
< 256) {
1552 if (packet_size
>= 128) {
1553 stats
->rx_128_to_255_packets
++;
1554 } else if (packet_size
<= 64) {
1555 stats
->rx_1_to_64_packets
++;
1557 stats
->rx_65_to_127_packets
++;
1560 if (packet_size
>= 1523) {
1561 stats
->rx_1523_to_max_packets
++;
1562 } else if (packet_size
>= 1024) {
1563 stats
->rx_1024_to_1522_packets
++;
1564 } else if (packet_size
< 512) {
1565 stats
->rx_256_to_511_packets
++;
1567 stats
->rx_512_to_1023_packets
++;
1573 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1574 struct dp_packet
**packets
, int count
,
1578 unsigned int packet_size
;
1579 struct dp_packet
*packet
;
1581 stats
->rx_packets
+= count
;
1582 stats
->rx_dropped
+= dropped
;
1583 for (i
= 0; i
< count
; i
++) {
1584 packet
= packets
[i
];
1585 packet_size
= dp_packet_size(packet
);
1587 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1588 /* This only protects the following multicast counting from
1589 * too short packets, but it does not stop the packet from
1590 * further processing. */
1592 stats
->rx_length_errors
++;
1596 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1598 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1599 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1603 stats
->rx_bytes
+= packet_size
;
1608 * The receive path for the vhost port is the TX path out from guest.
1611 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1612 struct dp_packet_batch
*batch
)
1614 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1615 int qid
= rxq
->queue_id
;
1616 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1618 uint16_t dropped
= 0;
1620 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1621 || !(dev
->flags
& NETDEV_UP
))) {
1625 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1626 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1628 (struct rte_mbuf
**) batch
->packets
,
1636 nb_rx
= ingress_policer_run(policer
,
1637 (struct rte_mbuf
**) batch
->packets
,
1642 rte_spinlock_lock(&dev
->stats_lock
);
1643 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1645 rte_spinlock_unlock(&dev
->stats_lock
);
1647 dp_packet_batch_init_cutlen(batch
);
1648 batch
->count
= (int) nb_rx
;
1653 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1655 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1656 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1657 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1661 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1665 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1666 (struct rte_mbuf
**) batch
->packets
,
1674 nb_rx
= ingress_policer_run(policer
,
1675 (struct rte_mbuf
**) batch
->packets
,
1680 /* Update stats to reflect dropped packets */
1681 if (OVS_UNLIKELY(dropped
)) {
1682 rte_spinlock_lock(&dev
->stats_lock
);
1683 dev
->stats
.rx_dropped
+= dropped
;
1684 rte_spinlock_unlock(&dev
->stats_lock
);
1687 dp_packet_batch_init_cutlen(batch
);
1688 batch
->count
= nb_rx
;
1694 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1697 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1700 rte_spinlock_lock(&qos_conf
->lock
);
1701 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
);
1702 rte_spinlock_unlock(&qos_conf
->lock
);
1709 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1714 struct rte_mbuf
*pkt
;
1716 for (i
= 0; i
< pkt_cnt
; i
++) {
1718 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1719 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1720 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1721 rte_pktmbuf_free(pkt
);
1725 if (OVS_UNLIKELY(i
!= cnt
)) {
1735 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1736 struct dp_packet
**packets
,
1741 int sent
= attempted
- dropped
;
1743 stats
->tx_packets
+= sent
;
1744 stats
->tx_dropped
+= dropped
;
1746 for (i
= 0; i
< sent
; i
++) {
1747 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1752 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1753 struct dp_packet
**pkts
, int cnt
)
1755 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1756 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1757 unsigned int total_pkts
= cnt
;
1758 unsigned int dropped
= 0;
1761 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1763 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1764 || !(dev
->flags
& NETDEV_UP
))) {
1765 rte_spinlock_lock(&dev
->stats_lock
);
1766 dev
->stats
.tx_dropped
+= cnt
;
1767 rte_spinlock_unlock(&dev
->stats_lock
);
1771 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1773 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1774 /* Check has QoS has been configured for the netdev */
1775 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
);
1776 dropped
= total_pkts
- cnt
;
1779 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1780 unsigned int tx_pkts
;
1782 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1783 vhost_qid
, cur_pkts
, cnt
);
1784 if (OVS_LIKELY(tx_pkts
)) {
1785 /* Packets have been sent.*/
1787 /* Prepare for possible retry.*/
1788 cur_pkts
= &cur_pkts
[tx_pkts
];
1790 /* No packets sent - do not retry.*/
1793 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1795 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1797 rte_spinlock_lock(&dev
->stats_lock
);
1798 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1800 rte_spinlock_unlock(&dev
->stats_lock
);
1803 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1804 dp_packet_delete(pkts
[i
]);
1808 /* Tx function. Transmit packets indefinitely */
1810 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1811 OVS_NO_THREAD_SAFETY_ANALYSIS
1813 #if !defined(__CHECKER__) && !defined(_WIN32)
1814 const size_t PKT_ARRAY_SIZE
= batch
->count
;
1816 /* Sparse or MSVC doesn't like variable length array. */
1817 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1819 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1820 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1825 dp_packet_batch_apply_cutlen(batch
);
1827 for (i
= 0; i
< batch
->count
; i
++) {
1828 int size
= dp_packet_size(batch
->packets
[i
]);
1830 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1831 VLOG_WARN_RL(&rl
, "Too big size %d max_packet_len %d",
1832 (int) size
, dev
->max_packet_len
);
1838 pkts
[newcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1840 if (!pkts
[newcnt
]) {
1841 dropped
+= batch
->count
- i
;
1845 /* We have to do a copy for now */
1846 memcpy(rte_pktmbuf_mtod(pkts
[newcnt
], void *),
1847 dp_packet_data(batch
->packets
[i
]), size
);
1849 rte_pktmbuf_data_len(pkts
[newcnt
]) = size
;
1850 rte_pktmbuf_pkt_len(pkts
[newcnt
]) = size
;
1855 if (dev
->type
== DPDK_DEV_VHOST
) {
1856 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1859 unsigned int qos_pkts
= newcnt
;
1861 /* Check if QoS has been configured for this netdev. */
1862 newcnt
= netdev_dpdk_qos_run(dev
, pkts
, newcnt
);
1864 dropped
+= qos_pkts
- newcnt
;
1865 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, newcnt
);
1868 if (OVS_UNLIKELY(dropped
)) {
1869 rte_spinlock_lock(&dev
->stats_lock
);
1870 dev
->stats
.tx_dropped
+= dropped
;
1871 rte_spinlock_unlock(&dev
->stats_lock
);
1876 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1877 struct dp_packet_batch
*batch
,
1878 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1881 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1882 dpdk_do_tx_copy(netdev
, qid
, batch
);
1883 dp_packet_delete_batch(batch
, may_steal
);
1885 dp_packet_batch_apply_cutlen(batch
);
1886 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1892 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1893 struct dp_packet_batch
*batch
, bool may_steal
,
1894 bool concurrent_txq
)
1896 if (OVS_UNLIKELY(!(dev
->flags
& NETDEV_UP
))) {
1897 dp_packet_delete_batch(batch
, may_steal
);
1901 if (OVS_UNLIKELY(concurrent_txq
)) {
1902 qid
= qid
% dev
->up
.n_txq
;
1903 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1906 if (OVS_UNLIKELY(!may_steal
||
1907 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1908 struct netdev
*netdev
= &dev
->up
;
1910 dpdk_do_tx_copy(netdev
, qid
, batch
);
1911 dp_packet_delete_batch(batch
, may_steal
);
1914 int cnt
= batch
->count
;
1915 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1917 dp_packet_batch_apply_cutlen(batch
);
1919 cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, cnt
);
1920 cnt
= netdev_dpdk_qos_run(dev
, pkts
, cnt
);
1921 dropped
= batch
->count
- cnt
;
1923 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, cnt
);
1925 if (OVS_UNLIKELY(dropped
)) {
1926 rte_spinlock_lock(&dev
->stats_lock
);
1927 dev
->stats
.tx_dropped
+= dropped
;
1928 rte_spinlock_unlock(&dev
->stats_lock
);
1932 if (OVS_UNLIKELY(concurrent_txq
)) {
1933 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1938 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1939 struct dp_packet_batch
*batch
, bool may_steal
,
1940 bool concurrent_txq
)
1942 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1944 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1949 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1951 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1953 ovs_mutex_lock(&dev
->mutex
);
1954 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1956 netdev_change_seq_changed(netdev
);
1958 ovs_mutex_unlock(&dev
->mutex
);
1964 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1966 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1968 ovs_mutex_lock(&dev
->mutex
);
1970 ovs_mutex_unlock(&dev
->mutex
);
1976 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1978 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1980 ovs_mutex_lock(&dev
->mutex
);
1982 ovs_mutex_unlock(&dev
->mutex
);
1988 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
1990 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1992 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
1993 || mtu
< ETHER_MIN_MTU
) {
1994 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
1998 ovs_mutex_lock(&dev
->mutex
);
1999 if (dev
->requested_mtu
!= mtu
) {
2000 dev
->requested_mtu
= mtu
;
2001 netdev_request_reconfigure(netdev
);
2003 ovs_mutex_unlock(&dev
->mutex
);
2009 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
2012 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
2013 struct netdev_stats
*stats
)
2015 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2017 ovs_mutex_lock(&dev
->mutex
);
2019 rte_spinlock_lock(&dev
->stats_lock
);
2020 /* Supported Stats */
2021 stats
->rx_packets
+= dev
->stats
.rx_packets
;
2022 stats
->tx_packets
+= dev
->stats
.tx_packets
;
2023 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2024 stats
->tx_dropped
+= dev
->stats
.tx_dropped
;
2025 stats
->multicast
= dev
->stats
.multicast
;
2026 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
2027 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
2028 stats
->rx_errors
= dev
->stats
.rx_errors
;
2029 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
2031 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
2032 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
2033 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
2034 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
2035 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
2036 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
2037 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
2039 rte_spinlock_unlock(&dev
->stats_lock
);
2041 ovs_mutex_unlock(&dev
->mutex
);
2047 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
2048 const struct rte_eth_xstat
*xstats
,
2049 const struct rte_eth_xstat_name
*names
,
2050 const unsigned int size
)
2052 for (unsigned int i
= 0; i
< size
; i
++) {
2053 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
2054 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
2055 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2056 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
2057 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2058 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
2059 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2060 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
2061 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2062 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
2063 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2064 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
2065 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2066 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
2067 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
2068 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
2069 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
2070 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
2071 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
2072 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
2073 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
2074 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
2075 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
2076 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
2077 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
2078 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
2079 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
2080 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
2081 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2082 stats
->multicast
= xstats
[i
].value
;
2083 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
2084 stats
->tx_multicast_packets
= xstats
[i
].value
;
2085 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2086 stats
->rx_broadcast_packets
= xstats
[i
].value
;
2087 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
2088 stats
->tx_broadcast_packets
= xstats
[i
].value
;
2089 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
2090 stats
->rx_undersized_errors
= xstats
[i
].value
;
2091 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
2092 stats
->rx_fragmented_errors
= xstats
[i
].value
;
2093 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
2094 stats
->rx_jabber_errors
= xstats
[i
].value
;
2100 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
2102 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2103 struct rte_eth_stats rte_stats
;
2106 netdev_dpdk_get_carrier(netdev
, &gg
);
2107 ovs_mutex_lock(&dev
->mutex
);
2109 struct rte_eth_xstat
*rte_xstats
= NULL
;
2110 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
2111 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
2113 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
2114 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8
, dev
->port_id
);
2115 ovs_mutex_unlock(&dev
->mutex
);
2119 /* Get length of statistics */
2120 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
2121 if (rte_xstats_len
< 0) {
2122 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2125 /* Reserve memory for xstats names and values */
2126 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
2127 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
2129 /* Retreive xstats names */
2130 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
2133 if (rte_xstats_new_len
!= rte_xstats_len
) {
2134 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8
, dev
->port_id
);
2137 /* Retreive xstats values */
2138 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
2139 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
2141 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
2142 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
2145 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8
, dev
->port_id
);
2150 free(rte_xstats_names
);
2152 stats
->rx_packets
= rte_stats
.ipackets
;
2153 stats
->tx_packets
= rte_stats
.opackets
;
2154 stats
->rx_bytes
= rte_stats
.ibytes
;
2155 stats
->tx_bytes
= rte_stats
.obytes
;
2156 stats
->rx_errors
= rte_stats
.ierrors
;
2157 stats
->tx_errors
= rte_stats
.oerrors
;
2159 rte_spinlock_lock(&dev
->stats_lock
);
2160 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
2161 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
2162 rte_spinlock_unlock(&dev
->stats_lock
);
2164 /* These are the available DPDK counters for packets not received due to
2165 * local resource constraints in DPDK and NIC respectively. */
2166 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
2167 stats
->rx_missed_errors
= rte_stats
.imissed
;
2169 ovs_mutex_unlock(&dev
->mutex
);
2175 netdev_dpdk_get_features(const struct netdev
*netdev
,
2176 enum netdev_features
*current
,
2177 enum netdev_features
*advertised
,
2178 enum netdev_features
*supported
,
2179 enum netdev_features
*peer
)
2181 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2182 struct rte_eth_link link
;
2184 ovs_mutex_lock(&dev
->mutex
);
2186 ovs_mutex_unlock(&dev
->mutex
);
2188 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
2189 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2190 *current
= NETDEV_F_10MB_HD
;
2192 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2193 *current
= NETDEV_F_100MB_HD
;
2195 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2196 *current
= NETDEV_F_1GB_HD
;
2198 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
2199 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
2200 *current
= NETDEV_F_10MB_FD
;
2202 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
2203 *current
= NETDEV_F_100MB_FD
;
2205 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
2206 *current
= NETDEV_F_1GB_FD
;
2208 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
2209 *current
= NETDEV_F_10GB_FD
;
2213 if (link
.link_autoneg
) {
2214 *current
|= NETDEV_F_AUTONEG
;
2217 *advertised
= *supported
= *peer
= 0;
2222 static struct ingress_policer
*
2223 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2225 struct ingress_policer
*policer
= NULL
;
2226 uint64_t rate_bytes
;
2227 uint64_t burst_bytes
;
2230 policer
= xmalloc(sizeof *policer
);
2231 rte_spinlock_init(&policer
->policer_lock
);
2233 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2234 rate_bytes
= rate
* 1000/8;
2235 burst_bytes
= burst
* 1000/8;
2237 policer
->app_srtcm_params
.cir
= rate_bytes
;
2238 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2239 policer
->app_srtcm_params
.ebs
= 0;
2240 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2241 &policer
->app_srtcm_params
);
2243 VLOG_ERR("Could not create rte meter for ingress policer");
2251 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2252 uint32_t policer_burst
)
2254 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2255 struct ingress_policer
*policer
;
2257 /* Force to 0 if no rate specified,
2258 * default to 8000 kbits if burst is 0,
2259 * else stick with user-specified value.
2261 policer_burst
= (!policer_rate
? 0
2262 : !policer_burst
? 8000
2265 ovs_mutex_lock(&dev
->mutex
);
2267 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2268 &dev
->ingress_policer
);
2270 if (dev
->policer_rate
== policer_rate
&&
2271 dev
->policer_burst
== policer_burst
) {
2272 /* Assume that settings haven't changed since we last set them. */
2273 ovs_mutex_unlock(&dev
->mutex
);
2277 /* Destroy any existing ingress policer for the device if one exists */
2279 ovsrcu_postpone(free
, policer
);
2282 if (policer_rate
!= 0) {
2283 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2287 ovsrcu_set(&dev
->ingress_policer
, policer
);
2288 dev
->policer_rate
= policer_rate
;
2289 dev
->policer_burst
= policer_burst
;
2290 ovs_mutex_unlock(&dev
->mutex
);
2296 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2298 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2300 ovs_mutex_lock(&dev
->mutex
);
2301 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2302 * postive integer to meet RFC 2863 recommendations.
2304 int ifindex
= hash_string(netdev
->name
, 0) % 0xfffffe + 1;
2305 ovs_mutex_unlock(&dev
->mutex
);
2311 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2313 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2315 ovs_mutex_lock(&dev
->mutex
);
2316 check_link_status(dev
);
2317 *carrier
= dev
->link
.link_status
;
2319 ovs_mutex_unlock(&dev
->mutex
);
2325 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2327 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2329 ovs_mutex_lock(&dev
->mutex
);
2331 if (is_vhost_running(dev
)) {
2337 ovs_mutex_unlock(&dev
->mutex
);
2342 static long long int
2343 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2345 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2346 long long int carrier_resets
;
2348 ovs_mutex_lock(&dev
->mutex
);
2349 carrier_resets
= dev
->link_reset_cnt
;
2350 ovs_mutex_unlock(&dev
->mutex
);
2352 return carrier_resets
;
2356 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2357 long long int interval OVS_UNUSED
)
2363 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2364 enum netdev_flags off
, enum netdev_flags on
,
2365 enum netdev_flags
*old_flagsp
)
2366 OVS_REQUIRES(dev
->mutex
)
2368 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2372 *old_flagsp
= dev
->flags
;
2376 if (dev
->flags
== *old_flagsp
) {
2380 if (dev
->type
== DPDK_DEV_ETH
) {
2381 if (dev
->flags
& NETDEV_PROMISC
) {
2382 rte_eth_promiscuous_enable(dev
->port_id
);
2385 netdev_change_seq_changed(&dev
->up
);
2387 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2388 * running then change netdev's change_seq to trigger link state
2391 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2392 && is_vhost_running(dev
)) {
2393 netdev_change_seq_changed(&dev
->up
);
2395 /* Clear statistics if device is getting up. */
2396 if (NETDEV_UP
& on
) {
2397 rte_spinlock_lock(&dev
->stats_lock
);
2398 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2399 rte_spinlock_unlock(&dev
->stats_lock
);
2408 netdev_dpdk_update_flags(struct netdev
*netdev
,
2409 enum netdev_flags off
, enum netdev_flags on
,
2410 enum netdev_flags
*old_flagsp
)
2412 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2415 ovs_mutex_lock(&dev
->mutex
);
2416 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2417 ovs_mutex_unlock(&dev
->mutex
);
2423 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2425 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2426 struct rte_eth_dev_info dev_info
;
2428 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2432 ovs_mutex_lock(&dev
->mutex
);
2433 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2434 ovs_mutex_unlock(&dev
->mutex
);
2436 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2437 smap_add_format(args
, "numa_id", "%d",
2438 rte_eth_dev_socket_id(dev
->port_id
));
2439 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2440 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2441 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2442 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2443 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2444 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2445 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2446 dev_info
.max_hash_mac_addrs
);
2447 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2448 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2450 if (dev_info
.pci_dev
) {
2451 smap_add_format(args
, "pci-vendor_id", "0x%u",
2452 dev_info
.pci_dev
->id
.vendor_id
);
2453 smap_add_format(args
, "pci-device_id", "0x%x",
2454 dev_info
.pci_dev
->id
.device_id
);
2461 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2462 OVS_REQUIRES(dev
->mutex
)
2464 enum netdev_flags old_flags
;
2467 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2469 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2474 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2475 const char *argv
[], void *aux OVS_UNUSED
)
2479 if (!strcasecmp(argv
[argc
- 1], "up")) {
2481 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2484 unixctl_command_reply_error(conn
, "Invalid Admin State");
2489 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2490 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2491 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2493 ovs_mutex_lock(&dpdk_dev
->mutex
);
2494 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2495 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2497 netdev_close(netdev
);
2499 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2500 netdev_close(netdev
);
2504 struct netdev_dpdk
*netdev
;
2506 ovs_mutex_lock(&dpdk_mutex
);
2507 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2508 ovs_mutex_lock(&netdev
->mutex
);
2509 netdev_dpdk_set_admin_state__(netdev
, up
);
2510 ovs_mutex_unlock(&netdev
->mutex
);
2512 ovs_mutex_unlock(&dpdk_mutex
);
2514 unixctl_command_reply(conn
, "OK");
2518 netdev_dpdk_detach(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
2519 const char *argv
[], void *aux OVS_UNUSED
)
2524 char devname
[RTE_ETH_NAME_MAX_LEN
];
2525 struct netdev_dpdk
*dev
;
2527 ovs_mutex_lock(&dpdk_mutex
);
2529 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv
[1],
2531 response
= xasprintf("Device '%s' not found in DPDK", argv
[1]);
2535 dev
= netdev_dpdk_lookup_by_port_id(port_id
);
2537 response
= xasprintf("Device '%s' is being used by interface '%s'. "
2538 "Remove it before detaching",
2539 argv
[1], netdev_get_name(&dev
->up
));
2543 rte_eth_dev_close(port_id
);
2545 ret
= rte_eth_dev_detach(port_id
, devname
);
2547 response
= xasprintf("Device '%s' can not be detached", argv
[1]);
2551 response
= xasprintf("Device '%s' has been detached", argv
[1]);
2553 ovs_mutex_unlock(&dpdk_mutex
);
2554 unixctl_command_reply(conn
, response
);
2559 ovs_mutex_unlock(&dpdk_mutex
);
2560 unixctl_command_reply_error(conn
, response
);
2565 * Set virtqueue flags so that we do not receive interrupts.
2568 set_irq_status(int vid
)
2572 for (i
= 0; i
< rte_vhost_get_vring_num(vid
); i
++) {
2573 rte_vhost_enable_guest_notification(vid
, i
, 0);
2578 * Fixes mapping for vhost-user tx queues. Must be called after each
2579 * enabling/disabling of queues and n_txq modifications.
2582 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2583 OVS_REQUIRES(dev
->mutex
)
2585 int *enabled_queues
, n_enabled
= 0;
2586 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2588 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2590 for (i
= 0; i
< total_txqs
; i
++) {
2591 /* Enabled queues always mapped to themselves. */
2592 if (dev
->tx_q
[i
].map
== i
) {
2593 enabled_queues
[n_enabled
++] = i
;
2597 if (n_enabled
== 0 && total_txqs
!= 0) {
2598 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2603 for (i
= 0; i
< total_txqs
; i
++) {
2604 if (dev
->tx_q
[i
].map
!= i
) {
2605 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2606 k
= (k
+ 1) % n_enabled
;
2610 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2611 for (i
= 0; i
< total_txqs
; i
++) {
2612 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2615 free(enabled_queues
);
2619 * A new virtio-net device is added to a vhost port.
2624 struct netdev_dpdk
*dev
;
2625 bool exists
= false;
2627 char ifname
[IF_NAME_SZ
];
2629 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2631 ovs_mutex_lock(&dpdk_mutex
);
2632 /* Add device to the vhost port with the same name as that passed down. */
2633 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2634 ovs_mutex_lock(&dev
->mutex
);
2635 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2636 uint32_t qp_num
= rte_vhost_get_vring_num(vid
)/VIRTIO_QNUM
;
2638 /* Get NUMA information */
2639 newnode
= rte_vhost_get_numa_node(vid
);
2640 if (newnode
== -1) {
2642 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2645 newnode
= dev
->socket_id
;
2648 if (dev
->requested_n_txq
!= qp_num
2649 || dev
->requested_n_rxq
!= qp_num
2650 || dev
->requested_socket_id
!= newnode
) {
2651 dev
->requested_socket_id
= newnode
;
2652 dev
->requested_n_rxq
= qp_num
;
2653 dev
->requested_n_txq
= qp_num
;
2654 netdev_request_reconfigure(&dev
->up
);
2656 /* Reconfiguration not required. */
2657 dev
->vhost_reconfigured
= true;
2660 ovsrcu_index_set(&dev
->vid
, vid
);
2663 /* Disable notifications. */
2664 set_irq_status(vid
);
2665 netdev_change_seq_changed(&dev
->up
);
2666 ovs_mutex_unlock(&dev
->mutex
);
2669 ovs_mutex_unlock(&dev
->mutex
);
2671 ovs_mutex_unlock(&dpdk_mutex
);
2674 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2679 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2685 /* Clears mapping for all available queues of vhost interface. */
2687 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2688 OVS_REQUIRES(dev
->mutex
)
2692 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2693 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2698 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2699 * flag to stop any more packets from being sent or received to/from a VM and
2700 * ensure all currently queued packets have been sent/received before removing
2704 destroy_device(int vid
)
2706 struct netdev_dpdk
*dev
;
2707 bool exists
= false;
2708 char ifname
[IF_NAME_SZ
];
2710 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2712 ovs_mutex_lock(&dpdk_mutex
);
2713 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2714 if (netdev_dpdk_get_vid(dev
) == vid
) {
2716 ovs_mutex_lock(&dev
->mutex
);
2717 dev
->vhost_reconfigured
= false;
2718 ovsrcu_index_set(&dev
->vid
, -1);
2719 netdev_dpdk_txq_map_clear(dev
);
2721 netdev_change_seq_changed(&dev
->up
);
2722 ovs_mutex_unlock(&dev
->mutex
);
2728 ovs_mutex_unlock(&dpdk_mutex
);
2732 * Wait for other threads to quiesce after setting the 'virtio_dev'
2733 * to NULL, before returning.
2735 ovsrcu_synchronize();
2737 * As call to ovsrcu_synchronize() will end the quiescent state,
2738 * put thread back into quiescent state before returning.
2740 ovsrcu_quiesce_start();
2741 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2743 VLOG_INFO("vHost Device '%s' not found", ifname
);
2748 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2750 struct netdev_dpdk
*dev
;
2751 bool exists
= false;
2752 int qid
= queue_id
/ VIRTIO_QNUM
;
2753 char ifname
[IF_NAME_SZ
];
2755 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2757 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2761 ovs_mutex_lock(&dpdk_mutex
);
2762 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2763 ovs_mutex_lock(&dev
->mutex
);
2764 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2766 dev
->tx_q
[qid
].map
= qid
;
2768 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2770 netdev_dpdk_remap_txqs(dev
);
2772 ovs_mutex_unlock(&dev
->mutex
);
2775 ovs_mutex_unlock(&dev
->mutex
);
2777 ovs_mutex_unlock(&dpdk_mutex
);
2780 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2781 "changed to \'%s\'", queue_id
, qid
, ifname
,
2782 (enable
== 1) ? "enabled" : "disabled");
2784 VLOG_INFO("vHost Device '%s' not found", ifname
);
2792 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2794 return ovsrcu_index_get(&dev
->vid
);
2797 struct ingress_policer
*
2798 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2800 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2804 netdev_dpdk_class_init(void)
2806 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
2808 /* This function can be called for different classes. The initialization
2809 * needs to be done only once */
2810 if (ovsthread_once_start(&once
)) {
2811 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
2812 unixctl_command_register("netdev-dpdk/set-admin-state",
2813 "[netdev] up|down", 1, 2,
2814 netdev_dpdk_set_admin_state
, NULL
);
2816 unixctl_command_register("netdev-dpdk/detach",
2817 "pci address of device", 1, 1,
2818 netdev_dpdk_detach
, NULL
);
2820 ovsthread_once_done(&once
);
2830 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2831 dpdk_port_t
*eth_port_id
)
2833 struct dpdk_ring
*ring_pair
;
2837 ring_pair
= dpdk_rte_mzalloc(sizeof *ring_pair
);
2842 /* XXX: Add support for multiquque ring. */
2843 ring_name
= xasprintf("%s_tx", dev_name
);
2845 /* Create single producer tx ring, netdev does explicit locking. */
2846 ring_pair
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2849 if (ring_pair
->cring_tx
== NULL
) {
2850 rte_free(ring_pair
);
2854 ring_name
= xasprintf("%s_rx", dev_name
);
2856 /* Create single consumer rx ring, netdev does explicit locking. */
2857 ring_pair
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2860 if (ring_pair
->cring_rx
== NULL
) {
2861 rte_free(ring_pair
);
2865 port_id
= rte_eth_from_rings(dev_name
, &ring_pair
->cring_rx
, 1,
2866 &ring_pair
->cring_tx
, 1, SOCKET0
);
2869 rte_free(ring_pair
);
2873 ring_pair
->user_port_id
= port_no
;
2874 ring_pair
->eth_port_id
= port_id
;
2875 *eth_port_id
= port_id
;
2877 ovs_list_push_back(&dpdk_ring_list
, &ring_pair
->list_node
);
2883 dpdk_ring_open(const char dev_name
[], dpdk_port_t
*eth_port_id
)
2884 OVS_REQUIRES(dpdk_mutex
)
2886 struct dpdk_ring
*ring_pair
;
2887 unsigned int port_no
;
2890 /* Names always start with "dpdkr" */
2891 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2896 /* Look through our list to find the device */
2897 LIST_FOR_EACH (ring_pair
, list_node
, &dpdk_ring_list
) {
2898 if (ring_pair
->user_port_id
== port_no
) {
2899 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2900 /* Really all that is needed */
2901 *eth_port_id
= ring_pair
->eth_port_id
;
2905 /* Need to create the device rings */
2906 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2910 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2911 struct dp_packet_batch
*batch
, bool may_steal
,
2912 bool concurrent_txq
)
2914 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2917 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2918 * the rss hash field is clear. This is because the same mbuf may be
2919 * modified by the consumer of the ring and return into the datapath
2920 * without recalculating the RSS hash. */
2921 for (i
= 0; i
< batch
->count
; i
++) {
2922 dp_packet_mbuf_rss_flag_reset(batch
->packets
[i
]);
2925 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2930 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2932 dpdk_port_t port_no
= 0;
2935 ovs_mutex_lock(&dpdk_mutex
);
2937 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2942 err
= common_construct(netdev
, port_no
, DPDK_DEV_ETH
,
2943 rte_eth_dev_socket_id(port_no
));
2945 ovs_mutex_unlock(&dpdk_mutex
);
2952 * Initialize QoS configuration operations.
2955 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2958 rte_spinlock_init(&conf
->lock
);
2962 * Search existing QoS operations in qos_ops and compare each set of
2963 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2966 static const struct dpdk_qos_ops
*
2967 qos_lookup_name(const char *name
)
2969 const struct dpdk_qos_ops
*const *opsp
;
2971 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2972 const struct dpdk_qos_ops
*ops
= *opsp
;
2973 if (!strcmp(name
, ops
->qos_name
)) {
2981 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2984 const struct dpdk_qos_ops
*const *opsp
;
2986 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2987 const struct dpdk_qos_ops
*ops
= *opsp
;
2988 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
2989 sset_add(types
, ops
->qos_name
);
2996 netdev_dpdk_get_qos(const struct netdev
*netdev
,
2997 const char **typep
, struct smap
*details
)
2999 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3000 struct qos_conf
*qos_conf
;
3003 ovs_mutex_lock(&dev
->mutex
);
3004 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3006 *typep
= qos_conf
->ops
->qos_name
;
3007 error
= (qos_conf
->ops
->qos_get
3008 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
3010 /* No QoS configuration set, return an empty string */
3013 ovs_mutex_unlock(&dev
->mutex
);
3019 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
3020 const struct smap
*details
)
3022 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3023 const struct dpdk_qos_ops
*new_ops
= NULL
;
3024 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
3027 ovs_mutex_lock(&dev
->mutex
);
3029 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
3031 new_ops
= qos_lookup_name(type
);
3033 if (!new_ops
|| !new_ops
->qos_construct
) {
3034 new_qos_conf
= NULL
;
3035 if (type
&& type
[0]) {
3038 } else if (qos_conf
&& qos_conf
->ops
== new_ops
3039 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
3040 new_qos_conf
= qos_conf
;
3042 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
3046 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3047 type
, netdev
->name
, rte_strerror(error
));
3050 if (new_qos_conf
!= qos_conf
) {
3051 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
3053 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
3057 ovs_mutex_unlock(&dev
->mutex
);
3062 /* egress-policer details */
3064 struct egress_policer
{
3065 struct qos_conf qos_conf
;
3066 struct rte_meter_srtcm_params app_srtcm_params
;
3067 struct rte_meter_srtcm egress_meter
;
3071 egress_policer_details_to_param(const struct smap
*details
,
3072 struct rte_meter_srtcm_params
*params
)
3074 memset(params
, 0, sizeof *params
);
3075 params
->cir
= smap_get_ullong(details
, "cir", 0);
3076 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
3081 egress_policer_qos_construct(const struct smap
*details
,
3082 struct qos_conf
**conf
)
3084 struct egress_policer
*policer
;
3087 policer
= xmalloc(sizeof *policer
);
3088 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
3089 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
3090 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
3091 &policer
->app_srtcm_params
);
3093 *conf
= &policer
->qos_conf
;
3104 egress_policer_qos_destruct(struct qos_conf
*conf
)
3106 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
3112 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
3114 struct egress_policer
*policer
=
3115 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3117 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
3118 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
3124 egress_policer_qos_is_equal(const struct qos_conf
*conf
,
3125 const struct smap
*details
)
3127 struct egress_policer
*policer
=
3128 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3129 struct rte_meter_srtcm_params params
;
3131 egress_policer_details_to_param(details
, ¶ms
);
3133 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
3137 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
)
3140 struct egress_policer
*policer
=
3141 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
3143 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
, pkt_cnt
);
3148 static const struct dpdk_qos_ops egress_policer_ops
= {
3149 "egress-policer", /* qos_name */
3150 egress_policer_qos_construct
,
3151 egress_policer_qos_destruct
,
3152 egress_policer_qos_get
,
3153 egress_policer_qos_is_equal
,
3158 netdev_dpdk_reconfigure(struct netdev
*netdev
)
3160 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3163 ovs_mutex_lock(&dev
->mutex
);
3165 if (netdev
->n_txq
== dev
->requested_n_txq
3166 && netdev
->n_rxq
== dev
->requested_n_rxq
3167 && dev
->mtu
== dev
->requested_mtu
3168 && dev
->rxq_size
== dev
->requested_rxq_size
3169 && dev
->txq_size
== dev
->requested_txq_size
3170 && dev
->socket_id
== dev
->requested_socket_id
) {
3171 /* Reconfiguration is unnecessary */
3176 rte_eth_dev_stop(dev
->port_id
);
3178 if (dev
->mtu
!= dev
->requested_mtu
3179 || dev
->socket_id
!= dev
->requested_socket_id
) {
3180 err
= netdev_dpdk_mempool_configure(dev
);
3186 netdev
->n_txq
= dev
->requested_n_txq
;
3187 netdev
->n_rxq
= dev
->requested_n_rxq
;
3189 dev
->rxq_size
= dev
->requested_rxq_size
;
3190 dev
->txq_size
= dev
->requested_txq_size
;
3192 rte_free(dev
->tx_q
);
3193 err
= dpdk_eth_dev_init(dev
);
3194 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
3199 netdev_change_seq_changed(netdev
);
3202 ovs_mutex_unlock(&dev
->mutex
);
3207 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
3208 OVS_REQUIRES(dev
->mutex
)
3210 dev
->up
.n_txq
= dev
->requested_n_txq
;
3211 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
3214 /* Enable TX queue 0 by default if it wasn't disabled. */
3215 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
3216 dev
->tx_q
[0].map
= 0;
3219 netdev_dpdk_remap_txqs(dev
);
3221 if (dev
->requested_socket_id
!= dev
->socket_id
3222 || dev
->requested_mtu
!= dev
->mtu
) {
3223 err
= netdev_dpdk_mempool_configure(dev
);
3227 netdev_change_seq_changed(&dev
->up
);
3231 if (netdev_dpdk_get_vid(dev
) >= 0) {
3232 dev
->vhost_reconfigured
= true;
3239 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
3241 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3244 ovs_mutex_lock(&dev
->mutex
);
3245 err
= dpdk_vhost_reconfigure_helper(dev
);
3246 ovs_mutex_unlock(&dev
->mutex
);
3252 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
3254 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3257 ovs_mutex_lock(&dev
->mutex
);
3259 /* Configure vHost client mode if requested and if the following criteria
3261 * 1. Device hasn't been registered yet.
3262 * 2. A path has been specified.
3264 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3265 && strlen(dev
->vhost_id
)) {
3266 /* Register client-mode device */
3267 err
= rte_vhost_driver_register(dev
->vhost_id
,
3268 RTE_VHOST_USER_CLIENT
);
3270 VLOG_ERR("vhost-user device setup failure for device %s\n",
3274 /* Configuration successful */
3275 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3276 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3277 "using client socket '%s'",
3278 dev
->up
.name
, dev
->vhost_id
);
3281 err
= rte_vhost_driver_callback_register(dev
->vhost_id
,
3282 &virtio_net_device_ops
);
3284 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3285 "vhost user client port: %s\n", dev
->up
.name
);
3289 err
= rte_vhost_driver_disable_features(dev
->vhost_id
,
3290 1ULL << VIRTIO_NET_F_HOST_TSO4
3291 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3292 | 1ULL << VIRTIO_NET_F_CSUM
);
3294 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3295 "client port: %s\n", dev
->up
.name
);
3299 err
= rte_vhost_driver_start(dev
->vhost_id
);
3301 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3302 "client port: %s\n", dev
->up
.name
);
3307 err
= dpdk_vhost_reconfigure_helper(dev
);
3310 ovs_mutex_unlock(&dev
->mutex
);
3315 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
3316 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3317 GET_CARRIER, GET_STATS, \
3318 GET_FEATURES, GET_STATUS, \
3319 RECONFIGURE, RXQ_RECV) \
3322 true, /* is_pmd */ \
3324 NULL, /* netdev_dpdk_run */ \
3325 NULL, /* netdev_dpdk_wait */ \
3327 netdev_dpdk_alloc, \
3330 netdev_dpdk_dealloc, \
3331 netdev_dpdk_get_config, \
3333 NULL, /* get_tunnel_config */ \
3334 NULL, /* build header */ \
3335 NULL, /* push header */ \
3336 NULL, /* pop header */ \
3337 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3341 NULL, /* send_wait */ \
3343 netdev_dpdk_set_etheraddr, \
3344 netdev_dpdk_get_etheraddr, \
3345 netdev_dpdk_get_mtu, \
3346 netdev_dpdk_set_mtu, \
3347 netdev_dpdk_get_ifindex, \
3349 netdev_dpdk_get_carrier_resets, \
3350 netdev_dpdk_set_miimon, \
3353 NULL, /* set_advertisements */ \
3354 NULL, /* get_pt_mode */ \
3356 netdev_dpdk_set_policing, \
3357 netdev_dpdk_get_qos_types, \
3358 NULL, /* get_qos_capabilities */ \
3359 netdev_dpdk_get_qos, \
3360 netdev_dpdk_set_qos, \
3361 NULL, /* get_queue */ \
3362 NULL, /* set_queue */ \
3363 NULL, /* delete_queue */ \
3364 NULL, /* get_queue_stats */ \
3365 NULL, /* queue_dump_start */ \
3366 NULL, /* queue_dump_next */ \
3367 NULL, /* queue_dump_done */ \
3368 NULL, /* dump_queue_stats */ \
3370 NULL, /* set_in4 */ \
3371 NULL, /* get_addr_list */ \
3372 NULL, /* add_router */ \
3373 NULL, /* get_next_hop */ \
3375 NULL, /* arp_lookup */ \
3377 netdev_dpdk_update_flags, \
3380 netdev_dpdk_rxq_alloc, \
3381 netdev_dpdk_rxq_construct, \
3382 netdev_dpdk_rxq_destruct, \
3383 netdev_dpdk_rxq_dealloc, \
3385 NULL, /* rx_wait */ \
3386 NULL, /* rxq_drain */ \
3390 static const struct netdev_class dpdk_class
=
3393 netdev_dpdk_class_init
,
3394 netdev_dpdk_construct
,
3395 netdev_dpdk_destruct
,
3396 netdev_dpdk_set_config
,
3397 netdev_dpdk_set_tx_multiq
,
3398 netdev_dpdk_eth_send
,
3399 netdev_dpdk_get_carrier
,
3400 netdev_dpdk_get_stats
,
3401 netdev_dpdk_get_features
,
3402 netdev_dpdk_get_status
,
3403 netdev_dpdk_reconfigure
,
3404 netdev_dpdk_rxq_recv
);
3406 static const struct netdev_class dpdk_ring_class
=
3409 netdev_dpdk_class_init
,
3410 netdev_dpdk_ring_construct
,
3411 netdev_dpdk_destruct
,
3412 netdev_dpdk_ring_set_config
,
3413 netdev_dpdk_set_tx_multiq
,
3414 netdev_dpdk_ring_send
,
3415 netdev_dpdk_get_carrier
,
3416 netdev_dpdk_get_stats
,
3417 netdev_dpdk_get_features
,
3418 netdev_dpdk_get_status
,
3419 netdev_dpdk_reconfigure
,
3420 netdev_dpdk_rxq_recv
);
3422 static const struct netdev_class dpdk_vhost_class
=
3426 netdev_dpdk_vhost_construct
,
3427 netdev_dpdk_vhost_destruct
,
3430 netdev_dpdk_vhost_send
,
3431 netdev_dpdk_vhost_get_carrier
,
3432 netdev_dpdk_vhost_get_stats
,
3435 netdev_dpdk_vhost_reconfigure
,
3436 netdev_dpdk_vhost_rxq_recv
);
3437 static const struct netdev_class dpdk_vhost_client_class
=
3439 "dpdkvhostuserclient",
3441 netdev_dpdk_vhost_client_construct
,
3442 netdev_dpdk_vhost_destruct
,
3443 netdev_dpdk_vhost_client_set_config
,
3445 netdev_dpdk_vhost_send
,
3446 netdev_dpdk_vhost_get_carrier
,
3447 netdev_dpdk_vhost_get_stats
,
3450 netdev_dpdk_vhost_client_reconfigure
,
3451 netdev_dpdk_vhost_rxq_recv
);
3454 netdev_dpdk_register(void)
3456 netdev_register_provider(&dpdk_class
);
3457 netdev_register_provider(&dpdk_ring_class
);
3458 netdev_register_provider(&dpdk_vhost_class
);
3459 netdev_register_provider(&dpdk_vhost_client_class
);