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