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.h>
39 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
42 #include <rte_kvargs.h>
43 #include <rte_virtio_net.h>
44 #include <rte_spinlock.h>
46 #include "rte_eth_vhost.h"
48 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
50 #define ETH_VHOST_IFACE_ARG "iface"
51 #define ETH_VHOST_QUEUES_ARG "queues"
52 #define ETH_VHOST_CLIENT_ARG "client"
53 #define ETH_VHOST_DEQUEUE_ZERO_COPY "dequeue-zero-copy"
54 #define VHOST_MAX_PKT_BURST 32
56 static const char *valid_arguments[] = {
60 ETH_VHOST_DEQUEUE_ZERO_COPY,
64 static struct ether_addr base_eth_addr = {
75 enum vhost_xstats_pkts {
76 VHOST_UNDERSIZE_PKT = 0,
81 VHOST_512_TO_1023_PKT,
82 VHOST_1024_TO_1522_PKT,
83 VHOST_1523_TO_MAX_PKT,
88 VHOST_ERRORS_FRAGMENTED,
90 VHOST_UNKNOWN_PROTOCOL,
98 uint64_t xstats[VHOST_XSTATS_MAX];
103 rte_atomic32_t allow_queuing;
104 rte_atomic32_t while_queuing;
105 struct pmd_internal *internal;
106 struct rte_mempool *mb_pool;
108 uint16_t virtqueue_id;
109 struct vhost_stats stats;
112 struct pmd_internal {
113 rte_atomic32_t dev_attached;
117 rte_atomic32_t started;
120 struct internal_list {
121 TAILQ_ENTRY(internal_list) next;
122 struct rte_eth_dev *eth_dev;
125 TAILQ_HEAD(internal_list_head, internal_list);
126 static struct internal_list_head internal_list =
127 TAILQ_HEAD_INITIALIZER(internal_list);
129 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
131 static rte_atomic16_t nb_started_ports;
132 static pthread_t session_th;
134 static struct rte_eth_link pmd_link = {
136 .link_duplex = ETH_LINK_FULL_DUPLEX,
137 .link_status = ETH_LINK_DOWN
140 struct rte_vhost_vring_state {
143 bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
144 bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
146 unsigned int max_vring;
149 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
151 #define VHOST_XSTATS_NAME_SIZE 64
153 struct vhost_xstats_name_off {
154 char name[VHOST_XSTATS_NAME_SIZE];
158 /* [rx]_is prepended to the name string here */
159 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
161 offsetof(struct vhost_queue, stats.pkts)},
163 offsetof(struct vhost_queue, stats.bytes)},
165 offsetof(struct vhost_queue, stats.missed_pkts)},
166 {"broadcast_packets",
167 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
168 {"multicast_packets",
169 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
171 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
172 {"undersize_packets",
173 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
175 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
176 {"size_65_to_127_packets",
177 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
178 {"size_128_to_255_packets",
179 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
180 {"size_256_to_511_packets",
181 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
182 {"size_512_to_1023_packets",
183 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
184 {"size_1024_to_1522_packets",
185 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
186 {"size_1523_to_max_packets",
187 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
188 {"errors_with_bad_CRC",
189 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
190 {"fragmented_errors",
191 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
193 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
194 {"unknown_protos_packets",
195 offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
198 /* [tx]_ is prepended to the name string here */
199 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
201 offsetof(struct vhost_queue, stats.pkts)},
203 offsetof(struct vhost_queue, stats.bytes)},
205 offsetof(struct vhost_queue, stats.missed_pkts)},
206 {"broadcast_packets",
207 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
208 {"multicast_packets",
209 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
211 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
212 {"undersize_packets",
213 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
215 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
216 {"size_65_to_127_packets",
217 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
218 {"size_128_to_255_packets",
219 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
220 {"size_256_to_511_packets",
221 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
222 {"size_512_to_1023_packets",
223 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
224 {"size_1024_to_1522_packets",
225 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
226 {"size_1523_to_max_packets",
227 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
228 {"errors_with_bad_CRC",
229 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
232 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
233 sizeof(vhost_rxport_stat_strings[0]))
235 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
236 sizeof(vhost_txport_stat_strings[0]))
239 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
241 struct vhost_queue *vq = NULL;
244 for (i = 0; i < dev->data->nb_rx_queues; i++) {
245 vq = dev->data->rx_queues[i];
248 memset(&vq->stats, 0, sizeof(vq->stats));
250 for (i = 0; i < dev->data->nb_tx_queues; i++) {
251 vq = dev->data->tx_queues[i];
254 memset(&vq->stats, 0, sizeof(vq->stats));
259 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
260 struct rte_eth_xstat_name *xstats_names,
261 unsigned int limit __rte_unused)
265 int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
269 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
270 snprintf(xstats_names[count].name,
271 sizeof(xstats_names[count].name),
272 "rx_%s", vhost_rxport_stat_strings[t].name);
275 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
276 snprintf(xstats_names[count].name,
277 sizeof(xstats_names[count].name),
278 "tx_%s", vhost_txport_stat_strings[t].name);
285 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
290 unsigned int count = 0;
291 struct vhost_queue *vq = NULL;
292 unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
297 for (i = 0; i < dev->data->nb_rx_queues; i++) {
298 vq = dev->data->rx_queues[i];
301 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
302 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
303 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
305 for (i = 0; i < dev->data->nb_tx_queues; i++) {
306 vq = dev->data->tx_queues[i];
309 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
310 + vq->stats.missed_pkts
311 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
312 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
314 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
315 xstats[count].value = 0;
316 for (i = 0; i < dev->data->nb_rx_queues; i++) {
317 vq = dev->data->rx_queues[i];
320 xstats[count].value +=
321 *(uint64_t *)(((char *)vq)
322 + vhost_rxport_stat_strings[t].offset);
324 xstats[count].id = count;
327 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
328 xstats[count].value = 0;
329 for (i = 0; i < dev->data->nb_tx_queues; i++) {
330 vq = dev->data->tx_queues[i];
333 xstats[count].value +=
334 *(uint64_t *)(((char *)vq)
335 + vhost_txport_stat_strings[t].offset);
337 xstats[count].id = count;
344 vhost_count_multicast_broadcast(struct vhost_queue *vq,
345 struct rte_mbuf *mbuf)
347 struct ether_addr *ea = NULL;
348 struct vhost_stats *pstats = &vq->stats;
350 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
351 if (is_multicast_ether_addr(ea)) {
352 if (is_broadcast_ether_addr(ea))
353 pstats->xstats[VHOST_BROADCAST_PKT]++;
355 pstats->xstats[VHOST_MULTICAST_PKT]++;
360 vhost_update_packet_xstats(struct vhost_queue *vq,
361 struct rte_mbuf **bufs,
364 uint32_t pkt_len = 0;
367 struct vhost_stats *pstats = &vq->stats;
369 for (i = 0; i < count ; i++) {
370 pkt_len = bufs[i]->pkt_len;
372 pstats->xstats[VHOST_64_PKT]++;
373 } else if (pkt_len > 64 && pkt_len < 1024) {
374 index = (sizeof(pkt_len) * 8)
375 - __builtin_clz(pkt_len) - 5;
376 pstats->xstats[index]++;
379 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
380 else if (pkt_len <= 1522)
381 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
382 else if (pkt_len > 1522)
383 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
385 vhost_count_multicast_broadcast(vq, bufs[i]);
390 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
392 struct vhost_queue *r = q;
393 uint16_t i, nb_rx = 0;
394 uint16_t nb_receive = nb_bufs;
396 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
399 rte_atomic32_set(&r->while_queuing, 1);
401 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
404 /* Dequeue packets from guest TX queue */
407 uint16_t num = (uint16_t)RTE_MIN(nb_receive,
408 VHOST_MAX_PKT_BURST);
410 nb_pkts = rte_vhost_dequeue_burst(r->vid, r->virtqueue_id,
411 r->mb_pool, &bufs[nb_rx],
415 nb_receive -= nb_pkts;
420 r->stats.pkts += nb_rx;
422 for (i = 0; likely(i < nb_rx); i++) {
423 bufs[i]->port = r->port;
424 r->stats.bytes += bufs[i]->pkt_len;
427 vhost_update_packet_xstats(r, bufs, nb_rx);
430 rte_atomic32_set(&r->while_queuing, 0);
436 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
438 struct vhost_queue *r = q;
439 uint16_t i, nb_tx = 0;
440 uint16_t nb_send = nb_bufs;
442 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
445 rte_atomic32_set(&r->while_queuing, 1);
447 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
450 /* Enqueue packets to guest RX queue */
453 uint16_t num = (uint16_t)RTE_MIN(nb_send,
454 VHOST_MAX_PKT_BURST);
456 nb_pkts = rte_vhost_enqueue_burst(r->vid, r->virtqueue_id,
465 r->stats.pkts += nb_tx;
466 r->stats.missed_pkts += nb_bufs - nb_tx;
468 for (i = 0; likely(i < nb_tx); i++)
469 r->stats.bytes += bufs[i]->pkt_len;
471 vhost_update_packet_xstats(r, bufs, nb_tx);
473 /* According to RFC2863 page42 section ifHCOutMulticastPkts and
474 * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
475 * are increased when packets are not transmitted successfully.
477 for (i = nb_tx; i < nb_bufs; i++)
478 vhost_count_multicast_broadcast(r, bufs[i]);
480 for (i = 0; likely(i < nb_tx); i++)
481 rte_pktmbuf_free(bufs[i]);
483 rte_atomic32_set(&r->while_queuing, 0);
489 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
494 static inline struct internal_list *
495 find_internal_resource(char *ifname)
498 struct internal_list *list;
499 struct pmd_internal *internal;
504 pthread_mutex_lock(&internal_list_lock);
506 TAILQ_FOREACH(list, &internal_list, next) {
507 internal = list->eth_dev->data->dev_private;
508 if (!strcmp(internal->iface_name, ifname)) {
514 pthread_mutex_unlock(&internal_list_lock);
523 update_queuing_status(struct rte_eth_dev *dev)
525 struct pmd_internal *internal = dev->data->dev_private;
526 struct vhost_queue *vq;
528 int allow_queuing = 1;
530 if (rte_atomic32_read(&internal->started) == 0 ||
531 rte_atomic32_read(&internal->dev_attached) == 0)
534 /* Wait until rx/tx_pkt_burst stops accessing vhost device */
535 for (i = 0; i < dev->data->nb_rx_queues; i++) {
536 vq = dev->data->rx_queues[i];
539 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
540 while (rte_atomic32_read(&vq->while_queuing))
544 for (i = 0; i < dev->data->nb_tx_queues; i++) {
545 vq = dev->data->tx_queues[i];
548 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
549 while (rte_atomic32_read(&vq->while_queuing))
557 struct rte_eth_dev *eth_dev;
558 struct internal_list *list;
559 struct pmd_internal *internal;
560 struct vhost_queue *vq;
562 char ifname[PATH_MAX];
563 #ifdef RTE_LIBRTE_VHOST_NUMA
567 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
568 list = find_internal_resource(ifname);
570 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
574 eth_dev = list->eth_dev;
575 internal = eth_dev->data->dev_private;
577 #ifdef RTE_LIBRTE_VHOST_NUMA
578 newnode = rte_vhost_get_numa_node(vid);
580 eth_dev->data->numa_node = newnode;
583 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
584 vq = eth_dev->data->rx_queues[i];
588 vq->internal = internal;
589 vq->port = eth_dev->data->port_id;
591 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
592 vq = eth_dev->data->tx_queues[i];
596 vq->internal = internal;
597 vq->port = eth_dev->data->port_id;
600 for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
601 rte_vhost_enable_guest_notification(vid, i, 0);
603 rte_vhost_get_mtu(vid, ð_dev->data->mtu);
605 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
607 rte_atomic32_set(&internal->dev_attached, 1);
608 update_queuing_status(eth_dev);
610 RTE_LOG(INFO, PMD, "New connection established\n");
612 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
618 destroy_device(int vid)
620 struct rte_eth_dev *eth_dev;
621 struct pmd_internal *internal;
622 struct vhost_queue *vq;
623 struct internal_list *list;
624 char ifname[PATH_MAX];
626 struct rte_vhost_vring_state *state;
628 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
629 list = find_internal_resource(ifname);
631 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
634 eth_dev = list->eth_dev;
635 internal = eth_dev->data->dev_private;
637 rte_atomic32_set(&internal->dev_attached, 0);
638 update_queuing_status(eth_dev);
640 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
642 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
643 vq = eth_dev->data->rx_queues[i];
648 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
649 vq = eth_dev->data->tx_queues[i];
655 state = vring_states[eth_dev->data->port_id];
656 rte_spinlock_lock(&state->lock);
657 for (i = 0; i <= state->max_vring; i++) {
658 state->cur[i] = false;
659 state->seen[i] = false;
661 state->max_vring = 0;
662 rte_spinlock_unlock(&state->lock);
664 RTE_LOG(INFO, PMD, "Connection closed\n");
666 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
670 vring_state_changed(int vid, uint16_t vring, int enable)
672 struct rte_vhost_vring_state *state;
673 struct rte_eth_dev *eth_dev;
674 struct internal_list *list;
675 char ifname[PATH_MAX];
677 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
678 list = find_internal_resource(ifname);
680 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
684 eth_dev = list->eth_dev;
686 state = vring_states[eth_dev->data->port_id];
687 rte_spinlock_lock(&state->lock);
688 state->cur[vring] = enable;
689 state->max_vring = RTE_MAX(vring, state->max_vring);
690 rte_spinlock_unlock(&state->lock);
692 RTE_LOG(INFO, PMD, "vring%u is %s\n",
693 vring, enable ? "enabled" : "disabled");
695 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
700 static struct vhost_device_ops vhost_ops = {
701 .new_device = new_device,
702 .destroy_device = destroy_device,
703 .vring_state_changed = vring_state_changed,
707 rte_eth_vhost_get_queue_event(uint8_t port_id,
708 struct rte_eth_vhost_queue_event *event)
710 struct rte_vhost_vring_state *state;
714 if (port_id >= RTE_MAX_ETHPORTS) {
715 RTE_LOG(ERR, PMD, "Invalid port id\n");
719 state = vring_states[port_id];
721 RTE_LOG(ERR, PMD, "Unused port\n");
725 rte_spinlock_lock(&state->lock);
726 for (i = 0; i <= state->max_vring; i++) {
727 idx = state->index++ % (state->max_vring + 1);
729 if (state->cur[idx] != state->seen[idx]) {
730 state->seen[idx] = state->cur[idx];
731 event->queue_id = idx / 2;
733 event->enable = state->cur[idx];
734 rte_spinlock_unlock(&state->lock);
738 rte_spinlock_unlock(&state->lock);
744 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
746 struct internal_list *list;
747 struct rte_eth_dev *eth_dev;
748 struct vhost_queue *vq;
751 if (!rte_eth_dev_is_valid_port(port_id))
754 pthread_mutex_lock(&internal_list_lock);
756 TAILQ_FOREACH(list, &internal_list, next) {
757 eth_dev = list->eth_dev;
758 if (eth_dev->data->port_id == port_id) {
759 vq = eth_dev->data->rx_queues[0];
767 pthread_mutex_unlock(&internal_list_lock);
773 vhost_driver_session(void *param __rte_unused)
775 /* start event handling */
776 rte_vhost_driver_session_start();
782 vhost_driver_session_start(void)
786 ret = pthread_create(&session_th,
787 NULL, vhost_driver_session, NULL);
789 RTE_LOG(ERR, PMD, "Can't create a thread\n");
795 vhost_driver_session_stop(void)
799 ret = pthread_cancel(session_th);
801 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
803 ret = pthread_join(session_th, NULL);
805 RTE_LOG(ERR, PMD, "Can't join the thread\n");
809 eth_dev_start(struct rte_eth_dev *dev)
811 struct pmd_internal *internal = dev->data->dev_private;
813 rte_atomic32_set(&internal->started, 1);
814 update_queuing_status(dev);
820 eth_dev_stop(struct rte_eth_dev *dev)
822 struct pmd_internal *internal = dev->data->dev_private;
824 rte_atomic32_set(&internal->started, 0);
825 update_queuing_status(dev);
829 eth_dev_close(struct rte_eth_dev *dev)
831 struct pmd_internal *internal;
832 struct internal_list *list;
834 internal = dev->data->dev_private;
838 rte_vhost_driver_unregister(internal->iface_name);
840 list = find_internal_resource(internal->iface_name);
844 pthread_mutex_lock(&internal_list_lock);
845 TAILQ_REMOVE(&internal_list, list, next);
846 pthread_mutex_unlock(&internal_list_lock);
849 free(internal->dev_name);
850 free(internal->iface_name);
855 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
856 uint16_t nb_rx_desc __rte_unused,
857 unsigned int socket_id,
858 const struct rte_eth_rxconf *rx_conf __rte_unused,
859 struct rte_mempool *mb_pool)
861 struct vhost_queue *vq;
863 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
864 RTE_CACHE_LINE_SIZE, socket_id);
866 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
870 vq->mb_pool = mb_pool;
871 vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
872 dev->data->rx_queues[rx_queue_id] = vq;
878 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
879 uint16_t nb_tx_desc __rte_unused,
880 unsigned int socket_id,
881 const struct rte_eth_txconf *tx_conf __rte_unused)
883 struct vhost_queue *vq;
885 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
886 RTE_CACHE_LINE_SIZE, socket_id);
888 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
892 vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
893 dev->data->tx_queues[tx_queue_id] = vq;
899 eth_dev_info(struct rte_eth_dev *dev,
900 struct rte_eth_dev_info *dev_info)
902 struct pmd_internal *internal;
904 internal = dev->data->dev_private;
905 if (internal == NULL) {
906 RTE_LOG(ERR, PMD, "Invalid device specified\n");
910 dev_info->max_mac_addrs = 1;
911 dev_info->max_rx_pktlen = (uint32_t)-1;
912 dev_info->max_rx_queues = internal->max_queues;
913 dev_info->max_tx_queues = internal->max_queues;
914 dev_info->min_rx_bufsize = 0;
918 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
921 unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
922 unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
923 struct vhost_queue *vq;
925 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
926 i < dev->data->nb_rx_queues; i++) {
927 if (dev->data->rx_queues[i] == NULL)
929 vq = dev->data->rx_queues[i];
930 stats->q_ipackets[i] = vq->stats.pkts;
931 rx_total += stats->q_ipackets[i];
933 stats->q_ibytes[i] = vq->stats.bytes;
934 rx_total_bytes += stats->q_ibytes[i];
937 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
938 i < dev->data->nb_tx_queues; i++) {
939 if (dev->data->tx_queues[i] == NULL)
941 vq = dev->data->tx_queues[i];
942 stats->q_opackets[i] = vq->stats.pkts;
943 tx_missed_total += vq->stats.missed_pkts;
944 tx_total += stats->q_opackets[i];
946 stats->q_obytes[i] = vq->stats.bytes;
947 tx_total_bytes += stats->q_obytes[i];
950 stats->ipackets = rx_total;
951 stats->opackets = tx_total;
952 stats->oerrors = tx_missed_total;
953 stats->ibytes = rx_total_bytes;
954 stats->obytes = tx_total_bytes;
958 eth_stats_reset(struct rte_eth_dev *dev)
960 struct vhost_queue *vq;
963 for (i = 0; i < dev->data->nb_rx_queues; i++) {
964 if (dev->data->rx_queues[i] == NULL)
966 vq = dev->data->rx_queues[i];
970 for (i = 0; i < dev->data->nb_tx_queues; i++) {
971 if (dev->data->tx_queues[i] == NULL)
973 vq = dev->data->tx_queues[i];
976 vq->stats.missed_pkts = 0;
981 eth_queue_release(void *q)
987 eth_tx_done_cleanup(void *txq __rte_unused, uint32_t free_cnt __rte_unused)
990 * vHost does not hang onto mbuf. eth_vhost_tx() copies packet data
991 * and releases mbuf, so nothing to cleanup.
997 eth_link_update(struct rte_eth_dev *dev __rte_unused,
998 int wait_to_complete __rte_unused)
1003 static const struct eth_dev_ops ops = {
1004 .dev_start = eth_dev_start,
1005 .dev_stop = eth_dev_stop,
1006 .dev_close = eth_dev_close,
1007 .dev_configure = eth_dev_configure,
1008 .dev_infos_get = eth_dev_info,
1009 .rx_queue_setup = eth_rx_queue_setup,
1010 .tx_queue_setup = eth_tx_queue_setup,
1011 .rx_queue_release = eth_queue_release,
1012 .tx_queue_release = eth_queue_release,
1013 .tx_done_cleanup = eth_tx_done_cleanup,
1014 .link_update = eth_link_update,
1015 .stats_get = eth_stats_get,
1016 .stats_reset = eth_stats_reset,
1017 .xstats_reset = vhost_dev_xstats_reset,
1018 .xstats_get = vhost_dev_xstats_get,
1019 .xstats_get_names = vhost_dev_xstats_get_names,
1022 static struct rte_vdev_driver pmd_vhost_drv;
1025 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
1026 const unsigned numa_node, uint64_t flags)
1028 struct rte_eth_dev_data *data = NULL;
1029 struct pmd_internal *internal = NULL;
1030 struct rte_eth_dev *eth_dev = NULL;
1031 struct ether_addr *eth_addr = NULL;
1032 struct rte_vhost_vring_state *vring_state = NULL;
1033 struct internal_list *list = NULL;
1035 RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
1038 /* now do all data allocation - for eth_dev structure, dummy pci driver
1039 * and internal (private) data
1041 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1045 internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
1046 if (internal == NULL)
1049 list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1053 /* reserve an ethdev entry */
1054 eth_dev = rte_eth_dev_allocate(name);
1055 if (eth_dev == NULL)
1058 eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1059 if (eth_addr == NULL)
1061 *eth_addr = base_eth_addr;
1062 eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1064 vring_state = rte_zmalloc_socket(name,
1065 sizeof(*vring_state), 0, numa_node);
1066 if (vring_state == NULL)
1069 /* now put it all together
1070 * - store queue data in internal,
1071 * - store numa_node info in ethdev data
1072 * - point eth_dev_data to internals
1073 * - and point eth_dev structure to new eth_dev_data structure
1075 internal->dev_name = strdup(name);
1076 if (internal->dev_name == NULL)
1078 internal->iface_name = strdup(iface_name);
1079 if (internal->iface_name == NULL)
1082 list->eth_dev = eth_dev;
1083 pthread_mutex_lock(&internal_list_lock);
1084 TAILQ_INSERT_TAIL(&internal_list, list, next);
1085 pthread_mutex_unlock(&internal_list_lock);
1087 rte_spinlock_init(&vring_state->lock);
1088 vring_states[eth_dev->data->port_id] = vring_state;
1090 data->dev_private = internal;
1091 data->port_id = eth_dev->data->port_id;
1092 memmove(data->name, eth_dev->data->name, sizeof(data->name));
1093 data->nb_rx_queues = queues;
1094 data->nb_tx_queues = queues;
1095 internal->max_queues = queues;
1096 data->dev_link = pmd_link;
1097 data->mac_addrs = eth_addr;
1099 /* We'll replace the 'data' originally allocated by eth_dev. So the
1100 * vhost PMD resources won't be shared between multi processes.
1102 eth_dev->data = data;
1103 eth_dev->dev_ops = &ops;
1104 eth_dev->driver = NULL;
1106 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1107 data->kdrv = RTE_KDRV_NONE;
1108 data->drv_name = pmd_vhost_drv.driver.name;
1109 data->numa_node = numa_node;
1111 /* finally assign rx and tx ops */
1112 eth_dev->rx_pkt_burst = eth_vhost_rx;
1113 eth_dev->tx_pkt_burst = eth_vhost_tx;
1115 if (rte_vhost_driver_register(iface_name, flags))
1118 if (rte_vhost_driver_callback_register(iface_name, &vhost_ops) < 0) {
1119 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
1123 /* We need only one message handling thread */
1124 if (rte_atomic16_add_return(&nb_started_ports, 1) == 1) {
1125 if (vhost_driver_session_start())
1129 return data->port_id;
1133 free(internal->dev_name);
1134 rte_free(vring_state);
1137 rte_eth_dev_release_port(eth_dev);
1146 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1148 const char **iface_name = extra_args;
1153 *iface_name = value;
1159 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1161 uint16_t *n = extra_args;
1163 if (value == NULL || extra_args == NULL)
1166 *n = (uint16_t)strtoul(value, NULL, 0);
1167 if (*n == USHRT_MAX && errno == ERANGE)
1174 rte_pmd_vhost_probe(const char *name, const char *params)
1176 struct rte_kvargs *kvlist = NULL;
1181 int client_mode = 0;
1182 int dequeue_zero_copy = 0;
1184 RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1186 kvlist = rte_kvargs_parse(params, valid_arguments);
1190 if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1191 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1192 &open_iface, &iface_name);
1200 if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1201 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1202 &open_int, &queues);
1203 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1209 if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1210 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1211 &open_int, &client_mode);
1216 flags |= RTE_VHOST_USER_CLIENT;
1219 if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1220 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1221 &open_int, &dequeue_zero_copy);
1225 if (dequeue_zero_copy)
1226 flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1229 eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1232 rte_kvargs_free(kvlist);
1237 rte_pmd_vhost_remove(const char *name)
1239 struct rte_eth_dev *eth_dev = NULL;
1242 RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1244 /* find an ethdev entry */
1245 eth_dev = rte_eth_dev_allocated(name);
1246 if (eth_dev == NULL)
1249 eth_dev_stop(eth_dev);
1251 eth_dev_close(eth_dev);
1253 if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
1254 vhost_driver_session_stop();
1256 rte_free(vring_states[eth_dev->data->port_id]);
1257 vring_states[eth_dev->data->port_id] = NULL;
1259 for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1260 rte_free(eth_dev->data->rx_queues[i]);
1261 for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1262 rte_free(eth_dev->data->tx_queues[i]);
1264 rte_free(eth_dev->data->mac_addrs);
1265 rte_free(eth_dev->data);
1267 rte_eth_dev_release_port(eth_dev);
1272 static struct rte_vdev_driver pmd_vhost_drv = {
1273 .probe = rte_pmd_vhost_probe,
1274 .remove = rte_pmd_vhost_remove,
1277 RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1278 RTE_PMD_REGISTER_ALIAS(net_vhost, eth_vhost);
1279 RTE_PMD_REGISTER_PARAM_STRING(net_vhost,