]> git.proxmox.com Git - ovs.git/blob - lib/netdev-dpdk.c
list: Remove lib/list.h completely.
[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 "openvswitch/list.h"
38 #include "netdev-dpdk.h"
39 #include "netdev-provider.h"
40 #include "netdev-vport.h"
41 #include "odp-util.h"
42 #include "ofp-print.h"
43 #include "ovs-numa.h"
44 #include "ovs-thread.h"
45 #include "ovs-rcu.h"
46 #include "packets.h"
47 #include "shash.h"
48 #include "smap.h"
49 #include "sset.h"
50 #include "unaligned.h"
51 #include "timeval.h"
52 #include "unixctl.h"
53 #include "openvswitch/vlog.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 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 *netdev = dpdk_rte_mzalloc(sizeof *netdev);
653 return &netdev->up;
654 }
655
656 static void
657 netdev_dpdk_alloc_txq(struct netdev_dpdk *netdev, unsigned int n_txqs)
658 {
659 unsigned i;
660
661 netdev->tx_q = dpdk_rte_mzalloc(n_txqs * sizeof *netdev->tx_q);
662 for (i = 0; i < n_txqs; i++) {
663 int numa_id = ovs_numa_get_numa_id(i);
664
665 if (!netdev->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 'netdev', flags the
669 * 'flush_tx'. */
670 netdev->tx_q[i].flush_tx = netdev->socket_id == numa_id;
671 } else {
672 /* Queues are shared among CPUs. Always flush */
673 netdev->tx_q[i].flush_tx = true;
674 }
675
676 /* Initialize map for vhost devices. */
677 netdev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
678 rte_spinlock_init(&netdev->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 *netdev = netdev_dpdk_cast(netdev_);
688 int sid;
689 int err = 0;
690 uint32_t buf_size;
691
692 ovs_mutex_init(&netdev->mutex);
693 ovs_mutex_lock(&netdev->mutex);
694
695 rte_spinlock_init(&netdev->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 netdev->socket_id = sid < 0 ? SOCKET0 : sid;
707 netdev->port_id = port_no;
708 netdev->type = type;
709 netdev->flags = 0;
710 netdev->mtu = ETHER_MTU;
711 netdev->max_packet_len = MTU_TO_FRAME_LEN(netdev->mtu);
712
713 buf_size = dpdk_buf_size(netdev->mtu);
714 netdev->dpdk_mp = dpdk_mp_get(netdev->socket_id, FRAME_LEN_TO_MTU(buf_size));
715 if (!netdev->dpdk_mp) {
716 err = ENOMEM;
717 goto unlock;
718 }
719
720 /* Initialise QoS configuration to NULL and qos lock to unlocked */
721 netdev->qos_conf = NULL;
722 rte_spinlock_init(&netdev->qos_lock);
723
724 netdev_->n_txq = NR_QUEUE;
725 netdev_->n_rxq = NR_QUEUE;
726 netdev_->requested_n_rxq = NR_QUEUE;
727 netdev->real_n_txq = NR_QUEUE;
728
729 if (type == DPDK_DEV_ETH) {
730 netdev_dpdk_alloc_txq(netdev, NR_QUEUE);
731 err = dpdk_eth_dev_init(netdev);
732 if (err) {
733 goto unlock;
734 }
735 } else {
736 netdev_dpdk_alloc_txq(netdev, OVS_VHOST_MAX_QUEUE_NUM);
737 }
738
739 list_push_back(&dpdk_list, &netdev->list_node);
740
741 unlock:
742 if (err) {
743 rte_free(netdev->tx_q);
744 }
745 ovs_mutex_unlock(&netdev->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 *netdev = netdev_dpdk_cast(netdev_);
784 int err;
785
786 ovs_mutex_lock(&dpdk_mutex);
787 strncpy(netdev->vhost_id, netdev->up.name, sizeof(netdev->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 *netdev = 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(netdev->vhost_id, sizeof(netdev->vhost_id), "%s/%s",
815 vhost_sock_dir, name);
816
817 err = rte_vhost_driver_register(netdev->vhost_id);
818 if (err) {
819 VLOG_ERR("vhost-user socket device setup failure for socket %s\n",
820 netdev->vhost_id);
821 } else {
822 fatal_signal_add_file_to_unlink(netdev->vhost_id);
823 VLOG_INFO("Socket %s created for vhost-user port %s\n",
824 netdev->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 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 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 *netdev = netdev_dpdk_cast(netdev_);
901
902 rte_free(netdev);
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 *netdev = netdev_dpdk_cast(netdev_);
939
940 return netdev->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 *netdev = netdev_dpdk_cast(netdev_);
951 int err = 0;
952 int old_rxq, old_txq;
953
954 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
955 return err;
956 }
957
958 ovs_mutex_lock(&dpdk_mutex);
959 ovs_mutex_lock(&netdev->mutex);
960
961 rte_eth_dev_stop(netdev->port_id);
962
963 old_txq = netdev->up.n_txq;
964 old_rxq = netdev->up.n_rxq;
965 netdev->up.n_txq = n_txq;
966 netdev->up.n_rxq = n_rxq;
967
968 rte_free(netdev->tx_q);
969 err = dpdk_eth_dev_init(netdev);
970 netdev_dpdk_alloc_txq(netdev, netdev->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->up.n_txq = old_txq;
975 netdev->up.n_rxq = old_rxq;
976 }
977
978 netdev->txq_needs_locking = netdev->real_n_txq != netdev->up.n_txq;
979
980 ovs_mutex_unlock(&netdev->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 *netdev = netdev_dpdk_cast(netdev_);
991 int err = 0;
992
993 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
994 return err;
995 }
996
997 ovs_mutex_lock(&dpdk_mutex);
998 ovs_mutex_lock(&netdev->mutex);
999
1000 netdev->up.n_txq = n_txq;
1001 netdev->real_n_txq = 1;
1002 netdev->up.n_rxq = 1;
1003 netdev->txq_needs_locking = netdev->real_n_txq != netdev->up.n_txq;
1004
1005 ovs_mutex_unlock(&netdev->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 *netdev = netdev_dpdk_cast(netdev_);
1016 int err = 0;
1017
1018 if (netdev->up.n_txq == n_txq && netdev->up.n_rxq == n_rxq) {
1019 return err;
1020 }
1021
1022 ovs_mutex_lock(&dpdk_mutex);
1023 ovs_mutex_lock(&netdev->mutex);
1024
1025 netdev->up.n_txq = n_txq;
1026 netdev->up.n_rxq = n_rxq;
1027
1028 ovs_mutex_unlock(&netdev->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 *rx)
1044 {
1045 return CONTAINER_OF(rx, 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 *netdev = netdev_dpdk_cast(rx->up.netdev);
1053
1054 ovs_mutex_lock(&netdev->mutex);
1055 rx->port_id = netdev->port_id;
1056 ovs_mutex_unlock(&netdev->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 *dev)
1122 {
1123 return (dev != NULL && (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_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
1163 struct netdev *netdev = rx->up.netdev;
1164 struct netdev_dpdk *vhost_dev = netdev_dpdk_cast(netdev);
1165 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(vhost_dev);
1166 int qid = rxq_->queue_id;
1167 uint16_t nb_rx = 0;
1168
1169 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev))) {
1170 return EAGAIN;
1171 }
1172
1173 if (rxq_->queue_id >= vhost_dev->real_n_rxq) {
1174 return EOPNOTSUPP;
1175 }
1176
1177 nb_rx = rte_vhost_dequeue_burst(virtio_dev, qid * VIRTIO_QNUM + VIRTIO_TXQ,
1178 vhost_dev->dpdk_mp->mp,
1179 (struct rte_mbuf **)packets,
1180 NETDEV_MAX_BURST);
1181 if (!nb_rx) {
1182 return EAGAIN;
1183 }
1184
1185 rte_spinlock_lock(&vhost_dev->stats_lock);
1186 netdev_dpdk_vhost_update_rx_counters(&vhost_dev->stats, packets, nb_rx);
1187 rte_spinlock_unlock(&vhost_dev->stats_lock);
1188
1189 *c = (int) nb_rx;
1190 return 0;
1191 }
1192
1193 static int
1194 netdev_dpdk_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **packets,
1195 int *c)
1196 {
1197 struct netdev_rxq_dpdk *rx = netdev_rxq_dpdk_cast(rxq_);
1198 struct netdev *netdev = rx->up.netdev;
1199 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1200 int nb_rx;
1201
1202 /* There is only one tx queue for this core. Do not flush other
1203 * queues.
1204 * Do not flush tx queue which is shared among CPUs
1205 * since it is always flushed */
1206 if (rxq_->queue_id == rte_lcore_id() &&
1207 OVS_LIKELY(!dev->txq_needs_locking)) {
1208 dpdk_queue_flush(dev, rxq_->queue_id);
1209 }
1210
1211 nb_rx = rte_eth_rx_burst(rx->port_id, rxq_->queue_id,
1212 (struct rte_mbuf **) packets,
1213 NETDEV_MAX_BURST);
1214 if (!nb_rx) {
1215 return EAGAIN;
1216 }
1217
1218 *c = nb_rx;
1219
1220 return 0;
1221 }
1222
1223 static inline int
1224 netdev_dpdk_qos_run__(struct netdev_dpdk *dev, struct rte_mbuf **pkts,
1225 int cnt)
1226 {
1227 struct netdev *netdev = &dev->up;
1228
1229 if (dev->qos_conf != NULL) {
1230 rte_spinlock_lock(&dev->qos_lock);
1231 if (dev->qos_conf != NULL) {
1232 cnt = dev->qos_conf->ops->qos_run(netdev, pkts, cnt);
1233 }
1234 rte_spinlock_unlock(&dev->qos_lock);
1235 }
1236
1237 return cnt;
1238 }
1239
1240 static inline void
1241 netdev_dpdk_vhost_update_tx_counters(struct netdev_stats *stats,
1242 struct dp_packet **packets,
1243 int attempted,
1244 int dropped)
1245 {
1246 int i;
1247 int sent = attempted - dropped;
1248
1249 stats->tx_packets += sent;
1250 stats->tx_dropped += dropped;
1251
1252 for (i = 0; i < sent; i++) {
1253 stats->tx_bytes += dp_packet_size(packets[i]);
1254 }
1255 }
1256
1257 static void
1258 __netdev_dpdk_vhost_send(struct netdev *netdev, int qid,
1259 struct dp_packet **pkts, int cnt,
1260 bool may_steal)
1261 {
1262 struct netdev_dpdk *vhost_dev = netdev_dpdk_cast(netdev);
1263 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(vhost_dev);
1264 struct rte_mbuf **cur_pkts = (struct rte_mbuf **) pkts;
1265 unsigned int total_pkts = cnt;
1266 unsigned int qos_pkts = cnt;
1267 uint64_t start = 0;
1268
1269 qid = vhost_dev->tx_q[qid % vhost_dev->real_n_txq].map;
1270
1271 if (OVS_UNLIKELY(!is_vhost_running(virtio_dev) || qid < 0)) {
1272 rte_spinlock_lock(&vhost_dev->stats_lock);
1273 vhost_dev->stats.tx_dropped+= cnt;
1274 rte_spinlock_unlock(&vhost_dev->stats_lock);
1275 goto out;
1276 }
1277
1278 rte_spinlock_lock(&vhost_dev->tx_q[qid].tx_lock);
1279
1280 /* Check has QoS has been configured for the netdev */
1281 cnt = netdev_dpdk_qos_run__(vhost_dev, cur_pkts, cnt);
1282 qos_pkts -= cnt;
1283
1284 do {
1285 int vhost_qid = qid * VIRTIO_QNUM + VIRTIO_RXQ;
1286 unsigned int tx_pkts;
1287
1288 tx_pkts = rte_vhost_enqueue_burst(virtio_dev, vhost_qid,
1289 cur_pkts, cnt);
1290 if (OVS_LIKELY(tx_pkts)) {
1291 /* Packets have been sent.*/
1292 cnt -= tx_pkts;
1293 /* Prepare for possible next iteration.*/
1294 cur_pkts = &cur_pkts[tx_pkts];
1295 } else {
1296 uint64_t timeout = VHOST_ENQ_RETRY_USECS * rte_get_timer_hz() / 1E6;
1297 unsigned int expired = 0;
1298
1299 if (!start) {
1300 start = rte_get_timer_cycles();
1301 }
1302
1303 /*
1304 * Unable to enqueue packets to vhost interface.
1305 * Check available entries before retrying.
1306 */
1307 while (!rte_vring_available_entries(virtio_dev, vhost_qid)) {
1308 if (OVS_UNLIKELY((rte_get_timer_cycles() - start) > timeout)) {
1309 expired = 1;
1310 break;
1311 }
1312 }
1313 if (expired) {
1314 /* break out of main loop. */
1315 break;
1316 }
1317 }
1318 } while (cnt);
1319
1320 rte_spinlock_unlock(&vhost_dev->tx_q[qid].tx_lock);
1321
1322 rte_spinlock_lock(&vhost_dev->stats_lock);
1323 cnt += qos_pkts;
1324 netdev_dpdk_vhost_update_tx_counters(&vhost_dev->stats, pkts, total_pkts,
1325 cnt);
1326 rte_spinlock_unlock(&vhost_dev->stats_lock);
1327
1328 out:
1329 if (may_steal) {
1330 int i;
1331
1332 for (i = 0; i < total_pkts; i++) {
1333 dp_packet_delete(pkts[i]);
1334 }
1335 }
1336 }
1337
1338 inline static void
1339 dpdk_queue_pkts(struct netdev_dpdk *dev, int qid,
1340 struct rte_mbuf **pkts, int cnt)
1341 {
1342 struct dpdk_tx_queue *txq = &dev->tx_q[qid];
1343 uint64_t diff_tsc;
1344
1345 int i = 0;
1346
1347 while (i < cnt) {
1348 int freeslots = MAX_TX_QUEUE_LEN - txq->count;
1349 int tocopy = MIN(freeslots, cnt-i);
1350
1351 memcpy(&txq->burst_pkts[txq->count], &pkts[i],
1352 tocopy * sizeof (struct rte_mbuf *));
1353
1354 txq->count += tocopy;
1355 i += tocopy;
1356
1357 if (txq->count == MAX_TX_QUEUE_LEN || txq->flush_tx) {
1358 dpdk_queue_flush__(dev, qid);
1359 }
1360 diff_tsc = rte_get_timer_cycles() - txq->tsc;
1361 if (diff_tsc >= DRAIN_TSC) {
1362 dpdk_queue_flush__(dev, qid);
1363 }
1364 }
1365 }
1366
1367 /* Tx function. Transmit packets indefinitely */
1368 static void
1369 dpdk_do_tx_copy(struct netdev *netdev, int qid, struct dp_packet **pkts,
1370 int cnt)
1371 OVS_NO_THREAD_SAFETY_ANALYSIS
1372 {
1373 #if !defined(__CHECKER__) && !defined(_WIN32)
1374 const size_t PKT_ARRAY_SIZE = cnt;
1375 #else
1376 /* Sparse or MSVC doesn't like variable length array. */
1377 enum { PKT_ARRAY_SIZE = NETDEV_MAX_BURST };
1378 #endif
1379 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1380 struct rte_mbuf *mbufs[PKT_ARRAY_SIZE];
1381 int dropped = 0;
1382 int newcnt = 0;
1383 int i;
1384
1385 /* If we are on a non pmd thread we have to use the mempool mutex, because
1386 * every non pmd thread shares the same mempool cache */
1387
1388 if (!dpdk_thread_is_pmd()) {
1389 ovs_mutex_lock(&nonpmd_mempool_mutex);
1390 }
1391
1392 for (i = 0; i < cnt; i++) {
1393 int size = dp_packet_size(pkts[i]);
1394
1395 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1396 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1397 (int)size , dev->max_packet_len);
1398
1399 dropped++;
1400 continue;
1401 }
1402
1403 mbufs[newcnt] = rte_pktmbuf_alloc(dev->dpdk_mp->mp);
1404
1405 if (!mbufs[newcnt]) {
1406 dropped += cnt - i;
1407 break;
1408 }
1409
1410 /* We have to do a copy for now */
1411 memcpy(rte_pktmbuf_mtod(mbufs[newcnt], void *), dp_packet_data(pkts[i]), size);
1412
1413 rte_pktmbuf_data_len(mbufs[newcnt]) = size;
1414 rte_pktmbuf_pkt_len(mbufs[newcnt]) = size;
1415
1416 newcnt++;
1417 }
1418
1419 if (dev->type == DPDK_DEV_VHOST) {
1420 __netdev_dpdk_vhost_send(netdev, qid, (struct dp_packet **) mbufs, newcnt, true);
1421 } else {
1422 unsigned int qos_pkts = newcnt;
1423
1424 /* Check if QoS has been configured for this netdev. */
1425 newcnt = netdev_dpdk_qos_run__(dev, mbufs, newcnt);
1426
1427 dropped += qos_pkts - newcnt;
1428 dpdk_queue_pkts(dev, qid, mbufs, newcnt);
1429 dpdk_queue_flush(dev, qid);
1430 }
1431
1432 if (OVS_UNLIKELY(dropped)) {
1433 rte_spinlock_lock(&dev->stats_lock);
1434 dev->stats.tx_dropped += dropped;
1435 rte_spinlock_unlock(&dev->stats_lock);
1436 }
1437
1438 if (!dpdk_thread_is_pmd()) {
1439 ovs_mutex_unlock(&nonpmd_mempool_mutex);
1440 }
1441 }
1442
1443 static int
1444 netdev_dpdk_vhost_send(struct netdev *netdev, int qid, struct dp_packet **pkts,
1445 int cnt, bool may_steal)
1446 {
1447 if (OVS_UNLIKELY(pkts[0]->source != DPBUF_DPDK)) {
1448 int i;
1449
1450 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1451 if (may_steal) {
1452 for (i = 0; i < cnt; i++) {
1453 dp_packet_delete(pkts[i]);
1454 }
1455 }
1456 } else {
1457 __netdev_dpdk_vhost_send(netdev, qid, pkts, cnt, may_steal);
1458 }
1459 return 0;
1460 }
1461
1462 static inline void
1463 netdev_dpdk_send__(struct netdev_dpdk *dev, int qid,
1464 struct dp_packet **pkts, int cnt, bool may_steal)
1465 {
1466 int i;
1467
1468 if (OVS_UNLIKELY(dev->txq_needs_locking)) {
1469 qid = qid % dev->real_n_txq;
1470 rte_spinlock_lock(&dev->tx_q[qid].tx_lock);
1471 }
1472
1473 if (OVS_UNLIKELY(!may_steal ||
1474 pkts[0]->source != DPBUF_DPDK)) {
1475 struct netdev *netdev = &dev->up;
1476
1477 dpdk_do_tx_copy(netdev, qid, pkts, cnt);
1478
1479 if (may_steal) {
1480 for (i = 0; i < cnt; i++) {
1481 dp_packet_delete(pkts[i]);
1482 }
1483 }
1484 } else {
1485 int next_tx_idx = 0;
1486 int dropped = 0;
1487 unsigned int qos_pkts = 0;
1488 unsigned int temp_cnt = 0;
1489
1490 for (i = 0; i < cnt; i++) {
1491 int size = dp_packet_size(pkts[i]);
1492
1493 if (OVS_UNLIKELY(size > dev->max_packet_len)) {
1494 if (next_tx_idx != i) {
1495 temp_cnt = i - next_tx_idx;
1496 qos_pkts = temp_cnt;
1497
1498 temp_cnt = netdev_dpdk_qos_run__(dev, (struct rte_mbuf**)pkts,
1499 temp_cnt);
1500 dropped += qos_pkts - temp_cnt;
1501 dpdk_queue_pkts(dev, qid,
1502 (struct rte_mbuf **)&pkts[next_tx_idx],
1503 temp_cnt);
1504
1505 }
1506
1507 VLOG_WARN_RL(&rl, "Too big size %d max_packet_len %d",
1508 (int)size , dev->max_packet_len);
1509
1510 dp_packet_delete(pkts[i]);
1511 dropped++;
1512 next_tx_idx = i + 1;
1513 }
1514 }
1515 if (next_tx_idx != cnt) {
1516 cnt -= next_tx_idx;
1517 qos_pkts = cnt;
1518
1519 cnt = netdev_dpdk_qos_run__(dev, (struct rte_mbuf**)pkts, cnt);
1520 dropped += qos_pkts - cnt;
1521 dpdk_queue_pkts(dev, qid, (struct rte_mbuf **)&pkts[next_tx_idx],
1522 cnt);
1523 }
1524
1525 if (OVS_UNLIKELY(dropped)) {
1526 rte_spinlock_lock(&dev->stats_lock);
1527 dev->stats.tx_dropped += dropped;
1528 rte_spinlock_unlock(&dev->stats_lock);
1529 }
1530 }
1531
1532 if (OVS_UNLIKELY(dev->txq_needs_locking)) {
1533 rte_spinlock_unlock(&dev->tx_q[qid].tx_lock);
1534 }
1535 }
1536
1537 static int
1538 netdev_dpdk_eth_send(struct netdev *netdev, int qid,
1539 struct dp_packet **pkts, int cnt, bool may_steal)
1540 {
1541 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1542
1543 netdev_dpdk_send__(dev, qid, pkts, cnt, may_steal);
1544 return 0;
1545 }
1546
1547 static int
1548 netdev_dpdk_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
1549 {
1550 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1551
1552 ovs_mutex_lock(&dev->mutex);
1553 if (!eth_addr_equals(dev->hwaddr, mac)) {
1554 dev->hwaddr = mac;
1555 netdev_change_seq_changed(netdev);
1556 }
1557 ovs_mutex_unlock(&dev->mutex);
1558
1559 return 0;
1560 }
1561
1562 static int
1563 netdev_dpdk_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
1564 {
1565 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1566
1567 ovs_mutex_lock(&dev->mutex);
1568 *mac = dev->hwaddr;
1569 ovs_mutex_unlock(&dev->mutex);
1570
1571 return 0;
1572 }
1573
1574 static int
1575 netdev_dpdk_get_mtu(const struct netdev *netdev, int *mtup)
1576 {
1577 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1578
1579 ovs_mutex_lock(&dev->mutex);
1580 *mtup = dev->mtu;
1581 ovs_mutex_unlock(&dev->mutex);
1582
1583 return 0;
1584 }
1585
1586 static int
1587 netdev_dpdk_set_mtu(const struct netdev *netdev, int mtu)
1588 {
1589 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1590 int old_mtu, err, dpdk_mtu;
1591 struct dpdk_mp *old_mp;
1592 struct dpdk_mp *mp;
1593 uint32_t buf_size;
1594
1595 ovs_mutex_lock(&dpdk_mutex);
1596 ovs_mutex_lock(&dev->mutex);
1597 if (dev->mtu == mtu) {
1598 err = 0;
1599 goto out;
1600 }
1601
1602 buf_size = dpdk_buf_size(mtu);
1603 dpdk_mtu = FRAME_LEN_TO_MTU(buf_size);
1604
1605 mp = dpdk_mp_get(dev->socket_id, dpdk_mtu);
1606 if (!mp) {
1607 err = ENOMEM;
1608 goto out;
1609 }
1610
1611 rte_eth_dev_stop(dev->port_id);
1612
1613 old_mtu = dev->mtu;
1614 old_mp = dev->dpdk_mp;
1615 dev->dpdk_mp = mp;
1616 dev->mtu = mtu;
1617 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
1618
1619 err = dpdk_eth_dev_init(dev);
1620 if (err) {
1621 dpdk_mp_put(mp);
1622 dev->mtu = old_mtu;
1623 dev->dpdk_mp = old_mp;
1624 dev->max_packet_len = MTU_TO_FRAME_LEN(dev->mtu);
1625 dpdk_eth_dev_init(dev);
1626 goto out;
1627 }
1628
1629 dpdk_mp_put(old_mp);
1630 netdev_change_seq_changed(netdev);
1631 out:
1632 ovs_mutex_unlock(&dev->mutex);
1633 ovs_mutex_unlock(&dpdk_mutex);
1634 return err;
1635 }
1636
1637 static int
1638 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier);
1639
1640 static int
1641 netdev_dpdk_vhost_get_stats(const struct netdev *netdev,
1642 struct netdev_stats *stats)
1643 {
1644 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1645
1646 ovs_mutex_lock(&dev->mutex);
1647 memset(stats, 0, sizeof(*stats));
1648 /* Unsupported Stats */
1649 stats->collisions = UINT64_MAX;
1650 stats->rx_crc_errors = UINT64_MAX;
1651 stats->rx_fifo_errors = UINT64_MAX;
1652 stats->rx_frame_errors = UINT64_MAX;
1653 stats->rx_missed_errors = UINT64_MAX;
1654 stats->rx_over_errors = UINT64_MAX;
1655 stats->tx_aborted_errors = UINT64_MAX;
1656 stats->tx_carrier_errors = UINT64_MAX;
1657 stats->tx_errors = UINT64_MAX;
1658 stats->tx_fifo_errors = UINT64_MAX;
1659 stats->tx_heartbeat_errors = UINT64_MAX;
1660 stats->tx_window_errors = UINT64_MAX;
1661 stats->rx_dropped += UINT64_MAX;
1662
1663 rte_spinlock_lock(&dev->stats_lock);
1664 /* Supported Stats */
1665 stats->rx_packets += dev->stats.rx_packets;
1666 stats->tx_packets += dev->stats.tx_packets;
1667 stats->tx_dropped += dev->stats.tx_dropped;
1668 stats->multicast = dev->stats.multicast;
1669 stats->rx_bytes = dev->stats.rx_bytes;
1670 stats->tx_bytes = dev->stats.tx_bytes;
1671 stats->rx_errors = dev->stats.rx_errors;
1672 stats->rx_length_errors = dev->stats.rx_length_errors;
1673 rte_spinlock_unlock(&dev->stats_lock);
1674
1675 ovs_mutex_unlock(&dev->mutex);
1676
1677 return 0;
1678 }
1679
1680 static int
1681 netdev_dpdk_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1682 {
1683 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1684 struct rte_eth_stats rte_stats;
1685 bool gg;
1686
1687 netdev_dpdk_get_carrier(netdev, &gg);
1688 ovs_mutex_lock(&dev->mutex);
1689 rte_eth_stats_get(dev->port_id, &rte_stats);
1690
1691 memset(stats, 0, sizeof(*stats));
1692
1693 stats->rx_packets = rte_stats.ipackets;
1694 stats->tx_packets = rte_stats.opackets;
1695 stats->rx_bytes = rte_stats.ibytes;
1696 stats->tx_bytes = rte_stats.obytes;
1697 /* DPDK counts imissed as errors, but count them here as dropped instead */
1698 stats->rx_errors = rte_stats.ierrors - rte_stats.imissed;
1699 stats->tx_errors = rte_stats.oerrors;
1700 stats->multicast = rte_stats.imcasts;
1701
1702 rte_spinlock_lock(&dev->stats_lock);
1703 stats->tx_dropped = dev->stats.tx_dropped;
1704 rte_spinlock_unlock(&dev->stats_lock);
1705
1706 /* These are the available DPDK counters for packets not received due to
1707 * local resource constraints in DPDK and NIC respectively. */
1708 stats->rx_dropped = rte_stats.rx_nombuf + rte_stats.imissed;
1709 stats->collisions = UINT64_MAX;
1710
1711 stats->rx_length_errors = UINT64_MAX;
1712 stats->rx_over_errors = UINT64_MAX;
1713 stats->rx_crc_errors = UINT64_MAX;
1714 stats->rx_frame_errors = UINT64_MAX;
1715 stats->rx_fifo_errors = UINT64_MAX;
1716 stats->rx_missed_errors = rte_stats.imissed;
1717
1718 stats->tx_aborted_errors = UINT64_MAX;
1719 stats->tx_carrier_errors = UINT64_MAX;
1720 stats->tx_fifo_errors = UINT64_MAX;
1721 stats->tx_heartbeat_errors = UINT64_MAX;
1722 stats->tx_window_errors = UINT64_MAX;
1723
1724 ovs_mutex_unlock(&dev->mutex);
1725
1726 return 0;
1727 }
1728
1729 static int
1730 netdev_dpdk_get_features(const struct netdev *netdev_,
1731 enum netdev_features *current,
1732 enum netdev_features *advertised OVS_UNUSED,
1733 enum netdev_features *supported OVS_UNUSED,
1734 enum netdev_features *peer OVS_UNUSED)
1735 {
1736 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1737 struct rte_eth_link link;
1738
1739 ovs_mutex_lock(&dev->mutex);
1740 link = dev->link;
1741 ovs_mutex_unlock(&dev->mutex);
1742
1743 if (link.link_duplex == ETH_LINK_AUTONEG_DUPLEX) {
1744 if (link.link_speed == ETH_LINK_SPEED_AUTONEG) {
1745 *current = NETDEV_F_AUTONEG;
1746 }
1747 } else if (link.link_duplex == ETH_LINK_HALF_DUPLEX) {
1748 if (link.link_speed == ETH_LINK_SPEED_10) {
1749 *current = NETDEV_F_10MB_HD;
1750 }
1751 if (link.link_speed == ETH_LINK_SPEED_100) {
1752 *current = NETDEV_F_100MB_HD;
1753 }
1754 if (link.link_speed == ETH_LINK_SPEED_1000) {
1755 *current = NETDEV_F_1GB_HD;
1756 }
1757 } else if (link.link_duplex == ETH_LINK_FULL_DUPLEX) {
1758 if (link.link_speed == ETH_LINK_SPEED_10) {
1759 *current = NETDEV_F_10MB_FD;
1760 }
1761 if (link.link_speed == ETH_LINK_SPEED_100) {
1762 *current = NETDEV_F_100MB_FD;
1763 }
1764 if (link.link_speed == ETH_LINK_SPEED_1000) {
1765 *current = NETDEV_F_1GB_FD;
1766 }
1767 if (link.link_speed == ETH_LINK_SPEED_10000) {
1768 *current = NETDEV_F_10GB_FD;
1769 }
1770 }
1771
1772 return 0;
1773 }
1774
1775 static int
1776 netdev_dpdk_get_ifindex(const struct netdev *netdev)
1777 {
1778 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
1779 int ifindex;
1780
1781 ovs_mutex_lock(&dev->mutex);
1782 ifindex = dev->port_id;
1783 ovs_mutex_unlock(&dev->mutex);
1784
1785 return ifindex;
1786 }
1787
1788 static int
1789 netdev_dpdk_get_carrier(const struct netdev *netdev_, bool *carrier)
1790 {
1791 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1792
1793 ovs_mutex_lock(&dev->mutex);
1794 check_link_status(dev);
1795 *carrier = dev->link.link_status;
1796
1797 ovs_mutex_unlock(&dev->mutex);
1798
1799 return 0;
1800 }
1801
1802 static int
1803 netdev_dpdk_vhost_get_carrier(const struct netdev *netdev_, bool *carrier)
1804 {
1805 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1806 struct virtio_net *virtio_dev = netdev_dpdk_get_virtio(dev);
1807
1808 ovs_mutex_lock(&dev->mutex);
1809
1810 if (is_vhost_running(virtio_dev)) {
1811 *carrier = 1;
1812 } else {
1813 *carrier = 0;
1814 }
1815
1816 ovs_mutex_unlock(&dev->mutex);
1817
1818 return 0;
1819 }
1820
1821 static long long int
1822 netdev_dpdk_get_carrier_resets(const struct netdev *netdev_)
1823 {
1824 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1825 long long int carrier_resets;
1826
1827 ovs_mutex_lock(&dev->mutex);
1828 carrier_resets = dev->link_reset_cnt;
1829 ovs_mutex_unlock(&dev->mutex);
1830
1831 return carrier_resets;
1832 }
1833
1834 static int
1835 netdev_dpdk_set_miimon(struct netdev *netdev_ OVS_UNUSED,
1836 long long int interval OVS_UNUSED)
1837 {
1838 return EOPNOTSUPP;
1839 }
1840
1841 static int
1842 netdev_dpdk_update_flags__(struct netdev_dpdk *dev,
1843 enum netdev_flags off, enum netdev_flags on,
1844 enum netdev_flags *old_flagsp) OVS_REQUIRES(dev->mutex)
1845 {
1846 int err;
1847
1848 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1849 return EINVAL;
1850 }
1851
1852 *old_flagsp = dev->flags;
1853 dev->flags |= on;
1854 dev->flags &= ~off;
1855
1856 if (dev->flags == *old_flagsp) {
1857 return 0;
1858 }
1859
1860 if (dev->type == DPDK_DEV_ETH) {
1861 if (dev->flags & NETDEV_UP) {
1862 err = rte_eth_dev_start(dev->port_id);
1863 if (err)
1864 return -err;
1865 }
1866
1867 if (dev->flags & NETDEV_PROMISC) {
1868 rte_eth_promiscuous_enable(dev->port_id);
1869 }
1870
1871 if (!(dev->flags & NETDEV_UP)) {
1872 rte_eth_dev_stop(dev->port_id);
1873 }
1874 }
1875
1876 return 0;
1877 }
1878
1879 static int
1880 netdev_dpdk_update_flags(struct netdev *netdev_,
1881 enum netdev_flags off, enum netdev_flags on,
1882 enum netdev_flags *old_flagsp)
1883 {
1884 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
1885 int error;
1886
1887 ovs_mutex_lock(&netdev->mutex);
1888 error = netdev_dpdk_update_flags__(netdev, off, on, old_flagsp);
1889 ovs_mutex_unlock(&netdev->mutex);
1890
1891 return error;
1892 }
1893
1894 static int
1895 netdev_dpdk_get_status(const struct netdev *netdev_, struct smap *args)
1896 {
1897 struct netdev_dpdk *dev = netdev_dpdk_cast(netdev_);
1898 struct rte_eth_dev_info dev_info;
1899
1900 if (dev->port_id < 0)
1901 return ENODEV;
1902
1903 ovs_mutex_lock(&dev->mutex);
1904 rte_eth_dev_info_get(dev->port_id, &dev_info);
1905 ovs_mutex_unlock(&dev->mutex);
1906
1907 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1908
1909 smap_add_format(args, "port_no", "%d", dev->port_id);
1910 smap_add_format(args, "numa_id", "%d", rte_eth_dev_socket_id(dev->port_id));
1911 smap_add_format(args, "driver_name", "%s", dev_info.driver_name);
1912 smap_add_format(args, "min_rx_bufsize", "%u", dev_info.min_rx_bufsize);
1913 smap_add_format(args, "max_rx_pktlen", "%u", dev->max_packet_len);
1914 smap_add_format(args, "max_rx_queues", "%u", dev_info.max_rx_queues);
1915 smap_add_format(args, "max_tx_queues", "%u", dev_info.max_tx_queues);
1916 smap_add_format(args, "max_mac_addrs", "%u", dev_info.max_mac_addrs);
1917 smap_add_format(args, "max_hash_mac_addrs", "%u", dev_info.max_hash_mac_addrs);
1918 smap_add_format(args, "max_vfs", "%u", dev_info.max_vfs);
1919 smap_add_format(args, "max_vmdq_pools", "%u", dev_info.max_vmdq_pools);
1920
1921 if (dev_info.pci_dev) {
1922 smap_add_format(args, "pci-vendor_id", "0x%u",
1923 dev_info.pci_dev->id.vendor_id);
1924 smap_add_format(args, "pci-device_id", "0x%x",
1925 dev_info.pci_dev->id.device_id);
1926 }
1927
1928 return 0;
1929 }
1930
1931 static void
1932 netdev_dpdk_set_admin_state__(struct netdev_dpdk *dev, bool admin_state)
1933 OVS_REQUIRES(dev->mutex)
1934 {
1935 enum netdev_flags old_flags;
1936
1937 if (admin_state) {
1938 netdev_dpdk_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1939 } else {
1940 netdev_dpdk_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1941 }
1942 }
1943
1944 static void
1945 netdev_dpdk_set_admin_state(struct unixctl_conn *conn, int argc,
1946 const char *argv[], void *aux OVS_UNUSED)
1947 {
1948 bool up;
1949
1950 if (!strcasecmp(argv[argc - 1], "up")) {
1951 up = true;
1952 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1953 up = false;
1954 } else {
1955 unixctl_command_reply_error(conn, "Invalid Admin State");
1956 return;
1957 }
1958
1959 if (argc > 2) {
1960 struct netdev *netdev = netdev_from_name(argv[1]);
1961 if (netdev && is_dpdk_class(netdev->netdev_class)) {
1962 struct netdev_dpdk *dpdk_dev = netdev_dpdk_cast(netdev);
1963
1964 ovs_mutex_lock(&dpdk_dev->mutex);
1965 netdev_dpdk_set_admin_state__(dpdk_dev, up);
1966 ovs_mutex_unlock(&dpdk_dev->mutex);
1967
1968 netdev_close(netdev);
1969 } else {
1970 unixctl_command_reply_error(conn, "Not a DPDK Interface");
1971 netdev_close(netdev);
1972 return;
1973 }
1974 } else {
1975 struct netdev_dpdk *netdev;
1976
1977 ovs_mutex_lock(&dpdk_mutex);
1978 LIST_FOR_EACH (netdev, list_node, &dpdk_list) {
1979 ovs_mutex_lock(&netdev->mutex);
1980 netdev_dpdk_set_admin_state__(netdev, up);
1981 ovs_mutex_unlock(&netdev->mutex);
1982 }
1983 ovs_mutex_unlock(&dpdk_mutex);
1984 }
1985 unixctl_command_reply(conn, "OK");
1986 }
1987
1988 /*
1989 * Set virtqueue flags so that we do not receive interrupts.
1990 */
1991 static void
1992 set_irq_status(struct virtio_net *dev)
1993 {
1994 uint32_t i;
1995 uint64_t idx;
1996
1997 for (i = 0; i < dev->virt_qp_nb; i++) {
1998 idx = i * VIRTIO_QNUM;
1999 rte_vhost_enable_guest_notification(dev, idx + VIRTIO_RXQ, 0);
2000 rte_vhost_enable_guest_notification(dev, idx + VIRTIO_TXQ, 0);
2001 }
2002 }
2003
2004 /*
2005 * Fixes mapping for vhost-user tx queues. Must be called after each
2006 * enabling/disabling of queues and real_n_txq modifications.
2007 */
2008 static void
2009 netdev_dpdk_remap_txqs(struct netdev_dpdk *netdev)
2010 OVS_REQUIRES(netdev->mutex)
2011 {
2012 int *enabled_queues, n_enabled = 0;
2013 int i, k, total_txqs = netdev->real_n_txq;
2014
2015 enabled_queues = dpdk_rte_mzalloc(total_txqs * sizeof *enabled_queues);
2016
2017 for (i = 0; i < total_txqs; i++) {
2018 /* Enabled queues always mapped to themselves. */
2019 if (netdev->tx_q[i].map == i) {
2020 enabled_queues[n_enabled++] = i;
2021 }
2022 }
2023
2024 if (n_enabled == 0 && total_txqs != 0) {
2025 enabled_queues[0] = OVS_VHOST_QUEUE_DISABLED;
2026 n_enabled = 1;
2027 }
2028
2029 k = 0;
2030 for (i = 0; i < total_txqs; i++) {
2031 if (netdev->tx_q[i].map != i) {
2032 netdev->tx_q[i].map = enabled_queues[k];
2033 k = (k + 1) % n_enabled;
2034 }
2035 }
2036
2037 VLOG_DBG("TX queue mapping for %s\n", netdev->vhost_id);
2038 for (i = 0; i < total_txqs; i++) {
2039 VLOG_DBG("%2d --> %2d", i, netdev->tx_q[i].map);
2040 }
2041
2042 rte_free(enabled_queues);
2043 }
2044
2045 static int
2046 netdev_dpdk_vhost_set_queues(struct netdev_dpdk *netdev, struct virtio_net *dev)
2047 OVS_REQUIRES(netdev->mutex)
2048 {
2049 uint32_t qp_num;
2050
2051 qp_num = dev->virt_qp_nb;
2052 if (qp_num > netdev->up.n_rxq) {
2053 VLOG_ERR("vHost Device '%s' %"PRIu64" can't be added - "
2054 "too many queues %d > %d", dev->ifname, dev->device_fh,
2055 qp_num, netdev->up.n_rxq);
2056 return -1;
2057 }
2058
2059 netdev->real_n_rxq = qp_num;
2060 netdev->real_n_txq = qp_num;
2061 netdev->txq_needs_locking = true;
2062 /* Enable TX queue 0 by default if it wasn't disabled. */
2063 if (netdev->tx_q[0].map == OVS_VHOST_QUEUE_MAP_UNKNOWN) {
2064 netdev->tx_q[0].map = 0;
2065 }
2066
2067 netdev_dpdk_remap_txqs(netdev);
2068
2069 return 0;
2070 }
2071
2072 /*
2073 * A new virtio-net device is added to a vhost port.
2074 */
2075 static int
2076 new_device(struct virtio_net *dev)
2077 {
2078 struct netdev_dpdk *netdev;
2079 bool exists = false;
2080
2081 ovs_mutex_lock(&dpdk_mutex);
2082 /* Add device to the vhost port with the same name as that passed down. */
2083 LIST_FOR_EACH(netdev, list_node, &dpdk_list) {
2084 if (strncmp(dev->ifname, netdev->vhost_id, IF_NAME_SZ) == 0) {
2085 ovs_mutex_lock(&netdev->mutex);
2086 if (netdev_dpdk_vhost_set_queues(netdev, dev)) {
2087 ovs_mutex_unlock(&netdev->mutex);
2088 ovs_mutex_unlock(&dpdk_mutex);
2089 return -1;
2090 }
2091 ovsrcu_set(&netdev->virtio_dev, dev);
2092 exists = true;
2093 dev->flags |= VIRTIO_DEV_RUNNING;
2094 /* Disable notifications. */
2095 set_irq_status(dev);
2096 ovs_mutex_unlock(&netdev->mutex);
2097 break;
2098 }
2099 }
2100 ovs_mutex_unlock(&dpdk_mutex);
2101
2102 if (!exists) {
2103 VLOG_INFO("vHost Device '%s' %"PRIu64" can't be added - name not "
2104 "found", dev->ifname, dev->device_fh);
2105
2106 return -1;
2107 }
2108
2109 VLOG_INFO("vHost Device '%s' %"PRIu64" has been added", dev->ifname,
2110 dev->device_fh);
2111 return 0;
2112 }
2113
2114 /* Clears mapping for all available queues of vhost interface. */
2115 static void
2116 netdev_dpdk_txq_map_clear(struct netdev_dpdk *dev)
2117 OVS_REQUIRES(dev->mutex)
2118 {
2119 int i;
2120
2121 for (i = 0; i < dev->real_n_txq; i++) {
2122 dev->tx_q[i].map = OVS_VHOST_QUEUE_MAP_UNKNOWN;
2123 }
2124 }
2125
2126 /*
2127 * Remove a virtio-net device from the specific vhost port. Use dev->remove
2128 * flag to stop any more packets from being sent or received to/from a VM and
2129 * ensure all currently queued packets have been sent/received before removing
2130 * the device.
2131 */
2132 static void
2133 destroy_device(volatile struct virtio_net *dev)
2134 {
2135 struct netdev_dpdk *vhost_dev;
2136 bool exists = false;
2137
2138 ovs_mutex_lock(&dpdk_mutex);
2139 LIST_FOR_EACH (vhost_dev, list_node, &dpdk_list) {
2140 if (netdev_dpdk_get_virtio(vhost_dev) == dev) {
2141
2142 ovs_mutex_lock(&vhost_dev->mutex);
2143 dev->flags &= ~VIRTIO_DEV_RUNNING;
2144 ovsrcu_set(&vhost_dev->virtio_dev, NULL);
2145 netdev_dpdk_txq_map_clear(vhost_dev);
2146 exists = true;
2147 ovs_mutex_unlock(&vhost_dev->mutex);
2148 break;
2149 }
2150 }
2151
2152 ovs_mutex_unlock(&dpdk_mutex);
2153
2154 if (exists == true) {
2155 /*
2156 * Wait for other threads to quiesce after setting the 'virtio_dev'
2157 * to NULL, before returning.
2158 */
2159 ovsrcu_synchronize();
2160 /*
2161 * As call to ovsrcu_synchronize() will end the quiescent state,
2162 * put thread back into quiescent state before returning.
2163 */
2164 ovsrcu_quiesce_start();
2165 VLOG_INFO("vHost Device '%s' %"PRIu64" has been removed", dev->ifname,
2166 dev->device_fh);
2167 } else {
2168 VLOG_INFO("vHost Device '%s' %"PRIu64" not found", dev->ifname,
2169 dev->device_fh);
2170 }
2171
2172 }
2173
2174 static int
2175 vring_state_changed(struct virtio_net *dev, uint16_t queue_id, int enable)
2176 {
2177 struct netdev_dpdk *vhost_dev;
2178 bool exists = false;
2179 int qid = queue_id / VIRTIO_QNUM;
2180
2181 if (queue_id % VIRTIO_QNUM == VIRTIO_TXQ) {
2182 return 0;
2183 }
2184
2185 ovs_mutex_lock(&dpdk_mutex);
2186 LIST_FOR_EACH (vhost_dev, list_node, &dpdk_list) {
2187 if (strncmp(dev->ifname, vhost_dev->vhost_id, IF_NAME_SZ) == 0) {
2188 ovs_mutex_lock(&vhost_dev->mutex);
2189 if (enable) {
2190 vhost_dev->tx_q[qid].map = qid;
2191 } else {
2192 vhost_dev->tx_q[qid].map = OVS_VHOST_QUEUE_DISABLED;
2193 }
2194 netdev_dpdk_remap_txqs(vhost_dev);
2195 exists = true;
2196 ovs_mutex_unlock(&vhost_dev->mutex);
2197 break;
2198 }
2199 }
2200 ovs_mutex_unlock(&dpdk_mutex);
2201
2202 if (exists) {
2203 VLOG_INFO("State of queue %d ( tx_qid %d ) of vhost device '%s' %"
2204 PRIu64" changed to \'%s\'", queue_id, qid, dev->ifname,
2205 dev->device_fh, (enable == 1) ? "enabled" : "disabled");
2206 } else {
2207 VLOG_INFO("vHost Device '%s' %"PRIu64" not found", dev->ifname,
2208 dev->device_fh);
2209 return -1;
2210 }
2211
2212 return 0;
2213 }
2214
2215 struct virtio_net *
2216 netdev_dpdk_get_virtio(const struct netdev_dpdk *dev)
2217 {
2218 return ovsrcu_get(struct virtio_net *, &dev->virtio_dev);
2219 }
2220
2221 /*
2222 * These callbacks allow virtio-net devices to be added to vhost ports when
2223 * configuration has been fully complete.
2224 */
2225 static const struct virtio_net_device_ops virtio_net_device_ops =
2226 {
2227 .new_device = new_device,
2228 .destroy_device = destroy_device,
2229 .vring_state_changed = vring_state_changed
2230 };
2231
2232 static void *
2233 start_vhost_loop(void *dummy OVS_UNUSED)
2234 {
2235 pthread_detach(pthread_self());
2236 /* Put the cuse thread into quiescent state. */
2237 ovsrcu_quiesce_start();
2238 rte_vhost_driver_session_start();
2239 return NULL;
2240 }
2241
2242 static int
2243 dpdk_vhost_class_init(void)
2244 {
2245 rte_vhost_driver_callback_register(&virtio_net_device_ops);
2246 ovs_thread_create("vhost_thread", start_vhost_loop, NULL);
2247 return 0;
2248 }
2249
2250 static int
2251 dpdk_vhost_cuse_class_init(void)
2252 {
2253 int err = -1;
2254
2255
2256 /* Register CUSE device to handle IOCTLs.
2257 * Unless otherwise specified on the vswitchd command line, cuse_dev_name
2258 * is set to vhost-net.
2259 */
2260 err = rte_vhost_driver_register(cuse_dev_name);
2261
2262 if (err != 0) {
2263 VLOG_ERR("CUSE device setup failure.");
2264 return -1;
2265 }
2266
2267 dpdk_vhost_class_init();
2268 return 0;
2269 }
2270
2271 static int
2272 dpdk_vhost_user_class_init(void)
2273 {
2274 dpdk_vhost_class_init();
2275 return 0;
2276 }
2277
2278 static void
2279 dpdk_common_init(void)
2280 {
2281 unixctl_command_register("netdev-dpdk/set-admin-state",
2282 "[netdev] up|down", 1, 2,
2283 netdev_dpdk_set_admin_state, NULL);
2284
2285 ovs_thread_create("dpdk_watchdog", dpdk_watchdog, NULL);
2286 }
2287
2288 /* Client Rings */
2289
2290 static int
2291 dpdk_ring_create(const char dev_name[], unsigned int port_no,
2292 unsigned int *eth_port_id)
2293 {
2294 struct dpdk_ring *ivshmem;
2295 char ring_name[RTE_RING_NAMESIZE];
2296 int err;
2297
2298 ivshmem = dpdk_rte_mzalloc(sizeof *ivshmem);
2299 if (ivshmem == NULL) {
2300 return ENOMEM;
2301 }
2302
2303 /* XXX: Add support for multiquque ring. */
2304 err = snprintf(ring_name, sizeof(ring_name), "%s_tx", dev_name);
2305 if (err < 0) {
2306 return -err;
2307 }
2308
2309 /* Create single producer tx ring, netdev does explicit locking. */
2310 ivshmem->cring_tx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2311 RING_F_SP_ENQ);
2312 if (ivshmem->cring_tx == NULL) {
2313 rte_free(ivshmem);
2314 return ENOMEM;
2315 }
2316
2317 err = snprintf(ring_name, sizeof(ring_name), "%s_rx", dev_name);
2318 if (err < 0) {
2319 return -err;
2320 }
2321
2322 /* Create single consumer rx ring, netdev does explicit locking. */
2323 ivshmem->cring_rx = rte_ring_create(ring_name, DPDK_RING_SIZE, SOCKET0,
2324 RING_F_SC_DEQ);
2325 if (ivshmem->cring_rx == NULL) {
2326 rte_free(ivshmem);
2327 return ENOMEM;
2328 }
2329
2330 err = rte_eth_from_rings(dev_name, &ivshmem->cring_rx, 1,
2331 &ivshmem->cring_tx, 1, SOCKET0);
2332
2333 if (err < 0) {
2334 rte_free(ivshmem);
2335 return ENODEV;
2336 }
2337
2338 ivshmem->user_port_id = port_no;
2339 ivshmem->eth_port_id = rte_eth_dev_count() - 1;
2340 list_push_back(&dpdk_ring_list, &ivshmem->list_node);
2341
2342 *eth_port_id = ivshmem->eth_port_id;
2343 return 0;
2344 }
2345
2346 static int
2347 dpdk_ring_open(const char dev_name[], unsigned int *eth_port_id) OVS_REQUIRES(dpdk_mutex)
2348 {
2349 struct dpdk_ring *ivshmem;
2350 unsigned int port_no;
2351 int err = 0;
2352
2353 /* Names always start with "dpdkr" */
2354 err = dpdk_dev_parse_name(dev_name, "dpdkr", &port_no);
2355 if (err) {
2356 return err;
2357 }
2358
2359 /* look through our list to find the device */
2360 LIST_FOR_EACH (ivshmem, list_node, &dpdk_ring_list) {
2361 if (ivshmem->user_port_id == port_no) {
2362 VLOG_INFO("Found dpdk ring device %s:", dev_name);
2363 *eth_port_id = ivshmem->eth_port_id; /* really all that is needed */
2364 return 0;
2365 }
2366 }
2367 /* Need to create the device rings */
2368 return dpdk_ring_create(dev_name, port_no, eth_port_id);
2369 }
2370
2371 static int
2372 netdev_dpdk_ring_send(struct netdev *netdev_, int qid,
2373 struct dp_packet **pkts, int cnt, bool may_steal)
2374 {
2375 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
2376 unsigned i;
2377
2378 /* When using 'dpdkr' and sending to a DPDK ring, we want to ensure that the
2379 * rss hash field is clear. This is because the same mbuf may be modified by
2380 * the consumer of the ring and return into the datapath without recalculating
2381 * the RSS hash. */
2382 for (i = 0; i < cnt; i++) {
2383 dp_packet_rss_invalidate(pkts[i]);
2384 }
2385
2386 netdev_dpdk_send__(netdev, qid, pkts, cnt, may_steal);
2387 return 0;
2388 }
2389
2390 static int
2391 netdev_dpdk_ring_construct(struct netdev *netdev)
2392 {
2393 unsigned int port_no = 0;
2394 int err = 0;
2395
2396 if (rte_eal_init_ret) {
2397 return rte_eal_init_ret;
2398 }
2399
2400 ovs_mutex_lock(&dpdk_mutex);
2401
2402 err = dpdk_ring_open(netdev->name, &port_no);
2403 if (err) {
2404 goto unlock_dpdk;
2405 }
2406
2407 err = netdev_dpdk_init(netdev, port_no, DPDK_DEV_ETH);
2408
2409 unlock_dpdk:
2410 ovs_mutex_unlock(&dpdk_mutex);
2411 return err;
2412 }
2413
2414 /* QoS Functions */
2415
2416 /*
2417 * Initialize QoS configuration operations.
2418 */
2419 static void
2420 qos_conf_init(struct qos_conf *conf, const struct dpdk_qos_ops *ops)
2421 {
2422 conf->ops = ops;
2423 }
2424
2425 /*
2426 * Search existing QoS operations in qos_ops and compare each set of
2427 * operations qos_name to name. Return a dpdk_qos_ops pointer to a match,
2428 * else return NULL
2429 */
2430 static const struct dpdk_qos_ops *
2431 qos_lookup_name(const char *name)
2432 {
2433 const struct dpdk_qos_ops *const *opsp;
2434
2435 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2436 const struct dpdk_qos_ops *ops = *opsp;
2437 if (!strcmp(name, ops->qos_name)) {
2438 return ops;
2439 }
2440 }
2441 return NULL;
2442 }
2443
2444 /*
2445 * Call qos_destruct to clean up items associated with the netdevs
2446 * qos_conf. Set netdevs qos_conf to NULL.
2447 */
2448 static void
2449 qos_delete_conf(struct netdev *netdev_)
2450 {
2451 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
2452
2453 rte_spinlock_lock(&netdev->qos_lock);
2454 if (netdev->qos_conf) {
2455 if (netdev->qos_conf->ops->qos_destruct) {
2456 netdev->qos_conf->ops->qos_destruct(netdev_, netdev->qos_conf);
2457 }
2458 netdev->qos_conf = NULL;
2459 }
2460 rte_spinlock_unlock(&netdev->qos_lock);
2461 }
2462
2463 static int
2464 netdev_dpdk_get_qos_types(const struct netdev *netdev OVS_UNUSED,
2465 struct sset *types)
2466 {
2467 const struct dpdk_qos_ops *const *opsp;
2468
2469 for (opsp = qos_confs; *opsp != NULL; opsp++) {
2470 const struct dpdk_qos_ops *ops = *opsp;
2471 if (ops->qos_construct && ops->qos_name[0] != '\0') {
2472 sset_add(types, ops->qos_name);
2473 }
2474 }
2475 return 0;
2476 }
2477
2478 static int
2479 netdev_dpdk_get_qos(const struct netdev *netdev_,
2480 const char **typep, struct smap *details)
2481 {
2482 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
2483 int error = 0;
2484
2485 ovs_mutex_lock(&netdev->mutex);
2486 if(netdev->qos_conf) {
2487 *typep = netdev->qos_conf->ops->qos_name;
2488 error = (netdev->qos_conf->ops->qos_get
2489 ? netdev->qos_conf->ops->qos_get(netdev_, details): 0);
2490 }
2491 ovs_mutex_unlock(&netdev->mutex);
2492
2493 return error;
2494 }
2495
2496 static int
2497 netdev_dpdk_set_qos(struct netdev *netdev_,
2498 const char *type, const struct smap *details)
2499 {
2500 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
2501 const struct dpdk_qos_ops *new_ops = NULL;
2502 int error = 0;
2503
2504 /* If type is empty or unsupported then the current QoS configuration
2505 * for the dpdk-netdev can be destroyed */
2506 new_ops = qos_lookup_name(type);
2507
2508 if (type[0] == '\0' || !new_ops || !new_ops->qos_construct) {
2509 qos_delete_conf(netdev_);
2510 return EOPNOTSUPP;
2511 }
2512
2513 ovs_mutex_lock(&netdev->mutex);
2514
2515 if (netdev->qos_conf) {
2516 if (new_ops == netdev->qos_conf->ops) {
2517 error = new_ops->qos_set ? new_ops->qos_set(netdev_, details) : 0;
2518 } else {
2519 /* Delete existing QoS configuration. */
2520 qos_delete_conf(netdev_);
2521 ovs_assert(netdev->qos_conf == NULL);
2522
2523 /* Install new QoS configuration. */
2524 error = new_ops->qos_construct(netdev_, details);
2525 ovs_assert((error == 0) == (netdev->qos_conf != NULL));
2526 }
2527 } else {
2528 error = new_ops->qos_construct(netdev_, details);
2529 ovs_assert((error == 0) == (netdev->qos_conf != NULL));
2530 }
2531
2532 ovs_mutex_unlock(&netdev->mutex);
2533 return error;
2534 }
2535
2536 /* egress-policer details */
2537
2538 struct egress_policer {
2539 struct qos_conf qos_conf;
2540 struct rte_meter_srtcm_params app_srtcm_params;
2541 struct rte_meter_srtcm egress_meter;
2542 };
2543
2544 static struct egress_policer *
2545 egress_policer_get__(const struct netdev *netdev_)
2546 {
2547 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
2548 return CONTAINER_OF(netdev->qos_conf, struct egress_policer, qos_conf);
2549 }
2550
2551 static int
2552 egress_policer_qos_construct(struct netdev *netdev_,
2553 const struct smap *details)
2554 {
2555 struct netdev_dpdk *netdev = netdev_dpdk_cast(netdev_);
2556 struct egress_policer *policer;
2557 const char *cir_s;
2558 const char *cbs_s;
2559 int err = 0;
2560
2561 rte_spinlock_lock(&netdev->qos_lock);
2562 policer = xmalloc(sizeof *policer);
2563 qos_conf_init(&policer->qos_conf, &egress_policer_ops);
2564 netdev->qos_conf = &policer->qos_conf;
2565 cir_s = smap_get(details, "cir");
2566 cbs_s = smap_get(details, "cbs");
2567 policer->app_srtcm_params.cir = cir_s ? strtoull(cir_s, NULL, 10) : 0;
2568 policer->app_srtcm_params.cbs = cbs_s ? strtoull(cbs_s, NULL, 10) : 0;
2569 policer->app_srtcm_params.ebs = 0;
2570 err = rte_meter_srtcm_config(&policer->egress_meter,
2571 &policer->app_srtcm_params);
2572 rte_spinlock_unlock(&netdev->qos_lock);
2573
2574 return err;
2575 }
2576
2577 static void
2578 egress_policer_qos_destruct(struct netdev *netdev_ OVS_UNUSED,
2579 struct qos_conf *conf)
2580 {
2581 struct egress_policer *policer = CONTAINER_OF(conf, struct egress_policer,
2582 qos_conf);
2583 free(policer);
2584 }
2585
2586 static int
2587 egress_policer_qos_get(const struct netdev *netdev, struct smap *details)
2588 {
2589 struct egress_policer *policer = egress_policer_get__(netdev);
2590 smap_add_format(details, "cir", "%llu",
2591 1ULL * policer->app_srtcm_params.cir);
2592 smap_add_format(details, "cbs", "%llu",
2593 1ULL * policer->app_srtcm_params.cbs);
2594 return 0;
2595 }
2596
2597 static int
2598 egress_policer_qos_set(struct netdev *netdev_, const struct smap *details)
2599 {
2600 struct egress_policer *policer;
2601 const char *cir_s;
2602 const char *cbs_s;
2603 int err = 0;
2604
2605 policer = egress_policer_get__(netdev_);
2606 cir_s = smap_get(details, "cir");
2607 cbs_s = smap_get(details, "cbs");
2608 policer->app_srtcm_params.cir = cir_s ? strtoull(cir_s, NULL, 10) : 0;
2609 policer->app_srtcm_params.cbs = cbs_s ? strtoull(cbs_s, NULL, 10) : 0;
2610 policer->app_srtcm_params.ebs = 0;
2611 err = rte_meter_srtcm_config(&policer->egress_meter,
2612 &policer->app_srtcm_params);
2613
2614 return err;
2615 }
2616
2617 static inline bool
2618 egress_policer_pkt_handle__(struct rte_meter_srtcm *meter,
2619 struct rte_mbuf *pkt, uint64_t time)
2620 {
2621 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
2622
2623 return rte_meter_srtcm_color_blind_check(meter, time, pkt_len) ==
2624 e_RTE_METER_GREEN;
2625 }
2626
2627 static int
2628 egress_policer_run(struct netdev *netdev_, struct rte_mbuf **pkts,
2629 int pkt_cnt)
2630 {
2631 int i = 0;
2632 int cnt = 0;
2633 struct egress_policer *policer = egress_policer_get__(netdev_);
2634 struct rte_mbuf *pkt = NULL;
2635 uint64_t current_time = rte_rdtsc();
2636
2637 for(i = 0; i < pkt_cnt; i++) {
2638 pkt = pkts[i];
2639 /* Handle current packet */
2640 if (egress_policer_pkt_handle__(&policer->egress_meter, pkt,
2641 current_time)) {
2642 if (cnt != i) {
2643 pkts[cnt] = pkt;
2644 }
2645 cnt++;
2646 } else {
2647 rte_pktmbuf_free(pkt);
2648 }
2649 }
2650
2651 return cnt;
2652 }
2653
2654 static const struct dpdk_qos_ops egress_policer_ops = {
2655 "egress-policer", /* qos_name */
2656 egress_policer_qos_construct,
2657 egress_policer_qos_destruct,
2658 egress_policer_qos_get,
2659 egress_policer_qos_set,
2660 egress_policer_run
2661 };
2662
2663 #define NETDEV_DPDK_CLASS(NAME, INIT, CONSTRUCT, DESTRUCT, MULTIQ, SEND, \
2664 GET_CARRIER, GET_STATS, GET_FEATURES, GET_STATUS, RXQ_RECV) \
2665 { \
2666 NAME, \
2667 true, /* is_pmd */ \
2668 INIT, /* init */ \
2669 NULL, /* netdev_dpdk_run */ \
2670 NULL, /* netdev_dpdk_wait */ \
2671 \
2672 netdev_dpdk_alloc, \
2673 CONSTRUCT, \
2674 DESTRUCT, \
2675 netdev_dpdk_dealloc, \
2676 netdev_dpdk_get_config, \
2677 netdev_dpdk_set_config, \
2678 NULL, /* get_tunnel_config */ \
2679 NULL, /* build header */ \
2680 NULL, /* push header */ \
2681 NULL, /* pop header */ \
2682 netdev_dpdk_get_numa_id, /* get_numa_id */ \
2683 MULTIQ, /* set_multiq */ \
2684 \
2685 SEND, /* send */ \
2686 NULL, /* send_wait */ \
2687 \
2688 netdev_dpdk_set_etheraddr, \
2689 netdev_dpdk_get_etheraddr, \
2690 netdev_dpdk_get_mtu, \
2691 netdev_dpdk_set_mtu, \
2692 netdev_dpdk_get_ifindex, \
2693 GET_CARRIER, \
2694 netdev_dpdk_get_carrier_resets, \
2695 netdev_dpdk_set_miimon, \
2696 GET_STATS, \
2697 GET_FEATURES, \
2698 NULL, /* set_advertisements */ \
2699 \
2700 NULL, /* set_policing */ \
2701 netdev_dpdk_get_qos_types, \
2702 NULL, /* get_qos_capabilities */ \
2703 netdev_dpdk_get_qos, \
2704 netdev_dpdk_set_qos, \
2705 NULL, /* get_queue */ \
2706 NULL, /* set_queue */ \
2707 NULL, /* delete_queue */ \
2708 NULL, /* get_queue_stats */ \
2709 NULL, /* queue_dump_start */ \
2710 NULL, /* queue_dump_next */ \
2711 NULL, /* queue_dump_done */ \
2712 NULL, /* dump_queue_stats */ \
2713 \
2714 NULL, /* set_in4 */ \
2715 NULL, /* get_addr_list */ \
2716 NULL, /* add_router */ \
2717 NULL, /* get_next_hop */ \
2718 GET_STATUS, \
2719 NULL, /* arp_lookup */ \
2720 \
2721 netdev_dpdk_update_flags, \
2722 \
2723 netdev_dpdk_rxq_alloc, \
2724 netdev_dpdk_rxq_construct, \
2725 netdev_dpdk_rxq_destruct, \
2726 netdev_dpdk_rxq_dealloc, \
2727 RXQ_RECV, \
2728 NULL, /* rx_wait */ \
2729 NULL, /* rxq_drain */ \
2730 }
2731
2732 static int
2733 process_vhost_flags(char *flag, char *default_val, int size,
2734 char **argv, char **new_val)
2735 {
2736 int changed = 0;
2737
2738 /* Depending on which version of vhost is in use, process the vhost-specific
2739 * flag if it is provided on the vswitchd command line, otherwise resort to
2740 * a default value.
2741 *
2742 * For vhost-user: Process "-vhost_sock_dir" to set the custom location of
2743 * the vhost-user socket(s).
2744 * For vhost-cuse: Process "-cuse_dev_name" to set the custom name of the
2745 * vhost-cuse character device.
2746 */
2747 if (!strcmp(argv[1], flag) && (strlen(argv[2]) <= size)) {
2748 changed = 1;
2749 *new_val = xstrdup(argv[2]);
2750 VLOG_INFO("User-provided %s in use: %s", flag, *new_val);
2751 } else {
2752 VLOG_INFO("No %s provided - defaulting to %s", flag, default_val);
2753 *new_val = default_val;
2754 }
2755
2756 return changed;
2757 }
2758
2759 int
2760 dpdk_init(int argc, char **argv)
2761 {
2762 int result;
2763 int base = 0;
2764 char *pragram_name = argv[0];
2765
2766 if (argc < 2 || strcmp(argv[1], "--dpdk"))
2767 return 0;
2768
2769 /* Remove the --dpdk argument from arg list.*/
2770 argc--;
2771 argv++;
2772
2773 /* Reject --user option */
2774 int i;
2775 for (i = 0; i < argc; i++) {
2776 if (!strcmp(argv[i], "--user")) {
2777 VLOG_ERR("Can not mix --dpdk and --user options, aborting.");
2778 }
2779 }
2780
2781 #ifdef VHOST_CUSE
2782 if (process_vhost_flags("-cuse_dev_name", xstrdup("vhost-net"),
2783 PATH_MAX, argv, &cuse_dev_name)) {
2784 #else
2785 if (process_vhost_flags("-vhost_sock_dir", xstrdup(ovs_rundir()),
2786 NAME_MAX, argv, &vhost_sock_dir)) {
2787 struct stat s;
2788 int err;
2789
2790 err = stat(vhost_sock_dir, &s);
2791 if (err) {
2792 VLOG_ERR("vHostUser socket DIR '%s' does not exist.",
2793 vhost_sock_dir);
2794 return err;
2795 }
2796 #endif
2797 /* Remove the vhost flag configuration parameters from the argument
2798 * list, so that the correct elements are passed to the DPDK
2799 * initialization function
2800 */
2801 argc -= 2;
2802 argv += 2; /* Increment by two to bypass the vhost flag arguments */
2803 base = 2;
2804 }
2805
2806 /* Keep the program name argument as this is needed for call to
2807 * rte_eal_init()
2808 */
2809 argv[0] = pragram_name;
2810
2811 /* Make sure things are initialized ... */
2812 result = rte_eal_init(argc, argv);
2813 if (result < 0) {
2814 ovs_abort(result, "Cannot init EAL");
2815 }
2816
2817 rte_memzone_dump(stdout);
2818 rte_eal_init_ret = 0;
2819
2820 if (argc > result) {
2821 argv[result] = argv[0];
2822 }
2823
2824 /* We are called from the main thread here */
2825 RTE_PER_LCORE(_lcore_id) = NON_PMD_CORE_ID;
2826
2827 return result + 1 + base;
2828 }
2829
2830 static const struct netdev_class dpdk_class =
2831 NETDEV_DPDK_CLASS(
2832 "dpdk",
2833 NULL,
2834 netdev_dpdk_construct,
2835 netdev_dpdk_destruct,
2836 netdev_dpdk_set_multiq,
2837 netdev_dpdk_eth_send,
2838 netdev_dpdk_get_carrier,
2839 netdev_dpdk_get_stats,
2840 netdev_dpdk_get_features,
2841 netdev_dpdk_get_status,
2842 netdev_dpdk_rxq_recv);
2843
2844 static const struct netdev_class dpdk_ring_class =
2845 NETDEV_DPDK_CLASS(
2846 "dpdkr",
2847 NULL,
2848 netdev_dpdk_ring_construct,
2849 netdev_dpdk_destruct,
2850 netdev_dpdk_set_multiq,
2851 netdev_dpdk_ring_send,
2852 netdev_dpdk_get_carrier,
2853 netdev_dpdk_get_stats,
2854 netdev_dpdk_get_features,
2855 netdev_dpdk_get_status,
2856 netdev_dpdk_rxq_recv);
2857
2858 static const struct netdev_class OVS_UNUSED dpdk_vhost_cuse_class =
2859 NETDEV_DPDK_CLASS(
2860 "dpdkvhostcuse",
2861 dpdk_vhost_cuse_class_init,
2862 netdev_dpdk_vhost_cuse_construct,
2863 netdev_dpdk_vhost_destruct,
2864 netdev_dpdk_vhost_cuse_set_multiq,
2865 netdev_dpdk_vhost_send,
2866 netdev_dpdk_vhost_get_carrier,
2867 netdev_dpdk_vhost_get_stats,
2868 NULL,
2869 NULL,
2870 netdev_dpdk_vhost_rxq_recv);
2871
2872 static const struct netdev_class OVS_UNUSED dpdk_vhost_user_class =
2873 NETDEV_DPDK_CLASS(
2874 "dpdkvhostuser",
2875 dpdk_vhost_user_class_init,
2876 netdev_dpdk_vhost_user_construct,
2877 netdev_dpdk_vhost_destruct,
2878 netdev_dpdk_vhost_set_multiq,
2879 netdev_dpdk_vhost_send,
2880 netdev_dpdk_vhost_get_carrier,
2881 netdev_dpdk_vhost_get_stats,
2882 NULL,
2883 NULL,
2884 netdev_dpdk_vhost_rxq_recv);
2885
2886 void
2887 netdev_dpdk_register(void)
2888 {
2889 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2890
2891 if (rte_eal_init_ret) {
2892 return;
2893 }
2894
2895 if (ovsthread_once_start(&once)) {
2896 dpdk_common_init();
2897 netdev_register_provider(&dpdk_class);
2898 netdev_register_provider(&dpdk_ring_class);
2899 #ifdef VHOST_CUSE
2900 netdev_register_provider(&dpdk_vhost_cuse_class);
2901 #else
2902 netdev_register_provider(&dpdk_vhost_user_class);
2903 #endif
2904 ovsthread_once_done(&once);
2905 }
2906 }
2907
2908 int
2909 pmd_thread_setaffinity_cpu(unsigned cpu)
2910 {
2911 cpu_set_t cpuset;
2912 int err;
2913
2914 CPU_ZERO(&cpuset);
2915 CPU_SET(cpu, &cpuset);
2916 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
2917 if (err) {
2918 VLOG_ERR("Thread affinity error %d",err);
2919 return err;
2920 }
2921 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
2922 ovs_assert(cpu != NON_PMD_CORE_ID);
2923 RTE_PER_LCORE(_lcore_id) = cpu;
2924
2925 return 0;
2926 }
2927
2928 static bool
2929 dpdk_thread_is_pmd(void)
2930 {
2931 return rte_lcore_id() != NON_PMD_CORE_ID;
2932 }