2 * Copyright (c) 2014, 2015, 2016 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.
30 #include <sys/types.h>
35 #include "dp-packet.h"
36 #include "dpif-netdev.h"
37 #include "fatal-signal.h"
38 #include "netdev-dpdk.h"
39 #include "netdev-provider.h"
40 #include "netdev-vport.h"
42 #include "openvswitch/dynamic-string.h"
43 #include "openvswitch/list.h"
44 #include "openvswitch/ofp-print.h"
45 #include "openvswitch/vlog.h"
47 #include "ovs-thread.h"
50 #include "openvswitch/shash.h"
53 #include "unaligned.h"
57 #include "rte_config.h"
59 #include "rte_meter.h"
61 #include "rte_pdump.h"
63 #include "rte_virtio_net.h"
65 VLOG_DEFINE_THIS_MODULE(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) (MTU_TO_MAX_FRAME_LEN(mtu) \
86 + sizeof(struct dp_packet) \
87 + RTE_PKTMBUF_HEADROOM)
88 #define NETDEV_DPDK_MBUF_ALIGN 1024
89 #define NETDEV_DPDK_MAX_PKT_LEN 9728
91 /* Max and min number of packets in the mempool. OVS tries to allocate a
92 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
93 * enough hugepages) we keep halving the number until the allocation succeeds
94 * or we reach MIN_NB_MBUF */
96 #define MAX_NB_MBUF (4096 * 64)
97 #define MIN_NB_MBUF (4096 * 4)
98 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
100 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
101 BUILD_ASSERT_DECL(MAX_NB_MBUF
% ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
) == 0);
103 /* The smallest possible NB_MBUF that we're going to try should be a multiple
104 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
105 BUILD_ASSERT_DECL((MAX_NB_MBUF
/ ROUND_DOWN_POW2(MAX_NB_MBUF
/MIN_NB_MBUF
))
109 * DPDK XSTATS Counter names definition
111 #define XSTAT_RX_64_PACKETS "rx_size_64_packets"
112 #define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
113 #define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
114 #define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
115 #define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
116 #define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
117 #define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
119 #define XSTAT_TX_64_PACKETS "tx_size_64_packets"
120 #define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
121 #define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
122 #define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
123 #define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
124 #define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
125 #define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
127 #define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
128 #define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
129 #define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
130 #define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
131 #define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
132 #define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
133 #define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
137 /* Default size of Physical NIC RXQ */
138 #define NIC_PORT_DEFAULT_RXQ_SIZE 2048
139 /* Default size of Physical NIC TXQ */
140 #define NIC_PORT_DEFAULT_TXQ_SIZE 2048
141 /* Maximum size of Physical NIC Queues */
142 #define NIC_PORT_MAX_Q_SIZE 4096
144 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
145 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
146 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
147 * yet mapped to another queue. */
149 static char *vhost_sock_dir
= NULL
; /* Location of vhost-user sockets */
151 #define VHOST_ENQ_RETRY_NUM 8
152 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
154 static const struct rte_eth_conf port_conf
= {
156 .mq_mode
= ETH_MQ_RX_RSS
,
158 .header_split
= 0, /* Header Split disabled */
159 .hw_ip_checksum
= 0, /* IP checksum offload disabled */
160 .hw_vlan_filter
= 0, /* VLAN filtering disabled */
161 .jumbo_frame
= 0, /* Jumbo Frame Support disabled */
167 .rss_hf
= ETH_RSS_IP
| ETH_RSS_UDP
| ETH_RSS_TCP
,
171 .mq_mode
= ETH_MQ_TX_NONE
,
175 enum { DPDK_RING_SIZE
= 256 };
176 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE
));
177 enum { DRAIN_TSC
= 200000ULL };
184 /* Quality of Service */
186 /* An instance of a QoS configuration. Always associated with a particular
189 * Each QoS implementation subclasses this with whatever additional data it
193 const struct dpdk_qos_ops
*ops
;
197 /* A particular implementation of dpdk QoS operations.
199 * The functions below return 0 if successful or a positive errno value on
200 * failure, except where otherwise noted. All of them must be provided, except
201 * where otherwise noted.
203 struct dpdk_qos_ops
{
205 /* Name of the QoS type */
206 const char *qos_name
;
208 /* Called to construct a qos_conf object. The implementation should make
209 * the appropriate calls to configure QoS according to 'details'.
211 * The contents of 'details' should be documented as valid for 'ovs_name'
212 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
213 * (which is built as ovs-vswitchd.conf.db(8)).
215 * This function must return 0 if and only if it sets '*conf' to an
216 * initialized 'struct qos_conf'.
218 * For all QoS implementations it should always be non-null.
220 int (*qos_construct
)(const struct smap
*details
, struct qos_conf
**conf
);
222 /* Destroys the data structures allocated by the implementation as part of
225 * For all QoS implementations it should always be non-null.
227 void (*qos_destruct
)(struct qos_conf
*conf
);
229 /* Retrieves details of 'conf' configuration into '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 int (*qos_get
)(const struct qos_conf
*conf
, struct smap
*details
);
237 /* Returns true if 'conf' is already configured according to 'details'.
239 * The contents of 'details' should be documented as valid for 'ovs_name'
240 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
241 * (which is built as ovs-vswitchd.conf.db(8)).
243 * For all QoS implementations it should always be non-null.
245 bool (*qos_is_equal
)(const struct qos_conf
*conf
,
246 const struct smap
*details
);
248 /* Modify an array of rte_mbufs. The modification is specific to
249 * each qos implementation.
251 * The function should take and array of mbufs and an int representing
252 * the current number of mbufs present in the array.
254 * After the function has performed a qos modification to the array of
255 * mbufs it returns an int representing the number of mbufs now present in
256 * the array. This value is can then be passed to the port send function
257 * along with the modified array for transmission.
259 * For all QoS implementations it should always be non-null.
261 int (*qos_run
)(struct qos_conf
*qos_conf
, struct rte_mbuf
**pkts
,
265 /* dpdk_qos_ops for each type of user space QoS implementation */
266 static const struct dpdk_qos_ops egress_policer_ops
;
269 * Array of dpdk_qos_ops, contains pointer to all supported QoS
272 static const struct dpdk_qos_ops
*const qos_confs
[] = {
277 static struct ovs_mutex dpdk_mutex
= OVS_MUTEX_INITIALIZER
;
279 /* Contains all 'struct dpdk_dev's. */
280 static struct ovs_list dpdk_list
OVS_GUARDED_BY(dpdk_mutex
)
281 = OVS_LIST_INITIALIZER(&dpdk_list
);
283 static struct ovs_mutex dpdk_mp_mutex
OVS_ACQ_AFTER(dpdk_mutex
)
284 = OVS_MUTEX_INITIALIZER
;
286 static struct ovs_list dpdk_mp_list
OVS_GUARDED_BY(dpdk_mp_mutex
)
287 = OVS_LIST_INITIALIZER(&dpdk_mp_list
);
290 struct rte_mempool
*mp
;
294 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mp_mutex
);
297 /* There should be one 'struct dpdk_tx_queue' created for
299 struct dpdk_tx_queue
{
300 rte_spinlock_t tx_lock
; /* Protects the members and the NIC queue
301 * from concurrent access. It is used only
302 * if the queue is shared among different
303 * pmd threads (see 'concurrent_txq'). */
304 int map
; /* Mapping of configured vhost-user queues
305 * to enabled by guest. */
308 /* dpdk has no way to remove dpdk ring ethernet devices
309 so we have to keep them around once they've been created
312 static struct ovs_list dpdk_ring_list
OVS_GUARDED_BY(dpdk_mutex
)
313 = OVS_LIST_INITIALIZER(&dpdk_ring_list
);
316 /* For the client rings */
317 struct rte_ring
*cring_tx
;
318 struct rte_ring
*cring_rx
;
319 unsigned int user_port_id
; /* User given port no, parsed from port name */
320 int eth_port_id
; /* ethernet device port id */
321 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
324 struct ingress_policer
{
325 struct rte_meter_srtcm_params app_srtcm_params
;
326 struct rte_meter_srtcm in_policer
;
327 rte_spinlock_t policer_lock
;
334 enum dpdk_dev_type type
;
336 struct dpdk_tx_queue
*tx_q
;
338 struct ovs_mutex mutex
OVS_ACQ_AFTER(dpdk_mutex
);
340 struct dpdk_mp
*dpdk_mp
;
344 struct netdev_stats stats
;
346 rte_spinlock_t stats_lock
;
348 struct eth_addr hwaddr
;
349 enum netdev_flags flags
;
351 struct rte_eth_link link
;
354 /* virtio identifier for vhost devices */
357 /* True if vHost device is 'up' and has been reconfigured at least once */
358 bool vhost_reconfigured
;
360 /* Identifier used to distinguish vhost devices from each other. */
361 char vhost_id
[PATH_MAX
];
364 struct ovs_list list_node
OVS_GUARDED_BY(dpdk_mutex
);
366 /* QoS configuration and lock for the device */
367 OVSRCU_TYPE(struct qos_conf
*) qos_conf
;
369 /* The following properties cannot be changed when a device is running,
370 * so we remember the request and update them next time
371 * netdev_dpdk*_reconfigure() is called */
375 int requested_rxq_size
;
376 int requested_txq_size
;
378 /* Number of rx/tx descriptors for physical devices */
382 /* Socket ID detected when vHost device is brought up */
383 int requested_socket_id
;
385 /* Denotes whether vHost port is client/server mode */
386 uint64_t vhost_driver_flags
;
388 /* Ingress Policer */
389 OVSRCU_TYPE(struct ingress_policer
*) ingress_policer
;
390 uint32_t policer_rate
;
391 uint32_t policer_burst
;
393 /* DPDK-ETH Flow control */
394 struct rte_eth_fc_conf fc_conf
;
397 struct netdev_rxq_dpdk
{
398 struct netdev_rxq up
;
402 static int netdev_dpdk_construct(struct netdev
*);
404 int netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
);
406 struct ingress_policer
*
407 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
);
410 is_dpdk_class(const struct netdev_class
*class)
412 return class->construct
== netdev_dpdk_construct
;
415 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
416 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
417 * value, insufficient buffers are allocated to accomodate the packet in its
418 * entirety. Furthermore, certain drivers need to ensure that there is also
419 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
420 * frames). If the RX buffer is too small, then the driver enables scatter RX
421 * behaviour, which reduces performance. To prevent this, use a buffer size
422 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
425 dpdk_buf_size(int mtu
)
427 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu
) + RTE_PKTMBUF_HEADROOM
),
428 NETDEV_DPDK_MBUF_ALIGN
);
431 /* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
433 * Unlike xmalloc(), this function can return NULL on failure. */
435 dpdk_rte_mzalloc(size_t sz
)
437 return rte_zmalloc(OVS_VPORT_DPDK
, sz
, OVS_CACHE_LINE_SIZE
);
441 free_dpdk_buf(struct dp_packet
*p
)
443 struct rte_mbuf
*pkt
= (struct rte_mbuf
*) p
;
445 rte_pktmbuf_free(pkt
);
449 ovs_rte_pktmbuf_init(struct rte_mempool
*mp
,
450 void *opaque_arg OVS_UNUSED
,
452 unsigned i OVS_UNUSED
)
454 struct rte_mbuf
*pkt
= _p
;
456 rte_pktmbuf_init(mp
, opaque_arg
, _p
, i
);
458 dp_packet_init_dpdk((struct dp_packet
*) pkt
, pkt
->buf_len
);
461 static struct dpdk_mp
*
462 dpdk_mp_create(int socket_id
, int mtu
)
464 struct rte_pktmbuf_pool_private mbp_priv
;
469 dmp
= dpdk_rte_mzalloc(sizeof *dmp
);
473 dmp
->socket_id
= socket_id
;
476 mbp_priv
.mbuf_data_room_size
= MBUF_SIZE(mtu
) - sizeof(struct dp_packet
);
477 mbp_priv
.mbuf_priv_size
= sizeof(struct dp_packet
)
478 - sizeof(struct rte_mbuf
);
479 /* XXX: this is a really rough method of provisioning memory.
480 * It's impossible to determine what the exact memory requirements are
481 * when the number of ports and rxqs that utilize a particular mempool can
482 * change dynamically at runtime. For now, use this rough heurisitic.
484 if (mtu
>= ETHER_MTU
) {
485 mp_size
= MAX_NB_MBUF
;
487 mp_size
= MIN_NB_MBUF
;
491 mp_name
= xasprintf("ovs_mp_%d_%d_%u", dmp
->mtu
, dmp
->socket_id
,
494 dmp
->mp
= rte_mempool_create(mp_name
, mp_size
, MBUF_SIZE(mtu
),
496 sizeof(struct rte_pktmbuf_pool_private
),
497 rte_pktmbuf_pool_init
, &mbp_priv
,
498 ovs_rte_pktmbuf_init
, NULL
,
501 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
508 } while (rte_errno
== ENOMEM
&& (mp_size
/= 2) >= MIN_NB_MBUF
);
514 static struct dpdk_mp
*
515 dpdk_mp_get(int socket_id
, int mtu
)
519 ovs_mutex_lock(&dpdk_mp_mutex
);
520 LIST_FOR_EACH (dmp
, list_node
, &dpdk_mp_list
) {
521 if (dmp
->socket_id
== socket_id
&& dmp
->mtu
== mtu
) {
527 dmp
= dpdk_mp_create(socket_id
, mtu
);
528 ovs_list_push_back(&dpdk_mp_list
, &dmp
->list_node
);
531 ovs_mutex_unlock(&dpdk_mp_mutex
);
537 dpdk_mp_put(struct dpdk_mp
*dmp
)
543 ovs_mutex_lock(&dpdk_mp_mutex
);
544 ovs_assert(dmp
->refcount
);
546 if (!--dmp
->refcount
) {
547 ovs_list_remove(&dmp
->list_node
);
548 rte_mempool_free(dmp
->mp
);
551 ovs_mutex_unlock(&dpdk_mp_mutex
);
554 /* Tries to allocate new mempool on requested_socket_id with
555 * mbuf size corresponding to requested_mtu.
556 * On success new configuration will be applied.
557 * On error, device will be left unchanged. */
559 netdev_dpdk_mempool_configure(struct netdev_dpdk
*dev
)
560 OVS_REQUIRES(dev
->mutex
)
562 uint32_t buf_size
= dpdk_buf_size(dev
->requested_mtu
);
565 mp
= dpdk_mp_get(dev
->requested_socket_id
, FRAME_LEN_TO_MTU(buf_size
));
567 VLOG_ERR("Insufficient memory to create memory pool for netdev "
568 "%s, with MTU %d on socket %d\n",
569 dev
->up
.name
, dev
->requested_mtu
, dev
->requested_socket_id
);
572 dpdk_mp_put(dev
->dpdk_mp
);
574 dev
->mtu
= dev
->requested_mtu
;
575 dev
->socket_id
= dev
->requested_socket_id
;
576 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
583 check_link_status(struct netdev_dpdk
*dev
)
585 struct rte_eth_link link
;
587 rte_eth_link_get_nowait(dev
->port_id
, &link
);
589 if (dev
->link
.link_status
!= link
.link_status
) {
590 netdev_change_seq_changed(&dev
->up
);
592 dev
->link_reset_cnt
++;
594 if (dev
->link
.link_status
) {
595 VLOG_DBG_RL(&rl
, "Port %d Link Up - speed %u Mbps - %s",
596 dev
->port_id
, (unsigned) dev
->link
.link_speed
,
597 (dev
->link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
598 ("full-duplex") : ("half-duplex"));
600 VLOG_DBG_RL(&rl
, "Port %d Link Down", dev
->port_id
);
606 dpdk_watchdog(void *dummy OVS_UNUSED
)
608 struct netdev_dpdk
*dev
;
610 pthread_detach(pthread_self());
613 ovs_mutex_lock(&dpdk_mutex
);
614 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
615 ovs_mutex_lock(&dev
->mutex
);
616 if (dev
->type
== DPDK_DEV_ETH
) {
617 check_link_status(dev
);
619 ovs_mutex_unlock(&dev
->mutex
);
621 ovs_mutex_unlock(&dpdk_mutex
);
622 xsleep(DPDK_PORT_WATCHDOG_INTERVAL
);
629 dpdk_eth_dev_queue_setup(struct netdev_dpdk
*dev
, int n_rxq
, int n_txq
)
633 struct rte_eth_conf conf
= port_conf
;
635 if (dev
->mtu
> ETHER_MTU
) {
636 conf
.rxmode
.jumbo_frame
= 1;
637 conf
.rxmode
.max_rx_pkt_len
= dev
->max_packet_len
;
639 conf
.rxmode
.jumbo_frame
= 0;
640 conf
.rxmode
.max_rx_pkt_len
= 0;
642 /* A device may report more queues than it makes available (this has
643 * been observed for Intel xl710, which reserves some of them for
644 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
645 * available. When this happens we can retry the configuration
646 * and request less queues */
647 while (n_rxq
&& n_txq
) {
649 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq
, n_txq
);
652 diag
= rte_eth_dev_configure(dev
->port_id
, n_rxq
, n_txq
, &conf
);
654 VLOG_WARN("Interface %s eth_dev setup error %s\n",
655 dev
->up
.name
, rte_strerror(-diag
));
659 for (i
= 0; i
< n_txq
; i
++) {
660 diag
= rte_eth_tx_queue_setup(dev
->port_id
, i
, dev
->txq_size
,
661 dev
->socket_id
, NULL
);
663 VLOG_INFO("Interface %s txq(%d) setup error: %s",
664 dev
->up
.name
, i
, rte_strerror(-diag
));
670 /* Retry with less tx queues */
675 for (i
= 0; i
< n_rxq
; i
++) {
676 diag
= rte_eth_rx_queue_setup(dev
->port_id
, i
, dev
->rxq_size
,
677 dev
->socket_id
, NULL
,
680 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
681 dev
->up
.name
, i
, rte_strerror(-diag
));
687 /* Retry with less rx queues */
692 dev
->up
.n_rxq
= n_rxq
;
693 dev
->up
.n_txq
= n_txq
;
702 dpdk_eth_flow_ctrl_setup(struct netdev_dpdk
*dev
) OVS_REQUIRES(dev
->mutex
)
704 if (rte_eth_dev_flow_ctrl_set(dev
->port_id
, &dev
->fc_conf
)) {
705 VLOG_WARN("Failed to enable flow control on device %d", dev
->port_id
);
710 dpdk_eth_dev_init(struct netdev_dpdk
*dev
)
711 OVS_REQUIRES(dev
->mutex
)
713 struct rte_pktmbuf_pool_private
*mbp_priv
;
714 struct rte_eth_dev_info info
;
715 struct ether_addr eth_addr
;
719 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
723 rte_eth_dev_info_get(dev
->port_id
, &info
);
725 n_rxq
= MIN(info
.max_rx_queues
, dev
->up
.n_rxq
);
726 n_txq
= MIN(info
.max_tx_queues
, dev
->up
.n_txq
);
728 diag
= dpdk_eth_dev_queue_setup(dev
, n_rxq
, n_txq
);
730 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
731 dev
->up
.name
, n_rxq
, n_txq
, rte_strerror(-diag
));
735 diag
= rte_eth_dev_start(dev
->port_id
);
737 VLOG_ERR("Interface %s start error: %s", dev
->up
.name
,
738 rte_strerror(-diag
));
742 rte_eth_promiscuous_enable(dev
->port_id
);
743 rte_eth_allmulticast_enable(dev
->port_id
);
745 memset(ð_addr
, 0x0, sizeof(eth_addr
));
746 rte_eth_macaddr_get(dev
->port_id
, ð_addr
);
747 VLOG_INFO_RL(&rl
, "Port %d: "ETH_ADDR_FMT
"",
748 dev
->port_id
, ETH_ADDR_BYTES_ARGS(eth_addr
.addr_bytes
));
750 memcpy(dev
->hwaddr
.ea
, eth_addr
.addr_bytes
, ETH_ADDR_LEN
);
751 rte_eth_link_get_nowait(dev
->port_id
, &dev
->link
);
753 mbp_priv
= rte_mempool_get_priv(dev
->dpdk_mp
->mp
);
754 dev
->buf_size
= mbp_priv
->mbuf_data_room_size
- RTE_PKTMBUF_HEADROOM
;
756 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
758 /* Get the Flow control configuration for DPDK-ETH */
759 diag
= rte_eth_dev_flow_ctrl_get(dev
->port_id
, &dev
->fc_conf
);
761 VLOG_DBG("cannot get flow control parameters on port=%d, err=%d",
768 static struct netdev_dpdk
*
769 netdev_dpdk_cast(const struct netdev
*netdev
)
771 return CONTAINER_OF(netdev
, struct netdev_dpdk
, up
);
774 static struct netdev
*
775 netdev_dpdk_alloc(void)
777 struct netdev_dpdk
*dev
;
779 dev
= dpdk_rte_mzalloc(sizeof *dev
);
787 static struct dpdk_tx_queue
*
788 netdev_dpdk_alloc_txq(unsigned int n_txqs
)
790 struct dpdk_tx_queue
*txqs
;
793 txqs
= dpdk_rte_mzalloc(n_txqs
* sizeof *txqs
);
795 for (i
= 0; i
< n_txqs
; i
++) {
796 /* Initialize map for vhost devices. */
797 txqs
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
798 rte_spinlock_init(&txqs
[i
].tx_lock
);
806 netdev_dpdk_init(struct netdev
*netdev
, unsigned int port_no
,
807 enum dpdk_dev_type type
)
808 OVS_REQUIRES(dpdk_mutex
)
810 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
814 ovs_mutex_init(&dev
->mutex
);
815 ovs_mutex_lock(&dev
->mutex
);
817 rte_spinlock_init(&dev
->stats_lock
);
819 /* If the 'sid' is negative, it means that the kernel fails
820 * to obtain the pci numa info. In that situation, always
822 if (type
== DPDK_DEV_ETH
) {
823 sid
= rte_eth_dev_socket_id(port_no
);
825 sid
= rte_lcore_to_socket_id(rte_get_master_lcore());
828 dev
->socket_id
= sid
< 0 ? SOCKET0
: sid
;
829 dev
->requested_socket_id
= dev
->socket_id
;
830 dev
->port_id
= port_no
;
833 dev
->requested_mtu
= dev
->mtu
= ETHER_MTU
;
834 dev
->max_packet_len
= MTU_TO_FRAME_LEN(dev
->mtu
);
835 ovsrcu_index_init(&dev
->vid
, -1);
836 dev
->vhost_reconfigured
= false;
838 err
= netdev_dpdk_mempool_configure(dev
);
843 ovsrcu_init(&dev
->qos_conf
, NULL
);
845 ovsrcu_init(&dev
->ingress_policer
, NULL
);
846 dev
->policer_rate
= 0;
847 dev
->policer_burst
= 0;
849 netdev
->n_rxq
= NR_QUEUE
;
850 netdev
->n_txq
= NR_QUEUE
;
851 dev
->requested_n_rxq
= netdev
->n_rxq
;
852 dev
->requested_n_txq
= netdev
->n_txq
;
853 dev
->rxq_size
= NIC_PORT_DEFAULT_RXQ_SIZE
;
854 dev
->txq_size
= NIC_PORT_DEFAULT_TXQ_SIZE
;
855 dev
->requested_rxq_size
= dev
->rxq_size
;
856 dev
->requested_txq_size
= dev
->txq_size
;
858 /* Initialize the flow control to NULL */
859 memset(&dev
->fc_conf
, 0, sizeof dev
->fc_conf
);
860 if (type
== DPDK_DEV_ETH
) {
861 err
= dpdk_eth_dev_init(dev
);
865 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
867 dev
->tx_q
= netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM
);
868 /* Enable DPDK_DEV_VHOST device and set promiscuous mode flag. */
869 dev
->flags
= NETDEV_UP
| NETDEV_PROMISC
;
877 ovs_list_push_back(&dpdk_list
, &dev
->list_node
);
880 ovs_mutex_unlock(&dev
->mutex
);
884 /* dev_name must be the prefix followed by a positive decimal number.
885 * (no leading + or - signs are allowed) */
887 dpdk_dev_parse_name(const char dev_name
[], const char prefix
[],
888 unsigned int *port_no
)
892 if (strncmp(dev_name
, prefix
, strlen(prefix
))) {
896 cport
= dev_name
+ strlen(prefix
);
898 if (str_to_uint(cport
, 10, port_no
)) {
906 netdev_dpdk_vhost_construct(struct netdev
*netdev
)
908 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
909 const char *name
= netdev
->name
;
912 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
913 * the file system. '/' or '\' would traverse directories, so they're not
914 * acceptable in 'name'. */
915 if (strchr(name
, '/') || strchr(name
, '\\')) {
916 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
917 "A valid name must not include '/' or '\\'",
922 ovs_mutex_lock(&dpdk_mutex
);
923 /* Take the name of the vhost-user port and append it to the location where
924 * the socket is to be created, then register the socket.
926 snprintf(dev
->vhost_id
, sizeof dev
->vhost_id
, "%s/%s",
927 vhost_sock_dir
, name
);
929 dev
->vhost_driver_flags
&= ~RTE_VHOST_USER_CLIENT
;
930 err
= rte_vhost_driver_register(dev
->vhost_id
, dev
->vhost_driver_flags
);
932 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
935 fatal_signal_add_file_to_unlink(dev
->vhost_id
);
936 VLOG_INFO("Socket %s created for vhost-user port %s\n",
937 dev
->vhost_id
, name
);
939 err
= netdev_dpdk_init(netdev
, -1, DPDK_DEV_VHOST
);
941 ovs_mutex_unlock(&dpdk_mutex
);
946 netdev_dpdk_vhost_client_construct(struct netdev
*netdev
)
950 ovs_mutex_lock(&dpdk_mutex
);
951 err
= netdev_dpdk_init(netdev
, -1, DPDK_DEV_VHOST
);
952 ovs_mutex_unlock(&dpdk_mutex
);
957 netdev_dpdk_construct(struct netdev
*netdev
)
959 unsigned int port_no
;
962 /* Names always start with "dpdk" */
963 err
= dpdk_dev_parse_name(netdev
->name
, "dpdk", &port_no
);
968 ovs_mutex_lock(&dpdk_mutex
);
969 err
= netdev_dpdk_init(netdev
, port_no
, DPDK_DEV_ETH
);
970 ovs_mutex_unlock(&dpdk_mutex
);
975 netdev_dpdk_destruct(struct netdev
*netdev
)
977 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
979 ovs_mutex_lock(&dpdk_mutex
);
980 ovs_mutex_lock(&dev
->mutex
);
982 rte_eth_dev_stop(dev
->port_id
);
983 free(ovsrcu_get_protected(struct ingress_policer
*,
984 &dev
->ingress_policer
));
987 ovs_list_remove(&dev
->list_node
);
988 dpdk_mp_put(dev
->dpdk_mp
);
990 ovs_mutex_unlock(&dev
->mutex
);
991 ovs_mutex_unlock(&dpdk_mutex
);
994 /* rte_vhost_driver_unregister() can call back destroy_device(), which will
995 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
996 * deadlock, none of the mutexes must be held while calling this function. */
998 dpdk_vhost_driver_unregister(struct netdev_dpdk
*dev OVS_UNUSED
,
1000 OVS_EXCLUDED(dpdk_mutex
)
1001 OVS_EXCLUDED(dev
->mutex
)
1003 return rte_vhost_driver_unregister(vhost_id
);
1007 netdev_dpdk_vhost_destruct(struct netdev
*netdev
)
1009 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1012 ovs_mutex_lock(&dpdk_mutex
);
1013 ovs_mutex_lock(&dev
->mutex
);
1015 /* Guest becomes an orphan if still attached. */
1016 if (netdev_dpdk_get_vid(dev
) >= 0
1017 && !(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1018 VLOG_ERR("Removing port '%s' while vhost device still attached.",
1020 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1021 "socket '%s' must be restarted.", dev
->vhost_id
);
1024 free(ovsrcu_get_protected(struct ingress_policer
*,
1025 &dev
->ingress_policer
));
1027 rte_free(dev
->tx_q
);
1028 ovs_list_remove(&dev
->list_node
);
1029 dpdk_mp_put(dev
->dpdk_mp
);
1031 vhost_id
= xstrdup(dev
->vhost_id
);
1033 ovs_mutex_unlock(&dev
->mutex
);
1034 ovs_mutex_unlock(&dpdk_mutex
);
1036 if (dpdk_vhost_driver_unregister(dev
, vhost_id
)) {
1037 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1038 netdev
->name
, vhost_id
);
1039 } else if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1040 /* OVS server mode - remove this socket from list for deletion */
1041 fatal_signal_remove_file_to_unlink(vhost_id
);
1047 netdev_dpdk_dealloc(struct netdev
*netdev
)
1049 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1055 netdev_dpdk_get_config(const struct netdev
*netdev
, struct smap
*args
)
1057 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1059 ovs_mutex_lock(&dev
->mutex
);
1061 smap_add_format(args
, "requested_rx_queues", "%d", dev
->requested_n_rxq
);
1062 smap_add_format(args
, "configured_rx_queues", "%d", netdev
->n_rxq
);
1063 smap_add_format(args
, "requested_tx_queues", "%d", dev
->requested_n_txq
);
1064 smap_add_format(args
, "configured_tx_queues", "%d", netdev
->n_txq
);
1065 smap_add_format(args
, "requested_rxq_descriptors", "%d",
1066 dev
->requested_rxq_size
);
1067 smap_add_format(args
, "configured_rxq_descriptors", "%d", dev
->rxq_size
);
1068 smap_add_format(args
, "requested_txq_descriptors", "%d",
1069 dev
->requested_txq_size
);
1070 smap_add_format(args
, "configured_txq_descriptors", "%d", dev
->txq_size
);
1071 smap_add_format(args
, "mtu", "%d", dev
->mtu
);
1072 ovs_mutex_unlock(&dev
->mutex
);
1078 dpdk_set_rxq_config(struct netdev_dpdk
*dev
, const struct smap
*args
)
1079 OVS_REQUIRES(dev
->mutex
)
1083 new_n_rxq
= MAX(smap_get_int(args
, "n_rxq", dev
->requested_n_rxq
), 1);
1084 if (new_n_rxq
!= dev
->requested_n_rxq
) {
1085 dev
->requested_n_rxq
= new_n_rxq
;
1086 netdev_request_reconfigure(&dev
->up
);
1091 dpdk_process_queue_size(struct netdev
*netdev
, const struct smap
*args
,
1092 const char *flag
, int default_size
, int *new_size
)
1094 int queue_size
= smap_get_int(args
, flag
, default_size
);
1096 if (queue_size
<= 0 || queue_size
> NIC_PORT_MAX_Q_SIZE
1097 || !is_pow2(queue_size
)) {
1098 queue_size
= default_size
;
1101 if (queue_size
!= *new_size
) {
1102 *new_size
= queue_size
;
1103 netdev_request_reconfigure(netdev
);
1108 netdev_dpdk_set_config(struct netdev
*netdev
, const struct smap
*args
)
1110 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1111 bool rx_fc_en
, tx_fc_en
, autoneg
;
1112 enum rte_eth_fc_mode fc_mode
;
1113 static const enum rte_eth_fc_mode fc_mode_set
[2][2] = {
1114 {RTE_FC_NONE
, RTE_FC_TX_PAUSE
},
1115 {RTE_FC_RX_PAUSE
, RTE_FC_FULL
}
1118 ovs_mutex_lock(&dev
->mutex
);
1120 dpdk_set_rxq_config(dev
, args
);
1122 dpdk_process_queue_size(netdev
, args
, "n_rxq_desc",
1123 NIC_PORT_DEFAULT_RXQ_SIZE
,
1124 &dev
->requested_rxq_size
);
1125 dpdk_process_queue_size(netdev
, args
, "n_txq_desc",
1126 NIC_PORT_DEFAULT_TXQ_SIZE
,
1127 &dev
->requested_txq_size
);
1129 rx_fc_en
= smap_get_bool(args
, "rx-flow-ctrl", false);
1130 tx_fc_en
= smap_get_bool(args
, "tx-flow-ctrl", false);
1131 autoneg
= smap_get_bool(args
, "flow-ctrl-autoneg", false);
1133 fc_mode
= fc_mode_set
[tx_fc_en
][rx_fc_en
];
1134 if (dev
->fc_conf
.mode
!= fc_mode
|| autoneg
!= dev
->fc_conf
.autoneg
) {
1135 dev
->fc_conf
.mode
= fc_mode
;
1136 dev
->fc_conf
.autoneg
= autoneg
;
1137 dpdk_eth_flow_ctrl_setup(dev
);
1140 ovs_mutex_unlock(&dev
->mutex
);
1146 netdev_dpdk_ring_set_config(struct netdev
*netdev
, const struct smap
*args
)
1148 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1150 ovs_mutex_lock(&dev
->mutex
);
1151 dpdk_set_rxq_config(dev
, args
);
1152 ovs_mutex_unlock(&dev
->mutex
);
1158 netdev_dpdk_vhost_client_set_config(struct netdev
*netdev
,
1159 const struct smap
*args
)
1161 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1164 ovs_mutex_lock(&dev
->mutex
);
1165 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)) {
1166 path
= smap_get(args
, "vhost-server-path");
1167 if (path
&& strcmp(path
, dev
->vhost_id
)) {
1168 strcpy(dev
->vhost_id
, path
);
1169 netdev_request_reconfigure(netdev
);
1172 ovs_mutex_unlock(&dev
->mutex
);
1178 netdev_dpdk_get_numa_id(const struct netdev
*netdev
)
1180 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1182 return dev
->socket_id
;
1185 /* Sets the number of tx queues for the dpdk interface. */
1187 netdev_dpdk_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
1189 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1191 ovs_mutex_lock(&dev
->mutex
);
1193 if (dev
->requested_n_txq
== n_txq
) {
1197 dev
->requested_n_txq
= n_txq
;
1198 netdev_request_reconfigure(netdev
);
1201 ovs_mutex_unlock(&dev
->mutex
);
1205 static struct netdev_rxq
*
1206 netdev_dpdk_rxq_alloc(void)
1208 struct netdev_rxq_dpdk
*rx
= dpdk_rte_mzalloc(sizeof *rx
);
1217 static struct netdev_rxq_dpdk
*
1218 netdev_rxq_dpdk_cast(const struct netdev_rxq
*rxq
)
1220 return CONTAINER_OF(rxq
, struct netdev_rxq_dpdk
, up
);
1224 netdev_dpdk_rxq_construct(struct netdev_rxq
*rxq
)
1226 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1227 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1229 ovs_mutex_lock(&dev
->mutex
);
1230 rx
->port_id
= dev
->port_id
;
1231 ovs_mutex_unlock(&dev
->mutex
);
1237 netdev_dpdk_rxq_destruct(struct netdev_rxq
*rxq OVS_UNUSED
)
1242 netdev_dpdk_rxq_dealloc(struct netdev_rxq
*rxq
)
1244 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1249 /* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1250 * 'pkts', even in case of failure.
1252 * Returns the number of packets that weren't transmitted. */
1254 netdev_dpdk_eth_tx_burst(struct netdev_dpdk
*dev
, int qid
,
1255 struct rte_mbuf
**pkts
, int cnt
)
1259 while (nb_tx
!= cnt
) {
1262 ret
= rte_eth_tx_burst(dev
->port_id
, qid
, pkts
+ nb_tx
, cnt
- nb_tx
);
1270 if (OVS_UNLIKELY(nb_tx
!= cnt
)) {
1271 /* Free buffers, which we couldn't transmit, one at a time (each
1272 * packet could come from a different mempool) */
1275 for (i
= nb_tx
; i
< cnt
; i
++) {
1276 rte_pktmbuf_free(pkts
[i
]);
1284 netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm
*meter
,
1285 struct rte_mbuf
*pkt
, uint64_t time
)
1287 uint32_t pkt_len
= rte_pktmbuf_pkt_len(pkt
) - sizeof(struct ether_hdr
);
1289 return rte_meter_srtcm_color_blind_check(meter
, time
, pkt_len
) ==
1294 netdev_dpdk_policer_run(struct rte_meter_srtcm
*meter
,
1295 struct rte_mbuf
**pkts
, int pkt_cnt
)
1299 struct rte_mbuf
*pkt
= NULL
;
1300 uint64_t current_time
= rte_rdtsc();
1302 for (i
= 0; i
< pkt_cnt
; i
++) {
1304 /* Handle current packet */
1305 if (netdev_dpdk_policer_pkt_handle(meter
, pkt
, current_time
)) {
1311 rte_pktmbuf_free(pkt
);
1319 ingress_policer_run(struct ingress_policer
*policer
, struct rte_mbuf
**pkts
,
1324 rte_spinlock_lock(&policer
->policer_lock
);
1325 cnt
= netdev_dpdk_policer_run(&policer
->in_policer
, pkts
, pkt_cnt
);
1326 rte_spinlock_unlock(&policer
->policer_lock
);
1332 is_vhost_running(struct netdev_dpdk
*dev
)
1334 return (netdev_dpdk_get_vid(dev
) >= 0 && dev
->vhost_reconfigured
);
1338 netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats
*stats
,
1339 unsigned int packet_size
)
1341 /* Hard-coded search for the size bucket. */
1342 if (packet_size
< 256) {
1343 if (packet_size
>= 128) {
1344 stats
->rx_128_to_255_packets
++;
1345 } else if (packet_size
<= 64) {
1346 stats
->rx_1_to_64_packets
++;
1348 stats
->rx_65_to_127_packets
++;
1351 if (packet_size
>= 1523) {
1352 stats
->rx_1523_to_max_packets
++;
1353 } else if (packet_size
>= 1024) {
1354 stats
->rx_1024_to_1522_packets
++;
1355 } else if (packet_size
< 512) {
1356 stats
->rx_256_to_511_packets
++;
1358 stats
->rx_512_to_1023_packets
++;
1364 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats
*stats
,
1365 struct dp_packet
**packets
, int count
,
1369 unsigned int packet_size
;
1370 struct dp_packet
*packet
;
1372 stats
->rx_packets
+= count
;
1373 stats
->rx_dropped
+= dropped
;
1374 for (i
= 0; i
< count
; i
++) {
1375 packet
= packets
[i
];
1376 packet_size
= dp_packet_size(packet
);
1378 if (OVS_UNLIKELY(packet_size
< ETH_HEADER_LEN
)) {
1379 /* This only protects the following multicast counting from
1380 * too short packets, but it does not stop the packet from
1381 * further processing. */
1383 stats
->rx_length_errors
++;
1387 netdev_dpdk_vhost_update_rx_size_counters(stats
, packet_size
);
1389 struct eth_header
*eh
= (struct eth_header
*) dp_packet_data(packet
);
1390 if (OVS_UNLIKELY(eth_addr_is_multicast(eh
->eth_dst
))) {
1394 stats
->rx_bytes
+= packet_size
;
1399 * The receive path for the vhost port is the TX path out from guest.
1402 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq
*rxq
,
1403 struct dp_packet_batch
*batch
)
1405 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1406 int qid
= rxq
->queue_id
;
1407 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1409 uint16_t dropped
= 0;
1411 if (OVS_UNLIKELY(!is_vhost_running(dev
)
1412 || !(dev
->flags
& NETDEV_UP
))) {
1416 nb_rx
= rte_vhost_dequeue_burst(netdev_dpdk_get_vid(dev
),
1417 qid
* VIRTIO_QNUM
+ VIRTIO_TXQ
,
1419 (struct rte_mbuf
**) batch
->packets
,
1427 nb_rx
= ingress_policer_run(policer
,
1428 (struct rte_mbuf
**) batch
->packets
,
1433 rte_spinlock_lock(&dev
->stats_lock
);
1434 netdev_dpdk_vhost_update_rx_counters(&dev
->stats
, batch
->packets
,
1436 rte_spinlock_unlock(&dev
->stats_lock
);
1438 batch
->count
= (int) nb_rx
;
1443 netdev_dpdk_rxq_recv(struct netdev_rxq
*rxq
, struct dp_packet_batch
*batch
)
1445 struct netdev_rxq_dpdk
*rx
= netdev_rxq_dpdk_cast(rxq
);
1446 struct netdev_dpdk
*dev
= netdev_dpdk_cast(rxq
->netdev
);
1447 struct ingress_policer
*policer
= netdev_dpdk_get_ingress_policer(dev
);
1451 nb_rx
= rte_eth_rx_burst(rx
->port_id
, rxq
->queue_id
,
1452 (struct rte_mbuf
**) batch
->packets
,
1460 nb_rx
= ingress_policer_run(policer
,
1461 (struct rte_mbuf
**) batch
->packets
,
1466 /* Update stats to reflect dropped packets */
1467 if (OVS_UNLIKELY(dropped
)) {
1468 rte_spinlock_lock(&dev
->stats_lock
);
1469 dev
->stats
.rx_dropped
+= dropped
;
1470 rte_spinlock_unlock(&dev
->stats_lock
);
1473 batch
->count
= nb_rx
;
1479 netdev_dpdk_qos_run(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1482 struct qos_conf
*qos_conf
= ovsrcu_get(struct qos_conf
*, &dev
->qos_conf
);
1485 rte_spinlock_lock(&qos_conf
->lock
);
1486 cnt
= qos_conf
->ops
->qos_run(qos_conf
, pkts
, cnt
);
1487 rte_spinlock_unlock(&qos_conf
->lock
);
1494 netdev_dpdk_filter_packet_len(struct netdev_dpdk
*dev
, struct rte_mbuf
**pkts
,
1499 struct rte_mbuf
*pkt
;
1501 for (i
= 0; i
< pkt_cnt
; i
++) {
1503 if (OVS_UNLIKELY(pkt
->pkt_len
> dev
->max_packet_len
)) {
1504 VLOG_WARN_RL(&rl
, "%s: Too big size %" PRIu32
" max_packet_len %d",
1505 dev
->up
.name
, pkt
->pkt_len
, dev
->max_packet_len
);
1506 rte_pktmbuf_free(pkt
);
1510 if (OVS_UNLIKELY(i
!= cnt
)) {
1520 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats
*stats
,
1521 struct dp_packet
**packets
,
1526 int sent
= attempted
- dropped
;
1528 stats
->tx_packets
+= sent
;
1529 stats
->tx_dropped
+= dropped
;
1531 for (i
= 0; i
< sent
; i
++) {
1532 stats
->tx_bytes
+= dp_packet_size(packets
[i
]);
1537 __netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1538 struct dp_packet
**pkts
, int cnt
)
1540 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1541 struct rte_mbuf
**cur_pkts
= (struct rte_mbuf
**) pkts
;
1542 unsigned int total_pkts
= cnt
;
1543 unsigned int dropped
= 0;
1546 qid
= dev
->tx_q
[qid
% netdev
->n_txq
].map
;
1548 if (OVS_UNLIKELY(!is_vhost_running(dev
) || qid
< 0
1549 || !(dev
->flags
& NETDEV_UP
))) {
1550 rte_spinlock_lock(&dev
->stats_lock
);
1551 dev
->stats
.tx_dropped
+= cnt
;
1552 rte_spinlock_unlock(&dev
->stats_lock
);
1556 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1558 cnt
= netdev_dpdk_filter_packet_len(dev
, cur_pkts
, cnt
);
1559 /* Check has QoS has been configured for the netdev */
1560 cnt
= netdev_dpdk_qos_run(dev
, cur_pkts
, cnt
);
1561 dropped
= total_pkts
- cnt
;
1564 int vhost_qid
= qid
* VIRTIO_QNUM
+ VIRTIO_RXQ
;
1565 unsigned int tx_pkts
;
1567 tx_pkts
= rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev
),
1568 vhost_qid
, cur_pkts
, cnt
);
1569 if (OVS_LIKELY(tx_pkts
)) {
1570 /* Packets have been sent.*/
1572 /* Prepare for possible retry.*/
1573 cur_pkts
= &cur_pkts
[tx_pkts
];
1575 /* No packets sent - do not retry.*/
1578 } while (cnt
&& (retries
++ <= VHOST_ENQ_RETRY_NUM
));
1580 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1582 rte_spinlock_lock(&dev
->stats_lock
);
1583 netdev_dpdk_vhost_update_tx_counters(&dev
->stats
, pkts
, total_pkts
,
1585 rte_spinlock_unlock(&dev
->stats_lock
);
1588 for (i
= 0; i
< total_pkts
- dropped
; i
++) {
1589 dp_packet_delete(pkts
[i
]);
1593 /* Tx function. Transmit packets indefinitely */
1595 dpdk_do_tx_copy(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
)
1596 OVS_NO_THREAD_SAFETY_ANALYSIS
1598 #if !defined(__CHECKER__) && !defined(_WIN32)
1599 const size_t PKT_ARRAY_SIZE
= batch
->count
;
1601 /* Sparse or MSVC doesn't like variable length array. */
1602 enum { PKT_ARRAY_SIZE
= NETDEV_MAX_BURST
};
1604 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1605 struct rte_mbuf
*pkts
[PKT_ARRAY_SIZE
];
1610 dp_packet_batch_apply_cutlen(batch
);
1612 for (i
= 0; i
< batch
->count
; i
++) {
1613 int size
= dp_packet_size(batch
->packets
[i
]);
1615 if (OVS_UNLIKELY(size
> dev
->max_packet_len
)) {
1616 VLOG_WARN_RL(&rl
, "Too big size %d max_packet_len %d",
1617 (int) size
, dev
->max_packet_len
);
1623 pkts
[newcnt
] = rte_pktmbuf_alloc(dev
->dpdk_mp
->mp
);
1625 if (!pkts
[newcnt
]) {
1626 dropped
+= batch
->count
- i
;
1630 /* We have to do a copy for now */
1631 memcpy(rte_pktmbuf_mtod(pkts
[newcnt
], void *),
1632 dp_packet_data(batch
->packets
[i
]), size
);
1634 rte_pktmbuf_data_len(pkts
[newcnt
]) = size
;
1635 rte_pktmbuf_pkt_len(pkts
[newcnt
]) = size
;
1640 if (dev
->type
== DPDK_DEV_VHOST
) {
1641 __netdev_dpdk_vhost_send(netdev
, qid
, (struct dp_packet
**) pkts
,
1644 unsigned int qos_pkts
= newcnt
;
1646 /* Check if QoS has been configured for this netdev. */
1647 newcnt
= netdev_dpdk_qos_run(dev
, pkts
, newcnt
);
1649 dropped
+= qos_pkts
- newcnt
;
1650 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, newcnt
);
1653 if (OVS_UNLIKELY(dropped
)) {
1654 rte_spinlock_lock(&dev
->stats_lock
);
1655 dev
->stats
.tx_dropped
+= dropped
;
1656 rte_spinlock_unlock(&dev
->stats_lock
);
1661 netdev_dpdk_vhost_send(struct netdev
*netdev
, int qid
,
1662 struct dp_packet_batch
*batch
,
1663 bool may_steal
, bool concurrent_txq OVS_UNUSED
)
1666 if (OVS_UNLIKELY(!may_steal
|| batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1667 dpdk_do_tx_copy(netdev
, qid
, batch
);
1668 dp_packet_delete_batch(batch
, may_steal
);
1670 dp_packet_batch_apply_cutlen(batch
);
1671 __netdev_dpdk_vhost_send(netdev
, qid
, batch
->packets
, batch
->count
);
1677 netdev_dpdk_send__(struct netdev_dpdk
*dev
, int qid
,
1678 struct dp_packet_batch
*batch
, bool may_steal
,
1679 bool concurrent_txq
)
1681 if (OVS_UNLIKELY(concurrent_txq
)) {
1682 qid
= qid
% dev
->up
.n_txq
;
1683 rte_spinlock_lock(&dev
->tx_q
[qid
].tx_lock
);
1686 if (OVS_UNLIKELY(!may_steal
||
1687 batch
->packets
[0]->source
!= DPBUF_DPDK
)) {
1688 struct netdev
*netdev
= &dev
->up
;
1690 dpdk_do_tx_copy(netdev
, qid
, batch
);
1691 dp_packet_delete_batch(batch
, may_steal
);
1694 int cnt
= batch
->count
;
1695 struct rte_mbuf
**pkts
= (struct rte_mbuf
**) batch
->packets
;
1697 dp_packet_batch_apply_cutlen(batch
);
1699 cnt
= netdev_dpdk_filter_packet_len(dev
, pkts
, cnt
);
1700 cnt
= netdev_dpdk_qos_run(dev
, pkts
, cnt
);
1701 dropped
= batch
->count
- cnt
;
1703 dropped
+= netdev_dpdk_eth_tx_burst(dev
, qid
, pkts
, cnt
);
1705 if (OVS_UNLIKELY(dropped
)) {
1706 rte_spinlock_lock(&dev
->stats_lock
);
1707 dev
->stats
.tx_dropped
+= dropped
;
1708 rte_spinlock_unlock(&dev
->stats_lock
);
1712 if (OVS_UNLIKELY(concurrent_txq
)) {
1713 rte_spinlock_unlock(&dev
->tx_q
[qid
].tx_lock
);
1718 netdev_dpdk_eth_send(struct netdev
*netdev
, int qid
,
1719 struct dp_packet_batch
*batch
, bool may_steal
,
1720 bool concurrent_txq
)
1722 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1724 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
1729 netdev_dpdk_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1731 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1733 ovs_mutex_lock(&dev
->mutex
);
1734 if (!eth_addr_equals(dev
->hwaddr
, mac
)) {
1736 netdev_change_seq_changed(netdev
);
1738 ovs_mutex_unlock(&dev
->mutex
);
1744 netdev_dpdk_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1746 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1748 ovs_mutex_lock(&dev
->mutex
);
1750 ovs_mutex_unlock(&dev
->mutex
);
1756 netdev_dpdk_get_mtu(const struct netdev
*netdev
, int *mtup
)
1758 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1760 ovs_mutex_lock(&dev
->mutex
);
1762 ovs_mutex_unlock(&dev
->mutex
);
1768 netdev_dpdk_set_mtu(struct netdev
*netdev
, int mtu
)
1770 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1772 if (MTU_TO_FRAME_LEN(mtu
) > NETDEV_DPDK_MAX_PKT_LEN
1773 || mtu
< ETHER_MIN_MTU
) {
1774 VLOG_WARN("%s: unsupported MTU %d\n", dev
->up
.name
, mtu
);
1778 ovs_mutex_lock(&dev
->mutex
);
1779 if (dev
->requested_mtu
!= mtu
) {
1780 dev
->requested_mtu
= mtu
;
1781 netdev_request_reconfigure(netdev
);
1783 ovs_mutex_unlock(&dev
->mutex
);
1789 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
);
1792 netdev_dpdk_vhost_get_stats(const struct netdev
*netdev
,
1793 struct netdev_stats
*stats
)
1795 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1797 ovs_mutex_lock(&dev
->mutex
);
1799 rte_spinlock_lock(&dev
->stats_lock
);
1800 /* Supported Stats */
1801 stats
->rx_packets
+= dev
->stats
.rx_packets
;
1802 stats
->tx_packets
+= dev
->stats
.tx_packets
;
1803 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
1804 stats
->tx_dropped
+= dev
->stats
.tx_dropped
;
1805 stats
->multicast
= dev
->stats
.multicast
;
1806 stats
->rx_bytes
= dev
->stats
.rx_bytes
;
1807 stats
->tx_bytes
= dev
->stats
.tx_bytes
;
1808 stats
->rx_errors
= dev
->stats
.rx_errors
;
1809 stats
->rx_length_errors
= dev
->stats
.rx_length_errors
;
1811 stats
->rx_1_to_64_packets
= dev
->stats
.rx_1_to_64_packets
;
1812 stats
->rx_65_to_127_packets
= dev
->stats
.rx_65_to_127_packets
;
1813 stats
->rx_128_to_255_packets
= dev
->stats
.rx_128_to_255_packets
;
1814 stats
->rx_256_to_511_packets
= dev
->stats
.rx_256_to_511_packets
;
1815 stats
->rx_512_to_1023_packets
= dev
->stats
.rx_512_to_1023_packets
;
1816 stats
->rx_1024_to_1522_packets
= dev
->stats
.rx_1024_to_1522_packets
;
1817 stats
->rx_1523_to_max_packets
= dev
->stats
.rx_1523_to_max_packets
;
1819 rte_spinlock_unlock(&dev
->stats_lock
);
1821 ovs_mutex_unlock(&dev
->mutex
);
1827 netdev_dpdk_convert_xstats(struct netdev_stats
*stats
,
1828 const struct rte_eth_xstat
*xstats
,
1829 const struct rte_eth_xstat_name
*names
,
1830 const unsigned int size
)
1832 for (unsigned int i
= 0; i
< size
; i
++) {
1833 if (strcmp(XSTAT_RX_64_PACKETS
, names
[i
].name
) == 0) {
1834 stats
->rx_1_to_64_packets
= xstats
[i
].value
;
1835 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
1836 stats
->rx_65_to_127_packets
= xstats
[i
].value
;
1837 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
1838 stats
->rx_128_to_255_packets
= xstats
[i
].value
;
1839 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
1840 stats
->rx_256_to_511_packets
= xstats
[i
].value
;
1841 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
1842 stats
->rx_512_to_1023_packets
= xstats
[i
].value
;
1843 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
1844 stats
->rx_1024_to_1522_packets
= xstats
[i
].value
;
1845 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
1846 stats
->rx_1523_to_max_packets
= xstats
[i
].value
;
1847 } else if (strcmp(XSTAT_TX_64_PACKETS
, names
[i
].name
) == 0) {
1848 stats
->tx_1_to_64_packets
= xstats
[i
].value
;
1849 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS
, names
[i
].name
) == 0) {
1850 stats
->tx_65_to_127_packets
= xstats
[i
].value
;
1851 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS
, names
[i
].name
) == 0) {
1852 stats
->tx_128_to_255_packets
= xstats
[i
].value
;
1853 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS
, names
[i
].name
) == 0) {
1854 stats
->tx_256_to_511_packets
= xstats
[i
].value
;
1855 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS
, names
[i
].name
) == 0) {
1856 stats
->tx_512_to_1023_packets
= xstats
[i
].value
;
1857 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS
, names
[i
].name
) == 0) {
1858 stats
->tx_1024_to_1522_packets
= xstats
[i
].value
;
1859 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS
, names
[i
].name
) == 0) {
1860 stats
->tx_1523_to_max_packets
= xstats
[i
].value
;
1861 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS
, names
[i
].name
) == 0) {
1862 stats
->tx_multicast_packets
= xstats
[i
].value
;
1863 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
1864 stats
->rx_broadcast_packets
= xstats
[i
].value
;
1865 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS
, names
[i
].name
) == 0) {
1866 stats
->tx_broadcast_packets
= xstats
[i
].value
;
1867 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS
, names
[i
].name
) == 0) {
1868 stats
->rx_undersized_errors
= xstats
[i
].value
;
1869 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS
, names
[i
].name
) == 0) {
1870 stats
->rx_fragmented_errors
= xstats
[i
].value
;
1871 } else if (strcmp(XSTAT_RX_JABBER_ERRORS
, names
[i
].name
) == 0) {
1872 stats
->rx_jabber_errors
= xstats
[i
].value
;
1878 netdev_dpdk_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1880 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1881 struct rte_eth_stats rte_stats
;
1884 netdev_dpdk_get_carrier(netdev
, &gg
);
1885 ovs_mutex_lock(&dev
->mutex
);
1887 struct rte_eth_xstat
*rte_xstats
= NULL
;
1888 struct rte_eth_xstat_name
*rte_xstats_names
= NULL
;
1889 int rte_xstats_len
, rte_xstats_new_len
, rte_xstats_ret
;
1891 if (rte_eth_stats_get(dev
->port_id
, &rte_stats
)) {
1892 VLOG_ERR("Can't get ETH statistics for port: %i.", dev
->port_id
);
1893 ovs_mutex_unlock(&dev
->mutex
);
1897 /* Get length of statistics */
1898 rte_xstats_len
= rte_eth_xstats_get_names(dev
->port_id
, NULL
, 0);
1899 if (rte_xstats_len
< 0) {
1900 VLOG_WARN("Cannot get XSTATS values for port: %i", dev
->port_id
);
1903 /* Reserve memory for xstats names and values */
1904 rte_xstats_names
= xcalloc(rte_xstats_len
, sizeof *rte_xstats_names
);
1905 rte_xstats
= xcalloc(rte_xstats_len
, sizeof *rte_xstats
);
1907 /* Retreive xstats names */
1908 rte_xstats_new_len
= rte_eth_xstats_get_names(dev
->port_id
,
1911 if (rte_xstats_new_len
!= rte_xstats_len
) {
1912 VLOG_WARN("Cannot get XSTATS names for port: %i.", dev
->port_id
);
1915 /* Retreive xstats values */
1916 memset(rte_xstats
, 0xff, sizeof *rte_xstats
* rte_xstats_len
);
1917 rte_xstats_ret
= rte_eth_xstats_get(dev
->port_id
, rte_xstats
,
1919 if (rte_xstats_ret
> 0 && rte_xstats_ret
<= rte_xstats_len
) {
1920 netdev_dpdk_convert_xstats(stats
, rte_xstats
, rte_xstats_names
,
1923 VLOG_WARN("Cannot get XSTATS values for port: %i.", dev
->port_id
);
1928 free(rte_xstats_names
);
1930 stats
->rx_packets
= rte_stats
.ipackets
;
1931 stats
->tx_packets
= rte_stats
.opackets
;
1932 stats
->rx_bytes
= rte_stats
.ibytes
;
1933 stats
->tx_bytes
= rte_stats
.obytes
;
1934 /* DPDK counts imissed as errors, but count them here as dropped instead */
1935 stats
->rx_errors
= rte_stats
.ierrors
- rte_stats
.imissed
;
1936 stats
->tx_errors
= rte_stats
.oerrors
;
1938 rte_spinlock_lock(&dev
->stats_lock
);
1939 stats
->tx_dropped
= dev
->stats
.tx_dropped
;
1940 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
1941 rte_spinlock_unlock(&dev
->stats_lock
);
1943 /* These are the available DPDK counters for packets not received due to
1944 * local resource constraints in DPDK and NIC respectively. */
1945 stats
->rx_dropped
+= rte_stats
.rx_nombuf
+ rte_stats
.imissed
;
1946 stats
->rx_missed_errors
= rte_stats
.imissed
;
1948 ovs_mutex_unlock(&dev
->mutex
);
1954 netdev_dpdk_get_features(const struct netdev
*netdev
,
1955 enum netdev_features
*current
,
1956 enum netdev_features
*advertised OVS_UNUSED
,
1957 enum netdev_features
*supported OVS_UNUSED
,
1958 enum netdev_features
*peer OVS_UNUSED
)
1960 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
1961 struct rte_eth_link link
;
1963 ovs_mutex_lock(&dev
->mutex
);
1965 ovs_mutex_unlock(&dev
->mutex
);
1967 if (link
.link_duplex
== ETH_LINK_HALF_DUPLEX
) {
1968 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
1969 *current
= NETDEV_F_10MB_HD
;
1971 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
1972 *current
= NETDEV_F_100MB_HD
;
1974 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
1975 *current
= NETDEV_F_1GB_HD
;
1977 } else if (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) {
1978 if (link
.link_speed
== ETH_SPEED_NUM_10M
) {
1979 *current
= NETDEV_F_10MB_FD
;
1981 if (link
.link_speed
== ETH_SPEED_NUM_100M
) {
1982 *current
= NETDEV_F_100MB_FD
;
1984 if (link
.link_speed
== ETH_SPEED_NUM_1G
) {
1985 *current
= NETDEV_F_1GB_FD
;
1987 if (link
.link_speed
== ETH_SPEED_NUM_10G
) {
1988 *current
= NETDEV_F_10GB_FD
;
1992 if (link
.link_autoneg
) {
1993 *current
|= NETDEV_F_AUTONEG
;
1999 static struct ingress_policer
*
2000 netdev_dpdk_policer_construct(uint32_t rate
, uint32_t burst
)
2002 struct ingress_policer
*policer
= NULL
;
2003 uint64_t rate_bytes
;
2004 uint64_t burst_bytes
;
2007 policer
= xmalloc(sizeof *policer
);
2008 rte_spinlock_init(&policer
->policer_lock
);
2010 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
2011 rate_bytes
= rate
* 1000/8;
2012 burst_bytes
= burst
* 1000/8;
2014 policer
->app_srtcm_params
.cir
= rate_bytes
;
2015 policer
->app_srtcm_params
.cbs
= burst_bytes
;
2016 policer
->app_srtcm_params
.ebs
= 0;
2017 err
= rte_meter_srtcm_config(&policer
->in_policer
,
2018 &policer
->app_srtcm_params
);
2020 VLOG_ERR("Could not create rte meter for ingress policer");
2028 netdev_dpdk_set_policing(struct netdev
* netdev
, uint32_t policer_rate
,
2029 uint32_t policer_burst
)
2031 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2032 struct ingress_policer
*policer
;
2034 /* Force to 0 if no rate specified,
2035 * default to 8000 kbits if burst is 0,
2036 * else stick with user-specified value.
2038 policer_burst
= (!policer_rate
? 0
2039 : !policer_burst
? 8000
2042 ovs_mutex_lock(&dev
->mutex
);
2044 policer
= ovsrcu_get_protected(struct ingress_policer
*,
2045 &dev
->ingress_policer
);
2047 if (dev
->policer_rate
== policer_rate
&&
2048 dev
->policer_burst
== policer_burst
) {
2049 /* Assume that settings haven't changed since we last set them. */
2050 ovs_mutex_unlock(&dev
->mutex
);
2054 /* Destroy any existing ingress policer for the device if one exists */
2056 ovsrcu_postpone(free
, policer
);
2059 if (policer_rate
!= 0) {
2060 policer
= netdev_dpdk_policer_construct(policer_rate
, policer_burst
);
2064 ovsrcu_set(&dev
->ingress_policer
, policer
);
2065 dev
->policer_rate
= policer_rate
;
2066 dev
->policer_burst
= policer_burst
;
2067 ovs_mutex_unlock(&dev
->mutex
);
2073 netdev_dpdk_get_ifindex(const struct netdev
*netdev
)
2075 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2078 ovs_mutex_lock(&dev
->mutex
);
2079 ifindex
= dev
->port_id
;
2080 ovs_mutex_unlock(&dev
->mutex
);
2086 netdev_dpdk_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2088 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2090 ovs_mutex_lock(&dev
->mutex
);
2091 check_link_status(dev
);
2092 *carrier
= dev
->link
.link_status
;
2094 ovs_mutex_unlock(&dev
->mutex
);
2100 netdev_dpdk_vhost_get_carrier(const struct netdev
*netdev
, bool *carrier
)
2102 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2104 ovs_mutex_lock(&dev
->mutex
);
2106 if (is_vhost_running(dev
)) {
2112 ovs_mutex_unlock(&dev
->mutex
);
2117 static long long int
2118 netdev_dpdk_get_carrier_resets(const struct netdev
*netdev
)
2120 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2121 long long int carrier_resets
;
2123 ovs_mutex_lock(&dev
->mutex
);
2124 carrier_resets
= dev
->link_reset_cnt
;
2125 ovs_mutex_unlock(&dev
->mutex
);
2127 return carrier_resets
;
2131 netdev_dpdk_set_miimon(struct netdev
*netdev OVS_UNUSED
,
2132 long long int interval OVS_UNUSED
)
2138 netdev_dpdk_update_flags__(struct netdev_dpdk
*dev
,
2139 enum netdev_flags off
, enum netdev_flags on
,
2140 enum netdev_flags
*old_flagsp
)
2141 OVS_REQUIRES(dev
->mutex
)
2145 if ((off
| on
) & ~(NETDEV_UP
| NETDEV_PROMISC
)) {
2149 *old_flagsp
= dev
->flags
;
2153 if (dev
->flags
== *old_flagsp
) {
2157 if (dev
->type
== DPDK_DEV_ETH
) {
2158 if (dev
->flags
& NETDEV_UP
) {
2159 err
= rte_eth_dev_start(dev
->port_id
);
2164 if (dev
->flags
& NETDEV_PROMISC
) {
2165 rte_eth_promiscuous_enable(dev
->port_id
);
2168 if (!(dev
->flags
& NETDEV_UP
)) {
2169 rte_eth_dev_stop(dev
->port_id
);
2172 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2173 * running then change netdev's change_seq to trigger link state
2176 if ((NETDEV_UP
& ((*old_flagsp
^ on
) | (*old_flagsp
^ off
)))
2177 && is_vhost_running(dev
)) {
2178 netdev_change_seq_changed(&dev
->up
);
2180 /* Clear statistics if device is getting up. */
2181 if (NETDEV_UP
& on
) {
2182 rte_spinlock_lock(&dev
->stats_lock
);
2183 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2184 rte_spinlock_unlock(&dev
->stats_lock
);
2193 netdev_dpdk_update_flags(struct netdev
*netdev
,
2194 enum netdev_flags off
, enum netdev_flags on
,
2195 enum netdev_flags
*old_flagsp
)
2197 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2200 ovs_mutex_lock(&dev
->mutex
);
2201 error
= netdev_dpdk_update_flags__(dev
, off
, on
, old_flagsp
);
2202 ovs_mutex_unlock(&dev
->mutex
);
2208 netdev_dpdk_get_status(const struct netdev
*netdev
, struct smap
*args
)
2210 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2211 struct rte_eth_dev_info dev_info
;
2213 if (!rte_eth_dev_is_valid_port(dev
->port_id
)) {
2217 ovs_mutex_lock(&dev
->mutex
);
2218 rte_eth_dev_info_get(dev
->port_id
, &dev_info
);
2219 ovs_mutex_unlock(&dev
->mutex
);
2221 smap_add_format(args
, "port_no", "%d", dev
->port_id
);
2222 smap_add_format(args
, "numa_id", "%d",
2223 rte_eth_dev_socket_id(dev
->port_id
));
2224 smap_add_format(args
, "driver_name", "%s", dev_info
.driver_name
);
2225 smap_add_format(args
, "min_rx_bufsize", "%u", dev_info
.min_rx_bufsize
);
2226 smap_add_format(args
, "max_rx_pktlen", "%u", dev
->max_packet_len
);
2227 smap_add_format(args
, "max_rx_queues", "%u", dev_info
.max_rx_queues
);
2228 smap_add_format(args
, "max_tx_queues", "%u", dev_info
.max_tx_queues
);
2229 smap_add_format(args
, "max_mac_addrs", "%u", dev_info
.max_mac_addrs
);
2230 smap_add_format(args
, "max_hash_mac_addrs", "%u",
2231 dev_info
.max_hash_mac_addrs
);
2232 smap_add_format(args
, "max_vfs", "%u", dev_info
.max_vfs
);
2233 smap_add_format(args
, "max_vmdq_pools", "%u", dev_info
.max_vmdq_pools
);
2235 if (dev_info
.pci_dev
) {
2236 smap_add_format(args
, "pci-vendor_id", "0x%u",
2237 dev_info
.pci_dev
->id
.vendor_id
);
2238 smap_add_format(args
, "pci-device_id", "0x%x",
2239 dev_info
.pci_dev
->id
.device_id
);
2246 netdev_dpdk_set_admin_state__(struct netdev_dpdk
*dev
, bool admin_state
)
2247 OVS_REQUIRES(dev
->mutex
)
2249 enum netdev_flags old_flags
;
2252 netdev_dpdk_update_flags__(dev
, 0, NETDEV_UP
, &old_flags
);
2254 netdev_dpdk_update_flags__(dev
, NETDEV_UP
, 0, &old_flags
);
2259 netdev_dpdk_set_admin_state(struct unixctl_conn
*conn
, int argc
,
2260 const char *argv
[], void *aux OVS_UNUSED
)
2264 if (!strcasecmp(argv
[argc
- 1], "up")) {
2266 } else if ( !strcasecmp(argv
[argc
- 1], "down")) {
2269 unixctl_command_reply_error(conn
, "Invalid Admin State");
2274 struct netdev
*netdev
= netdev_from_name(argv
[1]);
2275 if (netdev
&& is_dpdk_class(netdev
->netdev_class
)) {
2276 struct netdev_dpdk
*dpdk_dev
= netdev_dpdk_cast(netdev
);
2278 ovs_mutex_lock(&dpdk_dev
->mutex
);
2279 netdev_dpdk_set_admin_state__(dpdk_dev
, up
);
2280 ovs_mutex_unlock(&dpdk_dev
->mutex
);
2282 netdev_close(netdev
);
2284 unixctl_command_reply_error(conn
, "Not a DPDK Interface");
2285 netdev_close(netdev
);
2289 struct netdev_dpdk
*netdev
;
2291 ovs_mutex_lock(&dpdk_mutex
);
2292 LIST_FOR_EACH (netdev
, list_node
, &dpdk_list
) {
2293 ovs_mutex_lock(&netdev
->mutex
);
2294 netdev_dpdk_set_admin_state__(netdev
, up
);
2295 ovs_mutex_unlock(&netdev
->mutex
);
2297 ovs_mutex_unlock(&dpdk_mutex
);
2299 unixctl_command_reply(conn
, "OK");
2303 * Set virtqueue flags so that we do not receive interrupts.
2306 set_irq_status(int vid
)
2311 for (i
= 0; i
< rte_vhost_get_queue_num(vid
); i
++) {
2312 idx
= i
* VIRTIO_QNUM
;
2313 rte_vhost_enable_guest_notification(vid
, idx
+ VIRTIO_RXQ
, 0);
2314 rte_vhost_enable_guest_notification(vid
, idx
+ VIRTIO_TXQ
, 0);
2319 * Fixes mapping for vhost-user tx queues. Must be called after each
2320 * enabling/disabling of queues and n_txq modifications.
2323 netdev_dpdk_remap_txqs(struct netdev_dpdk
*dev
)
2324 OVS_REQUIRES(dev
->mutex
)
2326 int *enabled_queues
, n_enabled
= 0;
2327 int i
, k
, total_txqs
= dev
->up
.n_txq
;
2329 enabled_queues
= xcalloc(total_txqs
, sizeof *enabled_queues
);
2331 for (i
= 0; i
< total_txqs
; i
++) {
2332 /* Enabled queues always mapped to themselves. */
2333 if (dev
->tx_q
[i
].map
== i
) {
2334 enabled_queues
[n_enabled
++] = i
;
2338 if (n_enabled
== 0 && total_txqs
!= 0) {
2339 enabled_queues
[0] = OVS_VHOST_QUEUE_DISABLED
;
2344 for (i
= 0; i
< total_txqs
; i
++) {
2345 if (dev
->tx_q
[i
].map
!= i
) {
2346 dev
->tx_q
[i
].map
= enabled_queues
[k
];
2347 k
= (k
+ 1) % n_enabled
;
2351 VLOG_DBG("TX queue mapping for %s\n", dev
->vhost_id
);
2352 for (i
= 0; i
< total_txqs
; i
++) {
2353 VLOG_DBG("%2d --> %2d", i
, dev
->tx_q
[i
].map
);
2356 free(enabled_queues
);
2360 * A new virtio-net device is added to a vhost port.
2365 struct netdev_dpdk
*dev
;
2366 bool exists
= false;
2368 char ifname
[IF_NAME_SZ
];
2370 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2372 ovs_mutex_lock(&dpdk_mutex
);
2373 /* Add device to the vhost port with the same name as that passed down. */
2374 LIST_FOR_EACH(dev
, list_node
, &dpdk_list
) {
2375 ovs_mutex_lock(&dev
->mutex
);
2376 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2377 uint32_t qp_num
= rte_vhost_get_queue_num(vid
);
2379 /* Get NUMA information */
2380 newnode
= rte_vhost_get_numa_node(vid
);
2381 if (newnode
== -1) {
2383 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
2386 newnode
= dev
->socket_id
;
2389 if (dev
->requested_n_txq
!= qp_num
2390 || dev
->requested_n_rxq
!= qp_num
2391 || dev
->requested_socket_id
!= newnode
) {
2392 dev
->requested_socket_id
= newnode
;
2393 dev
->requested_n_rxq
= qp_num
;
2394 dev
->requested_n_txq
= qp_num
;
2395 netdev_request_reconfigure(&dev
->up
);
2397 /* Reconfiguration not required. */
2398 dev
->vhost_reconfigured
= true;
2401 ovsrcu_index_set(&dev
->vid
, vid
);
2404 /* Disable notifications. */
2405 set_irq_status(vid
);
2406 netdev_change_seq_changed(&dev
->up
);
2407 ovs_mutex_unlock(&dev
->mutex
);
2410 ovs_mutex_unlock(&dev
->mutex
);
2412 ovs_mutex_unlock(&dpdk_mutex
);
2415 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname
);
2420 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2426 /* Clears mapping for all available queues of vhost interface. */
2428 netdev_dpdk_txq_map_clear(struct netdev_dpdk
*dev
)
2429 OVS_REQUIRES(dev
->mutex
)
2433 for (i
= 0; i
< dev
->up
.n_txq
; i
++) {
2434 dev
->tx_q
[i
].map
= OVS_VHOST_QUEUE_MAP_UNKNOWN
;
2439 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2440 * flag to stop any more packets from being sent or received to/from a VM and
2441 * ensure all currently queued packets have been sent/received before removing
2445 destroy_device(int vid
)
2447 struct netdev_dpdk
*dev
;
2448 bool exists
= false;
2449 char ifname
[IF_NAME_SZ
];
2451 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2453 ovs_mutex_lock(&dpdk_mutex
);
2454 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2455 if (netdev_dpdk_get_vid(dev
) == vid
) {
2457 ovs_mutex_lock(&dev
->mutex
);
2458 dev
->vhost_reconfigured
= false;
2459 ovsrcu_index_set(&dev
->vid
, -1);
2460 netdev_dpdk_txq_map_clear(dev
);
2462 netdev_change_seq_changed(&dev
->up
);
2463 ovs_mutex_unlock(&dev
->mutex
);
2469 ovs_mutex_unlock(&dpdk_mutex
);
2473 * Wait for other threads to quiesce after setting the 'virtio_dev'
2474 * to NULL, before returning.
2476 ovsrcu_synchronize();
2478 * As call to ovsrcu_synchronize() will end the quiescent state,
2479 * put thread back into quiescent state before returning.
2481 ovsrcu_quiesce_start();
2482 VLOG_INFO("vHost Device '%s' has been removed", ifname
);
2484 VLOG_INFO("vHost Device '%s' not found", ifname
);
2489 vring_state_changed(int vid
, uint16_t queue_id
, int enable
)
2491 struct netdev_dpdk
*dev
;
2492 bool exists
= false;
2493 int qid
= queue_id
/ VIRTIO_QNUM
;
2494 char ifname
[IF_NAME_SZ
];
2496 rte_vhost_get_ifname(vid
, ifname
, sizeof ifname
);
2498 if (queue_id
% VIRTIO_QNUM
== VIRTIO_TXQ
) {
2502 ovs_mutex_lock(&dpdk_mutex
);
2503 LIST_FOR_EACH (dev
, list_node
, &dpdk_list
) {
2504 ovs_mutex_lock(&dev
->mutex
);
2505 if (strncmp(ifname
, dev
->vhost_id
, IF_NAME_SZ
) == 0) {
2507 dev
->tx_q
[qid
].map
= qid
;
2509 dev
->tx_q
[qid
].map
= OVS_VHOST_QUEUE_DISABLED
;
2511 netdev_dpdk_remap_txqs(dev
);
2513 ovs_mutex_unlock(&dev
->mutex
);
2516 ovs_mutex_unlock(&dev
->mutex
);
2518 ovs_mutex_unlock(&dpdk_mutex
);
2521 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2522 "changed to \'%s\'", queue_id
, qid
, ifname
,
2523 (enable
== 1) ? "enabled" : "disabled");
2525 VLOG_INFO("vHost Device '%s' not found", ifname
);
2533 netdev_dpdk_get_vid(const struct netdev_dpdk
*dev
)
2535 return ovsrcu_index_get(&dev
->vid
);
2538 struct ingress_policer
*
2539 netdev_dpdk_get_ingress_policer(const struct netdev_dpdk
*dev
)
2541 return ovsrcu_get(struct ingress_policer
*, &dev
->ingress_policer
);
2545 * These callbacks allow virtio-net devices to be added to vhost ports when
2546 * configuration has been fully complete.
2548 static const struct virtio_net_device_ops virtio_net_device_ops
=
2550 .new_device
= new_device
,
2551 .destroy_device
= destroy_device
,
2552 .vring_state_changed
= vring_state_changed
2556 start_vhost_loop(void *dummy OVS_UNUSED
)
2558 pthread_detach(pthread_self());
2559 /* Put the vhost thread into quiescent state. */
2560 ovsrcu_quiesce_start();
2561 rte_vhost_driver_session_start();
2566 dpdk_vhost_class_init(void)
2568 rte_vhost_driver_callback_register(&virtio_net_device_ops
);
2569 rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_HOST_TSO4
2570 | 1ULL << VIRTIO_NET_F_HOST_TSO6
2571 | 1ULL << VIRTIO_NET_F_CSUM
);
2573 ovs_thread_create("vhost_thread", start_vhost_loop
, NULL
);
2578 dpdk_common_init(void)
2580 unixctl_command_register("netdev-dpdk/set-admin-state",
2581 "[netdev] up|down", 1, 2,
2582 netdev_dpdk_set_admin_state
, NULL
);
2589 dpdk_ring_create(const char dev_name
[], unsigned int port_no
,
2590 unsigned int *eth_port_id
)
2592 struct dpdk_ring
*ivshmem
;
2596 ivshmem
= dpdk_rte_mzalloc(sizeof *ivshmem
);
2601 /* XXX: Add support for multiquque ring. */
2602 ring_name
= xasprintf("%s_tx", dev_name
);
2604 /* Create single producer tx ring, netdev does explicit locking. */
2605 ivshmem
->cring_tx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2608 if (ivshmem
->cring_tx
== NULL
) {
2613 ring_name
= xasprintf("%s_rx", dev_name
);
2615 /* Create single consumer rx ring, netdev does explicit locking. */
2616 ivshmem
->cring_rx
= rte_ring_create(ring_name
, DPDK_RING_SIZE
, SOCKET0
,
2619 if (ivshmem
->cring_rx
== NULL
) {
2624 err
= rte_eth_from_rings(dev_name
, &ivshmem
->cring_rx
, 1,
2625 &ivshmem
->cring_tx
, 1, SOCKET0
);
2632 ivshmem
->user_port_id
= port_no
;
2633 ivshmem
->eth_port_id
= rte_eth_dev_count() - 1;
2634 ovs_list_push_back(&dpdk_ring_list
, &ivshmem
->list_node
);
2636 *eth_port_id
= ivshmem
->eth_port_id
;
2641 dpdk_ring_open(const char dev_name
[], unsigned int *eth_port_id
)
2642 OVS_REQUIRES(dpdk_mutex
)
2644 struct dpdk_ring
*ivshmem
;
2645 unsigned int port_no
;
2648 /* Names always start with "dpdkr" */
2649 err
= dpdk_dev_parse_name(dev_name
, "dpdkr", &port_no
);
2654 /* Look through our list to find the device */
2655 LIST_FOR_EACH (ivshmem
, list_node
, &dpdk_ring_list
) {
2656 if (ivshmem
->user_port_id
== port_no
) {
2657 VLOG_INFO("Found dpdk ring device %s:", dev_name
);
2658 /* Really all that is needed */
2659 *eth_port_id
= ivshmem
->eth_port_id
;
2663 /* Need to create the device rings */
2664 return dpdk_ring_create(dev_name
, port_no
, eth_port_id
);
2668 netdev_dpdk_ring_send(struct netdev
*netdev
, int qid
,
2669 struct dp_packet_batch
*batch
, bool may_steal
,
2670 bool concurrent_txq
)
2672 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2675 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2676 * the rss hash field is clear. This is because the same mbuf may be
2677 * modified by the consumer of the ring and return into the datapath
2678 * without recalculating the RSS hash. */
2679 for (i
= 0; i
< batch
->count
; i
++) {
2680 dp_packet_rss_invalidate(batch
->packets
[i
]);
2683 netdev_dpdk_send__(dev
, qid
, batch
, may_steal
, concurrent_txq
);
2688 netdev_dpdk_ring_construct(struct netdev
*netdev
)
2690 unsigned int port_no
= 0;
2693 ovs_mutex_lock(&dpdk_mutex
);
2695 err
= dpdk_ring_open(netdev
->name
, &port_no
);
2700 err
= netdev_dpdk_init(netdev
, port_no
, DPDK_DEV_ETH
);
2703 ovs_mutex_unlock(&dpdk_mutex
);
2710 * Initialize QoS configuration operations.
2713 qos_conf_init(struct qos_conf
*conf
, const struct dpdk_qos_ops
*ops
)
2716 rte_spinlock_init(&conf
->lock
);
2720 * Search existing QoS operations in qos_ops and compare each set of
2721 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2724 static const struct dpdk_qos_ops
*
2725 qos_lookup_name(const char *name
)
2727 const struct dpdk_qos_ops
*const *opsp
;
2729 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2730 const struct dpdk_qos_ops
*ops
= *opsp
;
2731 if (!strcmp(name
, ops
->qos_name
)) {
2739 netdev_dpdk_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2742 const struct dpdk_qos_ops
*const *opsp
;
2744 for (opsp
= qos_confs
; *opsp
!= NULL
; opsp
++) {
2745 const struct dpdk_qos_ops
*ops
= *opsp
;
2746 if (ops
->qos_construct
&& ops
->qos_name
[0] != '\0') {
2747 sset_add(types
, ops
->qos_name
);
2754 netdev_dpdk_get_qos(const struct netdev
*netdev
,
2755 const char **typep
, struct smap
*details
)
2757 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2758 struct qos_conf
*qos_conf
;
2761 ovs_mutex_lock(&dev
->mutex
);
2762 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
2764 *typep
= qos_conf
->ops
->qos_name
;
2765 error
= (qos_conf
->ops
->qos_get
2766 ? qos_conf
->ops
->qos_get(qos_conf
, details
): 0);
2768 /* No QoS configuration set, return an empty string */
2771 ovs_mutex_unlock(&dev
->mutex
);
2777 netdev_dpdk_set_qos(struct netdev
*netdev
, const char *type
,
2778 const struct smap
*details
)
2780 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2781 const struct dpdk_qos_ops
*new_ops
= NULL
;
2782 struct qos_conf
*qos_conf
, *new_qos_conf
= NULL
;
2785 ovs_mutex_lock(&dev
->mutex
);
2787 qos_conf
= ovsrcu_get_protected(struct qos_conf
*, &dev
->qos_conf
);
2789 new_ops
= qos_lookup_name(type
);
2791 if (!new_ops
|| !new_ops
->qos_construct
) {
2792 new_qos_conf
= NULL
;
2793 if (type
&& type
[0]) {
2796 } else if (qos_conf
->ops
== new_ops
2797 && qos_conf
->ops
->qos_is_equal(qos_conf
, details
)) {
2798 new_qos_conf
= qos_conf
;
2800 error
= new_ops
->qos_construct(details
, &new_qos_conf
);
2804 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
2805 type
, netdev
->name
, rte_strerror(error
));
2808 if (new_qos_conf
!= qos_conf
) {
2809 ovsrcu_set(&dev
->qos_conf
, new_qos_conf
);
2811 ovsrcu_postpone(qos_conf
->ops
->qos_destruct
, qos_conf
);
2815 ovs_mutex_unlock(&dev
->mutex
);
2820 /* egress-policer details */
2822 struct egress_policer
{
2823 struct qos_conf qos_conf
;
2824 struct rte_meter_srtcm_params app_srtcm_params
;
2825 struct rte_meter_srtcm egress_meter
;
2829 egress_policer_details_to_param(const struct smap
*details
,
2830 struct rte_meter_srtcm_params
*params
)
2832 memset(params
, 0, sizeof *params
);
2833 params
->cir
= smap_get_ullong(details
, "cir", 0);
2834 params
->cbs
= smap_get_ullong(details
, "cbs", 0);
2839 egress_policer_qos_construct(const struct smap
*details
,
2840 struct qos_conf
**conf
)
2842 struct egress_policer
*policer
;
2845 policer
= xmalloc(sizeof *policer
);
2846 qos_conf_init(&policer
->qos_conf
, &egress_policer_ops
);
2847 egress_policer_details_to_param(details
, &policer
->app_srtcm_params
);
2848 err
= rte_meter_srtcm_config(&policer
->egress_meter
,
2849 &policer
->app_srtcm_params
);
2851 *conf
= &policer
->qos_conf
;
2862 egress_policer_qos_destruct(struct qos_conf
*conf
)
2864 struct egress_policer
*policer
= CONTAINER_OF(conf
, struct egress_policer
,
2870 egress_policer_qos_get(const struct qos_conf
*conf
, struct smap
*details
)
2872 struct egress_policer
*policer
=
2873 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
2875 smap_add_format(details
, "cir", "%"PRIu64
, policer
->app_srtcm_params
.cir
);
2876 smap_add_format(details
, "cbs", "%"PRIu64
, policer
->app_srtcm_params
.cbs
);
2882 egress_policer_qos_is_equal(const struct qos_conf
*conf
, const struct smap
*details
)
2884 struct egress_policer
*policer
=
2885 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
2886 struct rte_meter_srtcm_params params
;
2888 egress_policer_details_to_param(details
, ¶ms
);
2890 return !memcmp(¶ms
, &policer
->app_srtcm_params
, sizeof params
);
2894 egress_policer_run(struct qos_conf
*conf
, struct rte_mbuf
**pkts
, int pkt_cnt
)
2897 struct egress_policer
*policer
=
2898 CONTAINER_OF(conf
, struct egress_policer
, qos_conf
);
2900 cnt
= netdev_dpdk_policer_run(&policer
->egress_meter
, pkts
, pkt_cnt
);
2905 static const struct dpdk_qos_ops egress_policer_ops
= {
2906 "egress-policer", /* qos_name */
2907 egress_policer_qos_construct
,
2908 egress_policer_qos_destruct
,
2909 egress_policer_qos_get
,
2910 egress_policer_qos_is_equal
,
2915 netdev_dpdk_reconfigure(struct netdev
*netdev
)
2917 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2920 ovs_mutex_lock(&dev
->mutex
);
2922 if (netdev
->n_txq
== dev
->requested_n_txq
2923 && netdev
->n_rxq
== dev
->requested_n_rxq
2924 && dev
->mtu
== dev
->requested_mtu
2925 && dev
->rxq_size
== dev
->requested_rxq_size
2926 && dev
->txq_size
== dev
->requested_txq_size
) {
2927 /* Reconfiguration is unnecessary */
2932 rte_eth_dev_stop(dev
->port_id
);
2934 if (dev
->mtu
!= dev
->requested_mtu
) {
2935 netdev_dpdk_mempool_configure(dev
);
2938 netdev
->n_txq
= dev
->requested_n_txq
;
2939 netdev
->n_rxq
= dev
->requested_n_rxq
;
2941 dev
->rxq_size
= dev
->requested_rxq_size
;
2942 dev
->txq_size
= dev
->requested_txq_size
;
2944 rte_free(dev
->tx_q
);
2945 err
= dpdk_eth_dev_init(dev
);
2946 dev
->tx_q
= netdev_dpdk_alloc_txq(netdev
->n_txq
);
2951 netdev_change_seq_changed(netdev
);
2954 ovs_mutex_unlock(&dev
->mutex
);
2959 dpdk_vhost_reconfigure_helper(struct netdev_dpdk
*dev
)
2960 OVS_REQUIRES(dev
->mutex
)
2962 dev
->up
.n_txq
= dev
->requested_n_txq
;
2963 dev
->up
.n_rxq
= dev
->requested_n_rxq
;
2965 /* Enable TX queue 0 by default if it wasn't disabled. */
2966 if (dev
->tx_q
[0].map
== OVS_VHOST_QUEUE_MAP_UNKNOWN
) {
2967 dev
->tx_q
[0].map
= 0;
2970 netdev_dpdk_remap_txqs(dev
);
2972 if (dev
->requested_socket_id
!= dev
->socket_id
2973 || dev
->requested_mtu
!= dev
->mtu
) {
2974 if (!netdev_dpdk_mempool_configure(dev
)) {
2975 netdev_change_seq_changed(&dev
->up
);
2979 if (netdev_dpdk_get_vid(dev
) >= 0) {
2980 dev
->vhost_reconfigured
= true;
2985 netdev_dpdk_vhost_reconfigure(struct netdev
*netdev
)
2987 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
2989 ovs_mutex_lock(&dev
->mutex
);
2990 dpdk_vhost_reconfigure_helper(dev
);
2991 ovs_mutex_unlock(&dev
->mutex
);
2996 netdev_dpdk_vhost_client_reconfigure(struct netdev
*netdev
)
2998 struct netdev_dpdk
*dev
= netdev_dpdk_cast(netdev
);
3001 ovs_mutex_lock(&dev
->mutex
);
3003 dpdk_vhost_reconfigure_helper(dev
);
3005 /* Configure vHost client mode if requested and if the following criteria
3007 * 1. Device hasn't been registered yet.
3008 * 2. A path has been specified.
3010 if (!(dev
->vhost_driver_flags
& RTE_VHOST_USER_CLIENT
)
3011 && strlen(dev
->vhost_id
)) {
3012 /* Register client-mode device */
3013 err
= rte_vhost_driver_register(dev
->vhost_id
,
3014 RTE_VHOST_USER_CLIENT
);
3016 VLOG_ERR("vhost-user device setup failure for device %s\n",
3019 /* Configuration successful */
3020 dev
->vhost_driver_flags
|= RTE_VHOST_USER_CLIENT
;
3021 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3022 "using client socket '%s'",
3023 dev
->up
.name
, dev
->vhost_id
);
3027 ovs_mutex_unlock(&dev
->mutex
);
3032 #define NETDEV_DPDK_CLASS(NAME, CONSTRUCT, DESTRUCT, \
3033 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3034 GET_CARRIER, GET_STATS, \
3035 GET_FEATURES, GET_STATUS, \
3036 RECONFIGURE, RXQ_RECV) \
3039 true, /* is_pmd */ \
3041 NULL, /* netdev_dpdk_run */ \
3042 NULL, /* netdev_dpdk_wait */ \
3044 netdev_dpdk_alloc, \
3047 netdev_dpdk_dealloc, \
3048 netdev_dpdk_get_config, \
3050 NULL, /* get_tunnel_config */ \
3051 NULL, /* build header */ \
3052 NULL, /* push header */ \
3053 NULL, /* pop header */ \
3054 netdev_dpdk_get_numa_id, /* get_numa_id */ \
3058 NULL, /* send_wait */ \
3060 netdev_dpdk_set_etheraddr, \
3061 netdev_dpdk_get_etheraddr, \
3062 netdev_dpdk_get_mtu, \
3063 netdev_dpdk_set_mtu, \
3064 netdev_dpdk_get_ifindex, \
3066 netdev_dpdk_get_carrier_resets, \
3067 netdev_dpdk_set_miimon, \
3070 NULL, /* set_advertisements */ \
3072 netdev_dpdk_set_policing, \
3073 netdev_dpdk_get_qos_types, \
3074 NULL, /* get_qos_capabilities */ \
3075 netdev_dpdk_get_qos, \
3076 netdev_dpdk_set_qos, \
3077 NULL, /* get_queue */ \
3078 NULL, /* set_queue */ \
3079 NULL, /* delete_queue */ \
3080 NULL, /* get_queue_stats */ \
3081 NULL, /* queue_dump_start */ \
3082 NULL, /* queue_dump_next */ \
3083 NULL, /* queue_dump_done */ \
3084 NULL, /* dump_queue_stats */ \
3086 NULL, /* set_in4 */ \
3087 NULL, /* get_addr_list */ \
3088 NULL, /* add_router */ \
3089 NULL, /* get_next_hop */ \
3091 NULL, /* arp_lookup */ \
3093 netdev_dpdk_update_flags, \
3096 netdev_dpdk_rxq_alloc, \
3097 netdev_dpdk_rxq_construct, \
3098 netdev_dpdk_rxq_destruct, \
3099 netdev_dpdk_rxq_dealloc, \
3101 NULL, /* rx_wait */ \
3102 NULL, /* rxq_drain */ \
3106 process_vhost_flags(char *flag
, char *default_val
, int size
,
3107 const struct smap
*ovs_other_config
,
3113 val
= smap_get(ovs_other_config
, flag
);
3115 /* Process the vhost-sock-dir flag if it is provided, otherwise resort to
3118 if (val
&& (strlen(val
) <= size
)) {
3120 *new_val
= xstrdup(val
);
3121 VLOG_INFO("User-provided %s in use: %s", flag
, *new_val
);
3123 VLOG_INFO("No %s provided - defaulting to %s", flag
, default_val
);
3124 *new_val
= default_val
;
3131 grow_argv(char ***argv
, size_t cur_siz
, size_t grow_by
)
3133 return xrealloc(*argv
, sizeof(char *) * (cur_siz
+ grow_by
));
3137 dpdk_option_extend(char ***argv
, int argc
, const char *option
,
3140 char **newargv
= grow_argv(argv
, argc
, 2);
3142 newargv
[argc
] = xstrdup(option
);
3143 newargv
[argc
+1] = xstrdup(value
);
3147 move_argv(char ***argv
, size_t cur_size
, char **src_argv
, size_t src_argc
)
3149 char **newargv
= grow_argv(argv
, cur_size
, src_argc
);
3150 while (src_argc
--) {
3151 newargv
[cur_size
+src_argc
] = src_argv
[src_argc
];
3152 src_argv
[src_argc
] = NULL
;
3158 extra_dpdk_args(const char *ovs_extra_config
, char ***argv
, int argc
)
3161 char *release_tok
= xstrdup(ovs_extra_config
);
3162 char *tok
, *endptr
= NULL
;
3164 for (tok
= strtok_r(release_tok
, " ", &endptr
); tok
!= NULL
;
3165 tok
= strtok_r(NULL
, " ", &endptr
)) {
3166 char **newarg
= grow_argv(argv
, ret
, 1);
3168 newarg
[ret
++] = xstrdup(tok
);
3175 argv_contains(char **argv_haystack
, const size_t argc_haystack
,
3178 for (size_t i
= 0; i
< argc_haystack
; ++i
) {
3179 if (!strcmp(argv_haystack
[i
], needle
))
3186 construct_dpdk_options(const struct smap
*ovs_other_config
,
3187 char ***argv
, const int initial_size
,
3188 char **extra_args
, const size_t extra_argc
)
3190 struct dpdk_options_map
{
3191 const char *ovs_configuration
;
3192 const char *dpdk_option
;
3193 bool default_enabled
;
3194 const char *default_value
;
3196 {"dpdk-lcore-mask", "-c", false, NULL
},
3197 {"dpdk-hugepage-dir", "--huge-dir", false, NULL
},
3200 int i
, ret
= initial_size
;
3202 /*First, construct from the flat-options (non-mutex)*/
3203 for (i
= 0; i
< ARRAY_SIZE(opts
); ++i
) {
3204 const char *lookup
= smap_get(ovs_other_config
,
3205 opts
[i
].ovs_configuration
);
3206 if (!lookup
&& opts
[i
].default_enabled
) {
3207 lookup
= opts
[i
].default_value
;
3211 if (!argv_contains(extra_args
, extra_argc
, opts
[i
].dpdk_option
)) {
3212 dpdk_option_extend(argv
, ret
, opts
[i
].dpdk_option
, lookup
);
3215 VLOG_WARN("Ignoring database defined option '%s' due to "
3216 "dpdk_extras config", opts
[i
].dpdk_option
);
3224 #define MAX_DPDK_EXCL_OPTS 10
3227 construct_dpdk_mutex_options(const struct smap
*ovs_other_config
,
3228 char ***argv
, const int initial_size
,
3229 char **extra_args
, const size_t extra_argc
)
3231 struct dpdk_exclusive_options_map
{
3232 const char *category
;
3233 const char *ovs_dpdk_options
[MAX_DPDK_EXCL_OPTS
];
3234 const char *eal_dpdk_options
[MAX_DPDK_EXCL_OPTS
];
3235 const char *default_value
;
3239 {"dpdk-alloc-mem", "dpdk-socket-mem", NULL
,},
3240 {"-m", "--socket-mem", NULL
,},
3245 int i
, ret
= initial_size
;
3246 for (i
= 0; i
< ARRAY_SIZE(excl_opts
); ++i
) {
3247 int found_opts
= 0, scan
, found_pos
= -1;
3248 const char *found_value
;
3249 struct dpdk_exclusive_options_map
*popt
= &excl_opts
[i
];
3251 for (scan
= 0; scan
< MAX_DPDK_EXCL_OPTS
3252 && popt
->ovs_dpdk_options
[scan
]; ++scan
) {
3253 const char *lookup
= smap_get(ovs_other_config
,
3254 popt
->ovs_dpdk_options
[scan
]);
3255 if (lookup
&& strlen(lookup
)) {
3258 found_value
= lookup
;
3263 if (popt
->default_option
) {
3264 found_pos
= popt
->default_option
;
3265 found_value
= popt
->default_value
;
3271 if (found_opts
> 1) {
3272 VLOG_ERR("Multiple defined options for %s. Please check your"
3273 " database settings and reconfigure if necessary.",
3277 if (!argv_contains(extra_args
, extra_argc
,
3278 popt
->eal_dpdk_options
[found_pos
])) {
3279 dpdk_option_extend(argv
, ret
, popt
->eal_dpdk_options
[found_pos
],
3283 VLOG_WARN("Ignoring database defined option '%s' due to "
3284 "dpdk_extras config", popt
->eal_dpdk_options
[found_pos
]);
3292 get_dpdk_args(const struct smap
*ovs_other_config
, char ***argv
,
3295 const char *extra_configuration
;
3296 char **extra_args
= NULL
;
3298 size_t extra_argc
= 0;
3300 extra_configuration
= smap_get(ovs_other_config
, "dpdk-extra");
3301 if (extra_configuration
) {
3302 extra_argc
= extra_dpdk_args(extra_configuration
, &extra_args
, 0);
3305 i
= construct_dpdk_options(ovs_other_config
, argv
, argc
, extra_args
,
3307 i
= construct_dpdk_mutex_options(ovs_other_config
, argv
, i
, extra_args
,
3310 if (extra_configuration
) {
3311 *argv
= move_argv(argv
, i
, extra_args
, extra_argc
);
3314 return i
+ extra_argc
;
3317 static char **dpdk_argv
;
3318 static int dpdk_argc
;
3321 deferred_argv_release(void)
3324 for (result
= 0; result
< dpdk_argc
; ++result
) {
3325 free(dpdk_argv
[result
]);
3332 dpdk_init__(const struct smap
*ovs_other_config
)
3337 bool auto_determine
= true;
3340 char *sock_dir_subcomponent
;
3342 if (!smap_get_bool(ovs_other_config
, "dpdk-init", false)) {
3343 VLOG_INFO("DPDK Disabled - to change this requires a restart.\n");
3347 VLOG_INFO("DPDK Enabled, initializing");
3348 if (process_vhost_flags("vhost-sock-dir", xstrdup(ovs_rundir()),
3349 NAME_MAX
, ovs_other_config
,
3350 &sock_dir_subcomponent
)) {
3352 if (!strstr(sock_dir_subcomponent
, "..")) {
3353 vhost_sock_dir
= xasprintf("%s/%s", ovs_rundir(),
3354 sock_dir_subcomponent
);
3356 err
= stat(vhost_sock_dir
, &s
);
3358 VLOG_ERR("vhost-user sock directory '%s' does not exist.",
3362 vhost_sock_dir
= xstrdup(ovs_rundir());
3363 VLOG_ERR("vhost-user sock directory request '%s/%s' has invalid"
3364 "characters '..' - using %s instead.",
3365 ovs_rundir(), sock_dir_subcomponent
, ovs_rundir());
3367 free(sock_dir_subcomponent
);
3369 vhost_sock_dir
= sock_dir_subcomponent
;
3372 argv
= grow_argv(&argv
, 0, 1);
3374 argv
[0] = xstrdup(ovs_get_program_name());
3375 argc_tmp
= get_dpdk_args(ovs_other_config
, &argv
, argc
);
3377 while (argc_tmp
!= argc
) {
3378 if (!strcmp("-c", argv
[argc
]) || !strcmp("-l", argv
[argc
])) {
3379 auto_determine
= false;
3387 * NOTE: This is an unsophisticated mechanism for determining the DPDK
3388 * lcore for the DPDK Master.
3390 if (auto_determine
) {
3392 /* Get the main thread affinity */
3394 err
= pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t
),
3397 for (i
= 0; i
< CPU_SETSIZE
; i
++) {
3398 if (CPU_ISSET(i
, &cpuset
)) {
3399 argv
= grow_argv(&argv
, argc
, 2);
3400 argv
[argc
++] = xstrdup("-c");
3401 argv
[argc
++] = xasprintf("0x%08llX", (1ULL<<i
));
3406 VLOG_ERR("Thread getaffinity error %d. Using core 0x1", err
);
3407 /* User did not set dpdk-lcore-mask and unable to get current
3408 * thread affintity - default to core 0x1 */
3409 argv
= grow_argv(&argv
, argc
, 2);
3410 argv
[argc
++] = xstrdup("-c");
3411 argv
[argc
++] = xasprintf("0x%X", 1);
3415 argv
= grow_argv(&argv
, argc
, 1);
3420 if (VLOG_IS_INFO_ENABLED()) {
3424 ds_put_cstr(&eal_args
, "EAL ARGS:");
3425 for (opt
= 0; opt
< argc
; ++opt
) {
3426 ds_put_cstr(&eal_args
, " ");
3427 ds_put_cstr(&eal_args
, argv
[opt
]);
3429 VLOG_INFO("%s", ds_cstr_ro(&eal_args
));
3430 ds_destroy(&eal_args
);
3433 /* Make sure things are initialized ... */
3434 result
= rte_eal_init(argc
, argv
);
3436 ovs_abort(result
, "Cannot init EAL");
3439 /* Set the main thread affinity back to pre rte_eal_init() value */
3440 if (auto_determine
&& !err
) {
3441 err
= pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t
),
3444 VLOG_ERR("Thread setaffinity error %d", err
);
3451 atexit(deferred_argv_release
);
3453 rte_memzone_dump(stdout
);
3455 /* We are called from the main thread here */
3456 RTE_PER_LCORE(_lcore_id
) = NON_PMD_CORE_ID
;
3458 ovs_thread_create("dpdk_watchdog", dpdk_watchdog
, NULL
);
3460 dpdk_vhost_class_init();
3463 VLOG_INFO("DPDK pdump packet capture enabled");
3464 err
= rte_pdump_init(ovs_rundir());
3466 VLOG_INFO("Error initialising DPDK pdump");
3469 char *server_socket_path
;
3471 server_socket_path
= xasprintf("%s/%s", ovs_rundir(),
3472 "pdump_server_socket");
3473 fatal_signal_add_file_to_unlink(server_socket_path
);
3474 free(server_socket_path
);
3478 /* Finally, register the dpdk classes */
3479 netdev_dpdk_register();
3483 dpdk_init(const struct smap
*ovs_other_config
)
3485 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
3487 if (ovs_other_config
&& ovsthread_once_start(&once
)) {
3488 dpdk_init__(ovs_other_config
);
3489 ovsthread_once_done(&once
);
3493 static const struct netdev_class dpdk_class
=
3496 netdev_dpdk_construct
,
3497 netdev_dpdk_destruct
,
3498 netdev_dpdk_set_config
,
3499 netdev_dpdk_set_tx_multiq
,
3500 netdev_dpdk_eth_send
,
3501 netdev_dpdk_get_carrier
,
3502 netdev_dpdk_get_stats
,
3503 netdev_dpdk_get_features
,
3504 netdev_dpdk_get_status
,
3505 netdev_dpdk_reconfigure
,
3506 netdev_dpdk_rxq_recv
);
3508 static const struct netdev_class dpdk_ring_class
=
3511 netdev_dpdk_ring_construct
,
3512 netdev_dpdk_destruct
,
3513 netdev_dpdk_ring_set_config
,
3514 netdev_dpdk_set_tx_multiq
,
3515 netdev_dpdk_ring_send
,
3516 netdev_dpdk_get_carrier
,
3517 netdev_dpdk_get_stats
,
3518 netdev_dpdk_get_features
,
3519 netdev_dpdk_get_status
,
3520 netdev_dpdk_reconfigure
,
3521 netdev_dpdk_rxq_recv
);
3523 static const struct netdev_class dpdk_vhost_class
=
3526 netdev_dpdk_vhost_construct
,
3527 netdev_dpdk_vhost_destruct
,
3530 netdev_dpdk_vhost_send
,
3531 netdev_dpdk_vhost_get_carrier
,
3532 netdev_dpdk_vhost_get_stats
,
3535 netdev_dpdk_vhost_reconfigure
,
3536 netdev_dpdk_vhost_rxq_recv
);
3537 static const struct netdev_class dpdk_vhost_client_class
=
3539 "dpdkvhostuserclient",
3540 netdev_dpdk_vhost_client_construct
,
3541 netdev_dpdk_vhost_destruct
,
3542 netdev_dpdk_vhost_client_set_config
,
3544 netdev_dpdk_vhost_send
,
3545 netdev_dpdk_vhost_get_carrier
,
3546 netdev_dpdk_vhost_get_stats
,
3549 netdev_dpdk_vhost_client_reconfigure
,
3550 netdev_dpdk_vhost_rxq_recv
);
3553 netdev_dpdk_register(void)
3556 netdev_register_provider(&dpdk_class
);
3557 netdev_register_provider(&dpdk_ring_class
);
3558 netdev_register_provider(&dpdk_vhost_class
);
3559 netdev_register_provider(&dpdk_vhost_client_class
);
3563 dpdk_set_lcore_id(unsigned cpu
)
3565 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
3566 ovs_assert(cpu
!= NON_PMD_CORE_ID
);
3567 RTE_PER_LCORE(_lcore_id
) = cpu
;