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