Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016 IGEL Co., Ltd.
3 : : * Copyright(c) 2016-2018 Intel Corporation
4 : : */
5 : : #include <stdlib.h>
6 : : #include <unistd.h>
7 : : #include <pthread.h>
8 : : #include <stdbool.h>
9 : : #include <sys/epoll.h>
10 : :
11 : : #include <eal_export.h>
12 : : #include <rte_mbuf.h>
13 : : #include <ethdev_driver.h>
14 : : #include <ethdev_vdev.h>
15 : : #include <rte_malloc.h>
16 : : #include <rte_memcpy.h>
17 : : #include <rte_net.h>
18 : : #include <bus_vdev_driver.h>
19 : : #include <rte_kvargs.h>
20 : : #include <rte_vhost.h>
21 : : #include <rte_spinlock.h>
22 : :
23 : : #include "rte_eth_vhost.h"
24 : :
25 [ - + ]: 305 : RTE_LOG_REGISTER_DEFAULT(vhost_logtype, NOTICE);
26 : : #define RTE_LOGTYPE_VHOST vhost_logtype
27 : :
28 : : #define VHOST_LOG_LINE(level, ...) \
29 : : RTE_LOG_LINE(level, VHOST, __VA_ARGS__)
30 : :
31 : : enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
32 : :
33 : : #define ETH_VHOST_IFACE_ARG "iface"
34 : : #define ETH_VHOST_QUEUES_ARG "queues"
35 : : #define ETH_VHOST_CLIENT_ARG "client"
36 : : #define ETH_VHOST_IOMMU_SUPPORT "iommu-support"
37 : : #define ETH_VHOST_POSTCOPY_SUPPORT "postcopy-support"
38 : : #define ETH_VHOST_VIRTIO_NET_F_HOST_TSO "tso"
39 : : #define ETH_VHOST_LINEAR_BUF "linear-buffer"
40 : : #define ETH_VHOST_EXT_BUF "ext-buffer"
41 : : #define ETH_VHOST_LEGACY_OL_FLAGS "legacy-ol-flags"
42 : : #define VHOST_MAX_PKT_BURST 32
43 : :
44 : : static const char *valid_arguments[] = {
45 : : ETH_VHOST_IFACE_ARG,
46 : : ETH_VHOST_QUEUES_ARG,
47 : : ETH_VHOST_CLIENT_ARG,
48 : : ETH_VHOST_IOMMU_SUPPORT,
49 : : ETH_VHOST_POSTCOPY_SUPPORT,
50 : : ETH_VHOST_VIRTIO_NET_F_HOST_TSO,
51 : : ETH_VHOST_LINEAR_BUF,
52 : : ETH_VHOST_EXT_BUF,
53 : : ETH_VHOST_LEGACY_OL_FLAGS,
54 : : NULL
55 : : };
56 : :
57 : : static struct rte_ether_addr base_eth_addr = {
58 : : .addr_bytes = {
59 : : 0x56 /* V */,
60 : : 0x48 /* H */,
61 : : 0x4F /* O */,
62 : : 0x53 /* S */,
63 : : 0x54 /* T */,
64 : : 0x00
65 : : }
66 : : };
67 : :
68 : : struct vhost_stats {
69 : : uint64_t pkts;
70 : : uint64_t bytes;
71 : : uint64_t missed_pkts;
72 : : };
73 : :
74 : : struct vhost_queue {
75 : : int vid;
76 : : RTE_ATOMIC(uint32_t) allow_queuing;
77 : : RTE_ATOMIC(uint32_t) while_queuing;
78 : : struct pmd_internal *internal;
79 : : struct rte_mempool *mb_pool;
80 : : uint16_t port;
81 : : uint16_t virtqueue_id;
82 : : struct vhost_stats stats;
83 : : rte_spinlock_t intr_lock;
84 : : struct epoll_event ev;
85 : : int kickfd;
86 : : };
87 : :
88 : : struct pmd_internal {
89 : : RTE_ATOMIC(uint32_t) dev_attached;
90 : : char *iface_name;
91 : : uint64_t flags;
92 : : uint64_t disable_flags;
93 : : uint64_t features;
94 : : uint16_t max_queues;
95 : : int vid;
96 : : RTE_ATOMIC(uint32_t) started;
97 : : bool vlan_strip;
98 : : bool rx_sw_csum;
99 : : bool tx_sw_csum;
100 : : };
101 : :
102 : : struct internal_list {
103 : : TAILQ_ENTRY(internal_list) next;
104 : : struct rte_eth_dev *eth_dev;
105 : : };
106 : :
107 : : TAILQ_HEAD(internal_list_head, internal_list);
108 : : static struct internal_list_head internal_list =
109 : : TAILQ_HEAD_INITIALIZER(internal_list);
110 : :
111 : : static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
112 : :
113 : : static struct rte_eth_link pmd_link = {
114 : : .link_speed = 10000,
115 : : .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
116 : : .link_status = RTE_ETH_LINK_DOWN
117 : : };
118 : :
119 : : struct rte_vhost_vring_state {
120 : : rte_spinlock_t lock;
121 : :
122 : : bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
123 : : bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
124 : : unsigned int index;
125 : : unsigned int max_vring;
126 : : };
127 : :
128 : : static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
129 : :
130 : : static int
131 : 0 : vhost_dev_xstats_reset(struct rte_eth_dev *dev)
132 : : {
133 : : struct vhost_queue *vq;
134 : : int ret, i;
135 : :
136 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
137 : 0 : vq = dev->data->rx_queues[i];
138 : 0 : ret = rte_vhost_vring_stats_reset(vq->vid, vq->virtqueue_id);
139 [ # # ]: 0 : if (ret < 0)
140 : 0 : return ret;
141 : : }
142 : :
143 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
144 : 0 : vq = dev->data->tx_queues[i];
145 : 0 : ret = rte_vhost_vring_stats_reset(vq->vid, vq->virtqueue_id);
146 [ # # ]: 0 : if (ret < 0)
147 : 0 : return ret;
148 : : }
149 : :
150 : : return 0;
151 : : }
152 : :
153 : : static int
154 : 0 : vhost_dev_xstats_get_names(struct rte_eth_dev *dev,
155 : : struct rte_eth_xstat_name *xstats_names,
156 : : unsigned int limit)
157 : : {
158 : : struct rte_vhost_stat_name *name;
159 : : struct vhost_queue *vq;
160 : : int ret, i, count = 0, nstats = 0;
161 : :
162 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
163 : 0 : vq = dev->data->rx_queues[i];
164 : 0 : ret = rte_vhost_vring_stats_get_names(vq->vid, vq->virtqueue_id, NULL, 0);
165 [ # # ]: 0 : if (ret < 0)
166 : 0 : return ret;
167 : :
168 : 0 : nstats += ret;
169 : : }
170 : :
171 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
172 : 0 : vq = dev->data->tx_queues[i];
173 : 0 : ret = rte_vhost_vring_stats_get_names(vq->vid, vq->virtqueue_id, NULL, 0);
174 [ # # ]: 0 : if (ret < 0)
175 : 0 : return ret;
176 : :
177 : 0 : nstats += ret;
178 : : }
179 : :
180 [ # # ]: 0 : if (!xstats_names || limit < (unsigned int)nstats)
181 : : return nstats;
182 : :
183 : 0 : name = calloc(nstats, sizeof(*name));
184 [ # # ]: 0 : if (!name)
185 : : return -1;
186 : :
187 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
188 : 0 : vq = dev->data->rx_queues[i];
189 : 0 : ret = rte_vhost_vring_stats_get_names(vq->vid, vq->virtqueue_id,
190 : 0 : name + count, nstats - count);
191 [ # # ]: 0 : if (ret < 0) {
192 : 0 : free(name);
193 : 0 : return ret;
194 : : }
195 : :
196 : 0 : count += ret;
197 : : }
198 : :
199 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
200 : 0 : vq = dev->data->tx_queues[i];
201 : 0 : ret = rte_vhost_vring_stats_get_names(vq->vid, vq->virtqueue_id,
202 : 0 : name + count, nstats - count);
203 [ # # ]: 0 : if (ret < 0) {
204 : 0 : free(name);
205 : 0 : return ret;
206 : : }
207 : :
208 : 0 : count += ret;
209 : : }
210 : :
211 [ # # ]: 0 : for (i = 0; i < count; i++)
212 : 0 : strlcpy(xstats_names[i].name, name[i].name, RTE_ETH_XSTATS_NAME_SIZE);
213 : :
214 : 0 : free(name);
215 : :
216 : 0 : return count;
217 : : }
218 : :
219 : : static int
220 : 0 : vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
221 : : unsigned int n)
222 : : {
223 : : struct rte_vhost_stat *stats;
224 : : struct vhost_queue *vq;
225 : : int ret, i, count = 0, nstats = 0;
226 : :
227 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
228 : 0 : vq = dev->data->rx_queues[i];
229 : 0 : ret = rte_vhost_vring_stats_get(vq->vid, vq->virtqueue_id, NULL, 0);
230 [ # # ]: 0 : if (ret < 0)
231 : 0 : return ret;
232 : :
233 : 0 : nstats += ret;
234 : : }
235 : :
236 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
237 : 0 : vq = dev->data->tx_queues[i];
238 : 0 : ret = rte_vhost_vring_stats_get(vq->vid, vq->virtqueue_id, NULL, 0);
239 [ # # ]: 0 : if (ret < 0)
240 : 0 : return ret;
241 : :
242 : 0 : nstats += ret;
243 : : }
244 : :
245 [ # # ]: 0 : if (!xstats || n < (unsigned int)nstats)
246 : : return nstats;
247 : :
248 : 0 : stats = calloc(nstats, sizeof(*stats));
249 [ # # ]: 0 : if (!stats)
250 : : return -1;
251 : :
252 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
253 : 0 : vq = dev->data->rx_queues[i];
254 : 0 : ret = rte_vhost_vring_stats_get(vq->vid, vq->virtqueue_id,
255 : 0 : stats + count, nstats - count);
256 [ # # ]: 0 : if (ret < 0) {
257 : 0 : free(stats);
258 : 0 : return ret;
259 : : }
260 : :
261 : 0 : count += ret;
262 : : }
263 : :
264 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
265 : 0 : vq = dev->data->tx_queues[i];
266 : 0 : ret = rte_vhost_vring_stats_get(vq->vid, vq->virtqueue_id,
267 : 0 : stats + count, nstats - count);
268 [ # # ]: 0 : if (ret < 0) {
269 : 0 : free(stats);
270 : 0 : return ret;
271 : : }
272 : :
273 : 0 : count += ret;
274 : : }
275 : :
276 [ # # ]: 0 : for (i = 0; i < count; i++) {
277 : 0 : xstats[i].id = stats[i].id;
278 : 0 : xstats[i].value = stats[i].value;
279 : : }
280 : :
281 : 0 : free(stats);
282 : :
283 : 0 : return nstats;
284 : : }
285 : :
286 : : static void
287 : 0 : vhost_dev_csum_configure(struct rte_eth_dev *eth_dev)
288 : : {
289 : 0 : struct pmd_internal *internal = eth_dev->data->dev_private;
290 : : const struct rte_eth_rxmode *rxmode = ð_dev->data->dev_conf.rxmode;
291 : : const struct rte_eth_txmode *txmode = ð_dev->data->dev_conf.txmode;
292 : :
293 : 0 : internal->rx_sw_csum = false;
294 : 0 : internal->tx_sw_csum = false;
295 : :
296 : : /* SW checksum is not compatible with legacy mode */
297 [ # # ]: 0 : if (!(internal->flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS))
298 : : return;
299 : :
300 [ # # ]: 0 : if (internal->features & (1ULL << VIRTIO_NET_F_CSUM)) {
301 [ # # ]: 0 : if (!(rxmode->offloads &
302 : : (RTE_ETH_RX_OFFLOAD_UDP_CKSUM | RTE_ETH_RX_OFFLOAD_TCP_CKSUM))) {
303 : 0 : VHOST_LOG_LINE(NOTICE, "Rx csum will be done in SW, may impact performance.");
304 : 0 : internal->rx_sw_csum = true;
305 : : }
306 : : }
307 : :
308 [ # # ]: 0 : if (!(internal->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM))) {
309 [ # # ]: 0 : if (txmode->offloads &
310 : : (RTE_ETH_TX_OFFLOAD_UDP_CKSUM | RTE_ETH_TX_OFFLOAD_TCP_CKSUM)) {
311 : 0 : VHOST_LOG_LINE(NOTICE, "Tx csum will be done in SW, may impact performance.");
312 : 0 : internal->tx_sw_csum = true;
313 : : }
314 : : }
315 : : }
316 : :
317 : : static void
318 : 0 : vhost_dev_tx_sw_csum(struct rte_mbuf *mbuf)
319 : : {
320 : : uint32_t hdr_len;
321 : 0 : uint16_t csum = 0, csum_offset;
322 : :
323 [ # # # ]: 0 : switch (mbuf->ol_flags & RTE_MBUF_F_TX_L4_MASK) {
324 : : case RTE_MBUF_F_TX_L4_NO_CKSUM:
325 : 0 : return;
326 : : case RTE_MBUF_F_TX_TCP_CKSUM:
327 : : csum_offset = offsetof(struct rte_tcp_hdr, cksum);
328 : : break;
329 : 0 : case RTE_MBUF_F_TX_UDP_CKSUM:
330 : : csum_offset = offsetof(struct rte_udp_hdr, dgram_cksum);
331 : 0 : break;
332 : : default:
333 : : /* Unsupported packet type. */
334 : : return;
335 : : }
336 : :
337 : 0 : hdr_len = mbuf->l2_len + mbuf->l3_len;
338 : 0 : csum_offset += hdr_len;
339 : :
340 : : /* Prepare the pseudo-header checksum */
341 [ # # ]: 0 : if (rte_net_intel_cksum_prepare(mbuf) < 0)
342 : : return;
343 : :
344 [ # # ]: 0 : if (rte_raw_cksum_mbuf(mbuf, hdr_len, rte_pktmbuf_pkt_len(mbuf) - hdr_len, &csum) < 0)
345 : : return;
346 : :
347 : 0 : csum = ~csum;
348 : : /* See RFC768 */
349 [ # # # # ]: 0 : if (unlikely(csum == 0) &&
350 : : (mbuf->ol_flags & RTE_MBUF_F_TX_L4_MASK) == RTE_MBUF_F_TX_UDP_CKSUM)
351 : 0 : csum = 0xffff;
352 : :
353 [ # # ]: 0 : if (rte_pktmbuf_data_len(mbuf) >= csum_offset + sizeof(uint16_t))
354 : 0 : *rte_pktmbuf_mtod_offset(mbuf, uint16_t *, csum_offset) = csum;
355 : :
356 : 0 : mbuf->ol_flags &= ~RTE_MBUF_F_TX_L4_MASK;
357 : : mbuf->ol_flags |= RTE_MBUF_F_TX_L4_NO_CKSUM;
358 : : }
359 : :
360 : : static void
361 : 0 : vhost_dev_rx_sw_csum(struct rte_mbuf *mbuf)
362 : : {
363 : : struct rte_net_hdr_lens hdr_lens;
364 : : uint32_t ptype, hdr_len;
365 : 0 : uint16_t csum = 0, csum_offset;
366 : :
367 : : /* Return early if the L4 checksum was not offloaded */
368 [ # # ]: 0 : if ((mbuf->ol_flags & RTE_MBUF_F_RX_L4_CKSUM_MASK) != RTE_MBUF_F_RX_L4_CKSUM_NONE)
369 : 0 : return;
370 : :
371 : 0 : ptype = rte_net_get_ptype(mbuf, &hdr_lens, RTE_PTYPE_ALL_MASK);
372 : :
373 : 0 : hdr_len = hdr_lens.l2_len + hdr_lens.l3_len;
374 : :
375 [ # # # ]: 0 : switch (ptype & RTE_PTYPE_L4_MASK) {
376 : 0 : case RTE_PTYPE_L4_TCP:
377 : 0 : csum_offset = offsetof(struct rte_tcp_hdr, cksum) + hdr_len;
378 : 0 : break;
379 : 0 : case RTE_PTYPE_L4_UDP:
380 : 0 : csum_offset = offsetof(struct rte_udp_hdr, dgram_cksum) + hdr_len;
381 : 0 : break;
382 : : default:
383 : : /* Unsupported packet type */
384 : : return;
385 : : }
386 : :
387 : : /* The pseudo-header checksum is already performed, as per Virtio spec */
388 [ # # ]: 0 : if (rte_raw_cksum_mbuf(mbuf, hdr_len, rte_pktmbuf_pkt_len(mbuf) - hdr_len, &csum) < 0)
389 : : return;
390 : :
391 : 0 : csum = ~csum;
392 : : /* See RFC768 */
393 [ # # # # ]: 0 : if (unlikely(csum == 0) && (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP)
394 : 0 : csum = 0xffff;
395 : :
396 [ # # ]: 0 : if (rte_pktmbuf_data_len(mbuf) >= csum_offset + sizeof(uint16_t))
397 : 0 : *rte_pktmbuf_mtod_offset(mbuf, uint16_t *, csum_offset) = csum;
398 : :
399 : 0 : mbuf->ol_flags &= ~RTE_MBUF_F_RX_L4_CKSUM_MASK;
400 : 0 : mbuf->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_GOOD;
401 : : }
402 : :
403 : : static uint16_t
404 : 0 : eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
405 : : {
406 : : struct vhost_queue *r = q;
407 : : uint16_t i, nb_rx = 0;
408 : : uint16_t nb_receive = nb_bufs;
409 : :
410 : : /* Fast-path early exit; racy load is fine here -- if we miss a
411 : : * transition we get caught by the seq_cst check below.
412 : : */
413 [ # # ]: 0 : if (unlikely(rte_atomic_load_explicit(&r->allow_queuing, rte_memory_order_relaxed) == 0))
414 : : return 0;
415 : :
416 : : /* Announce presence, then re-check. The store and the following
417 : : * load MUST both be seq_cst so they are totally ordered with the
418 : : * control thread's store-to-allow_queuing / load-of-while_queuing
419 : : * pair. Anything weaker permits both sides to miss each other.
420 : : */
421 : 0 : rte_atomic_store_explicit(&r->while_queuing, 1, rte_memory_order_seq_cst);
422 [ # # ]: 0 : if (unlikely(rte_atomic_load_explicit(&r->allow_queuing, rte_memory_order_seq_cst) == 0))
423 : 0 : goto out;
424 : :
425 : : /* Dequeue packets from guest TX queue */
426 [ # # ]: 0 : while (nb_receive) {
427 : : uint16_t nb_pkts;
428 : 0 : uint16_t num = (uint16_t)RTE_MIN(nb_receive,
429 : : VHOST_MAX_PKT_BURST);
430 : :
431 : 0 : nb_pkts = rte_vhost_dequeue_burst(r->vid, r->virtqueue_id,
432 : 0 : r->mb_pool, &bufs[nb_rx],
433 : : num);
434 : :
435 : 0 : nb_rx += nb_pkts;
436 : 0 : nb_receive -= nb_pkts;
437 [ # # ]: 0 : if (nb_pkts < num)
438 : : break;
439 : : }
440 : :
441 : 0 : r->stats.pkts += nb_rx;
442 : :
443 [ # # ]: 0 : for (i = 0; likely(i < nb_rx); i++) {
444 : 0 : bufs[i]->port = r->port;
445 : 0 : bufs[i]->vlan_tci = 0;
446 : :
447 [ # # ]: 0 : if (r->internal->vlan_strip)
448 : 0 : rte_vlan_strip(bufs[i]);
449 : :
450 [ # # ]: 0 : if (r->internal->rx_sw_csum)
451 : 0 : vhost_dev_rx_sw_csum(bufs[i]);
452 : :
453 : 0 : r->stats.bytes += bufs[i]->pkt_len;
454 : : }
455 : :
456 : 0 : out:
457 : 0 : rte_atomic_store_explicit(&r->while_queuing, 0, rte_memory_order_release);
458 : :
459 : 0 : return nb_rx;
460 : : }
461 : :
462 : : static uint16_t
463 : 0 : eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
464 : : {
465 : : struct vhost_queue *r = q;
466 : : uint16_t i, nb_tx = 0;
467 : : uint16_t nb_send = 0;
468 : : uint64_t nb_bytes = 0;
469 : : uint64_t nb_missed = 0;
470 : :
471 : : /* Fast-path early exit; racy load is fine here -- if we miss a
472 : : * transition we get caught by the seq_cst check below.
473 : : */
474 [ # # ]: 0 : if (unlikely(rte_atomic_load_explicit(&r->allow_queuing, rte_memory_order_relaxed) == 0))
475 : : return 0;
476 : :
477 : : /* Announce presence, then re-check. The store and the following
478 : : * load MUST both be seq_cst so they are totally ordered with the
479 : : * control thread's store-to-allow_queuing / load-of-while_queuing
480 : : * pair. Anything weaker permits both sides to miss each other.
481 : : */
482 : 0 : rte_atomic_store_explicit(&r->while_queuing, 1, rte_memory_order_seq_cst);
483 [ # # ]: 0 : if (unlikely(rte_atomic_load_explicit(&r->allow_queuing, rte_memory_order_seq_cst) == 0))
484 : 0 : goto out;
485 : :
486 [ # # ]: 0 : for (i = 0; i < nb_bufs; i++) {
487 : 0 : struct rte_mbuf *m = bufs[i];
488 : :
489 : : /* Do VLAN tag insertion */
490 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_VLAN) {
491 : : int error = rte_vlan_insert(&m);
492 : :
493 [ # # ]: 0 : if (unlikely(error))
494 : : break;
495 : : }
496 : :
497 [ # # ]: 0 : if (r->internal->tx_sw_csum)
498 : 0 : vhost_dev_tx_sw_csum(m);
499 : :
500 : 0 : bufs[i] = m;
501 : : }
502 : : nb_send = i;
503 : :
504 : : /* Enqueue packets to guest RX queue */
505 [ # # ]: 0 : while (nb_send) {
506 : : uint16_t nb_pkts;
507 : 0 : uint16_t num = (uint16_t)RTE_MIN(nb_send,
508 : : VHOST_MAX_PKT_BURST);
509 : :
510 : 0 : nb_pkts = rte_vhost_enqueue_burst(r->vid, r->virtqueue_id,
511 : 0 : &bufs[nb_tx], num);
512 : :
513 : 0 : nb_tx += nb_pkts;
514 : 0 : nb_send -= nb_pkts;
515 [ # # ]: 0 : if (nb_pkts < num)
516 : : break;
517 : : }
518 : :
519 [ # # ]: 0 : for (i = 0; likely(i < nb_tx); i++)
520 : 0 : nb_bytes += bufs[i]->pkt_len;
521 : :
522 : 0 : nb_missed = nb_send;
523 : :
524 : 0 : r->stats.pkts += nb_tx;
525 : 0 : r->stats.bytes += nb_bytes;
526 : 0 : r->stats.missed_pkts += nb_missed;
527 : :
528 [ # # ]: 0 : for (i = 0; likely(i < nb_tx); i++)
529 : 0 : rte_pktmbuf_free(bufs[i]);
530 : 0 : out:
531 : 0 : rte_atomic_store_explicit(&r->while_queuing, 0, rte_memory_order_release);
532 : :
533 : 0 : return nb_tx;
534 : : }
535 : :
536 : : static inline struct internal_list *
537 : 0 : find_internal_resource(char *ifname)
538 : : {
539 : : int found = 0;
540 : : struct internal_list *list;
541 : : struct pmd_internal *internal;
542 : :
543 [ # # ]: 0 : if (ifname == NULL)
544 : : return NULL;
545 : :
546 : 0 : pthread_mutex_lock(&internal_list_lock);
547 : :
548 [ # # ]: 0 : TAILQ_FOREACH(list, &internal_list, next) {
549 : 0 : internal = list->eth_dev->data->dev_private;
550 [ # # ]: 0 : if (!strcmp(internal->iface_name, ifname)) {
551 : : found = 1;
552 : : break;
553 : : }
554 : : }
555 : :
556 : 0 : pthread_mutex_unlock(&internal_list_lock);
557 : :
558 [ # # ]: 0 : if (!found)
559 : 0 : return NULL;
560 : :
561 : : return list;
562 : : }
563 : :
564 : : static void
565 : 0 : eth_vhost_update_intr(struct rte_eth_dev *eth_dev, uint16_t rxq_idx)
566 : : {
567 : : struct rte_vhost_vring vring;
568 : : struct vhost_queue *vq;
569 : :
570 : 0 : vq = eth_dev->data->rx_queues[rxq_idx];
571 [ # # # # ]: 0 : if (vq == NULL || vq->vid < 0)
572 : 0 : return;
573 : :
574 [ # # ]: 0 : if (rte_vhost_get_vhost_vring(vq->vid, (rxq_idx << 1) + 1, &vring) < 0) {
575 : 0 : VHOST_LOG_LINE(DEBUG, "Failed to get rxq-%d's vring, skip!", rxq_idx);
576 : 0 : return;
577 : : }
578 : :
579 : 0 : rte_spinlock_lock(&vq->intr_lock);
580 : :
581 : : /* Remove previous kickfd from proxy epoll */
582 [ # # # # ]: 0 : if (vq->kickfd >= 0 && vq->kickfd != vring.kickfd) {
583 [ # # ]: 0 : if (epoll_ctl(vq->ev.data.fd, EPOLL_CTL_DEL, vq->kickfd, &vq->ev) < 0) {
584 : 0 : VHOST_LOG_LINE(DEBUG, "Failed to unregister %d from rxq-%d epoll: %s",
585 : : vq->kickfd, rxq_idx, strerror(errno));
586 : : } else {
587 : 0 : VHOST_LOG_LINE(DEBUG, "Unregistered %d from rxq-%d epoll",
588 : : vq->kickfd, rxq_idx);
589 : : }
590 : 0 : vq->kickfd = -1;
591 : : }
592 : :
593 : : /* Add new one, if valid */
594 [ # # # # ]: 0 : if (vq->kickfd != vring.kickfd && vring.kickfd >= 0) {
595 [ # # ]: 0 : if (epoll_ctl(vq->ev.data.fd, EPOLL_CTL_ADD, vring.kickfd, &vq->ev) < 0) {
596 : 0 : VHOST_LOG_LINE(ERR, "Failed to register %d in rxq-%d epoll: %s",
597 : : vring.kickfd, rxq_idx, strerror(errno));
598 : : } else {
599 : 0 : vq->kickfd = vring.kickfd;
600 : 0 : VHOST_LOG_LINE(DEBUG, "Registered %d in rxq-%d epoll",
601 : : vq->kickfd, rxq_idx);
602 : : }
603 : : }
604 : :
605 : : rte_spinlock_unlock(&vq->intr_lock);
606 : : }
607 : :
608 : : static int
609 : 0 : eth_rxq_intr_enable(struct rte_eth_dev *dev, uint16_t qid)
610 : : {
611 : 0 : struct vhost_queue *vq = dev->data->rx_queues[qid];
612 : :
613 [ # # ]: 0 : if (vq->vid >= 0)
614 : 0 : rte_vhost_enable_guest_notification(vq->vid, (qid << 1) + 1, 1);
615 : :
616 : 0 : return 0;
617 : : }
618 : :
619 : : static int
620 : 0 : eth_rxq_intr_disable(struct rte_eth_dev *dev, uint16_t qid)
621 : : {
622 : 0 : struct vhost_queue *vq = dev->data->rx_queues[qid];
623 : :
624 [ # # ]: 0 : if (vq->vid >= 0)
625 : 0 : rte_vhost_enable_guest_notification(vq->vid, (qid << 1) + 1, 0);
626 : :
627 : 0 : return 0;
628 : : }
629 : :
630 : : static void
631 : 0 : eth_vhost_uninstall_intr(struct rte_eth_dev *dev)
632 : : {
633 : 0 : struct rte_intr_handle *intr_handle = dev->intr_handle;
634 : :
635 [ # # ]: 0 : if (intr_handle != NULL) {
636 : : int i;
637 : :
638 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
639 : 0 : int epoll_fd = rte_intr_efds_index_get(dev->intr_handle, i);
640 : :
641 [ # # ]: 0 : if (epoll_fd >= 0)
642 : 0 : close(epoll_fd);
643 : : }
644 : 0 : rte_intr_vec_list_free(intr_handle);
645 : 0 : rte_intr_instance_free(intr_handle);
646 : : }
647 : 0 : dev->intr_handle = NULL;
648 : 0 : }
649 : :
650 : : static int
651 : 0 : eth_vhost_install_intr(struct rte_eth_dev *dev)
652 : : {
653 : 0 : int nb_rxq = dev->data->nb_rx_queues;
654 : : struct vhost_queue *vq;
655 : :
656 : : int ret;
657 : : int i;
658 : :
659 : 0 : dev->intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
660 [ # # ]: 0 : if (dev->intr_handle == NULL) {
661 : 0 : VHOST_LOG_LINE(ERR, "Fail to allocate intr_handle");
662 : : ret = -ENOMEM;
663 : 0 : goto error;
664 : : }
665 [ # # ]: 0 : if (rte_intr_efd_counter_size_set(dev->intr_handle, 0)) {
666 : 0 : ret = -rte_errno;
667 : 0 : goto error;
668 : : }
669 : :
670 [ # # ]: 0 : if (rte_intr_vec_list_alloc(dev->intr_handle, NULL, nb_rxq)) {
671 : 0 : VHOST_LOG_LINE(ERR, "Failed to allocate memory for interrupt vector");
672 : : ret = -ENOMEM;
673 : 0 : goto error;
674 : : }
675 : :
676 : 0 : VHOST_LOG_LINE(DEBUG, "Prepare intr vec");
677 [ # # ]: 0 : for (i = 0; i < nb_rxq; i++) {
678 : 0 : int epoll_fd = epoll_create1(0);
679 : :
680 [ # # ]: 0 : if (epoll_fd < 0) {
681 : 0 : VHOST_LOG_LINE(ERR, "Failed to create proxy epoll fd for rxq-%d", i);
682 : 0 : ret = -errno;
683 : 0 : goto error;
684 : : }
685 : :
686 [ # # ]: 0 : if (rte_intr_vec_list_index_set(dev->intr_handle, i,
687 [ # # ]: 0 : RTE_INTR_VEC_RXTX_OFFSET + i) ||
688 : 0 : rte_intr_efds_index_set(dev->intr_handle, i, epoll_fd)) {
689 : 0 : ret = -rte_errno;
690 : 0 : close(epoll_fd);
691 : 0 : goto error;
692 : : }
693 : :
694 : 0 : vq = dev->data->rx_queues[i];
695 : 0 : memset(&vq->ev, 0, sizeof(vq->ev));
696 : 0 : vq->ev.events = EPOLLIN;
697 : 0 : vq->ev.data.fd = epoll_fd;
698 : : }
699 : :
700 [ # # ]: 0 : if (rte_intr_nb_efd_set(dev->intr_handle, nb_rxq)) {
701 : 0 : ret = -rte_errno;
702 : 0 : goto error;
703 : : }
704 [ # # ]: 0 : if (rte_intr_max_intr_set(dev->intr_handle, nb_rxq + 1)) {
705 : 0 : ret = -rte_errno;
706 : 0 : goto error;
707 : : }
708 [ # # ]: 0 : if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_VDEV)) {
709 : 0 : ret = -rte_errno;
710 : 0 : goto error;
711 : : }
712 : :
713 : : return 0;
714 : :
715 : 0 : error:
716 : 0 : eth_vhost_uninstall_intr(dev);
717 : 0 : return ret;
718 : : }
719 : :
720 : : static void
721 : 0 : eth_vhost_configure_intr(struct rte_eth_dev *dev)
722 : : {
723 : : int i;
724 : :
725 : 0 : VHOST_LOG_LINE(DEBUG, "Configure intr vec");
726 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
727 : 0 : eth_vhost_update_intr(dev, i);
728 : 0 : }
729 : :
730 : : static void
731 : 0 : eth_vhost_unconfigure_intr(struct rte_eth_dev *eth_dev)
732 : : {
733 : : struct vhost_queue *vq;
734 : : int i;
735 : :
736 : 0 : VHOST_LOG_LINE(DEBUG, "Unconfigure intr vec");
737 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
738 : 0 : vq = eth_dev->data->rx_queues[i];
739 [ # # # # ]: 0 : if (vq == NULL || vq->vid < 0)
740 : 0 : continue;
741 : :
742 : 0 : rte_spinlock_lock(&vq->intr_lock);
743 : :
744 : : /* Remove previous kickfd from proxy epoll */
745 [ # # ]: 0 : if (vq->kickfd >= 0) {
746 [ # # ]: 0 : if (epoll_ctl(vq->ev.data.fd, EPOLL_CTL_DEL, vq->kickfd, &vq->ev) < 0) {
747 : 0 : VHOST_LOG_LINE(DEBUG, "Failed to unregister %d from rxq-%d epoll: %s",
748 : : vq->kickfd, i, strerror(errno));
749 : : } else {
750 : 0 : VHOST_LOG_LINE(DEBUG, "Unregistered %d from rxq-%d epoll",
751 : : vq->kickfd, i);
752 : : }
753 : 0 : vq->kickfd = -1;
754 : : }
755 : :
756 : : rte_spinlock_unlock(&vq->intr_lock);
757 : : }
758 : 0 : }
759 : :
760 : : static void
761 : 0 : update_queuing_status(struct rte_eth_dev *dev, bool wait_queuing)
762 : : {
763 : 0 : struct pmd_internal *internal = dev->data->dev_private;
764 : : struct vhost_queue *vq;
765 : : struct rte_vhost_vring_state *state;
766 : : unsigned int i;
767 : : int allow_queuing = 1;
768 : :
769 [ # # # # ]: 0 : if (!dev->data->rx_queues || !dev->data->tx_queues)
770 : : return;
771 : :
772 : : /* These are control-plane flags consulted only here;
773 : : * the real data-path handshake is on vq->allow_queuing below.
774 : : * Relaxed is sufficient.
775 : : */
776 [ # # ]: 0 : if (rte_atomic_load_explicit(&internal->started, rte_memory_order_relaxed) == 0 ||
777 [ # # ]: 0 : rte_atomic_load_explicit(&internal->dev_attached, rte_memory_order_relaxed) == 0)
778 : : allow_queuing = 0;
779 : :
780 : 0 : state = vring_states[dev->data->port_id];
781 : :
782 : : /* Wait until rx/tx_pkt_burst stops accessing vhost device */
783 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
784 : 0 : vq = dev->data->rx_queues[i];
785 [ # # ]: 0 : if (vq == NULL)
786 : 0 : continue;
787 : :
788 [ # # # # ]: 0 : rte_atomic_store_explicit(&vq->allow_queuing,
789 : : (allow_queuing && state->cur[vq->virtqueue_id]),
790 : : rte_memory_order_seq_cst);
791 : :
792 [ # # ]: 0 : while (wait_queuing &&
793 [ # # ]: 0 : rte_atomic_load_explicit(&vq->while_queuing, rte_memory_order_seq_cst))
794 : : rte_pause();
795 : : }
796 : :
797 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
798 : 0 : vq = dev->data->tx_queues[i];
799 [ # # ]: 0 : if (vq == NULL)
800 : 0 : continue;
801 : :
802 [ # # # # ]: 0 : rte_atomic_store_explicit(&vq->allow_queuing,
803 : : (allow_queuing && state->cur[vq->virtqueue_id]),
804 : : rte_memory_order_seq_cst);
805 : :
806 [ # # ]: 0 : while (wait_queuing &&
807 [ # # ]: 0 : rte_atomic_load_explicit(&vq->while_queuing, rte_memory_order_seq_cst))
808 : : rte_pause();
809 : : }
810 : : }
811 : :
812 : : static void
813 : 0 : queue_setup(struct rte_eth_dev *eth_dev, struct pmd_internal *internal)
814 : : {
815 : : struct vhost_queue *vq;
816 : : int i;
817 : :
818 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
819 : 0 : vq = eth_dev->data->rx_queues[i];
820 [ # # ]: 0 : if (!vq)
821 : 0 : continue;
822 : 0 : vq->vid = internal->vid;
823 : 0 : vq->internal = internal;
824 : 0 : vq->port = eth_dev->data->port_id;
825 : : }
826 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
827 : 0 : vq = eth_dev->data->tx_queues[i];
828 [ # # ]: 0 : if (!vq)
829 : 0 : continue;
830 : 0 : vq->vid = internal->vid;
831 : 0 : vq->internal = internal;
832 : 0 : vq->port = eth_dev->data->port_id;
833 : : }
834 : 0 : }
835 : :
836 : : static int
837 : 0 : new_device(int vid)
838 : : {
839 : : struct rte_eth_dev *eth_dev;
840 : : struct internal_list *list;
841 : : struct pmd_internal *internal;
842 : : struct rte_eth_conf *dev_conf;
843 : : unsigned i;
844 : : char ifname[PATH_MAX];
845 : : #ifdef RTE_LIBRTE_VHOST_NUMA
846 : : int newnode;
847 : : #endif
848 : :
849 : 0 : rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
850 : 0 : list = find_internal_resource(ifname);
851 [ # # ]: 0 : if (list == NULL) {
852 : 0 : VHOST_LOG_LINE(INFO, "Invalid device name: %s", ifname);
853 : 0 : return -1;
854 : : }
855 : :
856 : 0 : eth_dev = list->eth_dev;
857 : 0 : internal = eth_dev->data->dev_private;
858 : : dev_conf = ð_dev->data->dev_conf;
859 : :
860 : : #ifdef RTE_LIBRTE_VHOST_NUMA
861 : 0 : newnode = rte_vhost_get_numa_node(vid);
862 [ # # ]: 0 : if (newnode >= 0)
863 : 0 : eth_dev->data->numa_node = newnode;
864 : : #endif
865 : :
866 [ # # ]: 0 : if (rte_vhost_get_negotiated_features(vid, &internal->features)) {
867 : 0 : VHOST_LOG_LINE(ERR, "Failed to get device features");
868 : 0 : return -1;
869 : : }
870 : :
871 : 0 : internal->vid = vid;
872 [ # # ]: 0 : if (rte_atomic_load_explicit(&internal->started, rte_memory_order_relaxed) == 1) {
873 : 0 : queue_setup(eth_dev, internal);
874 [ # # ]: 0 : if (dev_conf->intr_conf.rxq)
875 : 0 : eth_vhost_configure_intr(eth_dev);
876 : : }
877 : :
878 [ # # ]: 0 : for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
879 : 0 : rte_vhost_enable_guest_notification(vid, i, 0);
880 : :
881 : 0 : rte_vhost_get_mtu(vid, ð_dev->data->mtu);
882 : :
883 : 0 : eth_dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
884 : :
885 : 0 : vhost_dev_csum_configure(eth_dev);
886 : :
887 : 0 : rte_atomic_store_explicit(&internal->dev_attached, 1, rte_memory_order_relaxed);
888 : 0 : update_queuing_status(eth_dev, false);
889 : :
890 : 0 : VHOST_LOG_LINE(INFO, "Vhost device %d created", vid);
891 : :
892 : 0 : rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
893 : :
894 : 0 : return 0;
895 : : }
896 : :
897 : : static void
898 : 0 : destroy_device(int vid)
899 : : {
900 : : struct rte_eth_dev *eth_dev;
901 : : struct pmd_internal *internal;
902 : : struct vhost_queue *vq;
903 : : struct internal_list *list;
904 : : char ifname[PATH_MAX];
905 : : unsigned i;
906 : : struct rte_vhost_vring_state *state;
907 : :
908 : 0 : rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
909 : 0 : list = find_internal_resource(ifname);
910 [ # # ]: 0 : if (list == NULL) {
911 : 0 : VHOST_LOG_LINE(ERR, "Invalid interface name: %s", ifname);
912 : 0 : return;
913 : : }
914 : 0 : eth_dev = list->eth_dev;
915 : 0 : internal = eth_dev->data->dev_private;
916 : :
917 : 0 : rte_atomic_store_explicit(&internal->dev_attached, 0, rte_memory_order_relaxed);
918 : 0 : update_queuing_status(eth_dev, true);
919 : 0 : eth_vhost_unconfigure_intr(eth_dev);
920 : :
921 : 0 : eth_dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
922 : :
923 [ # # # # ]: 0 : if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
924 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
925 : 0 : vq = eth_dev->data->rx_queues[i];
926 [ # # ]: 0 : if (!vq)
927 : 0 : continue;
928 : 0 : vq->vid = -1;
929 : : }
930 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
931 : 0 : vq = eth_dev->data->tx_queues[i];
932 [ # # ]: 0 : if (!vq)
933 : 0 : continue;
934 : 0 : vq->vid = -1;
935 : : }
936 : : }
937 : :
938 : 0 : state = vring_states[eth_dev->data->port_id];
939 : 0 : rte_spinlock_lock(&state->lock);
940 [ # # ]: 0 : for (i = 0; i <= state->max_vring; i++) {
941 : 0 : state->cur[i] = false;
942 : 0 : state->seen[i] = false;
943 : : }
944 : 0 : state->max_vring = 0;
945 : : rte_spinlock_unlock(&state->lock);
946 : :
947 : 0 : VHOST_LOG_LINE(INFO, "Vhost device %d destroyed", vid);
948 : :
949 : 0 : rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
950 : : }
951 : :
952 : : static int
953 : 0 : vring_state_changed(int vid, uint16_t vring, int enable)
954 : : {
955 : : struct rte_vhost_vring_state *state;
956 : : struct rte_eth_dev *eth_dev;
957 : : struct internal_list *list;
958 : : char ifname[PATH_MAX];
959 : :
960 : 0 : rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
961 : 0 : list = find_internal_resource(ifname);
962 [ # # ]: 0 : if (list == NULL) {
963 : 0 : VHOST_LOG_LINE(ERR, "Invalid interface name: %s", ifname);
964 : 0 : return -1;
965 : : }
966 : :
967 : 0 : eth_dev = list->eth_dev;
968 : : /* won't be NULL */
969 : 0 : state = vring_states[eth_dev->data->port_id];
970 : :
971 [ # # # # ]: 0 : if (eth_dev->data->dev_conf.intr_conf.rxq && vring % 2)
972 : 0 : eth_vhost_update_intr(eth_dev, (vring - 1) >> 1);
973 : :
974 : 0 : rte_spinlock_lock(&state->lock);
975 [ # # ]: 0 : if (state->cur[vring] == enable) {
976 : : rte_spinlock_unlock(&state->lock);
977 : 0 : return 0;
978 : : }
979 : 0 : state->cur[vring] = enable;
980 : 0 : state->max_vring = RTE_MAX(vring, state->max_vring);
981 : : rte_spinlock_unlock(&state->lock);
982 : :
983 : 0 : update_queuing_status(eth_dev, false);
984 : :
985 [ # # ]: 0 : VHOST_LOG_LINE(INFO, "vring%u is %s",
986 : : vring, enable ? "enabled" : "disabled");
987 : :
988 : 0 : rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
989 : :
990 : 0 : return 0;
991 : : }
992 : :
993 : : static struct rte_vhost_device_ops vhost_ops = {
994 : : .new_device = new_device,
995 : : .destroy_device = destroy_device,
996 : : .vring_state_changed = vring_state_changed,
997 : : };
998 : :
999 : : static int
1000 : 0 : vhost_driver_setup(struct rte_eth_dev *eth_dev)
1001 : : {
1002 : 0 : struct pmd_internal *internal = eth_dev->data->dev_private;
1003 : : struct internal_list *list = NULL;
1004 : : struct rte_vhost_vring_state *vring_state = NULL;
1005 : 0 : unsigned int numa_node = eth_dev->device->numa_node;
1006 : 0 : const char *name = eth_dev->device->name;
1007 : :
1008 : : /* Don't try to setup again if it has already been done. */
1009 : 0 : list = find_internal_resource(internal->iface_name);
1010 [ # # ]: 0 : if (list)
1011 : : return 0;
1012 : :
1013 : 0 : list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1014 [ # # ]: 0 : if (list == NULL)
1015 : : return -1;
1016 : :
1017 : 0 : vring_state = rte_zmalloc_socket(name, sizeof(*vring_state),
1018 : : 0, numa_node);
1019 [ # # ]: 0 : if (vring_state == NULL)
1020 : 0 : goto free_list;
1021 : :
1022 : 0 : list->eth_dev = eth_dev;
1023 : 0 : pthread_mutex_lock(&internal_list_lock);
1024 : 0 : TAILQ_INSERT_TAIL(&internal_list, list, next);
1025 : 0 : pthread_mutex_unlock(&internal_list_lock);
1026 : :
1027 : : rte_spinlock_init(&vring_state->lock);
1028 : 0 : vring_states[eth_dev->data->port_id] = vring_state;
1029 : :
1030 [ # # ]: 0 : if (rte_vhost_driver_register(internal->iface_name, internal->flags))
1031 : 0 : goto list_remove;
1032 : :
1033 [ # # ]: 0 : if (internal->disable_flags) {
1034 [ # # ]: 0 : if (rte_vhost_driver_disable_features(internal->iface_name,
1035 : : internal->disable_flags))
1036 : 0 : goto drv_unreg;
1037 : : }
1038 : :
1039 [ # # ]: 0 : if (rte_vhost_driver_set_max_queue_num(internal->iface_name, internal->max_queues))
1040 : 0 : goto drv_unreg;
1041 : :
1042 [ # # ]: 0 : if (rte_vhost_driver_callback_register(internal->iface_name,
1043 : : &vhost_ops) < 0) {
1044 : 0 : VHOST_LOG_LINE(ERR, "Can't register callbacks");
1045 : 0 : goto drv_unreg;
1046 : : }
1047 : :
1048 [ # # ]: 0 : if (rte_vhost_driver_start(internal->iface_name) < 0) {
1049 : 0 : VHOST_LOG_LINE(ERR, "Failed to start driver for %s",
1050 : : internal->iface_name);
1051 : 0 : goto drv_unreg;
1052 : : }
1053 : :
1054 : : return 0;
1055 : :
1056 : 0 : drv_unreg:
1057 : 0 : rte_vhost_driver_unregister(internal->iface_name);
1058 : 0 : list_remove:
1059 : 0 : vring_states[eth_dev->data->port_id] = NULL;
1060 : 0 : pthread_mutex_lock(&internal_list_lock);
1061 [ # # ]: 0 : TAILQ_REMOVE(&internal_list, list, next);
1062 : 0 : pthread_mutex_unlock(&internal_list_lock);
1063 : 0 : rte_free(vring_state);
1064 : 0 : free_list:
1065 : 0 : rte_free(list);
1066 : :
1067 : 0 : return -1;
1068 : : }
1069 : :
1070 : : RTE_EXPORT_SYMBOL(rte_eth_vhost_get_queue_event)
1071 : : int
1072 : 0 : rte_eth_vhost_get_queue_event(uint16_t port_id,
1073 : : struct rte_eth_vhost_queue_event *event)
1074 : : {
1075 : : struct rte_vhost_vring_state *state;
1076 : : unsigned int i;
1077 : : int idx;
1078 : :
1079 [ # # ]: 0 : if (port_id >= RTE_MAX_ETHPORTS) {
1080 : 0 : VHOST_LOG_LINE(ERR, "Invalid port id");
1081 : 0 : return -1;
1082 : : }
1083 : :
1084 : 0 : state = vring_states[port_id];
1085 [ # # ]: 0 : if (!state) {
1086 : 0 : VHOST_LOG_LINE(ERR, "Unused port");
1087 : 0 : return -1;
1088 : : }
1089 : :
1090 : 0 : rte_spinlock_lock(&state->lock);
1091 [ # # ]: 0 : for (i = 0; i <= state->max_vring; i++) {
1092 : 0 : idx = state->index++ % (state->max_vring + 1);
1093 : :
1094 [ # # ]: 0 : if (state->cur[idx] != state->seen[idx]) {
1095 : 0 : state->seen[idx] = state->cur[idx];
1096 : 0 : event->queue_id = idx / 2;
1097 : 0 : event->rx = idx & 1;
1098 : 0 : event->enable = state->cur[idx];
1099 : : rte_spinlock_unlock(&state->lock);
1100 : 0 : return 0;
1101 : : }
1102 : : }
1103 : : rte_spinlock_unlock(&state->lock);
1104 : :
1105 : 0 : return -1;
1106 : : }
1107 : :
1108 : : RTE_EXPORT_SYMBOL(rte_eth_vhost_get_vid_from_port_id)
1109 : : int
1110 : 0 : rte_eth_vhost_get_vid_from_port_id(uint16_t port_id)
1111 : : {
1112 : : struct internal_list *list;
1113 : : struct rte_eth_dev *eth_dev;
1114 : : struct vhost_queue *vq;
1115 : : int vid = -1;
1116 : :
1117 [ # # ]: 0 : if (!rte_eth_dev_is_valid_port(port_id))
1118 : : return -1;
1119 : :
1120 : 0 : pthread_mutex_lock(&internal_list_lock);
1121 : :
1122 [ # # ]: 0 : TAILQ_FOREACH(list, &internal_list, next) {
1123 : 0 : eth_dev = list->eth_dev;
1124 [ # # ]: 0 : if (eth_dev->data->port_id == port_id) {
1125 : 0 : vq = eth_dev->data->rx_queues[0];
1126 [ # # ]: 0 : if (vq) {
1127 : 0 : vid = vq->vid;
1128 : : }
1129 : : break;
1130 : : }
1131 : : }
1132 : :
1133 : 0 : pthread_mutex_unlock(&internal_list_lock);
1134 : :
1135 : 0 : return vid;
1136 : : }
1137 : :
1138 : : static int
1139 : 0 : eth_dev_configure(struct rte_eth_dev *dev)
1140 : : {
1141 : 0 : struct pmd_internal *internal = dev->data->dev_private;
1142 : : const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1143 : :
1144 : : /* NOTE: the same process has to operate a vhost interface
1145 : : * from beginning to end (from eth_dev configure to eth_dev close).
1146 : : * It is user's responsibility at the moment.
1147 : : */
1148 [ # # ]: 0 : if (vhost_driver_setup(dev) < 0)
1149 : : return -1;
1150 : :
1151 : 0 : internal->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
1152 : :
1153 : 0 : vhost_dev_csum_configure(dev);
1154 : :
1155 : 0 : return 0;
1156 : : }
1157 : :
1158 : : static int
1159 : 0 : eth_dev_start(struct rte_eth_dev *eth_dev)
1160 : : {
1161 : 0 : struct pmd_internal *internal = eth_dev->data->dev_private;
1162 : : struct rte_eth_conf *dev_conf = ð_dev->data->dev_conf;
1163 : : uint16_t i;
1164 : :
1165 : 0 : eth_vhost_uninstall_intr(eth_dev);
1166 [ # # # # ]: 0 : if (dev_conf->intr_conf.rxq && eth_vhost_install_intr(eth_dev) < 0) {
1167 : 0 : VHOST_LOG_LINE(ERR, "Failed to install interrupt handler.");
1168 : 0 : return -1;
1169 : : }
1170 : :
1171 : 0 : queue_setup(eth_dev, internal);
1172 [ # # # # ]: 0 : if (rte_atomic_load_explicit(&internal->dev_attached, rte_memory_order_relaxed) == 1 &&
1173 : : dev_conf->intr_conf.rxq)
1174 : 0 : eth_vhost_configure_intr(eth_dev);
1175 : :
1176 : 0 : rte_atomic_store_explicit(&internal->started, 1, rte_memory_order_relaxed);
1177 : 0 : update_queuing_status(eth_dev, false);
1178 : :
1179 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1180 : 0 : eth_dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
1181 [ # # ]: 0 : for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1182 : 0 : eth_dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
1183 : :
1184 : : return 0;
1185 : : }
1186 : :
1187 : : static int
1188 : 0 : eth_dev_stop(struct rte_eth_dev *dev)
1189 : : {
1190 : 0 : struct pmd_internal *internal = dev->data->dev_private;
1191 : : uint16_t i;
1192 : :
1193 : 0 : dev->data->dev_started = 0;
1194 : 0 : rte_atomic_store_explicit(&internal->started, 0, rte_memory_order_relaxed);
1195 : 0 : update_queuing_status(dev, true);
1196 : :
1197 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
1198 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1199 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
1200 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1201 : :
1202 : 0 : return 0;
1203 : : }
1204 : :
1205 : : static int
1206 : 0 : eth_dev_close(struct rte_eth_dev *dev)
1207 : : {
1208 : : struct pmd_internal *internal;
1209 : : struct internal_list *list;
1210 : : unsigned int i, ret;
1211 : :
1212 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1213 : : return 0;
1214 : :
1215 : 0 : internal = dev->data->dev_private;
1216 [ # # ]: 0 : if (!internal)
1217 : : return 0;
1218 : :
1219 : 0 : ret = eth_dev_stop(dev);
1220 : :
1221 : 0 : list = find_internal_resource(internal->iface_name);
1222 [ # # ]: 0 : if (list) {
1223 : 0 : rte_vhost_driver_unregister(internal->iface_name);
1224 : 0 : pthread_mutex_lock(&internal_list_lock);
1225 [ # # ]: 0 : TAILQ_REMOVE(&internal_list, list, next);
1226 : 0 : pthread_mutex_unlock(&internal_list_lock);
1227 : 0 : rte_free(list);
1228 : : }
1229 : :
1230 [ # # ]: 0 : if (dev->data->rx_queues)
1231 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
1232 : 0 : rte_free(dev->data->rx_queues[i]);
1233 : :
1234 [ # # ]: 0 : if (dev->data->tx_queues)
1235 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
1236 : 0 : rte_free(dev->data->tx_queues[i]);
1237 : :
1238 : 0 : rte_free(internal->iface_name);
1239 : 0 : rte_free(internal);
1240 : :
1241 : 0 : eth_vhost_uninstall_intr(dev);
1242 : :
1243 : 0 : dev->data->dev_private = NULL;
1244 : :
1245 : 0 : rte_free(vring_states[dev->data->port_id]);
1246 : 0 : vring_states[dev->data->port_id] = NULL;
1247 : :
1248 : 0 : return ret;
1249 : : }
1250 : :
1251 : : static int
1252 : 0 : eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1253 : : uint16_t nb_rx_desc __rte_unused,
1254 : : unsigned int socket_id,
1255 : : const struct rte_eth_rxconf *rx_conf __rte_unused,
1256 : : struct rte_mempool *mb_pool)
1257 : : {
1258 : : struct vhost_queue *vq;
1259 : :
1260 : 0 : vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
1261 : : RTE_CACHE_LINE_SIZE, socket_id);
1262 [ # # ]: 0 : if (vq == NULL) {
1263 : 0 : VHOST_LOG_LINE(ERR, "Failed to allocate memory for rx queue");
1264 : 0 : return -ENOMEM;
1265 : : }
1266 : :
1267 : 0 : vq->mb_pool = mb_pool;
1268 : 0 : vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
1269 : : rte_spinlock_init(&vq->intr_lock);
1270 : 0 : vq->kickfd = -1;
1271 : 0 : dev->data->rx_queues[rx_queue_id] = vq;
1272 : :
1273 : 0 : return 0;
1274 : : }
1275 : :
1276 : : static int
1277 : 0 : eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1278 : : uint16_t nb_tx_desc __rte_unused,
1279 : : unsigned int socket_id,
1280 : : const struct rte_eth_txconf *tx_conf __rte_unused)
1281 : : {
1282 : : struct vhost_queue *vq;
1283 : :
1284 : 0 : vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
1285 : : RTE_CACHE_LINE_SIZE, socket_id);
1286 [ # # ]: 0 : if (vq == NULL) {
1287 : 0 : VHOST_LOG_LINE(ERR, "Failed to allocate memory for tx queue");
1288 : 0 : return -ENOMEM;
1289 : : }
1290 : :
1291 : 0 : vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
1292 : : rte_spinlock_init(&vq->intr_lock);
1293 : 0 : vq->kickfd = -1;
1294 : 0 : dev->data->tx_queues[tx_queue_id] = vq;
1295 : :
1296 : 0 : return 0;
1297 : : }
1298 : :
1299 : : static int
1300 : 0 : eth_dev_info(struct rte_eth_dev *dev,
1301 : : struct rte_eth_dev_info *dev_info)
1302 : : {
1303 : : struct pmd_internal *internal;
1304 : :
1305 : 0 : internal = dev->data->dev_private;
1306 [ # # ]: 0 : if (internal == NULL) {
1307 : 0 : VHOST_LOG_LINE(ERR, "Invalid device specified");
1308 : 0 : return -ENODEV;
1309 : : }
1310 : :
1311 : 0 : dev_info->max_mac_addrs = 1;
1312 : 0 : dev_info->max_rx_pktlen = (uint32_t)-1;
1313 : 0 : dev_info->max_rx_queues = internal->max_queues;
1314 : 0 : dev_info->max_tx_queues = internal->max_queues;
1315 : 0 : dev_info->min_rx_bufsize = 0;
1316 : :
1317 : 0 : dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
1318 : : RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
1319 [ # # ]: 0 : if (internal->flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS) {
1320 : 0 : dev_info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
1321 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM;
1322 : : }
1323 : :
1324 : 0 : dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
1325 [ # # ]: 0 : if (internal->flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS) {
1326 : 0 : dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
1327 : : RTE_ETH_RX_OFFLOAD_TCP_CKSUM;
1328 : : }
1329 : :
1330 : : return 0;
1331 : : }
1332 : :
1333 : : static int
1334 : 0 : eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats,
1335 : : struct eth_queue_stats *qstats)
1336 : : {
1337 : : unsigned i;
1338 : : unsigned long rx_total = 0, tx_total = 0;
1339 : : unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
1340 : : unsigned long tx_total_errors = 0;
1341 : : struct vhost_queue *vq;
1342 : :
1343 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1344 [ # # ]: 0 : if (dev->data->rx_queues[i] == NULL)
1345 : 0 : continue;
1346 : : vq = dev->data->rx_queues[i];
1347 [ # # ]: 0 : if (qstats != NULL) {
1348 : 0 : qstats[i].q_ipackets = vq->stats.pkts;
1349 : 0 : qstats[i].q_ibytes = vq->stats.bytes;
1350 : : }
1351 : 0 : rx_total += vq->stats.pkts;
1352 : 0 : rx_total_bytes += vq->stats.bytes;
1353 : : }
1354 : :
1355 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1356 [ # # ]: 0 : if (dev->data->tx_queues[i] == NULL)
1357 : 0 : continue;
1358 : : vq = dev->data->tx_queues[i];
1359 [ # # ]: 0 : if (qstats != NULL) {
1360 : 0 : qstats[i].q_opackets = vq->stats.pkts;
1361 : 0 : qstats[i].q_obytes = vq->stats.bytes;
1362 : : }
1363 : 0 : tx_total += vq->stats.pkts;
1364 : 0 : tx_total_bytes += vq->stats.bytes;
1365 : 0 : tx_total_errors += vq->stats.missed_pkts;
1366 : : }
1367 : :
1368 : 0 : stats->ipackets = rx_total;
1369 : 0 : stats->opackets = tx_total;
1370 : 0 : stats->ibytes = rx_total_bytes;
1371 : 0 : stats->obytes = tx_total_bytes;
1372 : 0 : stats->oerrors = tx_total_errors;
1373 : :
1374 : 0 : return 0;
1375 : : }
1376 : :
1377 : : static int
1378 : 0 : eth_stats_reset(struct rte_eth_dev *dev)
1379 : : {
1380 : : struct vhost_queue *vq;
1381 : : unsigned i;
1382 : :
1383 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1384 [ # # ]: 0 : if (dev->data->rx_queues[i] == NULL)
1385 : 0 : continue;
1386 : : vq = dev->data->rx_queues[i];
1387 : 0 : vq->stats.pkts = 0;
1388 : 0 : vq->stats.bytes = 0;
1389 : : }
1390 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1391 [ # # ]: 0 : if (dev->data->tx_queues[i] == NULL)
1392 : 0 : continue;
1393 : : vq = dev->data->tx_queues[i];
1394 : 0 : vq->stats.pkts = 0;
1395 : 0 : vq->stats.bytes = 0;
1396 : 0 : vq->stats.missed_pkts = 0;
1397 : : }
1398 : :
1399 : 0 : return 0;
1400 : : }
1401 : :
1402 : : static void
1403 : 0 : eth_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1404 : : {
1405 : 0 : rte_free(dev->data->rx_queues[qid]);
1406 : 0 : }
1407 : :
1408 : : static void
1409 : 0 : eth_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1410 : : {
1411 : 0 : rte_free(dev->data->tx_queues[qid]);
1412 : 0 : }
1413 : :
1414 : : static int
1415 : 0 : eth_tx_done_cleanup(void *txq __rte_unused, uint32_t free_cnt __rte_unused)
1416 : : {
1417 : : /*
1418 : : * vHost does not hang onto mbuf. eth_vhost_tx() copies packet data
1419 : : * and releases mbuf, so nothing to cleanup.
1420 : : */
1421 : 0 : return 0;
1422 : : }
1423 : :
1424 : : static int
1425 : 0 : eth_link_update(struct rte_eth_dev *dev __rte_unused,
1426 : : int wait_to_complete __rte_unused)
1427 : : {
1428 : 0 : return 0;
1429 : : }
1430 : :
1431 : : static int
1432 : 0 : eth_rx_queue_count(void *rx_queue)
1433 : : {
1434 : : struct vhost_queue *vq;
1435 : :
1436 : : vq = rx_queue;
1437 [ # # ]: 0 : if (vq == NULL)
1438 : : return 0;
1439 : :
1440 : 0 : return rte_vhost_rx_queue_count(vq->vid, vq->virtqueue_id);
1441 : : }
1442 : :
1443 : : #define CLB_VAL_IDX 0
1444 : : #define CLB_MSK_IDX 1
1445 : : #define CLB_MATCH_IDX 2
1446 : : static int
1447 : 0 : vhost_monitor_callback(const uint64_t value,
1448 : : const uint64_t opaque[RTE_POWER_MONITOR_OPAQUE_SZ])
1449 : : {
1450 : 0 : const uint64_t m = opaque[CLB_MSK_IDX];
1451 : 0 : const uint64_t v = opaque[CLB_VAL_IDX];
1452 : 0 : const uint64_t c = opaque[CLB_MATCH_IDX];
1453 : :
1454 [ # # ]: 0 : if (c)
1455 [ # # ]: 0 : return (value & m) == v ? -1 : 0;
1456 : : else
1457 [ # # ]: 0 : return (value & m) == v ? 0 : -1;
1458 : : }
1459 : :
1460 : : static int
1461 : 0 : vhost_get_monitor_addr(void *rx_queue, struct rte_power_monitor_cond *pmc)
1462 : : {
1463 : : struct vhost_queue *vq = rx_queue;
1464 : : struct rte_vhost_power_monitor_cond vhost_pmc;
1465 : : int ret;
1466 [ # # ]: 0 : if (vq == NULL)
1467 : : return -EINVAL;
1468 : 0 : ret = rte_vhost_get_monitor_addr(vq->vid, vq->virtqueue_id,
1469 : : &vhost_pmc);
1470 [ # # ]: 0 : if (ret < 0)
1471 : : return -EINVAL;
1472 : 0 : pmc->addr = vhost_pmc.addr;
1473 : 0 : pmc->opaque[CLB_VAL_IDX] = vhost_pmc.val;
1474 : 0 : pmc->opaque[CLB_MSK_IDX] = vhost_pmc.mask;
1475 : 0 : pmc->opaque[CLB_MATCH_IDX] = vhost_pmc.match;
1476 : 0 : pmc->size = vhost_pmc.size;
1477 : 0 : pmc->fn = vhost_monitor_callback;
1478 : :
1479 : 0 : return 0;
1480 : : }
1481 : :
1482 : : static int
1483 : 0 : vhost_dev_priv_dump(struct rte_eth_dev *dev, FILE *f)
1484 : : {
1485 : 0 : struct pmd_internal *internal = dev->data->dev_private;
1486 : :
1487 : 0 : fprintf(f, "iface_name: %s\n", internal->iface_name);
1488 : 0 : fprintf(f, "flags: 0x%" PRIx64 "\n", internal->flags);
1489 : 0 : fprintf(f, "disable_flags: 0x%" PRIx64 "\n", internal->disable_flags);
1490 : 0 : fprintf(f, "features: 0x%" PRIx64 "\n", internal->features);
1491 : 0 : fprintf(f, "max_queues: %u\n", internal->max_queues);
1492 : 0 : fprintf(f, "vid: %d\n", internal->vid);
1493 : 0 : fprintf(f, "started: %u\n",
1494 : 0 : rte_atomic_load_explicit(&internal->started, rte_memory_order_relaxed));
1495 : 0 : fprintf(f, "dev_attached: %u\n",
1496 : 0 : rte_atomic_load_explicit(&internal->dev_attached, rte_memory_order_relaxed));
1497 : 0 : fprintf(f, "vlan_strip: %d\n", internal->vlan_strip);
1498 : 0 : fprintf(f, "rx_sw_csum: %d\n", internal->rx_sw_csum);
1499 : 0 : fprintf(f, "tx_sw_csum: %d\n", internal->tx_sw_csum);
1500 : :
1501 : 0 : return 0;
1502 : : }
1503 : :
1504 : : static void
1505 : 0 : eth_rxq_info_get(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1506 : : struct rte_eth_rxq_info *qinfo)
1507 : : {
1508 : : struct rte_vhost_vring vring;
1509 : 0 : struct vhost_queue *vq = dev->data->rx_queues[rx_queue_id];
1510 : :
1511 : 0 : qinfo->nb_desc = 0;
1512 : :
1513 [ # # # # ]: 0 : if (vq == NULL || vq->vid < 0)
1514 : 0 : return;
1515 : :
1516 [ # # ]: 0 : if (rte_vhost_get_vhost_vring(vq->vid, vq->virtqueue_id, &vring) < 0)
1517 : : return;
1518 : :
1519 : 0 : qinfo->nb_desc = vring.size;
1520 : : }
1521 : :
1522 : : static void
1523 : 0 : eth_txq_info_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1524 : : struct rte_eth_txq_info *qinfo)
1525 : : {
1526 : : struct rte_vhost_vring vring;
1527 : 0 : struct vhost_queue *vq = dev->data->tx_queues[tx_queue_id];
1528 : :
1529 : 0 : qinfo->nb_desc = 0;
1530 : :
1531 [ # # # # ]: 0 : if (vq == NULL || vq->vid < 0)
1532 : 0 : return;
1533 : :
1534 [ # # ]: 0 : if (rte_vhost_get_vhost_vring(vq->vid, vq->virtqueue_id, &vring) < 0)
1535 : : return;
1536 : :
1537 : 0 : qinfo->nb_desc = vring.size;
1538 : : }
1539 : :
1540 : : static const struct eth_dev_ops ops = {
1541 : : .dev_start = eth_dev_start,
1542 : : .dev_stop = eth_dev_stop,
1543 : : .dev_close = eth_dev_close,
1544 : : .dev_configure = eth_dev_configure,
1545 : : .dev_infos_get = eth_dev_info,
1546 : : .rxq_info_get = eth_rxq_info_get,
1547 : : .txq_info_get = eth_txq_info_get,
1548 : : .rx_queue_setup = eth_rx_queue_setup,
1549 : : .tx_queue_setup = eth_tx_queue_setup,
1550 : : .rx_queue_release = eth_rx_queue_release,
1551 : : .tx_queue_release = eth_tx_queue_release,
1552 : : .tx_done_cleanup = eth_tx_done_cleanup,
1553 : : .link_update = eth_link_update,
1554 : : .stats_get = eth_stats_get,
1555 : : .stats_reset = eth_stats_reset,
1556 : : .xstats_reset = vhost_dev_xstats_reset,
1557 : : .xstats_get = vhost_dev_xstats_get,
1558 : : .xstats_get_names = vhost_dev_xstats_get_names,
1559 : : .rx_queue_intr_enable = eth_rxq_intr_enable,
1560 : : .rx_queue_intr_disable = eth_rxq_intr_disable,
1561 : : .get_monitor_addr = vhost_get_monitor_addr,
1562 : : .eth_dev_priv_dump = vhost_dev_priv_dump,
1563 : : };
1564 : :
1565 : : static int
1566 [ # # ]: 0 : eth_dev_vhost_create(struct rte_vdev_device *dev, char *iface_name,
1567 : : int16_t queues, const unsigned int numa_node, uint64_t flags,
1568 : : uint64_t disable_flags)
1569 : : {
1570 : : const char *name = rte_vdev_device_name(dev);
1571 : : struct rte_eth_dev_data *data;
1572 : : struct pmd_internal *internal = NULL;
1573 : : struct rte_eth_dev *eth_dev = NULL;
1574 : : struct rte_ether_addr *eth_addr = NULL;
1575 : :
1576 : 0 : VHOST_LOG_LINE(INFO, "Creating VHOST-USER backend on numa socket %u",
1577 : : numa_node);
1578 : :
1579 : : /* reserve an ethdev entry */
1580 : 0 : eth_dev = rte_eth_vdev_allocate(dev, sizeof(*internal));
1581 [ # # ]: 0 : if (eth_dev == NULL)
1582 : 0 : goto error;
1583 : 0 : data = eth_dev->data;
1584 : :
1585 : 0 : eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1586 [ # # ]: 0 : if (eth_addr == NULL)
1587 : 0 : goto error;
1588 : 0 : data->mac_addrs = eth_addr;
1589 : 0 : *eth_addr = base_eth_addr;
1590 : 0 : eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1591 : :
1592 : : /* now put it all together
1593 : : * - store queue data in internal,
1594 : : * - point eth_dev_data to internals
1595 : : * - and point eth_dev structure to new eth_dev_data structure
1596 : : */
1597 : 0 : internal = eth_dev->data->dev_private;
1598 : 0 : internal->iface_name = rte_malloc_socket(name, strlen(iface_name) + 1,
1599 : : 0, numa_node);
1600 [ # # ]: 0 : if (internal->iface_name == NULL)
1601 : 0 : goto error;
1602 : : strcpy(internal->iface_name, iface_name);
1603 : :
1604 : 0 : data->nb_rx_queues = queues;
1605 : 0 : data->nb_tx_queues = queues;
1606 : 0 : internal->max_queues = queues;
1607 : 0 : internal->vid = -1;
1608 : 0 : internal->flags = flags;
1609 : 0 : internal->disable_flags = disable_flags;
1610 : 0 : data->dev_link = pmd_link;
1611 : 0 : data->dev_flags = RTE_ETH_DEV_INTR_LSC |
1612 : : RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1613 : 0 : data->promiscuous = 1;
1614 : 0 : data->all_multicast = 1;
1615 : :
1616 : 0 : eth_dev->dev_ops = &ops;
1617 : 0 : eth_dev->rx_queue_count = eth_rx_queue_count;
1618 : :
1619 : : /* finally assign rx and tx ops */
1620 : 0 : eth_dev->rx_pkt_burst = eth_vhost_rx;
1621 : 0 : eth_dev->tx_pkt_burst = eth_vhost_tx;
1622 : :
1623 : 0 : rte_eth_dev_probing_finish(eth_dev);
1624 : 0 : return 0;
1625 : :
1626 : : error:
1627 : : if (internal)
1628 : 0 : rte_free(internal->iface_name);
1629 : 0 : rte_eth_dev_release_port(eth_dev);
1630 : :
1631 : 0 : return -1;
1632 : : }
1633 : :
1634 : : static inline int
1635 : 0 : open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1636 : : {
1637 : : const char **iface_name = extra_args;
1638 : :
1639 [ # # ]: 0 : if (value == NULL)
1640 : : return -1;
1641 : :
1642 : 0 : *iface_name = value;
1643 : :
1644 : 0 : return 0;
1645 : : }
1646 : :
1647 : : static inline int
1648 : 0 : open_int(const char *key __rte_unused, const char *value, void *extra_args)
1649 : : {
1650 : : uint16_t *n = extra_args;
1651 : :
1652 [ # # ]: 0 : if (value == NULL || extra_args == NULL)
1653 : : return -EINVAL;
1654 : :
1655 : 0 : *n = (uint16_t)strtoul(value, NULL, 0);
1656 [ # # # # ]: 0 : if (*n == USHRT_MAX && errno == ERANGE)
1657 : 0 : return -1;
1658 : :
1659 : : return 0;
1660 : : }
1661 : :
1662 : : static int
1663 : 0 : rte_pmd_vhost_probe(struct rte_vdev_device *dev)
1664 : : {
1665 : : struct rte_kvargs *kvlist = NULL;
1666 : : int ret = 0;
1667 : : char *iface_name;
1668 : : uint16_t queues;
1669 : : uint64_t flags = RTE_VHOST_USER_NET_STATS_ENABLE;
1670 : : uint64_t disable_flags = 0;
1671 : 0 : int client_mode = 0;
1672 : 0 : int iommu_support = 0;
1673 : 0 : int postcopy_support = 0;
1674 : 0 : int tso = 0;
1675 : 0 : int linear_buf = 0;
1676 : 0 : int ext_buf = 0;
1677 [ # # ]: 0 : int legacy_ol_flags = 0;
1678 : : struct rte_eth_dev *eth_dev;
1679 : : const char *name = rte_vdev_device_name(dev);
1680 : :
1681 : 0 : VHOST_LOG_LINE(INFO, "Initializing pmd_vhost for %s", name);
1682 : :
1683 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1684 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
1685 [ # # ]: 0 : if (!eth_dev) {
1686 : 0 : VHOST_LOG_LINE(ERR, "Failed to probe %s", name);
1687 : 0 : return -1;
1688 : : }
1689 : 0 : eth_dev->rx_pkt_burst = eth_vhost_rx;
1690 : 0 : eth_dev->tx_pkt_burst = eth_vhost_tx;
1691 : 0 : eth_dev->dev_ops = &ops;
1692 [ # # ]: 0 : if (dev->device.numa_node == SOCKET_ID_ANY)
1693 : 0 : dev->device.numa_node = rte_socket_id();
1694 : 0 : eth_dev->device = &dev->device;
1695 : 0 : rte_eth_dev_probing_finish(eth_dev);
1696 : 0 : return 0;
1697 : : }
1698 : :
1699 : 0 : kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1700 [ # # ]: 0 : if (kvlist == NULL)
1701 : : return -1;
1702 : :
1703 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1704 : 0 : ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1705 : : &open_iface, &iface_name);
1706 [ # # ]: 0 : if (ret < 0)
1707 : 0 : goto out_free;
1708 : : } else {
1709 : : ret = -1;
1710 : 0 : goto out_free;
1711 : : }
1712 : :
1713 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1714 : 0 : ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1715 : : &open_int, &queues);
1716 [ # # # # ]: 0 : if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1717 : 0 : goto out_free;
1718 : :
1719 : : } else
1720 : 0 : queues = 1;
1721 : :
1722 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1723 : 0 : ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1724 : : &open_int, &client_mode);
1725 [ # # ]: 0 : if (ret < 0)
1726 : 0 : goto out_free;
1727 : :
1728 [ # # ]: 0 : if (client_mode)
1729 : : flags |= RTE_VHOST_USER_CLIENT;
1730 : : }
1731 : :
1732 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_IOMMU_SUPPORT) == 1) {
1733 : 0 : ret = rte_kvargs_process(kvlist, ETH_VHOST_IOMMU_SUPPORT,
1734 : : &open_int, &iommu_support);
1735 [ # # ]: 0 : if (ret < 0)
1736 : 0 : goto out_free;
1737 : :
1738 [ # # ]: 0 : if (iommu_support)
1739 : 0 : flags |= RTE_VHOST_USER_IOMMU_SUPPORT;
1740 : : }
1741 : :
1742 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_POSTCOPY_SUPPORT) == 1) {
1743 : 0 : ret = rte_kvargs_process(kvlist, ETH_VHOST_POSTCOPY_SUPPORT,
1744 : : &open_int, &postcopy_support);
1745 [ # # ]: 0 : if (ret < 0)
1746 : 0 : goto out_free;
1747 : :
1748 [ # # ]: 0 : if (postcopy_support)
1749 : 0 : flags |= RTE_VHOST_USER_POSTCOPY_SUPPORT;
1750 : : }
1751 : :
1752 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_VIRTIO_NET_F_HOST_TSO) == 1) {
1753 : 0 : ret = rte_kvargs_process(kvlist,
1754 : : ETH_VHOST_VIRTIO_NET_F_HOST_TSO,
1755 : : &open_int, &tso);
1756 [ # # ]: 0 : if (ret < 0)
1757 : 0 : goto out_free;
1758 : : }
1759 : :
1760 [ # # ]: 0 : if (tso == 0) {
1761 : : disable_flags |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
1762 : : disable_flags |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
1763 : : }
1764 : :
1765 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_LINEAR_BUF) == 1) {
1766 : 0 : ret = rte_kvargs_process(kvlist,
1767 : : ETH_VHOST_LINEAR_BUF,
1768 : : &open_int, &linear_buf);
1769 [ # # ]: 0 : if (ret < 0)
1770 : 0 : goto out_free;
1771 : :
1772 [ # # ]: 0 : if (linear_buf == 1)
1773 : 0 : flags |= RTE_VHOST_USER_LINEARBUF_SUPPORT;
1774 : : }
1775 : :
1776 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_EXT_BUF) == 1) {
1777 : 0 : ret = rte_kvargs_process(kvlist,
1778 : : ETH_VHOST_EXT_BUF,
1779 : : &open_int, &ext_buf);
1780 [ # # ]: 0 : if (ret < 0)
1781 : 0 : goto out_free;
1782 : :
1783 [ # # ]: 0 : if (ext_buf == 1)
1784 : 0 : flags |= RTE_VHOST_USER_EXTBUF_SUPPORT;
1785 : : }
1786 : :
1787 [ # # ]: 0 : if (rte_kvargs_count(kvlist, ETH_VHOST_LEGACY_OL_FLAGS) == 1) {
1788 : 0 : ret = rte_kvargs_process(kvlist,
1789 : : ETH_VHOST_LEGACY_OL_FLAGS,
1790 : : &open_int, &legacy_ol_flags);
1791 [ # # ]: 0 : if (ret < 0)
1792 : 0 : goto out_free;
1793 : : }
1794 : :
1795 [ # # ]: 0 : if (legacy_ol_flags == 0)
1796 : 0 : flags |= RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
1797 : :
1798 [ # # ]: 0 : if (dev->device.numa_node == SOCKET_ID_ANY)
1799 : 0 : dev->device.numa_node = rte_socket_id();
1800 : :
1801 : 0 : ret = eth_dev_vhost_create(dev, iface_name, queues,
1802 : 0 : dev->device.numa_node, flags, disable_flags);
1803 [ # # ]: 0 : if (ret == -1)
1804 : 0 : VHOST_LOG_LINE(ERR, "Failed to create %s", name);
1805 : :
1806 : 0 : out_free:
1807 : 0 : rte_kvargs_free(kvlist);
1808 : 0 : return ret;
1809 : : }
1810 : :
1811 : : static int
1812 [ # # ]: 0 : rte_pmd_vhost_remove(struct rte_vdev_device *dev)
1813 : : {
1814 : : const char *name;
1815 : : struct rte_eth_dev *eth_dev = NULL;
1816 : :
1817 : : name = rte_vdev_device_name(dev);
1818 : 0 : VHOST_LOG_LINE(INFO, "Un-Initializing pmd_vhost for %s", name);
1819 : :
1820 : : /* find an ethdev entry */
1821 : 0 : eth_dev = rte_eth_dev_allocated(name);
1822 [ # # ]: 0 : if (eth_dev == NULL)
1823 : : return 0;
1824 : :
1825 : 0 : eth_dev_close(eth_dev);
1826 : 0 : rte_eth_dev_release_port(eth_dev);
1827 : :
1828 : 0 : return 0;
1829 : : }
1830 : :
1831 : : static struct rte_vdev_driver pmd_vhost_drv = {
1832 : : .driver.alias = "eth_vhost",
1833 : : .probe = rte_pmd_vhost_probe,
1834 : : .remove = rte_pmd_vhost_remove,
1835 : : };
1836 : :
1837 : 305 : RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1838 : : RTE_PMD_REGISTER_PARAM_STRING(net_vhost,
1839 : : "iface=<ifc> "
1840 : : "queues=<int> "
1841 : : "client=<0|1> "
1842 : : "iommu-support=<0|1> "
1843 : : "postcopy-support=<0|1> "
1844 : : "tso=<0|1> "
1845 : : "linear-buffer=<0|1> "
1846 : : "ext-buffer=<0|1>");
|