]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-dpdk.c
netdev-dpdk: Cleanup dpdk_do_tx_copy.
[mirror_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
11d4c7a8 1672 dp_packet_batch_init_cutlen(batch);
64839cf4 1673 batch->count = (int) nb_rx;
58397e6c
KT
1674 return 0;
1675}
1676
8a9562d2 1677static int
64839cf4 1678netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet_batch *batch)
8a9562d2 1679{
d46285a2
DDP
1680 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1681 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
9509913a 1682 struct ingress_policer *policer = netdev_dpdk_get_ingress_policer(dev);
8a9562d2 1683 int nb_rx;
9509913a 1684 int dropped = 0;
8a9562d2 1685
3b1fb077
DDP
1686 if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) {
1687 return EAGAIN;
1688 }
1689
d46285a2 1690 nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id,
64839cf4 1691 (struct rte_mbuf **) batch->packets,
cd159f1a 1692 NETDEV_MAX_BURST);
8a9562d2
PS
1693 if (!nb_rx) {
1694 return EAGAIN;
1695 }
1696
9509913a
IS
1697 if (policer) {
1698 dropped = nb_rx;
64839cf4 1699 nb_rx = ingress_policer_run(policer,
58be5c0e 1700 (struct rte_mbuf **) batch->packets,
3e90f7d7 1701 nb_rx, true);
9509913a
IS
1702 dropped -= nb_rx;
1703 }
1704
1705 /* Update stats to reflect dropped packets */
1706 if (OVS_UNLIKELY(dropped)) {
1707 rte_spinlock_lock(&dev->stats_lock);
1708 dev->stats.rx_dropped += dropped;
1709 rte_spinlock_unlock(&dev->stats_lock);
1710 }
1711
11d4c7a8 1712 dp_packet_batch_init_cutlen(batch);
64839cf4 1713 batch->count = nb_rx;
8a9562d2
PS
1714
1715 return 0;
1716}
1717
0bf765f7 1718static inline int
78bd47cf 1719netdev_dpdk_qos_run(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
3e90f7d7 1720 int cnt, bool may_steal)
0bf765f7 1721{
78bd47cf 1722 struct qos_conf *qos_conf = ovsrcu_get(struct qos_conf *, &dev->qos_conf);
0bf765f7 1723
78bd47cf
DDP
1724 if (qos_conf) {
1725 rte_spinlock_lock(&qos_conf->lock);
3e90f7d7 1726 cnt = qos_conf->ops->qos_run(qos_conf, pkts, cnt, may_steal);
78bd47cf 1727 rte_spinlock_unlock(&qos_conf->lock);
0bf765f7
IS
1728 }
1729
1730 return cnt;
1731}
1732
c6ec9d17
IM
1733static int
1734netdev_dpdk_filter_packet_len(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1735 int pkt_cnt)
1736{
1737 int i = 0;
1738 int cnt = 0;
1739 struct rte_mbuf *pkt;
1740
1741 for (i = 0; i < pkt_cnt; i++) {
1742 pkt = pkts[i];
1743 if (OVS_UNLIKELY(pkt->pkt_len > dev->max_packet_len)) {
1744 VLOG_WARN_RL(&rl, "%s: Too big size %" PRIu32 " max_packet_len %d",
1745 dev->up.name, pkt->pkt_len, dev->max_packet_len);
1746 rte_pktmbuf_free(pkt);
1747 continue;
1748 }
1749
1750 if (OVS_UNLIKELY(i != cnt)) {
1751 pkts[cnt] = pkt;
1752 }
1753 cnt++;
1754 }
1755
1756 return cnt;
1757}
1758
9e3ddd45
TP
1759static inline void
1760netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats,
1761 struct dp_packet **packets,
1762 int attempted,
1763 int dropped)
1764{
1765 int i;
1766 int sent = attempted - dropped;
1767
1768 stats->tx_packets += sent;
1769 stats->tx_dropped += dropped;
1770
1771 for (i = 0; i < sent; i++) {
1772 stats->tx_bytes += dp_packet_size(packets[i]);
1773 }
1774}
1775
58397e6c 1776static void
4573fbd3 1777__netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
dd52de45 1778 struct dp_packet **pkts, int cnt)
58397e6c 1779{
d46285a2 1780 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
95e9881f
KT
1781 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
1782 unsigned int total_pkts = cnt;
c6ec9d17 1783 unsigned int dropped = 0;
dd52de45 1784 int i, retries = 0;
58397e6c 1785
81acebda 1786 qid = dev->tx_q[qid % netdev->n_txq].map;
585a5bea 1787
0a0f39df 1788 if (OVS_UNLIKELY(!is_vhost_running(dev) || qid < 0
e543851d 1789 || !(dev->flags & NETDEV_UP))) {
d46285a2
DDP
1790 rte_spinlock_lock(&dev->stats_lock);
1791 dev->stats.tx_dropped+= cnt;
1792 rte_spinlock_unlock(&dev->stats_lock);
1b99bb05 1793 goto out;
58397e6c
KT
1794 }
1795
d46285a2 1796 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
58397e6c 1797
c6ec9d17 1798 cnt = netdev_dpdk_filter_packet_len(dev, cur_pkts, cnt);
0bf765f7 1799 /* Check has QoS has been configured for the netdev */
3e90f7d7 1800 cnt = netdev_dpdk_qos_run(dev, cur_pkts, cnt, true);
c6ec9d17 1801 dropped = total_pkts - cnt;
0bf765f7 1802
95e9881f 1803 do {
4573fbd3 1804 int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ;
95e9881f
KT
1805 unsigned int tx_pkts;
1806
0a0f39df 1807 tx_pkts = rte_vhost_enqueue_burst(netdev_dpdk_get_vid(dev),
c6ec9d17 1808 vhost_qid, cur_pkts, cnt);
95e9881f
KT
1809 if (OVS_LIKELY(tx_pkts)) {
1810 /* Packets have been sent.*/
1811 cnt -= tx_pkts;
31871ee3 1812 /* Prepare for possible retry.*/
95e9881f
KT
1813 cur_pkts = &cur_pkts[tx_pkts];
1814 } else {
31871ee3
KT
1815 /* No packets sent - do not retry.*/
1816 break;
95e9881f 1817 }
c6ec9d17 1818 } while (cnt && (retries++ <= VHOST_ENQ_RETRY_NUM));
4573fbd3 1819
d46285a2 1820 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
95e9881f 1821
d46285a2 1822 rte_spinlock_lock(&dev->stats_lock);
0072e931 1823 netdev_dpdk_vhost_update_tx_counters(&dev->stats, pkts, total_pkts,
c6ec9d17 1824 cnt + dropped);
d46285a2 1825 rte_spinlock_unlock(&dev->stats_lock);
58397e6c
KT
1826
1827out:
c6ec9d17 1828 for (i = 0; i < total_pkts - dropped; i++) {
dd52de45 1829 dp_packet_delete(pkts[i]);
58397e6c
KT
1830 }
1831}
1832
8a9562d2
PS
1833/* Tx function. Transmit packets indefinitely */
1834static void
64839cf4 1835dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet_batch *batch)
db73f716 1836 OVS_NO_THREAD_SAFETY_ANALYSIS
8a9562d2 1837{
8a14bd7b 1838 const size_t batch_cnt = dp_packet_batch_size(batch);
bce01e3a 1839#if !defined(__CHECKER__) && !defined(_WIN32)
8a14bd7b 1840 const size_t PKT_ARRAY_SIZE = batch_cnt;
bce01e3a
EJ
1841#else
1842 /* Sparse or MSVC doesn't like variable length array. */
cd159f1a 1843 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
bce01e3a 1844#endif
8a9562d2 1845 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2391135c 1846 struct rte_mbuf *pkts[PKT_ARRAY_SIZE];
8a14bd7b 1847 uint32_t cnt = batch_cnt;
3e90f7d7
GZ
1848 uint32_t dropped = 0;
1849
1850 if (dev->type != DPDK_DEV_VHOST) {
1851 /* Check if QoS has been configured for this netdev. */
1852 cnt = netdev_dpdk_qos_run(dev, (struct rte_mbuf **) batch->packets,
8a14bd7b
BB
1853 batch_cnt, false);
1854 dropped += batch_cnt - cnt;
3e90f7d7 1855 }
8a9562d2 1856
7d6d1a40
WT
1857 dp_packet_batch_apply_cutlen(batch);
1858
3e90f7d7
GZ
1859 uint32_t txcnt = 0;
1860
1861 for (uint32_t i = 0; i < cnt; i++) {
8a14bd7b
BB
1862 struct dp_packet *packet = batch->packets[i];
1863 uint32_t size = dp_packet_size(packet);
95fb793a 1864
f98d7864 1865 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
3e90f7d7
GZ
1866 VLOG_WARN_RL(&rl, "Too big size %u max_packet_len %d",
1867 size, dev->max_packet_len);
f4fd623c 1868
175cf4de 1869 dropped++;
f4fd623c
DDP
1870 continue;
1871 }
8a9562d2 1872
3e90f7d7 1873 pkts[txcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
8a14bd7b 1874 if (OVS_UNLIKELY(!pkts[txcnt])) {
3e90f7d7 1875 dropped += cnt - i;
175cf4de 1876 break;
f4fd623c
DDP
1877 }
1878
1879 /* We have to do a copy for now */
3e90f7d7 1880 memcpy(rte_pktmbuf_mtod(pkts[txcnt], void *),
8a14bd7b
BB
1881 dp_packet_data(packet), size);
1882 dp_packet_set_size((struct dp_packet *)pkts[txcnt], size);
f4fd623c 1883
3e90f7d7 1884 txcnt++;
f4fd623c 1885 }
8a9562d2 1886
3e90f7d7
GZ
1887 if (OVS_LIKELY(txcnt)) {
1888 if (dev->type == DPDK_DEV_VHOST) {
1889 __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) pkts,
1890 txcnt);
1891 } else {
1892 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, txcnt);
1893 }
58397e6c 1894 }
db73f716 1895
0bf765f7
IS
1896 if (OVS_UNLIKELY(dropped)) {
1897 rte_spinlock_lock(&dev->stats_lock);
1898 dev->stats.tx_dropped += dropped;
1899 rte_spinlock_unlock(&dev->stats_lock);
1900 }
8a9562d2
PS
1901}
1902
58397e6c 1903static int
64839cf4
WT
1904netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1905 struct dp_packet_batch *batch,
324c8374 1906 bool may_steal, bool concurrent_txq OVS_UNUSED)
58397e6c 1907{
58397e6c 1908
dd52de45 1909 if (OVS_UNLIKELY(!may_steal || batch->packets[0]->source != DPBUF_DPDK)) {
64839cf4
WT
1910 dpdk_do_tx_copy(netdev, qid, batch);
1911 dp_packet_delete_batch(batch, may_steal);
58397e6c 1912 } else {
64839cf4 1913 dp_packet_batch_apply_cutlen(batch);
dd52de45 1914 __netdev_dpdk_vhost_send(netdev, qid, batch->packets, batch->count);
58397e6c
KT
1915 }
1916 return 0;
1917}
1918
7251515e
DV
1919static inline void
1920netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
324c8374
IM
1921 struct dp_packet_batch *batch, bool may_steal,
1922 bool concurrent_txq)
8a9562d2 1923{
3b1fb077
DDP
1924 if (OVS_UNLIKELY(!(dev->flags & NETDEV_UP))) {
1925 dp_packet_delete_batch(batch, may_steal);
1926 return;
1927 }
1928
324c8374 1929 if (OVS_UNLIKELY(concurrent_txq)) {
81acebda 1930 qid = qid % dev->up.n_txq;
a0cb2d66
DDP
1931 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1932 }
1933
7251515e 1934 if (OVS_UNLIKELY(!may_steal ||
64839cf4 1935 batch->packets[0]->source != DPBUF_DPDK)) {
7251515e
DV
1936 struct netdev *netdev = &dev->up;
1937
64839cf4
WT
1938 dpdk_do_tx_copy(netdev, qid, batch);
1939 dp_packet_delete_batch(batch, may_steal);
8a9562d2 1940 } else {
6b094bf4 1941 int dropped;
64839cf4 1942 int cnt = batch->count;
2391135c 1943 struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets;
8a9562d2 1944
7d6d1a40
WT
1945 dp_packet_batch_apply_cutlen(batch);
1946
2391135c 1947 cnt = netdev_dpdk_filter_packet_len(dev, pkts, cnt);
3e90f7d7 1948 cnt = netdev_dpdk_qos_run(dev, pkts, cnt, true);
6b094bf4 1949 dropped = batch->count - cnt;
1b99bb05 1950
819f13bd 1951 dropped += netdev_dpdk_eth_tx_burst(dev, qid, pkts, cnt);
8a9562d2 1952
f4fd623c 1953 if (OVS_UNLIKELY(dropped)) {
45d947c4 1954 rte_spinlock_lock(&dev->stats_lock);
f4fd623c 1955 dev->stats.tx_dropped += dropped;
45d947c4 1956 rte_spinlock_unlock(&dev->stats_lock);
f4fd623c 1957 }
8a9562d2 1958 }
a0cb2d66 1959
324c8374 1960 if (OVS_UNLIKELY(concurrent_txq)) {
a0cb2d66
DDP
1961 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1962 }
7251515e
DV
1963}
1964
1965static int
1966netdev_dpdk_eth_send(struct netdev *netdev, int qid,
324c8374
IM
1967 struct dp_packet_batch *batch, bool may_steal,
1968 bool concurrent_txq)
7251515e
DV
1969{
1970 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 1971
324c8374 1972 netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq);
7251515e 1973 return 0;
8a9562d2
PS
1974}
1975
1976static int
74ff3298 1977netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
8a9562d2
PS
1978{
1979 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1980
1981 ovs_mutex_lock(&dev->mutex);
1982 if (!eth_addr_equals(dev->hwaddr, mac)) {
74ff3298 1983 dev->hwaddr = mac;
045c0d1a 1984 netdev_change_seq_changed(netdev);
8a9562d2
PS
1985 }
1986 ovs_mutex_unlock(&dev->mutex);
1987
1988 return 0;
1989}
1990
1991static int
74ff3298 1992netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
8a9562d2
PS
1993{
1994 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1995
1996 ovs_mutex_lock(&dev->mutex);
74ff3298 1997 *mac = dev->hwaddr;
8a9562d2
PS
1998 ovs_mutex_unlock(&dev->mutex);
1999
2000 return 0;
2001}
2002
2003static int
2004netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
2005{
2006 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2007
2008 ovs_mutex_lock(&dev->mutex);
2009 *mtup = dev->mtu;
2010 ovs_mutex_unlock(&dev->mutex);
2011
2012 return 0;
2013}
2014
0072e931
MK
2015static int
2016netdev_dpdk_set_mtu(struct netdev *netdev, int mtu)
2017{
2018 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2019
2020 if (MTU_TO_FRAME_LEN(mtu) > NETDEV_DPDK_MAX_PKT_LEN
2021 || mtu < ETHER_MIN_MTU) {
2022 VLOG_WARN("%s: unsupported MTU %d\n", dev->up.name, mtu);
2023 return EINVAL;
2024 }
2025
2026 ovs_mutex_lock(&dev->mutex);
2027 if (dev->requested_mtu != mtu) {
2028 dev->requested_mtu = mtu;
2029 netdev_request_reconfigure(netdev);
2030 }
2031 ovs_mutex_unlock(&dev->mutex);
2032
2033 return 0;
2034}
2035
8a9562d2 2036static int
d46285a2 2037netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier);
8a9562d2 2038
58397e6c
KT
2039static int
2040netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
2041 struct netdev_stats *stats)
2042{
2043 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2044
2045 ovs_mutex_lock(&dev->mutex);
58397e6c 2046
45d947c4 2047 rte_spinlock_lock(&dev->stats_lock);
58397e6c 2048 /* Supported Stats */
50986e78 2049 stats->rx_packets = dev->stats.rx_packets;
2050 stats->tx_packets = dev->stats.tx_packets;
9509913a 2051 stats->rx_dropped = dev->stats.rx_dropped;
50986e78 2052 stats->tx_dropped = dev->stats.tx_dropped;
9e3ddd45
TP
2053 stats->multicast = dev->stats.multicast;
2054 stats->rx_bytes = dev->stats.rx_bytes;
2055 stats->tx_bytes = dev->stats.tx_bytes;
2056 stats->rx_errors = dev->stats.rx_errors;
2057 stats->rx_length_errors = dev->stats.rx_length_errors;
d6e3feb5 2058
2059 stats->rx_1_to_64_packets = dev->stats.rx_1_to_64_packets;
2060 stats->rx_65_to_127_packets = dev->stats.rx_65_to_127_packets;
2061 stats->rx_128_to_255_packets = dev->stats.rx_128_to_255_packets;
2062 stats->rx_256_to_511_packets = dev->stats.rx_256_to_511_packets;
2063 stats->rx_512_to_1023_packets = dev->stats.rx_512_to_1023_packets;
2064 stats->rx_1024_to_1522_packets = dev->stats.rx_1024_to_1522_packets;
2065 stats->rx_1523_to_max_packets = dev->stats.rx_1523_to_max_packets;
2066
45d947c4 2067 rte_spinlock_unlock(&dev->stats_lock);
9e3ddd45 2068
58397e6c
KT
2069 ovs_mutex_unlock(&dev->mutex);
2070
2071 return 0;
2072}
2073
d6e3feb5 2074static void
2075netdev_dpdk_convert_xstats(struct netdev_stats *stats,
0a0f39df
CL
2076 const struct rte_eth_xstat *xstats,
2077 const struct rte_eth_xstat_name *names,
d6e3feb5 2078 const unsigned int size)
2079{
d6e3feb5 2080 for (unsigned int i = 0; i < size; i++) {
0a0f39df 2081 if (strcmp(XSTAT_RX_64_PACKETS, names[i].name) == 0) {
d6e3feb5 2082 stats->rx_1_to_64_packets = xstats[i].value;
0a0f39df 2083 } else if (strcmp(XSTAT_RX_65_TO_127_PACKETS, names[i].name) == 0) {
d6e3feb5 2084 stats->rx_65_to_127_packets = xstats[i].value;
0a0f39df 2085 } else if (strcmp(XSTAT_RX_128_TO_255_PACKETS, names[i].name) == 0) {
d6e3feb5 2086 stats->rx_128_to_255_packets = xstats[i].value;
0a0f39df 2087 } else if (strcmp(XSTAT_RX_256_TO_511_PACKETS, names[i].name) == 0) {
d6e3feb5 2088 stats->rx_256_to_511_packets = xstats[i].value;
0a0f39df 2089 } else if (strcmp(XSTAT_RX_512_TO_1023_PACKETS, names[i].name) == 0) {
d6e3feb5 2090 stats->rx_512_to_1023_packets = xstats[i].value;
0a0f39df 2091 } else if (strcmp(XSTAT_RX_1024_TO_1522_PACKETS, names[i].name) == 0) {
d6e3feb5 2092 stats->rx_1024_to_1522_packets = xstats[i].value;
0a0f39df 2093 } else if (strcmp(XSTAT_RX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
d6e3feb5 2094 stats->rx_1523_to_max_packets = xstats[i].value;
0a0f39df 2095 } else if (strcmp(XSTAT_TX_64_PACKETS, names[i].name) == 0) {
d6e3feb5 2096 stats->tx_1_to_64_packets = xstats[i].value;
0a0f39df 2097 } else if (strcmp(XSTAT_TX_65_TO_127_PACKETS, names[i].name) == 0) {
d6e3feb5 2098 stats->tx_65_to_127_packets = xstats[i].value;
0a0f39df 2099 } else if (strcmp(XSTAT_TX_128_TO_255_PACKETS, names[i].name) == 0) {
d6e3feb5 2100 stats->tx_128_to_255_packets = xstats[i].value;
0a0f39df 2101 } else if (strcmp(XSTAT_TX_256_TO_511_PACKETS, names[i].name) == 0) {
d6e3feb5 2102 stats->tx_256_to_511_packets = xstats[i].value;
0a0f39df 2103 } else if (strcmp(XSTAT_TX_512_TO_1023_PACKETS, names[i].name) == 0) {
d6e3feb5 2104 stats->tx_512_to_1023_packets = xstats[i].value;
0a0f39df 2105 } else if (strcmp(XSTAT_TX_1024_TO_1522_PACKETS, names[i].name) == 0) {
d6e3feb5 2106 stats->tx_1024_to_1522_packets = xstats[i].value;
0a0f39df 2107 } else if (strcmp(XSTAT_TX_1523_TO_MAX_PACKETS, names[i].name) == 0) {
d6e3feb5 2108 stats->tx_1523_to_max_packets = xstats[i].value;
d57f777f
PS
2109 } else if (strcmp(XSTAT_RX_MULTICAST_PACKETS, names[i].name) == 0) {
2110 stats->multicast = xstats[i].value;
0a0f39df 2111 } else if (strcmp(XSTAT_TX_MULTICAST_PACKETS, names[i].name) == 0) {
d6e3feb5 2112 stats->tx_multicast_packets = xstats[i].value;
0a0f39df 2113 } else if (strcmp(XSTAT_RX_BROADCAST_PACKETS, names[i].name) == 0) {
d6e3feb5 2114 stats->rx_broadcast_packets = xstats[i].value;
0a0f39df 2115 } else if (strcmp(XSTAT_TX_BROADCAST_PACKETS, names[i].name) == 0) {
d6e3feb5 2116 stats->tx_broadcast_packets = xstats[i].value;
0a0f39df 2117 } else if (strcmp(XSTAT_RX_UNDERSIZED_ERRORS, names[i].name) == 0) {
d6e3feb5 2118 stats->rx_undersized_errors = xstats[i].value;
0a0f39df 2119 } else if (strcmp(XSTAT_RX_FRAGMENTED_ERRORS, names[i].name) == 0) {
d6e3feb5 2120 stats->rx_fragmented_errors = xstats[i].value;
0a0f39df 2121 } else if (strcmp(XSTAT_RX_JABBER_ERRORS, names[i].name) == 0) {
d6e3feb5 2122 stats->rx_jabber_errors = xstats[i].value;
2123 }
2124 }
2125}
2126
8a9562d2
PS
2127static int
2128netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
2129{
2130 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2131 struct rte_eth_stats rte_stats;
2132 bool gg;
2133
2134 netdev_dpdk_get_carrier(netdev, &gg);
2135 ovs_mutex_lock(&dev->mutex);
8a9562d2 2136
0a0f39df
CL
2137 struct rte_eth_xstat *rte_xstats = NULL;
2138 struct rte_eth_xstat_name *rte_xstats_names = NULL;
2139 int rte_xstats_len, rte_xstats_new_len, rte_xstats_ret;
d6e3feb5 2140
2141 if (rte_eth_stats_get(dev->port_id, &rte_stats)) {
bb37956a 2142 VLOG_ERR("Can't get ETH statistics for port: %"PRIu8, dev->port_id);
f9256822 2143 ovs_mutex_unlock(&dev->mutex);
d6e3feb5 2144 return EPROTO;
2145 }
2146
0a0f39df
CL
2147 /* Get length of statistics */
2148 rte_xstats_len = rte_eth_xstats_get_names(dev->port_id, NULL, 0);
2149 if (rte_xstats_len < 0) {
bb37956a 2150 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8, dev->port_id);
0a0f39df
CL
2151 goto out;
2152 }
2153 /* Reserve memory for xstats names and values */
2154 rte_xstats_names = xcalloc(rte_xstats_len, sizeof *rte_xstats_names);
2155 rte_xstats = xcalloc(rte_xstats_len, sizeof *rte_xstats);
2156
2157 /* Retreive xstats names */
2158 rte_xstats_new_len = rte_eth_xstats_get_names(dev->port_id,
2159 rte_xstats_names,
2160 rte_xstats_len);
2161 if (rte_xstats_new_len != rte_xstats_len) {
bb37956a 2162 VLOG_WARN("Cannot get XSTATS names for port: %"PRIu8, dev->port_id);
0a0f39df
CL
2163 goto out;
2164 }
2165 /* Retreive xstats values */
2166 memset(rte_xstats, 0xff, sizeof *rte_xstats * rte_xstats_len);
2167 rte_xstats_ret = rte_eth_xstats_get(dev->port_id, rte_xstats,
2168 rte_xstats_len);
2169 if (rte_xstats_ret > 0 && rte_xstats_ret <= rte_xstats_len) {
2170 netdev_dpdk_convert_xstats(stats, rte_xstats, rte_xstats_names,
2171 rte_xstats_len);
d6e3feb5 2172 } else {
bb37956a 2173 VLOG_WARN("Cannot get XSTATS values for port: %"PRIu8, dev->port_id);
d6e3feb5 2174 }
8a9562d2 2175
0a0f39df
CL
2176out:
2177 free(rte_xstats);
2178 free(rte_xstats_names);
2179
2f9dd77f
PS
2180 stats->rx_packets = rte_stats.ipackets;
2181 stats->tx_packets = rte_stats.opackets;
2182 stats->rx_bytes = rte_stats.ibytes;
2183 stats->tx_bytes = rte_stats.obytes;
21e9844c 2184 stats->rx_errors = rte_stats.ierrors;
2f9dd77f 2185 stats->tx_errors = rte_stats.oerrors;
8a9562d2 2186
45d947c4 2187 rte_spinlock_lock(&dev->stats_lock);
2f9dd77f 2188 stats->tx_dropped = dev->stats.tx_dropped;
9509913a 2189 stats->rx_dropped = dev->stats.rx_dropped;
45d947c4 2190 rte_spinlock_unlock(&dev->stats_lock);
9e3ddd45
TP
2191
2192 /* These are the available DPDK counters for packets not received due to
2193 * local resource constraints in DPDK and NIC respectively. */
9509913a 2194 stats->rx_dropped += rte_stats.rx_nombuf + rte_stats.imissed;
9e3ddd45
TP
2195 stats->rx_missed_errors = rte_stats.imissed;
2196
8a9562d2
PS
2197 ovs_mutex_unlock(&dev->mutex);
2198
2199 return 0;
2200}
2201
2202static int
d46285a2 2203netdev_dpdk_get_features(const struct netdev *netdev,
8a9562d2 2204 enum netdev_features *current,
ca3d4f55
BX
2205 enum netdev_features *advertised,
2206 enum netdev_features *supported,
2207 enum netdev_features *peer)
8a9562d2 2208{
d46285a2 2209 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2210 struct rte_eth_link link;
2211
2212 ovs_mutex_lock(&dev->mutex);
2213 link = dev->link;
2214 ovs_mutex_unlock(&dev->mutex);
2215
362ca396 2216 if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
2217 if (link.link_speed == ETH_SPEED_NUM_10M) {
8a9562d2
PS
2218 *current = NETDEV_F_10MB_HD;
2219 }
362ca396 2220 if (link.link_speed == ETH_SPEED_NUM_100M) {
8a9562d2
PS
2221 *current = NETDEV_F_100MB_HD;
2222 }
362ca396 2223 if (link.link_speed == ETH_SPEED_NUM_1G) {
8a9562d2
PS
2224 *current = NETDEV_F_1GB_HD;
2225 }
2226 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
362ca396 2227 if (link.link_speed == ETH_SPEED_NUM_10M) {
8a9562d2
PS
2228 *current = NETDEV_F_10MB_FD;
2229 }
362ca396 2230 if (link.link_speed == ETH_SPEED_NUM_100M) {
8a9562d2
PS
2231 *current = NETDEV_F_100MB_FD;
2232 }
362ca396 2233 if (link.link_speed == ETH_SPEED_NUM_1G) {
8a9562d2
PS
2234 *current = NETDEV_F_1GB_FD;
2235 }
362ca396 2236 if (link.link_speed == ETH_SPEED_NUM_10G) {
8a9562d2
PS
2237 *current = NETDEV_F_10GB_FD;
2238 }
2239 }
2240
362ca396 2241 if (link.link_autoneg) {
2242 *current |= NETDEV_F_AUTONEG;
2243 }
2244
ca3d4f55
BX
2245 *advertised = *supported = *peer = 0;
2246
8a9562d2
PS
2247 return 0;
2248}
2249
9509913a
IS
2250static struct ingress_policer *
2251netdev_dpdk_policer_construct(uint32_t rate, uint32_t burst)
2252{
2253 struct ingress_policer *policer = NULL;
2254 uint64_t rate_bytes;
2255 uint64_t burst_bytes;
2256 int err = 0;
2257
2258 policer = xmalloc(sizeof *policer);
2259 rte_spinlock_init(&policer->policer_lock);
2260
2261 /* rte_meter requires bytes so convert kbits rate and burst to bytes. */
602c8668
LR
2262 rate_bytes = rate * 1000ULL / 8;
2263 burst_bytes = burst * 1000ULL / 8;
9509913a
IS
2264
2265 policer->app_srtcm_params.cir = rate_bytes;
2266 policer->app_srtcm_params.cbs = burst_bytes;
2267 policer->app_srtcm_params.ebs = 0;
2268 err = rte_meter_srtcm_config(&policer->in_policer,
2269 &policer->app_srtcm_params);
58be5c0e 2270 if (err) {
9509913a
IS
2271 VLOG_ERR("Could not create rte meter for ingress policer");
2272 return NULL;
2273 }
2274
2275 return policer;
2276}
2277
2278static int
2279netdev_dpdk_set_policing(struct netdev* netdev, uint32_t policer_rate,
2280 uint32_t policer_burst)
2281{
2282 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2283 struct ingress_policer *policer;
2284
2285 /* Force to 0 if no rate specified,
2286 * default to 8000 kbits if burst is 0,
2287 * else stick with user-specified value.
2288 */
2289 policer_burst = (!policer_rate ? 0
2290 : !policer_burst ? 8000
2291 : policer_burst);
2292
2293 ovs_mutex_lock(&dev->mutex);
2294
2295 policer = ovsrcu_get_protected(struct ingress_policer *,
2296 &dev->ingress_policer);
2297
2298 if (dev->policer_rate == policer_rate &&
2299 dev->policer_burst == policer_burst) {
2300 /* Assume that settings haven't changed since we last set them. */
2301 ovs_mutex_unlock(&dev->mutex);
2302 return 0;
2303 }
2304
2305 /* Destroy any existing ingress policer for the device if one exists */
2306 if (policer) {
2307 ovsrcu_postpone(free, policer);
2308 }
2309
2310 if (policer_rate != 0) {
2311 policer = netdev_dpdk_policer_construct(policer_rate, policer_burst);
2312 } else {
2313 policer = NULL;
2314 }
2315 ovsrcu_set(&dev->ingress_policer, policer);
2316 dev->policer_rate = policer_rate;
2317 dev->policer_burst = policer_burst;
2318 ovs_mutex_unlock(&dev->mutex);
2319
2320 return 0;
2321}
2322
8a9562d2
PS
2323static int
2324netdev_dpdk_get_ifindex(const struct netdev *netdev)
2325{
2326 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2327
2328 ovs_mutex_lock(&dev->mutex);
12d0d124
PL
2329 /* Calculate hash from the netdev name. Ensure that ifindex is a 24-bit
2330 * postive integer to meet RFC 2863 recommendations.
2331 */
2332 int ifindex = hash_string(netdev->name, 0) % 0xfffffe + 1;
8a9562d2
PS
2333 ovs_mutex_unlock(&dev->mutex);
2334
2335 return ifindex;
2336}
2337
2338static int
d46285a2 2339netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier)
8a9562d2 2340{
d46285a2 2341 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2342
2343 ovs_mutex_lock(&dev->mutex);
2344 check_link_status(dev);
2345 *carrier = dev->link.link_status;
58397e6c
KT
2346
2347 ovs_mutex_unlock(&dev->mutex);
2348
2349 return 0;
2350}
2351
2352static int
d46285a2 2353netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier)
58397e6c 2354{
d46285a2 2355 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
58397e6c
KT
2356
2357 ovs_mutex_lock(&dev->mutex);
2358
0a0f39df 2359 if (is_vhost_running(dev)) {
58397e6c
KT
2360 *carrier = 1;
2361 } else {
2362 *carrier = 0;
2363 }
2364
8a9562d2
PS
2365 ovs_mutex_unlock(&dev->mutex);
2366
2367 return 0;
2368}
2369
2370static long long int
d46285a2 2371netdev_dpdk_get_carrier_resets(const struct netdev *netdev)
8a9562d2 2372{
d46285a2 2373 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2374 long long int carrier_resets;
2375
2376 ovs_mutex_lock(&dev->mutex);
2377 carrier_resets = dev->link_reset_cnt;
2378 ovs_mutex_unlock(&dev->mutex);
2379
2380 return carrier_resets;
2381}
2382
2383static int
d46285a2 2384netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED,
8a9562d2
PS
2385 long long int interval OVS_UNUSED)
2386{
ee32150e 2387 return EOPNOTSUPP;
8a9562d2
PS
2388}
2389
2390static int
2391netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
2392 enum netdev_flags off, enum netdev_flags on,
64839cf4
WT
2393 enum netdev_flags *old_flagsp)
2394 OVS_REQUIRES(dev->mutex)
8a9562d2 2395{
8a9562d2
PS
2396 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
2397 return EINVAL;
2398 }
2399
2400 *old_flagsp = dev->flags;
2401 dev->flags |= on;
2402 dev->flags &= ~off;
2403
2404 if (dev->flags == *old_flagsp) {
2405 return 0;
2406 }
2407
58397e6c 2408 if (dev->type == DPDK_DEV_ETH) {
58397e6c
KT
2409 if (dev->flags & NETDEV_PROMISC) {
2410 rte_eth_promiscuous_enable(dev->port_id);
2411 }
8a9562d2 2412
314fb5ad 2413 netdev_change_seq_changed(&dev->up);
e543851d
ZB
2414 } else {
2415 /* If DPDK_DEV_VHOST device's NETDEV_UP flag was changed and vhost is
2416 * running then change netdev's change_seq to trigger link state
2417 * update. */
e543851d
ZB
2418
2419 if ((NETDEV_UP & ((*old_flagsp ^ on) | (*old_flagsp ^ off)))
0a0f39df 2420 && is_vhost_running(dev)) {
e543851d
ZB
2421 netdev_change_seq_changed(&dev->up);
2422
2423 /* Clear statistics if device is getting up. */
2424 if (NETDEV_UP & on) {
2425 rte_spinlock_lock(&dev->stats_lock);
58be5c0e 2426 memset(&dev->stats, 0, sizeof dev->stats);
e543851d
ZB
2427 rte_spinlock_unlock(&dev->stats_lock);
2428 }
2429 }
8a9562d2
PS
2430 }
2431
2432 return 0;
2433}
2434
2435static int
d46285a2 2436netdev_dpdk_update_flags(struct netdev *netdev,
8a9562d2
PS
2437 enum netdev_flags off, enum netdev_flags on,
2438 enum netdev_flags *old_flagsp)
2439{
d46285a2 2440 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2441 int error;
2442
d46285a2
DDP
2443 ovs_mutex_lock(&dev->mutex);
2444 error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp);
2445 ovs_mutex_unlock(&dev->mutex);
8a9562d2
PS
2446
2447 return error;
2448}
2449
2450static int
d46285a2 2451netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args)
8a9562d2 2452{
d46285a2 2453 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2
PS
2454 struct rte_eth_dev_info dev_info;
2455
7cd1261d 2456 if (!rte_eth_dev_is_valid_port(dev->port_id)) {
8a9562d2 2457 return ENODEV;
7cd1261d 2458 }
8a9562d2
PS
2459
2460 ovs_mutex_lock(&dev->mutex);
2461 rte_eth_dev_info_get(dev->port_id, &dev_info);
2462 ovs_mutex_unlock(&dev->mutex);
2463
95fb793a 2464 smap_add_format(args, "port_no", "%d", dev->port_id);
58be5c0e
MK
2465 smap_add_format(args, "numa_id", "%d",
2466 rte_eth_dev_socket_id(dev->port_id));
8a9562d2
PS
2467 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
2468 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
4be4d22c 2469 smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len);
8a9562d2
PS
2470 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
2471 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
2472 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
58be5c0e
MK
2473 smap_add_format(args, "max_hash_mac_addrs", "%u",
2474 dev_info.max_hash_mac_addrs);
8a9562d2
PS
2475 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
2476 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
2477
39c2baa9 2478 if (dev_info.pci_dev) {
2479 smap_add_format(args, "pci-vendor_id", "0x%u",
2480 dev_info.pci_dev->id.vendor_id);
2481 smap_add_format(args, "pci-device_id", "0x%x",
2482 dev_info.pci_dev->id.device_id);
2483 }
8a9562d2
PS
2484
2485 return 0;
2486}
2487
2488static void
2489netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
2490 OVS_REQUIRES(dev->mutex)
2491{
2492 enum netdev_flags old_flags;
2493
2494 if (admin_state) {
2495 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
2496 } else {
2497 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
2498 }
2499}
2500
2501static void
2502netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
2503 const char *argv[], void *aux OVS_UNUSED)
2504{
2505 bool up;
2506
2507 if (!strcasecmp(argv[argc - 1], "up")) {
2508 up = true;
2509 } else if ( !strcasecmp(argv[argc - 1], "down")) {
2510 up = false;
2511 } else {
2512 unixctl_command_reply_error(conn, "Invalid Admin State");
2513 return;
2514 }
2515
2516 if (argc > 2) {
2517 struct netdev *netdev = netdev_from_name(argv[1]);
2518 if (netdev && is_dpdk_class(netdev->netdev_class)) {
2519 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
2520
2521 ovs_mutex_lock(&dpdk_dev->mutex);
2522 netdev_dpdk_set_admin_state__(dpdk_dev, up);
2523 ovs_mutex_unlock(&dpdk_dev->mutex);
2524
2525 netdev_close(netdev);
2526 } else {
2527 unixctl_command_reply_error(conn, "Not a DPDK Interface");
2528 netdev_close(netdev);
2529 return;
2530 }
2531 } else {
2532 struct netdev_dpdk *netdev;
2533
2534 ovs_mutex_lock(&dpdk_mutex);
2535 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
2536 ovs_mutex_lock(&netdev->mutex);
2537 netdev_dpdk_set_admin_state__(netdev, up);
2538 ovs_mutex_unlock(&netdev->mutex);
2539 }
2540 ovs_mutex_unlock(&dpdk_mutex);
2541 }
2542 unixctl_command_reply(conn, "OK");
2543}
2544
0ee821c2
DB
2545static void
2546netdev_dpdk_detach(struct unixctl_conn *conn, int argc OVS_UNUSED,
2547 const char *argv[], void *aux OVS_UNUSED)
2548{
2549 int ret;
2550 char *response;
2551 uint8_t port_id;
2552 char devname[RTE_ETH_NAME_MAX_LEN];
2553 struct netdev_dpdk *dev;
2554
2555 ovs_mutex_lock(&dpdk_mutex);
2556
2557 if (!rte_eth_dev_count() || rte_eth_dev_get_port_by_name(argv[1],
2558 &port_id)) {
2559 response = xasprintf("Device '%s' not found in DPDK", argv[1]);
2560 goto error;
2561 }
2562
2563 dev = netdev_dpdk_lookup_by_port_id(port_id);
2564 if (dev) {
2565 response = xasprintf("Device '%s' is being used by interface '%s'. "
2566 "Remove it before detaching",
2567 argv[1], netdev_get_name(&dev->up));
2568 goto error;
2569 }
2570
2571 rte_eth_dev_close(port_id);
2572
2573 ret = rte_eth_dev_detach(port_id, devname);
2574 if (ret < 0) {
2575 response = xasprintf("Device '%s' can not be detached", argv[1]);
2576 goto error;
2577 }
2578
2579 response = xasprintf("Device '%s' has been detached", argv[1]);
2580
2581 ovs_mutex_unlock(&dpdk_mutex);
2582 unixctl_command_reply(conn, response);
2583 free(response);
2584 return;
2585
2586error:
2587 ovs_mutex_unlock(&dpdk_mutex);
2588 unixctl_command_reply_error(conn, response);
2589 free(response);
2590}
2591
58397e6c
KT
2592/*
2593 * Set virtqueue flags so that we do not receive interrupts.
2594 */
2595static void
0a0f39df 2596set_irq_status(int vid)
58397e6c 2597{
4573fbd3 2598 uint32_t i;
4573fbd3 2599
f3e7ec25
MW
2600 for (i = 0; i < rte_vhost_get_vring_num(vid); i++) {
2601 rte_vhost_enable_guest_notification(vid, i, 0);
4573fbd3
FL
2602 }
2603}
2604
585a5bea
IM
2605/*
2606 * Fixes mapping for vhost-user tx queues. Must be called after each
81acebda 2607 * enabling/disabling of queues and n_txq modifications.
585a5bea
IM
2608 */
2609static void
d46285a2
DDP
2610netdev_dpdk_remap_txqs(struct netdev_dpdk *dev)
2611 OVS_REQUIRES(dev->mutex)
585a5bea
IM
2612{
2613 int *enabled_queues, n_enabled = 0;
81acebda 2614 int i, k, total_txqs = dev->up.n_txq;
585a5bea 2615
eff23640 2616 enabled_queues = xcalloc(total_txqs, sizeof *enabled_queues);
585a5bea
IM
2617
2618 for (i = 0; i < total_txqs; i++) {
2619 /* Enabled queues always mapped to themselves. */
d46285a2 2620 if (dev->tx_q[i].map == i) {
585a5bea
IM
2621 enabled_queues[n_enabled++] = i;
2622 }
2623 }
2624
2625 if (n_enabled == 0 && total_txqs != 0) {
f3ea2ad2 2626 enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED;
585a5bea
IM
2627 n_enabled = 1;
2628 }
2629
2630 k = 0;
2631 for (i = 0; i < total_txqs; i++) {
d46285a2
DDP
2632 if (dev->tx_q[i].map != i) {
2633 dev->tx_q[i].map = enabled_queues[k];
585a5bea
IM
2634 k = (k + 1) % n_enabled;
2635 }
2636 }
2637
2d24d165 2638 VLOG_DBG("TX queue mapping for %s\n", dev->vhost_id);
585a5bea 2639 for (i = 0; i < total_txqs; i++) {
d46285a2 2640 VLOG_DBG("%2d --> %2d", i, dev->tx_q[i].map);
585a5bea
IM
2641 }
2642
eff23640 2643 free(enabled_queues);
585a5bea 2644}
4573fbd3 2645
58397e6c
KT
2646/*
2647 * A new virtio-net device is added to a vhost port.
2648 */
2649static int
0a0f39df 2650new_device(int vid)
58397e6c 2651{
d46285a2 2652 struct netdev_dpdk *dev;
58397e6c 2653 bool exists = false;
db8f13b0 2654 int newnode = 0;
0a0f39df
CL
2655 char ifname[IF_NAME_SZ];
2656
58be5c0e 2657 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
58397e6c
KT
2658
2659 ovs_mutex_lock(&dpdk_mutex);
2660 /* Add device to the vhost port with the same name as that passed down. */
d46285a2 2661 LIST_FOR_EACH(dev, list_node, &dpdk_list) {
c1ff66ac 2662 ovs_mutex_lock(&dev->mutex);
2d24d165 2663 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
f3e7ec25 2664 uint32_t qp_num = rte_vhost_get_vring_num(vid)/VIRTIO_QNUM;
db8f13b0
CL
2665
2666 /* Get NUMA information */
0a0f39df
CL
2667 newnode = rte_vhost_get_numa_node(vid);
2668 if (newnode == -1) {
5b9bf9e0 2669#ifdef VHOST_NUMA
db8f13b0 2670 VLOG_INFO("Error getting NUMA info for vHost Device '%s'",
0a0f39df 2671 ifname);
5b9bf9e0 2672#endif
db8f13b0 2673 newnode = dev->socket_id;
db8f13b0
CL
2674 }
2675
7f5f2bd0
IM
2676 if (dev->requested_n_txq != qp_num
2677 || dev->requested_n_rxq != qp_num
2678 || dev->requested_socket_id != newnode) {
2679 dev->requested_socket_id = newnode;
2680 dev->requested_n_rxq = qp_num;
2681 dev->requested_n_txq = qp_num;
2682 netdev_request_reconfigure(&dev->up);
2683 } else {
2684 /* Reconfiguration not required. */
2685 dev->vhost_reconfigured = true;
2686 }
81acebda 2687
0a0f39df 2688 ovsrcu_index_set(&dev->vid, vid);
81acebda
IM
2689 exists = true;
2690
58397e6c 2691 /* Disable notifications. */
0a0f39df 2692 set_irq_status(vid);
e543851d 2693 netdev_change_seq_changed(&dev->up);
d46285a2 2694 ovs_mutex_unlock(&dev->mutex);
58397e6c
KT
2695 break;
2696 }
c1ff66ac 2697 ovs_mutex_unlock(&dev->mutex);
58397e6c
KT
2698 }
2699 ovs_mutex_unlock(&dpdk_mutex);
2700
2701 if (!exists) {
0a0f39df 2702 VLOG_INFO("vHost Device '%s' can't be added - name not found", ifname);
58397e6c
KT
2703
2704 return -1;
2705 }
2706
0a0f39df
CL
2707 VLOG_INFO("vHost Device '%s' has been added on numa node %i",
2708 ifname, newnode);
2709
58397e6c
KT
2710 return 0;
2711}
2712
f3ea2ad2
IM
2713/* Clears mapping for all available queues of vhost interface. */
2714static void
2715netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev)
2716 OVS_REQUIRES(dev->mutex)
2717{
2718 int i;
2719
81acebda 2720 for (i = 0; i < dev->up.n_txq; i++) {
f3ea2ad2
IM
2721 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
2722 }
2723}
2724
58397e6c
KT
2725/*
2726 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2727 * flag to stop any more packets from being sent or received to/from a VM and
2728 * ensure all currently queued packets have been sent/received before removing
2729 * the device.
2730 */
2731static void
0a0f39df 2732destroy_device(int vid)
58397e6c 2733{
d46285a2 2734 struct netdev_dpdk *dev;
afee281f 2735 bool exists = false;
0a0f39df
CL
2736 char ifname[IF_NAME_SZ];
2737
58be5c0e 2738 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
58397e6c
KT
2739
2740 ovs_mutex_lock(&dpdk_mutex);
d46285a2 2741 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
0a0f39df 2742 if (netdev_dpdk_get_vid(dev) == vid) {
58397e6c 2743
d46285a2 2744 ovs_mutex_lock(&dev->mutex);
0a0f39df
CL
2745 dev->vhost_reconfigured = false;
2746 ovsrcu_index_set(&dev->vid, -1);
d46285a2 2747 netdev_dpdk_txq_map_clear(dev);
81acebda 2748
e543851d 2749 netdev_change_seq_changed(&dev->up);
d46285a2 2750 ovs_mutex_unlock(&dev->mutex);
81acebda 2751 exists = true;
afee281f 2752 break;
58397e6c
KT
2753 }
2754 }
afee281f 2755
58397e6c
KT
2756 ovs_mutex_unlock(&dpdk_mutex);
2757
0a0f39df 2758 if (exists) {
afee281f
KT
2759 /*
2760 * Wait for other threads to quiesce after setting the 'virtio_dev'
2761 * to NULL, before returning.
2762 */
2763 ovsrcu_synchronize();
2764 /*
2765 * As call to ovsrcu_synchronize() will end the quiescent state,
2766 * put thread back into quiescent state before returning.
2767 */
2768 ovsrcu_quiesce_start();
0a0f39df 2769 VLOG_INFO("vHost Device '%s' has been removed", ifname);
afee281f 2770 } else {
0a0f39df 2771 VLOG_INFO("vHost Device '%s' not found", ifname);
afee281f 2772 }
58397e6c
KT
2773}
2774
585a5bea 2775static int
0a0f39df 2776vring_state_changed(int vid, uint16_t queue_id, int enable)
585a5bea 2777{
d46285a2 2778 struct netdev_dpdk *dev;
585a5bea
IM
2779 bool exists = false;
2780 int qid = queue_id / VIRTIO_QNUM;
0a0f39df
CL
2781 char ifname[IF_NAME_SZ];
2782
58be5c0e 2783 rte_vhost_get_ifname(vid, ifname, sizeof ifname);
585a5bea
IM
2784
2785 if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) {
2786 return 0;
2787 }
2788
2789 ovs_mutex_lock(&dpdk_mutex);
d46285a2 2790 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
c1ff66ac 2791 ovs_mutex_lock(&dev->mutex);
2d24d165 2792 if (strncmp(ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
585a5bea 2793 if (enable) {
d46285a2 2794 dev->tx_q[qid].map = qid;
585a5bea 2795 } else {
d46285a2 2796 dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED;
585a5bea 2797 }
d46285a2 2798 netdev_dpdk_remap_txqs(dev);
585a5bea 2799 exists = true;
d46285a2 2800 ovs_mutex_unlock(&dev->mutex);
585a5bea
IM
2801 break;
2802 }
c1ff66ac 2803 ovs_mutex_unlock(&dev->mutex);
585a5bea
IM
2804 }
2805 ovs_mutex_unlock(&dpdk_mutex);
2806
2807 if (exists) {
0a0f39df
CL
2808 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s'"
2809 "changed to \'%s\'", queue_id, qid, ifname,
d46285a2 2810 (enable == 1) ? "enabled" : "disabled");
585a5bea 2811 } else {
0a0f39df 2812 VLOG_INFO("vHost Device '%s' not found", ifname);
585a5bea
IM
2813 return -1;
2814 }
2815
2816 return 0;
2817}
2818
0a0f39df
CL
2819int
2820netdev_dpdk_get_vid(const struct netdev_dpdk *dev)
58397e6c 2821{
0a0f39df 2822 return ovsrcu_index_get(&dev->vid);
58397e6c
KT
2823}
2824
9509913a
IS
2825struct ingress_policer *
2826netdev_dpdk_get_ingress_policer(const struct netdev_dpdk *dev)
2827{
2828 return ovsrcu_get(struct ingress_policer *, &dev->ingress_policer);
2829}
2830
58397e6c 2831static int
ecc1a34e 2832netdev_dpdk_class_init(void)
7d1ced01 2833{
ecc1a34e
DDP
2834 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2835
2836 /* This function can be called for different classes. The initialization
2837 * needs to be done only once */
2838 if (ovsthread_once_start(&once)) {
2839 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
2840 unixctl_command_register("netdev-dpdk/set-admin-state",
2841 "[netdev] up|down", 1, 2,
2842 netdev_dpdk_set_admin_state, NULL);
2843
0ee821c2
DB
2844 unixctl_command_register("netdev-dpdk/detach",
2845 "pci address of device", 1, 1,
2846 netdev_dpdk_detach, NULL);
2847
ecc1a34e
DDP
2848 ovsthread_once_done(&once);
2849 }
362ca396 2850
7d1ced01
CL
2851 return 0;
2852}
2853
033e9df2 2854
95fb793a 2855/* Client Rings */
2856
95fb793a 2857static int
2858dpdk_ring_create(const char dev_name[], unsigned int port_no,
bb37956a 2859 dpdk_port_t *eth_port_id)
95fb793a 2860{
48fffdee 2861 struct dpdk_ring *ring_pair;
0c6f39e5 2862 char *ring_name;
b8374d0d 2863 int port_id;
95fb793a 2864
48fffdee
KT
2865 ring_pair = dpdk_rte_mzalloc(sizeof *ring_pair);
2866 if (!ring_pair) {
95fb793a 2867 return ENOMEM;
2868 }
2869
7251515e 2870 /* XXX: Add support for multiquque ring. */
0c6f39e5 2871 ring_name = xasprintf("%s_tx", dev_name);
95fb793a 2872
8f0a76c9 2873 /* Create single producer tx ring, netdev does explicit locking. */
48fffdee 2874 ring_pair->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
8f0a76c9 2875 RING_F_SP_ENQ);
0c6f39e5 2876 free(ring_name);
48fffdee
KT
2877 if (ring_pair->cring_tx == NULL) {
2878 rte_free(ring_pair);
95fb793a 2879 return ENOMEM;
2880 }
2881
0c6f39e5 2882 ring_name = xasprintf("%s_rx", dev_name);
95fb793a 2883
8f0a76c9 2884 /* Create single consumer rx ring, netdev does explicit locking. */
48fffdee 2885 ring_pair->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
8f0a76c9 2886 RING_F_SC_DEQ);
0c6f39e5 2887 free(ring_name);
48fffdee
KT
2888 if (ring_pair->cring_rx == NULL) {
2889 rte_free(ring_pair);
95fb793a 2890 return ENOMEM;
2891 }
2892
b8374d0d
MV
2893 port_id = rte_eth_from_rings(dev_name, &ring_pair->cring_rx, 1,
2894 &ring_pair->cring_tx, 1, SOCKET0);
d7310583 2895
b8374d0d 2896 if (port_id < 0) {
48fffdee 2897 rte_free(ring_pair);
95fb793a 2898 return ENODEV;
2899 }
2900
48fffdee 2901 ring_pair->user_port_id = port_no;
b8374d0d
MV
2902 ring_pair->eth_port_id = port_id;
2903 *eth_port_id = port_id;
2904
48fffdee 2905 ovs_list_push_back(&dpdk_ring_list, &ring_pair->list_node);
95fb793a 2906
95fb793a 2907 return 0;
2908}
2909
2910static int
bb37956a 2911dpdk_ring_open(const char dev_name[], dpdk_port_t *eth_port_id)
64839cf4 2912 OVS_REQUIRES(dpdk_mutex)
95fb793a 2913{
48fffdee 2914 struct dpdk_ring *ring_pair;
95fb793a 2915 unsigned int port_no;
2916 int err = 0;
2917
2918 /* Names always start with "dpdkr" */
2919 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
2920 if (err) {
2921 return err;
2922 }
2923
58be5c0e 2924 /* Look through our list to find the device */
48fffdee
KT
2925 LIST_FOR_EACH (ring_pair, list_node, &dpdk_ring_list) {
2926 if (ring_pair->user_port_id == port_no) {
58397e6c 2927 VLOG_INFO("Found dpdk ring device %s:", dev_name);
58be5c0e 2928 /* Really all that is needed */
48fffdee 2929 *eth_port_id = ring_pair->eth_port_id;
95fb793a 2930 return 0;
2931 }
2932 }
2933 /* Need to create the device rings */
2934 return dpdk_ring_create(dev_name, port_no, eth_port_id);
2935}
2936
7251515e 2937static int
d46285a2 2938netdev_dpdk_ring_send(struct netdev *netdev, int qid,
324c8374
IM
2939 struct dp_packet_batch *batch, bool may_steal,
2940 bool concurrent_txq)
7251515e 2941{
d46285a2 2942 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a543eb0 2943 struct dp_packet *packet;
1b99bb05 2944
58be5c0e
MK
2945 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that
2946 * the rss hash field is clear. This is because the same mbuf may be
2947 * modified by the consumer of the ring and return into the datapath
2948 * without recalculating the RSS hash. */
8a543eb0
BB
2949 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
2950 dp_packet_mbuf_rss_flag_reset(packet);
1b99bb05 2951 }
7251515e 2952
324c8374 2953 netdev_dpdk_send__(dev, qid, batch, may_steal, concurrent_txq);
7251515e
DV
2954 return 0;
2955}
2956
95fb793a 2957static int
2958netdev_dpdk_ring_construct(struct netdev *netdev)
2959{
bb37956a 2960 dpdk_port_t port_no = 0;
95fb793a 2961 int err = 0;
2962
95fb793a 2963 ovs_mutex_lock(&dpdk_mutex);
2964
2965 err = dpdk_ring_open(netdev->name, &port_no);
2966 if (err) {
2967 goto unlock_dpdk;
2968 }
2969
1ce30dfd
DDP
2970 err = common_construct(netdev, port_no, DPDK_DEV_ETH,
2971 rte_eth_dev_socket_id(port_no));
95fb793a 2972unlock_dpdk:
2973 ovs_mutex_unlock(&dpdk_mutex);
2974 return err;
2975}
2976
0bf765f7
IS
2977/* QoS Functions */
2978
2979/*
2980 * Initialize QoS configuration operations.
2981 */
2982static void
2983qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops)
2984{
2985 conf->ops = ops;
78bd47cf 2986 rte_spinlock_init(&conf->lock);
0bf765f7
IS
2987}
2988
2989/*
2990 * Search existing QoS operations in qos_ops and compare each set of
2991 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2992 * else return NULL
2993 */
2994static const struct dpdk_qos_ops *
2995qos_lookup_name(const char *name)
2996{
2997 const struct dpdk_qos_ops *const *opsp;
2998
2999 for (opsp = qos_confs; *opsp != NULL; opsp++) {
3000 const struct dpdk_qos_ops *ops = *opsp;
3001 if (!strcmp(name, ops->qos_name)) {
3002 return ops;
3003 }
3004 }
3005 return NULL;
3006}
3007
0bf765f7
IS
3008static int
3009netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED,
3010 struct sset *types)
3011{
3012 const struct dpdk_qos_ops *const *opsp;
3013
3014 for (opsp = qos_confs; *opsp != NULL; opsp++) {
3015 const struct dpdk_qos_ops *ops = *opsp;
3016 if (ops->qos_construct && ops->qos_name[0] != '\0') {
3017 sset_add(types, ops->qos_name);
3018 }
3019 }
3020 return 0;
3021}
3022
3023static int
d46285a2 3024netdev_dpdk_get_qos(const struct netdev *netdev,
0bf765f7
IS
3025 const char **typep, struct smap *details)
3026{
d46285a2 3027 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
78bd47cf 3028 struct qos_conf *qos_conf;
0bf765f7
IS
3029 int error = 0;
3030
d46285a2 3031 ovs_mutex_lock(&dev->mutex);
78bd47cf
DDP
3032 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
3033 if (qos_conf) {
3034 *typep = qos_conf->ops->qos_name;
3035 error = (qos_conf->ops->qos_get
3036 ? qos_conf->ops->qos_get(qos_conf, details): 0);
d03603c4
MC
3037 } else {
3038 /* No QoS configuration set, return an empty string */
3039 *typep = "";
0bf765f7 3040 }
d46285a2 3041 ovs_mutex_unlock(&dev->mutex);
0bf765f7
IS
3042
3043 return error;
3044}
3045
3046static int
78bd47cf
DDP
3047netdev_dpdk_set_qos(struct netdev *netdev, const char *type,
3048 const struct smap *details)
0bf765f7 3049{
d46285a2 3050 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
0bf765f7 3051 const struct dpdk_qos_ops *new_ops = NULL;
78bd47cf 3052 struct qos_conf *qos_conf, *new_qos_conf = NULL;
0bf765f7
IS
3053 int error = 0;
3054
d46285a2 3055 ovs_mutex_lock(&dev->mutex);
0bf765f7 3056
78bd47cf 3057 qos_conf = ovsrcu_get_protected(struct qos_conf *, &dev->qos_conf);
0bf765f7 3058
78bd47cf
DDP
3059 new_ops = qos_lookup_name(type);
3060
3061 if (!new_ops || !new_ops->qos_construct) {
3062 new_qos_conf = NULL;
3063 if (type && type[0]) {
3064 error = EOPNOTSUPP;
0bf765f7 3065 }
44975bb0 3066 } else if (qos_conf && qos_conf->ops == new_ops
78bd47cf
DDP
3067 && qos_conf->ops->qos_is_equal(qos_conf, details)) {
3068 new_qos_conf = qos_conf;
0bf765f7 3069 } else {
78bd47cf 3070 error = new_ops->qos_construct(details, &new_qos_conf);
7ea266e9
IS
3071 }
3072
7ea266e9 3073 if (error) {
78bd47cf
DDP
3074 VLOG_ERR("Failed to set QoS type %s on port %s: %s",
3075 type, netdev->name, rte_strerror(error));
3076 }
3077
3078 if (new_qos_conf != qos_conf) {
3079 ovsrcu_set(&dev->qos_conf, new_qos_conf);
3080 if (qos_conf) {
3081 ovsrcu_postpone(qos_conf->ops->qos_destruct, qos_conf);
3082 }
0bf765f7
IS
3083 }
3084
d46285a2 3085 ovs_mutex_unlock(&dev->mutex);
78bd47cf 3086
0bf765f7
IS
3087 return error;
3088}
3089
3090/* egress-policer details */
3091
3092struct egress_policer {
3093 struct qos_conf qos_conf;
3094 struct rte_meter_srtcm_params app_srtcm_params;
3095 struct rte_meter_srtcm egress_meter;
3096};
3097
78bd47cf
DDP
3098static void
3099egress_policer_details_to_param(const struct smap *details,
3100 struct rte_meter_srtcm_params *params)
0bf765f7 3101{
78bd47cf
DDP
3102 memset(params, 0, sizeof *params);
3103 params->cir = smap_get_ullong(details, "cir", 0);
3104 params->cbs = smap_get_ullong(details, "cbs", 0);
3105 params->ebs = 0;
0bf765f7
IS
3106}
3107
3108static int
78bd47cf
DDP
3109egress_policer_qos_construct(const struct smap *details,
3110 struct qos_conf **conf)
0bf765f7 3111{
0bf765f7 3112 struct egress_policer *policer;
0bf765f7
IS
3113 int err = 0;
3114
0bf765f7
IS
3115 policer = xmalloc(sizeof *policer);
3116 qos_conf_init(&policer->qos_conf, &egress_policer_ops);
78bd47cf 3117 egress_policer_details_to_param(details, &policer->app_srtcm_params);
0bf765f7 3118 err = rte_meter_srtcm_config(&policer->egress_meter,
78bd47cf
DDP
3119 &policer->app_srtcm_params);
3120 if (!err) {
3121 *conf = &policer->qos_conf;
3122 } else {
7ea266e9 3123 free(policer);
78bd47cf 3124 *conf = NULL;
7ea266e9
IS
3125 err = -err;
3126 }
0bf765f7
IS
3127
3128 return err;
3129}
3130
3131static void
78bd47cf 3132egress_policer_qos_destruct(struct qos_conf *conf)
0bf765f7
IS
3133{
3134 struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer,
78bd47cf 3135 qos_conf);
0bf765f7
IS
3136 free(policer);
3137}
3138
3139static int
78bd47cf 3140egress_policer_qos_get(const struct qos_conf *conf, struct smap *details)
0bf765f7 3141{
78bd47cf
DDP
3142 struct egress_policer *policer =
3143 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3144
3145 smap_add_format(details, "cir", "%"PRIu64, policer->app_srtcm_params.cir);
3146 smap_add_format(details, "cbs", "%"PRIu64, policer->app_srtcm_params.cbs);
050c60bf 3147
0bf765f7
IS
3148 return 0;
3149}
3150
78bd47cf 3151static bool
47a45d86
KT
3152egress_policer_qos_is_equal(const struct qos_conf *conf,
3153 const struct smap *details)
0bf765f7 3154{
78bd47cf
DDP
3155 struct egress_policer *policer =
3156 CONTAINER_OF(conf, struct egress_policer, qos_conf);
3157 struct rte_meter_srtcm_params params;
0bf765f7 3158
78bd47cf 3159 egress_policer_details_to_param(details, &params);
7ea266e9 3160
78bd47cf 3161 return !memcmp(&params, &policer->app_srtcm_params, sizeof params);
0bf765f7
IS
3162}
3163
0bf765f7 3164static int
3e90f7d7
GZ
3165egress_policer_run(struct qos_conf *conf, struct rte_mbuf **pkts, int pkt_cnt,
3166 bool may_steal)
0bf765f7 3167{
0bf765f7 3168 int cnt = 0;
78bd47cf
DDP
3169 struct egress_policer *policer =
3170 CONTAINER_OF(conf, struct egress_policer, qos_conf);
0bf765f7 3171
3e90f7d7
GZ
3172 cnt = netdev_dpdk_policer_run(&policer->egress_meter, pkts,
3173 pkt_cnt, may_steal);
0bf765f7
IS
3174
3175 return cnt;
3176}
3177
3178static const struct dpdk_qos_ops egress_policer_ops = {
3179 "egress-policer", /* qos_name */
3180 egress_policer_qos_construct,
3181 egress_policer_qos_destruct,
3182 egress_policer_qos_get,
78bd47cf 3183 egress_policer_qos_is_equal,
0bf765f7
IS
3184 egress_policer_run
3185};
3186
050c60bf
DDP
3187static int
3188netdev_dpdk_reconfigure(struct netdev *netdev)
3189{
3190 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
3191 int err = 0;
3192
050c60bf
DDP
3193 ovs_mutex_lock(&dev->mutex);
3194
3195 if (netdev->n_txq == dev->requested_n_txq
0072e931 3196 && netdev->n_rxq == dev->requested_n_rxq
b685696b
CL
3197 && dev->mtu == dev->requested_mtu
3198 && dev->rxq_size == dev->requested_rxq_size
bd4e172b 3199 && dev->txq_size == dev->requested_txq_size
3200 && dev->socket_id == dev->requested_socket_id) {
050c60bf
DDP
3201 /* Reconfiguration is unnecessary */
3202
3203 goto out;
3204 }
3205
3206 rte_eth_dev_stop(dev->port_id);
3207
d555d9bd
RW
3208 err = netdev_dpdk_mempool_configure(dev);
3209 if (err) {
3210 goto out;
0072e931
MK
3211 }
3212
050c60bf
DDP
3213 netdev->n_txq = dev->requested_n_txq;
3214 netdev->n_rxq = dev->requested_n_rxq;
3215
b685696b
CL
3216 dev->rxq_size = dev->requested_rxq_size;
3217 dev->txq_size = dev->requested_txq_size;
3218
050c60bf
DDP
3219 rte_free(dev->tx_q);
3220 err = dpdk_eth_dev_init(dev);
eff23640
DDP
3221 dev->tx_q = netdev_dpdk_alloc_txq(netdev->n_txq);
3222 if (!dev->tx_q) {
3223 err = ENOMEM;
3224 }
050c60bf 3225
0072e931
MK
3226 netdev_change_seq_changed(netdev);
3227
050c60bf 3228out:
050c60bf 3229 ovs_mutex_unlock(&dev->mutex);
050c60bf
DDP
3230 return err;
3231}
3232
7f381c2e 3233static int
2d24d165 3234dpdk_vhost_reconfigure_helper(struct netdev_dpdk *dev)
2d24d165 3235 OVS_REQUIRES(dev->mutex)
050c60bf 3236{
2d24d165
CL
3237 dev->up.n_txq = dev->requested_n_txq;
3238 dev->up.n_rxq = dev->requested_n_rxq;
96e9b168 3239 int err;
050c60bf 3240
81acebda
IM
3241 /* Enable TX queue 0 by default if it wasn't disabled. */
3242 if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) {
3243 dev->tx_q[0].map = 0;
3244 }
3245
3246 netdev_dpdk_remap_txqs(dev);
3247
d555d9bd
RW
3248 err = netdev_dpdk_mempool_configure(dev);
3249 if (err) {
3250 return err;
3251 } else {
3252 netdev_change_seq_changed(&dev->up);
db8f13b0
CL
3253 }
3254
0a0f39df 3255 if (netdev_dpdk_get_vid(dev) >= 0) {
894af647 3256 if (dev->vhost_reconfigured == false) {
3257 dev->vhost_reconfigured = true;
3258 /* Carrier status may need updating. */
3259 netdev_change_seq_changed(&dev->up);
3260 }
81acebda 3261 }
7f381c2e
DDP
3262
3263 return 0;
2d24d165
CL
3264}
3265
3266static int
3267netdev_dpdk_vhost_reconfigure(struct netdev *netdev)
3268{
3269 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
7f381c2e 3270 int err;
2d24d165 3271
2d24d165 3272 ovs_mutex_lock(&dev->mutex);
7f381c2e 3273 err = dpdk_vhost_reconfigure_helper(dev);
2d24d165 3274 ovs_mutex_unlock(&dev->mutex);
7f381c2e
DDP
3275
3276 return err;
2d24d165
CL
3277}
3278
3279static int
3280netdev_dpdk_vhost_client_reconfigure(struct netdev *netdev)
3281{
3282 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
7f381c2e 3283 int err;
2d24d165 3284
2d24d165
CL
3285 ovs_mutex_lock(&dev->mutex);
3286
c1ff66ac
CL
3287 /* Configure vHost client mode if requested and if the following criteria
3288 * are met:
2d24d165
CL
3289 * 1. Device hasn't been registered yet.
3290 * 2. A path has been specified.
c1ff66ac
CL
3291 */
3292 if (!(dev->vhost_driver_flags & RTE_VHOST_USER_CLIENT)
2d24d165
CL
3293 && strlen(dev->vhost_id)) {
3294 /* Register client-mode device */
3295 err = rte_vhost_driver_register(dev->vhost_id,
3296 RTE_VHOST_USER_CLIENT);
c1ff66ac 3297 if (err) {
2d24d165
CL
3298 VLOG_ERR("vhost-user device setup failure for device %s\n",
3299 dev->vhost_id);
7f381c2e 3300 goto unlock;
c1ff66ac 3301 } else {
2d24d165
CL
3302 /* Configuration successful */
3303 dev->vhost_driver_flags |= RTE_VHOST_USER_CLIENT;
3304 VLOG_INFO("vHost User device '%s' created in 'client' mode, "
3305 "using client socket '%s'",
3306 dev->up.name, dev->vhost_id);
c1ff66ac 3307 }
f3e7ec25
MW
3308
3309 err = rte_vhost_driver_callback_register(dev->vhost_id,
3310 &virtio_net_device_ops);
3311 if (err) {
3312 VLOG_ERR("rte_vhost_driver_callback_register failed for "
3313 "vhost user client port: %s\n", dev->up.name);
3314 goto unlock;
3315 }
3316
3317 err = rte_vhost_driver_disable_features(dev->vhost_id,
3318 1ULL << VIRTIO_NET_F_HOST_TSO4
3319 | 1ULL << VIRTIO_NET_F_HOST_TSO6
3320 | 1ULL << VIRTIO_NET_F_CSUM);
3321 if (err) {
3322 VLOG_ERR("rte_vhost_driver_disable_features failed for vhost user "
3323 "client port: %s\n", dev->up.name);
3324 goto unlock;
3325 }
3326
3327 err = rte_vhost_driver_start(dev->vhost_id);
3328 if (err) {
3329 VLOG_ERR("rte_vhost_driver_start failed for vhost user "
3330 "client port: %s\n", dev->up.name);
3331 goto unlock;
3332 }
c1ff66ac
CL
3333 }
3334
7f381c2e
DDP
3335 err = dpdk_vhost_reconfigure_helper(dev);
3336
3337unlock:
050c60bf 3338 ovs_mutex_unlock(&dev->mutex);
050c60bf 3339
7f381c2e 3340 return err;
050c60bf
DDP
3341}
3342
ecc1a34e 3343#define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, \
81acebda
IM
3344 SET_CONFIG, SET_TX_MULTIQ, SEND, \
3345 GET_CARRIER, GET_STATS, \
3346 GET_FEATURES, GET_STATUS, \
3347 RECONFIGURE, RXQ_RECV) \
95fb793a 3348{ \
3349 NAME, \
118c77b1 3350 true, /* is_pmd */ \
ecc1a34e 3351 INIT, /* init */ \
95fb793a 3352 NULL, /* netdev_dpdk_run */ \
3353 NULL, /* netdev_dpdk_wait */ \
3354 \
3355 netdev_dpdk_alloc, \
3356 CONSTRUCT, \
58397e6c 3357 DESTRUCT, \
95fb793a 3358 netdev_dpdk_dealloc, \
3359 netdev_dpdk_get_config, \
81acebda 3360 SET_CONFIG, \
95fb793a 3361 NULL, /* get_tunnel_config */ \
58397e6c
KT
3362 NULL, /* build header */ \
3363 NULL, /* push header */ \
3364 NULL, /* pop header */ \
7dec44fe 3365 netdev_dpdk_get_numa_id, /* get_numa_id */ \
81acebda 3366 SET_TX_MULTIQ, \
95fb793a 3367 \
7251515e 3368 SEND, /* send */ \
95fb793a 3369 NULL, /* send_wait */ \
3370 \
3371 netdev_dpdk_set_etheraddr, \
3372 netdev_dpdk_get_etheraddr, \
3373 netdev_dpdk_get_mtu, \
0072e931 3374 netdev_dpdk_set_mtu, \
95fb793a 3375 netdev_dpdk_get_ifindex, \
58397e6c 3376 GET_CARRIER, \
95fb793a 3377 netdev_dpdk_get_carrier_resets, \
3378 netdev_dpdk_set_miimon, \
58397e6c
KT
3379 GET_STATS, \
3380 GET_FEATURES, \
95fb793a 3381 NULL, /* set_advertisements */ \
875ab130 3382 NULL, /* get_pt_mode */ \
95fb793a 3383 \
9509913a 3384 netdev_dpdk_set_policing, \
0bf765f7 3385 netdev_dpdk_get_qos_types, \
95fb793a 3386 NULL, /* get_qos_capabilities */ \
0bf765f7
IS
3387 netdev_dpdk_get_qos, \
3388 netdev_dpdk_set_qos, \
95fb793a 3389 NULL, /* get_queue */ \
3390 NULL, /* set_queue */ \
3391 NULL, /* delete_queue */ \
3392 NULL, /* get_queue_stats */ \
3393 NULL, /* queue_dump_start */ \
3394 NULL, /* queue_dump_next */ \
3395 NULL, /* queue_dump_done */ \
3396 NULL, /* dump_queue_stats */ \
3397 \
95fb793a 3398 NULL, /* set_in4 */ \
a8704b50 3399 NULL, /* get_addr_list */ \
95fb793a 3400 NULL, /* add_router */ \
3401 NULL, /* get_next_hop */ \
58397e6c 3402 GET_STATUS, \
95fb793a 3403 NULL, /* arp_lookup */ \
3404 \
3405 netdev_dpdk_update_flags, \
050c60bf 3406 RECONFIGURE, \
95fb793a 3407 \
3408 netdev_dpdk_rxq_alloc, \
3409 netdev_dpdk_rxq_construct, \
3410 netdev_dpdk_rxq_destruct, \
3411 netdev_dpdk_rxq_dealloc, \
58397e6c 3412 RXQ_RECV, \
95fb793a 3413 NULL, /* rx_wait */ \
3414 NULL, /* rxq_drain */ \
18ebd48c 3415 NO_OFFLOAD_API \
95fb793a 3416}
8a9562d2 3417
bce01e3a 3418static const struct netdev_class dpdk_class =
95fb793a 3419 NETDEV_DPDK_CLASS(
3420 "dpdk",
ecc1a34e 3421 netdev_dpdk_class_init,
5496878c 3422 netdev_dpdk_construct,
58397e6c 3423 netdev_dpdk_destruct,
81acebda
IM
3424 netdev_dpdk_set_config,
3425 netdev_dpdk_set_tx_multiq,
58397e6c
KT
3426 netdev_dpdk_eth_send,
3427 netdev_dpdk_get_carrier,
3428 netdev_dpdk_get_stats,
3429 netdev_dpdk_get_features,
3430 netdev_dpdk_get_status,
050c60bf 3431 netdev_dpdk_reconfigure,
58397e6c 3432 netdev_dpdk_rxq_recv);
95fb793a 3433
bce01e3a 3434static const struct netdev_class dpdk_ring_class =
95fb793a 3435 NETDEV_DPDK_CLASS(
3436 "dpdkr",
ecc1a34e 3437 netdev_dpdk_class_init,
5496878c 3438 netdev_dpdk_ring_construct,
58397e6c 3439 netdev_dpdk_destruct,
c3d062a7 3440 netdev_dpdk_ring_set_config,
81acebda 3441 netdev_dpdk_set_tx_multiq,
58397e6c
KT
3442 netdev_dpdk_ring_send,
3443 netdev_dpdk_get_carrier,
3444 netdev_dpdk_get_stats,
3445 netdev_dpdk_get_features,
3446 netdev_dpdk_get_status,
050c60bf 3447 netdev_dpdk_reconfigure,
58397e6c
KT
3448 netdev_dpdk_rxq_recv);
3449
53f50d24 3450static const struct netdev_class dpdk_vhost_class =
7d1ced01
CL
3451 NETDEV_DPDK_CLASS(
3452 "dpdkvhostuser",
f3e7ec25 3453 NULL,
53f50d24 3454 netdev_dpdk_vhost_construct,
58397e6c 3455 netdev_dpdk_vhost_destruct,
2d24d165 3456 NULL,
81acebda 3457 NULL,
58397e6c
KT
3458 netdev_dpdk_vhost_send,
3459 netdev_dpdk_vhost_get_carrier,
3460 netdev_dpdk_vhost_get_stats,
3461 NULL,
7251515e 3462 NULL,
53f50d24 3463 netdev_dpdk_vhost_reconfigure,
58397e6c 3464 netdev_dpdk_vhost_rxq_recv);
2d24d165
CL
3465static const struct netdev_class dpdk_vhost_client_class =
3466 NETDEV_DPDK_CLASS(
3467 "dpdkvhostuserclient",
f3e7ec25 3468 NULL,
2d24d165
CL
3469 netdev_dpdk_vhost_client_construct,
3470 netdev_dpdk_vhost_destruct,
3471 netdev_dpdk_vhost_client_set_config,
3472 NULL,
3473 netdev_dpdk_vhost_send,
3474 netdev_dpdk_vhost_get_carrier,
3475 netdev_dpdk_vhost_get_stats,
3476 NULL,
3477 NULL,
3478 netdev_dpdk_vhost_client_reconfigure,
3479 netdev_dpdk_vhost_rxq_recv);
95fb793a 3480
8a9562d2
PS
3481void
3482netdev_dpdk_register(void)
3483{
bab69409
AC
3484 netdev_register_provider(&dpdk_class);
3485 netdev_register_provider(&dpdk_ring_class);
53f50d24 3486 netdev_register_provider(&dpdk_vhost_class);
2d24d165 3487 netdev_register_provider(&dpdk_vhost_client_class);
8a9562d2 3488}