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