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