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