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