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