]> git.proxmox.com Git - ovs.git/blob - lib/netdev-dpdk.c
Move lib/ofp-print.h to include/openvswitch directory
[ovs.git] / lib / netdev-dpdk.c
1 /*
2 * Copyright (c) 2014, 2015, 2016 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 "fatal-signal.h"
37 #include "netdev-dpdk.h"
38 #include "netdev-provider.h"
39 #include "netdev-vport.h"
40 #include "odp-util.h"
41 #include "openvswitch/list.h"
42 #include "openvswitch/ofp-print.h"
43 #include "openvswitch/vlog.h"
44 #include "ovs-numa.h"
45 #include "ovs-thread.h"
46 #include "ovs-rcu.h"
47 #include "packets.h"
48 #include "shash.h"
49 #include "smap.h"
50 #include "sset.h"
51 #include "unaligned.h"
52 #include "timeval.h"
53 #include "unixctl.h"
54
55 #include "rte_config.h"
56 #include "rte_mbuf.h"
57 #include "rte_meter.h"
58 #include "rte_virtio_net.h"
59
60 VLOG_DEFINE_THIS_MODULE(dpdk);
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
62
63 #define DPDK_PORT_WATCHDOG_INTERVAL 5
64
65 #define OVS_CACHE_LINE_SIZE CACHE_LINE_SIZE
66 #define OVS_VPORT_DPDK "ovs_dpdk"
67
68 /*
69 * need to reserve tons of extra space in the mbufs so we can align the
70 * DMA addresses to 4KB.
71 * The minimum mbuf size is limited to avoid scatter behaviour and drop in
72 * performance for standard Ethernet MTU.
73 */
74 #define ETHER_HDR_MAX_LEN (ETHER_HDR_LEN + ETHER_CRC_LEN + (2 * VLAN_HEADER_LEN))
75 #define MTU_TO_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_LEN + ETHER_CRC_LEN)
76 #define MTU_TO_MAX_FRAME_LEN(mtu) ((mtu) + ETHER_HDR_MAX_LEN)
77 #define FRAME_LEN_TO_MTU(frame_len) ((frame_len)- ETHER_HDR_LEN - ETHER_CRC_LEN)
78 #define MBUF_SIZE(mtu) ( MTU_TO_MAX_FRAME_LEN(mtu) \
79 + sizeof(struct dp_packet) \
80 + RTE_PKTMBUF_HEADROOM)
81 #define NETDEV_DPDK_MBUF_ALIGN 1024
82
83 /* Max and min number of packets in the mempool. OVS tries to allocate a
84 * mempool with MAX_NB_MBUF: if this fails (because the system doesn't have
85 * enough hugepages) we keep halving the number until the allocation succeeds
86 * or we reach MIN_NB_MBUF */
87
88 #define MAX_NB_MBUF (4096 * 64)
89 #define MIN_NB_MBUF (4096 * 4)
90 #define MP_CACHE_SZ RTE_MEMPOOL_CACHE_MAX_SIZE
91
92 /* MAX_NB_MBUF can be divided by 2 many times, until MIN_NB_MBUF */
93 BUILD_ASSERT_DECL(MAX_NB_MBUF % ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF) == 0);
94
95 /* The smallest possible NB_MBUF that we're going to try should be a multiple
96 * of MP_CACHE_SZ. This is advised by DPDK documentation. */
97 BUILD_ASSERT_DECL((MAX_NB_MBUF / ROUND_DOWN_POW2(MAX_NB_MBUF/MIN_NB_MBUF))
98 % MP_CACHE_SZ == 0);
99
100 #define SOCKET0 0
101
102 #define NIC_PORT_RX_Q_SIZE 2048 /* Size of Physical NIC RX Queue, Max (n+32<=4096)*/
103 #define NIC_PORT_TX_Q_SIZE 2048 /* Size of Physical NIC TX Queue, Max (n+32<=4096)*/
104
105 #define OVS_VHOST_MAX_QUEUE_NUM 1024 /* Maximum number of vHost TX queues. */
106 #define OVS_VHOST_QUEUE_MAP_UNKNOWN (-1) /* Mapping not initialized. */
107 #define OVS_VHOST_QUEUE_DISABLED (-2) /* Queue was disabled by guest and not
108 * yet mapped to another queue. */
109
110 static char *cuse_dev_name = NULL; /* Character device cuse_dev_name. */
111 static char *vhost_sock_dir = NULL; /* Location of vhost-user sockets */
112
113 /*
114 * Maximum amount of time in micro seconds to try and enqueue to vhost.
115 */
116 #define VHOST_ENQ_RETRY_USECS 100
117
118 static const struct rte_eth_conf port_conf = {
119 .rxmode = {
120 .mq_mode = ETH_MQ_RX_RSS,
121 .split_hdr_size = 0,
122 .header_split = 0, /* Header Split disabled */
123 .hw_ip_checksum = 0, /* IP checksum offload disabled */
124 .hw_vlan_filter = 0, /* VLAN filtering disabled */
125 .jumbo_frame = 0, /* Jumbo Frame Support disabled */
126 .hw_strip_crc = 0,
127 },
128 .rx_adv_conf = {
129 .rss_conf = {
130 .rss_key = NULL,
131 .rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP,
132 },
133 },
134 .txmode = {
135 .mq_mode = ETH_MQ_TX_NONE,
136 },
137 };
138
139 enum { MAX_TX_QUEUE_LEN = 384 };
140 enum { DPDK_RING_SIZE = 256 };
141 BUILD_ASSERT_DECL(IS_POW2(DPDK_RING_SIZE));
142 enum { DRAIN_TSC = 200000ULL };
143
144 enum dpdk_dev_type {
145 DPDK_DEV_ETH = 0,
146 DPDK_DEV_VHOST = 1,
147 };
148
149 static int rte_eal_init_ret = ENODEV;
150
151 static struct ovs_mutex dpdk_mutex = OVS_MUTEX_INITIALIZER;
152
153 /* Quality of Service */
154
155 /* An instance of a QoS configuration. Always associated with a particular
156 * network device.
157 *
158 * Each QoS implementation subclasses this with whatever additional data it
159 * needs.
160 */
161 struct qos_conf {
162 const struct dpdk_qos_ops *ops;
163 };
164
165 /* A particular implementation of dpdk QoS operations.
166 *
167 * The functions below return 0 if successful or a positive errno value on
168 * failure, except where otherwise noted. All of them must be provided, except
169 * where otherwise noted.
170 */
171 struct dpdk_qos_ops {
172
173 /* Name of the QoS type */
174 const char *qos_name;
175
176 /* Called to construct the QoS implementation on 'netdev'. The
177 * implementation should make the appropriate calls to configure QoS
178 * according to 'details'. The implementation may assume that any current
179 * QoS configuration already installed should be destroyed before
180 * constructing the new configuration.
181 *
182 * The contents of 'details' should be documented as valid for 'ovs_name'
183 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
184 * (which is built as ovs-vswitchd.conf.db(8)).
185 *
186 * This function must return 0 if and only if it sets 'netdev->qos_conf'
187 * to an initialized 'struct qos_conf'.
188 *
189 * For all QoS implementations it should always be non-null.
190 */
191 int (*qos_construct)(struct netdev *netdev, const struct smap *details);
192
193 /* Destroys the data structures allocated by the implementation as part of
194 * 'qos_conf.
195 *
196 * For all QoS implementations it should always be non-null.
197 */
198 void (*qos_destruct)(struct netdev *netdev, struct qos_conf *conf);
199
200 /* Retrieves details of 'netdev->qos_conf' configuration into 'details'.
201 *
202 * The contents of 'details' should be documented as valid for 'ovs_name'
203 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
204 * (which is built as ovs-vswitchd.conf.db(8)).
205 */
206 int (*qos_get)(const struct netdev *netdev, struct smap *details);
207
208 /* Reconfigures 'netdev->qos_conf' according to 'details', performing any
209 * required calls to complete the reconfiguration.
210 *
211 * The contents of 'details' should be documented as valid for 'ovs_name'
212 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
213 * (which is built as ovs-vswitchd.conf.db(8)).
214 *
215 * This function may be null if 'qos_conf' is not configurable.
216 */
217 int (*qos_set)(struct netdev *netdev, const struct smap *details);
218
219 /* Modify an array of rte_mbufs. The modification is specific to
220 * each qos implementation.
221 *
222 * The function should take and array of mbufs and an int representing
223 * the current number of mbufs present in the array.
224 *
225 * After the function has performed a qos modification to the array of
226 * mbufs it returns an int representing the number of mbufs now present in
227 * the array. This value is can then be passed to the port send function
228 * along with the modified array for transmission.
229 *
230 * For all QoS implementations it should always be non-null.
231 */
232 int (*qos_run)(struct netdev *netdev, struct rte_mbuf **pkts,
233 int pkt_cnt);
234 };
235
236 /* dpdk_qos_ops for each type of user space QoS implementation */
237 static const struct dpdk_qos_ops egress_policer_ops;
238
239 /*
240 * Array of dpdk_qos_ops, contains pointer to all supported QoS
241 * operations.
242 */
243 static const struct dpdk_qos_ops *const qos_confs[] = {
244 &egress_policer_ops,
245 NULL
246 };
247
248 /* Contains all 'struct dpdk_dev's. */
249 static struct ovs_list dpdk_list OVS_GUARDED_BY(dpdk_mutex)
250 = OVS_LIST_INITIALIZER(&dpdk_list);
251
252 static struct ovs_list dpdk_mp_list OVS_GUARDED_BY(dpdk_mutex)
253 = OVS_LIST_INITIALIZER(&dpdk_mp_list);
254
255 /* This mutex must be used by non pmd threads when allocating or freeing
256 * mbufs through mempools. Since dpdk_queue_pkts() and dpdk_queue_flush() may
257 * use mempools, a non pmd thread should hold this mutex while calling them */
258 static struct ovs_mutex nonpmd_mempool_mutex = OVS_MUTEX_INITIALIZER;
259
260 struct dpdk_mp {
261 struct rte_mempool *mp;
262 int mtu;
263 int socket_id;
264 int refcount;
265 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
266 };
267
268 /* There should be one 'struct dpdk_tx_queue' created for
269 * each cpu core. */
270 struct dpdk_tx_queue {
271 bool flush_tx; /* Set to true to flush queue everytime */
272 /* pkts are queued. */
273 int count;
274 rte_spinlock_t tx_lock; /* Protects the members and the NIC queue
275 * from concurrent access. It is used only
276 * if the queue is shared among different
277 * pmd threads (see 'txq_needs_locking'). */
278 int map; /* Mapping of configured vhost-user queues
279 * to enabled by guest. */
280 uint64_t tsc;
281 struct rte_mbuf *burst_pkts[MAX_TX_QUEUE_LEN];
282 };
283
284 /* dpdk has no way to remove dpdk ring ethernet devices
285 so we have to keep them around once they've been created
286 */
287
288 static struct ovs_list dpdk_ring_list OVS_GUARDED_BY(dpdk_mutex)
289 = OVS_LIST_INITIALIZER(&dpdk_ring_list);
290
291 struct dpdk_ring {
292 /* For the client rings */
293 struct rte_ring *cring_tx;
294 struct rte_ring *cring_rx;
295 unsigned int user_port_id; /* User given port no, parsed from port name */
296 int eth_port_id; /* ethernet device port id */
297 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
298 };
299
300 struct netdev_dpdk {
301 struct netdev up;
302 int port_id;
303 int max_packet_len;
304 enum dpdk_dev_type type;
305
306 struct dpdk_tx_queue *tx_q;
307
308 struct ovs_mutex mutex OVS_ACQ_AFTER(dpdk_mutex);
309
310 struct dpdk_mp *dpdk_mp;
311 int mtu;
312 int socket_id;
313 int buf_size;
314 struct netdev_stats stats;
315 /* Protects stats */
316 rte_spinlock_t stats_lock;
317
318 struct eth_addr hwaddr;
319 enum netdev_flags flags;
320
321 struct rte_eth_link link;
322 int link_reset_cnt;
323
324 /* The user might request more txqs than the NIC has. We remap those
325 * ('up.n_txq') on these ('real_n_txq').
326 * If the numbers match, 'txq_needs_locking' is false, otherwise it is
327 * true and we will take a spinlock on transmission */
328 int real_n_txq;
329 int real_n_rxq;
330 bool txq_needs_locking;
331
332 /* virtio-net structure for vhost device */
333 OVSRCU_TYPE(struct virtio_net *) virtio_dev;
334
335 /* Identifier used to distinguish vhost devices from each other */
336 char vhost_id[PATH_MAX];
337
338 /* In dpdk_list. */
339 struct ovs_list list_node OVS_GUARDED_BY(dpdk_mutex);
340
341 /* QoS configuration and lock for the device */
342 struct qos_conf *qos_conf;
343 rte_spinlock_t qos_lock;
344
345 };
346
347 struct netdev_rxq_dpdk {
348 struct netdev_rxq up;
349 int port_id;
350 };
351
352 static bool dpdk_thread_is_pmd(void);
353
354 static int netdev_dpdk_construct(struct netdev *);
355
356 struct virtio_net * netdev_dpdk_get_virtio(const struct netdev_dpdk *dev);
357
358 static bool
359 is_dpdk_class(const struct netdev_class *class)
360 {
361 return class->construct == netdev_dpdk_construct;
362 }
363
364 /* DPDK NIC drivers allocate RX buffers at a particular granularity, typically
365 * aligned at 1k or less. If a declared mbuf size is not a multiple of this
366 * value, insufficient buffers are allocated to accomodate the packet in its
367 * entirety. Furthermore, certain drivers need to ensure that there is also
368 * sufficient space in the Rx buffer to accommodate two VLAN tags (for QinQ
369 * frames). If the RX buffer is too small, then the driver enables scatter RX
370 * behaviour, which reduces performance. To prevent this, use a buffer size that
371 * is closest to 'mtu', but which satisfies the aforementioned criteria.
372 */
373 static uint32_t
374 dpdk_buf_size(int mtu)
375 {
376 return ROUND_UP((MTU_TO_MAX_FRAME_LEN(mtu) + RTE_PKTMBUF_HEADROOM),
377 NETDEV_DPDK_MBUF_ALIGN);
378 }
379
380 /* XXX: use dpdk malloc for entire OVS. in fact huge page should be used
381 * for all other segments data, bss and text. */
382
383 static void *
384 dpdk_rte_mzalloc(size_t sz)
385 {
386 void *ptr;
387
388 ptr = rte_zmalloc(OVS_VPORT_DPDK, sz, OVS_CACHE_LINE_SIZE);
389 if (ptr == NULL) {
390 out_of_memory();
391 }
392 return ptr;
393 }
394
395 /* XXX this function should be called only by pmd threads (or by non pmd
396 * threads holding the nonpmd_mempool_mutex) */
397 void
398 free_dpdk_buf(struct dp_packet *p)
399 {
400 struct rte_mbuf *pkt = (struct rte_mbuf *) p;
401
402 rte_pktmbuf_free(pkt);
403 }
404
405 static void
406 ovs_rte_pktmbuf_init(struct rte_mempool *mp,
407 void *opaque_arg OVS_UNUSED,
408 void *_m,
409 unsigned i OVS_UNUSED)
410 {
411 struct rte_mbuf *m = _m;
412
413 rte_pktmbuf_init(mp, opaque_arg, _m, i);
414
415 dp_packet_init_dpdk((struct dp_packet *) m, m->buf_len);
416 }
417
418 static struct dpdk_mp *
419 dpdk_mp_get(int socket_id, int mtu) OVS_REQUIRES(dpdk_mutex)
420 {
421 struct dpdk_mp *dmp = NULL;
422 char mp_name[RTE_MEMPOOL_NAMESIZE];
423 unsigned mp_size;
424 struct rte_pktmbuf_pool_private mbp_priv;
425
426 LIST_FOR_EACH (dmp, list_node, &dpdk_mp_list) {
427 if (dmp->socket_id == socket_id && dmp->mtu == mtu) {
428 dmp->refcount++;
429 return dmp;
430 }
431 }
432
433 dmp = dpdk_rte_mzalloc(sizeof *dmp);
434 dmp->socket_id = socket_id;
435 dmp->mtu = mtu;
436 dmp->refcount = 1;
437 mbp_priv.mbuf_data_room_size = MBUF_SIZE(mtu) - sizeof(struct dp_packet);
438 mbp_priv.mbuf_priv_size = sizeof (struct dp_packet) - sizeof (struct rte_mbuf);
439
440 mp_size = MAX_NB_MBUF;
441 do {
442 if (snprintf(mp_name, RTE_MEMPOOL_NAMESIZE, "ovs_mp_%d_%d_%u",
443 dmp->mtu, dmp->socket_id, mp_size) < 0) {
444 return NULL;
445 }
446
447 dmp->mp = rte_mempool_create(mp_name, mp_size, MBUF_SIZE(mtu),
448 MP_CACHE_SZ,
449 sizeof(struct rte_pktmbuf_pool_private),
450 rte_pktmbuf_pool_init, &mbp_priv,
451 ovs_rte_pktmbuf_init, NULL,
452 socket_id, 0);
453 } while (!dmp->mp && rte_errno == ENOMEM && (mp_size /= 2) >= MIN_NB_MBUF);
454
455 if (dmp->mp == NULL) {
456 return NULL;
457 } else {
458 VLOG_DBG("Allocated \"%s\" mempool with %u mbufs", mp_name, mp_size );
459 }
460
461 ovs_list_push_back(&dpdk_mp_list, &dmp->list_node);
462 return dmp;
463 }
464
465 static void
466 dpdk_mp_put(struct dpdk_mp *dmp)
467 {
468
469 if (!dmp) {
470 return;
471 }
472
473 dmp->refcount--;
474 ovs_assert(dmp->refcount >= 0);
475
476 #if 0
477 /* I could not find any API to destroy mp. */
478 if (dmp->refcount == 0) {
479 list_delete(dmp->list_node);
480 /* destroy mp-pool. */
481 }
482 #endif
483 }
484
485 static void
486 check_link_status(struct netdev_dpdk *dev)
487 {
488 struct rte_eth_link link;
489
490 rte_eth_link_get_nowait(dev->port_id, &link);
491
492 if (dev->link.link_status != link.link_status) {
493 netdev_change_seq_changed(&dev->up);
494
495 dev->link_reset_cnt++;
496 dev->link = link;
497 if (dev->link.link_status) {
498 VLOG_DBG_RL(&rl, "Port %d Link Up - speed %u Mbps - %s",
499 dev->port_id, (unsigned)dev->link.link_speed,
500 (dev->link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
501 ("full-duplex") : ("half-duplex"));
502 } else {
503 VLOG_DBG_RL(&rl, "Port %d Link Down", dev->port_id);
504 }
505 }
506 }
507
508 static void *
509 dpdk_watchdog(void *dummy OVS_UNUSED)
510 {
511 struct netdev_dpdk *dev;
512
513 pthread_detach(pthread_self());
514
515 for (;;) {
516 ovs_mutex_lock(&dpdk_mutex);
517 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
518 ovs_mutex_lock(&dev->mutex);
519 check_link_status(dev);
520 ovs_mutex_unlock(&dev->mutex);
521 }
522 ovs_mutex_unlock(&dpdk_mutex);
523 xsleep(DPDK_PORT_WATCHDOG_INTERVAL);
524 }
525
526 return NULL;
527 }
528
529 static int
530 dpdk_eth_dev_queue_setup(struct netdev_dpdk *dev, int n_rxq, int n_txq)
531 {
532 int diag = 0;
533 int i;
534
535 /* A device may report more queues than it makes available (this has
536 * been observed for Intel xl710, which reserves some of them for
537 * SRIOV): rte_eth_*_queue_setup will fail if a queue is not
538 * available. When this happens we can retry the configuration
539 * and request less queues */
540 while (n_rxq && n_txq) {
541 if (diag) {
542 VLOG_INFO("Retrying setup with (rxq:%d txq:%d)", n_rxq, n_txq);
543 }
544
545 diag = rte_eth_dev_configure(dev->port_id, n_rxq, n_txq, &port_conf);
546 if (diag) {
547 break;
548 }
549
550 for (i = 0; i < n_txq; i++) {
551 diag = rte_eth_tx_queue_setup(dev->port_id, i, NIC_PORT_TX_Q_SIZE,
552 dev->socket_id, NULL);
553 if (diag) {
554 VLOG_INFO("Interface %s txq(%d) setup error: %s",
555 dev->up.name, i, rte_strerror(-diag));
556 break;
557 }
558 }
559
560 if (i != n_txq) {
561 /* Retry with less tx queues */
562 n_txq = i;
563 continue;
564 }
565
566 for (i = 0; i < n_rxq; i++) {
567 diag = rte_eth_rx_queue_setup(dev->port_id, i, NIC_PORT_RX_Q_SIZE,
568 dev->socket_id, NULL,
569 dev->dpdk_mp->mp);
570 if (diag) {
571 VLOG_INFO("Interface %s rxq(%d) setup error: %s",
572 dev->up.name, i, rte_strerror(-diag));
573 break;
574 }
575 }
576
577 if (i != n_rxq) {
578 /* Retry with less rx queues */
579 n_rxq = i;
580 continue;
581 }
582
583 dev->up.n_rxq = n_rxq;
584 dev->real_n_txq = n_txq;
585
586 return 0;
587 }
588
589 return diag;
590 }
591
592
593 static int
594 dpdk_eth_dev_init(struct netdev_dpdk *dev) OVS_REQUIRES(dpdk_mutex)
595 {
596 struct rte_pktmbuf_pool_private *mbp_priv;
597 struct rte_eth_dev_info info;
598 struct ether_addr eth_addr;
599 int diag;
600 int n_rxq, n_txq;
601
602 if (dev->port_id < 0 || dev->port_id >= rte_eth_dev_count()) {
603 return ENODEV;
604 }
605
606 rte_eth_dev_info_get(dev->port_id, &info);
607
608 n_rxq = MIN(info.max_rx_queues, dev->up.n_rxq);
609 n_txq = MIN(info.max_tx_queues, dev->up.n_txq);
610
611 diag = dpdk_eth_dev_queue_setup(dev, n_rxq, n_txq);
612 if (diag) {
613 VLOG_ERR("Interface %s(rxq:%d txq:%d) configure error: %s",
614 dev->up.name, n_rxq, n_txq, rte_strerror(-diag));
615 return -diag;
616 }
617
618 diag = rte_eth_dev_start(dev->port_id);
619 if (diag) {
620 VLOG_ERR("Interface %s start error: %s", dev->up.name,
621 rte_strerror(-diag));
622 return -diag;
623 }
624
625 rte_eth_promiscuous_enable(dev->port_id);
626 rte_eth_allmulticast_enable(dev->port_id);
627
628 memset(&eth_addr, 0x0, sizeof(eth_addr));
629 rte_eth_macaddr_get(dev->port_id, &eth_addr);
630 VLOG_INFO_RL(&rl, "Port %d: "ETH_ADDR_FMT"",
631 dev->port_id, ETH_ADDR_BYTES_ARGS(eth_addr.addr_bytes));
632
633 memcpy(dev->hwaddr.ea, eth_addr.addr_bytes, ETH_ADDR_LEN);
634 rte_eth_link_get_nowait(dev->port_id, &dev->link);
635
636 mbp_priv = rte_mempool_get_priv(dev->dpdk_mp->mp);
637 dev->buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
638
639 dev->flags = NETDEV_UP | NETDEV_PROMISC;
640 return 0;
641 }
642
643 static struct netdev_dpdk *
644 netdev_dpdk_cast(const struct netdev *netdev)
645 {
646 return CONTAINER_OF(netdev, struct netdev_dpdk, up);
647 }
648
649 static struct netdev *
650 netdev_dpdk_alloc(void)
651 {
652 struct netdev_dpdk *dev = dpdk_rte_mzalloc(sizeof *dev);
653 return &dev->up;
654 }
655
656 static void
657 netdev_dpdk_alloc_txq(struct netdev_dpdk *dev, unsigned int n_txqs)
658 {
659 unsigned i;
660
661 dev->tx_q = dpdk_rte_mzalloc(n_txqs * sizeof *dev->tx_q);
662 for (i = 0; i < n_txqs; i++) {
663 int numa_id = ovs_numa_get_numa_id(i);
664
665 if (!dev->txq_needs_locking) {
666 /* Each index is considered as a cpu core id, since there should
667 * be one tx queue for each cpu core. If the corresponding core
668 * is not on the same numa node as 'dev', flags the
669 * 'flush_tx'. */
670 dev->tx_q[i].flush_tx = dev->socket_id == numa_id;
671 } else {
672 /* Queues are shared among CPUs. Always flush */
673 dev->tx_q[i].flush_tx = true;
674 }
675
676 /* Initialize map for vhost devices. */
677 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
678 rte_spinlock_init(&dev->tx_q[i].tx_lock);
679 }
680 }
681
682 static int
683 netdev_dpdk_init(struct netdev *netdev, unsigned int port_no,
684 enum dpdk_dev_type type)
685 OVS_REQUIRES(dpdk_mutex)
686 {
687 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
688 int sid;
689 int err = 0;
690 uint32_t buf_size;
691
692 ovs_mutex_init(&dev->mutex);
693 ovs_mutex_lock(&dev->mutex);
694
695 rte_spinlock_init(&dev->stats_lock);
696
697 /* If the 'sid' is negative, it means that the kernel fails
698 * to obtain the pci numa info. In that situation, always
699 * use 'SOCKET0'. */
700 if (type == DPDK_DEV_ETH) {
701 sid = rte_eth_dev_socket_id(port_no);
702 } else {
703 sid = rte_lcore_to_socket_id(rte_get_master_lcore());
704 }
705
706 dev->socket_id = sid < 0 ? SOCKET0 : sid;
707 dev->port_id = port_no;
708 dev->type = type;
709 dev->flags = 0;
710 dev->mtu = ETHER_MTU;
711 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
712
713 buf_size = dpdk_buf_size(dev->mtu);
714 dev->dpdk_mp = dpdk_mp_get(dev->socket_id, FRAME_LEN_TO_MTU(buf_size));
715 if (!dev->dpdk_mp) {
716 err = ENOMEM;
717 goto unlock;
718 }
719
720 /* Initialise QoS configuration to NULL and qos lock to unlocked */
721 dev->qos_conf = NULL;
722 rte_spinlock_init(&dev->qos_lock);
723
724 netdev->n_txq = NR_QUEUE;
725 netdev->n_rxq = NR_QUEUE;
726 netdev->requested_n_rxq = NR_QUEUE;
727 dev->real_n_txq = NR_QUEUE;
728
729 if (type == DPDK_DEV_ETH) {
730 netdev_dpdk_alloc_txq(dev, NR_QUEUE);
731 err = dpdk_eth_dev_init(dev);
732 if (err) {
733 goto unlock;
734 }
735 } else {
736 netdev_dpdk_alloc_txq(dev, OVS_VHOST_MAX_QUEUE_NUM);
737 }
738
739 ovs_list_push_back(&dpdk_list, &dev->list_node);
740
741 unlock:
742 if (err) {
743 rte_free(dev->tx_q);
744 }
745 ovs_mutex_unlock(&dev->mutex);
746 return err;
747 }
748
749 /* dev_name must be the prefix followed by a positive decimal number.
750 * (no leading + or - signs are allowed) */
751 static int
752 dpdk_dev_parse_name(const char dev_name[], const char prefix[],
753 unsigned int *port_no)
754 {
755 const char *cport;
756
757 if (strncmp(dev_name, prefix, strlen(prefix))) {
758 return ENODEV;
759 }
760
761 cport = dev_name + strlen(prefix);
762
763 if (str_to_uint(cport, 10, port_no)) {
764 return 0;
765 } else {
766 return ENODEV;
767 }
768 }
769
770 static int
771 vhost_construct_helper(struct netdev *netdev) OVS_REQUIRES(dpdk_mutex)
772 {
773 if (rte_eal_init_ret) {
774 return rte_eal_init_ret;
775 }
776
777 return netdev_dpdk_init(netdev, -1, DPDK_DEV_VHOST);
778 }
779
780 static int
781 netdev_dpdk_vhost_cuse_construct(struct netdev *netdev)
782 {
783 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
784 int err;
785
786 ovs_mutex_lock(&dpdk_mutex);
787 strncpy(dev->vhost_id, netdev->name, sizeof(dev->vhost_id));
788 err = vhost_construct_helper(netdev);
789 ovs_mutex_unlock(&dpdk_mutex);
790 return err;
791 }
792
793 static int
794 netdev_dpdk_vhost_user_construct(struct netdev *netdev)
795 {
796 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
797 const char *name = netdev->name;
798 int err;
799
800 /* 'name' is appended to 'vhost_sock_dir' and used to create a socket in
801 * the file system. '/' or '\' would traverse directories, so they're not
802 * acceptable in 'name'. */
803 if (strchr(name, '/') || strchr(name, '\\')) {
804 VLOG_ERR("\"%s\" is not a valid name for a vhost-user port. "
805 "A valid name must not include '/' or '\\'",
806 name);
807 return EINVAL;
808 }
809
810 ovs_mutex_lock(&dpdk_mutex);
811 /* Take the name of the vhost-user port and append it to the location where
812 * the socket is to be created, then register the socket.
813 */
814 snprintf(dev->vhost_id, sizeof(dev->vhost_id), "%s/%s",
815 vhost_sock_dir, name);
816
817 err = rte_vhost_driver_register(dev->vhost_id);
818 if (err) {
819 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
820 dev->vhost_id);
821 } else {
822 fatal_signal_add_file_to_unlink(dev->vhost_id);
823 VLOG_INFO("Socket %s created for vhost-user port %s\n",
824 dev->vhost_id, name);
825 err = vhost_construct_helper(netdev);
826 }
827
828 ovs_mutex_unlock(&dpdk_mutex);
829 return err;
830 }
831
832 static int
833 netdev_dpdk_construct(struct netdev *netdev)
834 {
835 unsigned int port_no;
836 int err;
837
838 if (rte_eal_init_ret) {
839 return rte_eal_init_ret;
840 }
841
842 /* Names always start with "dpdk" */
843 err = dpdk_dev_parse_name(netdev->name, "dpdk", &port_no);
844 if (err) {
845 return err;
846 }
847
848 ovs_mutex_lock(&dpdk_mutex);
849 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
850 ovs_mutex_unlock(&dpdk_mutex);
851 return err;
852 }
853
854 static void
855 netdev_dpdk_destruct(struct netdev *netdev)
856 {
857 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
858
859 ovs_mutex_lock(&dev->mutex);
860 rte_eth_dev_stop(dev->port_id);
861 ovs_mutex_unlock(&dev->mutex);
862
863 ovs_mutex_lock(&dpdk_mutex);
864 rte_free(dev->tx_q);
865 ovs_list_remove(&dev->list_node);
866 dpdk_mp_put(dev->dpdk_mp);
867 ovs_mutex_unlock(&dpdk_mutex);
868 }
869
870 static void
871 netdev_dpdk_vhost_destruct(struct netdev *netdev)
872 {
873 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
874
875 /* Guest becomes an orphan if still attached. */
876 if (netdev_dpdk_get_virtio(dev) != NULL) {
877 VLOG_ERR("Removing port '%s' while vhost device still attached.",
878 netdev->name);
879 VLOG_ERR("To restore connectivity after re-adding of port, VM on socket"
880 " '%s' must be restarted.",
881 dev->vhost_id);
882 }
883
884 if (rte_vhost_driver_unregister(dev->vhost_id)) {
885 VLOG_ERR("Unable to remove vhost-user socket %s", dev->vhost_id);
886 } else {
887 fatal_signal_remove_file_to_unlink(dev->vhost_id);
888 }
889
890 ovs_mutex_lock(&dpdk_mutex);
891 rte_free(dev->tx_q);
892 ovs_list_remove(&dev->list_node);
893 dpdk_mp_put(dev->dpdk_mp);
894 ovs_mutex_unlock(&dpdk_mutex);
895 }
896
897 static void
898 netdev_dpdk_dealloc(struct netdev *netdev)
899 {
900 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
901
902 rte_free(dev);
903 }
904
905 static int
906 netdev_dpdk_get_config(const struct netdev *netdev, struct smap *args)
907 {
908 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
909
910 ovs_mutex_lock(&dev->mutex);
911
912 smap_add_format(args, "requested_rx_queues", "%d", netdev->requested_n_rxq);
913 smap_add_format(args, "configured_rx_queues", "%d", netdev->n_rxq);
914 smap_add_format(args, "requested_tx_queues", "%d", netdev->n_txq);
915 smap_add_format(args, "configured_tx_queues", "%d", dev->real_n_txq);
916 ovs_mutex_unlock(&dev->mutex);
917
918 return 0;
919 }
920
921 static int
922 netdev_dpdk_set_config(struct netdev *netdev, const struct smap *args)
923 {
924 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
925
926 ovs_mutex_lock(&dev->mutex);
927 netdev->requested_n_rxq = MAX(smap_get_int(args, "n_rxq",
928 netdev->requested_n_rxq), 1);
929 netdev_change_seq_changed(netdev);
930 ovs_mutex_unlock(&dev->mutex);
931
932 return 0;
933 }
934
935 static int
936 netdev_dpdk_get_numa_id(const struct netdev *netdev)
937 {
938 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
939
940 return dev->socket_id;
941 }
942
943 /* Sets the number of tx queues and rx queues for the dpdk interface.
944 * If the configuration fails, do not try restoring its old configuration
945 * and just returns the error. */
946 static int
947 netdev_dpdk_set_multiq(struct netdev *netdev, unsigned int n_txq,
948 unsigned int n_rxq)
949 {
950 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
951 int err = 0;
952 int old_rxq, old_txq;
953
954 if (netdev->n_txq == n_txq && netdev->n_rxq == n_rxq) {
955 return err;
956 }
957
958 ovs_mutex_lock(&dpdk_mutex);
959 ovs_mutex_lock(&dev->mutex);
960
961 rte_eth_dev_stop(dev->port_id);
962
963 old_txq = netdev->n_txq;
964 old_rxq = netdev->n_rxq;
965 netdev->n_txq = n_txq;
966 netdev->n_rxq = n_rxq;
967
968 rte_free(dev->tx_q);
969 err = dpdk_eth_dev_init(dev);
970 netdev_dpdk_alloc_txq(dev, dev->real_n_txq);
971 if (err) {
972 /* If there has been an error, it means that the requested queues
973 * have not been created. Restore the old numbers. */
974 netdev->n_txq = old_txq;
975 netdev->n_rxq = old_rxq;
976 }
977
978 dev->txq_needs_locking = dev->real_n_txq != netdev->n_txq;
979
980 ovs_mutex_unlock(&dev->mutex);
981 ovs_mutex_unlock(&dpdk_mutex);
982
983 return err;
984 }
985
986 static int
987 netdev_dpdk_vhost_cuse_set_multiq(struct netdev *netdev, unsigned int n_txq,
988 unsigned int n_rxq)
989 {
990 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
991 int err = 0;
992
993 if (netdev->n_txq == n_txq && netdev->n_rxq == n_rxq) {
994 return err;
995 }
996
997 ovs_mutex_lock(&dpdk_mutex);
998 ovs_mutex_lock(&dev->mutex);
999
1000 netdev->n_txq = n_txq;
1001 dev->real_n_txq = 1;
1002 netdev->n_rxq = 1;
1003 dev->txq_needs_locking = dev->real_n_txq != netdev->n_txq;
1004
1005 ovs_mutex_unlock(&dev->mutex);
1006 ovs_mutex_unlock(&dpdk_mutex);
1007
1008 return err;
1009 }
1010
1011 static int
1012 netdev_dpdk_vhost_set_multiq(struct netdev *netdev, unsigned int n_txq,
1013 unsigned int n_rxq)
1014 {
1015 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1016 int err = 0;
1017
1018 if (netdev->n_txq == n_txq && netdev->n_rxq == n_rxq) {
1019 return err;
1020 }
1021
1022 ovs_mutex_lock(&dpdk_mutex);
1023 ovs_mutex_lock(&dev->mutex);
1024
1025 netdev->n_txq = n_txq;
1026 netdev->n_rxq = n_rxq;
1027
1028 ovs_mutex_unlock(&dev->mutex);
1029 ovs_mutex_unlock(&dpdk_mutex);
1030
1031 return err;
1032 }
1033
1034 static struct netdev_rxq *
1035 netdev_dpdk_rxq_alloc(void)
1036 {
1037 struct netdev_rxq_dpdk *rx = dpdk_rte_mzalloc(sizeof *rx);
1038
1039 return &rx->up;
1040 }
1041
1042 static struct netdev_rxq_dpdk *
1043 netdev_rxq_dpdk_cast(const struct netdev_rxq *rxq)
1044 {
1045 return CONTAINER_OF(rxq, struct netdev_rxq_dpdk, up);
1046 }
1047
1048 static int
1049 netdev_dpdk_rxq_construct(struct netdev_rxq *rxq)
1050 {
1051 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1052 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1053
1054 ovs_mutex_lock(&dev->mutex);
1055 rx->port_id = dev->port_id;
1056 ovs_mutex_unlock(&dev->mutex);
1057
1058 return 0;
1059 }
1060
1061 static void
1062 netdev_dpdk_rxq_destruct(struct netdev_rxq *rxq OVS_UNUSED)
1063 {
1064 }
1065
1066 static void
1067 netdev_dpdk_rxq_dealloc(struct netdev_rxq *rxq)
1068 {
1069 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1070
1071 rte_free(rx);
1072 }
1073
1074 static inline void
1075 dpdk_queue_flush__(struct netdev_dpdk *dev, int qid)
1076 {
1077 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
1078 uint32_t nb_tx = 0;
1079
1080 while (nb_tx != txq->count) {
1081 uint32_t ret;
1082
1083 ret = rte_eth_tx_burst(dev->port_id, qid, txq->burst_pkts + nb_tx,
1084 txq->count - nb_tx);
1085 if (!ret) {
1086 break;
1087 }
1088
1089 nb_tx += ret;
1090 }
1091
1092 if (OVS_UNLIKELY(nb_tx != txq->count)) {
1093 /* free buffers, which we couldn't transmit, one at a time (each
1094 * packet could come from a different mempool) */
1095 int i;
1096
1097 for (i = nb_tx; i < txq->count; i++) {
1098 rte_pktmbuf_free(txq->burst_pkts[i]);
1099 }
1100 rte_spinlock_lock(&dev->stats_lock);
1101 dev->stats.tx_dropped += txq->count-nb_tx;
1102 rte_spinlock_unlock(&dev->stats_lock);
1103 }
1104
1105 txq->count = 0;
1106 txq->tsc = rte_get_timer_cycles();
1107 }
1108
1109 static inline void
1110 dpdk_queue_flush(struct netdev_dpdk *dev, int qid)
1111 {
1112 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
1113
1114 if (txq->count == 0) {
1115 return;
1116 }
1117 dpdk_queue_flush__(dev, qid);
1118 }
1119
1120 static bool
1121 is_vhost_running(struct virtio_net *virtio_dev)
1122 {
1123 return (virtio_dev != NULL && (virtio_dev->flags & VIRTIO_DEV_RUNNING));
1124 }
1125
1126 static inline void
1127 netdev_dpdk_vhost_update_rx_counters(struct netdev_stats *stats,
1128 struct dp_packet **packets, int count)
1129 {
1130 int i;
1131 struct dp_packet *packet;
1132
1133 stats->rx_packets += count;
1134 for (i = 0; i < count; i++) {
1135 packet = packets[i];
1136
1137 if (OVS_UNLIKELY(dp_packet_size(packet) < ETH_HEADER_LEN)) {
1138 /* This only protects the following multicast counting from
1139 * too short packets, but it does not stop the packet from
1140 * further processing. */
1141 stats->rx_errors++;
1142 stats->rx_length_errors++;
1143 continue;
1144 }
1145
1146 struct eth_header *eh = (struct eth_header *) dp_packet_data(packet);
1147 if (OVS_UNLIKELY(eth_addr_is_multicast(eh->eth_dst))) {
1148 stats->multicast++;
1149 }
1150
1151 stats->rx_bytes += dp_packet_size(packet);
1152 }
1153 }
1154
1155 /*
1156 * The receive path for the vhost port is the TX path out from guest.
1157 */
1158 static int
1159 netdev_dpdk_vhost_rxq_recv(struct netdev_rxq *rxq,
1160 struct dp_packet **packets, int *c)
1161 {
1162 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1163 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(dev);
1164 int qid = rxq->queue_id;
1165 uint16_t nb_rx = 0;
1166
1167 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev))) {
1168 return EAGAIN;
1169 }
1170
1171 if (rxq->queue_id >= dev->real_n_rxq) {
1172 return EOPNOTSUPP;
1173 }
1174
1175 nb_rx = rte_vhost_dequeue_burst(virtio_dev, qid * VIRTIO_QNUM + VIRTIO_TXQ,
1176 dev->dpdk_mp->mp,
1177 (struct rte_mbuf **)packets,
1178 NETDEV_MAX_BURST);
1179 if (!nb_rx) {
1180 return EAGAIN;
1181 }
1182
1183 rte_spinlock_lock(&dev->stats_lock);
1184 netdev_dpdk_vhost_update_rx_counters(&dev->stats, packets, nb_rx);
1185 rte_spinlock_unlock(&dev->stats_lock);
1186
1187 *c = (int) nb_rx;
1188 return 0;
1189 }
1190
1191 static int
1192 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq, struct dp_packet **packets,
1193 int *c)
1194 {
1195 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq);
1196 struct netdev_dpdk *dev = netdev_dpdk_cast(rxq->netdev);
1197 int nb_rx;
1198
1199 /* There is only one tx queue for this core. Do not flush other
1200 * queues.
1201 * Do not flush tx queue which is shared among CPUs
1202 * since it is always flushed */
1203 if (rxq->queue_id == rte_lcore_id() &&
1204 OVS_LIKELY(!dev->txq_needs_locking)) {
1205 dpdk_queue_flush(dev, rxq->queue_id);
1206 }
1207
1208 nb_rx = rte_eth_rx_burst(rx->port_id, rxq->queue_id,
1209 (struct rte_mbuf **) packets,
1210 NETDEV_MAX_BURST);
1211 if (!nb_rx) {
1212 return EAGAIN;
1213 }
1214
1215 *c = nb_rx;
1216
1217 return 0;
1218 }
1219
1220 static inline int
1221 netdev_dpdk_qos_run__(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1222 int cnt)
1223 {
1224 struct netdev *netdev = &dev->up;
1225
1226 if (dev->qos_conf != NULL) {
1227 rte_spinlock_lock(&dev->qos_lock);
1228 if (dev->qos_conf != NULL) {
1229 cnt = dev->qos_conf->ops->qos_run(netdev, pkts, cnt);
1230 }
1231 rte_spinlock_unlock(&dev->qos_lock);
1232 }
1233
1234 return cnt;
1235 }
1236
1237 static inline void
1238 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats,
1239 struct dp_packet **packets,
1240 int attempted,
1241 int dropped)
1242 {
1243 int i;
1244 int sent = attempted - dropped;
1245
1246 stats->tx_packets += sent;
1247 stats->tx_dropped += dropped;
1248
1249 for (i = 0; i < sent; i++) {
1250 stats->tx_bytes += dp_packet_size(packets[i]);
1251 }
1252 }
1253
1254 static void
1255 __netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1256 struct dp_packet **pkts, int cnt,
1257 bool may_steal)
1258 {
1259 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1260 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(dev);
1261 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
1262 unsigned int total_pkts = cnt;
1263 unsigned int qos_pkts = cnt;
1264 uint64_t start = 0;
1265
1266 qid = dev->tx_q[qid % dev->real_n_txq].map;
1267
1268 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev) || qid < 0)) {
1269 rte_spinlock_lock(&dev->stats_lock);
1270 dev->stats.tx_dropped+= cnt;
1271 rte_spinlock_unlock(&dev->stats_lock);
1272 goto out;
1273 }
1274
1275 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1276
1277 /* Check has QoS has been configured for the netdev */
1278 cnt = netdev_dpdk_qos_run__(dev, cur_pkts, cnt);
1279 qos_pkts -= cnt;
1280
1281 do {
1282 int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ;
1283 unsigned int tx_pkts;
1284
1285 tx_pkts = rte_vhost_enqueue_burst(virtio_dev, vhost_qid,
1286 cur_pkts, cnt);
1287 if (OVS_LIKELY(tx_pkts)) {
1288 /* Packets have been sent.*/
1289 cnt -= tx_pkts;
1290 /* Prepare for possible next iteration.*/
1291 cur_pkts = &cur_pkts[tx_pkts];
1292 } else {
1293 uint64_t timeout = VHOST_ENQ_RETRY_USECS * rte_get_timer_hz() / 1E6;
1294 unsigned int expired = 0;
1295
1296 if (!start) {
1297 start = rte_get_timer_cycles();
1298 }
1299
1300 /*
1301 * Unable to enqueue packets to vhost interface.
1302 * Check available entries before retrying.
1303 */
1304 while (!rte_vring_available_entries(virtio_dev, vhost_qid)) {
1305 if (OVS_UNLIKELY((rte_get_timer_cycles() - start) > timeout)) {
1306 expired = 1;
1307 break;
1308 }
1309 }
1310 if (expired) {
1311 /* break out of main loop. */
1312 break;
1313 }
1314 }
1315 } while (cnt);
1316
1317 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1318
1319 rte_spinlock_lock(&dev->stats_lock);
1320 cnt += qos_pkts;
1321 netdev_dpdk_vhost_update_tx_counters(&dev->stats, pkts, total_pkts, cnt);
1322 rte_spinlock_unlock(&dev->stats_lock);
1323
1324 out:
1325 if (may_steal) {
1326 int i;
1327
1328 for (i = 0; i < total_pkts; i++) {
1329 dp_packet_delete(pkts[i]);
1330 }
1331 }
1332 }
1333
1334 inline static void
1335 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
1336 struct rte_mbuf **pkts, int cnt)
1337 {
1338 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
1339 uint64_t diff_tsc;
1340
1341 int i = 0;
1342
1343 while (i < cnt) {
1344 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
1345 int tocopy = MIN(freeslots, cnt-i);
1346
1347 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
1348 tocopy * sizeof (struct rte_mbuf *));
1349
1350 txq->count += tocopy;
1351 i += tocopy;
1352
1353 if (txq->count == MAX_TX_QUEUE_LEN || txq->flush_tx) {
1354 dpdk_queue_flush__(dev, qid);
1355 }
1356 diff_tsc = rte_get_timer_cycles() - txq->tsc;
1357 if (diff_tsc >= DRAIN_TSC) {
1358 dpdk_queue_flush__(dev, qid);
1359 }
1360 }
1361 }
1362
1363 /* Tx function. Transmit packets indefinitely */
1364 static void
1365 dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet **pkts,
1366 int cnt)
1367 OVS_NO_THREAD_SAFETY_ANALYSIS
1368 {
1369 #if !defined(__CHECKER__) && !defined(_WIN32)
1370 const size_t PKT_ARRAY_SIZE = cnt;
1371 #else
1372 /* Sparse or MSVC doesn't like variable length array. */
1373 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
1374 #endif
1375 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1376 struct rte_mbuf *mbufs[PKT_ARRAY_SIZE];
1377 int dropped = 0;
1378 int newcnt = 0;
1379 int i;
1380
1381 /* If we are on a non pmd thread we have to use the mempool mutex, because
1382 * every non pmd thread shares the same mempool cache */
1383
1384 if (!dpdk_thread_is_pmd()) {
1385 ovs_mutex_lock(&nonpmd_mempool_mutex);
1386 }
1387
1388 for (i = 0; i < cnt; i++) {
1389 int size = dp_packet_size(pkts[i]);
1390
1391 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1392 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1393 (int)size , dev->max_packet_len);
1394
1395 dropped++;
1396 continue;
1397 }
1398
1399 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
1400
1401 if (!mbufs[newcnt]) {
1402 dropped += cnt - i;
1403 break;
1404 }
1405
1406 /* We have to do a copy for now */
1407 memcpy(rte_pktmbuf_mtod(mbufs[newcnt], void *), dp_packet_data(pkts[i]), size);
1408
1409 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
1410 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
1411
1412 newcnt++;
1413 }
1414
1415 if (dev->type == DPDK_DEV_VHOST) {
1416 __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) mbufs, newcnt, true);
1417 } else {
1418 unsigned int qos_pkts = newcnt;
1419
1420 /* Check if QoS has been configured for this netdev. */
1421 newcnt = netdev_dpdk_qos_run__(dev, mbufs, newcnt);
1422
1423 dropped += qos_pkts - newcnt;
1424 dpdk_queue_pkts(dev, qid, mbufs, newcnt);
1425 dpdk_queue_flush(dev, qid);
1426 }
1427
1428 if (OVS_UNLIKELY(dropped)) {
1429 rte_spinlock_lock(&dev->stats_lock);
1430 dev->stats.tx_dropped += dropped;
1431 rte_spinlock_unlock(&dev->stats_lock);
1432 }
1433
1434 if (!dpdk_thread_is_pmd()) {
1435 ovs_mutex_unlock(&nonpmd_mempool_mutex);
1436 }
1437 }
1438
1439 static int
1440 netdev_dpdk_vhost_send(struct netdev *netdev, int qid, struct dp_packet **pkts,
1441 int cnt, bool may_steal)
1442 {
1443 if (OVS_UNLIKELY(pkts[0]->source != DPBUF_DPDK)) {
1444 int i;
1445
1446 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1447 if (may_steal) {
1448 for (i = 0; i < cnt; i++) {
1449 dp_packet_delete(pkts[i]);
1450 }
1451 }
1452 } else {
1453 __netdev_dpdk_vhost_send(netdev, qid, pkts, cnt, may_steal);
1454 }
1455 return 0;
1456 }
1457
1458 static inline void
1459 netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
1460 struct dp_packet **pkts, int cnt, bool may_steal)
1461 {
1462 int i;
1463
1464 if (OVS_UNLIKELY(dev->txq_needs_locking)) {
1465 qid = qid % dev->real_n_txq;
1466 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1467 }
1468
1469 if (OVS_UNLIKELY(!may_steal ||
1470 pkts[0]->source != DPBUF_DPDK)) {
1471 struct netdev *netdev = &dev->up;
1472
1473 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1474
1475 if (may_steal) {
1476 for (i = 0; i < cnt; i++) {
1477 dp_packet_delete(pkts[i]);
1478 }
1479 }
1480 } else {
1481 int next_tx_idx = 0;
1482 int dropped = 0;
1483 unsigned int qos_pkts = 0;
1484 unsigned int temp_cnt = 0;
1485
1486 for (i = 0; i < cnt; i++) {
1487 int size = dp_packet_size(pkts[i]);
1488
1489 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1490 if (next_tx_idx != i) {
1491 temp_cnt = i - next_tx_idx;
1492 qos_pkts = temp_cnt;
1493
1494 temp_cnt = netdev_dpdk_qos_run__(dev, (struct rte_mbuf**)pkts,
1495 temp_cnt);
1496 dropped += qos_pkts - temp_cnt;
1497 dpdk_queue_pkts(dev, qid,
1498 (struct rte_mbuf **)&pkts[next_tx_idx],
1499 temp_cnt);
1500
1501 }
1502
1503 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1504 (int)size , dev->max_packet_len);
1505
1506 dp_packet_delete(pkts[i]);
1507 dropped++;
1508 next_tx_idx = i + 1;
1509 }
1510 }
1511 if (next_tx_idx != cnt) {
1512 cnt -= next_tx_idx;
1513 qos_pkts = cnt;
1514
1515 cnt = netdev_dpdk_qos_run__(dev, (struct rte_mbuf**)pkts, cnt);
1516 dropped += qos_pkts - cnt;
1517 dpdk_queue_pkts(dev, qid, (struct rte_mbuf **)&pkts[next_tx_idx],
1518 cnt);
1519 }
1520
1521 if (OVS_UNLIKELY(dropped)) {
1522 rte_spinlock_lock(&dev->stats_lock);
1523 dev->stats.tx_dropped += dropped;
1524 rte_spinlock_unlock(&dev->stats_lock);
1525 }
1526 }
1527
1528 if (OVS_UNLIKELY(dev->txq_needs_locking)) {
1529 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1530 }
1531 }
1532
1533 static int
1534 netdev_dpdk_eth_send(struct netdev *netdev, int qid,
1535 struct dp_packet **pkts, int cnt, bool may_steal)
1536 {
1537 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1538
1539 netdev_dpdk_send__(dev, qid, pkts, cnt, may_steal);
1540 return 0;
1541 }
1542
1543 static int
1544 netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
1545 {
1546 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1547
1548 ovs_mutex_lock(&dev->mutex);
1549 if (!eth_addr_equals(dev->hwaddr, mac)) {
1550 dev->hwaddr = mac;
1551 netdev_change_seq_changed(netdev);
1552 }
1553 ovs_mutex_unlock(&dev->mutex);
1554
1555 return 0;
1556 }
1557
1558 static int
1559 netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
1560 {
1561 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1562
1563 ovs_mutex_lock(&dev->mutex);
1564 *mac = dev->hwaddr;
1565 ovs_mutex_unlock(&dev->mutex);
1566
1567 return 0;
1568 }
1569
1570 static int
1571 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
1572 {
1573 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1574
1575 ovs_mutex_lock(&dev->mutex);
1576 *mtup = dev->mtu;
1577 ovs_mutex_unlock(&dev->mutex);
1578
1579 return 0;
1580 }
1581
1582 static int
1583 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
1584 {
1585 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1586 int old_mtu, err, dpdk_mtu;
1587 struct dpdk_mp *old_mp;
1588 struct dpdk_mp *mp;
1589 uint32_t buf_size;
1590
1591 ovs_mutex_lock(&dpdk_mutex);
1592 ovs_mutex_lock(&dev->mutex);
1593 if (dev->mtu == mtu) {
1594 err = 0;
1595 goto out;
1596 }
1597
1598 buf_size = dpdk_buf_size(mtu);
1599 dpdk_mtu = FRAME_LEN_TO_MTU(buf_size);
1600
1601 mp = dpdk_mp_get(dev->socket_id, dpdk_mtu);
1602 if (!mp) {
1603 err = ENOMEM;
1604 goto out;
1605 }
1606
1607 rte_eth_dev_stop(dev->port_id);
1608
1609 old_mtu = dev->mtu;
1610 old_mp = dev->dpdk_mp;
1611 dev->dpdk_mp = mp;
1612 dev->mtu = mtu;
1613 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
1614
1615 err = dpdk_eth_dev_init(dev);
1616 if (err) {
1617 dpdk_mp_put(mp);
1618 dev->mtu = old_mtu;
1619 dev->dpdk_mp = old_mp;
1620 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
1621 dpdk_eth_dev_init(dev);
1622 goto out;
1623 }
1624
1625 dpdk_mp_put(old_mp);
1626 netdev_change_seq_changed(netdev);
1627 out:
1628 ovs_mutex_unlock(&dev->mutex);
1629 ovs_mutex_unlock(&dpdk_mutex);
1630 return err;
1631 }
1632
1633 static int
1634 netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier);
1635
1636 static int
1637 netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
1638 struct netdev_stats *stats)
1639 {
1640 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1641
1642 ovs_mutex_lock(&dev->mutex);
1643 memset(stats, 0, sizeof(*stats));
1644 /* Unsupported Stats */
1645 stats->collisions = UINT64_MAX;
1646 stats->rx_crc_errors = UINT64_MAX;
1647 stats->rx_fifo_errors = UINT64_MAX;
1648 stats->rx_frame_errors = UINT64_MAX;
1649 stats->rx_missed_errors = UINT64_MAX;
1650 stats->rx_over_errors = UINT64_MAX;
1651 stats->tx_aborted_errors = UINT64_MAX;
1652 stats->tx_carrier_errors = UINT64_MAX;
1653 stats->tx_errors = UINT64_MAX;
1654 stats->tx_fifo_errors = UINT64_MAX;
1655 stats->tx_heartbeat_errors = UINT64_MAX;
1656 stats->tx_window_errors = UINT64_MAX;
1657 stats->rx_dropped += UINT64_MAX;
1658
1659 rte_spinlock_lock(&dev->stats_lock);
1660 /* Supported Stats */
1661 stats->rx_packets += dev->stats.rx_packets;
1662 stats->tx_packets += dev->stats.tx_packets;
1663 stats->tx_dropped += dev->stats.tx_dropped;
1664 stats->multicast = dev->stats.multicast;
1665 stats->rx_bytes = dev->stats.rx_bytes;
1666 stats->tx_bytes = dev->stats.tx_bytes;
1667 stats->rx_errors = dev->stats.rx_errors;
1668 stats->rx_length_errors = dev->stats.rx_length_errors;
1669 rte_spinlock_unlock(&dev->stats_lock);
1670
1671 ovs_mutex_unlock(&dev->mutex);
1672
1673 return 0;
1674 }
1675
1676 static int
1677 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1678 {
1679 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1680 struct rte_eth_stats rte_stats;
1681 bool gg;
1682
1683 netdev_dpdk_get_carrier(netdev, &gg);
1684 ovs_mutex_lock(&dev->mutex);
1685 rte_eth_stats_get(dev->port_id, &rte_stats);
1686
1687 memset(stats, 0, sizeof(*stats));
1688
1689 stats->rx_packets = rte_stats.ipackets;
1690 stats->tx_packets = rte_stats.opackets;
1691 stats->rx_bytes = rte_stats.ibytes;
1692 stats->tx_bytes = rte_stats.obytes;
1693 /* DPDK counts imissed as errors, but count them here as dropped instead */
1694 stats->rx_errors = rte_stats.ierrors - rte_stats.imissed;
1695 stats->tx_errors = rte_stats.oerrors;
1696 stats->multicast = rte_stats.imcasts;
1697
1698 rte_spinlock_lock(&dev->stats_lock);
1699 stats->tx_dropped = dev->stats.tx_dropped;
1700 rte_spinlock_unlock(&dev->stats_lock);
1701
1702 /* These are the available DPDK counters for packets not received due to
1703 * local resource constraints in DPDK and NIC respectively. */
1704 stats->rx_dropped = rte_stats.rx_nombuf + rte_stats.imissed;
1705 stats->collisions = UINT64_MAX;
1706
1707 stats->rx_length_errors = UINT64_MAX;
1708 stats->rx_over_errors = UINT64_MAX;
1709 stats->rx_crc_errors = UINT64_MAX;
1710 stats->rx_frame_errors = UINT64_MAX;
1711 stats->rx_fifo_errors = UINT64_MAX;
1712 stats->rx_missed_errors = rte_stats.imissed;
1713
1714 stats->tx_aborted_errors = UINT64_MAX;
1715 stats->tx_carrier_errors = UINT64_MAX;
1716 stats->tx_fifo_errors = UINT64_MAX;
1717 stats->tx_heartbeat_errors = UINT64_MAX;
1718 stats->tx_window_errors = UINT64_MAX;
1719
1720 ovs_mutex_unlock(&dev->mutex);
1721
1722 return 0;
1723 }
1724
1725 static int
1726 netdev_dpdk_get_features(const struct netdev *netdev,
1727 enum netdev_features *current,
1728 enum netdev_features *advertised OVS_UNUSED,
1729 enum netdev_features *supported OVS_UNUSED,
1730 enum netdev_features *peer OVS_UNUSED)
1731 {
1732 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1733 struct rte_eth_link link;
1734
1735 ovs_mutex_lock(&dev->mutex);
1736 link = dev->link;
1737 ovs_mutex_unlock(&dev->mutex);
1738
1739 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1740 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1741 *current = NETDEV_F_AUTONEG;
1742 }
1743 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1744 if (link.link_speed == ETH_LINK_SPEED_10) {
1745 *current = NETDEV_F_10MB_HD;
1746 }
1747 if (link.link_speed == ETH_LINK_SPEED_100) {
1748 *current = NETDEV_F_100MB_HD;
1749 }
1750 if (link.link_speed == ETH_LINK_SPEED_1000) {
1751 *current = NETDEV_F_1GB_HD;
1752 }
1753 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1754 if (link.link_speed == ETH_LINK_SPEED_10) {
1755 *current = NETDEV_F_10MB_FD;
1756 }
1757 if (link.link_speed == ETH_LINK_SPEED_100) {
1758 *current = NETDEV_F_100MB_FD;
1759 }
1760 if (link.link_speed == ETH_LINK_SPEED_1000) {
1761 *current = NETDEV_F_1GB_FD;
1762 }
1763 if (link.link_speed == ETH_LINK_SPEED_10000) {
1764 *current = NETDEV_F_10GB_FD;
1765 }
1766 }
1767
1768 return 0;
1769 }
1770
1771 static int
1772 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1773 {
1774 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1775 int ifindex;
1776
1777 ovs_mutex_lock(&dev->mutex);
1778 ifindex = dev->port_id;
1779 ovs_mutex_unlock(&dev->mutex);
1780
1781 return ifindex;
1782 }
1783
1784 static int
1785 netdev_dpdk_get_carrier(const struct netdev *netdev, bool *carrier)
1786 {
1787 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1788
1789 ovs_mutex_lock(&dev->mutex);
1790 check_link_status(dev);
1791 *carrier = dev->link.link_status;
1792
1793 ovs_mutex_unlock(&dev->mutex);
1794
1795 return 0;
1796 }
1797
1798 static int
1799 netdev_dpdk_vhost_get_carrier(const struct netdev *netdev, bool *carrier)
1800 {
1801 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1802 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(dev);
1803
1804 ovs_mutex_lock(&dev->mutex);
1805
1806 if (is_vhost_running(virtio_dev)) {
1807 *carrier = 1;
1808 } else {
1809 *carrier = 0;
1810 }
1811
1812 ovs_mutex_unlock(&dev->mutex);
1813
1814 return 0;
1815 }
1816
1817 static long long int
1818 netdev_dpdk_get_carrier_resets(const struct netdev *netdev)
1819 {
1820 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1821 long long int carrier_resets;
1822
1823 ovs_mutex_lock(&dev->mutex);
1824 carrier_resets = dev->link_reset_cnt;
1825 ovs_mutex_unlock(&dev->mutex);
1826
1827 return carrier_resets;
1828 }
1829
1830 static int
1831 netdev_dpdk_set_miimon(struct netdev *netdev OVS_UNUSED,
1832 long long int interval OVS_UNUSED)
1833 {
1834 return EOPNOTSUPP;
1835 }
1836
1837 static int
1838 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1839 enum netdev_flags off, enum netdev_flags on,
1840 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1841 {
1842 int err;
1843
1844 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1845 return EINVAL;
1846 }
1847
1848 *old_flagsp = dev->flags;
1849 dev->flags |= on;
1850 dev->flags &= ~off;
1851
1852 if (dev->flags == *old_flagsp) {
1853 return 0;
1854 }
1855
1856 if (dev->type == DPDK_DEV_ETH) {
1857 if (dev->flags & NETDEV_UP) {
1858 err = rte_eth_dev_start(dev->port_id);
1859 if (err)
1860 return -err;
1861 }
1862
1863 if (dev->flags & NETDEV_PROMISC) {
1864 rte_eth_promiscuous_enable(dev->port_id);
1865 }
1866
1867 if (!(dev->flags & NETDEV_UP)) {
1868 rte_eth_dev_stop(dev->port_id);
1869 }
1870 }
1871
1872 return 0;
1873 }
1874
1875 static int
1876 netdev_dpdk_update_flags(struct netdev *netdev,
1877 enum netdev_flags off, enum netdev_flags on,
1878 enum netdev_flags *old_flagsp)
1879 {
1880 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1881 int error;
1882
1883 ovs_mutex_lock(&dev->mutex);
1884 error = netdev_dpdk_update_flags__(dev, off, on, old_flagsp);
1885 ovs_mutex_unlock(&dev->mutex);
1886
1887 return error;
1888 }
1889
1890 static int
1891 netdev_dpdk_get_status(const struct netdev *netdev, struct smap *args)
1892 {
1893 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1894 struct rte_eth_dev_info dev_info;
1895
1896 if (dev->port_id < 0)
1897 return ENODEV;
1898
1899 ovs_mutex_lock(&dev->mutex);
1900 rte_eth_dev_info_get(dev->port_id, &dev_info);
1901 ovs_mutex_unlock(&dev->mutex);
1902
1903 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1904
1905 smap_add_format(args, "port_no", "%d", dev->port_id);
1906 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1907 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1908 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1909 smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len);
1910 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1911 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1912 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1913 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1914 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1915 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1916
1917 if (dev_info.pci_dev) {
1918 smap_add_format(args, "pci-vendor_id", "0x%u",
1919 dev_info.pci_dev->id.vendor_id);
1920 smap_add_format(args, "pci-device_id", "0x%x",
1921 dev_info.pci_dev->id.device_id);
1922 }
1923
1924 return 0;
1925 }
1926
1927 static void
1928 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1929 OVS_REQUIRES(dev->mutex)
1930 {
1931 enum netdev_flags old_flags;
1932
1933 if (admin_state) {
1934 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1935 } else {
1936 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1937 }
1938 }
1939
1940 static void
1941 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1942 const char *argv[], void *aux OVS_UNUSED)
1943 {
1944 bool up;
1945
1946 if (!strcasecmp(argv[argc - 1], "up")) {
1947 up = true;
1948 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1949 up = false;
1950 } else {
1951 unixctl_command_reply_error(conn, "Invalid Admin State");
1952 return;
1953 }
1954
1955 if (argc > 2) {
1956 struct netdev *netdev = netdev_from_name(argv[1]);
1957 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1958 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1959
1960 ovs_mutex_lock(&dpdk_dev->mutex);
1961 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1962 ovs_mutex_unlock(&dpdk_dev->mutex);
1963
1964 netdev_close(netdev);
1965 } else {
1966 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1967 netdev_close(netdev);
1968 return;
1969 }
1970 } else {
1971 struct netdev_dpdk *netdev;
1972
1973 ovs_mutex_lock(&dpdk_mutex);
1974 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1975 ovs_mutex_lock(&netdev->mutex);
1976 netdev_dpdk_set_admin_state__(netdev, up);
1977 ovs_mutex_unlock(&netdev->mutex);
1978 }
1979 ovs_mutex_unlock(&dpdk_mutex);
1980 }
1981 unixctl_command_reply(conn, "OK");
1982 }
1983
1984 /*
1985 * Set virtqueue flags so that we do not receive interrupts.
1986 */
1987 static void
1988 set_irq_status(struct virtio_net *virtio_dev)
1989 {
1990 uint32_t i;
1991 uint64_t idx;
1992
1993 for (i = 0; i < virtio_dev->virt_qp_nb; i++) {
1994 idx = i * VIRTIO_QNUM;
1995 rte_vhost_enable_guest_notification(virtio_dev, idx + VIRTIO_RXQ, 0);
1996 rte_vhost_enable_guest_notification(virtio_dev, idx + VIRTIO_TXQ, 0);
1997 }
1998 }
1999
2000 /*
2001 * Fixes mapping for vhost-user tx queues. Must be called after each
2002 * enabling/disabling of queues and real_n_txq modifications.
2003 */
2004 static void
2005 netdev_dpdk_remap_txqs(struct netdev_dpdk *dev)
2006 OVS_REQUIRES(dev->mutex)
2007 {
2008 int *enabled_queues, n_enabled = 0;
2009 int i, k, total_txqs = dev->real_n_txq;
2010
2011 enabled_queues = dpdk_rte_mzalloc(total_txqs * sizeof *enabled_queues);
2012
2013 for (i = 0; i < total_txqs; i++) {
2014 /* Enabled queues always mapped to themselves. */
2015 if (dev->tx_q[i].map == i) {
2016 enabled_queues[n_enabled++] = i;
2017 }
2018 }
2019
2020 if (n_enabled == 0 && total_txqs != 0) {
2021 enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED;
2022 n_enabled = 1;
2023 }
2024
2025 k = 0;
2026 for (i = 0; i < total_txqs; i++) {
2027 if (dev->tx_q[i].map != i) {
2028 dev->tx_q[i].map = enabled_queues[k];
2029 k = (k + 1) % n_enabled;
2030 }
2031 }
2032
2033 VLOG_DBG("TX queue mapping for %s\n", dev->vhost_id);
2034 for (i = 0; i < total_txqs; i++) {
2035 VLOG_DBG("%2d --> %2d", i, dev->tx_q[i].map);
2036 }
2037
2038 rte_free(enabled_queues);
2039 }
2040
2041 static int
2042 netdev_dpdk_vhost_set_queues(struct netdev_dpdk *dev, struct virtio_net *virtio_dev)
2043 OVS_REQUIRES(dev->mutex)
2044 {
2045 uint32_t qp_num;
2046
2047 qp_num = virtio_dev->virt_qp_nb;
2048 if (qp_num > dev->up.n_rxq) {
2049 VLOG_ERR("vHost Device '%s' %"PRIu64" can't be added - "
2050 "too many queues %d > %d", virtio_dev->ifname, virtio_dev->device_fh,
2051 qp_num, dev->up.n_rxq);
2052 return -1;
2053 }
2054
2055 dev->real_n_rxq = qp_num;
2056 dev->real_n_txq = qp_num;
2057 dev->txq_needs_locking = true;
2058 /* Enable TX queue 0 by default if it wasn't disabled. */
2059 if (dev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) {
2060 dev->tx_q[0].map = 0;
2061 }
2062
2063 netdev_dpdk_remap_txqs(dev);
2064
2065 return 0;
2066 }
2067
2068 /*
2069 * A new virtio-net device is added to a vhost port.
2070 */
2071 static int
2072 new_device(struct virtio_net *virtio_dev)
2073 {
2074 struct netdev_dpdk *dev;
2075 bool exists = false;
2076
2077 ovs_mutex_lock(&dpdk_mutex);
2078 /* Add device to the vhost port with the same name as that passed down. */
2079 LIST_FOR_EACH(dev, list_node, &dpdk_list) {
2080 if (strncmp(virtio_dev->ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
2081 ovs_mutex_lock(&dev->mutex);
2082 if (netdev_dpdk_vhost_set_queues(dev, virtio_dev)) {
2083 ovs_mutex_unlock(&dev->mutex);
2084 ovs_mutex_unlock(&dpdk_mutex);
2085 return -1;
2086 }
2087 ovsrcu_set(&dev->virtio_dev, virtio_dev);
2088 exists = true;
2089 virtio_dev->flags |= VIRTIO_DEV_RUNNING;
2090 /* Disable notifications. */
2091 set_irq_status(virtio_dev);
2092 ovs_mutex_unlock(&dev->mutex);
2093 break;
2094 }
2095 }
2096 ovs_mutex_unlock(&dpdk_mutex);
2097
2098 if (!exists) {
2099 VLOG_INFO("vHost Device '%s' %"PRIu64" can't be added - name not "
2100 "found", virtio_dev->ifname, virtio_dev->device_fh);
2101
2102 return -1;
2103 }
2104
2105 VLOG_INFO("vHost Device '%s' %"PRIu64" has been added", virtio_dev->ifname,
2106 virtio_dev->device_fh);
2107 return 0;
2108 }
2109
2110 /* Clears mapping for all available queues of vhost interface. */
2111 static void
2112 netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev)
2113 OVS_REQUIRES(dev->mutex)
2114 {
2115 int i;
2116
2117 for (i = 0; i < dev->real_n_txq; i++) {
2118 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
2119 }
2120 }
2121
2122 /*
2123 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2124 * flag to stop any more packets from being sent or received to/from a VM and
2125 * ensure all currently queued packets have been sent/received before removing
2126 * the device.
2127 */
2128 static void
2129 destroy_device(volatile struct virtio_net *virtio_dev)
2130 {
2131 struct netdev_dpdk *dev;
2132 bool exists = false;
2133
2134 ovs_mutex_lock(&dpdk_mutex);
2135 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2136 if (netdev_dpdk_get_virtio(dev) == virtio_dev) {
2137
2138 ovs_mutex_lock(&dev->mutex);
2139 virtio_dev->flags &= ~VIRTIO_DEV_RUNNING;
2140 ovsrcu_set(&dev->virtio_dev, NULL);
2141 netdev_dpdk_txq_map_clear(dev);
2142 exists = true;
2143 ovs_mutex_unlock(&dev->mutex);
2144 break;
2145 }
2146 }
2147
2148 ovs_mutex_unlock(&dpdk_mutex);
2149
2150 if (exists == true) {
2151 /*
2152 * Wait for other threads to quiesce after setting the 'virtio_dev'
2153 * to NULL, before returning.
2154 */
2155 ovsrcu_synchronize();
2156 /*
2157 * As call to ovsrcu_synchronize() will end the quiescent state,
2158 * put thread back into quiescent state before returning.
2159 */
2160 ovsrcu_quiesce_start();
2161 VLOG_INFO("vHost Device '%s' %"PRIu64" has been removed",
2162 virtio_dev->ifname, virtio_dev->device_fh);
2163 } else {
2164 VLOG_INFO("vHost Device '%s' %"PRIu64" not found", virtio_dev->ifname,
2165 virtio_dev->device_fh);
2166 }
2167 }
2168
2169 static int
2170 vring_state_changed(struct virtio_net *virtio_dev, uint16_t queue_id,
2171 int enable)
2172 {
2173 struct netdev_dpdk *dev;
2174 bool exists = false;
2175 int qid = queue_id / VIRTIO_QNUM;
2176
2177 if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) {
2178 return 0;
2179 }
2180
2181 ovs_mutex_lock(&dpdk_mutex);
2182 LIST_FOR_EACH (dev, list_node, &dpdk_list) {
2183 if (strncmp(virtio_dev->ifname, dev->vhost_id, IF_NAME_SZ) == 0) {
2184 ovs_mutex_lock(&dev->mutex);
2185 if (enable) {
2186 dev->tx_q[qid].map = qid;
2187 } else {
2188 dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED;
2189 }
2190 netdev_dpdk_remap_txqs(dev);
2191 exists = true;
2192 ovs_mutex_unlock(&dev->mutex);
2193 break;
2194 }
2195 }
2196 ovs_mutex_unlock(&dpdk_mutex);
2197
2198 if (exists) {
2199 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s' %"
2200 PRIu64" changed to \'%s\'", queue_id, qid,
2201 virtio_dev->ifname, virtio_dev->device_fh,
2202 (enable == 1) ? "enabled" : "disabled");
2203 } else {
2204 VLOG_INFO("vHost Device '%s' %"PRIu64" not found", virtio_dev->ifname,
2205 virtio_dev->device_fh);
2206 return -1;
2207 }
2208
2209 return 0;
2210 }
2211
2212 struct virtio_net *
2213 netdev_dpdk_get_virtio(const struct netdev_dpdk *dev)
2214 {
2215 return ovsrcu_get(struct virtio_net *, &dev->virtio_dev);
2216 }
2217
2218 /*
2219 * These callbacks allow virtio-net devices to be added to vhost ports when
2220 * configuration has been fully complete.
2221 */
2222 static const struct virtio_net_device_ops virtio_net_device_ops =
2223 {
2224 .new_device = new_device,
2225 .destroy_device = destroy_device,
2226 .vring_state_changed = vring_state_changed
2227 };
2228
2229 static void *
2230 start_vhost_loop(void *dummy OVS_UNUSED)
2231 {
2232 pthread_detach(pthread_self());
2233 /* Put the cuse thread into quiescent state. */
2234 ovsrcu_quiesce_start();
2235 rte_vhost_driver_session_start();
2236 return NULL;
2237 }
2238
2239 static int
2240 dpdk_vhost_class_init(void)
2241 {
2242 rte_vhost_driver_callback_register(&virtio_net_device_ops);
2243 ovs_thread_create("vhost_thread", start_vhost_loop, NULL);
2244 return 0;
2245 }
2246
2247 static int
2248 dpdk_vhost_cuse_class_init(void)
2249 {
2250 int err = -1;
2251
2252
2253 /* Register CUSE device to handle IOCTLs.
2254 * Unless otherwise specified on the vswitchd command line, cuse_dev_name
2255 * is set to vhost-net.
2256 */
2257 err = rte_vhost_driver_register(cuse_dev_name);
2258
2259 if (err != 0) {
2260 VLOG_ERR("CUSE device setup failure.");
2261 return -1;
2262 }
2263
2264 dpdk_vhost_class_init();
2265 return 0;
2266 }
2267
2268 static int
2269 dpdk_vhost_user_class_init(void)
2270 {
2271 dpdk_vhost_class_init();
2272 return 0;
2273 }
2274
2275 static void
2276 dpdk_common_init(void)
2277 {
2278 unixctl_command_register("netdev-dpdk/set-admin-state",
2279 "[netdev] up|down", 1, 2,
2280 netdev_dpdk_set_admin_state, NULL);
2281
2282 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
2283 }
2284
2285 /* Client Rings */
2286
2287 static int
2288 dpdk_ring_create(const char dev_name[], unsigned int port_no,
2289 unsigned int *eth_port_id)
2290 {
2291 struct dpdk_ring *ivshmem;
2292 char ring_name[RTE_RING_NAMESIZE];
2293 int err;
2294
2295 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
2296 if (ivshmem == NULL) {
2297 return ENOMEM;
2298 }
2299
2300 /* XXX: Add support for multiquque ring. */
2301 err = snprintf(ring_name, sizeof(ring_name), "%s_tx", dev_name);
2302 if (err < 0) {
2303 return -err;
2304 }
2305
2306 /* Create single producer tx ring, netdev does explicit locking. */
2307 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2308 RING_F_SP_ENQ);
2309 if (ivshmem->cring_tx == NULL) {
2310 rte_free(ivshmem);
2311 return ENOMEM;
2312 }
2313
2314 err = snprintf(ring_name, sizeof(ring_name), "%s_rx", dev_name);
2315 if (err < 0) {
2316 return -err;
2317 }
2318
2319 /* Create single consumer rx ring, netdev does explicit locking. */
2320 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2321 RING_F_SC_DEQ);
2322 if (ivshmem->cring_rx == NULL) {
2323 rte_free(ivshmem);
2324 return ENOMEM;
2325 }
2326
2327 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
2328 &ivshmem->cring_tx, 1, SOCKET0);
2329
2330 if (err < 0) {
2331 rte_free(ivshmem);
2332 return ENODEV;
2333 }
2334
2335 ivshmem->user_port_id = port_no;
2336 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
2337 ovs_list_push_back(&dpdk_ring_list, &ivshmem->list_node);
2338
2339 *eth_port_id = ivshmem->eth_port_id;
2340 return 0;
2341 }
2342
2343 static int
2344 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
2345 {
2346 struct dpdk_ring *ivshmem;
2347 unsigned int port_no;
2348 int err = 0;
2349
2350 /* Names always start with "dpdkr" */
2351 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
2352 if (err) {
2353 return err;
2354 }
2355
2356 /* look through our list to find the device */
2357 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
2358 if (ivshmem->user_port_id == port_no) {
2359 VLOG_INFO("Found dpdk ring device %s:", dev_name);
2360 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
2361 return 0;
2362 }
2363 }
2364 /* Need to create the device rings */
2365 return dpdk_ring_create(dev_name, port_no, eth_port_id);
2366 }
2367
2368 static int
2369 netdev_dpdk_ring_send(struct netdev *netdev, int qid,
2370 struct dp_packet **pkts, int cnt, bool may_steal)
2371 {
2372 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2373 unsigned i;
2374
2375 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that the
2376 * rss hash field is clear. This is because the same mbuf may be modified by
2377 * the consumer of the ring and return into the datapath without recalculating
2378 * the RSS hash. */
2379 for (i = 0; i < cnt; i++) {
2380 dp_packet_rss_invalidate(pkts[i]);
2381 }
2382
2383 netdev_dpdk_send__(dev, qid, pkts, cnt, may_steal);
2384 return 0;
2385 }
2386
2387 static int
2388 netdev_dpdk_ring_construct(struct netdev *netdev)
2389 {
2390 unsigned int port_no = 0;
2391 int err = 0;
2392
2393 if (rte_eal_init_ret) {
2394 return rte_eal_init_ret;
2395 }
2396
2397 ovs_mutex_lock(&dpdk_mutex);
2398
2399 err = dpdk_ring_open(netdev->name, &port_no);
2400 if (err) {
2401 goto unlock_dpdk;
2402 }
2403
2404 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
2405
2406 unlock_dpdk:
2407 ovs_mutex_unlock(&dpdk_mutex);
2408 return err;
2409 }
2410
2411 /* QoS Functions */
2412
2413 /*
2414 * Initialize QoS configuration operations.
2415 */
2416 static void
2417 qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops)
2418 {
2419 conf->ops = ops;
2420 }
2421
2422 /*
2423 * Search existing QoS operations in qos_ops and compare each set of
2424 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2425 * else return NULL
2426 */
2427 static const struct dpdk_qos_ops *
2428 qos_lookup_name(const char *name)
2429 {
2430 const struct dpdk_qos_ops *const *opsp;
2431
2432 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2433 const struct dpdk_qos_ops *ops = *opsp;
2434 if (!strcmp(name, ops->qos_name)) {
2435 return ops;
2436 }
2437 }
2438 return NULL;
2439 }
2440
2441 /*
2442 * Call qos_destruct to clean up items associated with the netdevs
2443 * qos_conf. Set netdevs qos_conf to NULL.
2444 */
2445 static void
2446 qos_delete_conf(struct netdev *netdev)
2447 {
2448 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2449
2450 rte_spinlock_lock(&dev->qos_lock);
2451 if (dev->qos_conf) {
2452 if (dev->qos_conf->ops->qos_destruct) {
2453 dev->qos_conf->ops->qos_destruct(netdev, dev->qos_conf);
2454 }
2455 dev->qos_conf = NULL;
2456 }
2457 rte_spinlock_unlock(&dev->qos_lock);
2458 }
2459
2460 static int
2461 netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED,
2462 struct sset *types)
2463 {
2464 const struct dpdk_qos_ops *const *opsp;
2465
2466 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2467 const struct dpdk_qos_ops *ops = *opsp;
2468 if (ops->qos_construct && ops->qos_name[0] != '\0') {
2469 sset_add(types, ops->qos_name);
2470 }
2471 }
2472 return 0;
2473 }
2474
2475 static int
2476 netdev_dpdk_get_qos(const struct netdev *netdev,
2477 const char **typep, struct smap *details)
2478 {
2479 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2480 int error = 0;
2481
2482 ovs_mutex_lock(&dev->mutex);
2483 if(dev->qos_conf) {
2484 *typep = dev->qos_conf->ops->qos_name;
2485 error = (dev->qos_conf->ops->qos_get
2486 ? dev->qos_conf->ops->qos_get(netdev, details): 0);
2487 }
2488 ovs_mutex_unlock(&dev->mutex);
2489
2490 return error;
2491 }
2492
2493 static int
2494 netdev_dpdk_set_qos(struct netdev *netdev,
2495 const char *type, const struct smap *details)
2496 {
2497 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2498 const struct dpdk_qos_ops *new_ops = NULL;
2499 int error = 0;
2500
2501 /* If type is empty or unsupported then the current QoS configuration
2502 * for the dpdk-netdev can be destroyed */
2503 new_ops = qos_lookup_name(type);
2504
2505 if (type[0] == '\0' || !new_ops || !new_ops->qos_construct) {
2506 qos_delete_conf(netdev);
2507 return EOPNOTSUPP;
2508 }
2509
2510 ovs_mutex_lock(&dev->mutex);
2511
2512 if (dev->qos_conf) {
2513 if (new_ops == dev->qos_conf->ops) {
2514 error = new_ops->qos_set ? new_ops->qos_set(netdev, details) : 0;
2515 } else {
2516 /* Delete existing QoS configuration. */
2517 qos_delete_conf(netdev);
2518 ovs_assert(dev->qos_conf == NULL);
2519
2520 /* Install new QoS configuration. */
2521 error = new_ops->qos_construct(netdev, details);
2522 ovs_assert((error == 0) == (dev->qos_conf != NULL));
2523 }
2524 } else {
2525 error = new_ops->qos_construct(netdev, details);
2526 ovs_assert((error == 0) == (dev->qos_conf != NULL));
2527 }
2528
2529 ovs_mutex_unlock(&dev->mutex);
2530 return error;
2531 }
2532
2533 /* egress-policer details */
2534
2535 struct egress_policer {
2536 struct qos_conf qos_conf;
2537 struct rte_meter_srtcm_params app_srtcm_params;
2538 struct rte_meter_srtcm egress_meter;
2539 };
2540
2541 static struct egress_policer *
2542 egress_policer_get__(const struct netdev *netdev)
2543 {
2544 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2545 return CONTAINER_OF(dev->qos_conf, struct egress_policer, qos_conf);
2546 }
2547
2548 static int
2549 egress_policer_qos_construct(struct netdev *netdev,
2550 const struct smap *details)
2551 {
2552 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
2553 struct egress_policer *policer;
2554 const char *cir_s;
2555 const char *cbs_s;
2556 int err = 0;
2557
2558 rte_spinlock_lock(&dev->qos_lock);
2559 policer = xmalloc(sizeof *policer);
2560 qos_conf_init(&policer->qos_conf, &egress_policer_ops);
2561 dev->qos_conf = &policer->qos_conf;
2562 cir_s = smap_get(details, "cir");
2563 cbs_s = smap_get(details, "cbs");
2564 policer->app_srtcm_params.cir = cir_s ? strtoull(cir_s, NULL, 10) : 0;
2565 policer->app_srtcm_params.cbs = cbs_s ? strtoull(cbs_s, NULL, 10) : 0;
2566 policer->app_srtcm_params.ebs = 0;
2567 err = rte_meter_srtcm_config(&policer->egress_meter,
2568 &policer->app_srtcm_params);
2569 rte_spinlock_unlock(&dev->qos_lock);
2570
2571 return err;
2572 }
2573
2574 static void
2575 egress_policer_qos_destruct(struct netdev *netdev OVS_UNUSED,
2576 struct qos_conf *conf)
2577 {
2578 struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer,
2579 qos_conf);
2580 free(policer);
2581 }
2582
2583 static int
2584 egress_policer_qos_get(const struct netdev *netdev, struct smap *details)
2585 {
2586 struct egress_policer *policer = egress_policer_get__(netdev);
2587 smap_add_format(details, "cir", "%llu",
2588 1ULL * policer->app_srtcm_params.cir);
2589 smap_add_format(details, "cbs", "%llu",
2590 1ULL * policer->app_srtcm_params.cbs);
2591 return 0;
2592 }
2593
2594 static int
2595 egress_policer_qos_set(struct netdev *netdev, const struct smap *details)
2596 {
2597 struct egress_policer *policer;
2598 const char *cir_s;
2599 const char *cbs_s;
2600 int err = 0;
2601
2602 policer = egress_policer_get__(netdev);
2603 cir_s = smap_get(details, "cir");
2604 cbs_s = smap_get(details, "cbs");
2605 policer->app_srtcm_params.cir = cir_s ? strtoull(cir_s, NULL, 10) : 0;
2606 policer->app_srtcm_params.cbs = cbs_s ? strtoull(cbs_s, NULL, 10) : 0;
2607 policer->app_srtcm_params.ebs = 0;
2608 err = rte_meter_srtcm_config(&policer->egress_meter,
2609 &policer->app_srtcm_params);
2610
2611 return err;
2612 }
2613
2614 static inline bool
2615 egress_policer_pkt_handle__(struct rte_meter_srtcm *meter,
2616 struct rte_mbuf *pkt, uint64_t time)
2617 {
2618 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
2619
2620 return rte_meter_srtcm_color_blind_check(meter, time, pkt_len) ==
2621 e_RTE_METER_GREEN;
2622 }
2623
2624 static int
2625 egress_policer_run(struct netdev *netdev, struct rte_mbuf **pkts,
2626 int pkt_cnt)
2627 {
2628 int i = 0;
2629 int cnt = 0;
2630 struct egress_policer *policer = egress_policer_get__(netdev);
2631 struct rte_mbuf *pkt = NULL;
2632 uint64_t current_time = rte_rdtsc();
2633
2634 for(i = 0; i < pkt_cnt; i++) {
2635 pkt = pkts[i];
2636 /* Handle current packet */
2637 if (egress_policer_pkt_handle__(&policer->egress_meter, pkt,
2638 current_time)) {
2639 if (cnt != i) {
2640 pkts[cnt] = pkt;
2641 }
2642 cnt++;
2643 } else {
2644 rte_pktmbuf_free(pkt);
2645 }
2646 }
2647
2648 return cnt;
2649 }
2650
2651 static const struct dpdk_qos_ops egress_policer_ops = {
2652 "egress-policer", /* qos_name */
2653 egress_policer_qos_construct,
2654 egress_policer_qos_destruct,
2655 egress_policer_qos_get,
2656 egress_policer_qos_set,
2657 egress_policer_run
2658 };
2659
2660 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, MULTIQ, SEND, \
2661 GET_CARRIER, GET_STATS, GET_FEATURES, GET_STATUS, RXQ_RECV) \
2662 { \
2663 NAME, \
2664 true, /* is_pmd */ \
2665 INIT, /* init */ \
2666 NULL, /* netdev_dpdk_run */ \
2667 NULL, /* netdev_dpdk_wait */ \
2668 \
2669 netdev_dpdk_alloc, \
2670 CONSTRUCT, \
2671 DESTRUCT, \
2672 netdev_dpdk_dealloc, \
2673 netdev_dpdk_get_config, \
2674 netdev_dpdk_set_config, \
2675 NULL, /* get_tunnel_config */ \
2676 NULL, /* build header */ \
2677 NULL, /* push header */ \
2678 NULL, /* pop header */ \
2679 netdev_dpdk_get_numa_id, /* get_numa_id */ \
2680 MULTIQ, /* set_multiq */ \
2681 \
2682 SEND, /* send */ \
2683 NULL, /* send_wait */ \
2684 \
2685 netdev_dpdk_set_etheraddr, \
2686 netdev_dpdk_get_etheraddr, \
2687 netdev_dpdk_get_mtu, \
2688 netdev_dpdk_set_mtu, \
2689 netdev_dpdk_get_ifindex, \
2690 GET_CARRIER, \
2691 netdev_dpdk_get_carrier_resets, \
2692 netdev_dpdk_set_miimon, \
2693 GET_STATS, \
2694 GET_FEATURES, \
2695 NULL, /* set_advertisements */ \
2696 \
2697 NULL, /* set_policing */ \
2698 netdev_dpdk_get_qos_types, \
2699 NULL, /* get_qos_capabilities */ \
2700 netdev_dpdk_get_qos, \
2701 netdev_dpdk_set_qos, \
2702 NULL, /* get_queue */ \
2703 NULL, /* set_queue */ \
2704 NULL, /* delete_queue */ \
2705 NULL, /* get_queue_stats */ \
2706 NULL, /* queue_dump_start */ \
2707 NULL, /* queue_dump_next */ \
2708 NULL, /* queue_dump_done */ \
2709 NULL, /* dump_queue_stats */ \
2710 \
2711 NULL, /* set_in4 */ \
2712 NULL, /* get_addr_list */ \
2713 NULL, /* add_router */ \
2714 NULL, /* get_next_hop */ \
2715 GET_STATUS, \
2716 NULL, /* arp_lookup */ \
2717 \
2718 netdev_dpdk_update_flags, \
2719 \
2720 netdev_dpdk_rxq_alloc, \
2721 netdev_dpdk_rxq_construct, \
2722 netdev_dpdk_rxq_destruct, \
2723 netdev_dpdk_rxq_dealloc, \
2724 RXQ_RECV, \
2725 NULL, /* rx_wait */ \
2726 NULL, /* rxq_drain */ \
2727 }
2728
2729 static int
2730 process_vhost_flags(char *flag, char *default_val, int size,
2731 char **argv, char **new_val)
2732 {
2733 int changed = 0;
2734
2735 /* Depending on which version of vhost is in use, process the vhost-specific
2736 * flag if it is provided on the vswitchd command line, otherwise resort to
2737 * a default value.
2738 *
2739 * For vhost-user: Process "-vhost_sock_dir" to set the custom location of
2740 * the vhost-user socket(s).
2741 * For vhost-cuse: Process "-cuse_dev_name" to set the custom name of the
2742 * vhost-cuse character device.
2743 */
2744 if (!strcmp(argv[1], flag) && (strlen(argv[2]) <= size)) {
2745 changed = 1;
2746 *new_val = xstrdup(argv[2]);
2747 VLOG_INFO("User-provided %s in use: %s", flag, *new_val);
2748 } else {
2749 VLOG_INFO("No %s provided - defaulting to %s", flag, default_val);
2750 *new_val = default_val;
2751 }
2752
2753 return changed;
2754 }
2755
2756 int
2757 dpdk_init(int argc, char **argv)
2758 {
2759 int result;
2760 int base = 0;
2761 char *pragram_name = argv[0];
2762
2763 if (argc < 2 || strcmp(argv[1], "--dpdk"))
2764 return 0;
2765
2766 /* Remove the --dpdk argument from arg list.*/
2767 argc--;
2768 argv++;
2769
2770 /* Reject --user option */
2771 int i;
2772 for (i = 0; i < argc; i++) {
2773 if (!strcmp(argv[i], "--user")) {
2774 VLOG_ERR("Can not mix --dpdk and --user options, aborting.");
2775 }
2776 }
2777
2778 #ifdef VHOST_CUSE
2779 if (process_vhost_flags("-cuse_dev_name", xstrdup("vhost-net"),
2780 PATH_MAX, argv, &cuse_dev_name)) {
2781 #else
2782 if (process_vhost_flags("-vhost_sock_dir", xstrdup(ovs_rundir()),
2783 NAME_MAX, argv, &vhost_sock_dir)) {
2784 struct stat s;
2785 int err;
2786
2787 err = stat(vhost_sock_dir, &s);
2788 if (err) {
2789 VLOG_ERR("vHostUser socket DIR '%s' does not exist.",
2790 vhost_sock_dir);
2791 return err;
2792 }
2793 #endif
2794 /* Remove the vhost flag configuration parameters from the argument
2795 * list, so that the correct elements are passed to the DPDK
2796 * initialization function
2797 */
2798 argc -= 2;
2799 argv += 2; /* Increment by two to bypass the vhost flag arguments */
2800 base = 2;
2801 }
2802
2803 /* Keep the program name argument as this is needed for call to
2804 * rte_eal_init()
2805 */
2806 argv[0] = pragram_name;
2807
2808 /* Make sure things are initialized ... */
2809 result = rte_eal_init(argc, argv);
2810 if (result < 0) {
2811 ovs_abort(result, "Cannot init EAL");
2812 }
2813
2814 rte_memzone_dump(stdout);
2815 rte_eal_init_ret = 0;
2816
2817 if (argc > result) {
2818 argv[result] = argv[0];
2819 }
2820
2821 /* We are called from the main thread here */
2822 RTE_PER_LCORE(_lcore_id) = NON_PMD_CORE_ID;
2823
2824 return result + 1 + base;
2825 }
2826
2827 static const struct netdev_class dpdk_class =
2828 NETDEV_DPDK_CLASS(
2829 "dpdk",
2830 NULL,
2831 netdev_dpdk_construct,
2832 netdev_dpdk_destruct,
2833 netdev_dpdk_set_multiq,
2834 netdev_dpdk_eth_send,
2835 netdev_dpdk_get_carrier,
2836 netdev_dpdk_get_stats,
2837 netdev_dpdk_get_features,
2838 netdev_dpdk_get_status,
2839 netdev_dpdk_rxq_recv);
2840
2841 static const struct netdev_class dpdk_ring_class =
2842 NETDEV_DPDK_CLASS(
2843 "dpdkr",
2844 NULL,
2845 netdev_dpdk_ring_construct,
2846 netdev_dpdk_destruct,
2847 netdev_dpdk_set_multiq,
2848 netdev_dpdk_ring_send,
2849 netdev_dpdk_get_carrier,
2850 netdev_dpdk_get_stats,
2851 netdev_dpdk_get_features,
2852 netdev_dpdk_get_status,
2853 netdev_dpdk_rxq_recv);
2854
2855 static const struct netdev_class OVS_UNUSED dpdk_vhost_cuse_class =
2856 NETDEV_DPDK_CLASS(
2857 "dpdkvhostcuse",
2858 dpdk_vhost_cuse_class_init,
2859 netdev_dpdk_vhost_cuse_construct,
2860 netdev_dpdk_vhost_destruct,
2861 netdev_dpdk_vhost_cuse_set_multiq,
2862 netdev_dpdk_vhost_send,
2863 netdev_dpdk_vhost_get_carrier,
2864 netdev_dpdk_vhost_get_stats,
2865 NULL,
2866 NULL,
2867 netdev_dpdk_vhost_rxq_recv);
2868
2869 static const struct netdev_class OVS_UNUSED dpdk_vhost_user_class =
2870 NETDEV_DPDK_CLASS(
2871 "dpdkvhostuser",
2872 dpdk_vhost_user_class_init,
2873 netdev_dpdk_vhost_user_construct,
2874 netdev_dpdk_vhost_destruct,
2875 netdev_dpdk_vhost_set_multiq,
2876 netdev_dpdk_vhost_send,
2877 netdev_dpdk_vhost_get_carrier,
2878 netdev_dpdk_vhost_get_stats,
2879 NULL,
2880 NULL,
2881 netdev_dpdk_vhost_rxq_recv);
2882
2883 void
2884 netdev_dpdk_register(void)
2885 {
2886 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2887
2888 if (rte_eal_init_ret) {
2889 return;
2890 }
2891
2892 if (ovsthread_once_start(&once)) {
2893 dpdk_common_init();
2894 netdev_register_provider(&dpdk_class);
2895 netdev_register_provider(&dpdk_ring_class);
2896 #ifdef VHOST_CUSE
2897 netdev_register_provider(&dpdk_vhost_cuse_class);
2898 #else
2899 netdev_register_provider(&dpdk_vhost_user_class);
2900 #endif
2901 ovsthread_once_done(&once);
2902 }
2903 }
2904
2905 int
2906 pmd_thread_setaffinity_cpu(unsigned cpu)
2907 {
2908 cpu_set_t cpuset;
2909 int err;
2910
2911 CPU_ZERO(&cpuset);
2912 CPU_SET(cpu, &cpuset);
2913 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2914 if (err) {
2915 VLOG_ERR("Thread affinity error %d",err);
2916 return err;
2917 }
2918 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
2919 ovs_assert(cpu != NON_PMD_CORE_ID);
2920 RTE_PER_LCORE(_lcore_id) = cpu;
2921
2922 return 0;
2923 }
2924
2925 static bool
2926 dpdk_thread_is_pmd(void)
2927 {
2928 return rte_lcore_id() != NON_PMD_CORE_ID;
2929 }