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