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