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