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