]> git.proxmox.com Git - ovs.git/blame - lib/netdev-dpdk.c
ovn-controller: Only add comment in binding_cleanup() in case of changes.
[ovs.git] / lib / netdev-dpdk.c
CommitLineData
8a9562d2 1/*
12d0d124 2 * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc.
8a9562d2
PS
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
01961bbd 18#include "netdev-dpdk.h"
8a9562d2 19
8a9562d2
PS
20#include <string.h>
21#include <signal.h>
22#include <stdlib.h>
8a9562d2 23#include <errno.h>
8a9562d2 24#include <unistd.h>
f3e7ec25
MW
25#include <linux/virtio_net.h>
26#include <sys/socket.h>
27#include <linux/if.h>
01961bbd 28
5e925ccc 29#include <rte_bus_pci.h>
01961bbd
DDP
30#include <rte_config.h>
31#include <rte_cycles.h>
32#include <rte_errno.h>
33#include <rte_eth_ring.h>
34#include <rte_ethdev.h>
35#include <rte_malloc.h>
36#include <rte_mbuf.h>
37#include <rte_meter.h>
fc56f5e0 38#include <rte_pci.h>
f3e7ec25 39#include <rte_vhost.h>
3eb8d4fa 40#include <rte_version.h>
8a9562d2 41
7d1ced01 42#include "dirs.h"
e14deea0 43#include "dp-packet.h"
01961bbd 44#include "dpdk.h"
8a9562d2 45#include "dpif-netdev.h"
e5c0f5a4 46#include "fatal-signal.h"
8a9562d2
PS
47#include "netdev-provider.h"
48#include "netdev-vport.h"
49#include "odp-util.h"
eac84432 50#include "openvswitch/dynamic-string.h"
25d436fb
BW
51#include "openvswitch/list.h"
52#include "openvswitch/ofp-print.h"
53#include "openvswitch/vlog.h"
94143fc4 54#include "ovs-numa.h"
8a9562d2
PS
55#include "ovs-thread.h"
56#include "ovs-rcu.h"
57#include "packets.h"
ee89ea7b 58#include "openvswitch/shash.h"
0bf765f7 59#include "smap.h"
8a9562d2
PS
60#include "sset.h"
61#include "unaligned.h"
62#include "timeval.h"
63#include "unixctl.h"
8a9562d2 64
f3e7ec25
MW
65enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
66
05b49df6 67VLOG_DEFINE_THIS_MODULE(netdev_dpdk);
8a9562d2
PS
68static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
69
70#define DPDK_PORT_WATCHDOG_INTERVAL 5
71
72#define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
73#define OVS_VPORT_DPDK "ovs_dpdk"
74
75/*
76 * need to reserve tons of extra space in the mbufs so we can align the
77 * DMA addresses to 4KB.
18f777b2
TP
78 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
79 * performance for standard Ethernet MTU.
8a9562d2 80 */
58be5c0e
MK
81#define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN \
82 + (2 * VLAN_HEADER_LEN))
4be4d22c
MK
83#define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
84#define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
58be5c0e
MK
85#define FRAME_LEN_TO_MTU(frame_len) ((frame_len) \
86 - ETHER_HDR_LEN - ETHER_CRC_LEN)
31b88c97
SS
87#define MBUF_SIZE(mtu) ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) \
88 + sizeof(struct dp_packet) \
89 + RTE_PKTMBUF_HEADROOM), \
90 RTE_CACHE_LINE_SIZE)
4be4d22c 91#define NETDEV_DPDK_MBUF_ALIGN 1024
0072e931 92#define NETDEV_DPDK_MAX_PKT_LEN 9728
8a9562d2 93
bc57ed90
IM
94/* Min number of packets in the mempool. OVS tries to allocate a mempool with
95 * roughly estimated number of mbufs: if this fails (because the system doesn't
96 * have enough hugepages) we keep halving the number until the allocation
97 * succeeds or we reach MIN_NB_MBUF */
da79ce2b
DDP
98#define MIN_NB_MBUF (4096 * 4)
99#define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
100
d6e3feb5 101/*
102 * DPDK XSTATS Counter names definition
103 */
104#define XSTAT_RX_64_PACKETS "rx_size_64_packets"
105#define XSTAT_RX_65_TO_127_PACKETS "rx_size_65_to_127_packets"
106#define XSTAT_RX_128_TO_255_PACKETS "rx_size_128_to_255_packets"
107#define XSTAT_RX_256_TO_511_PACKETS "rx_size_256_to_511_packets"
108#define XSTAT_RX_512_TO_1023_PACKETS "rx_size_512_to_1023_packets"
109#define XSTAT_RX_1024_TO_1522_PACKETS "rx_size_1024_to_1522_packets"
110#define XSTAT_RX_1523_TO_MAX_PACKETS "rx_size_1523_to_max_packets"
111
112#define XSTAT_TX_64_PACKETS "tx_size_64_packets"
113#define XSTAT_TX_65_TO_127_PACKETS "tx_size_65_to_127_packets"
114#define XSTAT_TX_128_TO_255_PACKETS "tx_size_128_to_255_packets"
115#define XSTAT_TX_256_TO_511_PACKETS "tx_size_256_to_511_packets"
116#define XSTAT_TX_512_TO_1023_PACKETS "tx_size_512_to_1023_packets"
117#define XSTAT_TX_1024_TO_1522_PACKETS "tx_size_1024_to_1522_packets"
118#define XSTAT_TX_1523_TO_MAX_PACKETS "tx_size_1523_to_max_packets"
119
d57f777f 120#define XSTAT_RX_MULTICAST_PACKETS "rx_multicast_packets"
d6e3feb5 121#define XSTAT_TX_MULTICAST_PACKETS "tx_multicast_packets"
122#define XSTAT_RX_BROADCAST_PACKETS "rx_broadcast_packets"
123#define XSTAT_TX_BROADCAST_PACKETS "tx_broadcast_packets"
124#define XSTAT_RX_UNDERSIZED_ERRORS "rx_undersized_errors"
125#define XSTAT_RX_OVERSIZE_ERRORS "rx_oversize_errors"
126#define XSTAT_RX_FRAGMENTED_ERRORS "rx_fragmented_errors"
127#define XSTAT_RX_JABBER_ERRORS "rx_jabber_errors"
128
8a9562d2
PS
129#define SOCKET0 0
130
b685696b
CL
131/* Default size of Physical NIC RXQ */
132#define NIC_PORT_DEFAULT_RXQ_SIZE 2048
133/* Default size of Physical NIC TXQ */
134#define NIC_PORT_DEFAULT_TXQ_SIZE 2048
135/* Maximum size of Physical NIC Queues */
136#define NIC_PORT_MAX_Q_SIZE 4096
79f5354c 137
585a5bea 138#define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
f3ea2ad2
IM
139#define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
140#define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
141 * yet mapped to another queue. */
585a5bea 142
bb37956a
IM
143#define DPDK_ETH_PORT_ID_INVALID RTE_MAX_ETHPORTS
144
5e925ccc
MK
145/* DPDK library uses uint16_t for port_id. */
146typedef uint16_t dpdk_port_t;
fa9f4eeb 147#define DPDK_PORT_ID_FMT "%"PRIu16
bb37956a 148
31871ee3 149#define VHOST_ENQ_RETRY_NUM 8
0a0f39df 150#define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
95e9881f 151
8a9562d2 152static const struct rte_eth_conf port_conf = {
a28ddd11
DDP
153 .rxmode = {
154 .mq_mode = ETH_MQ_RX_RSS,
155 .split_hdr_size = 0,
156 .header_split = 0, /* Header Split disabled */
157 .hw_ip_checksum = 0, /* IP checksum offload disabled */
158 .hw_vlan_filter = 0, /* VLAN filtering disabled */
159 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
160 .hw_strip_crc = 0,
161 },
162 .rx_adv_conf = {
163 .rss_conf = {
164 .rss_key = NULL,
543342a4 165 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP,
8a9562d2 166 },
a28ddd11
DDP
167 },
168 .txmode = {
169 .mq_mode = ETH_MQ_TX_NONE,
170 },
8a9562d2
PS
171};
172
f3e7ec25
MW
173/*
174 * These callbacks allow virtio-net devices to be added to vhost ports when
175 * configuration has been fully completed.
176 */
177static int new_device(int vid);
178static void destroy_device(int vid);
179static int vring_state_changed(int vid, uint16_t queue_id, int enable);
180static const struct vhost_device_ops virtio_net_device_ops =
181{
182 .new_device = new_device,
183 .destroy_device = destroy_device,
184 .vring_state_changed = vring_state_changed,
185 .features_changed = NULL
186};
187
58f7c37b
DDP
188enum { DPDK_RING_SIZE = 256 };
189BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
8a9562d2
PS
190enum { DRAIN_TSC = 200000ULL };
191
58397e6c
KT
192enum dpdk_dev_type {
193 DPDK_DEV_ETH = 0,
7d1ced01 194 DPDK_DEV_VHOST = 1,
58397e6c
KT
195};
196
0bf765f7
IS
197/* Quality of Service */
198
199/* An instance of a QoS configuration. Always associated with a particular
200 * network device.
201 *
202 * Each QoS implementation subclasses this with whatever additional data it
203 * needs.
204 */
205struct qos_conf {
206 const struct dpdk_qos_ops *ops;
78bd47cf 207 rte_spinlock_t lock;
0bf765f7
IS
208};
209
210/* A particular implementation of dpdk QoS operations.
211 *
212 * The functions below return 0 if successful or a positive errno value on
213 * failure, except where otherwise noted. All of them must be provided, except
214 * where otherwise noted.
215 */
216struct dpdk_qos_ops {
217
218 /* Name of the QoS type */
219 const char *qos_name;
220
78bd47cf
DDP
221 /* Called to construct a qos_conf object. The implementation should make
222 * the appropriate calls to configure QoS according to 'details'.
0bf765f7
IS
223 *
224 * The contents of 'details' should be documented as valid for 'ovs_name'
225 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
226 * (which is built as ovs-vswitchd.conf.db(8)).
227 *
78bd47cf
DDP
228 * This function must return 0 if and only if it sets '*conf' to an
229 * initialized 'struct qos_conf'.
0bf765f7
IS
230 *
231 * For all QoS implementations it should always be non-null.
232 */
78bd47cf 233 int (*qos_construct)(const struct smap *details, struct qos_conf **conf);
0bf765f7
IS
234
235 /* Destroys the data structures allocated by the implementation as part of
78bd47cf 236 * 'qos_conf'.
0bf765f7
IS
237 *
238 * For all QoS implementations it should always be non-null.
239 */
78bd47cf 240 void (*qos_destruct)(struct qos_conf *conf);
0bf765f7 241
78bd47cf 242 /* Retrieves details of 'conf' configuration into 'details'.
0bf765f7
IS
243 *
244 * The contents of 'details' should be documented as valid for 'ovs_name'
245 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
246 * (which is built as ovs-vswitchd.conf.db(8)).
247 */
78bd47cf 248 int (*qos_get)(const struct qos_conf *conf, struct smap *details);
0bf765f7 249
78bd47cf 250 /* Returns true if 'conf' is already configured according to 'details'.
0bf765f7
IS
251 *
252 * The contents of 'details' should be documented as valid for 'ovs_name'
253 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
254 * (which is built as ovs-vswitchd.conf.db(8)).
255 *
78bd47cf 256 * For all QoS implementations it should always be non-null.
0bf765f7 257 */
78bd47cf
DDP
258 bool (*qos_is_equal)(const struct qos_conf *conf,
259 const struct smap *details);
0bf765f7
IS
260
261 /* Modify an array of rte_mbufs. The modification is specific to
262 * each qos implementation.
263 *
264 * The function should take and array of mbufs and an int representing
265 * the current number of mbufs present in the array.
266 *
267 * After the function has performed a qos modification to the array of
268 * mbufs it returns an int representing the number of mbufs now present in
269 * the array. This value is can then be passed to the port send function
270 * along with the modified array for transmission.
271 *
272 * For all QoS implementations it should always be non-null.
273 */
78bd47cf 274 int (*qos_run)(struct qos_conf *qos_conf, struct rte_mbuf **pkts,
7d7ded7a 275 int pkt_cnt, bool should_steal);
0bf765f7
IS
276};
277
278/* dpdk_qos_ops for each type of user space QoS implementation */
279static const struct dpdk_qos_ops egress_policer_ops;
280
281/*
282 * Array of dpdk_qos_ops, contains pointer to all supported QoS
283 * operations.
284 */
285static const struct dpdk_qos_ops *const qos_confs[] = {
286 &egress_policer_ops,
287 NULL
288};
289
c2adb102
IM
290static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
291
8a9562d2 292/* Contains all 'struct dpdk_dev's. */
ca6ba700 293static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
55951e15 294 = OVS_LIST_INITIALIZER(&dpdk_list);
8a9562d2 295
c2adb102
IM
296static struct ovs_mutex dpdk_mp_mutex OVS_ACQ_AFTER(dpdk_mutex)
297 = OVS_MUTEX_INITIALIZER;
298
91fccdad
KT
299/* Contains all 'struct dpdk_mp's. */
300static struct ovs_list dpdk_mp_free_list OVS_GUARDED_BY(dpdk_mp_mutex)
301 = OVS_LIST_INITIALIZER(&dpdk_mp_free_list);
302
303/* Wrapper for a mempool released but not yet freed. */
304struct dpdk_mp {
305 struct rte_mempool *mp;
306 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mp_mutex);
307 };
308
5a034064
AW
309/* There should be one 'struct dpdk_tx_queue' created for
310 * each cpu core. */
8a9562d2 311struct dpdk_tx_queue {
a0cb2d66
DDP
312 rte_spinlock_t tx_lock; /* Protects the members and the NIC queue
313 * from concurrent access. It is used only
314 * if the queue is shared among different
324c8374 315 * pmd threads (see 'concurrent_txq'). */
585a5bea
IM
316 int map; /* Mapping of configured vhost-user queues
317 * to enabled by guest. */
8a9562d2
PS
318};
319
95fb793a 320/* dpdk has no way to remove dpdk ring ethernet devices
321 so we have to keep them around once they've been created
322*/
323
ca6ba700 324static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
55951e15 325 = OVS_LIST_INITIALIZER(&dpdk_ring_list);
95fb793a 326
327struct dpdk_ring {
328 /* For the client rings */
329 struct rte_ring *cring_tx;
330 struct rte_ring *cring_rx;
b83a2df1 331 unsigned int user_port_id; /* User given port no, parsed from port name */
bb37956a 332 dpdk_port_t eth_port_id; /* ethernet device port id */
ca6ba700 333 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
95fb793a 334};
335
9509913a
IS
336struct ingress_policer {
337 struct rte_meter_srtcm_params app_srtcm_params;
338 struct rte_meter_srtcm in_policer;
339 rte_spinlock_t policer_lock;
340};
341
1a2bb118
SC
342enum dpdk_hw_ol_features {
343 NETDEV_RX_CHECKSUM_OFFLOAD = 1 << 0,
e10ca8b9 344 NETDEV_RX_HW_CRC_STRIP = 1 << 1,
1a2bb118
SC
345};
346
b2e72a9c
IM
347/*
348 * In order to avoid confusion in variables names, following naming convention
349 * should be used, if possible:
350 *
351 * 'struct netdev' : 'netdev'
352 * 'struct netdev_dpdk' : 'dev'
353 * 'struct netdev_rxq' : 'rxq'
354 * 'struct netdev_rxq_dpdk' : 'rx'
355 *
356 * Example:
357 * struct netdev *netdev = netdev_from_name(name);
358 * struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
359 *
360 * Also, 'netdev' should be used instead of 'dev->up', where 'netdev' was
361 * already defined.
362 */
363
8a9562d2 364struct netdev_dpdk {
23d4d53f
BB
365 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline0,
366 dpdk_port_t port_id;
367
368 /* If true, device was attached by rte_eth_dev_attach(). */
369 bool attached;
606f6650
EC
370 /* If true, rte_eth_dev_start() was successfully called */
371 bool started;
23d4d53f
BB
372 struct eth_addr hwaddr;
373 int mtu;
374 int socket_id;
375 int buf_size;
376 int max_packet_len;
377 enum dpdk_dev_type type;
378 enum netdev_flags flags;
eaa43581 379 int link_reset_cnt;
23d4d53f
BB
380 char *devargs; /* Device arguments for dpdk ports */
381 struct dpdk_tx_queue *tx_q;
382 struct rte_eth_link link;
23d4d53f
BB
383 );
384
385 PADDED_MEMBERS_CACHELINE_MARKER(CACHE_LINE_SIZE, cacheline1,
386 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
24e78f93 387 struct rte_mempool *mp;
23d4d53f
BB
388
389 /* virtio identifier for vhost devices */
390 ovsrcu_index vid;
391
392 /* True if vHost device is 'up' and has been reconfigured at least once */
393 bool vhost_reconfigured;
394 /* 3 pad bytes here. */
395 );
396
397 PADDED_MEMBERS(CACHE_LINE_SIZE,
398 /* Identifier used to distinguish vhost devices from each other. */
399 char vhost_id[PATH_MAX];
400 );
401
402 PADDED_MEMBERS(CACHE_LINE_SIZE,
403 struct netdev up;
404 /* In dpdk_list. */
405 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
406
407 /* QoS configuration and lock for the device */
408 OVSRCU_TYPE(struct qos_conf *) qos_conf;
409
410 /* Ingress Policer */
411 OVSRCU_TYPE(struct ingress_policer *) ingress_policer;
412 uint32_t policer_rate;
413 uint32_t policer_burst;
414 );
415
416 PADDED_MEMBERS(CACHE_LINE_SIZE,
417 struct netdev_stats stats;
418 /* Protects stats */
419 rte_spinlock_t stats_lock;
420 /* 44 pad bytes here. */
421 );
422
423 PADDED_MEMBERS(CACHE_LINE_SIZE,
424 /* The following properties cannot be changed when a device is running,
425 * so we remember the request and update them next time
426 * netdev_dpdk*_reconfigure() is called */
427 int requested_mtu;
428 int requested_n_txq;
429 int requested_n_rxq;
430 int requested_rxq_size;
431 int requested_txq_size;
432
433 /* Number of rx/tx descriptors for physical devices */
434 int rxq_size;
435 int txq_size;
436
437 /* Socket ID detected when vHost device is brought up */
438 int requested_socket_id;
439
440 /* Denotes whether vHost port is client/server mode */
441 uint64_t vhost_driver_flags;
442
443 /* DPDK-ETH Flow control */
444 struct rte_eth_fc_conf fc_conf;
445
446 /* DPDK-ETH hardware offload features,
447 * from the enum set 'dpdk_hw_ol_features' */
448 uint32_t hw_ol_features;
f8b64a61
RM
449
450 /* Properties for link state change detection mode.
451 * If lsc_interrupt_mode is set to false, poll mode is used,
452 * otherwise interrupt mode is used. */
453 bool requested_lsc_interrupt_mode;
454 bool lsc_interrupt_mode;
23d4d53f 455 );
971f4b39
MW
456
457 PADDED_MEMBERS(CACHE_LINE_SIZE,
458 /* Names of all XSTATS counters */
459 struct rte_eth_xstat_name *rte_xstats_names;
460 int rte_xstats_names_size;
461 int rte_xstats_ids_size;
462 uint64_t *rte_xstats_ids;
463 );
8a9562d2
PS
464};
465
466struct netdev_rxq_dpdk {
467 struct netdev_rxq up;
bb37956a 468 dpdk_port_t port_id;
8a9562d2
PS
469};
470
f3e7ec25
MW
471static void netdev_dpdk_destruct(struct netdev *netdev);
472static void netdev_dpdk_vhost_destruct(struct netdev *netdev);
8a9562d2 473
ac1a9bb9
IM
474static void netdev_dpdk_clear_xstats(struct netdev_dpdk *dev);
475
0a0f39df 476int netdev_dpdk_get_vid(const struct netdev_dpdk *dev);
58397e6c 477
9509913a
IS
478struct ingress_policer *
479netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev);
480
8a9562d2
PS
481static bool
482is_dpdk_class(const struct netdev_class *class)
483{
f3e7ec25
MW
484 return class->destruct == netdev_dpdk_destruct
485 || class->destruct == netdev_dpdk_vhost_destruct;
8a9562d2
PS
486}
487
4be4d22c
MK
488/* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
489 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
490 * value, insufficient buffers are allocated to accomodate the packet in its
491 * entirety. Furthermore, certain drivers need to ensure that there is also
492 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
493 * frames). If the RX buffer is too small, then the driver enables scatter RX
58be5c0e
MK
494 * behaviour, which reduces performance. To prevent this, use a buffer size
495 * that is closest to 'mtu', but which satisfies the aforementioned criteria.
4be4d22c
MK
496 */
497static uint32_t
498dpdk_buf_size(int mtu)
499{
500 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) + RTE_PKTMBUF_HEADROOM),
501 NETDEV_DPDK_MBUF_ALIGN);
502}
503
eff23640
DDP
504/* Allocates an area of 'sz' bytes from DPDK. The memory is zero'ed.
505 *
506 * Unlike xmalloc(), this function can return NULL on failure. */
8a9562d2
PS
507static void *
508dpdk_rte_mzalloc(size_t sz)
509{
eff23640 510 return rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
8a9562d2
PS
511}
512
513void
e14deea0 514free_dpdk_buf(struct dp_packet *p)
8a9562d2 515{
db73f716 516 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
8a9562d2 517
b00b4a81 518 rte_pktmbuf_free(pkt);
8a9562d2
PS
519}
520
b3cd9f9d 521static void
401b70d6 522ovs_rte_pktmbuf_init(struct rte_mempool *mp OVS_UNUSED,
b3cd9f9d 523 void *opaque_arg OVS_UNUSED,
2391135c 524 void *_p,
b3cd9f9d
PS
525 unsigned i OVS_UNUSED)
526{
2391135c 527 struct rte_mbuf *pkt = _p;
b3cd9f9d 528
2391135c 529 dp_packet_init_dpdk((struct dp_packet *) pkt, pkt->buf_len);
b3cd9f9d
PS
530}
531
91fccdad
KT
532static int
533dpdk_mp_full(const struct rte_mempool *mp) OVS_REQUIRES(dpdk_mp_mutex)
534{
1f84a2d5
KT
535 /* At this point we want to know if all the mbufs are back
536 * in the mempool. rte_mempool_full() is not atomic but it's
537 * the best available and as we are no longer requesting mbufs
538 * from the mempool, it means mbufs will not move from
539 * 'mempool ring' --> 'mempool cache'. In rte_mempool_full()
540 * the ring is counted before caches, so we won't get false
541 * positives in this use case and we handle false negatives.
542 *
543 * If future implementations of rte_mempool_full() were to change
544 * it could be possible for a false positive. Even that would
545 * likely be ok, as there are additional checks during mempool
546 * freeing but it would make things racey.
91fccdad 547 */
1f84a2d5 548 return rte_mempool_full(mp);
91fccdad
KT
549}
550
551/* Free unused mempools. */
552static void
553dpdk_mp_sweep(void)
554{
555 struct dpdk_mp *dmp, *next;
556
557 ovs_mutex_lock(&dpdk_mp_mutex);
558 LIST_FOR_EACH_SAFE (dmp, next, list_node, &dpdk_mp_free_list) {
559 if (dpdk_mp_full(dmp->mp)) {
560 VLOG_DBG("Freeing mempool \"%s\"", dmp->mp->name);
561 ovs_list_remove(&dmp->list_node);
562 rte_mempool_free(dmp->mp);
563 rte_free(dmp);
564 }
565 }
566 ovs_mutex_unlock(&dpdk_mp_mutex);
567}
568
569/* Ensure a mempool will not be freed. */
570static void
571dpdk_mp_do_not_free(struct rte_mempool *mp) OVS_REQUIRES(dpdk_mp_mutex)
572{
573 struct dpdk_mp *dmp, *next;
574
575 LIST_FOR_EACH_SAFE (dmp, next, list_node, &dpdk_mp_free_list) {
576 if (dmp->mp == mp) {
577 VLOG_DBG("Removing mempool \"%s\" from free list", dmp->mp->name);
578 ovs_list_remove(&dmp->list_node);
579 rte_free(dmp);
580 break;
581 }
582 }
583}
584
24e78f93
IM
585/* Returns a valid pointer when either of the following is true:
586 * - a new mempool was just created;
587 * - a matching mempool already exists. */
588static struct rte_mempool *
589dpdk_mp_create(struct netdev_dpdk *dev, int mtu)
8a9562d2 590{
24e78f93
IM
591 char mp_name[RTE_MEMPOOL_NAMESIZE];
592 const char *netdev_name = netdev_get_name(&dev->up);
593 int socket_id = dev->requested_socket_id;
594 uint32_t n_mbufs;
595 uint32_t hash = hash_string(netdev_name, 0);
596 struct rte_mempool *mp = NULL;
d555d9bd
RW
597
598 /*
ad9b5b9b 599 * XXX: rough estimation of number of mbufs required for this port:
d555d9bd
RW
600 * <packets required to fill the device rxqs>
601 * + <packets that could be stuck on other ports txqs>
602 * + <packets in the pmd threads>
603 * + <additional memory for corner cases>
0072e931 604 */
24e78f93
IM
605 n_mbufs = dev->requested_n_rxq * dev->requested_rxq_size
606 + dev->requested_n_txq * dev->requested_txq_size
607 + MIN(RTE_MAX_LCORE, dev->requested_n_rxq) * NETDEV_MAX_BURST
608 + MIN_NB_MBUF;
d555d9bd 609
24e78f93 610 ovs_mutex_lock(&dpdk_mp_mutex);
da79ce2b 611 do {
24e78f93
IM
612 /* Full DPDK memory pool name must be unique and cannot be
613 * longer than RTE_MEMPOOL_NAMESIZE. */
af5b0dad
IM
614 int ret = snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
615 "ovs%08x%02d%05d%07u",
24e78f93
IM
616 hash, socket_id, mtu, n_mbufs);
617 if (ret < 0 || ret >= RTE_MEMPOOL_NAMESIZE) {
618 VLOG_DBG("snprintf returned %d. "
619 "Failed to generate a mempool name for \"%s\". "
620 "Hash:0x%x, socket_id: %d, mtu:%d, mbufs:%u.",
621 ret, netdev_name, hash, socket_id, mtu, n_mbufs);
622 break;
65056fd7 623 }
95fb793a 624
f06546a5
FA
625 VLOG_DBG("Port %s: Requesting a mempool of %u mbufs "
626 "on socket %d for %d Rx and %d Tx queues.",
24e78f93 627 netdev_name, n_mbufs, socket_id,
f06546a5 628 dev->requested_n_rxq, dev->requested_n_txq);
d555d9bd 629
24e78f93
IM
630 mp = rte_pktmbuf_pool_create(mp_name, n_mbufs, MP_CACHE_SZ,
631 sizeof (struct dp_packet) - sizeof (struct rte_mbuf),
632 MBUF_SIZE(mtu) - sizeof(struct dp_packet), socket_id);
633
634 if (mp) {
635 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs",
636 mp_name, n_mbufs);
837c1761
FA
637 /* rte_pktmbuf_pool_create has done some initialization of the
638 * rte_mbuf part of each dp_packet. Some OvS specific fields
639 * of the packet still need to be initialized by
640 * ovs_rte_pktmbuf_init. */
24e78f93 641 rte_mempool_obj_iter(mp, ovs_rte_pktmbuf_init, NULL);
d555d9bd
RW
642 } else if (rte_errno == EEXIST) {
643 /* A mempool with the same name already exists. We just
644 * retrieve its pointer to be returned to the caller. */
24e78f93 645 mp = rte_mempool_lookup(mp_name);
d555d9bd
RW
646 /* As the mempool create returned EEXIST we can expect the
647 * lookup has returned a valid pointer. If for some reason
648 * that's not the case we keep track of it. */
24e78f93
IM
649 VLOG_DBG("A mempool with name \"%s\" already exists at %p.",
650 mp_name, mp);
91fccdad
KT
651 /* Ensure this reused mempool will not be freed. */
652 dpdk_mp_do_not_free(mp);
d555d9bd
RW
653 } else {
654 VLOG_ERR("Failed mempool \"%s\" create request of %u mbufs",
24e78f93 655 mp_name, n_mbufs);
0c6f39e5 656 }
24e78f93 657 } while (!mp && rte_errno == ENOMEM && (n_mbufs /= 2) >= MIN_NB_MBUF);
2ae3d542 658
c2adb102 659 ovs_mutex_unlock(&dpdk_mp_mutex);
24e78f93 660 return mp;
8a9562d2
PS
661}
662
a08a115d 663/* Release an existing mempool. */
8a9562d2 664static void
91fccdad 665dpdk_mp_release(struct rte_mempool *mp)
8a9562d2 666{
24e78f93 667 if (!mp) {
8a9562d2
PS
668 return;
669 }
670
c2adb102 671 ovs_mutex_lock(&dpdk_mp_mutex);
91fccdad
KT
672 if (dpdk_mp_full(mp)) {
673 VLOG_DBG("Freeing mempool \"%s\"", mp->name);
674 rte_mempool_free(mp);
675 } else {
676 struct dpdk_mp *dmp;
677
678 dmp = dpdk_rte_mzalloc(sizeof *dmp);
679 if (dmp) {
680 dmp->mp = mp;
681 ovs_list_push_back(&dpdk_mp_free_list, &dmp->list_node);
682 }
683 }
c2adb102 684 ovs_mutex_unlock(&dpdk_mp_mutex);
8a9562d2
PS
685}
686
b6b26021
FA
687/* Tries to allocate a new mempool - or re-use an existing one where
688 * appropriate - on requested_socket_id with a size determined by
689 * requested_mtu and requested Rx/Tx queues.
690 * On success - or when re-using an existing mempool - the new configuration
691 * will be applied.
0072e931
MK
692 * On error, device will be left unchanged. */
693static int
694netdev_dpdk_mempool_configure(struct netdev_dpdk *dev)
0072e931
MK
695 OVS_REQUIRES(dev->mutex)
696{
697 uint32_t buf_size = dpdk_buf_size(dev->requested_mtu);
24e78f93
IM
698 struct rte_mempool *mp;
699 int ret = 0;
0072e931 700
91fccdad
KT
701 dpdk_mp_sweep();
702
24e78f93 703 mp = dpdk_mp_create(dev, FRAME_LEN_TO_MTU(buf_size));
0072e931 704 if (!mp) {
c67e46c0
MK
705 VLOG_ERR("Failed to create memory pool for netdev "
706 "%s, with MTU %d on socket %d: %s\n",
707 dev->up.name, dev->requested_mtu, dev->requested_socket_id,
708 rte_strerror(rte_errno));
24e78f93 709 ret = rte_errno;
0072e931 710 } else {
24e78f93
IM
711 /* If a new MTU was requested and its rounded value equals the one
712 * that is currently used, then the existing mempool is returned. */
713 if (dev->mp != mp) {
714 /* A new mempool was created, release the previous one. */
91fccdad 715 dpdk_mp_release(dev->mp);
24e78f93
IM
716 } else {
717 ret = EEXIST;
718 }
719 dev->mp = mp;
0072e931
MK
720 dev->mtu = dev->requested_mtu;
721 dev->socket_id = dev->requested_socket_id;
722 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
723 }
724
24e78f93 725 return ret;
0072e931
MK
726}
727
8a9562d2
PS
728static void
729check_link_status(struct netdev_dpdk *dev)
730{
731 struct rte_eth_link link;
732
733 rte_eth_link_get_nowait(dev->port_id, &link);
734
735 if (dev->link.link_status != link.link_status) {
3e912ffc 736 netdev_change_seq_changed(&dev->up);
8a9562d2
PS
737
738 dev->link_reset_cnt++;
739 dev->link = link;
740 if (dev->link.link_status) {
fa9f4eeb
IM
741 VLOG_DBG_RL(&rl,
742 "Port "DPDK_PORT_ID_FMT" Link Up - speed %u Mbps - %s",
58be5c0e 743 dev->port_id, (unsigned) dev->link.link_speed,
fa9f4eeb
IM
744 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX)
745 ? "full-duplex" : "half-duplex");
8a9562d2 746 } else {
fa9f4eeb
IM
747 VLOG_DBG_RL(&rl, "Port "DPDK_PORT_ID_FMT" Link Down",
748 dev->port_id);
8a9562d2
PS
749 }
750 }
751}
752
753static void *
754dpdk_watchdog(void *dummy OVS_UNUSED)
755{
756 struct netdev_dpdk *dev;
757
758 pthread_detach(pthread_self());
759
760 for (;;) {
761 ovs_mutex_lock(&dpdk_mutex);
762 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
763 ovs_mutex_lock(&dev->mutex);
1f5b157e
IM
764 if (dev->type == DPDK_DEV_ETH) {
765 check_link_status(dev);
766 }
8a9562d2
PS
767 ovs_mutex_unlock(&dev->mutex);
768 }
769 ovs_mutex_unlock(&dpdk_mutex);
770 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
771 }
772
773 return NULL;
774}
775
b98d7669 776static int
f8b64a61 777dpdk_eth_dev_port_config(struct netdev_dpdk *dev, int n_rxq, int n_txq)
b98d7669
DDP
778{
779 int diag = 0;
780 int i;
0072e931 781 struct rte_eth_conf conf = port_conf;
65a87968 782 struct rte_eth_dev_info info;
4dd16ca0 783 uint16_t conf_mtu;
65a87968
PC
784
785 /* As of DPDK 17.11.1 a few PMDs require to explicitly enable
786 * scatter to support jumbo RX. Checking the offload capabilities
787 * is not an option as PMDs are not required yet to report
788 * them. The only reliable info is the driver name and knowledge
789 * (testing or code review). Listing all such PMDs feels harder
790 * than highlighting the one known not to need scatter */
0072e931 791 if (dev->mtu > ETHER_MTU) {
65a87968 792 rte_eth_dev_info_get(dev->port_id, &info);
7bbc2e1d 793 if (strncmp(info.driver_name, "net_nfp", 7)) {
65a87968
PC
794 conf.rxmode.enable_scatter = 1;
795 }
0072e931 796 }
67fe6d63 797
f8b64a61 798 conf.intr_conf.lsc = dev->lsc_interrupt_mode;
1a2bb118
SC
799 conf.rxmode.hw_ip_checksum = (dev->hw_ol_features &
800 NETDEV_RX_CHECKSUM_OFFLOAD) != 0;
e10ca8b9
MW
801
802 if (dev->hw_ol_features & NETDEV_RX_HW_CRC_STRIP) {
803 conf.rxmode.hw_strip_crc = 1;
804 }
805
b98d7669
DDP
806 /* A device may report more queues than it makes available (this has
807 * been observed for Intel xl710, which reserves some of them for
808 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
809 * available. When this happens we can retry the configuration
810 * and request less queues */
811 while (n_rxq && n_txq) {
812 if (diag) {
813 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq, n_txq);
814 }
815
0072e931 816 diag = rte_eth_dev_configure(dev->port_id, n_rxq, n_txq, &conf);
b98d7669 817 if (diag) {
0072e931
MK
818 VLOG_WARN("Interface %s eth_dev setup error %s\n",
819 dev->up.name, rte_strerror(-diag));
b98d7669
DDP
820 break;
821 }
822
67fe6d63
MK
823 diag = rte_eth_dev_set_mtu(dev->port_id, dev->mtu);
824 if (diag) {
4dd16ca0
IS
825 /* A device may not support rte_eth_dev_set_mtu, in this case
826 * flag a warning to the user and include the devices configured
827 * MTU value that will be used instead. */
828 if (-ENOTSUP == diag) {
829 rte_eth_dev_get_mtu(dev->port_id, &conf_mtu);
830 VLOG_WARN("Interface %s does not support MTU configuration, "
831 "max packet size supported is %"PRIu16".",
832 dev->up.name, conf_mtu);
833 } else {
834 VLOG_ERR("Interface %s MTU (%d) setup error: %s",
835 dev->up.name, dev->mtu, rte_strerror(-diag));
836 break;
837 }
67fe6d63
MK
838 }
839
b98d7669 840 for (i = 0; i < n_txq; i++) {
b685696b 841 diag = rte_eth_tx_queue_setup(dev->port_id, i, dev->txq_size,
b98d7669
DDP
842 dev->socket_id, NULL);
843 if (diag) {
1dfebee9 844 VLOG_INFO("Interface %s unable to setup txq(%d): %s",
b98d7669
DDP
845 dev->up.name, i, rte_strerror(-diag));
846 break;
847 }
848 }
849
850 if (i != n_txq) {
851 /* Retry with less tx queues */
852 n_txq = i;
853 continue;
854 }
855
856 for (i = 0; i < n_rxq; i++) {
b685696b 857 diag = rte_eth_rx_queue_setup(dev->port_id, i, dev->rxq_size,
24e78f93 858 dev->socket_id, NULL, dev->mp);
b98d7669 859 if (diag) {
1dfebee9 860 VLOG_INFO("Interface %s unable to setup rxq(%d): %s",
b98d7669
DDP
861 dev->up.name, i, rte_strerror(-diag));
862 break;
863 }
864 }
865
866 if (i != n_rxq) {
867 /* Retry with less rx queues */
868 n_rxq = i;
869 continue;
870 }
871
872 dev->up.n_rxq = n_rxq;
81acebda 873 dev->up.n_txq = n_txq;
b98d7669
DDP
874
875 return 0;
876 }
877
878 return diag;
879}
880
9fd39370
SC
881static void
882dpdk_eth_flow_ctrl_setup(struct netdev_dpdk *dev) OVS_REQUIRES(dev->mutex)
883{
884 if (rte_eth_dev_flow_ctrl_set(dev->port_id, &dev->fc_conf)) {
fa9f4eeb 885 VLOG_WARN("Failed to enable flow control on device "DPDK_PORT_ID_FMT,
bb37956a 886 dev->port_id);
9fd39370
SC
887 }
888}
b98d7669 889
8a9562d2 890static int
c2adb102
IM
891dpdk_eth_dev_init(struct netdev_dpdk *dev)
892 OVS_REQUIRES(dev->mutex)
8a9562d2
PS
893{
894 struct rte_pktmbuf_pool_private *mbp_priv;
a0cb2d66 895 struct rte_eth_dev_info info;
8a9562d2
PS
896 struct ether_addr eth_addr;
897 int diag;
b98d7669 898 int n_rxq, n_txq;
d4f5282c
KT
899 uint32_t rx_chksm_offload_capa = DEV_RX_OFFLOAD_UDP_CKSUM |
900 DEV_RX_OFFLOAD_TCP_CKSUM |
901 DEV_RX_OFFLOAD_IPV4_CKSUM;
8a9562d2 902
a0cb2d66 903 rte_eth_dev_info_get(dev->port_id, &info);
a0cb2d66 904
e10ca8b9
MW
905 if (strstr(info.driver_name, "vf") != NULL) {
906 VLOG_INFO("Virtual function detected, HW_CRC_STRIP will be enabled");
907 dev->hw_ol_features |= NETDEV_RX_HW_CRC_STRIP;
908 } else {
909 dev->hw_ol_features &= ~NETDEV_RX_HW_CRC_STRIP;
910 }
911
d4f5282c
KT
912 if ((info.rx_offload_capa & rx_chksm_offload_capa) !=
913 rx_chksm_offload_capa) {
fa9f4eeb
IM
914 VLOG_WARN("Rx checksum offload is not supported on port "
915 DPDK_PORT_ID_FMT, dev->port_id);
d4f5282c
KT
916 dev->hw_ol_features &= ~NETDEV_RX_CHECKSUM_OFFLOAD;
917 } else {
918 dev->hw_ol_features |= NETDEV_RX_CHECKSUM_OFFLOAD;
919 }
920
b98d7669
DDP
921 n_rxq = MIN(info.max_rx_queues, dev->up.n_rxq);
922 n_txq = MIN(info.max_tx_queues, dev->up.n_txq);
923
f8b64a61 924 diag = dpdk_eth_dev_port_config(dev, n_rxq, n_txq);
8a9562d2 925 if (diag) {
f8b64a61
RM
926 VLOG_ERR("Interface %s(rxq:%d txq:%d lsc interrupt mode:%s) "
927 "configure error: %s",
928 dev->up.name, n_rxq, n_txq,
929 dev->lsc_interrupt_mode ? "true" : "false",
930 rte_strerror(-diag));
95fb793a 931 return -diag;
8a9562d2
PS
932 }
933
8a9562d2
PS
934 diag = rte_eth_dev_start(dev->port_id);
935 if (diag) {
b98d7669
DDP
936 VLOG_ERR("Interface %s start error: %s", dev->up.name,
937 rte_strerror(-diag));
95fb793a 938 return -diag;
8a9562d2 939 }
606f6650 940 dev->started = true;
8a9562d2
PS
941
942 rte_eth_promiscuous_enable(dev->port_id);
943 rte_eth_allmulticast_enable(dev->port_id);
944
945 memset(&eth_addr, 0x0, sizeof(eth_addr));
946 rte_eth_macaddr_get(dev->port_id, &eth_addr);
fa9f4eeb
IM
947 VLOG_INFO_RL(&rl, "Port "DPDK_PORT_ID_FMT": "ETH_ADDR_FMT,
948 dev->port_id, ETH_ADDR_BYTES_ARGS(eth_addr.addr_bytes));
8a9562d2 949
ca92d173 950 memcpy(dev->hwaddr.ea, eth_addr.addr_bytes, ETH_ADDR_LEN);
8a9562d2
PS
951 rte_eth_link_get_nowait(dev->port_id, &dev->link);
952
24e78f93 953 mbp_priv = rte_mempool_get_priv(dev->mp);
8a9562d2
PS
954 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
955
9fd39370
SC
956 /* Get the Flow control configuration for DPDK-ETH */
957 diag = rte_eth_dev_flow_ctrl_get(dev->port_id, &dev->fc_conf);
958 if (diag) {
fa9f4eeb
IM
959 VLOG_DBG("cannot get flow control parameters on port "DPDK_PORT_ID_FMT
960 ", err=%d", dev->port_id, diag);
9fd39370
SC
961 }
962
8a9562d2
PS
963 return 0;
964}
965
966static struct netdev_dpdk *
967netdev_dpdk_cast(const struct netdev *netdev)
968{
969 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
970}
971
972static struct netdev *
973netdev_dpdk_alloc(void)
974{
bab69409
AC
975 struct netdev_dpdk *dev;
976
65e19e70
DDP
977 dev = dpdk_rte_mzalloc(sizeof *dev);
978 if (dev) {
979 return &dev->up;
bab69409 980 }
65e19e70 981
bab69409 982 return NULL;
8a9562d2
PS
983}
984
eff23640
DDP
985static struct dpdk_tx_queue *
986netdev_dpdk_alloc_txq(unsigned int n_txqs)
5a034064 987{
eff23640 988 struct dpdk_tx_queue *txqs;
bd5131ba 989 unsigned i;
5a034064 990
eff23640
DDP
991 txqs = dpdk_rte_mzalloc(n_txqs * sizeof *txqs);
992 if (txqs) {
993 for (i = 0; i < n_txqs; i++) {
994 /* Initialize map for vhost devices. */
995 txqs[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
996 rte_spinlock_init(&txqs[i].tx_lock);
997 }
5a034064 998 }
eff23640
DDP
999
1000 return txqs;
5a034064
AW
1001}
1002
8a9562d2 1003static int
bb37956a 1004common_construct(struct netdev *netdev, dpdk_port_t port_no,
1ce30dfd 1005 enum dpdk_dev_type type, int socket_id)
5a034064 1006 OVS_REQUIRES(dpdk_mutex)
8a9562d2 1007{
d46285a2 1008 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 1009
d46285a2 1010 ovs_mutex_init(&dev->mutex);
8a9562d2 1011
d46285a2 1012 rte_spinlock_init(&dev->stats_lock);
45d947c4 1013
1b7a04e0
AW
1014 /* If the 'sid' is negative, it means that the kernel fails
1015 * to obtain the pci numa info. In that situation, always
1016 * use 'SOCKET0'. */
1ce30dfd 1017 dev->socket_id = socket_id < 0 ? SOCKET0 : socket_id;
db8f13b0 1018 dev->requested_socket_id = dev->socket_id;
d46285a2
DDP
1019 dev->port_id = port_no;
1020 dev->type = type;
1021 dev->flags = 0;
7f381c2e 1022 dev->requested_mtu = ETHER_MTU;
d46285a2 1023 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
f8b64a61 1024 dev->requested_lsc_interrupt_mode = 0;
0a0f39df
CL
1025 ovsrcu_index_init(&dev->vid, -1);
1026 dev->vhost_reconfigured = false;
5dcde09c 1027 dev->attached = false;
8a9562d2 1028
78bd47cf 1029 ovsrcu_init(&dev->qos_conf, NULL);
0bf765f7 1030
9509913a
IS
1031 ovsrcu_init(&dev->ingress_policer, NULL);
1032 dev->policer_rate = 0;
1033 dev->policer_burst = 0;
1034
7f381c2e
DDP
1035 netdev->n_rxq = 0;
1036 netdev->n_txq = 0;
1037 dev->requested_n_rxq = NR_QUEUE;
1038 dev->requested_n_txq = NR_QUEUE;
1039 dev->requested_rxq_size = NIC_PORT_DEFAULT_RXQ_SIZE;
1040 dev->requested_txq_size = NIC_PORT_DEFAULT_TXQ_SIZE;
58397e6c 1041
9fd39370
SC
1042 /* Initialize the flow control to NULL */
1043 memset(&dev->fc_conf, 0, sizeof dev->fc_conf);
1a2bb118
SC
1044
1045 /* Initilize the hardware offload flags to 0 */
1046 dev->hw_ol_features = 0;
3b1fb077
DDP
1047
1048 dev->flags = NETDEV_UP | NETDEV_PROMISC;
1049
d46285a2 1050 ovs_list_push_back(&dpdk_list, &dev->list_node);
8a9562d2 1051
7f381c2e
DDP
1052 netdev_request_reconfigure(netdev);
1053
971f4b39
MW
1054 dev->rte_xstats_names = NULL;
1055 dev->rte_xstats_names_size = 0;
1056
1057 dev->rte_xstats_ids = NULL;
1058 dev->rte_xstats_ids_size = 0;
1059
1ce30dfd 1060 return 0;
95fb793a 1061}
1062
b83a2df1
MV
1063/* dev_name must be the prefix followed by a positive decimal number.
1064 * (no leading + or - signs are allowed) */
95fb793a 1065static int
1066dpdk_dev_parse_name(const char dev_name[], const char prefix[],
1067 unsigned int *port_no)
1068{
1069 const char *cport;
1070
1071 if (strncmp(dev_name, prefix, strlen(prefix))) {
1072 return ENODEV;
1073 }
1074
1075 cport = dev_name + strlen(prefix);
b83a2df1
MV
1076
1077 if (str_to_uint(cport, 10, port_no)) {
1078 return 0;
1079 } else {
1080 return ENODEV;
1081 }
95fb793a 1082}
1083
1ce30dfd
DDP
1084static int
1085vhost_common_construct(struct netdev *netdev)
1086 OVS_REQUIRES(dpdk_mutex)
1087{
1088 int socket_id = rte_lcore_to_socket_id(rte_get_master_lcore());
1089 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1090
1091 dev->tx_q = netdev_dpdk_alloc_txq(OVS_VHOST_MAX_QUEUE_NUM);
1092 if (!dev->tx_q) {
1093 return ENOMEM;
1094 }
1095
bb37956a
IM
1096 return common_construct(netdev, DPDK_ETH_PORT_ID_INVALID,
1097 DPDK_DEV_VHOST, socket_id);
1ce30dfd
DDP
1098}
1099
7d1ced01 1100static int
53f50d24 1101netdev_dpdk_vhost_construct(struct netdev *netdev)
7d1ced01 1102{
d46285a2
DDP
1103 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1104 const char *name = netdev->name;
7d1ced01 1105 int err;
a0cb2d66 1106
1af27e8a
DDP
1107 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
1108 * the file system. '/' or '\' would traverse directories, so they're not
1109 * acceptable in 'name'. */
1110 if (strchr(name, '/') || strchr(name, '\\')) {
1111 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
1112 "A valid name must not include '/' or '\\'",
1113 name);
1114 return EINVAL;
1115 }
1116
7d1ced01
CL
1117 ovs_mutex_lock(&dpdk_mutex);
1118 /* Take the name of the vhost-user port and append it to the location where
2d24d165 1119 * the socket is to be created, then register the socket.
7d1ced01 1120 */
2d24d165 1121 snprintf(dev->vhost_id, sizeof dev->vhost_id, "%s/%s",
01961bbd 1122 dpdk_get_vhost_sock_dir(), name);
1af27e8a 1123
2d24d165
CL
1124 dev->vhost_driver_flags &= ~RTE_VHOST_USER_CLIENT;
1125 err = rte_vhost_driver_register(dev->vhost_id, dev->vhost_driver_flags);
7d1ced01
CL
1126 if (err) {
1127 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
2d24d165 1128 dev->vhost_id);
f3e7ec25 1129 goto out;
e5c0f5a4 1130 } else {
2d24d165
CL
1131 fatal_signal_add_file_to_unlink(dev->vhost_id);
1132 VLOG_INFO("Socket %s created for vhost-user port %s\n",
1133 dev->vhost_id, name);
1134 }
f3e7ec25
MW
1135
1136 err = rte_vhost_driver_callback_register(dev->vhost_id,
1137 &virtio_net_device_ops);
1138 if (err) {
1139 VLOG_ERR("rte_vhost_driver_callback_register failed for vhost user "
1140 "port: %s\n", name);
1141 goto out;
1142 }
1143
1144 err = rte_vhost_driver_disable_features(dev->vhost_id,
1145 1ULL << VIRTIO_NET_F_HOST_TSO4
1146 | 1ULL << VIRTIO_NET_F_HOST_TSO6
1147 | 1ULL << VIRTIO_NET_F_CSUM);
1148 if (err) {
1149 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
1150 "port: %s\n", name);
1151 goto out;
1152 }
1153
1154 err = rte_vhost_driver_start(dev->vhost_id);
1155 if (err) {
1156 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
1157 "port: %s\n", name);
1158 goto out;
1159 }
1160
1ce30dfd 1161 err = vhost_common_construct(netdev);
f3e7ec25
MW
1162 if (err) {
1163 VLOG_ERR("vhost_common_construct failed for vhost user "
1164 "port: %s\n", name);
1165 }
2d24d165 1166
f3e7ec25 1167out:
2d24d165 1168 ovs_mutex_unlock(&dpdk_mutex);
28ca969e
AC
1169 VLOG_WARN_ONCE("dpdkvhostuser ports are considered deprecated; "
1170 "please migrate to dpdkvhostuserclient ports.");
2d24d165
CL
1171 return err;
1172}
1173
1174static int
1175netdev_dpdk_vhost_client_construct(struct netdev *netdev)
1176{
1177 int err;
1178
2d24d165 1179 ovs_mutex_lock(&dpdk_mutex);
1ce30dfd 1180 err = vhost_common_construct(netdev);
f3e7ec25
MW
1181 if (err) {
1182 VLOG_ERR("vhost_common_construct failed for vhost user client"
1183 "port: %s\n", netdev->name);
1184 }
7d1ced01 1185 ovs_mutex_unlock(&dpdk_mutex);
58397e6c
KT
1186 return err;
1187}
1188
95fb793a 1189static int
1190netdev_dpdk_construct(struct netdev *netdev)
1191{
95fb793a 1192 int err;
1193
95fb793a 1194 ovs_mutex_lock(&dpdk_mutex);
bb37956a
IM
1195 err = common_construct(netdev, DPDK_ETH_PORT_ID_INVALID,
1196 DPDK_DEV_ETH, SOCKET0);
8a9562d2
PS
1197 ovs_mutex_unlock(&dpdk_mutex);
1198 return err;
1199}
1200
1ce30dfd
DDP
1201static void
1202common_destruct(struct netdev_dpdk *dev)
1203 OVS_REQUIRES(dpdk_mutex)
1204 OVS_EXCLUDED(dev->mutex)
1205{
1206 rte_free(dev->tx_q);
91fccdad 1207 dpdk_mp_release(dev->mp);
1ce30dfd
DDP
1208
1209 ovs_list_remove(&dev->list_node);
1210 free(ovsrcu_get_protected(struct ingress_policer *,
1211 &dev->ingress_policer));
1212 ovs_mutex_destroy(&dev->mutex);
1213}
1214
8a9562d2 1215static void
d46285a2 1216netdev_dpdk_destruct(struct netdev *netdev)
8a9562d2 1217{
d46285a2 1218 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
5dcde09c 1219 char devname[RTE_ETH_NAME_MAX_LEN];
8a9562d2 1220
8d38823b 1221 ovs_mutex_lock(&dpdk_mutex);
8d38823b 1222
8a9562d2 1223 rte_eth_dev_stop(dev->port_id);
606f6650 1224 dev->started = false;
5dcde09c
IM
1225
1226 if (dev->attached) {
1227 rte_eth_dev_close(dev->port_id);
1228 if (rte_eth_dev_detach(dev->port_id, devname) < 0) {
1229 VLOG_ERR("Device '%s' can not be detached", dev->devargs);
1230 } else {
0ee821c2 1231 VLOG_INFO("Device '%s' has been detached", devname);
5dcde09c
IM
1232 }
1233 }
1234
ac1a9bb9 1235 netdev_dpdk_clear_xstats(dev);
55e075e6 1236 free(dev->devargs);
1ce30dfd 1237 common_destruct(dev);
8d38823b 1238
8a9562d2 1239 ovs_mutex_unlock(&dpdk_mutex);
58397e6c 1240}
8a9562d2 1241
3f891bbe
DDP
1242/* rte_vhost_driver_unregister() can call back destroy_device(), which will
1243 * try to acquire 'dpdk_mutex' and possibly 'dev->mutex'. To avoid a
1244 * deadlock, none of the mutexes must be held while calling this function. */
1245static int
c1ff66ac
CL
1246dpdk_vhost_driver_unregister(struct netdev_dpdk *dev OVS_UNUSED,
1247 char *vhost_id)
3f891bbe
DDP
1248 OVS_EXCLUDED(dpdk_mutex)
1249 OVS_EXCLUDED(dev->mutex)
1250{
c1ff66ac 1251 return rte_vhost_driver_unregister(vhost_id);
3f891bbe
DDP
1252}
1253
58397e6c 1254static void
d46285a2 1255netdev_dpdk_vhost_destruct(struct netdev *netdev)
58397e6c 1256{
d46285a2 1257 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
c1ff66ac 1258 char *vhost_id;
58397e6c 1259
8d38823b 1260 ovs_mutex_lock(&dpdk_mutex);
8d38823b 1261
c62da695 1262 /* Guest becomes an orphan if still attached. */
c1ff66ac
CL
1263 if (netdev_dpdk_get_vid(dev) >= 0
1264 && !(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) {
c62da695 1265 VLOG_ERR("Removing port '%s' while vhost device still attached.",
d46285a2 1266 netdev->name);
58be5c0e
MK
1267 VLOG_ERR("To restore connectivity after re-adding of port, VM on "
1268 "socket '%s' must be restarted.", dev->vhost_id);
58397e6c
KT
1269 }
1270
2d24d165 1271 vhost_id = xstrdup(dev->vhost_id);
c1ff66ac 1272
1ce30dfd
DDP
1273 common_destruct(dev);
1274
58397e6c 1275 ovs_mutex_unlock(&dpdk_mutex);
3f891bbe 1276
569c26da 1277 if (!vhost_id[0]) {
821b8664
IM
1278 goto out;
1279 }
1280
c1ff66ac 1281 if (dpdk_vhost_driver_unregister(dev, vhost_id)) {
41964543
IM
1282 VLOG_ERR("%s: Unable to unregister vhost driver for socket '%s'.\n",
1283 netdev->name, vhost_id);
c1ff66ac
CL
1284 } else if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) {
1285 /* OVS server mode - remove this socket from list for deletion */
1286 fatal_signal_remove_file_to_unlink(vhost_id);
3f891bbe 1287 }
821b8664 1288out:
c1ff66ac 1289 free(vhost_id);
8a9562d2
PS
1290}
1291
1292static void
d46285a2 1293netdev_dpdk_dealloc(struct netdev *netdev)
8a9562d2 1294{
d46285a2 1295 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 1296
d46285a2 1297 rte_free(dev);
8a9562d2
PS
1298}
1299
971f4b39 1300static void
ac1a9bb9 1301netdev_dpdk_clear_xstats(struct netdev_dpdk *dev)
971f4b39
MW
1302{
1303 /* If statistics are already allocated, we have to
1304 * reconfigure, as port_id could have been changed. */
1305 if (dev->rte_xstats_names) {
1306 free(dev->rte_xstats_names);
1307 dev->rte_xstats_names = NULL;
1308 dev->rte_xstats_names_size = 0;
1309 }
1310 if (dev->rte_xstats_ids) {
1311 free(dev->rte_xstats_ids);
1312 dev->rte_xstats_ids = NULL;
1313 dev->rte_xstats_ids_size = 0;
1314 }
1315}
1316
1317static const char*
1318netdev_dpdk_get_xstat_name(struct netdev_dpdk *dev, uint64_t id)
1319{
1320 if (id >= dev->rte_xstats_names_size) {
1321 return "UNKNOWN";
1322 }
1323 return dev->rte_xstats_names[id].name;
1324}
1325
1326static bool
1327netdev_dpdk_configure_xstats(struct netdev_dpdk *dev)
1328 OVS_REQUIRES(dev->mutex)
1329{
1330 int rte_xstats_len;
1331 bool ret;
1332 struct rte_eth_xstat *rte_xstats;
1333 uint64_t id;
1334 int xstats_no;
1335 const char *name;
1336
1337 /* Retrieving all XSTATS names. If something will go wrong
1338 * or amount of counters will be equal 0, rte_xstats_names
1339 * buffer will be marked as NULL, and any further xstats
1340 * query won't be performed (e.g. during netdev_dpdk_get_stats
1341 * execution). */
1342
1343 ret = false;
1344 rte_xstats = NULL;
1345
1346 if (dev->rte_xstats_names == NULL || dev->rte_xstats_ids == NULL) {
1347 dev->rte_xstats_names_size =
1348 rte_eth_xstats_get_names(dev->port_id, NULL, 0);
1349
1350 if (dev->rte_xstats_names_size < 0) {
fa9f4eeb
IM
1351 VLOG_WARN("Cannot get XSTATS for port: "DPDK_PORT_ID_FMT,
1352 dev->port_id);
971f4b39
MW
1353 dev->rte_xstats_names_size = 0;
1354 } else {
1355 /* Reserve memory for xstats names and values */
1356 dev->rte_xstats_names = xcalloc(dev->rte_xstats_names_size,
1357 sizeof *dev->rte_xstats_names);
1358
1359 if (dev->rte_xstats_names) {
1360 /* Retreive xstats names */
1361 rte_xstats_len =
1362 rte_eth_xstats_get_names(dev->port_id,
1363 dev->rte_xstats_names,
1364 dev->rte_xstats_names_size);
1365
1366 if (rte_xstats_len < 0) {
fa9f4eeb
IM
1367 VLOG_WARN("Cannot get XSTATS names for port: "
1368 DPDK_PORT_ID_FMT, dev->port_id);
971f4b39
MW
1369 goto out;
1370 } else if (rte_xstats_len != dev->rte_xstats_names_size) {
fa9f4eeb
IM
1371 VLOG_WARN("XSTATS size doesn't match for port: "
1372 DPDK_PORT_ID_FMT, dev->port_id);
971f4b39
MW
1373 goto out;
1374 }
1375
1376 dev->rte_xstats_ids = xcalloc(dev->rte_xstats_names_size,
1377 sizeof(uint64_t));
1378
1379 /* We have to calculate number of counters */
1380 rte_xstats = xmalloc(rte_xstats_len * sizeof *rte_xstats);
1381 memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len);
1382
1383 /* Retreive xstats values */
1384 if (rte_eth_xstats_get(dev->port_id, rte_xstats,
1385 rte_xstats_len) > 0) {
1386 dev->rte_xstats_ids_size = 0;
1387 xstats_no = 0;
1388 for (uint32_t i = 0; i < rte_xstats_len; i++) {
1389 id = rte_xstats[i].id;
1390 name = netdev_dpdk_get_xstat_name(dev, id);
1391 /* We need to filter out everything except
1392 * dropped, error and management counters */
1393 if (string_ends_with(name, "_errors") ||
1394 strstr(name, "_management_") ||
1395 string_ends_with(name, "_dropped")) {
1396
1397 dev->rte_xstats_ids[xstats_no] = id;
1398 xstats_no++;
1399 }
1400 }
1401 dev->rte_xstats_ids_size = xstats_no;
1402 ret = true;
1403 } else {
fa9f4eeb
IM
1404 VLOG_WARN("Can't get XSTATS IDs for port: "
1405 DPDK_PORT_ID_FMT, dev->port_id);
971f4b39 1406 }
34eb0863
IM
1407
1408 free(rte_xstats);
971f4b39
MW
1409 }
1410 }
1411 } else {
1412 /* Already configured */
1413 ret = true;
1414 }
1415
1416out:
1417 if (!ret) {
1418 netdev_dpdk_clear_xstats(dev);
1419 }
1420 return ret;
1421}
1422
8a9562d2 1423static int
a14b8947 1424netdev_dpdk_get_config(const struct netdev *netdev, struct smap *args)
8a9562d2 1425{
a14b8947 1426 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
1427
1428 ovs_mutex_lock(&dev->mutex);
1429
050c60bf 1430 smap_add_format(args, "requested_rx_queues", "%d", dev->requested_n_rxq);
a14b8947 1431 smap_add_format(args, "configured_rx_queues", "%d", netdev->n_rxq);
81acebda
IM
1432 smap_add_format(args, "requested_tx_queues", "%d", dev->requested_n_txq);
1433 smap_add_format(args, "configured_tx_queues", "%d", netdev->n_txq);
0072e931 1434 smap_add_format(args, "mtu", "%d", dev->mtu);
451f26fd
IM
1435
1436 if (dev->type == DPDK_DEV_ETH) {
1437 smap_add_format(args, "requested_rxq_descriptors", "%d",
1438 dev->requested_rxq_size);
1439 smap_add_format(args, "configured_rxq_descriptors", "%d",
1440 dev->rxq_size);
1441 smap_add_format(args, "requested_txq_descriptors", "%d",
1442 dev->requested_txq_size);
1443 smap_add_format(args, "configured_txq_descriptors", "%d",
1444 dev->txq_size);
1a2bb118
SC
1445 if (dev->hw_ol_features & NETDEV_RX_CHECKSUM_OFFLOAD) {
1446 smap_add(args, "rx_csum_offload", "true");
8155ab7e
KT
1447 } else {
1448 smap_add(args, "rx_csum_offload", "false");
1a2bb118 1449 }
f8b64a61
RM
1450 smap_add(args, "lsc_interrupt_mode",
1451 dev->lsc_interrupt_mode ? "true" : "false");
451f26fd 1452 }
8a9562d2
PS
1453 ovs_mutex_unlock(&dev->mutex);
1454
1455 return 0;
1456}
1457
55e075e6 1458static struct netdev_dpdk *
bb37956a 1459netdev_dpdk_lookup_by_port_id(dpdk_port_t port_id)
55e075e6
CL
1460 OVS_REQUIRES(dpdk_mutex)
1461{
1462 struct netdev_dpdk *dev;
1463
1464 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
1465 if (dev->port_id == port_id) {
1466 return dev;
1467 }
1468 }
1469
1470 return NULL;
1471}
1472
5e758818
YL
1473static dpdk_port_t
1474netdev_dpdk_get_port_by_mac(const char *mac_str)
1475{
1476 dpdk_port_t port_id;
1477 struct eth_addr mac, port_mac;
1478
1479 if (!eth_addr_from_string(mac_str, &mac)) {
1480 VLOG_ERR("invalid mac: %s", mac_str);
1481 return DPDK_ETH_PORT_ID_INVALID;
1482 }
1483
1484 RTE_ETH_FOREACH_DEV (port_id) {
1485 struct ether_addr ea;
1486
1487 rte_eth_macaddr_get(port_id, &ea);
1488 memcpy(port_mac.ea, ea.addr_bytes, ETH_ADDR_LEN);
1489 if (eth_addr_equals(mac, port_mac)) {
1490 return port_id;
1491 }
1492 }
1493
1494 return DPDK_ETH_PORT_ID_INVALID;
1495}
1496
1497/*
1498 * Normally, a PCI id is enough for identifying a specific DPDK port.
1499 * However, for some NICs having multiple ports sharing the same PCI
1500 * id, using PCI id won't work then.
1501 *
1502 * To fix that, here one more method is introduced: "class=eth,mac=$MAC".
1503 *
1504 * Note that the compatibility is fully kept: user can still use the
1505 * PCI id for adding ports (when it's enough for them).
1506 */
bb37956a 1507static dpdk_port_t
5dcde09c
IM
1508netdev_dpdk_process_devargs(struct netdev_dpdk *dev,
1509 const char *devargs, char **errp)
55e075e6 1510{
5e758818 1511 char *name;
bb37956a 1512 dpdk_port_t new_port_id = DPDK_ETH_PORT_ID_INVALID;
55e075e6 1513
5e758818
YL
1514 if (strncmp(devargs, "class=eth,mac=", 14) == 0) {
1515 new_port_id = netdev_dpdk_get_port_by_mac(&devargs[14]);
1516 } else {
1517 name = xmemdup0(devargs, strcspn(devargs, ","));
1518 if (rte_eth_dev_get_port_by_name(name, &new_port_id)
1519 || !rte_eth_dev_is_valid_port(new_port_id)) {
1520 /* Device not found in DPDK, attempt to attach it */
1521 if (!rte_eth_dev_attach(devargs, &new_port_id)) {
1522 /* Attach successful */
1523 dev->attached = true;
1524 VLOG_INFO("Device '%s' attached to DPDK", devargs);
1525 } else {
1526 /* Attach unsuccessful */
1527 new_port_id = DPDK_ETH_PORT_ID_INVALID;
1528 }
55e075e6 1529 }
5e758818
YL
1530 free(name);
1531 }
1532
1533 if (new_port_id == DPDK_ETH_PORT_ID_INVALID) {
1534 VLOG_WARN_BUF(errp, "Error attaching device '%s' to DPDK", devargs);
55e075e6
CL
1535 }
1536
1537 return new_port_id;
1538}
1539
c3d062a7
CL
1540static void
1541dpdk_set_rxq_config(struct netdev_dpdk *dev, const struct smap *args)
b614c894 1542 OVS_REQUIRES(dev->mutex)
a14b8947 1543{
050c60bf 1544 int new_n_rxq;
a14b8947 1545
2a21e757 1546 new_n_rxq = MAX(smap_get_int(args, "n_rxq", NR_QUEUE), 1);
050c60bf
DDP
1547 if (new_n_rxq != dev->requested_n_rxq) {
1548 dev->requested_n_rxq = new_n_rxq;
c3d062a7 1549 netdev_request_reconfigure(&dev->up);
050c60bf 1550 }
c3d062a7
CL
1551}
1552
b685696b
CL
1553static void
1554dpdk_process_queue_size(struct netdev *netdev, const struct smap *args,
1555 const char *flag, int default_size, int *new_size)
1556{
1557 int queue_size = smap_get_int(args, flag, default_size);
1558
1559 if (queue_size <= 0 || queue_size > NIC_PORT_MAX_Q_SIZE
1560 || !is_pow2(queue_size)) {
1561 queue_size = default_size;
1562 }
1563
1564 if (queue_size != *new_size) {
1565 *new_size = queue_size;
1566 netdev_request_reconfigure(netdev);
1567 }
1568}
1569
c3d062a7 1570static int
9fff138e
DDP
1571netdev_dpdk_set_config(struct netdev *netdev, const struct smap *args,
1572 char **errp)
c3d062a7
CL
1573{
1574 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
f8b64a61 1575 bool rx_fc_en, tx_fc_en, autoneg, lsc_interrupt_mode;
b614c894
IM
1576 enum rte_eth_fc_mode fc_mode;
1577 static const enum rte_eth_fc_mode fc_mode_set[2][2] = {
1578 {RTE_FC_NONE, RTE_FC_TX_PAUSE},
1579 {RTE_FC_RX_PAUSE, RTE_FC_FULL }
1580 };
55e075e6
CL
1581 const char *new_devargs;
1582 int err = 0;
c3d062a7 1583
55e075e6 1584 ovs_mutex_lock(&dpdk_mutex);
c3d062a7
CL
1585 ovs_mutex_lock(&dev->mutex);
1586
1587 dpdk_set_rxq_config(dev, args);
1588
b685696b
CL
1589 dpdk_process_queue_size(netdev, args, "n_rxq_desc",
1590 NIC_PORT_DEFAULT_RXQ_SIZE,
1591 &dev->requested_rxq_size);
1592 dpdk_process_queue_size(netdev, args, "n_txq_desc",
1593 NIC_PORT_DEFAULT_TXQ_SIZE,
1594 &dev->requested_txq_size);
1595
55e075e6
CL
1596 new_devargs = smap_get(args, "dpdk-devargs");
1597
1598 if (dev->devargs && strcmp(new_devargs, dev->devargs)) {
1599 /* The user requested a new device. If we return error, the caller
1600 * will delete this netdev and try to recreate it. */
1601 err = EAGAIN;
1602 goto out;
1603 }
1604
1605 /* dpdk-devargs is required for device configuration */
1606 if (new_devargs && new_devargs[0]) {
1607 /* Don't process dpdk-devargs if value is unchanged and port id
1608 * is valid */
1609 if (!(dev->devargs && !strcmp(dev->devargs, new_devargs)
1610 && rte_eth_dev_is_valid_port(dev->port_id))) {
bb37956a
IM
1611 dpdk_port_t new_port_id = netdev_dpdk_process_devargs(dev,
1612 new_devargs,
1613 errp);
55e075e6
CL
1614 if (!rte_eth_dev_is_valid_port(new_port_id)) {
1615 err = EINVAL;
1616 } else if (new_port_id == dev->port_id) {
1617 /* Already configured, do not reconfigure again */
1618 err = 0;
1619 } else {
1620 struct netdev_dpdk *dup_dev;
bb37956a 1621
55e075e6
CL
1622 dup_dev = netdev_dpdk_lookup_by_port_id(new_port_id);
1623 if (dup_dev) {
9fff138e
DDP
1624 VLOG_WARN_BUF(errp, "'%s' is trying to use device '%s' "
1625 "which is already in use by '%s'",
1626 netdev_get_name(netdev), new_devargs,
1627 netdev_get_name(&dup_dev->up));
55e075e6
CL
1628 err = EADDRINUSE;
1629 } else {
bd4e172b 1630 int sid = rte_eth_dev_socket_id(new_port_id);
bb37956a 1631
bd4e172b 1632 dev->requested_socket_id = sid < 0 ? SOCKET0 : sid;
55e075e6
CL
1633 dev->devargs = xstrdup(new_devargs);
1634 dev->port_id = new_port_id;
1635 netdev_request_reconfigure(&dev->up);
971f4b39 1636 netdev_dpdk_clear_xstats(dev);
55e075e6
CL
1637 err = 0;
1638 }
1639 }
1640 }
1641 } else {
9fff138e
DDP
1642 VLOG_WARN_BUF(errp, "'%s' is missing 'options:dpdk-devargs'. "
1643 "The old 'dpdk<port_id>' names are not supported",
1644 netdev_get_name(netdev));
55e075e6
CL
1645 err = EINVAL;
1646 }
1647
1648 if (err) {
1649 goto out;
1650 }
1651
f8b64a61
RM
1652 lsc_interrupt_mode = smap_get_bool(args, "dpdk-lsc-interrupt", false);
1653 if (dev->requested_lsc_interrupt_mode != lsc_interrupt_mode) {
1654 dev->requested_lsc_interrupt_mode = lsc_interrupt_mode;
1655 netdev_request_reconfigure(netdev);
1656 }
1657
c3d062a7
CL
1658 rx_fc_en = smap_get_bool(args, "rx-flow-ctrl", false);
1659 tx_fc_en = smap_get_bool(args, "tx-flow-ctrl", false);
b614c894 1660 autoneg = smap_get_bool(args, "flow-ctrl-autoneg", false);
c3d062a7 1661
b614c894
IM
1662 fc_mode = fc_mode_set[tx_fc_en][rx_fc_en];
1663 if (dev->fc_conf.mode != fc_mode || autoneg != dev->fc_conf.autoneg) {
1664 dev->fc_conf.mode = fc_mode;
1665 dev->fc_conf.autoneg = autoneg;
1666 dpdk_eth_flow_ctrl_setup(dev);
1667 }
9fd39370 1668
55e075e6 1669out:
c3d062a7 1670 ovs_mutex_unlock(&dev->mutex);
55e075e6 1671 ovs_mutex_unlock(&dpdk_mutex);
c3d062a7 1672
55e075e6 1673 return err;
c3d062a7
CL
1674}
1675
1676static int
9fff138e
DDP
1677netdev_dpdk_ring_set_config(struct netdev *netdev, const struct smap *args,
1678 char **errp OVS_UNUSED)
c3d062a7
CL
1679{
1680 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1681
1682 ovs_mutex_lock(&dev->mutex);
1683 dpdk_set_rxq_config(dev, args);
a14b8947
IM
1684 ovs_mutex_unlock(&dev->mutex);
1685
1686 return 0;
1687}
1688
c1ff66ac 1689static int
2d24d165 1690netdev_dpdk_vhost_client_set_config(struct netdev *netdev,
9fff138e
DDP
1691 const struct smap *args,
1692 char **errp OVS_UNUSED)
c1ff66ac
CL
1693{
1694 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1695 const char *path;
1696
6881885a 1697 ovs_mutex_lock(&dev->mutex);
c1ff66ac
CL
1698 if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)) {
1699 path = smap_get(args, "vhost-server-path");
2d24d165
CL
1700 if (path && strcmp(path, dev->vhost_id)) {
1701 strcpy(dev->vhost_id, path);
10087cba
CL
1702 /* check zero copy configuration */
1703 if (smap_get_bool(args, "dq-zero-copy", false)) {
1704 dev->vhost_driver_flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1705 } else {
1706 dev->vhost_driver_flags &= ~RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1707 }
c1ff66ac
CL
1708 netdev_request_reconfigure(netdev);
1709 }
1710 }
6881885a 1711 ovs_mutex_unlock(&dev->mutex);
c1ff66ac
CL
1712
1713 return 0;
1714}
1715
7dec44fe 1716static int
d46285a2 1717netdev_dpdk_get_numa_id(const struct netdev *netdev)
7dec44fe 1718{
d46285a2 1719 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
7dec44fe 1720
d46285a2 1721 return dev->socket_id;
7dec44fe
AW
1722}
1723
050c60bf 1724/* Sets the number of tx queues for the dpdk interface. */
5496878c 1725static int
050c60bf 1726netdev_dpdk_set_tx_multiq(struct netdev *netdev, unsigned int n_txq)
5496878c 1727{
d46285a2 1728 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
5496878c 1729
d46285a2 1730 ovs_mutex_lock(&dev->mutex);
91968eb0 1731
050c60bf
DDP
1732 if (dev->requested_n_txq == n_txq) {
1733 goto out;
4573fbd3
FL
1734 }
1735
050c60bf
DDP
1736 dev->requested_n_txq = n_txq;
1737 netdev_request_reconfigure(netdev);
58397e6c 1738
050c60bf 1739out:
d46285a2 1740 ovs_mutex_unlock(&dev->mutex);
050c60bf 1741 return 0;
58397e6c
KT
1742}
1743
8a9562d2
PS
1744static struct netdev_rxq *
1745netdev_dpdk_rxq_alloc(void)
1746{
1747 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
1748
eff23640
DDP
1749 if (rx) {
1750 return &rx->up;
1751 }
1752
1753 return NULL;
8a9562d2
PS
1754}
1755
1756static struct netdev_rxq_dpdk *
d46285a2 1757netdev_rxq_dpdk_cast(const struct netdev_rxq *rxq)
8a9562d2 1758{
d46285a2 1759 return CONTAINER_OF(rxq, struct netdev_rxq_dpdk, up);
8a9562d2
PS
1760}
1761
1762static int
d46285a2 1763netdev_dpdk_rxq_construct(struct netdev_rxq *rxq)
8a9562d2 1764{
d46285a2
DDP
1765 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1766 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
8a9562d2 1767
d46285a2
DDP
1768 ovs_mutex_lock(&dev->mutex);
1769 rx->port_id = dev->port_id;
1770 ovs_mutex_unlock(&dev->mutex);
8a9562d2
PS
1771
1772 return 0;
1773}
1774
1775static void
d46285a2 1776netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq OVS_UNUSED)
8a9562d2
PS
1777{
1778}
1779
1780static void
d46285a2 1781netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq)
8a9562d2 1782{
d46285a2 1783 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
8a9562d2
PS
1784
1785 rte_free(rx);
1786}
1787
819f13bd
DDP
1788/* Tries to transmit 'pkts' to txq 'qid' of device 'dev'. Takes ownership of
1789 * 'pkts', even in case of failure.
1790 *
1791 * Returns the number of packets that weren't transmitted. */
1792static inline int
b59cc14e 1793netdev_dpdk_eth_tx_burst(struct netdev_dpdk *dev, int qid,
819f13bd 1794 struct rte_mbuf **pkts, int cnt)
8a9562d2 1795{
1304f1f8
DDP
1796 uint32_t nb_tx = 0;
1797
b59cc14e 1798 while (nb_tx != cnt) {
1304f1f8
DDP
1799 uint32_t ret;
1800
b59cc14e 1801 ret = rte_eth_tx_burst(dev->port_id, qid, pkts + nb_tx, cnt - nb_tx);
1304f1f8
DDP
1802 if (!ret) {
1803 break;
1804 }
1805
1806 nb_tx += ret;
1807 }
8a9562d2 1808
b59cc14e 1809 if (OVS_UNLIKELY(nb_tx != cnt)) {
819f13bd 1810 /* Free buffers, which we couldn't transmit, one at a time (each
db73f716
DDP
1811 * packet could come from a different mempool) */
1812 int i;
1813
b59cc14e
IM
1814 for (i = nb_tx; i < cnt; i++) {
1815 rte_pktmbuf_free(pkts[i]);
db73f716 1816 }
8a9562d2 1817 }
819f13bd
DDP
1818
1819 return cnt - nb_tx;
8a9562d2
PS
1820}
1821
f3926f29
IS
1822static inline bool
1823netdev_dpdk_policer_pkt_handle(struct rte_meter_srtcm *meter,
1824 struct rte_mbuf *pkt, uint64_t time)
1825{
1826 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
1827
1828 return rte_meter_srtcm_color_blind_check(meter, time, pkt_len) ==
1829 e_RTE_METER_GREEN;
1830}
1831
1832static int
1833netdev_dpdk_policer_run(struct rte_meter_srtcm *meter,
3e90f7d7 1834 struct rte_mbuf **pkts, int pkt_cnt,
7d7ded7a 1835 bool should_steal)
f3926f29
IS
1836{
1837 int i = 0;
1838 int cnt = 0;
1839 struct rte_mbuf *pkt = NULL;
1840 uint64_t current_time = rte_rdtsc();
1841
1842 for (i = 0; i < pkt_cnt; i++) {
1843 pkt = pkts[i];
1844 /* Handle current packet */
1845 if (netdev_dpdk_policer_pkt_handle(meter, pkt, current_time)) {
1846 if (cnt != i) {
1847 pkts[cnt] = pkt;
1848 }
1849 cnt++;
1850 } else {
7d7ded7a 1851 if (should_steal) {
3e90f7d7
GZ
1852 rte_pktmbuf_free(pkt);
1853 }
f3926f29
IS
1854 }
1855 }
1856
1857 return cnt;
1858}
1859
9509913a
IS
1860static int
1861ingress_policer_run(struct ingress_policer *policer, struct rte_mbuf **pkts,
7d7ded7a 1862 int pkt_cnt, bool should_steal)
9509913a
IS
1863{
1864 int cnt = 0;
1865
1866 rte_spinlock_lock(&policer->policer_lock);
3e90f7d7 1867 cnt = netdev_dpdk_policer_run(&policer->in_policer, pkts,
7d7ded7a 1868 pkt_cnt, should_steal);
9509913a
IS
1869 rte_spinlock_unlock(&policer->policer_lock);
1870
1871 return cnt;
1872}
1873
58397e6c 1874static bool
0a0f39df 1875is_vhost_running(struct netdev_dpdk *dev)
58397e6c 1876{
0a0f39df 1877 return (netdev_dpdk_get_vid(dev) >= 0 && dev->vhost_reconfigured);
58397e6c
KT
1878}
1879
d6e3feb5 1880static inline void
1881netdev_dpdk_vhost_update_rx_size_counters(struct netdev_stats *stats,
1882 unsigned int packet_size)
1883{
1884 /* Hard-coded search for the size bucket. */
1885 if (packet_size < 256) {
1886 if (packet_size >= 128) {
1887 stats->rx_128_to_255_packets++;
1888 } else if (packet_size <= 64) {
1889 stats->rx_1_to_64_packets++;
1890 } else {
1891 stats->rx_65_to_127_packets++;
1892 }
1893 } else {
1894 if (packet_size >= 1523) {
1895 stats->rx_1523_to_max_packets++;
1896 } else if (packet_size >= 1024) {
1897 stats->rx_1024_to_1522_packets++;
1898 } else if (packet_size < 512) {
1899 stats->rx_256_to_511_packets++;
1900 } else {
1901 stats->rx_512_to_1023_packets++;
1902 }
1903 }
1904}
1905
9e3ddd45
TP
1906static inline void
1907netdev_dpdk_vhost_update_rx_counters(struct netdev_stats *stats,
9509913a
IS
1908 struct dp_packet **packets, int count,
1909 int dropped)
9e3ddd45
TP
1910{
1911 int i;
d6e3feb5 1912 unsigned int packet_size;
9e3ddd45
TP
1913 struct dp_packet *packet;
1914
1915 stats->rx_packets += count;
9509913a 1916 stats->rx_dropped += dropped;
9e3ddd45
TP
1917 for (i = 0; i < count; i++) {
1918 packet = packets[i];
d6e3feb5 1919 packet_size = dp_packet_size(packet);
9e3ddd45 1920
d6e3feb5 1921 if (OVS_UNLIKELY(packet_size < ETH_HEADER_LEN)) {
9e3ddd45
TP
1922 /* This only protects the following multicast counting from
1923 * too short packets, but it does not stop the packet from
1924 * further processing. */
1925 stats->rx_errors++;
1926 stats->rx_length_errors++;
1927 continue;
1928 }
1929
d6e3feb5 1930 netdev_dpdk_vhost_update_rx_size_counters(stats, packet_size);
1931
9e3ddd45
TP
1932 struct eth_header *eh = (struct eth_header *) dp_packet_data(packet);
1933 if (OVS_UNLIKELY(eth_addr_is_multicast(eh->eth_dst))) {
1934 stats->multicast++;
1935 }
1936
d6e3feb5 1937 stats->rx_bytes += packet_size;
9e3ddd45
TP
1938 }
1939}
1940
58397e6c
KT
1941/*
1942 * The receive path for the vhost port is the TX path out from guest.
1943 */
1944static int
d46285a2 1945netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq,
8492adc2 1946 struct dp_packet_batch *batch, int *qfill)
58397e6c 1947{
d46285a2 1948 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
9509913a 1949 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
58397e6c 1950 uint16_t nb_rx = 0;
9509913a 1951 uint16_t dropped = 0;
8492adc2 1952 int qid = rxq->queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
daf22bf7 1953 int vid = netdev_dpdk_get_vid(dev);
58397e6c 1954
daf22bf7 1955 if (OVS_UNLIKELY(vid < 0 || !dev->vhost_reconfigured
e543851d 1956 || !(dev->flags & NETDEV_UP))) {
58397e6c
KT
1957 return EAGAIN;
1958 }
1959
8492adc2 1960 nb_rx = rte_vhost_dequeue_burst(vid, qid, dev->mp,
64839cf4 1961 (struct rte_mbuf **) batch->packets,
cd159f1a 1962 NETDEV_MAX_BURST);
58397e6c
KT
1963 if (!nb_rx) {
1964 return EAGAIN;
1965 }
1966
8492adc2
JS
1967 if (qfill) {
1968 if (nb_rx == NETDEV_MAX_BURST) {
1969 /* The DPDK API returns a uint32_t which often has invalid bits in
1970 * the upper 16-bits. Need to restrict the value to uint16_t. */
1971 *qfill = rte_vhost_rx_queue_count(vid, qid) & UINT16_MAX;
1972 } else {
1973 *qfill = 0;
1974 }
1975 }
1976
9509913a
IS
1977 if (policer) {
1978 dropped = nb_rx;
64839cf4
WT
1979 nb_rx = ingress_policer_run(policer,
1980 (struct rte_mbuf **) batch->packets,
3e90f7d7 1981 nb_rx, true);
9509913a
IS
1982 dropped -= nb_rx;
1983 }
1984
d46285a2 1985 rte_spinlock_lock(&dev->stats_lock);
64839cf4
WT
1986 netdev_dpdk_vhost_update_rx_counters(&dev->stats, batch->packets,
1987 nb_rx, dropped);
d46285a2 1988 rte_spinlock_unlock(&dev->stats_lock);
45d947c4 1989
75fb9148
ZB
1990 batch->count = nb_rx;
1991 dp_packet_batch_init_packet_fields(batch);
1992
58397e6c
KT
1993 return 0;
1994}
1995
8a9562d2 1996static int
8492adc2
JS
1997netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet_batch *batch,
1998 int *qfill)
8a9562d2 1999{
d46285a2
DDP
2000 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
2001 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
9509913a 2002 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
8a9562d2 2003 int nb_rx;
9509913a 2004 int dropped = 0;
8a9562d2 2005
3b1fb077
DDP
2006 if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) {
2007 return EAGAIN;
2008 }
2009
d46285a2 2010 nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id,
64839cf4 2011 (struct rte_mbuf **) batch->packets,
cd159f1a 2012 NETDEV_MAX_BURST);
8a9562d2
PS
2013 if (!nb_rx) {
2014 return EAGAIN;
2015 }
2016
9509913a
IS
2017 if (policer) {
2018 dropped = nb_rx;
64839cf4 2019 nb_rx = ingress_policer_run(policer,
58be5c0e 2020 (struct rte_mbuf **) batch->packets,
3e90f7d7 2021 nb_rx, true);
9509913a
IS
2022 dropped -= nb_rx;
2023 }
2024
2025 /* Update stats to reflect dropped packets */
2026 if (OVS_UNLIKELY(dropped)) {
2027 rte_spinlock_lock(&dev->stats_lock);
2028 dev->stats.rx_dropped += dropped;
2029 rte_spinlock_unlock(&dev->stats_lock);
2030 }
2031
64839cf4 2032 batch->count = nb_rx;
75fb9148 2033 dp_packet_batch_init_packet_fields(batch);
8a9562d2 2034
8492adc2
JS
2035 if (qfill) {
2036 if (nb_rx == NETDEV_MAX_BURST) {
2037 *qfill = rte_eth_rx_queue_count(rx->port_id, rxq->queue_id);
2038 } else {
2039 *qfill = 0;
2040 }
2041 }
2042
8a9562d2
PS
2043 return 0;
2044}
2045
0bf765f7 2046static inline int
78bd47cf 2047netdev_dpdk_qos_run(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
7d7ded7a 2048 int cnt, bool should_steal)
0bf765f7 2049{
78bd47cf 2050 struct qos_conf *qos_conf = ovsrcu_get(struct qos_conf *, &dev->qos_conf);
0bf765f7 2051
78bd47cf
DDP
2052 if (qos_conf) {
2053 rte_spinlock_lock(&qos_conf->lock);
7d7ded7a 2054 cnt = qos_conf->ops->qos_run(qos_conf, pkts, cnt, should_steal);
78bd47cf 2055 rte_spinlock_unlock(&qos_conf->lock);
0bf765f7
IS
2056 }
2057
2058 return cnt;
2059}
2060
c6ec9d17
IM
2061static int
2062netdev_dpdk_filter_packet_len(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
2063 int pkt_cnt)
2064{
2065 int i = 0;
2066 int cnt = 0;
2067 struct rte_mbuf *pkt;
2068
2069 for (i = 0; i < pkt_cnt; i++) {
2070 pkt = pkts[i];
2071 if (OVS_UNLIKELY(pkt->pkt_len > dev->max_packet_len)) {
2072 VLOG_WARN_RL(&rl, "%s: Too big size %" PRIu32 " max_packet_len %d",
2073 dev->up.name, pkt->pkt_len, dev->max_packet_len);
2074 rte_pktmbuf_free(pkt);
2075 continue;
2076 }
2077
2078 if (OVS_UNLIKELY(i != cnt)) {
2079 pkts[cnt] = pkt;
2080 }
2081 cnt++;
2082 }
2083
2084 return cnt;
2085}
2086
9e3ddd45
TP
2087static inline void
2088netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats,
2089 struct dp_packet **packets,
2090 int attempted,
2091 int dropped)
2092{
2093 int i;
2094 int sent = attempted - dropped;
2095
2096 stats->tx_packets += sent;
2097 stats->tx_dropped += dropped;
2098
2099 for (i = 0; i < sent; i++) {
2100 stats->tx_bytes += dp_packet_size(packets[i]);
2101 }
2102}
2103
58397e6c 2104static void
4573fbd3 2105__netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
dd52de45 2106 struct dp_packet **pkts, int cnt)
58397e6c 2107{
d46285a2 2108 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
95e9881f
KT
2109 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
2110 unsigned int total_pkts = cnt;
c6ec9d17 2111 unsigned int dropped = 0;
dd52de45 2112 int i, retries = 0;
daf22bf7 2113 int vid = netdev_dpdk_get_vid(dev);
58397e6c 2114
81acebda 2115 qid = dev->tx_q[qid % netdev->n_txq].map;
585a5bea 2116
daf22bf7 2117 if (OVS_UNLIKELY(vid < 0 || !dev->vhost_reconfigured || qid < 0
e543851d 2118 || !(dev->flags & NETDEV_UP))) {
d46285a2
DDP
2119 rte_spinlock_lock(&dev->stats_lock);
2120 dev->stats.tx_dropped+= cnt;
2121 rte_spinlock_unlock(&dev->stats_lock);
1b99bb05 2122 goto out;
58397e6c
KT
2123 }
2124
d46285a2 2125 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
58397e6c 2126
c6ec9d17 2127 cnt = netdev_dpdk_filter_packet_len(dev, cur_pkts, cnt);
0bf765f7 2128 /* Check has QoS has been configured for the netdev */
3e90f7d7 2129 cnt = netdev_dpdk_qos_run(dev, cur_pkts, cnt, true);
c6ec9d17 2130 dropped = total_pkts - cnt;
0bf765f7 2131
95e9881f 2132 do {
4573fbd3 2133 int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ;
95e9881f
KT
2134 unsigned int tx_pkts;
2135
daf22bf7 2136 tx_pkts = rte_vhost_enqueue_burst(vid, vhost_qid, cur_pkts, cnt);
95e9881f
KT
2137 if (OVS_LIKELY(tx_pkts)) {
2138 /* Packets have been sent.*/
2139 cnt -= tx_pkts;
31871ee3 2140 /* Prepare for possible retry.*/
95e9881f
KT
2141 cur_pkts = &cur_pkts[tx_pkts];
2142 } else {
31871ee3
KT
2143 /* No packets sent - do not retry.*/
2144 break;
95e9881f 2145 }
c6ec9d17 2146 } while (cnt && (retries++ <= VHOST_ENQ_RETRY_NUM));
4573fbd3 2147
d46285a2 2148 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
95e9881f 2149
d46285a2 2150 rte_spinlock_lock(&dev->stats_lock);
0072e931 2151 netdev_dpdk_vhost_update_tx_counters(&dev->stats, pkts, total_pkts,
c6ec9d17 2152 cnt + dropped);
d46285a2 2153 rte_spinlock_unlock(&dev->stats_lock);
58397e6c
KT
2154
2155out:
c6ec9d17 2156 for (i = 0; i < total_pkts - dropped; i++) {
dd52de45 2157 dp_packet_delete(pkts[i]);
58397e6c
KT
2158 }
2159}
2160
8a9562d2
PS
2161/* Tx function. Transmit packets indefinitely */
2162static void
64839cf4 2163dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)
db73f716 2164 OVS_NO_THREAD_SAFETY_ANALYSIS
8a9562d2 2165{
8a14bd7b 2166 const size_t batch_cnt = dp_packet_batch_size(batch);
bce01e3a 2167#if !defined(__CHECKER__) && !defined(_WIN32)
8a14bd7b 2168 const size_t PKT_ARRAY_SIZE = batch_cnt;
bce01e3a
EJ
2169#else
2170 /* Sparse or MSVC doesn't like variable length array. */
cd159f1a 2171 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
bce01e3a 2172#endif
8a9562d2 2173 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2391135c 2174 struct rte_mbuf *pkts[PKT_ARRAY_SIZE];
8a14bd7b 2175 uint32_t cnt = batch_cnt;
3e90f7d7
GZ
2176 uint32_t dropped = 0;
2177
2178 if (dev->type != DPDK_DEV_VHOST) {
2179 /* Check if QoS has been configured for this netdev. */
2180 cnt = netdev_dpdk_qos_run(dev, (struct rte_mbuf **) batch->packets,
8a14bd7b
BB
2181 batch_cnt, false);
2182 dropped += batch_cnt - cnt;
3e90f7d7 2183 }
8a9562d2 2184
3e90f7d7
GZ
2185 uint32_t txcnt = 0;
2186
2187 for (uint32_t i = 0; i < cnt; i++) {
8a14bd7b
BB
2188 struct dp_packet *packet = batch->packets[i];
2189 uint32_t size = dp_packet_size(packet);
95fb793a 2190
f98d7864 2191 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
3e90f7d7
GZ
2192 VLOG_WARN_RL(&rl, "Too big size %u max_packet_len %d",
2193 size, dev->max_packet_len);
f4fd623c 2194
175cf4de 2195 dropped++;
f4fd623c
DDP
2196 continue;
2197 }
8a9562d2 2198
24e78f93 2199 pkts[txcnt] = rte_pktmbuf_alloc(dev->mp);
8a14bd7b 2200 if (OVS_UNLIKELY(!pkts[txcnt])) {
3e90f7d7 2201 dropped += cnt - i;
175cf4de 2202 break;
f4fd623c
DDP
2203 }
2204
2205 /* We have to do a copy for now */
3e90f7d7 2206 memcpy(rte_pktmbuf_mtod(pkts[txcnt], void *),
8a14bd7b
BB
2207 dp_packet_data(packet), size);
2208 dp_packet_set_size((struct dp_packet *)pkts[txcnt], size);
f4fd623c 2209
3e90f7d7 2210 txcnt++;
f4fd623c 2211 }
8a9562d2 2212
3e90f7d7
GZ
2213 if (OVS_LIKELY(txcnt)) {
2214 if (dev->type == DPDK_DEV_VHOST) {
2215 __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) pkts,
2216 txcnt);
2217 } else {
2218 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, txcnt);
2219 }
58397e6c 2220 }
db73f716 2221
0bf765f7
IS
2222 if (OVS_UNLIKELY(dropped)) {
2223 rte_spinlock_lock(&dev->stats_lock);
2224 dev->stats.tx_dropped += dropped;
2225 rte_spinlock_unlock(&dev->stats_lock);
2226 }
8a9562d2
PS
2227}
2228
58397e6c 2229static int
64839cf4
WT
2230netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
2231 struct dp_packet_batch *batch,
b30896c9 2232 bool concurrent_txq OVS_UNUSED)
58397e6c 2233{
58397e6c 2234
b30896c9 2235 if (OVS_UNLIKELY(batch->packets[0]->source != DPBUF_DPDK)) {
64839cf4 2236 dpdk_do_tx_copy(netdev, qid, batch);
b30896c9 2237 dp_packet_delete_batch(batch, true);
58397e6c 2238 } else {
dd52de45 2239 __netdev_dpdk_vhost_send(netdev, qid, batch->packets, batch->count);
58397e6c
KT
2240 }
2241 return 0;
2242}
2243
7251515e
DV
2244static inline void
2245netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
b30896c9 2246 struct dp_packet_batch *batch,
324c8374 2247 bool concurrent_txq)
8a9562d2 2248{
3b1fb077 2249 if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) {
b30896c9 2250 dp_packet_delete_batch(batch, true);
3b1fb077
DDP
2251 return;
2252 }
2253
324c8374 2254 if (OVS_UNLIKELY(concurrent_txq)) {
81acebda 2255 qid = qid % dev->up.n_txq;
a0cb2d66
DDP
2256 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
2257 }
2258
b30896c9 2259 if (OVS_UNLIKELY(batch->packets[0]->source != DPBUF_DPDK)) {
7251515e
DV
2260 struct netdev *netdev = &dev->up;
2261
64839cf4 2262 dpdk_do_tx_copy(netdev, qid, batch);
b30896c9 2263 dp_packet_delete_batch(batch, true);
8a9562d2 2264 } else {
fd57eeba
BB
2265 int tx_cnt, dropped;
2266 int batch_cnt = dp_packet_batch_size(batch);
2391135c 2267 struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets;
8a9562d2 2268
fd57eeba
BB
2269 tx_cnt = netdev_dpdk_filter_packet_len(dev, pkts, batch_cnt);
2270 tx_cnt = netdev_dpdk_qos_run(dev, pkts, tx_cnt, true);
2271 dropped = batch_cnt - tx_cnt;
1b99bb05 2272
fd57eeba 2273 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, tx_cnt);
8a9562d2 2274
f4fd623c 2275 if (OVS_UNLIKELY(dropped)) {
45d947c4 2276 rte_spinlock_lock(&dev->stats_lock);
f4fd623c 2277 dev->stats.tx_dropped += dropped;
45d947c4 2278 rte_spinlock_unlock(&dev->stats_lock);
f4fd623c 2279 }
8a9562d2 2280 }
a0cb2d66 2281
324c8374 2282 if (OVS_UNLIKELY(concurrent_txq)) {
a0cb2d66
DDP
2283 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
2284 }
7251515e
DV
2285}
2286
2287static int
2288netdev_dpdk_eth_send(struct netdev *netdev, int qid,
b30896c9 2289 struct dp_packet_batch *batch, bool concurrent_txq)
7251515e
DV
2290{
2291 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 2292
b30896c9 2293 netdev_dpdk_send__(dev, qid, batch, concurrent_txq);
7251515e 2294 return 0;
8a9562d2
PS
2295}
2296
2297static int
74ff3298 2298netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
8a9562d2
PS
2299{
2300 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2301
2302 ovs_mutex_lock(&dev->mutex);
2303 if (!eth_addr_equals(dev->hwaddr, mac)) {
74ff3298 2304 dev->hwaddr = mac;
045c0d1a 2305 netdev_change_seq_changed(netdev);
8a9562d2
PS
2306 }
2307 ovs_mutex_unlock(&dev->mutex);
2308
2309 return 0;
2310}
2311
2312static int
74ff3298 2313netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
8a9562d2
PS
2314{
2315 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2316
2317 ovs_mutex_lock(&dev->mutex);
74ff3298 2318 *mac = dev->hwaddr;
8a9562d2
PS
2319 ovs_mutex_unlock(&dev->mutex);
2320
2321 return 0;
2322}
2323
2324static int
2325netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
2326{
2327 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2328
2329 ovs_mutex_lock(&dev->mutex);
2330 *mtup = dev->mtu;
2331 ovs_mutex_unlock(&dev->mutex);
2332
2333 return 0;
2334}
2335
0072e931
MK
2336static int
2337netdev_dpdk_set_mtu(struct netdev *netdev, int mtu)
2338{
2339 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2340
f6f50552
IS
2341 /* XXX: Ensure that the overall frame length of the requested MTU does not
2342 * surpass the NETDEV_DPDK_MAX_PKT_LEN. DPDK device drivers differ in how
2343 * the L2 frame length is calculated for a given MTU when
2344 * rte_eth_dev_set_mtu(mtu) is called e.g. i40e driver includes 2 x vlan
2345 * headers, the em driver includes 1 x vlan header, the ixgbe driver does
2346 * not include vlan headers. As such we should use
2347 * MTU_TO_MAX_FRAME_LEN(mtu) which includes an additional 2 x vlan headers
2348 * (8 bytes) for comparison. This avoids a failure later with
2349 * rte_eth_dev_set_mtu(). This approach should be used until DPDK provides
2350 * a method to retrieve the upper bound MTU for a given device.
2351 */
2352 if (MTU_TO_MAX_FRAME_LEN(mtu) > NETDEV_DPDK_MAX_PKT_LEN
0072e931
MK
2353 || mtu < ETHER_MIN_MTU) {
2354 VLOG_WARN("%s: unsupported MTU %d\n", dev->up.name, mtu);
2355 return EINVAL;
2356 }
2357
2358 ovs_mutex_lock(&dev->mutex);
2359 if (dev->requested_mtu != mtu) {
2360 dev->requested_mtu = mtu;
2361 netdev_request_reconfigure(netdev);
2362 }
2363 ovs_mutex_unlock(&dev->mutex);
2364
2365 return 0;
2366}
2367
8a9562d2 2368static int
d46285a2 2369netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier);
8a9562d2 2370
58397e6c
KT
2371static int
2372netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
2373 struct netdev_stats *stats)
2374{
2375 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2376
2377 ovs_mutex_lock(&dev->mutex);
58397e6c 2378
45d947c4 2379 rte_spinlock_lock(&dev->stats_lock);
58397e6c 2380 /* Supported Stats */
50986e78 2381 stats->rx_packets = dev->stats.rx_packets;
2382 stats->tx_packets = dev->stats.tx_packets;
9509913a 2383 stats->rx_dropped = dev->stats.rx_dropped;
50986e78 2384 stats->tx_dropped = dev->stats.tx_dropped;
9e3ddd45
TP
2385 stats->multicast = dev->stats.multicast;
2386 stats->rx_bytes = dev->stats.rx_bytes;
2387 stats->tx_bytes = dev->stats.tx_bytes;
2388 stats->rx_errors = dev->stats.rx_errors;
2389 stats->rx_length_errors = dev->stats.rx_length_errors;
d6e3feb5 2390
2391 stats->rx_1_to_64_packets = dev->stats.rx_1_to_64_packets;
2392 stats->rx_65_to_127_packets = dev->stats.rx_65_to_127_packets;
2393 stats->rx_128_to_255_packets = dev->stats.rx_128_to_255_packets;
2394 stats->rx_256_to_511_packets = dev->stats.rx_256_to_511_packets;
2395 stats->rx_512_to_1023_packets = dev->stats.rx_512_to_1023_packets;
2396 stats->rx_1024_to_1522_packets = dev->stats.rx_1024_to_1522_packets;
2397 stats->rx_1523_to_max_packets = dev->stats.rx_1523_to_max_packets;
2398
45d947c4 2399 rte_spinlock_unlock(&dev->stats_lock);
9e3ddd45 2400
58397e6c
KT
2401 ovs_mutex_unlock(&dev->mutex);
2402
2403 return 0;
2404}
2405
d6e3feb5 2406static void
2407netdev_dpdk_convert_xstats(struct netdev_stats *stats,
0a0f39df
CL
2408 const struct rte_eth_xstat *xstats,
2409 const struct rte_eth_xstat_name *names,
d6e3feb5 2410 const unsigned int size)
2411{
d6e3feb5 2412 for (unsigned int i = 0; i < size; i++) {
0a0f39df 2413 if (strcmp(XSTAT_RX_64_PACKETS, names[i].name) == 0) {
d6e3feb5 2414 stats->rx_1_to_64_packets = xstats[i].value;
0a0f39df 2415 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS, names[i].name) == 0) {
d6e3feb5 2416 stats->rx_65_to_127_packets = xstats[i].value;
0a0f39df 2417 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS, names[i].name) == 0) {
d6e3feb5 2418 stats->rx_128_to_255_packets = xstats[i].value;
0a0f39df 2419 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS, names[i].name) == 0) {
d6e3feb5 2420 stats->rx_256_to_511_packets = xstats[i].value;
0a0f39df 2421 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS, names[i].name) == 0) {
d6e3feb5 2422 stats->rx_512_to_1023_packets = xstats[i].value;
0a0f39df 2423 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS, names[i].name) == 0) {
d6e3feb5 2424 stats->rx_1024_to_1522_packets = xstats[i].value;
0a0f39df 2425 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
d6e3feb5 2426 stats->rx_1523_to_max_packets = xstats[i].value;
0a0f39df 2427 } else if (strcmp(XSTAT_TX_64_PACKETS, names[i].name) == 0) {
d6e3feb5 2428 stats->tx_1_to_64_packets = xstats[i].value;
0a0f39df 2429 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS, names[i].name) == 0) {
d6e3feb5 2430 stats->tx_65_to_127_packets = xstats[i].value;
0a0f39df 2431 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS, names[i].name) == 0) {
d6e3feb5 2432 stats->tx_128_to_255_packets = xstats[i].value;
0a0f39df 2433 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS, names[i].name) == 0) {
d6e3feb5 2434 stats->tx_256_to_511_packets = xstats[i].value;
0a0f39df 2435 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS, names[i].name) == 0) {
d6e3feb5 2436 stats->tx_512_to_1023_packets = xstats[i].value;
0a0f39df 2437 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS, names[i].name) == 0) {
d6e3feb5 2438 stats->tx_1024_to_1522_packets = xstats[i].value;
0a0f39df 2439 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
d6e3feb5 2440 stats->tx_1523_to_max_packets = xstats[i].value;
d57f777f
PS
2441 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS, names[i].name) == 0) {
2442 stats->multicast = xstats[i].value;
0a0f39df 2443 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS, names[i].name) == 0) {
d6e3feb5 2444 stats->tx_multicast_packets = xstats[i].value;
0a0f39df 2445 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS, names[i].name) == 0) {
d6e3feb5 2446 stats->rx_broadcast_packets = xstats[i].value;
0a0f39df 2447 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS, names[i].name) == 0) {
d6e3feb5 2448 stats->tx_broadcast_packets = xstats[i].value;
0a0f39df 2449 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS, names[i].name) == 0) {
d6e3feb5 2450 stats->rx_undersized_errors = xstats[i].value;
0a0f39df 2451 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS, names[i].name) == 0) {
d6e3feb5 2452 stats->rx_fragmented_errors = xstats[i].value;
0a0f39df 2453 } else if (strcmp(XSTAT_RX_JABBER_ERRORS, names[i].name) == 0) {
d6e3feb5 2454 stats->rx_jabber_errors = xstats[i].value;
2455 }
2456 }
2457}
2458
8a9562d2
PS
2459static int
2460netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
2461{
2462 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2463 struct rte_eth_stats rte_stats;
2464 bool gg;
2465
2466 netdev_dpdk_get_carrier(netdev, &gg);
2467 ovs_mutex_lock(&dev->mutex);
8a9562d2 2468
0a0f39df
CL
2469 struct rte_eth_xstat *rte_xstats = NULL;
2470 struct rte_eth_xstat_name *rte_xstats_names = NULL;
2471 int rte_xstats_len, rte_xstats_new_len, rte_xstats_ret;
d6e3feb5 2472
2473 if (rte_eth_stats_get(dev->port_id, &rte_stats)) {
fa9f4eeb
IM
2474 VLOG_ERR("Can't get ETH statistics for port: "DPDK_PORT_ID_FMT,
2475 dev->port_id);
f9256822 2476 ovs_mutex_unlock(&dev->mutex);
d6e3feb5 2477 return EPROTO;
2478 }
2479
0a0f39df
CL
2480 /* Get length of statistics */
2481 rte_xstats_len = rte_eth_xstats_get_names(dev->port_id, NULL, 0);
2482 if (rte_xstats_len < 0) {
fa9f4eeb
IM
2483 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT,
2484 dev->port_id);
0a0f39df
CL
2485 goto out;
2486 }
2487 /* Reserve memory for xstats names and values */
2488 rte_xstats_names = xcalloc(rte_xstats_len, sizeof *rte_xstats_names);
2489 rte_xstats = xcalloc(rte_xstats_len, sizeof *rte_xstats);
2490
2491 /* Retreive xstats names */
2492 rte_xstats_new_len = rte_eth_xstats_get_names(dev->port_id,
2493 rte_xstats_names,
2494 rte_xstats_len);
2495 if (rte_xstats_new_len != rte_xstats_len) {
fa9f4eeb
IM
2496 VLOG_WARN("Cannot get XSTATS names for port: "DPDK_PORT_ID_FMT,
2497 dev->port_id);
0a0f39df
CL
2498 goto out;
2499 }
2500 /* Retreive xstats values */
2501 memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len);
2502 rte_xstats_ret = rte_eth_xstats_get(dev->port_id, rte_xstats,
2503 rte_xstats_len);
2504 if (rte_xstats_ret > 0 && rte_xstats_ret <= rte_xstats_len) {
2505 netdev_dpdk_convert_xstats(stats, rte_xstats, rte_xstats_names,
2506 rte_xstats_len);
d6e3feb5 2507 } else {
fa9f4eeb
IM
2508 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT,
2509 dev->port_id);
d6e3feb5 2510 }
8a9562d2 2511
0a0f39df
CL
2512out:
2513 free(rte_xstats);
2514 free(rte_xstats_names);
2515
2f9dd77f
PS
2516 stats->rx_packets = rte_stats.ipackets;
2517 stats->tx_packets = rte_stats.opackets;
2518 stats->rx_bytes = rte_stats.ibytes;
2519 stats->tx_bytes = rte_stats.obytes;
21e9844c 2520 stats->rx_errors = rte_stats.ierrors;
2f9dd77f 2521 stats->tx_errors = rte_stats.oerrors;
8a9562d2 2522
45d947c4 2523 rte_spinlock_lock(&dev->stats_lock);
2f9dd77f 2524 stats->tx_dropped = dev->stats.tx_dropped;
9509913a 2525 stats->rx_dropped = dev->stats.rx_dropped;
45d947c4 2526 rte_spinlock_unlock(&dev->stats_lock);
9e3ddd45
TP
2527
2528 /* These are the available DPDK counters for packets not received due to
2529 * local resource constraints in DPDK and NIC respectively. */
9509913a 2530 stats->rx_dropped += rte_stats.rx_nombuf + rte_stats.imissed;
9e3ddd45
TP
2531 stats->rx_missed_errors = rte_stats.imissed;
2532
8a9562d2
PS
2533 ovs_mutex_unlock(&dev->mutex);
2534
2535 return 0;
2536}
2537
971f4b39
MW
2538static int
2539netdev_dpdk_get_custom_stats(const struct netdev *netdev,
2540 struct netdev_custom_stats *custom_stats)
2541{
2542
2543 uint32_t i;
2544 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2545 int rte_xstats_ret;
2546
2547 ovs_mutex_lock(&dev->mutex);
2548
2549 if (netdev_dpdk_configure_xstats(dev)) {
2550 uint64_t *values = xcalloc(dev->rte_xstats_ids_size,
2551 sizeof(uint64_t));
2552
2553 rte_xstats_ret =
2554 rte_eth_xstats_get_by_id(dev->port_id, dev->rte_xstats_ids,
2555 values, dev->rte_xstats_ids_size);
2556
2557 if (rte_xstats_ret > 0 &&
2558 rte_xstats_ret <= dev->rte_xstats_ids_size) {
2559
2560 custom_stats->size = rte_xstats_ret;
2561 custom_stats->counters =
2562 (struct netdev_custom_counter *) xcalloc(rte_xstats_ret,
2563 sizeof(struct netdev_custom_counter));
2564
2565 for (i = 0; i < rte_xstats_ret; i++) {
2566 ovs_strlcpy(custom_stats->counters[i].name,
2567 netdev_dpdk_get_xstat_name(dev,
2568 dev->rte_xstats_ids[i]),
2569 NETDEV_CUSTOM_STATS_NAME_SIZE);
2570 custom_stats->counters[i].value = values[i];
2571 }
2572 } else {
fa9f4eeb 2573 VLOG_WARN("Cannot get XSTATS values for port: "DPDK_PORT_ID_FMT,
971f4b39
MW
2574 dev->port_id);
2575 custom_stats->counters = NULL;
2576 custom_stats->size = 0;
2577 /* Let's clear statistics cache, so it will be
2578 * reconfigured */
2579 netdev_dpdk_clear_xstats(dev);
2580 }
526259f2
IM
2581
2582 free(values);
971f4b39
MW
2583 }
2584
2585 ovs_mutex_unlock(&dev->mutex);
2586
2587 return 0;
2588}
2589
8a9562d2 2590static int
d46285a2 2591netdev_dpdk_get_features(const struct netdev *netdev,
8a9562d2 2592 enum netdev_features *current,
ca3d4f55
BX
2593 enum netdev_features *advertised,
2594 enum netdev_features *supported,
2595 enum netdev_features *peer)
8a9562d2 2596{
d46285a2 2597 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2598 struct rte_eth_link link;
2599
2600 ovs_mutex_lock(&dev->mutex);
2601 link = dev->link;
2602 ovs_mutex_unlock(&dev->mutex);
2603
362ca396 2604 if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
2605 if (link.link_speed == ETH_SPEED_NUM_10M) {
8a9562d2
PS
2606 *current = NETDEV_F_10MB_HD;
2607 }
362ca396 2608 if (link.link_speed == ETH_SPEED_NUM_100M) {
8a9562d2
PS
2609 *current = NETDEV_F_100MB_HD;
2610 }
362ca396 2611 if (link.link_speed == ETH_SPEED_NUM_1G) {
8a9562d2
PS
2612 *current = NETDEV_F_1GB_HD;
2613 }
2614 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
362ca396 2615 if (link.link_speed == ETH_SPEED_NUM_10M) {
8a9562d2
PS
2616 *current = NETDEV_F_10MB_FD;
2617 }
362ca396 2618 if (link.link_speed == ETH_SPEED_NUM_100M) {
8a9562d2
PS
2619 *current = NETDEV_F_100MB_FD;
2620 }
362ca396 2621 if (link.link_speed == ETH_SPEED_NUM_1G) {
8a9562d2
PS
2622 *current = NETDEV_F_1GB_FD;
2623 }
362ca396 2624 if (link.link_speed == ETH_SPEED_NUM_10G) {
8a9562d2
PS
2625 *current = NETDEV_F_10GB_FD;
2626 }
2627 }
2628
362ca396 2629 if (link.link_autoneg) {
2630 *current |= NETDEV_F_AUTONEG;
2631 }
2632
ca3d4f55
BX
2633 *advertised = *supported = *peer = 0;
2634
8a9562d2
PS
2635 return 0;
2636}
2637
9509913a
IS
2638static struct ingress_policer *
2639netdev_dpdk_policer_construct(uint32_t rate, uint32_t burst)
2640{
2641 struct ingress_policer *policer = NULL;
2642 uint64_t rate_bytes;
2643 uint64_t burst_bytes;
2644 int err = 0;
2645
2646 policer = xmalloc(sizeof *policer);
2647 rte_spinlock_init(&policer->policer_lock);
2648
2649 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
602c8668
LR
2650 rate_bytes = rate * 1000ULL / 8;
2651 burst_bytes = burst * 1000ULL / 8;
9509913a
IS
2652
2653 policer->app_srtcm_params.cir = rate_bytes;
2654 policer->app_srtcm_params.cbs = burst_bytes;
2655 policer->app_srtcm_params.ebs = 0;
2656 err = rte_meter_srtcm_config(&policer->in_policer,
2657 &policer->app_srtcm_params);
58be5c0e 2658 if (err) {
9509913a 2659 VLOG_ERR("Could not create rte meter for ingress policer");
4c47ddde 2660 free(policer);
9509913a
IS
2661 return NULL;
2662 }
2663
2664 return policer;
2665}
2666
2667static int
2668netdev_dpdk_set_policing(struct netdev* netdev, uint32_t policer_rate,
2669 uint32_t policer_burst)
2670{
2671 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2672 struct ingress_policer *policer;
2673
2674 /* Force to 0 if no rate specified,
2675 * default to 8000 kbits if burst is 0,
2676 * else stick with user-specified value.
2677 */
2678 policer_burst = (!policer_rate ? 0
2679 : !policer_burst ? 8000
2680 : policer_burst);
2681
2682 ovs_mutex_lock(&dev->mutex);
2683
2684 policer = ovsrcu_get_protected(struct ingress_policer *,
2685 &dev->ingress_policer);
2686
2687 if (dev->policer_rate == policer_rate &&
2688 dev->policer_burst == policer_burst) {
2689 /* Assume that settings haven't changed since we last set them. */
2690 ovs_mutex_unlock(&dev->mutex);
2691 return 0;
2692 }
2693
2694 /* Destroy any existing ingress policer for the device if one exists */
2695 if (policer) {
2696 ovsrcu_postpone(free, policer);
2697 }
2698
2699 if (policer_rate != 0) {
2700 policer = netdev_dpdk_policer_construct(policer_rate, policer_burst);
2701 } else {
2702 policer = NULL;
2703 }
2704 ovsrcu_set(&dev->ingress_policer, policer);
2705 dev->policer_rate = policer_rate;
2706 dev->policer_burst = policer_burst;
2707 ovs_mutex_unlock(&dev->mutex);
2708
2709 return 0;
2710}
2711
8a9562d2
PS
2712static int
2713netdev_dpdk_get_ifindex(const struct netdev *netdev)
2714{
2715 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2716
2717 ovs_mutex_lock(&dev->mutex);
12d0d124
PL
2718 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2719 * postive integer to meet RFC 2863 recommendations.
2720 */
2721 int ifindex = hash_string(netdev->name, 0) % 0xfffffe + 1;
8a9562d2
PS
2722 ovs_mutex_unlock(&dev->mutex);
2723
2724 return ifindex;
2725}
2726
2727static int
d46285a2 2728netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier)
8a9562d2 2729{
d46285a2 2730 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2731
2732 ovs_mutex_lock(&dev->mutex);
2733 check_link_status(dev);
2734 *carrier = dev->link.link_status;
58397e6c
KT
2735
2736 ovs_mutex_unlock(&dev->mutex);
2737
2738 return 0;
2739}
2740
2741static int
d46285a2 2742netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier)
58397e6c 2743{
d46285a2 2744 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
58397e6c
KT
2745
2746 ovs_mutex_lock(&dev->mutex);
2747
0a0f39df 2748 if (is_vhost_running(dev)) {
58397e6c
KT
2749 *carrier = 1;
2750 } else {
2751 *carrier = 0;
2752 }
2753
8a9562d2
PS
2754 ovs_mutex_unlock(&dev->mutex);
2755
2756 return 0;
2757}
2758
2759static long long int
d46285a2 2760netdev_dpdk_get_carrier_resets(const struct netdev *netdev)
8a9562d2 2761{
d46285a2 2762 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2763 long long int carrier_resets;
2764
2765 ovs_mutex_lock(&dev->mutex);
2766 carrier_resets = dev->link_reset_cnt;
2767 ovs_mutex_unlock(&dev->mutex);
2768
2769 return carrier_resets;
2770}
2771
2772static int
d46285a2 2773netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED,
8a9562d2
PS
2774 long long int interval OVS_UNUSED)
2775{
ee32150e 2776 return EOPNOTSUPP;
8a9562d2
PS
2777}
2778
2779static int
2780netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
2781 enum netdev_flags off, enum netdev_flags on,
64839cf4
WT
2782 enum netdev_flags *old_flagsp)
2783 OVS_REQUIRES(dev->mutex)
8a9562d2 2784{
8a9562d2
PS
2785 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
2786 return EINVAL;
2787 }
2788
2789 *old_flagsp = dev->flags;
2790 dev->flags |= on;
2791 dev->flags &= ~off;
2792
2793 if (dev->flags == *old_flagsp) {
2794 return 0;
2795 }
2796
58397e6c 2797 if (dev->type == DPDK_DEV_ETH) {
58397e6c
KT
2798 if (dev->flags & NETDEV_PROMISC) {
2799 rte_eth_promiscuous_enable(dev->port_id);
2800 }
8a9562d2 2801
314fb5ad 2802 netdev_change_seq_changed(&dev->up);
e543851d
ZB
2803 } else {
2804 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2805 * running then change netdev's change_seq to trigger link state
2806 * update. */
e543851d
ZB
2807
2808 if ((NETDEV_UP & ((*old_flagsp ^ on) | (*old_flagsp ^ off)))
0a0f39df 2809 && is_vhost_running(dev)) {
e543851d
ZB
2810 netdev_change_seq_changed(&dev->up);
2811
2812 /* Clear statistics if device is getting up. */
2813 if (NETDEV_UP & on) {
2814 rte_spinlock_lock(&dev->stats_lock);
58be5c0e 2815 memset(&dev->stats, 0, sizeof dev->stats);
e543851d
ZB
2816 rte_spinlock_unlock(&dev->stats_lock);
2817 }
2818 }
8a9562d2
PS
2819 }
2820
2821 return 0;
2822}
2823
2824static int
d46285a2 2825netdev_dpdk_update_flags(struct netdev *netdev,
8a9562d2
PS
2826 enum netdev_flags off, enum netdev_flags on,
2827 enum netdev_flags *old_flagsp)
2828{
d46285a2 2829 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2830 int error;
2831
d46285a2
DDP
2832 ovs_mutex_lock(&dev->mutex);
2833 error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp);
2834 ovs_mutex_unlock(&dev->mutex);
8a9562d2
PS
2835
2836 return error;
2837}
2838
b2e8b12f
FL
2839static int
2840netdev_dpdk_vhost_user_get_status(const struct netdev *netdev,
2841 struct smap *args)
2842{
2843 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2844
2845 ovs_mutex_lock(&dev->mutex);
2846
2847 bool client_mode = dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT;
2848 smap_add_format(args, "mode", "%s", client_mode ? "client" : "server");
2849
2850 int vid = netdev_dpdk_get_vid(dev);
2851 if (vid < 0) {
2852 smap_add_format(args, "status", "disconnected");
2853 ovs_mutex_unlock(&dev->mutex);
2854 return 0;
2855 } else {
2856 smap_add_format(args, "status", "connected");
2857 }
2858
2859 char socket_name[PATH_MAX];
2860 if (!rte_vhost_get_ifname(vid, socket_name, PATH_MAX)) {
2861 smap_add_format(args, "socket", "%s", socket_name);
2862 }
2863
2864 uint64_t features;
2865 if (!rte_vhost_get_negotiated_features(vid, &features)) {
2866 smap_add_format(args, "features", "0x%016"PRIx64, features);
2867 }
2868
2869 uint16_t mtu;
2870 if (!rte_vhost_get_mtu(vid, &mtu)) {
2871 smap_add_format(args, "mtu", "%d", mtu);
2872 }
2873
2874 int numa = rte_vhost_get_numa_node(vid);
2875 if (numa >= 0) {
2876 smap_add_format(args, "numa", "%d", numa);
2877 }
2878
2879 uint16_t vring_num = rte_vhost_get_vring_num(vid);
2880 if (vring_num) {
2881 smap_add_format(args, "num_of_vrings", "%d", vring_num);
2882 }
2883
2884 for (int i = 0; i < vring_num; i++) {
2885 struct rte_vhost_vring vring;
2886 char vhost_vring[16];
2887
2888 rte_vhost_get_vhost_vring(vid, i, &vring);
2889 snprintf(vhost_vring, 16, "vring_%d_size", i);
2890 smap_add_format(args, vhost_vring, "%d", vring.size);
2891 }
2892
2893 ovs_mutex_unlock(&dev->mutex);
2894 return 0;
2895}
2896
8a9562d2 2897static int
d46285a2 2898netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args)
8a9562d2 2899{
d46285a2 2900 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2901 struct rte_eth_dev_info dev_info;
2902
7cd1261d 2903 if (!rte_eth_dev_is_valid_port(dev->port_id)) {
8a9562d2 2904 return ENODEV;
7cd1261d 2905 }
8a9562d2
PS
2906
2907 ovs_mutex_lock(&dev->mutex);
2908 rte_eth_dev_info_get(dev->port_id, &dev_info);
2909 ovs_mutex_unlock(&dev->mutex);
2910
fa9f4eeb 2911 smap_add_format(args, "port_no", DPDK_PORT_ID_FMT, dev->port_id);
58be5c0e
MK
2912 smap_add_format(args, "numa_id", "%d",
2913 rte_eth_dev_socket_id(dev->port_id));
8a9562d2
PS
2914 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
2915 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
4be4d22c 2916 smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len);
8a9562d2
PS
2917 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
2918 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
2919 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
58be5c0e
MK
2920 smap_add_format(args, "max_hash_mac_addrs", "%u",
2921 dev_info.max_hash_mac_addrs);
8a9562d2
PS
2922 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
2923 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
2924
3eb8d4fa
MW
2925 /* Querying the DPDK library for iftype may be done in future, pending
2926 * support; cf. RFC 3635 Section 3.2.4. */
2927 enum { IF_TYPE_ETHERNETCSMACD = 6 };
2928
2929 smap_add_format(args, "if_type", "%"PRIu32, IF_TYPE_ETHERNETCSMACD);
2930 smap_add_format(args, "if_descr", "%s %s", rte_version(),
2931 dev_info.driver_name);
2932
39c2baa9 2933 if (dev_info.pci_dev) {
2934 smap_add_format(args, "pci-vendor_id", "0x%u",
2935 dev_info.pci_dev->id.vendor_id);
2936 smap_add_format(args, "pci-device_id", "0x%x",
2937 dev_info.pci_dev->id.device_id);
2938 }
8a9562d2
PS
2939
2940 return 0;
2941}
2942
2943static void
2944netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
2945 OVS_REQUIRES(dev->mutex)
2946{
2947 enum netdev_flags old_flags;
2948
2949 if (admin_state) {
2950 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
2951 } else {
2952 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
2953 }
2954}
2955
2956static void
2957netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
2958 const char *argv[], void *aux OVS_UNUSED)
2959{
2960 bool up;
2961
2962 if (!strcasecmp(argv[argc - 1], "up")) {
2963 up = true;
2964 } else if ( !strcasecmp(argv[argc - 1], "down")) {
2965 up = false;
2966 } else {
2967 unixctl_command_reply_error(conn, "Invalid Admin State");
2968 return;
2969 }
2970
2971 if (argc > 2) {
2972 struct netdev *netdev = netdev_from_name(argv[1]);
3d0d5ab1 2973
8a9562d2 2974 if (netdev && is_dpdk_class(netdev->netdev_class)) {
3d0d5ab1 2975 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 2976
3d0d5ab1
IM
2977 ovs_mutex_lock(&dev->mutex);
2978 netdev_dpdk_set_admin_state__(dev, up);
2979 ovs_mutex_unlock(&dev->mutex);
8a9562d2
PS
2980
2981 netdev_close(netdev);
2982 } else {
2983 unixctl_command_reply_error(conn, "Not a DPDK Interface");
2984 netdev_close(netdev);
2985 return;
2986 }
2987 } else {
3d0d5ab1 2988 struct netdev_dpdk *dev;
8a9562d2
PS
2989
2990 ovs_mutex_lock(&dpdk_mutex);
3d0d5ab1
IM
2991 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2992 ovs_mutex_lock(&dev->mutex);
2993 netdev_dpdk_set_admin_state__(dev, up);
2994 ovs_mutex_unlock(&dev->mutex);
8a9562d2
PS
2995 }
2996 ovs_mutex_unlock(&dpdk_mutex);
2997 }
2998 unixctl_command_reply(conn, "OK");
2999}
3000
0ee821c2
DB
3001static void
3002netdev_dpdk_detach(struct unixctl_conn *conn, int argc OVS_UNUSED,
3003 const char *argv[], void *aux OVS_UNUSED)
3004{
3005 int ret;
3006 char *response;
7ee94cba 3007 dpdk_port_t port_id;
0ee821c2
DB
3008 char devname[RTE_ETH_NAME_MAX_LEN];
3009 struct netdev_dpdk *dev;
3010
3011 ovs_mutex_lock(&dpdk_mutex);
3012
255b7bda 3013 if (rte_eth_dev_get_port_by_name(argv[1], &port_id)) {
0ee821c2
DB
3014 response = xasprintf("Device '%s' not found in DPDK", argv[1]);
3015 goto error;
3016 }
3017
3018 dev = netdev_dpdk_lookup_by_port_id(port_id);
3019 if (dev) {
3020 response = xasprintf("Device '%s' is being used by interface '%s'. "
3021 "Remove it before detaching",
3022 argv[1], netdev_get_name(&dev->up));
3023 goto error;
3024 }
3025
3026 rte_eth_dev_close(port_id);
3027
3028 ret = rte_eth_dev_detach(port_id, devname);
3029 if (ret < 0) {
3030 response = xasprintf("Device '%s' can not be detached", argv[1]);
3031 goto error;
3032 }
3033
3034 response = xasprintf("Device '%s' has been detached", argv[1]);
3035
3036 ovs_mutex_unlock(&dpdk_mutex);
3037 unixctl_command_reply(conn, response);
3038 free(response);
3039 return;
3040
3041error:
3042 ovs_mutex_unlock(&dpdk_mutex);
3043 unixctl_command_reply_error(conn, response);
3044 free(response);
3045}
3046
be481733
IM
3047static void
3048netdev_dpdk_get_mempool_info(struct unixctl_conn *conn,
3049 int argc, const char *argv[],
3050 void *aux OVS_UNUSED)
3051{
3052 size_t size;
3053 FILE *stream;
3054 char *response = NULL;
3055 struct netdev *netdev = NULL;
3056
3057 if (argc == 2) {
3058 netdev = netdev_from_name(argv[1]);
3059 if (!netdev || !is_dpdk_class(netdev->netdev_class)) {
3060 unixctl_command_reply_error(conn, "Not a DPDK Interface");
3061 goto out;
3062 }
3063 }
3064
3065 stream = open_memstream(&response, &size);
3066 if (!stream) {
3067 response = xasprintf("Unable to open memstream: %s.",
3068 ovs_strerror(errno));
3069 unixctl_command_reply_error(conn, response);
3070 goto out;
3071 }
3072
3073 if (netdev) {
3074 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3075
3076 ovs_mutex_lock(&dev->mutex);
3077 ovs_mutex_lock(&dpdk_mp_mutex);
3078
3079 rte_mempool_dump(stream, dev->mp);
3080
3081 ovs_mutex_unlock(&dpdk_mp_mutex);
3082 ovs_mutex_unlock(&dev->mutex);
3083 } else {
3084 ovs_mutex_lock(&dpdk_mp_mutex);
3085 rte_mempool_list_dump(stream);
3086 ovs_mutex_unlock(&dpdk_mp_mutex);
3087 }
3088
3089 fclose(stream);
3090
3091 unixctl_command_reply(conn, response);
3092out:
3093 free(response);
3094 netdev_close(netdev);
3095}
3096
58397e6c
KT
3097/*
3098 * Set virtqueue flags so that we do not receive interrupts.
3099 */
3100static void
0a0f39df 3101set_irq_status(int vid)
58397e6c 3102{
4573fbd3 3103 uint32_t i;
4573fbd3 3104
f3e7ec25
MW
3105 for (i = 0; i < rte_vhost_get_vring_num(vid); i++) {
3106 rte_vhost_enable_guest_notification(vid, i, 0);
4573fbd3
FL
3107 }
3108}
3109
585a5bea
IM
3110/*
3111 * Fixes mapping for vhost-user tx queues. Must be called after each
81acebda 3112 * enabling/disabling of queues and n_txq modifications.
585a5bea
IM
3113 */
3114static void
d46285a2
DDP
3115netdev_dpdk_remap_txqs(struct netdev_dpdk *dev)
3116 OVS_REQUIRES(dev->mutex)
585a5bea
IM
3117{
3118 int *enabled_queues, n_enabled = 0;
81acebda 3119 int i, k, total_txqs = dev->up.n_txq;
585a5bea 3120
eff23640 3121 enabled_queues = xcalloc(total_txqs, sizeof *enabled_queues);
585a5bea
IM
3122
3123 for (i = 0; i < total_txqs; i++) {
3124 /* Enabled queues always mapped to themselves. */
d46285a2 3125 if (dev->tx_q[i].map == i) {
585a5bea
IM
3126 enabled_queues[n_enabled++] = i;
3127 }
3128 }
3129
3130 if (n_enabled == 0 && total_txqs != 0) {
f3ea2ad2 3131 enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED;
585a5bea
IM
3132 n_enabled = 1;
3133 }
3134
3135 k = 0;
3136 for (i = 0; i < total_txqs; i++) {
d46285a2
DDP
3137 if (dev->tx_q[i].map != i) {
3138 dev->tx_q[i].map = enabled_queues[k];
585a5bea
IM
3139 k = (k + 1) % n_enabled;
3140 }
3141 }
3142
2d24d165 3143 VLOG_DBG("TX queue mapping for %s\n", dev->vhost_id);
585a5bea 3144 for (i = 0; i < total_txqs; i++) {
d46285a2 3145 VLOG_DBG("%2d --> %2d", i, dev->tx_q[i].map);
585a5bea
IM
3146 }
3147
eff23640 3148 free(enabled_queues);
585a5bea 3149}
4573fbd3 3150
58397e6c
KT
3151/*
3152 * A new virtio-net device is added to a vhost port.
3153 */
3154static int
0a0f39df 3155new_device(int vid)
58397e6c 3156{
d46285a2 3157 struct netdev_dpdk *dev;
58397e6c 3158 bool exists = false;
db8f13b0 3159 int newnode = 0;
0a0f39df
CL
3160 char ifname[IF_NAME_SZ];
3161
58be5c0e 3162 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
58397e6c
KT
3163
3164 ovs_mutex_lock(&dpdk_mutex);
3165 /* Add device to the vhost port with the same name as that passed down. */
d46285a2 3166 LIST_FOR_EACH(dev, list_node, &dpdk_list) {
c1ff66ac 3167 ovs_mutex_lock(&dev->mutex);
2d24d165 3168 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
f3e7ec25 3169 uint32_t qp_num = rte_vhost_get_vring_num(vid)/VIRTIO_QNUM;
db8f13b0
CL
3170
3171 /* Get NUMA information */
0a0f39df
CL
3172 newnode = rte_vhost_get_numa_node(vid);
3173 if (newnode == -1) {
5b9bf9e0 3174#ifdef VHOST_NUMA
db8f13b0 3175 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
0a0f39df 3176 ifname);
5b9bf9e0 3177#endif
db8f13b0 3178 newnode = dev->socket_id;
db8f13b0
CL
3179 }
3180
7f5f2bd0
IM
3181 if (dev->requested_n_txq != qp_num
3182 || dev->requested_n_rxq != qp_num
3183 || dev->requested_socket_id != newnode) {
3184 dev->requested_socket_id = newnode;
3185 dev->requested_n_rxq = qp_num;
3186 dev->requested_n_txq = qp_num;
3187 netdev_request_reconfigure(&dev->up);
3188 } else {
3189 /* Reconfiguration not required. */
3190 dev->vhost_reconfigured = true;
3191 }
81acebda 3192
0a0f39df 3193 ovsrcu_index_set(&dev->vid, vid);
81acebda
IM
3194 exists = true;
3195
58397e6c 3196 /* Disable notifications. */
0a0f39df 3197 set_irq_status(vid);
e543851d 3198 netdev_change_seq_changed(&dev->up);
d46285a2 3199 ovs_mutex_unlock(&dev->mutex);
58397e6c
KT
3200 break;
3201 }
c1ff66ac 3202 ovs_mutex_unlock(&dev->mutex);
58397e6c
KT
3203 }
3204 ovs_mutex_unlock(&dpdk_mutex);
3205
3206 if (!exists) {
0a0f39df 3207 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname);
58397e6c
KT
3208
3209 return -1;
3210 }
3211
0a0f39df
CL
3212 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
3213 ifname, newnode);
3214
58397e6c
KT
3215 return 0;
3216}
3217
f3ea2ad2
IM
3218/* Clears mapping for all available queues of vhost interface. */
3219static void
3220netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev)
3221 OVS_REQUIRES(dev->mutex)
3222{
3223 int i;
3224
81acebda 3225 for (i = 0; i < dev->up.n_txq; i++) {
f3ea2ad2
IM
3226 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
3227 }
3228}
3229
58397e6c
KT
3230/*
3231 * Remove a virtio-net device from the specific vhost port. Use dev->remove
3232 * flag to stop any more packets from being sent or received to/from a VM and
3233 * ensure all currently queued packets have been sent/received before removing
3234 * the device.
3235 */
3236static void
0a0f39df 3237destroy_device(int vid)
58397e6c 3238{
d46285a2 3239 struct netdev_dpdk *dev;
afee281f 3240 bool exists = false;
0a0f39df
CL
3241 char ifname[IF_NAME_SZ];
3242
58be5c0e 3243 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
58397e6c
KT
3244
3245 ovs_mutex_lock(&dpdk_mutex);
d46285a2 3246 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
0a0f39df 3247 if (netdev_dpdk_get_vid(dev) == vid) {
58397e6c 3248
d46285a2 3249 ovs_mutex_lock(&dev->mutex);
0a0f39df
CL
3250 dev->vhost_reconfigured = false;
3251 ovsrcu_index_set(&dev->vid, -1);
d46285a2 3252 netdev_dpdk_txq_map_clear(dev);
81acebda 3253
e543851d 3254 netdev_change_seq_changed(&dev->up);
d46285a2 3255 ovs_mutex_unlock(&dev->mutex);
81acebda 3256 exists = true;
afee281f 3257 break;
58397e6c
KT
3258 }
3259 }
afee281f 3260
58397e6c
KT
3261 ovs_mutex_unlock(&dpdk_mutex);
3262
0a0f39df 3263 if (exists) {
afee281f
KT
3264 /*
3265 * Wait for other threads to quiesce after setting the 'virtio_dev'
3266 * to NULL, before returning.
3267 */
3268 ovsrcu_synchronize();
3269 /*
3270 * As call to ovsrcu_synchronize() will end the quiescent state,
3271 * put thread back into quiescent state before returning.
3272 */
3273 ovsrcu_quiesce_start();
0a0f39df 3274 VLOG_INFO("vHost Device '%s' has been removed", ifname);
afee281f 3275 } else {
0a0f39df 3276 VLOG_INFO("vHost Device '%s' not found", ifname);
afee281f 3277 }
58397e6c
KT
3278}
3279
585a5bea 3280static int
0a0f39df 3281vring_state_changed(int vid, uint16_t queue_id, int enable)
585a5bea 3282{
d46285a2 3283 struct netdev_dpdk *dev;
585a5bea
IM
3284 bool exists = false;
3285 int qid = queue_id / VIRTIO_QNUM;
0a0f39df
CL
3286 char ifname[IF_NAME_SZ];
3287
58be5c0e 3288 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
585a5bea
IM
3289
3290 if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) {
3291 return 0;
3292 }
3293
3294 ovs_mutex_lock(&dpdk_mutex);
d46285a2 3295 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
c1ff66ac 3296 ovs_mutex_lock(&dev->mutex);
2d24d165 3297 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
585a5bea 3298 if (enable) {
d46285a2 3299 dev->tx_q[qid].map = qid;
585a5bea 3300 } else {
d46285a2 3301 dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED;
585a5bea 3302 }
d46285a2 3303 netdev_dpdk_remap_txqs(dev);
585a5bea 3304 exists = true;
d46285a2 3305 ovs_mutex_unlock(&dev->mutex);
585a5bea
IM
3306 break;
3307 }
c1ff66ac 3308 ovs_mutex_unlock(&dev->mutex);
585a5bea
IM
3309 }
3310 ovs_mutex_unlock(&dpdk_mutex);
3311
3312 if (exists) {
0a0f39df
CL
3313 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
3314 "changed to \'%s\'", queue_id, qid, ifname,
d46285a2 3315 (enable == 1) ? "enabled" : "disabled");
585a5bea 3316 } else {
0a0f39df 3317 VLOG_INFO("vHost Device '%s' not found", ifname);
585a5bea
IM
3318 return -1;
3319 }
3320
3321 return 0;
3322}
3323
8492adc2
JS
3324/*
3325 * Retrieve the DPDK virtio device ID (vid) associated with a vhostuser
3326 * or vhostuserclient netdev.
3327 *
3328 * Returns a value greater or equal to zero for a valid vid or '-1' if
3329 * there is no valid vid associated. A vid of '-1' must not be used in
3330 * rte_vhost_ APi calls.
3331 *
3332 * Once obtained and validated, a vid can be used by a PMD for multiple
3333 * subsequent rte_vhost API calls until the PMD quiesces. A PMD should
3334 * not fetch the vid again for each of a series of API calls.
3335 */
3336
0a0f39df
CL
3337int
3338netdev_dpdk_get_vid(const struct netdev_dpdk *dev)
58397e6c 3339{
0a0f39df 3340 return ovsrcu_index_get(&dev->vid);
58397e6c
KT
3341}
3342
9509913a
IS
3343struct ingress_policer *
3344netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev)
3345{
3346 return ovsrcu_get(struct ingress_policer *, &dev->ingress_policer);
3347}
3348
58397e6c 3349static int
ecc1a34e 3350netdev_dpdk_class_init(void)
7d1ced01 3351{
ecc1a34e
DDP
3352 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
3353
3354 /* This function can be called for different classes. The initialization
3355 * needs to be done only once */
3356 if (ovsthread_once_start(&once)) {
3357 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
3358 unixctl_command_register("netdev-dpdk/set-admin-state",
3359 "[netdev] up|down", 1, 2,
3360 netdev_dpdk_set_admin_state, NULL);
3361
0ee821c2
DB
3362 unixctl_command_register("netdev-dpdk/detach",
3363 "pci address of device", 1, 1,
3364 netdev_dpdk_detach, NULL);
3365
be481733
IM
3366 unixctl_command_register("netdev-dpdk/get-mempool-info",
3367 "[netdev]", 0, 1,
3368 netdev_dpdk_get_mempool_info, NULL);
3369
ecc1a34e
DDP
3370 ovsthread_once_done(&once);
3371 }
362ca396 3372
7d1ced01
CL
3373 return 0;
3374}
3375
033e9df2 3376
95fb793a 3377/* Client Rings */
3378
95fb793a 3379static int
3380dpdk_ring_create(const char dev_name[], unsigned int port_no,
bb37956a 3381 dpdk_port_t *eth_port_id)
95fb793a 3382{
48fffdee 3383 struct dpdk_ring *ring_pair;
0c6f39e5 3384 char *ring_name;
b8374d0d 3385 int port_id;
95fb793a 3386
48fffdee
KT
3387 ring_pair = dpdk_rte_mzalloc(sizeof *ring_pair);
3388 if (!ring_pair) {
95fb793a 3389 return ENOMEM;
3390 }
3391
7251515e 3392 /* XXX: Add support for multiquque ring. */
0c6f39e5 3393 ring_name = xasprintf("%s_tx", dev_name);
95fb793a 3394
8f0a76c9 3395 /* Create single producer tx ring, netdev does explicit locking. */
48fffdee 3396 ring_pair->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
8f0a76c9 3397 RING_F_SP_ENQ);
0c6f39e5 3398 free(ring_name);
48fffdee
KT
3399 if (ring_pair->cring_tx == NULL) {
3400 rte_free(ring_pair);
95fb793a 3401 return ENOMEM;
3402 }
3403
0c6f39e5 3404 ring_name = xasprintf("%s_rx", dev_name);
95fb793a 3405
8f0a76c9 3406 /* Create single consumer rx ring, netdev does explicit locking. */
48fffdee 3407 ring_pair->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
8f0a76c9 3408 RING_F_SC_DEQ);
0c6f39e5 3409 free(ring_name);
48fffdee
KT
3410 if (ring_pair->cring_rx == NULL) {
3411 rte_free(ring_pair);
95fb793a 3412 return ENOMEM;
3413 }
3414
b8374d0d
MV
3415 port_id = rte_eth_from_rings(dev_name, &ring_pair->cring_rx, 1,
3416 &ring_pair->cring_tx, 1, SOCKET0);
d7310583 3417
b8374d0d 3418 if (port_id < 0) {
48fffdee 3419 rte_free(ring_pair);
95fb793a 3420 return ENODEV;
3421 }
3422
48fffdee 3423 ring_pair->user_port_id = port_no;
b8374d0d
MV
3424 ring_pair->eth_port_id = port_id;
3425 *eth_port_id = port_id;
3426
48fffdee 3427 ovs_list_push_back(&dpdk_ring_list, &ring_pair->list_node);
95fb793a 3428
95fb793a 3429 return 0;
3430}
3431
3432static int
bb37956a 3433dpdk_ring_open(const char dev_name[], dpdk_port_t *eth_port_id)
64839cf4 3434 OVS_REQUIRES(dpdk_mutex)
95fb793a 3435{
48fffdee 3436 struct dpdk_ring *ring_pair;
95fb793a 3437 unsigned int port_no;
3438 int err = 0;
3439
3440 /* Names always start with "dpdkr" */
3441 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
3442 if (err) {
3443 return err;
3444 }
3445
58be5c0e 3446 /* Look through our list to find the device */
48fffdee
KT
3447 LIST_FOR_EACH (ring_pair, list_node, &dpdk_ring_list) {
3448 if (ring_pair->user_port_id == port_no) {
58397e6c 3449 VLOG_INFO("Found dpdk ring device %s:", dev_name);
58be5c0e 3450 /* Really all that is needed */
48fffdee 3451 *eth_port_id = ring_pair->eth_port_id;
95fb793a 3452 return 0;
3453 }
3454 }
3455 /* Need to create the device rings */
3456 return dpdk_ring_create(dev_name, port_no, eth_port_id);
3457}
3458
7251515e 3459static int
d46285a2 3460netdev_dpdk_ring_send(struct netdev *netdev, int qid,
b30896c9 3461 struct dp_packet_batch *batch, bool concurrent_txq)
7251515e 3462{
d46285a2 3463 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a543eb0 3464 struct dp_packet *packet;
1b99bb05 3465
58be5c0e
MK
3466 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
3467 * the rss hash field is clear. This is because the same mbuf may be
3468 * modified by the consumer of the ring and return into the datapath
3469 * without recalculating the RSS hash. */
e883448e 3470 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
8a543eb0 3471 dp_packet_mbuf_rss_flag_reset(packet);
1b99bb05 3472 }
7251515e 3473
b30896c9 3474 netdev_dpdk_send__(dev, qid, batch, concurrent_txq);
7251515e
DV
3475 return 0;
3476}
3477
95fb793a 3478static int
3479netdev_dpdk_ring_construct(struct netdev *netdev)
3480{
bb37956a 3481 dpdk_port_t port_no = 0;
95fb793a 3482 int err = 0;
3483
95fb793a 3484 ovs_mutex_lock(&dpdk_mutex);
3485
3486 err = dpdk_ring_open(netdev->name, &port_no);
3487 if (err) {
3488 goto unlock_dpdk;
3489 }
3490
1ce30dfd
DDP
3491 err = common_construct(netdev, port_no, DPDK_DEV_ETH,
3492 rte_eth_dev_socket_id(port_no));
95fb793a 3493unlock_dpdk:
3494 ovs_mutex_unlock(&dpdk_mutex);
3495 return err;
3496}
3497
0bf765f7
IS
3498/* QoS Functions */
3499
3500/*
3501 * Initialize QoS configuration operations.
3502 */
3503static void
3504qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops)
3505{
3506 conf->ops = ops;
78bd47cf 3507 rte_spinlock_init(&conf->lock);
0bf765f7
IS
3508}
3509
3510/*
3511 * Search existing QoS operations in qos_ops and compare each set of
3512 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
3513 * else return NULL
3514 */
3515static const struct dpdk_qos_ops *
3516qos_lookup_name(const char *name)
3517{
3518 const struct dpdk_qos_ops *const *opsp;
3519
3520 for (opsp = qos_confs; *opsp != NULL; opsp++) {
3521 const struct dpdk_qos_ops *ops = *opsp;
3522 if (!strcmp(name, ops->qos_name)) {
3523 return ops;
3524 }
3525 }
3526 return NULL;
3527}
3528
0bf765f7
IS
3529static int
3530netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED,
3531 struct sset *types)
3532{
3533 const struct dpdk_qos_ops *const *opsp;
3534
3535 for (opsp = qos_confs; *opsp != NULL; opsp++) {
3536 const struct dpdk_qos_ops *ops = *opsp;
3537 if (ops->qos_construct && ops->qos_name[0] != '\0') {
3538 sset_add(types, ops->qos_name);
3539 }
3540 }
3541 return 0;
3542}
3543
3544static int
d46285a2 3545netdev_dpdk_get_qos(const struct netdev *netdev,
0bf765f7
IS
3546 const char **typep, struct smap *details)
3547{
d46285a2 3548 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
78bd47cf 3549 struct qos_conf *qos_conf;
0bf765f7
IS
3550 int error = 0;
3551
d46285a2 3552 ovs_mutex_lock(&dev->mutex);
78bd47cf
DDP
3553 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
3554 if (qos_conf) {
3555 *typep = qos_conf->ops->qos_name;
3556 error = (qos_conf->ops->qos_get
3557 ? qos_conf->ops->qos_get(qos_conf, details): 0);
d03603c4
MC
3558 } else {
3559 /* No QoS configuration set, return an empty string */
3560 *typep = "";
0bf765f7 3561 }
d46285a2 3562 ovs_mutex_unlock(&dev->mutex);
0bf765f7
IS
3563
3564 return error;
3565}
3566
3567static int
78bd47cf
DDP
3568netdev_dpdk_set_qos(struct netdev *netdev, const char *type,
3569 const struct smap *details)
0bf765f7 3570{
d46285a2 3571 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
0bf765f7 3572 const struct dpdk_qos_ops *new_ops = NULL;
78bd47cf 3573 struct qos_conf *qos_conf, *new_qos_conf = NULL;
0bf765f7
IS
3574 int error = 0;
3575
d46285a2 3576 ovs_mutex_lock(&dev->mutex);
0bf765f7 3577
78bd47cf 3578 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
0bf765f7 3579
78bd47cf
DDP
3580 new_ops = qos_lookup_name(type);
3581
3582 if (!new_ops || !new_ops->qos_construct) {
3583 new_qos_conf = NULL;
3584 if (type && type[0]) {
3585 error = EOPNOTSUPP;
0bf765f7 3586 }
44975bb0 3587 } else if (qos_conf && qos_conf->ops == new_ops
78bd47cf
DDP
3588 && qos_conf->ops->qos_is_equal(qos_conf, details)) {
3589 new_qos_conf = qos_conf;
0bf765f7 3590 } else {
78bd47cf 3591 error = new_ops->qos_construct(details, &new_qos_conf);
7ea266e9
IS
3592 }
3593
7ea266e9 3594 if (error) {
78bd47cf
DDP
3595 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3596 type, netdev->name, rte_strerror(error));
3597 }
3598
3599 if (new_qos_conf != qos_conf) {
3600 ovsrcu_set(&dev->qos_conf, new_qos_conf);
3601 if (qos_conf) {
3602 ovsrcu_postpone(qos_conf->ops->qos_destruct, qos_conf);
3603 }
0bf765f7
IS
3604 }
3605
d46285a2 3606 ovs_mutex_unlock(&dev->mutex);
78bd47cf 3607
0bf765f7
IS
3608 return error;
3609}
3610
3611/* egress-policer details */
3612
3613struct egress_policer {
3614 struct qos_conf qos_conf;
3615 struct rte_meter_srtcm_params app_srtcm_params;
3616 struct rte_meter_srtcm egress_meter;
3617};
3618
78bd47cf
DDP
3619static void
3620egress_policer_details_to_param(const struct smap *details,
3621 struct rte_meter_srtcm_params *params)
0bf765f7 3622{
78bd47cf
DDP
3623 memset(params, 0, sizeof *params);
3624 params->cir = smap_get_ullong(details, "cir", 0);
3625 params->cbs = smap_get_ullong(details, "cbs", 0);
3626 params->ebs = 0;
0bf765f7
IS
3627}
3628
3629static int
78bd47cf
DDP
3630egress_policer_qos_construct(const struct smap *details,
3631 struct qos_conf **conf)
0bf765f7 3632{
0bf765f7 3633 struct egress_policer *policer;
0bf765f7
IS
3634 int err = 0;
3635
0bf765f7
IS
3636 policer = xmalloc(sizeof *policer);
3637 qos_conf_init(&policer->qos_conf, &egress_policer_ops);
78bd47cf 3638 egress_policer_details_to_param(details, &policer->app_srtcm_params);
0bf765f7 3639 err = rte_meter_srtcm_config(&policer->egress_meter,
78bd47cf
DDP
3640 &policer->app_srtcm_params);
3641 if (!err) {
3642 *conf = &policer->qos_conf;
3643 } else {
7ea266e9 3644 free(policer);
78bd47cf 3645 *conf = NULL;
7ea266e9
IS
3646 err = -err;
3647 }
0bf765f7
IS
3648
3649 return err;
3650}
3651
3652static void
78bd47cf 3653egress_policer_qos_destruct(struct qos_conf *conf)
0bf765f7
IS
3654{
3655 struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer,
78bd47cf 3656 qos_conf);
0bf765f7
IS
3657 free(policer);
3658}
3659
3660static int
78bd47cf 3661egress_policer_qos_get(const struct qos_conf *conf, struct smap *details)
0bf765f7 3662{
78bd47cf
DDP
3663 struct egress_policer *policer =
3664 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3665
3666 smap_add_format(details, "cir", "%"PRIu64, policer->app_srtcm_params.cir);
3667 smap_add_format(details, "cbs", "%"PRIu64, policer->app_srtcm_params.cbs);
050c60bf 3668
0bf765f7
IS
3669 return 0;
3670}
3671
78bd47cf 3672static bool
47a45d86
KT
3673egress_policer_qos_is_equal(const struct qos_conf *conf,
3674 const struct smap *details)
0bf765f7 3675{
78bd47cf
DDP
3676 struct egress_policer *policer =
3677 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3678 struct rte_meter_srtcm_params params;
0bf765f7 3679
78bd47cf 3680 egress_policer_details_to_param(details, &params);
7ea266e9 3681
78bd47cf 3682 return !memcmp(&params, &policer->app_srtcm_params, sizeof params);
0bf765f7
IS
3683}
3684
0bf765f7 3685static int
3e90f7d7 3686egress_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt,
7d7ded7a 3687 bool should_steal)
0bf765f7 3688{
0bf765f7 3689 int cnt = 0;
78bd47cf
DDP
3690 struct egress_policer *policer =
3691 CONTAINER_OF(conf, struct egress_policer, qos_conf);
0bf765f7 3692
3e90f7d7 3693 cnt = netdev_dpdk_policer_run(&policer->egress_meter, pkts,
7d7ded7a 3694 pkt_cnt, should_steal);
0bf765f7
IS
3695
3696 return cnt;
3697}
3698
3699static const struct dpdk_qos_ops egress_policer_ops = {
3700 "egress-policer", /* qos_name */
3701 egress_policer_qos_construct,
3702 egress_policer_qos_destruct,
3703 egress_policer_qos_get,
78bd47cf 3704 egress_policer_qos_is_equal,
0bf765f7
IS
3705 egress_policer_run
3706};
3707
050c60bf
DDP
3708static int
3709netdev_dpdk_reconfigure(struct netdev *netdev)
3710{
3711 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3712 int err = 0;
3713
050c60bf
DDP
3714 ovs_mutex_lock(&dev->mutex);
3715
3716 if (netdev->n_txq == dev->requested_n_txq
0072e931 3717 && netdev->n_rxq == dev->requested_n_rxq
b685696b 3718 && dev->mtu == dev->requested_mtu
f8b64a61 3719 && dev->lsc_interrupt_mode == dev->requested_lsc_interrupt_mode
b685696b 3720 && dev->rxq_size == dev->requested_rxq_size
bd4e172b 3721 && dev->txq_size == dev->requested_txq_size
606f6650
EC
3722 && dev->socket_id == dev->requested_socket_id
3723 && dev->started) {
050c60bf
DDP
3724 /* Reconfiguration is unnecessary */
3725
3726 goto out;
3727 }
3728
3729 rte_eth_dev_stop(dev->port_id);
606f6650 3730 dev->started = false;
050c60bf 3731
d555d9bd 3732 err = netdev_dpdk_mempool_configure(dev);
b6b26021 3733 if (err && err != EEXIST) {
d555d9bd 3734 goto out;
0072e931
MK
3735 }
3736
f8b64a61
RM
3737 dev->lsc_interrupt_mode = dev->requested_lsc_interrupt_mode;
3738
050c60bf
DDP
3739 netdev->n_txq = dev->requested_n_txq;
3740 netdev->n_rxq = dev->requested_n_rxq;
3741
b685696b
CL
3742 dev->rxq_size = dev->requested_rxq_size;
3743 dev->txq_size = dev->requested_txq_size;
3744
050c60bf
DDP
3745 rte_free(dev->tx_q);
3746 err = dpdk_eth_dev_init(dev);
eff23640
DDP
3747 dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq);
3748 if (!dev->tx_q) {
3749 err = ENOMEM;
3750 }
050c60bf 3751
0072e931
MK
3752 netdev_change_seq_changed(netdev);
3753
050c60bf 3754out:
050c60bf 3755 ovs_mutex_unlock(&dev->mutex);
050c60bf
DDP
3756 return err;
3757}
3758
7f381c2e 3759static int
2d24d165 3760dpdk_vhost_reconfigure_helper(struct netdev_dpdk *dev)
2d24d165 3761 OVS_REQUIRES(dev->mutex)
050c60bf 3762{
2d24d165
CL
3763 dev->up.n_txq = dev->requested_n_txq;
3764 dev->up.n_rxq = dev->requested_n_rxq;
96e9b168 3765 int err;
050c60bf 3766
81acebda
IM
3767 /* Enable TX queue 0 by default if it wasn't disabled. */
3768 if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) {
3769 dev->tx_q[0].map = 0;
3770 }
3771
3772 netdev_dpdk_remap_txqs(dev);
3773
d555d9bd 3774 err = netdev_dpdk_mempool_configure(dev);
b6b26021
FA
3775 if (!err) {
3776 /* A new mempool was created. */
d555d9bd 3777 netdev_change_seq_changed(&dev->up);
b6b26021
FA
3778 } else if (err != EEXIST){
3779 return err;
db8f13b0 3780 }
0a0f39df 3781 if (netdev_dpdk_get_vid(dev) >= 0) {
894af647 3782 if (dev->vhost_reconfigured == false) {
3783 dev->vhost_reconfigured = true;
3784 /* Carrier status may need updating. */
3785 netdev_change_seq_changed(&dev->up);
3786 }
81acebda 3787 }
7f381c2e
DDP
3788
3789 return 0;
2d24d165
CL
3790}
3791
3792static int
3793netdev_dpdk_vhost_reconfigure(struct netdev *netdev)
3794{
3795 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
7f381c2e 3796 int err;
2d24d165 3797
2d24d165 3798 ovs_mutex_lock(&dev->mutex);
7f381c2e 3799 err = dpdk_vhost_reconfigure_helper(dev);
2d24d165 3800 ovs_mutex_unlock(&dev->mutex);
7f381c2e
DDP
3801
3802 return err;
2d24d165
CL
3803}
3804
3805static int
3806netdev_dpdk_vhost_client_reconfigure(struct netdev *netdev)
3807{
3808 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
7f381c2e 3809 int err;
a14d1cc8 3810 uint64_t vhost_flags = 0;
10087cba 3811 bool zc_enabled;
2d24d165 3812
2d24d165
CL
3813 ovs_mutex_lock(&dev->mutex);
3814
c1ff66ac
CL
3815 /* Configure vHost client mode if requested and if the following criteria
3816 * are met:
2d24d165
CL
3817 * 1. Device hasn't been registered yet.
3818 * 2. A path has been specified.
c1ff66ac
CL
3819 */
3820 if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)
2d24d165 3821 && strlen(dev->vhost_id)) {
a14d1cc8
MK
3822 /* Register client-mode device. */
3823 vhost_flags |= RTE_VHOST_USER_CLIENT;
3824
3825 /* Enable IOMMU support, if explicitly requested. */
3826 if (dpdk_vhost_iommu_enabled()) {
3827 vhost_flags |= RTE_VHOST_USER_IOMMU_SUPPORT;
3828 }
10087cba
CL
3829
3830 zc_enabled = dev->vhost_driver_flags
3831 & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
3832 /* Enable zero copy flag, if requested */
3833 if (zc_enabled) {
3834 vhost_flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
3835 }
3836
a14d1cc8 3837 err = rte_vhost_driver_register(dev->vhost_id, vhost_flags);
c1ff66ac 3838 if (err) {
2d24d165
CL
3839 VLOG_ERR("vhost-user device setup failure for device %s\n",
3840 dev->vhost_id);
7f381c2e 3841 goto unlock;
c1ff66ac 3842 } else {
2d24d165 3843 /* Configuration successful */
a14d1cc8 3844 dev->vhost_driver_flags |= vhost_flags;
2d24d165
CL
3845 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3846 "using client socket '%s'",
3847 dev->up.name, dev->vhost_id);
10087cba
CL
3848 if (zc_enabled) {
3849 VLOG_INFO("Zero copy enabled for vHost port %s", dev->up.name);
3850 }
c1ff66ac 3851 }
f3e7ec25
MW
3852
3853 err = rte_vhost_driver_callback_register(dev->vhost_id,
3854 &virtio_net_device_ops);
3855 if (err) {
3856 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3857 "vhost user client port: %s\n", dev->up.name);
3858 goto unlock;
3859 }
3860
3861 err = rte_vhost_driver_disable_features(dev->vhost_id,
3862 1ULL << VIRTIO_NET_F_HOST_TSO4
3863 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3864 | 1ULL << VIRTIO_NET_F_CSUM);
3865 if (err) {
3866 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3867 "client port: %s\n", dev->up.name);
3868 goto unlock;
3869 }
3870
3871 err = rte_vhost_driver_start(dev->vhost_id);
3872 if (err) {
3873 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3874 "client port: %s\n", dev->up.name);
3875 goto unlock;
3876 }
c1ff66ac
CL
3877 }
3878
7f381c2e
DDP
3879 err = dpdk_vhost_reconfigure_helper(dev);
3880
3881unlock:
050c60bf 3882 ovs_mutex_unlock(&dev->mutex);
050c60bf 3883
7f381c2e 3884 return err;
050c60bf
DDP
3885}
3886
ecc1a34e 3887#define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
81acebda 3888 SET_CONFIG, SET_TX_MULTIQ, SEND, \
971f4b39
MW
3889 GET_CARRIER, GET_STATS, \
3890 GET_CUSTOM_STATS, \
81acebda
IM
3891 GET_FEATURES, GET_STATUS, \
3892 RECONFIGURE, RXQ_RECV) \
95fb793a 3893{ \
3894 NAME, \
118c77b1 3895 true, /* is_pmd */ \
ecc1a34e 3896 INIT, /* init */ \
95fb793a 3897 NULL, /* netdev_dpdk_run */ \
3898 NULL, /* netdev_dpdk_wait */ \
3899 \
3900 netdev_dpdk_alloc, \
3901 CONSTRUCT, \
58397e6c 3902 DESTRUCT, \
95fb793a 3903 netdev_dpdk_dealloc, \
3904 netdev_dpdk_get_config, \
81acebda 3905 SET_CONFIG, \
95fb793a 3906 NULL, /* get_tunnel_config */ \
58397e6c
KT
3907 NULL, /* build header */ \
3908 NULL, /* push header */ \
3909 NULL, /* pop header */ \
7dec44fe 3910 netdev_dpdk_get_numa_id, /* get_numa_id */ \
81acebda 3911 SET_TX_MULTIQ, \
95fb793a 3912 \
7251515e 3913 SEND, /* send */ \
95fb793a 3914 NULL, /* send_wait */ \
3915 \
3916 netdev_dpdk_set_etheraddr, \
3917 netdev_dpdk_get_etheraddr, \
3918 netdev_dpdk_get_mtu, \
0072e931 3919 netdev_dpdk_set_mtu, \
95fb793a 3920 netdev_dpdk_get_ifindex, \
58397e6c 3921 GET_CARRIER, \
95fb793a 3922 netdev_dpdk_get_carrier_resets, \
3923 netdev_dpdk_set_miimon, \
58397e6c 3924 GET_STATS, \
971f4b39 3925 GET_CUSTOM_STATS, \
58397e6c 3926 GET_FEATURES, \
95fb793a 3927 NULL, /* set_advertisements */ \
875ab130 3928 NULL, /* get_pt_mode */ \
95fb793a 3929 \
9509913a 3930 netdev_dpdk_set_policing, \
0bf765f7 3931 netdev_dpdk_get_qos_types, \
95fb793a 3932 NULL, /* get_qos_capabilities */ \
0bf765f7
IS
3933 netdev_dpdk_get_qos, \
3934 netdev_dpdk_set_qos, \
95fb793a 3935 NULL, /* get_queue */ \
3936 NULL, /* set_queue */ \
3937 NULL, /* delete_queue */ \
3938 NULL, /* get_queue_stats */ \
3939 NULL, /* queue_dump_start */ \
3940 NULL, /* queue_dump_next */ \
3941 NULL, /* queue_dump_done */ \
3942 NULL, /* dump_queue_stats */ \
3943 \
95fb793a 3944 NULL, /* set_in4 */ \
a8704b50 3945 NULL, /* get_addr_list */ \
95fb793a 3946 NULL, /* add_router */ \
3947 NULL, /* get_next_hop */ \
58397e6c 3948 GET_STATUS, \
95fb793a 3949 NULL, /* arp_lookup */ \
3950 \
3951 netdev_dpdk_update_flags, \
050c60bf 3952 RECONFIGURE, \
95fb793a 3953 \
3954 netdev_dpdk_rxq_alloc, \
3955 netdev_dpdk_rxq_construct, \
3956 netdev_dpdk_rxq_destruct, \
3957 netdev_dpdk_rxq_dealloc, \
58397e6c 3958 RXQ_RECV, \
95fb793a 3959 NULL, /* rx_wait */ \
3960 NULL, /* rxq_drain */ \
18ebd48c 3961 NO_OFFLOAD_API \
95fb793a 3962}
8a9562d2 3963
bce01e3a 3964static const struct netdev_class dpdk_class =
95fb793a 3965 NETDEV_DPDK_CLASS(
3966 "dpdk",
ecc1a34e 3967 netdev_dpdk_class_init,
5496878c 3968 netdev_dpdk_construct,
58397e6c 3969 netdev_dpdk_destruct,
81acebda
IM
3970 netdev_dpdk_set_config,
3971 netdev_dpdk_set_tx_multiq,
58397e6c
KT
3972 netdev_dpdk_eth_send,
3973 netdev_dpdk_get_carrier,
3974 netdev_dpdk_get_stats,
971f4b39 3975 netdev_dpdk_get_custom_stats,
58397e6c
KT
3976 netdev_dpdk_get_features,
3977 netdev_dpdk_get_status,
050c60bf 3978 netdev_dpdk_reconfigure,
58397e6c 3979 netdev_dpdk_rxq_recv);
95fb793a 3980
bce01e3a 3981static const struct netdev_class dpdk_ring_class =
95fb793a 3982 NETDEV_DPDK_CLASS(
3983 "dpdkr",
ecc1a34e 3984 netdev_dpdk_class_init,
5496878c 3985 netdev_dpdk_ring_construct,
58397e6c 3986 netdev_dpdk_destruct,
c3d062a7 3987 netdev_dpdk_ring_set_config,
81acebda 3988 netdev_dpdk_set_tx_multiq,
58397e6c
KT
3989 netdev_dpdk_ring_send,
3990 netdev_dpdk_get_carrier,
3991 netdev_dpdk_get_stats,
971f4b39 3992 netdev_dpdk_get_custom_stats,
58397e6c
KT
3993 netdev_dpdk_get_features,
3994 netdev_dpdk_get_status,
050c60bf 3995 netdev_dpdk_reconfigure,
58397e6c
KT
3996 netdev_dpdk_rxq_recv);
3997
53f50d24 3998static const struct netdev_class dpdk_vhost_class =
7d1ced01
CL
3999 NETDEV_DPDK_CLASS(
4000 "dpdkvhostuser",
f3e7ec25 4001 NULL,
53f50d24 4002 netdev_dpdk_vhost_construct,
58397e6c 4003 netdev_dpdk_vhost_destruct,
2d24d165 4004 NULL,
81acebda 4005 NULL,
58397e6c
KT
4006 netdev_dpdk_vhost_send,
4007 netdev_dpdk_vhost_get_carrier,
4008 netdev_dpdk_vhost_get_stats,
4009 NULL,
7251515e 4010 NULL,
b2e8b12f 4011 netdev_dpdk_vhost_user_get_status,
53f50d24 4012 netdev_dpdk_vhost_reconfigure,
58397e6c 4013 netdev_dpdk_vhost_rxq_recv);
2d24d165
CL
4014static const struct netdev_class dpdk_vhost_client_class =
4015 NETDEV_DPDK_CLASS(
4016 "dpdkvhostuserclient",
f3e7ec25 4017 NULL,
2d24d165
CL
4018 netdev_dpdk_vhost_client_construct,
4019 netdev_dpdk_vhost_destruct,
4020 netdev_dpdk_vhost_client_set_config,
4021 NULL,
4022 netdev_dpdk_vhost_send,
4023 netdev_dpdk_vhost_get_carrier,
4024 netdev_dpdk_vhost_get_stats,
4025 NULL,
4026 NULL,
b2e8b12f 4027 netdev_dpdk_vhost_user_get_status,
2d24d165
CL
4028 netdev_dpdk_vhost_client_reconfigure,
4029 netdev_dpdk_vhost_rxq_recv);
95fb793a 4030
8a9562d2
PS
4031void
4032netdev_dpdk_register(void)
4033{
bab69409
AC
4034 netdev_register_provider(&dpdk_class);
4035 netdev_register_provider(&dpdk_ring_class);
53f50d24 4036 netdev_register_provider(&dpdk_vhost_class);
2d24d165 4037 netdev_register_provider(&dpdk_vhost_client_class);
8a9562d2 4038}