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