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