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