4 * Copyright (c) 2016 IGEL Co., Ltd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of IGEL Co.,Ltd. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_ethdev_driver.h>
39 #include <rte_ethdev_vdev.h>
40 #include <rte_malloc.h>
41 #include <rte_memcpy.h>
42 #include <rte_bus_vdev.h>
43 #include <rte_kvargs.h>
44 #include <rte_vhost.h>
45 #include <rte_spinlock.h>
47 #include "rte_eth_vhost.h"
49 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
51 #define ETH_VHOST_IFACE_ARG "iface"
52 #define ETH_VHOST_QUEUES_ARG "queues"
53 #define ETH_VHOST_CLIENT_ARG "client"
54 #define ETH_VHOST_DEQUEUE_ZERO_COPY "dequeue-zero-copy"
55 #define ETH_VHOST_IOMMU_SUPPORT "iommu-support"
56 #define VHOST_MAX_PKT_BURST 32
58 static const char *valid_arguments[] = {
62 ETH_VHOST_DEQUEUE_ZERO_COPY,
63 ETH_VHOST_IOMMU_SUPPORT,
67 static struct ether_addr base_eth_addr = {
78 enum vhost_xstats_pkts {
79 VHOST_UNDERSIZE_PKT = 0,
84 VHOST_512_TO_1023_PKT,
85 VHOST_1024_TO_1522_PKT,
86 VHOST_1523_TO_MAX_PKT,
91 VHOST_ERRORS_FRAGMENTED,
93 VHOST_UNKNOWN_PROTOCOL,
100 uint64_t missed_pkts;
101 uint64_t xstats[VHOST_XSTATS_MAX];
106 rte_atomic32_t allow_queuing;
107 rte_atomic32_t while_queuing;
108 struct pmd_internal *internal;
109 struct rte_mempool *mb_pool;
111 uint16_t virtqueue_id;
112 struct vhost_stats stats;
115 struct pmd_internal {
116 rte_atomic32_t dev_attached;
121 rte_atomic32_t started;
125 struct internal_list {
126 TAILQ_ENTRY(internal_list) next;
127 struct rte_eth_dev *eth_dev;
130 TAILQ_HEAD(internal_list_head, internal_list);
131 static struct internal_list_head internal_list =
132 TAILQ_HEAD_INITIALIZER(internal_list);
134 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
136 static struct rte_eth_link pmd_link = {
138 .link_duplex = ETH_LINK_FULL_DUPLEX,
139 .link_status = ETH_LINK_DOWN
142 struct rte_vhost_vring_state {
145 bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
146 bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
148 unsigned int max_vring;
151 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
153 #define VHOST_XSTATS_NAME_SIZE 64
155 struct vhost_xstats_name_off {
156 char name[VHOST_XSTATS_NAME_SIZE];
160 /* [rx]_is prepended to the name string here */
161 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
163 offsetof(struct vhost_queue, stats.pkts)},
165 offsetof(struct vhost_queue, stats.bytes)},
167 offsetof(struct vhost_queue, stats.missed_pkts)},
168 {"broadcast_packets",
169 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
170 {"multicast_packets",
171 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
173 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
174 {"undersize_packets",
175 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
177 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
178 {"size_65_to_127_packets",
179 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
180 {"size_128_to_255_packets",
181 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
182 {"size_256_to_511_packets",
183 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
184 {"size_512_to_1023_packets",
185 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
186 {"size_1024_to_1522_packets",
187 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
188 {"size_1523_to_max_packets",
189 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
190 {"errors_with_bad_CRC",
191 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
192 {"fragmented_errors",
193 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
195 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
196 {"unknown_protos_packets",
197 offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
200 /* [tx]_ is prepended to the name string here */
201 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
203 offsetof(struct vhost_queue, stats.pkts)},
205 offsetof(struct vhost_queue, stats.bytes)},
207 offsetof(struct vhost_queue, stats.missed_pkts)},
208 {"broadcast_packets",
209 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
210 {"multicast_packets",
211 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
213 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
214 {"undersize_packets",
215 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
217 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
218 {"size_65_to_127_packets",
219 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
220 {"size_128_to_255_packets",
221 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
222 {"size_256_to_511_packets",
223 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
224 {"size_512_to_1023_packets",
225 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
226 {"size_1024_to_1522_packets",
227 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
228 {"size_1523_to_max_packets",
229 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
230 {"errors_with_bad_CRC",
231 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
234 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
235 sizeof(vhost_rxport_stat_strings[0]))
237 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
238 sizeof(vhost_txport_stat_strings[0]))
241 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
243 struct vhost_queue *vq = NULL;
246 for (i = 0; i < dev->data->nb_rx_queues; i++) {
247 vq = dev->data->rx_queues[i];
250 memset(&vq->stats, 0, sizeof(vq->stats));
252 for (i = 0; i < dev->data->nb_tx_queues; i++) {
253 vq = dev->data->tx_queues[i];
256 memset(&vq->stats, 0, sizeof(vq->stats));
261 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
262 struct rte_eth_xstat_name *xstats_names,
263 unsigned int limit __rte_unused)
267 int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
271 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
272 snprintf(xstats_names[count].name,
273 sizeof(xstats_names[count].name),
274 "rx_%s", vhost_rxport_stat_strings[t].name);
277 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
278 snprintf(xstats_names[count].name,
279 sizeof(xstats_names[count].name),
280 "tx_%s", vhost_txport_stat_strings[t].name);
287 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
292 unsigned int count = 0;
293 struct vhost_queue *vq = NULL;
294 unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
299 for (i = 0; i < dev->data->nb_rx_queues; i++) {
300 vq = dev->data->rx_queues[i];
303 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
304 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
305 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
307 for (i = 0; i < dev->data->nb_tx_queues; i++) {
308 vq = dev->data->tx_queues[i];
311 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
312 + vq->stats.missed_pkts
313 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
314 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
316 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
317 xstats[count].value = 0;
318 for (i = 0; i < dev->data->nb_rx_queues; i++) {
319 vq = dev->data->rx_queues[i];
322 xstats[count].value +=
323 *(uint64_t *)(((char *)vq)
324 + vhost_rxport_stat_strings[t].offset);
326 xstats[count].id = count;
329 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
330 xstats[count].value = 0;
331 for (i = 0; i < dev->data->nb_tx_queues; i++) {
332 vq = dev->data->tx_queues[i];
335 xstats[count].value +=
336 *(uint64_t *)(((char *)vq)
337 + vhost_txport_stat_strings[t].offset);
339 xstats[count].id = count;
346 vhost_count_multicast_broadcast(struct vhost_queue *vq,
347 struct rte_mbuf *mbuf)
349 struct ether_addr *ea = NULL;
350 struct vhost_stats *pstats = &vq->stats;
352 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
353 if (is_multicast_ether_addr(ea)) {
354 if (is_broadcast_ether_addr(ea))
355 pstats->xstats[VHOST_BROADCAST_PKT]++;
357 pstats->xstats[VHOST_MULTICAST_PKT]++;
362 vhost_update_packet_xstats(struct vhost_queue *vq,
363 struct rte_mbuf **bufs,
366 uint32_t pkt_len = 0;
369 struct vhost_stats *pstats = &vq->stats;
371 for (i = 0; i < count ; i++) {
372 pkt_len = bufs[i]->pkt_len;
374 pstats->xstats[VHOST_64_PKT]++;
375 } else if (pkt_len > 64 && pkt_len < 1024) {
376 index = (sizeof(pkt_len) * 8)
377 - __builtin_clz(pkt_len) - 5;
378 pstats->xstats[index]++;
381 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
382 else if (pkt_len <= 1522)
383 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
384 else if (pkt_len > 1522)
385 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
387 vhost_count_multicast_broadcast(vq, bufs[i]);
392 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
394 struct vhost_queue *r = q;
395 uint16_t i, nb_rx = 0;
396 uint16_t nb_receive = nb_bufs;
398 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
401 rte_atomic32_set(&r->while_queuing, 1);
403 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
406 /* Dequeue packets from guest TX queue */
409 uint16_t num = (uint16_t)RTE_MIN(nb_receive,
410 VHOST_MAX_PKT_BURST);
412 nb_pkts = rte_vhost_dequeue_burst(r->vid, r->virtqueue_id,
413 r->mb_pool, &bufs[nb_rx],
417 nb_receive -= nb_pkts;
422 r->stats.pkts += nb_rx;
424 for (i = 0; likely(i < nb_rx); i++) {
425 bufs[i]->port = r->port;
426 bufs[i]->ol_flags = 0;
427 bufs[i]->vlan_tci = 0;
429 if (r->internal->vlan_strip)
430 rte_vlan_strip(bufs[i]);
432 r->stats.bytes += bufs[i]->pkt_len;
435 vhost_update_packet_xstats(r, bufs, nb_rx);
438 rte_atomic32_set(&r->while_queuing, 0);
444 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
446 struct vhost_queue *r = q;
447 uint16_t i, nb_tx = 0;
448 uint16_t nb_send = 0;
450 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
453 rte_atomic32_set(&r->while_queuing, 1);
455 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
458 for (i = 0; i < nb_bufs; i++) {
459 struct rte_mbuf *m = bufs[i];
461 /* Do VLAN tag insertion */
462 if (m->ol_flags & PKT_TX_VLAN_PKT) {
463 int error = rte_vlan_insert(&m);
464 if (unlikely(error)) {
474 /* Enqueue packets to guest RX queue */
477 uint16_t num = (uint16_t)RTE_MIN(nb_send,
478 VHOST_MAX_PKT_BURST);
480 nb_pkts = rte_vhost_enqueue_burst(r->vid, r->virtqueue_id,
489 r->stats.pkts += nb_tx;
490 r->stats.missed_pkts += nb_bufs - nb_tx;
492 for (i = 0; likely(i < nb_tx); i++)
493 r->stats.bytes += bufs[i]->pkt_len;
495 vhost_update_packet_xstats(r, bufs, nb_tx);
497 /* According to RFC2863 page42 section ifHCOutMulticastPkts and
498 * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
499 * are increased when packets are not transmitted successfully.
501 for (i = nb_tx; i < nb_bufs; i++)
502 vhost_count_multicast_broadcast(r, bufs[i]);
504 for (i = 0; likely(i < nb_tx); i++)
505 rte_pktmbuf_free(bufs[i]);
507 rte_atomic32_set(&r->while_queuing, 0);
513 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
515 struct pmd_internal *internal = dev->data->dev_private;
516 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
518 internal->vlan_strip = rxmode->hw_vlan_strip;
520 if (rxmode->hw_vlan_filter)
521 RTE_LOG(WARNING, PMD,
522 "vhost(%s): vlan filtering not available\n",
528 static inline struct internal_list *
529 find_internal_resource(char *ifname)
532 struct internal_list *list;
533 struct pmd_internal *internal;
538 pthread_mutex_lock(&internal_list_lock);
540 TAILQ_FOREACH(list, &internal_list, next) {
541 internal = list->eth_dev->data->dev_private;
542 if (!strcmp(internal->iface_name, ifname)) {
548 pthread_mutex_unlock(&internal_list_lock);
557 update_queuing_status(struct rte_eth_dev *dev)
559 struct pmd_internal *internal = dev->data->dev_private;
560 struct vhost_queue *vq;
562 int allow_queuing = 1;
564 if (rte_atomic32_read(&internal->dev_attached) == 0)
567 if (rte_atomic32_read(&internal->started) == 0)
570 /* Wait until rx/tx_pkt_burst stops accessing vhost device */
571 for (i = 0; i < dev->data->nb_rx_queues; i++) {
572 vq = dev->data->rx_queues[i];
575 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
576 while (rte_atomic32_read(&vq->while_queuing))
580 for (i = 0; i < dev->data->nb_tx_queues; i++) {
581 vq = dev->data->tx_queues[i];
584 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
585 while (rte_atomic32_read(&vq->while_queuing))
591 queue_setup(struct rte_eth_dev *eth_dev, struct pmd_internal *internal)
593 struct vhost_queue *vq;
596 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
597 vq = eth_dev->data->rx_queues[i];
600 vq->vid = internal->vid;
601 vq->internal = internal;
602 vq->port = eth_dev->data->port_id;
604 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
605 vq = eth_dev->data->tx_queues[i];
608 vq->vid = internal->vid;
609 vq->internal = internal;
610 vq->port = eth_dev->data->port_id;
617 struct rte_eth_dev *eth_dev;
618 struct internal_list *list;
619 struct pmd_internal *internal;
621 char ifname[PATH_MAX];
622 #ifdef RTE_LIBRTE_VHOST_NUMA
626 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
627 list = find_internal_resource(ifname);
629 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
633 eth_dev = list->eth_dev;
634 internal = eth_dev->data->dev_private;
636 #ifdef RTE_LIBRTE_VHOST_NUMA
637 newnode = rte_vhost_get_numa_node(vid);
639 eth_dev->data->numa_node = newnode;
643 if (eth_dev->data->rx_queues && eth_dev->data->tx_queues) {
644 queue_setup(eth_dev, internal);
645 rte_atomic32_set(&internal->dev_attached, 1);
647 RTE_LOG(INFO, PMD, "RX/TX queues have not setup yet\n");
648 rte_atomic32_set(&internal->dev_attached, 0);
651 for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
652 rte_vhost_enable_guest_notification(vid, i, 0);
654 rte_vhost_get_mtu(vid, ð_dev->data->mtu);
656 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
658 update_queuing_status(eth_dev);
660 RTE_LOG(INFO, PMD, "Vhost device %d created\n", vid);
662 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
668 destroy_device(int vid)
670 struct rte_eth_dev *eth_dev;
671 struct pmd_internal *internal;
672 struct vhost_queue *vq;
673 struct internal_list *list;
674 char ifname[PATH_MAX];
676 struct rte_vhost_vring_state *state;
678 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
679 list = find_internal_resource(ifname);
681 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
684 eth_dev = list->eth_dev;
685 internal = eth_dev->data->dev_private;
687 rte_atomic32_set(&internal->started, 0);
688 update_queuing_status(eth_dev);
689 rte_atomic32_set(&internal->dev_attached, 0);
691 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
693 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
694 vq = eth_dev->data->rx_queues[i];
699 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
700 vq = eth_dev->data->tx_queues[i];
706 state = vring_states[eth_dev->data->port_id];
707 rte_spinlock_lock(&state->lock);
708 for (i = 0; i <= state->max_vring; i++) {
709 state->cur[i] = false;
710 state->seen[i] = false;
712 state->max_vring = 0;
713 rte_spinlock_unlock(&state->lock);
715 RTE_LOG(INFO, PMD, "Vhost device %d destroyed\n", vid);
717 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
721 vring_state_changed(int vid, uint16_t vring, int enable)
723 struct rte_vhost_vring_state *state;
724 struct rte_eth_dev *eth_dev;
725 struct internal_list *list;
726 char ifname[PATH_MAX];
728 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
729 list = find_internal_resource(ifname);
731 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
735 eth_dev = list->eth_dev;
737 state = vring_states[eth_dev->data->port_id];
738 rte_spinlock_lock(&state->lock);
739 state->cur[vring] = enable;
740 state->max_vring = RTE_MAX(vring, state->max_vring);
741 rte_spinlock_unlock(&state->lock);
743 RTE_LOG(INFO, PMD, "vring%u is %s\n",
744 vring, enable ? "enabled" : "disabled");
746 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
751 static struct vhost_device_ops vhost_ops = {
752 .new_device = new_device,
753 .destroy_device = destroy_device,
754 .vring_state_changed = vring_state_changed,
758 rte_eth_vhost_get_queue_event(uint16_t port_id,
759 struct rte_eth_vhost_queue_event *event)
761 struct rte_vhost_vring_state *state;
765 if (port_id >= RTE_MAX_ETHPORTS) {
766 RTE_LOG(ERR, PMD, "Invalid port id\n");
770 state = vring_states[port_id];
772 RTE_LOG(ERR, PMD, "Unused port\n");
776 rte_spinlock_lock(&state->lock);
777 for (i = 0; i <= state->max_vring; i++) {
778 idx = state->index++ % (state->max_vring + 1);
780 if (state->cur[idx] != state->seen[idx]) {
781 state->seen[idx] = state->cur[idx];
782 event->queue_id = idx / 2;
784 event->enable = state->cur[idx];
785 rte_spinlock_unlock(&state->lock);
789 rte_spinlock_unlock(&state->lock);
795 rte_eth_vhost_get_vid_from_port_id(uint16_t port_id)
797 struct internal_list *list;
798 struct rte_eth_dev *eth_dev;
799 struct vhost_queue *vq;
802 if (!rte_eth_dev_is_valid_port(port_id))
805 pthread_mutex_lock(&internal_list_lock);
807 TAILQ_FOREACH(list, &internal_list, next) {
808 eth_dev = list->eth_dev;
809 if (eth_dev->data->port_id == port_id) {
810 vq = eth_dev->data->rx_queues[0];
818 pthread_mutex_unlock(&internal_list_lock);
824 eth_dev_start(struct rte_eth_dev *eth_dev)
826 struct pmd_internal *internal = eth_dev->data->dev_private;
828 if (unlikely(rte_atomic32_read(&internal->dev_attached) == 0)) {
829 queue_setup(eth_dev, internal);
830 rte_atomic32_set(&internal->dev_attached, 1);
833 rte_atomic32_set(&internal->started, 1);
834 update_queuing_status(eth_dev);
840 eth_dev_stop(struct rte_eth_dev *dev)
842 struct pmd_internal *internal = dev->data->dev_private;
844 rte_atomic32_set(&internal->started, 0);
845 update_queuing_status(dev);
849 eth_dev_close(struct rte_eth_dev *dev)
851 struct pmd_internal *internal;
852 struct internal_list *list;
855 internal = dev->data->dev_private;
861 rte_vhost_driver_unregister(internal->iface_name);
863 list = find_internal_resource(internal->iface_name);
867 pthread_mutex_lock(&internal_list_lock);
868 TAILQ_REMOVE(&internal_list, list, next);
869 pthread_mutex_unlock(&internal_list_lock);
872 for (i = 0; i < dev->data->nb_rx_queues; i++)
873 rte_free(dev->data->rx_queues[i]);
874 for (i = 0; i < dev->data->nb_tx_queues; i++)
875 rte_free(dev->data->tx_queues[i]);
877 rte_free(dev->data->mac_addrs);
878 free(internal->dev_name);
879 free(internal->iface_name);
882 dev->data->dev_private = NULL;
886 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
887 uint16_t nb_rx_desc __rte_unused,
888 unsigned int socket_id,
889 const struct rte_eth_rxconf *rx_conf __rte_unused,
890 struct rte_mempool *mb_pool)
892 struct vhost_queue *vq;
894 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
895 RTE_CACHE_LINE_SIZE, socket_id);
897 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
901 vq->mb_pool = mb_pool;
902 vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
903 dev->data->rx_queues[rx_queue_id] = vq;
909 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
910 uint16_t nb_tx_desc __rte_unused,
911 unsigned int socket_id,
912 const struct rte_eth_txconf *tx_conf __rte_unused)
914 struct vhost_queue *vq;
916 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
917 RTE_CACHE_LINE_SIZE, socket_id);
919 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
923 vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
924 dev->data->tx_queues[tx_queue_id] = vq;
930 eth_dev_info(struct rte_eth_dev *dev,
931 struct rte_eth_dev_info *dev_info)
933 struct pmd_internal *internal;
935 internal = dev->data->dev_private;
936 if (internal == NULL) {
937 RTE_LOG(ERR, PMD, "Invalid device specified\n");
941 dev_info->max_mac_addrs = 1;
942 dev_info->max_rx_pktlen = (uint32_t)-1;
943 dev_info->max_rx_queues = internal->max_queues;
944 dev_info->max_tx_queues = internal->max_queues;
945 dev_info->min_rx_bufsize = 0;
949 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
952 unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
953 unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
954 struct vhost_queue *vq;
956 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
957 i < dev->data->nb_rx_queues; i++) {
958 if (dev->data->rx_queues[i] == NULL)
960 vq = dev->data->rx_queues[i];
961 stats->q_ipackets[i] = vq->stats.pkts;
962 rx_total += stats->q_ipackets[i];
964 stats->q_ibytes[i] = vq->stats.bytes;
965 rx_total_bytes += stats->q_ibytes[i];
968 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
969 i < dev->data->nb_tx_queues; i++) {
970 if (dev->data->tx_queues[i] == NULL)
972 vq = dev->data->tx_queues[i];
973 stats->q_opackets[i] = vq->stats.pkts;
974 tx_missed_total += vq->stats.missed_pkts;
975 tx_total += stats->q_opackets[i];
977 stats->q_obytes[i] = vq->stats.bytes;
978 tx_total_bytes += stats->q_obytes[i];
981 stats->ipackets = rx_total;
982 stats->opackets = tx_total;
983 stats->oerrors = tx_missed_total;
984 stats->ibytes = rx_total_bytes;
985 stats->obytes = tx_total_bytes;
991 eth_stats_reset(struct rte_eth_dev *dev)
993 struct vhost_queue *vq;
996 for (i = 0; i < dev->data->nb_rx_queues; i++) {
997 if (dev->data->rx_queues[i] == NULL)
999 vq = dev->data->rx_queues[i];
1001 vq->stats.bytes = 0;
1003 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1004 if (dev->data->tx_queues[i] == NULL)
1006 vq = dev->data->tx_queues[i];
1008 vq->stats.bytes = 0;
1009 vq->stats.missed_pkts = 0;
1014 eth_queue_release(void *q)
1020 eth_tx_done_cleanup(void *txq __rte_unused, uint32_t free_cnt __rte_unused)
1023 * vHost does not hang onto mbuf. eth_vhost_tx() copies packet data
1024 * and releases mbuf, so nothing to cleanup.
1030 eth_link_update(struct rte_eth_dev *dev __rte_unused,
1031 int wait_to_complete __rte_unused)
1037 eth_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1039 struct vhost_queue *vq;
1041 vq = dev->data->rx_queues[rx_queue_id];
1045 return rte_vhost_rx_queue_count(vq->vid, vq->virtqueue_id);
1048 static const struct eth_dev_ops ops = {
1049 .dev_start = eth_dev_start,
1050 .dev_stop = eth_dev_stop,
1051 .dev_close = eth_dev_close,
1052 .dev_configure = eth_dev_configure,
1053 .dev_infos_get = eth_dev_info,
1054 .rx_queue_setup = eth_rx_queue_setup,
1055 .tx_queue_setup = eth_tx_queue_setup,
1056 .rx_queue_release = eth_queue_release,
1057 .tx_queue_release = eth_queue_release,
1058 .tx_done_cleanup = eth_tx_done_cleanup,
1059 .rx_queue_count = eth_rx_queue_count,
1060 .link_update = eth_link_update,
1061 .stats_get = eth_stats_get,
1062 .stats_reset = eth_stats_reset,
1063 .xstats_reset = vhost_dev_xstats_reset,
1064 .xstats_get = vhost_dev_xstats_get,
1065 .xstats_get_names = vhost_dev_xstats_get_names,
1068 static struct rte_vdev_driver pmd_vhost_drv;
1071 eth_dev_vhost_create(struct rte_vdev_device *dev, char *iface_name,
1072 int16_t queues, const unsigned int numa_node, uint64_t flags)
1074 const char *name = rte_vdev_device_name(dev);
1075 struct rte_eth_dev_data *data = NULL;
1076 struct pmd_internal *internal = NULL;
1077 struct rte_eth_dev *eth_dev = NULL;
1078 struct ether_addr *eth_addr = NULL;
1079 struct rte_vhost_vring_state *vring_state = NULL;
1080 struct internal_list *list = NULL;
1082 RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
1085 /* now do all data allocation - for eth_dev structure and internal
1088 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1092 list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1096 /* reserve an ethdev entry */
1097 eth_dev = rte_eth_vdev_allocate(dev, sizeof(*internal));
1098 if (eth_dev == NULL)
1101 eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1102 if (eth_addr == NULL)
1104 *eth_addr = base_eth_addr;
1105 eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1107 vring_state = rte_zmalloc_socket(name,
1108 sizeof(*vring_state), 0, numa_node);
1109 if (vring_state == NULL)
1112 /* now put it all together
1113 * - store queue data in internal,
1114 * - point eth_dev_data to internals
1115 * - and point eth_dev structure to new eth_dev_data structure
1117 internal = eth_dev->data->dev_private;
1118 internal->dev_name = strdup(name);
1119 if (internal->dev_name == NULL)
1121 internal->iface_name = strdup(iface_name);
1122 if (internal->iface_name == NULL)
1125 list->eth_dev = eth_dev;
1126 pthread_mutex_lock(&internal_list_lock);
1127 TAILQ_INSERT_TAIL(&internal_list, list, next);
1128 pthread_mutex_unlock(&internal_list_lock);
1130 rte_spinlock_init(&vring_state->lock);
1131 vring_states[eth_dev->data->port_id] = vring_state;
1133 /* We'll replace the 'data' originally allocated by eth_dev. So the
1134 * vhost PMD resources won't be shared between multi processes.
1136 rte_memcpy(data, eth_dev->data, sizeof(*data));
1137 eth_dev->data = data;
1139 data->nb_rx_queues = queues;
1140 data->nb_tx_queues = queues;
1141 internal->max_queues = queues;
1142 data->dev_link = pmd_link;
1143 data->mac_addrs = eth_addr;
1144 data->dev_flags = RTE_ETH_DEV_INTR_LSC;
1146 eth_dev->dev_ops = &ops;
1148 /* finally assign rx and tx ops */
1149 eth_dev->rx_pkt_burst = eth_vhost_rx;
1150 eth_dev->tx_pkt_burst = eth_vhost_tx;
1152 if (rte_vhost_driver_register(iface_name, flags))
1155 if (rte_vhost_driver_callback_register(iface_name, &vhost_ops) < 0) {
1156 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
1160 if (rte_vhost_driver_start(iface_name) < 0) {
1161 RTE_LOG(ERR, PMD, "Failed to start driver for %s\n",
1166 return data->port_id;
1170 free(internal->iface_name);
1171 free(internal->dev_name);
1173 rte_free(vring_state);
1176 rte_eth_dev_release_port(eth_dev);
1185 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1187 const char **iface_name = extra_args;
1192 *iface_name = value;
1198 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1200 uint16_t *n = extra_args;
1202 if (value == NULL || extra_args == NULL)
1205 *n = (uint16_t)strtoul(value, NULL, 0);
1206 if (*n == USHRT_MAX && errno == ERANGE)
1213 rte_pmd_vhost_probe(struct rte_vdev_device *dev)
1215 struct rte_kvargs *kvlist = NULL;
1220 int client_mode = 0;
1221 int dequeue_zero_copy = 0;
1222 int iommu_support = 0;
1224 RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n",
1225 rte_vdev_device_name(dev));
1227 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1231 if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1232 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1233 &open_iface, &iface_name);
1241 if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1242 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1243 &open_int, &queues);
1244 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1250 if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1251 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1252 &open_int, &client_mode);
1257 flags |= RTE_VHOST_USER_CLIENT;
1260 if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1261 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1262 &open_int, &dequeue_zero_copy);
1266 if (dequeue_zero_copy)
1267 flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1270 if (rte_kvargs_count(kvlist, ETH_VHOST_IOMMU_SUPPORT) == 1) {
1271 ret = rte_kvargs_process(kvlist, ETH_VHOST_IOMMU_SUPPORT,
1272 &open_int, &iommu_support);
1277 flags |= RTE_VHOST_USER_IOMMU_SUPPORT;
1280 if (dev->device.numa_node == SOCKET_ID_ANY)
1281 dev->device.numa_node = rte_socket_id();
1283 eth_dev_vhost_create(dev, iface_name, queues, dev->device.numa_node,
1287 rte_kvargs_free(kvlist);
1292 rte_pmd_vhost_remove(struct rte_vdev_device *dev)
1295 struct rte_eth_dev *eth_dev = NULL;
1297 name = rte_vdev_device_name(dev);
1298 RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1300 /* find an ethdev entry */
1301 eth_dev = rte_eth_dev_allocated(name);
1302 if (eth_dev == NULL)
1305 eth_dev_close(eth_dev);
1307 rte_free(vring_states[eth_dev->data->port_id]);
1308 vring_states[eth_dev->data->port_id] = NULL;
1310 rte_free(eth_dev->data);
1312 rte_eth_dev_release_port(eth_dev);
1317 static struct rte_vdev_driver pmd_vhost_drv = {
1318 .probe = rte_pmd_vhost_probe,
1319 .remove = rte_pmd_vhost_remove,
1322 RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1323 RTE_PMD_REGISTER_ALIAS(net_vhost, eth_vhost);
1324 RTE_PMD_REGISTER_PARAM_STRING(net_vhost,