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