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