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