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