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