]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-dpdk.c
netdev-dpdk: Add vhost enqueue retries.
[mirror_ovs.git] / lib / netdev-dpdk.c
CommitLineData
8a9562d2
PS
1/*
2 * Copyright (c) 2014 Nicira, Inc.
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
19#include <stdio.h>
20#include <string.h>
21#include <signal.h>
22#include <stdlib.h>
23#include <pthread.h>
24#include <config.h>
25#include <errno.h>
26#include <sched.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <stdio.h>
30
e14deea0 31#include "dp-packet.h"
8a9562d2
PS
32#include "dpif-netdev.h"
33#include "list.h"
34#include "netdev-dpdk.h"
35#include "netdev-provider.h"
36#include "netdev-vport.h"
37#include "odp-util.h"
38#include "ofp-print.h"
94143fc4 39#include "ovs-numa.h"
8a9562d2
PS
40#include "ovs-thread.h"
41#include "ovs-rcu.h"
42#include "packets.h"
43#include "shash.h"
8a9562d2
PS
44#include "sset.h"
45#include "unaligned.h"
46#include "timeval.h"
47#include "unixctl.h"
e6211adc 48#include "openvswitch/vlog.h"
8a9562d2 49
b8e57534
MK
50#include "rte_config.h"
51#include "rte_mbuf.h"
58397e6c 52#include "rte_virtio_net.h"
b8e57534 53
8a9562d2
PS
54VLOG_DEFINE_THIS_MODULE(dpdk);
55static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
56
57#define DPDK_PORT_WATCHDOG_INTERVAL 5
58
59#define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
60#define OVS_VPORT_DPDK "ovs_dpdk"
61
62/*
63 * need to reserve tons of extra space in the mbufs so we can align the
64 * DMA addresses to 4KB.
65 */
66
67#define MTU_TO_MAX_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
68#define MBUF_SIZE(mtu) (MTU_TO_MAX_LEN(mtu) + (512) + \
69 sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
70
da79ce2b
DDP
71/* Max and min number of packets in the mempool. OVS tries to allocate a
72 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
73 * enough hugepages) we keep halving the number until the allocation succeeds
74 * or we reach MIN_NB_MBUF */
75
76#define MAX_NB_MBUF (4096 * 64)
77#define MIN_NB_MBUF (4096 * 4)
78#define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
79
80/* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
81BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF) == 0);
82
83/* The smallest possible NB_MBUF that we're going to try should be a multiple
84 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
85BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF))
86 % MP_CACHE_SZ == 0);
87
8a9562d2
PS
88#define SOCKET0 0
89
79f5354c
PM
90#define NIC_PORT_RX_Q_SIZE 2048 /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
91#define NIC_PORT_TX_Q_SIZE 2048 /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
92
645b8934 93/* XXX: Needs per NIC value for these constants. */
8a9562d2
PS
94#define RX_PTHRESH 32 /* Default values of RX prefetch threshold reg. */
95#define RX_HTHRESH 32 /* Default values of RX host threshold reg. */
96#define RX_WTHRESH 16 /* Default values of RX write-back threshold reg. */
97
98#define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
99#define TX_HTHRESH 0 /* Default values of TX host threshold reg. */
100#define TX_WTHRESH 0 /* Default values of TX write-back threshold reg. */
101
58397e6c
KT
102#define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
103
104/* Character device cuse_dev_name. */
105char *cuse_dev_name = NULL;
106
95e9881f
KT
107/*
108 * Maximum amount of time in micro seconds to try and enqueue to vhost.
109 */
110#define VHOST_ENQ_RETRY_USECS 100
111
8a9562d2 112static const struct rte_eth_conf port_conf = {
a28ddd11
DDP
113 .rxmode = {
114 .mq_mode = ETH_MQ_RX_RSS,
115 .split_hdr_size = 0,
116 .header_split = 0, /* Header Split disabled */
117 .hw_ip_checksum = 0, /* IP checksum offload disabled */
118 .hw_vlan_filter = 0, /* VLAN filtering disabled */
119 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
120 .hw_strip_crc = 0,
121 },
122 .rx_adv_conf = {
123 .rss_conf = {
124 .rss_key = NULL,
543342a4 125 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP,
8a9562d2 126 },
a28ddd11
DDP
127 },
128 .txmode = {
129 .mq_mode = ETH_MQ_TX_NONE,
130 },
8a9562d2
PS
131};
132
133static const struct rte_eth_rxconf rx_conf = {
a28ddd11
DDP
134 .rx_thresh = {
135 .pthresh = RX_PTHRESH,
136 .hthresh = RX_HTHRESH,
137 .wthresh = RX_WTHRESH,
138 },
8a9562d2
PS
139};
140
141static const struct rte_eth_txconf tx_conf = {
a28ddd11
DDP
142 .tx_thresh = {
143 .pthresh = TX_PTHRESH,
144 .hthresh = TX_HTHRESH,
145 .wthresh = TX_WTHRESH,
146 },
147 .tx_free_thresh = 0,
148 .tx_rs_thresh = 0,
94777510 149 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS|ETH_TXQ_FLAGS_NOOFFLOADS,
8a9562d2
PS
150};
151
3a100265 152enum { MAX_TX_QUEUE_LEN = 384 };
58f7c37b
DDP
153enum { DPDK_RING_SIZE = 256 };
154BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
8a9562d2
PS
155enum { DRAIN_TSC = 200000ULL };
156
58397e6c
KT
157enum dpdk_dev_type {
158 DPDK_DEV_ETH = 0,
159 DPDK_DEV_VHOST = 1
160};
161
8a9562d2
PS
162static int rte_eal_init_ret = ENODEV;
163
164static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
165
166/* Contains all 'struct dpdk_dev's. */
ca6ba700 167static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
55951e15 168 = OVS_LIST_INITIALIZER(&dpdk_list);
8a9562d2 169
ca6ba700 170static struct ovs_list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
55951e15 171 = OVS_LIST_INITIALIZER(&dpdk_mp_list);
8a9562d2 172
db73f716
DDP
173/* This mutex must be used by non pmd threads when allocating or freeing
174 * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
175 * use mempools, a non pmd thread should hold this mutex while calling them */
176struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
177
8a9562d2
PS
178struct dpdk_mp {
179 struct rte_mempool *mp;
180 int mtu;
181 int socket_id;
182 int refcount;
ca6ba700 183 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
8a9562d2
PS
184};
185
5a034064
AW
186/* There should be one 'struct dpdk_tx_queue' created for
187 * each cpu core. */
8a9562d2 188struct dpdk_tx_queue {
94143fc4
AW
189 bool flush_tx; /* Set to true to flush queue everytime */
190 /* pkts are queued. */
8a9562d2
PS
191 int count;
192 uint64_t tsc;
193 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
194};
195
95fb793a 196/* dpdk has no way to remove dpdk ring ethernet devices
197 so we have to keep them around once they've been created
198*/
199
ca6ba700 200static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
55951e15 201 = OVS_LIST_INITIALIZER(&dpdk_ring_list);
95fb793a 202
203struct dpdk_ring {
204 /* For the client rings */
205 struct rte_ring *cring_tx;
206 struct rte_ring *cring_rx;
207 int user_port_id; /* User given port no, parsed from port name */
208 int eth_port_id; /* ethernet device port id */
ca6ba700 209 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
95fb793a 210};
211
8a9562d2
PS
212struct netdev_dpdk {
213 struct netdev up;
214 int port_id;
215 int max_packet_len;
58397e6c 216 enum dpdk_dev_type type;
8a9562d2 217
5a034064 218 struct dpdk_tx_queue *tx_q;
8a9562d2
PS
219
220 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
221
222 struct dpdk_mp *dpdk_mp;
223 int mtu;
224 int socket_id;
225 int buf_size;
8a9562d2
PS
226 struct netdev_stats stats;
227
228 uint8_t hwaddr[ETH_ADDR_LEN];
229 enum netdev_flags flags;
230
231 struct rte_eth_link link;
232 int link_reset_cnt;
233
58397e6c
KT
234 /* virtio-net structure for vhost device */
235 OVSRCU_TYPE(struct virtio_net *) virtio_dev;
236
8a9562d2 237 /* In dpdk_list. */
ca6ba700 238 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
58397e6c 239 rte_spinlock_t txq_lock;
8a9562d2
PS
240};
241
242struct netdev_rxq_dpdk {
243 struct netdev_rxq up;
244 int port_id;
245};
246
db73f716
DDP
247static bool thread_is_pmd(void);
248
8a9562d2
PS
249static int netdev_dpdk_construct(struct netdev *);
250
58397e6c
KT
251struct virtio_net * netdev_dpdk_get_virtio(const struct netdev_dpdk *dev);
252
8a9562d2
PS
253static bool
254is_dpdk_class(const struct netdev_class *class)
255{
256 return class->construct == netdev_dpdk_construct;
257}
258
58397e6c
KT
259/* XXX: use dpdk malloc for entire OVS. in fact huge page should be used
260 * for all other segments data, bss and text. */
8a9562d2
PS
261
262static void *
263dpdk_rte_mzalloc(size_t sz)
264{
265 void *ptr;
266
267 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
268 if (ptr == NULL) {
269 out_of_memory();
270 }
271 return ptr;
272}
273
db73f716
DDP
274/* XXX this function should be called only by pmd threads (or by non pmd
275 * threads holding the nonpmd_mempool_mutex) */
8a9562d2 276void
e14deea0 277free_dpdk_buf(struct dp_packet *p)
8a9562d2 278{
db73f716 279 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
8a9562d2 280
db73f716 281 rte_pktmbuf_free_seg(pkt);
8a9562d2
PS
282}
283
b3cd9f9d
PS
284static void
285__rte_pktmbuf_init(struct rte_mempool *mp,
286 void *opaque_arg OVS_UNUSED,
287 void *_m,
288 unsigned i OVS_UNUSED)
289{
290 struct rte_mbuf *m = _m;
e14deea0 291 uint32_t buf_len = mp->elt_size - sizeof(struct dp_packet);
b3cd9f9d 292
e14deea0 293 RTE_MBUF_ASSERT(mp->elt_size >= sizeof(struct dp_packet));
b3cd9f9d
PS
294
295 memset(m, 0, mp->elt_size);
296
297 /* start of buffer is just after mbuf structure */
e14deea0 298 m->buf_addr = (char *)m + sizeof(struct dp_packet);
b3cd9f9d 299 m->buf_physaddr = rte_mempool_virt2phy(mp, m) +
e14deea0 300 sizeof(struct dp_packet);
b3cd9f9d
PS
301 m->buf_len = (uint16_t)buf_len;
302
303 /* keep some headroom between start of buffer and data */
b8e57534 304 m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, m->buf_len);
b3cd9f9d
PS
305
306 /* init some constant fields */
b3cd9f9d 307 m->pool = mp;
b8e57534
MK
308 m->nb_segs = 1;
309 m->port = 0xff;
b3cd9f9d
PS
310}
311
312static void
313ovs_rte_pktmbuf_init(struct rte_mempool *mp,
314 void *opaque_arg OVS_UNUSED,
315 void *_m,
316 unsigned i OVS_UNUSED)
317{
318 struct rte_mbuf *m = _m;
319
320 __rte_pktmbuf_init(mp, opaque_arg, _m, i);
321
cf62fa4c 322 dp_packet_init_dpdk((struct dp_packet *) m, m->buf_len);
b3cd9f9d
PS
323}
324
8a9562d2
PS
325static struct dpdk_mp *
326dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
327{
328 struct dpdk_mp *dmp = NULL;
329 char mp_name[RTE_MEMPOOL_NAMESIZE];
da79ce2b 330 unsigned mp_size;
8a9562d2
PS
331
332 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
333 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
334 dmp->refcount++;
335 return dmp;
336 }
337 }
338
339 dmp = dpdk_rte_mzalloc(sizeof *dmp);
340 dmp->socket_id = socket_id;
341 dmp->mtu = mtu;
342 dmp->refcount = 1;
343
da79ce2b
DDP
344 mp_size = MAX_NB_MBUF;
345 do {
346 if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d_%d_%u",
347 dmp->mtu, dmp->socket_id, mp_size) < 0) {
348 return NULL;
349 }
95fb793a 350
da79ce2b
DDP
351 dmp->mp = rte_mempool_create(mp_name, mp_size, MBUF_SIZE(mtu),
352 MP_CACHE_SZ,
353 sizeof(struct rte_pktmbuf_pool_private),
354 rte_pktmbuf_pool_init, NULL,
355 ovs_rte_pktmbuf_init, NULL,
356 socket_id, 0);
357 } while (!dmp->mp && rte_errno == ENOMEM && (mp_size /= 2) >= MIN_NB_MBUF);
8a9562d2
PS
358
359 if (dmp->mp == NULL) {
360 return NULL;
da79ce2b
DDP
361 } else {
362 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", mp_name, mp_size );
8a9562d2
PS
363 }
364
365 list_push_back(&dpdk_mp_list, &dmp->list_node);
366 return dmp;
367}
368
369static void
370dpdk_mp_put(struct dpdk_mp *dmp)
371{
372
373 if (!dmp) {
374 return;
375 }
376
377 dmp->refcount--;
378 ovs_assert(dmp->refcount >= 0);
379
380#if 0
381 /* I could not find any API to destroy mp. */
382 if (dmp->refcount == 0) {
383 list_delete(dmp->list_node);
384 /* destroy mp-pool. */
385 }
386#endif
387}
388
389static void
390check_link_status(struct netdev_dpdk *dev)
391{
392 struct rte_eth_link link;
393
394 rte_eth_link_get_nowait(dev->port_id, &link);
395
396 if (dev->link.link_status != link.link_status) {
3e912ffc 397 netdev_change_seq_changed(&dev->up);
8a9562d2
PS
398
399 dev->link_reset_cnt++;
400 dev->link = link;
401 if (dev->link.link_status) {
402 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
403 dev->port_id, (unsigned)dev->link.link_speed,
404 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
405 ("full-duplex") : ("half-duplex"));
406 } else {
407 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
408 }
409 }
410}
411
412static void *
413dpdk_watchdog(void *dummy OVS_UNUSED)
414{
415 struct netdev_dpdk *dev;
416
417 pthread_detach(pthread_self());
418
419 for (;;) {
420 ovs_mutex_lock(&dpdk_mutex);
421 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
422 ovs_mutex_lock(&dev->mutex);
423 check_link_status(dev);
424 ovs_mutex_unlock(&dev->mutex);
425 }
426 ovs_mutex_unlock(&dpdk_mutex);
427 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
428 }
429
430 return NULL;
431}
432
433static int
434dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
435{
436 struct rte_pktmbuf_pool_private *mbp_priv;
437 struct ether_addr eth_addr;
438 int diag;
439 int i;
440
441 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
95fb793a 442 return ENODEV;
8a9562d2
PS
443 }
444
5496878c
AW
445 diag = rte_eth_dev_configure(dev->port_id, dev->up.n_rxq, dev->up.n_txq,
446 &port_conf);
8a9562d2
PS
447 if (diag) {
448 VLOG_ERR("eth dev config error %d",diag);
95fb793a 449 return -diag;
8a9562d2
PS
450 }
451
5496878c 452 for (i = 0; i < dev->up.n_txq; i++) {
79f5354c 453 diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
d221ffa1 454 dev->socket_id, &tx_conf);
8a9562d2
PS
455 if (diag) {
456 VLOG_ERR("eth dev tx queue setup error %d",diag);
95fb793a 457 return -diag;
8a9562d2
PS
458 }
459 }
460
5496878c 461 for (i = 0; i < dev->up.n_rxq; i++) {
79f5354c 462 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
d221ffa1 463 dev->socket_id,
a715f600 464 &rx_conf, dev->dpdk_mp->mp);
8a9562d2
PS
465 if (diag) {
466 VLOG_ERR("eth dev rx queue setup error %d",diag);
95fb793a 467 return -diag;
8a9562d2
PS
468 }
469 }
470
471 diag = rte_eth_dev_start(dev->port_id);
472 if (diag) {
473 VLOG_ERR("eth dev start error %d",diag);
95fb793a 474 return -diag;
8a9562d2
PS
475 }
476
477 rte_eth_promiscuous_enable(dev->port_id);
478 rte_eth_allmulticast_enable(dev->port_id);
479
480 memset(&eth_addr, 0x0, sizeof(eth_addr));
481 rte_eth_macaddr_get(dev->port_id, &eth_addr);
482 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
483 dev->port_id, ETH_ADDR_ARGS(eth_addr.addr_bytes));
484
485 memcpy(dev->hwaddr, eth_addr.addr_bytes, ETH_ADDR_LEN);
486 rte_eth_link_get_nowait(dev->port_id, &dev->link);
487
488 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
489 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
490
491 dev->flags = NETDEV_UP | NETDEV_PROMISC;
492 return 0;
493}
494
495static struct netdev_dpdk *
496netdev_dpdk_cast(const struct netdev *netdev)
497{
498 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
499}
500
501static struct netdev *
502netdev_dpdk_alloc(void)
503{
504 struct netdev_dpdk *netdev = dpdk_rte_mzalloc(sizeof *netdev);
505 return &netdev->up;
506}
507
5a034064 508static void
91968eb0 509netdev_dpdk_alloc_txq(struct netdev_dpdk *netdev, unsigned int n_txqs)
5a034064
AW
510{
511 int i;
512
513 netdev->tx_q = dpdk_rte_mzalloc(n_txqs * sizeof *netdev->tx_q);
94143fc4
AW
514 /* Each index is considered as a cpu core id, since there should
515 * be one tx queue for each cpu core. */
5a034064 516 for (i = 0; i < n_txqs; i++) {
ba0358a1 517 int numa_id = ovs_numa_get_numa_id(i);
94143fc4 518
94143fc4
AW
519 /* If the corresponding core is not on the same numa node
520 * as 'netdev', flags the 'flush_tx'. */
ba0358a1 521 netdev->tx_q[i].flush_tx = netdev->socket_id == numa_id;
5a034064
AW
522 }
523}
524
8a9562d2 525static int
58397e6c
KT
526netdev_dpdk_init(struct netdev *netdev_, unsigned int port_no,
527 enum dpdk_dev_type type)
5a034064 528 OVS_REQUIRES(dpdk_mutex)
8a9562d2
PS
529{
530 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1b7a04e0 531 int sid;
95fb793a 532 int err = 0;
8a9562d2 533
95fb793a 534 ovs_mutex_init(&netdev->mutex);
95fb793a 535 ovs_mutex_lock(&netdev->mutex);
8a9562d2 536
1b7a04e0
AW
537 /* If the 'sid' is negative, it means that the kernel fails
538 * to obtain the pci numa info. In that situation, always
539 * use 'SOCKET0'. */
58397e6c
KT
540 if (type == DPDK_DEV_ETH) {
541 sid = rte_eth_dev_socket_id(port_no);
542 } else {
543 sid = rte_lcore_to_socket_id(rte_get_master_lcore());
544 }
545
1b7a04e0 546 netdev->socket_id = sid < 0 ? SOCKET0 : sid;
95fb793a 547 netdev->port_id = port_no;
58397e6c 548 netdev->type = type;
8a9562d2 549 netdev->flags = 0;
8a9562d2
PS
550 netdev->mtu = ETHER_MTU;
551 netdev->max_packet_len = MTU_TO_MAX_LEN(netdev->mtu);
58397e6c 552 rte_spinlock_init(&netdev->txq_lock);
8a9562d2 553
8a9562d2
PS
554 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, netdev->mtu);
555 if (!netdev->dpdk_mp) {
556 err = ENOMEM;
95fb793a 557 goto unlock;
8a9562d2
PS
558 }
559
5496878c
AW
560 netdev_->n_txq = NR_QUEUE;
561 netdev_->n_rxq = NR_QUEUE;
58397e6c
KT
562
563 if (type == DPDK_DEV_ETH) {
1b99bb05
MG
564 netdev_dpdk_alloc_txq(netdev, NR_QUEUE);
565 err = dpdk_eth_dev_init(netdev);
566 if (err) {
567 goto unlock;
568 }
8a9562d2 569 }
8a9562d2
PS
570
571 list_push_back(&dpdk_list, &netdev->list_node);
572
95fb793a 573unlock:
5a034064
AW
574 if (err) {
575 rte_free(netdev->tx_q);
576 }
8a9562d2 577 ovs_mutex_unlock(&netdev->mutex);
95fb793a 578 return err;
579}
580
581static int
582dpdk_dev_parse_name(const char dev_name[], const char prefix[],
583 unsigned int *port_no)
584{
585 const char *cport;
586
587 if (strncmp(dev_name, prefix, strlen(prefix))) {
588 return ENODEV;
589 }
590
591 cport = dev_name + strlen(prefix);
592 *port_no = strtol(cport, 0, 0); /* string must be null terminated */
593 return 0;
594}
595
58397e6c
KT
596static int
597netdev_dpdk_vhost_construct(struct netdev *netdev_)
598{
599 int err;
600
601 if (rte_eal_init_ret) {
602 return rte_eal_init_ret;
603 }
604
605 ovs_mutex_lock(&dpdk_mutex);
606 err = netdev_dpdk_init(netdev_, -1, DPDK_DEV_VHOST);
607 ovs_mutex_unlock(&dpdk_mutex);
608
609 return err;
610}
611
95fb793a 612static int
613netdev_dpdk_construct(struct netdev *netdev)
614{
615 unsigned int port_no;
616 int err;
617
618 if (rte_eal_init_ret) {
619 return rte_eal_init_ret;
620 }
621
622 /* Names always start with "dpdk" */
623 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
624 if (err) {
625 return err;
626 }
627
628 ovs_mutex_lock(&dpdk_mutex);
58397e6c 629 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
8a9562d2
PS
630 ovs_mutex_unlock(&dpdk_mutex);
631 return err;
632}
633
634static void
635netdev_dpdk_destruct(struct netdev *netdev_)
636{
637 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
638
639 ovs_mutex_lock(&dev->mutex);
640 rte_eth_dev_stop(dev->port_id);
641 ovs_mutex_unlock(&dev->mutex);
642
643 ovs_mutex_lock(&dpdk_mutex);
5a034064 644 rte_free(dev->tx_q);
8a9562d2
PS
645 list_remove(&dev->list_node);
646 dpdk_mp_put(dev->dpdk_mp);
647 ovs_mutex_unlock(&dpdk_mutex);
58397e6c 648}
8a9562d2 649
58397e6c
KT
650static void
651netdev_dpdk_vhost_destruct(struct netdev *netdev_)
652{
653 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
654
655 /* Can't remove a port while a guest is attached to it. */
656 if (netdev_dpdk_get_virtio(dev) != NULL) {
657 VLOG_ERR("Can not remove port, vhost device still attached");
658 return;
659 }
660
661 ovs_mutex_lock(&dpdk_mutex);
662 list_remove(&dev->list_node);
663 dpdk_mp_put(dev->dpdk_mp);
664 ovs_mutex_unlock(&dpdk_mutex);
8a9562d2
PS
665}
666
667static void
668netdev_dpdk_dealloc(struct netdev *netdev_)
669{
670 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
671
672 rte_free(netdev);
673}
674
675static int
676netdev_dpdk_get_config(const struct netdev *netdev_, struct smap *args)
677{
678 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
679
680 ovs_mutex_lock(&dev->mutex);
681
47659062
AW
682 smap_add_format(args, "configured_rx_queues", "%d", netdev_->n_rxq);
683 smap_add_format(args, "configured_tx_queues", "%d", netdev_->n_txq);
8a9562d2
PS
684 ovs_mutex_unlock(&dev->mutex);
685
686 return 0;
687}
688
7dec44fe
AW
689static int
690netdev_dpdk_get_numa_id(const struct netdev *netdev_)
691{
692 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
693
694 return netdev->socket_id;
695}
696
5496878c
AW
697/* Sets the number of tx queues and rx queues for the dpdk interface.
698 * If the configuration fails, do not try restoring its old configuration
699 * and just returns the error. */
700static int
701netdev_dpdk_set_multiq(struct netdev *netdev_, unsigned int n_txq,
702 unsigned int n_rxq)
703{
704 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
705 int err = 0;
706
707 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
708 return err;
709 }
710
b7ccaf67 711 ovs_mutex_lock(&dpdk_mutex);
5496878c 712 ovs_mutex_lock(&netdev->mutex);
91968eb0 713
5496878c 714 rte_eth_dev_stop(netdev->port_id);
91968eb0 715
5496878c
AW
716 netdev->up.n_txq = n_txq;
717 netdev->up.n_rxq = n_rxq;
58397e6c 718
91968eb0
AW
719 rte_free(netdev->tx_q);
720 netdev_dpdk_alloc_txq(netdev, n_txq);
5496878c 721 err = dpdk_eth_dev_init(netdev);
91968eb0 722
5496878c 723 ovs_mutex_unlock(&netdev->mutex);
b7ccaf67 724 ovs_mutex_unlock(&dpdk_mutex);
5496878c
AW
725
726 return err;
727}
728
58397e6c
KT
729static int
730netdev_dpdk_vhost_set_multiq(struct netdev *netdev_, unsigned int n_txq,
731 unsigned int n_rxq)
732{
733 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
734 int err = 0;
735
736 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
737 return err;
738 }
739
740 ovs_mutex_lock(&dpdk_mutex);
741 ovs_mutex_lock(&netdev->mutex);
742
743 netdev->up.n_txq = n_txq;
744 netdev->up.n_rxq = n_rxq;
745
746 ovs_mutex_unlock(&netdev->mutex);
747 ovs_mutex_unlock(&dpdk_mutex);
748
749 return err;
750}
751
8a9562d2
PS
752static struct netdev_rxq *
753netdev_dpdk_rxq_alloc(void)
754{
755 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
756
757 return &rx->up;
758}
759
760static struct netdev_rxq_dpdk *
761netdev_rxq_dpdk_cast(const struct netdev_rxq *rx)
762{
763 return CONTAINER_OF(rx, struct netdev_rxq_dpdk, up);
764}
765
766static int
767netdev_dpdk_rxq_construct(struct netdev_rxq *rxq_)
768{
769 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
770 struct netdev_dpdk *netdev = netdev_dpdk_cast(rx->up.netdev);
771
772 ovs_mutex_lock(&netdev->mutex);
773 rx->port_id = netdev->port_id;
774 ovs_mutex_unlock(&netdev->mutex);
775
776 return 0;
777}
778
779static void
780netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq_ OVS_UNUSED)
781{
782}
783
784static void
785netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq_)
786{
787 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
788
789 rte_free(rx);
790}
791
b170db2a
RW
792static inline void
793dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
8a9562d2
PS
794{
795 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
1304f1f8
DDP
796 uint32_t nb_tx = 0;
797
798 while (nb_tx != txq->count) {
799 uint32_t ret;
800
801 ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
802 txq->count - nb_tx);
803 if (!ret) {
804 break;
805 }
806
807 nb_tx += ret;
808 }
8a9562d2 809
b170db2a 810 if (OVS_UNLIKELY(nb_tx != txq->count)) {
db73f716
DDP
811 /* free buffers, which we couldn't transmit, one at a time (each
812 * packet could come from a different mempool) */
813 int i;
814
815 for (i = nb_tx; i < txq->count; i++) {
816 rte_pktmbuf_free_seg(txq->burst_pkts[i]);
817 }
1304f1f8
DDP
818 ovs_mutex_lock(&dev->mutex);
819 dev->stats.tx_dropped += txq->count-nb_tx;
820 ovs_mutex_unlock(&dev->mutex);
8a9562d2 821 }
1304f1f8 822
8a9562d2 823 txq->count = 0;
844f2d74 824 txq->tsc = rte_get_timer_cycles();
b170db2a
RW
825}
826
827static inline void
828dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
829{
830 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
831
832 if (txq->count == 0) {
833 return;
834 }
b170db2a 835 dpdk_queue_flush__(dev, qid);
8a9562d2
PS
836}
837
58397e6c
KT
838static bool
839is_vhost_running(struct virtio_net *dev)
840{
841 return (dev != NULL && (dev->flags & VIRTIO_DEV_RUNNING));
842}
843
844/*
845 * The receive path for the vhost port is the TX path out from guest.
846 */
847static int
848netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq_,
849 struct dp_packet **packets, int *c)
850{
851 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
852 struct netdev *netdev = rx->up.netdev;
853 struct netdev_dpdk *vhost_dev = netdev_dpdk_cast(netdev);
854 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(vhost_dev);
855 int qid = 1;
856 uint16_t nb_rx = 0;
857
858 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev))) {
859 return EAGAIN;
860 }
861
862 nb_rx = rte_vhost_dequeue_burst(virtio_dev, qid,
863 vhost_dev->dpdk_mp->mp,
864 (struct rte_mbuf **)packets,
865 MAX_PKT_BURST);
866 if (!nb_rx) {
867 return EAGAIN;
868 }
869
870 vhost_dev->stats.rx_packets += (uint64_t)nb_rx;
871 *c = (int) nb_rx;
872 return 0;
873}
874
8a9562d2 875static int
e14deea0 876netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **packets,
91088554 877 int *c)
8a9562d2
PS
878{
879 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
880 struct netdev *netdev = rx->up.netdev;
881 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 882 int nb_rx;
8a9562d2 883
5496878c
AW
884 /* There is only one tx queue for this core. Do not flush other
885 * queueus. */
886 if (rxq_->queue_id == rte_lcore_id()) {
887 dpdk_queue_flush(dev, rxq_->queue_id);
888 }
8a9562d2
PS
889
890 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
7d08d53e 891 (struct rte_mbuf **) packets,
4345e1b5
KT
892 MIN((int) NETDEV_MAX_RX_BATCH,
893 (int) MAX_PKT_BURST));
8a9562d2
PS
894 if (!nb_rx) {
895 return EAGAIN;
896 }
897
8a9562d2
PS
898 *c = nb_rx;
899
900 return 0;
901}
902
58397e6c
KT
903static void
904__netdev_dpdk_vhost_send(struct netdev *netdev, struct dp_packet **pkts,
905 int cnt, bool may_steal)
906{
907 struct netdev_dpdk *vhost_dev = netdev_dpdk_cast(netdev);
908 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(vhost_dev);
95e9881f
KT
909 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
910 unsigned int total_pkts = cnt;
911 uint64_t start = 0;
58397e6c
KT
912
913 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev))) {
1b99bb05
MG
914 ovs_mutex_lock(&vhost_dev->mutex);
915 vhost_dev->stats.tx_dropped+= cnt;
916 ovs_mutex_unlock(&vhost_dev->mutex);
917 goto out;
58397e6c
KT
918 }
919
920 /* There is vHost TX single queue, So we need to lock it for TX. */
921 rte_spinlock_lock(&vhost_dev->txq_lock);
58397e6c 922
95e9881f
KT
923 do {
924 unsigned int tx_pkts;
925
926 tx_pkts = rte_vhost_enqueue_burst(virtio_dev, VIRTIO_RXQ,
927 cur_pkts, cnt);
928 if (OVS_LIKELY(tx_pkts)) {
929 /* Packets have been sent.*/
930 cnt -= tx_pkts;
931 /* Prepare for possible next iteration.*/
932 cur_pkts = &cur_pkts[tx_pkts];
933 } else {
934 uint64_t timeout = VHOST_ENQ_RETRY_USECS * rte_get_timer_hz() / 1E6;
935 unsigned int expired = 0;
936
937 if (!start) {
938 start = rte_get_timer_cycles();
939 }
940
941 /*
942 * Unable to enqueue packets to vhost interface.
943 * Check available entries before retrying.
944 */
945 while (!rte_vring_available_entries(virtio_dev, VIRTIO_RXQ)) {
946 if (OVS_UNLIKELY((rte_get_timer_cycles() - start) > timeout)) {
947 expired = 1;
948 break;
949 }
950 }
951 if (expired) {
952 /* break out of main loop. */
953 break;
954 }
955 }
956 } while (cnt);
957
958 vhost_dev->stats.tx_packets += (total_pkts - cnt);
959 vhost_dev->stats.tx_dropped += cnt;
58397e6c
KT
960 rte_spinlock_unlock(&vhost_dev->txq_lock);
961
962out:
963 if (may_steal) {
95e9881f
KT
964 int i;
965
966 for (i = 0; i < total_pkts; i++) {
1b99bb05
MG
967 dp_packet_delete(pkts[i]);
968 }
58397e6c
KT
969 }
970}
971
8a9562d2 972inline static void
f4fd623c
DDP
973dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
974 struct rte_mbuf **pkts, int cnt)
8a9562d2
PS
975{
976 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
977 uint64_t diff_tsc;
8a9562d2 978
f4fd623c
DDP
979 int i = 0;
980
f4fd623c
DDP
981 while (i < cnt) {
982 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
983 int tocopy = MIN(freeslots, cnt-i);
8a9562d2 984
f4fd623c
DDP
985 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
986 tocopy * sizeof (struct rte_mbuf *));
987
988 txq->count += tocopy;
989 i += tocopy;
990
94143fc4 991 if (txq->count == MAX_TX_QUEUE_LEN || txq->flush_tx) {
b170db2a 992 dpdk_queue_flush__(dev, qid);
f4fd623c 993 }
844f2d74 994 diff_tsc = rte_get_timer_cycles() - txq->tsc;
f4fd623c 995 if (diff_tsc >= DRAIN_TSC) {
b170db2a 996 dpdk_queue_flush__(dev, qid);
f4fd623c 997 }
8a9562d2 998 }
8a9562d2
PS
999}
1000
1001/* Tx function. Transmit packets indefinitely */
1002static void
58397e6c 1003dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet **pkts,
2654cc33 1004 int cnt)
db73f716 1005 OVS_NO_THREAD_SAFETY_ANALYSIS
8a9562d2
PS
1006{
1007 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
f4fd623c 1008 struct rte_mbuf *mbufs[cnt];
175cf4de
RW
1009 int dropped = 0;
1010 int newcnt = 0;
1011 int i;
8a9562d2 1012
db73f716
DDP
1013 /* If we are on a non pmd thread we have to use the mempool mutex, because
1014 * every non pmd thread shares the same mempool cache */
1015
1016 if (!thread_is_pmd()) {
1017 ovs_mutex_lock(&nonpmd_mempool_mutex);
1018 }
1019
f4fd623c 1020 for (i = 0; i < cnt; i++) {
cf62fa4c 1021 int size = dp_packet_size(pkts[i]);
95fb793a 1022
f98d7864 1023 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
f4fd623c
DDP
1024 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1025 (int)size , dev->max_packet_len);
1026
175cf4de 1027 dropped++;
f4fd623c
DDP
1028 continue;
1029 }
8a9562d2 1030
f4fd623c 1031 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
8a9562d2 1032
f4fd623c 1033 if (!mbufs[newcnt]) {
175cf4de
RW
1034 dropped += cnt - i;
1035 break;
f4fd623c
DDP
1036 }
1037
1038 /* We have to do a copy for now */
b8e57534 1039 memcpy(rte_pktmbuf_mtod(mbufs[newcnt], void *), dp_packet_data(pkts[i]), size);
f4fd623c
DDP
1040
1041 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
1042 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
1043
1044 newcnt++;
1045 }
8a9562d2 1046
f98d7864 1047 if (OVS_UNLIKELY(dropped)) {
175cf4de
RW
1048 ovs_mutex_lock(&dev->mutex);
1049 dev->stats.tx_dropped += dropped;
1050 ovs_mutex_unlock(&dev->mutex);
1051 }
1052
58397e6c
KT
1053 if (dev->type == DPDK_DEV_VHOST) {
1054 __netdev_dpdk_vhost_send(netdev, (struct dp_packet **) mbufs, newcnt, true);
1055 } else {
1056 dpdk_queue_pkts(dev, qid, mbufs, newcnt);
1057 dpdk_queue_flush(dev, qid);
1058 }
db73f716
DDP
1059
1060 if (!thread_is_pmd()) {
1061 ovs_mutex_unlock(&nonpmd_mempool_mutex);
1062 }
8a9562d2
PS
1063}
1064
58397e6c
KT
1065static int
1066netdev_dpdk_vhost_send(struct netdev *netdev, int qid OVS_UNUSED, struct dp_packet **pkts,
1067 int cnt, bool may_steal)
1068{
1069 if (OVS_UNLIKELY(pkts[0]->source != DPBUF_DPDK)) {
1070 int i;
1071
1072 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1073 if (may_steal) {
1074 for (i = 0; i < cnt; i++) {
1075 dp_packet_delete(pkts[i]);
1076 }
1077 }
1078 } else {
1079 __netdev_dpdk_vhost_send(netdev, pkts, cnt, may_steal);
1080 }
1081 return 0;
1082}
1083
7251515e
DV
1084static inline void
1085netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
e14deea0 1086 struct dp_packet **pkts, int cnt, bool may_steal)
8a9562d2 1087{
f4fd623c 1088 int i;
8a9562d2 1089
7251515e 1090 if (OVS_UNLIKELY(!may_steal ||
cf62fa4c 1091 pkts[0]->source != DPBUF_DPDK)) {
7251515e
DV
1092 struct netdev *netdev = &dev->up;
1093
2654cc33 1094 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
b3cd9f9d
PS
1095
1096 if (may_steal) {
f4fd623c 1097 for (i = 0; i < cnt; i++) {
e14deea0 1098 dp_packet_delete(pkts[i]);
f4fd623c 1099 }
b3cd9f9d 1100 }
8a9562d2 1101 } else {
f4fd623c
DDP
1102 int next_tx_idx = 0;
1103 int dropped = 0;
8a9562d2 1104
f4fd623c 1105 for (i = 0; i < cnt; i++) {
cf62fa4c 1106 int size = dp_packet_size(pkts[i]);
1b99bb05 1107
f4fd623c
DDP
1108 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1109 if (next_tx_idx != i) {
1110 dpdk_queue_pkts(dev, qid,
1111 (struct rte_mbuf **)&pkts[next_tx_idx],
1112 i-next_tx_idx);
1ebfe1ac 1113 }
f4fd623c 1114
1ebfe1ac
DDP
1115 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1116 (int)size , dev->max_packet_len);
f4fd623c 1117
e14deea0 1118 dp_packet_delete(pkts[i]);
1ebfe1ac 1119 dropped++;
f4fd623c
DDP
1120 next_tx_idx = i + 1;
1121 }
1122 }
1123 if (next_tx_idx != cnt) {
1124 dpdk_queue_pkts(dev, qid,
1125 (struct rte_mbuf **)&pkts[next_tx_idx],
1126 cnt-next_tx_idx);
1127 }
8a9562d2 1128
f4fd623c
DDP
1129 if (OVS_UNLIKELY(dropped)) {
1130 ovs_mutex_lock(&dev->mutex);
1131 dev->stats.tx_dropped += dropped;
1132 ovs_mutex_unlock(&dev->mutex);
1133 }
8a9562d2 1134 }
7251515e
DV
1135}
1136
1137static int
1138netdev_dpdk_eth_send(struct netdev *netdev, int qid,
e14deea0 1139 struct dp_packet **pkts, int cnt, bool may_steal)
7251515e
DV
1140{
1141 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
8a9562d2 1142
7251515e
DV
1143 netdev_dpdk_send__(dev, qid, pkts, cnt, may_steal);
1144 return 0;
8a9562d2
PS
1145}
1146
1147static int
1148netdev_dpdk_set_etheraddr(struct netdev *netdev,
1149 const uint8_t mac[ETH_ADDR_LEN])
1150{
1151 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1152
1153 ovs_mutex_lock(&dev->mutex);
1154 if (!eth_addr_equals(dev->hwaddr, mac)) {
1155 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
045c0d1a 1156 netdev_change_seq_changed(netdev);
8a9562d2
PS
1157 }
1158 ovs_mutex_unlock(&dev->mutex);
1159
1160 return 0;
1161}
1162
1163static int
1164netdev_dpdk_get_etheraddr(const struct netdev *netdev,
1165 uint8_t mac[ETH_ADDR_LEN])
1166{
1167 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1168
1169 ovs_mutex_lock(&dev->mutex);
1170 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
1171 ovs_mutex_unlock(&dev->mutex);
1172
1173 return 0;
1174}
1175
1176static int
1177netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
1178{
1179 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1180
1181 ovs_mutex_lock(&dev->mutex);
1182 *mtup = dev->mtu;
1183 ovs_mutex_unlock(&dev->mutex);
1184
1185 return 0;
1186}
1187
1188static int
1189netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
1190{
1191 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1192 int old_mtu, err;
1193 struct dpdk_mp *old_mp;
1194 struct dpdk_mp *mp;
1195
1196 ovs_mutex_lock(&dpdk_mutex);
1197 ovs_mutex_lock(&dev->mutex);
1198 if (dev->mtu == mtu) {
1199 err = 0;
1200 goto out;
1201 }
1202
1203 mp = dpdk_mp_get(dev->socket_id, dev->mtu);
1204 if (!mp) {
1205 err = ENOMEM;
1206 goto out;
1207 }
1208
1209 rte_eth_dev_stop(dev->port_id);
1210
1211 old_mtu = dev->mtu;
1212 old_mp = dev->dpdk_mp;
1213 dev->dpdk_mp = mp;
1214 dev->mtu = mtu;
1215 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
1216
1217 err = dpdk_eth_dev_init(dev);
1218 if (err) {
8a9562d2
PS
1219 dpdk_mp_put(mp);
1220 dev->mtu = old_mtu;
1221 dev->dpdk_mp = old_mp;
1222 dev->max_packet_len = MTU_TO_MAX_LEN(dev->mtu);
1223 dpdk_eth_dev_init(dev);
1224 goto out;
1225 }
1226
1227 dpdk_mp_put(old_mp);
045c0d1a 1228 netdev_change_seq_changed(netdev);
8a9562d2
PS
1229out:
1230 ovs_mutex_unlock(&dev->mutex);
1231 ovs_mutex_unlock(&dpdk_mutex);
1232 return err;
1233}
1234
1235static int
1236netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
1237
58397e6c
KT
1238static int
1239netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
1240 struct netdev_stats *stats)
1241{
1242 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1243
1244 ovs_mutex_lock(&dev->mutex);
1245 memset(stats, 0, sizeof(*stats));
1246 /* Unsupported Stats */
1247 stats->rx_errors = UINT64_MAX;
1248 stats->tx_errors = UINT64_MAX;
1249 stats->multicast = UINT64_MAX;
1250 stats->collisions = UINT64_MAX;
1251 stats->rx_crc_errors = UINT64_MAX;
1252 stats->rx_fifo_errors = UINT64_MAX;
1253 stats->rx_frame_errors = UINT64_MAX;
1254 stats->rx_length_errors = UINT64_MAX;
1255 stats->rx_missed_errors = UINT64_MAX;
1256 stats->rx_over_errors = UINT64_MAX;
1257 stats->tx_aborted_errors = UINT64_MAX;
1258 stats->tx_carrier_errors = UINT64_MAX;
1259 stats->tx_errors = UINT64_MAX;
1260 stats->tx_fifo_errors = UINT64_MAX;
1261 stats->tx_heartbeat_errors = UINT64_MAX;
1262 stats->tx_window_errors = UINT64_MAX;
1263 stats->rx_bytes += UINT64_MAX;
1264 stats->rx_dropped += UINT64_MAX;
1265 stats->tx_bytes += UINT64_MAX;
1266
1267 /* Supported Stats */
1268 stats->rx_packets += dev->stats.rx_packets;
1269 stats->tx_packets += dev->stats.tx_packets;
1270 stats->tx_dropped += dev->stats.tx_dropped;
1271 ovs_mutex_unlock(&dev->mutex);
1272
1273 return 0;
1274}
1275
8a9562d2
PS
1276static int
1277netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1278{
1279 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1280 struct rte_eth_stats rte_stats;
1281 bool gg;
1282
1283 netdev_dpdk_get_carrier(netdev, &gg);
1284 ovs_mutex_lock(&dev->mutex);
1285 rte_eth_stats_get(dev->port_id, &rte_stats);
1286
2f9dd77f 1287 memset(stats, 0, sizeof(*stats));
8a9562d2 1288
2f9dd77f
PS
1289 stats->rx_packets = rte_stats.ipackets;
1290 stats->tx_packets = rte_stats.opackets;
1291 stats->rx_bytes = rte_stats.ibytes;
1292 stats->tx_bytes = rte_stats.obytes;
1293 stats->rx_errors = rte_stats.ierrors;
1294 stats->tx_errors = rte_stats.oerrors;
1295 stats->multicast = rte_stats.imcasts;
8a9562d2 1296
2f9dd77f 1297 stats->tx_dropped = dev->stats.tx_dropped;
8a9562d2
PS
1298 ovs_mutex_unlock(&dev->mutex);
1299
1300 return 0;
1301}
1302
1303static int
1304netdev_dpdk_get_features(const struct netdev *netdev_,
1305 enum netdev_features *current,
1306 enum netdev_features *advertised OVS_UNUSED,
1307 enum netdev_features *supported OVS_UNUSED,
1308 enum netdev_features *peer OVS_UNUSED)
1309{
1310 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1311 struct rte_eth_link link;
1312
1313 ovs_mutex_lock(&dev->mutex);
1314 link = dev->link;
1315 ovs_mutex_unlock(&dev->mutex);
1316
1317 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1318 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1319 *current = NETDEV_F_AUTONEG;
1320 }
1321 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1322 if (link.link_speed == ETH_LINK_SPEED_10) {
1323 *current = NETDEV_F_10MB_HD;
1324 }
1325 if (link.link_speed == ETH_LINK_SPEED_100) {
1326 *current = NETDEV_F_100MB_HD;
1327 }
1328 if (link.link_speed == ETH_LINK_SPEED_1000) {
1329 *current = NETDEV_F_1GB_HD;
1330 }
1331 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1332 if (link.link_speed == ETH_LINK_SPEED_10) {
1333 *current = NETDEV_F_10MB_FD;
1334 }
1335 if (link.link_speed == ETH_LINK_SPEED_100) {
1336 *current = NETDEV_F_100MB_FD;
1337 }
1338 if (link.link_speed == ETH_LINK_SPEED_1000) {
1339 *current = NETDEV_F_1GB_FD;
1340 }
1341 if (link.link_speed == ETH_LINK_SPEED_10000) {
1342 *current = NETDEV_F_10GB_FD;
1343 }
1344 }
1345
1346 return 0;
1347}
1348
1349static int
1350netdev_dpdk_get_ifindex(const struct netdev *netdev)
1351{
1352 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1353 int ifindex;
1354
1355 ovs_mutex_lock(&dev->mutex);
1356 ifindex = dev->port_id;
1357 ovs_mutex_unlock(&dev->mutex);
1358
1359 return ifindex;
1360}
1361
1362static int
1363netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1364{
1365 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1366
1367 ovs_mutex_lock(&dev->mutex);
1368 check_link_status(dev);
1369 *carrier = dev->link.link_status;
58397e6c
KT
1370
1371 ovs_mutex_unlock(&dev->mutex);
1372
1373 return 0;
1374}
1375
1376static int
1377netdev_dpdk_vhost_get_carrier(const struct netdev *netdev_, bool *carrier)
1378{
1379 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1380 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(dev);
1381
1382 ovs_mutex_lock(&dev->mutex);
1383
1384 if (is_vhost_running(virtio_dev)) {
1385 *carrier = 1;
1386 } else {
1387 *carrier = 0;
1388 }
1389
8a9562d2
PS
1390 ovs_mutex_unlock(&dev->mutex);
1391
1392 return 0;
1393}
1394
1395static long long int
1396netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1397{
1398 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1399 long long int carrier_resets;
1400
1401 ovs_mutex_lock(&dev->mutex);
1402 carrier_resets = dev->link_reset_cnt;
1403 ovs_mutex_unlock(&dev->mutex);
1404
1405 return carrier_resets;
1406}
1407
1408static int
1409netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1410 long long int interval OVS_UNUSED)
1411{
ee32150e 1412 return EOPNOTSUPP;
8a9562d2
PS
1413}
1414
1415static int
1416netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1417 enum netdev_flags off, enum netdev_flags on,
95fb793a 1418 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
8a9562d2
PS
1419{
1420 int err;
1421
1422 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1423 return EINVAL;
1424 }
1425
1426 *old_flagsp = dev->flags;
1427 dev->flags |= on;
1428 dev->flags &= ~off;
1429
1430 if (dev->flags == *old_flagsp) {
1431 return 0;
1432 }
1433
58397e6c
KT
1434 if (dev->type == DPDK_DEV_ETH) {
1435 if (dev->flags & NETDEV_UP) {
1436 err = rte_eth_dev_start(dev->port_id);
1437 if (err)
1438 return -err;
1439 }
8a9562d2 1440
58397e6c
KT
1441 if (dev->flags & NETDEV_PROMISC) {
1442 rte_eth_promiscuous_enable(dev->port_id);
1443 }
8a9562d2 1444
58397e6c
KT
1445 if (!(dev->flags & NETDEV_UP)) {
1446 rte_eth_dev_stop(dev->port_id);
1447 }
8a9562d2
PS
1448 }
1449
1450 return 0;
1451}
1452
1453static int
1454netdev_dpdk_update_flags(struct netdev *netdev_,
1455 enum netdev_flags off, enum netdev_flags on,
1456 enum netdev_flags *old_flagsp)
1457{
1458 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1459 int error;
1460
1461 ovs_mutex_lock(&netdev->mutex);
1462 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1463 ovs_mutex_unlock(&netdev->mutex);
1464
1465 return error;
1466}
1467
1468static int
1469netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1470{
1471 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1472 struct rte_eth_dev_info dev_info;
1473
e0a801c7 1474 if (dev->port_id < 0)
8a9562d2
PS
1475 return ENODEV;
1476
1477 ovs_mutex_lock(&dev->mutex);
1478 rte_eth_dev_info_get(dev->port_id, &dev_info);
1479 ovs_mutex_unlock(&dev->mutex);
1480
1481 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1482
95fb793a 1483 smap_add_format(args, "port_no", "%d", dev->port_id);
8a9562d2
PS
1484 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1485 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1486 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1487 smap_add_format(args, "max_rx_pktlen", "%u", dev_info.max_rx_pktlen);
1488 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1489 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1490 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1491 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1492 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1493 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1494
1495 smap_add_format(args, "pci-vendor_id", "0x%u", dev_info.pci_dev->id.vendor_id);
1496 smap_add_format(args, "pci-device_id", "0x%x", dev_info.pci_dev->id.device_id);
1497
1498 return 0;
1499}
1500
1501static void
1502netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1503 OVS_REQUIRES(dev->mutex)
1504{
1505 enum netdev_flags old_flags;
1506
1507 if (admin_state) {
1508 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1509 } else {
1510 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1511 }
1512}
1513
1514static void
1515netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1516 const char *argv[], void *aux OVS_UNUSED)
1517{
1518 bool up;
1519
1520 if (!strcasecmp(argv[argc - 1], "up")) {
1521 up = true;
1522 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1523 up = false;
1524 } else {
1525 unixctl_command_reply_error(conn, "Invalid Admin State");
1526 return;
1527 }
1528
1529 if (argc > 2) {
1530 struct netdev *netdev = netdev_from_name(argv[1]);
1531 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1532 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1533
1534 ovs_mutex_lock(&dpdk_dev->mutex);
1535 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1536 ovs_mutex_unlock(&dpdk_dev->mutex);
1537
1538 netdev_close(netdev);
1539 } else {
1540 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1541 netdev_close(netdev);
1542 return;
1543 }
1544 } else {
1545 struct netdev_dpdk *netdev;
1546
1547 ovs_mutex_lock(&dpdk_mutex);
1548 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1549 ovs_mutex_lock(&netdev->mutex);
1550 netdev_dpdk_set_admin_state__(netdev, up);
1551 ovs_mutex_unlock(&netdev->mutex);
1552 }
1553 ovs_mutex_unlock(&dpdk_mutex);
1554 }
1555 unixctl_command_reply(conn, "OK");
1556}
1557
58397e6c
KT
1558/*
1559 * Set virtqueue flags so that we do not receive interrupts.
1560 */
1561static void
1562set_irq_status(struct virtio_net *dev)
1563{
1564 dev->virtqueue[VIRTIO_RXQ]->used->flags = VRING_USED_F_NO_NOTIFY;
1565 dev->virtqueue[VIRTIO_TXQ]->used->flags = VRING_USED_F_NO_NOTIFY;
1566}
1567
1568/*
1569 * A new virtio-net device is added to a vhost port.
1570 */
1571static int
1572new_device(struct virtio_net *dev)
1573{
1574 struct netdev_dpdk *netdev;
1575 bool exists = false;
1576
1577 ovs_mutex_lock(&dpdk_mutex);
1578 /* Add device to the vhost port with the same name as that passed down. */
1579 LIST_FOR_EACH(netdev, list_node, &dpdk_list) {
1580 if (strncmp(dev->ifname, netdev->up.name, IFNAMSIZ) == 0) {
1581 ovs_mutex_lock(&netdev->mutex);
1582 ovsrcu_set(&netdev->virtio_dev, dev);
1583 ovs_mutex_unlock(&netdev->mutex);
1584 exists = true;
1585 dev->flags |= VIRTIO_DEV_RUNNING;
1586 /* Disable notifications. */
1587 set_irq_status(dev);
1588 break;
1589 }
1590 }
1591 ovs_mutex_unlock(&dpdk_mutex);
1592
1593 if (!exists) {
1594 VLOG_INFO("vHost Device '%s' (%ld) can't be added - name not found",
1595 dev->ifname, dev->device_fh);
1596
1597 return -1;
1598 }
1599
1600 VLOG_INFO("vHost Device '%s' (%ld) has been added",
1601 dev->ifname, dev->device_fh);
1602 return 0;
1603}
1604
1605/*
1606 * Remove a virtio-net device from the specific vhost port. Use dev->remove
1607 * flag to stop any more packets from being sent or received to/from a VM and
1608 * ensure all currently queued packets have been sent/received before removing
1609 * the device.
1610 */
1611static void
1612destroy_device(volatile struct virtio_net *dev)
1613{
1614 struct netdev_dpdk *vhost_dev;
1615
1616 ovs_mutex_lock(&dpdk_mutex);
1617 LIST_FOR_EACH (vhost_dev, list_node, &dpdk_list) {
1618 if (netdev_dpdk_get_virtio(vhost_dev) == dev) {
1619
1620 ovs_mutex_lock(&vhost_dev->mutex);
1621 dev->flags &= ~VIRTIO_DEV_RUNNING;
1622 ovsrcu_set(&vhost_dev->virtio_dev, NULL);
1623 ovs_mutex_unlock(&vhost_dev->mutex);
1624
1625 /*
1626 * Wait for other threads to quiesce before
1627 * setting the virtio_dev to NULL.
1628 */
1629 ovsrcu_synchronize();
618f44f7
KT
1630 /*
1631 * As call to ovsrcu_synchronize() will end the quiescent state,
1632 * put thread back into quiescent state before returning.
1633 */
1634 ovsrcu_quiesce_start();
58397e6c
KT
1635 }
1636 }
1637 ovs_mutex_unlock(&dpdk_mutex);
1638
1639 VLOG_INFO("vHost Device '%s' (%ld) has been removed",
1640 dev->ifname, dev->device_fh);
1641}
1642
1643struct virtio_net *
1644netdev_dpdk_get_virtio(const struct netdev_dpdk *dev)
1645{
1646 return ovsrcu_get(struct virtio_net *, &dev->virtio_dev);
1647}
1648
1649/*
1650 * These callbacks allow virtio-net devices to be added to vhost ports when
1651 * configuration has been fully complete.
1652 */
1653const struct virtio_net_device_ops virtio_net_device_ops =
1654{
1655 .new_device = new_device,
1656 .destroy_device = destroy_device,
1657};
1658
1659static void *
1660start_cuse_session_loop(void *dummy OVS_UNUSED)
1661{
1662 pthread_detach(pthread_self());
618f44f7
KT
1663 /* Put the cuse thread into quiescent state. */
1664 ovsrcu_quiesce_start();
58397e6c
KT
1665 rte_vhost_driver_session_start();
1666 return NULL;
1667}
1668
1669static int
1670dpdk_vhost_class_init(void)
1671{
58397e6c
KT
1672 int err = -1;
1673
1674 rte_vhost_driver_callback_register(&virtio_net_device_ops);
1675
1676 /* Register CUSE device to handle IOCTLs.
1677 * Unless otherwise specified on the vswitchd command line, cuse_dev_name
1678 * is set to vhost-net.
1679 */
1680 err = rte_vhost_driver_register(cuse_dev_name);
1681
1682 if (err != 0) {
1683 VLOG_ERR("CUSE device setup failure.");
1684 return -1;
1685 }
1686
618f44f7
KT
1687 ovs_thread_create("cuse_thread", start_cuse_session_loop, NULL);
1688 return 0;
58397e6c
KT
1689}
1690
033e9df2
DDP
1691static void
1692dpdk_common_init(void)
1693{
1694 unixctl_command_register("netdev-dpdk/set-admin-state",
1695 "[netdev] up|down", 1, 2,
1696 netdev_dpdk_set_admin_state, NULL);
1697
1698 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
1699}
1700
95fb793a 1701/* Client Rings */
1702
95fb793a 1703static int
1704dpdk_ring_create(const char dev_name[], unsigned int port_no,
1705 unsigned int *eth_port_id)
1706{
1707 struct dpdk_ring *ivshmem;
1708 char ring_name[10];
1709 int err;
1710
1711 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
1712 if (ivshmem == NULL) {
1713 return ENOMEM;
1714 }
1715
7251515e 1716 /* XXX: Add support for multiquque ring. */
95fb793a 1717 err = snprintf(ring_name, 10, "%s_tx", dev_name);
1718 if (err < 0) {
1719 return -err;
1720 }
1721
7251515e
DV
1722 /* Create single consumer/producer rings, netdev does explicit locking. */
1723 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
1724 RING_F_SP_ENQ | RING_F_SC_DEQ);
95fb793a 1725 if (ivshmem->cring_tx == NULL) {
1726 rte_free(ivshmem);
1727 return ENOMEM;
1728 }
1729
1730 err = snprintf(ring_name, 10, "%s_rx", dev_name);
1731 if (err < 0) {
1732 return -err;
1733 }
1734
7251515e
DV
1735 /* Create single consumer/producer rings, netdev does explicit locking. */
1736 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
1737 RING_F_SP_ENQ | RING_F_SC_DEQ);
95fb793a 1738 if (ivshmem->cring_rx == NULL) {
1739 rte_free(ivshmem);
1740 return ENOMEM;
1741 }
1742
d7310583
DDP
1743 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
1744 &ivshmem->cring_tx, 1, SOCKET0);
1745
95fb793a 1746 if (err < 0) {
1747 rte_free(ivshmem);
1748 return ENODEV;
1749 }
1750
1751 ivshmem->user_port_id = port_no;
1752 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
1753 list_push_back(&dpdk_ring_list, &ivshmem->list_node);
1754
1755 *eth_port_id = ivshmem->eth_port_id;
1756 return 0;
1757}
1758
1759static int
1760dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
1761{
1762 struct dpdk_ring *ivshmem;
1763 unsigned int port_no;
1764 int err = 0;
1765
1766 /* Names always start with "dpdkr" */
1767 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
1768 if (err) {
1769 return err;
1770 }
1771
1772 /* look through our list to find the device */
1773 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
1774 if (ivshmem->user_port_id == port_no) {
58397e6c 1775 VLOG_INFO("Found dpdk ring device %s:", dev_name);
95fb793a 1776 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
1777 return 0;
1778 }
1779 }
1780 /* Need to create the device rings */
1781 return dpdk_ring_create(dev_name, port_no, eth_port_id);
1782}
1783
7251515e
DV
1784static int
1785netdev_dpdk_ring_send(struct netdev *netdev, int qid OVS_UNUSED,
e14deea0 1786 struct dp_packet **pkts, int cnt, bool may_steal)
7251515e
DV
1787{
1788 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1b99bb05
MG
1789 unsigned i;
1790
1791 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that the
1792 * rss hash field is clear. This is because the same mbuf may be modified by
1793 * the consumer of the ring and return into the datapath without recalculating
1794 * the RSS hash. */
1795 for (i = 0; i < cnt; i++) {
1796 dp_packet_set_rss_hash(pkts[i], 0);
1797 }
7251515e
DV
1798
1799 /* DPDK Rings have a single TX queue, Therefore needs locking. */
58397e6c 1800 rte_spinlock_lock(&dev->txq_lock);
7251515e 1801 netdev_dpdk_send__(dev, 0, pkts, cnt, may_steal);
58397e6c 1802 rte_spinlock_unlock(&dev->txq_lock);
7251515e
DV
1803 return 0;
1804}
1805
95fb793a 1806static int
1807netdev_dpdk_ring_construct(struct netdev *netdev)
1808{
1809 unsigned int port_no = 0;
1810 int err = 0;
1811
1812 if (rte_eal_init_ret) {
1813 return rte_eal_init_ret;
1814 }
1815
1816 ovs_mutex_lock(&dpdk_mutex);
1817
1818 err = dpdk_ring_open(netdev->name, &port_no);
1819 if (err) {
1820 goto unlock_dpdk;
1821 }
1822
58397e6c 1823 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
95fb793a 1824
1825unlock_dpdk:
1826 ovs_mutex_unlock(&dpdk_mutex);
1827 return err;
1828}
1829
58397e6c
KT
1830#define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, MULTIQ, SEND, \
1831 GET_CARRIER, GET_STATS, GET_FEATURES, GET_STATUS, RXQ_RECV) \
95fb793a 1832{ \
1833 NAME, \
1834 INIT, /* init */ \
1835 NULL, /* netdev_dpdk_run */ \
1836 NULL, /* netdev_dpdk_wait */ \
1837 \
1838 netdev_dpdk_alloc, \
1839 CONSTRUCT, \
58397e6c 1840 DESTRUCT, \
95fb793a 1841 netdev_dpdk_dealloc, \
1842 netdev_dpdk_get_config, \
1843 NULL, /* netdev_dpdk_set_config */ \
1844 NULL, /* get_tunnel_config */ \
58397e6c
KT
1845 NULL, /* build header */ \
1846 NULL, /* push header */ \
1847 NULL, /* pop header */ \
7dec44fe 1848 netdev_dpdk_get_numa_id, /* get_numa_id */ \
5496878c 1849 MULTIQ, /* set_multiq */ \
95fb793a 1850 \
7251515e 1851 SEND, /* send */ \
95fb793a 1852 NULL, /* send_wait */ \
1853 \
1854 netdev_dpdk_set_etheraddr, \
1855 netdev_dpdk_get_etheraddr, \
1856 netdev_dpdk_get_mtu, \
1857 netdev_dpdk_set_mtu, \
1858 netdev_dpdk_get_ifindex, \
58397e6c 1859 GET_CARRIER, \
95fb793a 1860 netdev_dpdk_get_carrier_resets, \
1861 netdev_dpdk_set_miimon, \
58397e6c
KT
1862 GET_STATS, \
1863 GET_FEATURES, \
95fb793a 1864 NULL, /* set_advertisements */ \
1865 \
1866 NULL, /* set_policing */ \
1867 NULL, /* get_qos_types */ \
1868 NULL, /* get_qos_capabilities */ \
1869 NULL, /* get_qos */ \
1870 NULL, /* set_qos */ \
1871 NULL, /* get_queue */ \
1872 NULL, /* set_queue */ \
1873 NULL, /* delete_queue */ \
1874 NULL, /* get_queue_stats */ \
1875 NULL, /* queue_dump_start */ \
1876 NULL, /* queue_dump_next */ \
1877 NULL, /* queue_dump_done */ \
1878 NULL, /* dump_queue_stats */ \
1879 \
1880 NULL, /* get_in4 */ \
1881 NULL, /* set_in4 */ \
1882 NULL, /* get_in6 */ \
1883 NULL, /* add_router */ \
1884 NULL, /* get_next_hop */ \
58397e6c 1885 GET_STATUS, \
95fb793a 1886 NULL, /* arp_lookup */ \
1887 \
1888 netdev_dpdk_update_flags, \
1889 \
1890 netdev_dpdk_rxq_alloc, \
1891 netdev_dpdk_rxq_construct, \
1892 netdev_dpdk_rxq_destruct, \
1893 netdev_dpdk_rxq_dealloc, \
58397e6c 1894 RXQ_RECV, \
95fb793a 1895 NULL, /* rx_wait */ \
1896 NULL, /* rxq_drain */ \
1897}
8a9562d2
PS
1898
1899int
1900dpdk_init(int argc, char **argv)
1901{
1902 int result;
58397e6c
KT
1903 int base = 0;
1904 char *pragram_name = argv[0];
8a9562d2 1905
9441caf3 1906 if (argc < 2 || strcmp(argv[1], "--dpdk"))
8a9562d2
PS
1907 return 0;
1908
58397e6c 1909 /* Remove the --dpdk argument from arg list.*/
8a9562d2
PS
1910 argc--;
1911 argv++;
1912
58397e6c
KT
1913 /* If the cuse_dev_name parameter has been provided, set 'cuse_dev_name' to
1914 * this string if it meets the correct criteria. Otherwise, set it to the
1915 * default (vhost-net).
1916 */
1917 if (!strcmp(argv[1], "--cuse_dev_name") &&
1918 (strlen(argv[2]) <= NAME_MAX)) {
1919
1920 cuse_dev_name = strdup(argv[2]);
1921
1922 /* Remove the cuse_dev_name configuration parameters from the argument
1923 * list, so that the correct elements are passed to the DPDK
1924 * initialization function
1925 */
1926 argc -= 2;
1927 argv += 2; /* Increment by two to bypass the cuse_dev_name arguments */
1928 base = 2;
1929
1930 VLOG_ERR("User-provided cuse_dev_name in use: /dev/%s", cuse_dev_name);
1931 } else {
1932 cuse_dev_name = "vhost-net";
1933 VLOG_INFO("No cuse_dev_name provided - defaulting to /dev/vhost-net");
1934 }
1935
1936 /* Keep the program name argument as this is needed for call to
1937 * rte_eal_init()
1938 */
1939 argv[0] = pragram_name;
1940
8a9562d2
PS
1941 /* Make sure things are initialized ... */
1942 result = rte_eal_init(argc, argv);
451450fa 1943 if (result < 0) {
58397e6c 1944 ovs_abort(result, "Cannot init EAL");
451450fa 1945 }
8a9562d2 1946
d7310583 1947 rte_memzone_dump(stdout);
8a9562d2
PS
1948 rte_eal_init_ret = 0;
1949
451450fa 1950 if (argc > result) {
9441caf3 1951 argv[result] = argv[0];
451450fa 1952 }
9441caf3 1953
db73f716
DDP
1954 /* We are called from the main thread here */
1955 thread_set_nonpmd();
1956
58397e6c 1957 return result + 1 + base;
8a9562d2
PS
1958}
1959
95fb793a 1960const struct netdev_class dpdk_class =
1961 NETDEV_DPDK_CLASS(
1962 "dpdk",
b8e57534 1963 NULL,
5496878c 1964 netdev_dpdk_construct,
58397e6c 1965 netdev_dpdk_destruct,
7251515e 1966 netdev_dpdk_set_multiq,
58397e6c
KT
1967 netdev_dpdk_eth_send,
1968 netdev_dpdk_get_carrier,
1969 netdev_dpdk_get_stats,
1970 netdev_dpdk_get_features,
1971 netdev_dpdk_get_status,
1972 netdev_dpdk_rxq_recv);
95fb793a 1973
1974const struct netdev_class dpdk_ring_class =
1975 NETDEV_DPDK_CLASS(
1976 "dpdkr",
033e9df2 1977 NULL,
5496878c 1978 netdev_dpdk_ring_construct,
58397e6c
KT
1979 netdev_dpdk_destruct,
1980 NULL,
1981 netdev_dpdk_ring_send,
1982 netdev_dpdk_get_carrier,
1983 netdev_dpdk_get_stats,
1984 netdev_dpdk_get_features,
1985 netdev_dpdk_get_status,
1986 netdev_dpdk_rxq_recv);
1987
1988const struct netdev_class dpdk_vhost_class =
1989 NETDEV_DPDK_CLASS(
1990 "dpdkvhost",
1991 dpdk_vhost_class_init,
1992 netdev_dpdk_vhost_construct,
1993 netdev_dpdk_vhost_destruct,
1994 netdev_dpdk_vhost_set_multiq,
1995 netdev_dpdk_vhost_send,
1996 netdev_dpdk_vhost_get_carrier,
1997 netdev_dpdk_vhost_get_stats,
1998 NULL,
7251515e 1999 NULL,
58397e6c 2000 netdev_dpdk_vhost_rxq_recv);
95fb793a 2001
8a9562d2
PS
2002void
2003netdev_dpdk_register(void)
2004{
95fb793a 2005 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2006
033e9df2
DDP
2007 if (rte_eal_init_ret) {
2008 return;
2009 }
2010
95fb793a 2011 if (ovsthread_once_start(&once)) {
033e9df2 2012 dpdk_common_init();
95fb793a 2013 netdev_register_provider(&dpdk_class);
2014 netdev_register_provider(&dpdk_ring_class);
58397e6c 2015 netdev_register_provider(&dpdk_vhost_class);
95fb793a 2016 ovsthread_once_done(&once);
2017 }
8a9562d2 2018}
8617afff
PS
2019
2020int
2021pmd_thread_setaffinity_cpu(int cpu)
2022{
2023 cpu_set_t cpuset;
2024 int err;
2025
2026 CPU_ZERO(&cpuset);
2027 CPU_SET(cpu, &cpuset);
2028 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2029 if (err) {
2030 VLOG_ERR("Thread affinity error %d",err);
2031 return err;
2032 }
abb5943d
AW
2033 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
2034 ovs_assert(cpu != NON_PMD_CORE_ID);
65f13b50 2035 RTE_PER_LCORE(_lcore_id) = cpu;
8617afff
PS
2036
2037 return 0;
2038}
db73f716
DDP
2039
2040void
2041thread_set_nonpmd(void)
2042{
abb5943d
AW
2043 /* We have to use NON_PMD_CORE_ID to allow non-pmd threads to perform
2044 * certain DPDK operations, like rte_eth_dev_configure(). */
2045 RTE_PER_LCORE(_lcore_id) = NON_PMD_CORE_ID;
db73f716
DDP
2046}
2047
2048static bool
2049thread_is_pmd(void)
2050{
abb5943d 2051 return rte_lcore_id() != NON_PMD_CORE_ID;
db73f716 2052}