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