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