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 #define ETH_VHOST_IFACE_ARG "iface"
49 #define ETH_VHOST_QUEUES_ARG "queues"
50 #define ETH_VHOST_CLIENT_ARG "client"
51 #define ETH_VHOST_DEQUEUE_ZERO_COPY "dequeue-zero-copy"
52 #define VHOST_MAX_PKT_BURST 32
54 static const char *valid_arguments[] = {
58 ETH_VHOST_DEQUEUE_ZERO_COPY,
62 static struct ether_addr base_eth_addr = {
73 enum vhost_xstats_pkts {
74 VHOST_UNDERSIZE_PKT = 0,
79 VHOST_512_TO_1023_PKT,
80 VHOST_1024_TO_1522_PKT,
81 VHOST_1523_TO_MAX_PKT,
86 VHOST_ERRORS_FRAGMENTED,
88 VHOST_UNKNOWN_PROTOCOL,
96 uint64_t xstats[VHOST_XSTATS_MAX];
101 rte_atomic32_t allow_queuing;
102 rte_atomic32_t while_queuing;
103 struct pmd_internal *internal;
104 struct rte_mempool *mb_pool;
106 uint16_t virtqueue_id;
107 struct vhost_stats stats;
110 struct pmd_internal {
111 rte_atomic32_t dev_attached;
115 rte_atomic32_t started;
118 struct internal_list {
119 TAILQ_ENTRY(internal_list) next;
120 struct rte_eth_dev *eth_dev;
123 TAILQ_HEAD(internal_list_head, internal_list);
124 static struct internal_list_head internal_list =
125 TAILQ_HEAD_INITIALIZER(internal_list);
127 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
129 static rte_atomic16_t nb_started_ports;
130 static pthread_t session_th;
132 static struct rte_eth_link pmd_link = {
134 .link_duplex = ETH_LINK_FULL_DUPLEX,
135 .link_status = ETH_LINK_DOWN
138 struct rte_vhost_vring_state {
141 bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
142 bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
144 unsigned int max_vring;
147 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
149 #define VHOST_XSTATS_NAME_SIZE 64
151 struct vhost_xstats_name_off {
152 char name[VHOST_XSTATS_NAME_SIZE];
156 /* [rx]_is prepended to the name string here */
157 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
159 offsetof(struct vhost_queue, stats.pkts)},
161 offsetof(struct vhost_queue, stats.bytes)},
163 offsetof(struct vhost_queue, stats.missed_pkts)},
164 {"broadcast_packets",
165 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
166 {"multicast_packets",
167 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
169 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
170 {"undersize_packets",
171 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
173 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
174 {"size_65_to_127_packets",
175 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
176 {"size_128_to_255_packets",
177 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
178 {"size_256_to_511_packets",
179 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
180 {"size_512_to_1023_packets",
181 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
182 {"size_1024_to_1522_packets",
183 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
184 {"size_1523_to_max_packets",
185 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
186 {"errors_with_bad_CRC",
187 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
188 {"fragmented_errors",
189 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
191 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
192 {"unknown_protos_packets",
193 offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
196 /* [tx]_ is prepended to the name string here */
197 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
199 offsetof(struct vhost_queue, stats.pkts)},
201 offsetof(struct vhost_queue, stats.bytes)},
203 offsetof(struct vhost_queue, stats.missed_pkts)},
204 {"broadcast_packets",
205 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
206 {"multicast_packets",
207 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
209 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
210 {"undersize_packets",
211 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
213 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
214 {"size_65_to_127_packets",
215 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
216 {"size_128_to_255_packets",
217 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
218 {"size_256_to_511_packets",
219 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
220 {"size_512_to_1023_packets",
221 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
222 {"size_1024_to_1522_packets",
223 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
224 {"size_1523_to_max_packets",
225 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
226 {"errors_with_bad_CRC",
227 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
230 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
231 sizeof(vhost_rxport_stat_strings[0]))
233 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
234 sizeof(vhost_txport_stat_strings[0]))
237 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
239 struct vhost_queue *vq = NULL;
242 for (i = 0; i < dev->data->nb_rx_queues; i++) {
243 vq = dev->data->rx_queues[i];
246 memset(&vq->stats, 0, sizeof(vq->stats));
248 for (i = 0; i < dev->data->nb_tx_queues; i++) {
249 vq = dev->data->tx_queues[i];
252 memset(&vq->stats, 0, sizeof(vq->stats));
257 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
258 struct rte_eth_xstat_name *xstats_names,
259 unsigned int limit __rte_unused)
263 int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
267 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
268 snprintf(xstats_names[count].name,
269 sizeof(xstats_names[count].name),
270 "rx_%s", vhost_rxport_stat_strings[t].name);
273 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
274 snprintf(xstats_names[count].name,
275 sizeof(xstats_names[count].name),
276 "tx_%s", vhost_txport_stat_strings[t].name);
283 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
288 unsigned int count = 0;
289 struct vhost_queue *vq = NULL;
290 unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
295 for (i = 0; i < dev->data->nb_rx_queues; i++) {
296 vq = dev->data->rx_queues[i];
299 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
300 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
301 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
303 for (i = 0; i < dev->data->nb_tx_queues; i++) {
304 vq = dev->data->tx_queues[i];
307 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
308 + vq->stats.missed_pkts
309 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
310 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
312 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
313 xstats[count].value = 0;
314 for (i = 0; i < dev->data->nb_rx_queues; i++) {
315 vq = dev->data->rx_queues[i];
318 xstats[count].value +=
319 *(uint64_t *)(((char *)vq)
320 + vhost_rxport_stat_strings[t].offset);
322 xstats[count].id = count;
325 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
326 xstats[count].value = 0;
327 for (i = 0; i < dev->data->nb_tx_queues; i++) {
328 vq = dev->data->tx_queues[i];
331 xstats[count].value +=
332 *(uint64_t *)(((char *)vq)
333 + vhost_txport_stat_strings[t].offset);
335 xstats[count].id = count;
342 vhost_count_multicast_broadcast(struct vhost_queue *vq,
343 struct rte_mbuf *mbuf)
345 struct ether_addr *ea = NULL;
346 struct vhost_stats *pstats = &vq->stats;
348 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
349 if (is_multicast_ether_addr(ea)) {
350 if (is_broadcast_ether_addr(ea))
351 pstats->xstats[VHOST_BROADCAST_PKT]++;
353 pstats->xstats[VHOST_MULTICAST_PKT]++;
358 vhost_update_packet_xstats(struct vhost_queue *vq,
359 struct rte_mbuf **bufs,
362 uint32_t pkt_len = 0;
365 struct vhost_stats *pstats = &vq->stats;
367 for (i = 0; i < count ; i++) {
368 pkt_len = bufs[i]->pkt_len;
370 pstats->xstats[VHOST_64_PKT]++;
371 } else if (pkt_len > 64 && pkt_len < 1024) {
372 index = (sizeof(pkt_len) * 8)
373 - __builtin_clz(pkt_len) - 5;
374 pstats->xstats[index]++;
377 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
378 else if (pkt_len <= 1522)
379 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
380 else if (pkt_len > 1522)
381 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
383 vhost_count_multicast_broadcast(vq, bufs[i]);
388 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
390 struct vhost_queue *r = q;
391 uint16_t i, nb_rx = 0;
392 uint16_t nb_receive = nb_bufs;
394 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
397 rte_atomic32_set(&r->while_queuing, 1);
399 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
402 /* Dequeue packets from guest TX queue */
405 uint16_t num = (uint16_t)RTE_MIN(nb_receive,
406 VHOST_MAX_PKT_BURST);
408 nb_pkts = rte_vhost_dequeue_burst(r->vid, r->virtqueue_id,
409 r->mb_pool, &bufs[nb_rx],
413 nb_receive -= nb_pkts;
418 r->stats.pkts += nb_rx;
420 for (i = 0; likely(i < nb_rx); i++) {
421 bufs[i]->port = r->port;
422 r->stats.bytes += bufs[i]->pkt_len;
425 vhost_update_packet_xstats(r, bufs, nb_rx);
428 rte_atomic32_set(&r->while_queuing, 0);
434 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
436 struct vhost_queue *r = q;
437 uint16_t i, nb_tx = 0;
438 uint16_t nb_send = nb_bufs;
440 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
443 rte_atomic32_set(&r->while_queuing, 1);
445 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
448 /* Enqueue packets to guest RX queue */
451 uint16_t num = (uint16_t)RTE_MIN(nb_send,
452 VHOST_MAX_PKT_BURST);
454 nb_pkts = rte_vhost_enqueue_burst(r->vid, r->virtqueue_id,
463 r->stats.pkts += nb_tx;
464 r->stats.missed_pkts += nb_bufs - nb_tx;
466 for (i = 0; likely(i < nb_tx); i++)
467 r->stats.bytes += bufs[i]->pkt_len;
469 vhost_update_packet_xstats(r, bufs, nb_tx);
471 /* According to RFC2863 page42 section ifHCOutMulticastPkts and
472 * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
473 * are increased when packets are not transmitted successfully.
475 for (i = nb_tx; i < nb_bufs; i++)
476 vhost_count_multicast_broadcast(r, bufs[i]);
478 for (i = 0; likely(i < nb_tx); i++)
479 rte_pktmbuf_free(bufs[i]);
481 rte_atomic32_set(&r->while_queuing, 0);
487 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
492 static inline struct internal_list *
493 find_internal_resource(char *ifname)
496 struct internal_list *list;
497 struct pmd_internal *internal;
502 pthread_mutex_lock(&internal_list_lock);
504 TAILQ_FOREACH(list, &internal_list, next) {
505 internal = list->eth_dev->data->dev_private;
506 if (!strcmp(internal->iface_name, ifname)) {
512 pthread_mutex_unlock(&internal_list_lock);
521 update_queuing_status(struct rte_eth_dev *dev)
523 struct pmd_internal *internal = dev->data->dev_private;
524 struct vhost_queue *vq;
526 int allow_queuing = 1;
528 if (rte_atomic32_read(&internal->started) == 0 ||
529 rte_atomic32_read(&internal->dev_attached) == 0)
532 /* Wait until rx/tx_pkt_burst stops accessing vhost device */
533 for (i = 0; i < dev->data->nb_rx_queues; i++) {
534 vq = dev->data->rx_queues[i];
537 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
538 while (rte_atomic32_read(&vq->while_queuing))
542 for (i = 0; i < dev->data->nb_tx_queues; i++) {
543 vq = dev->data->tx_queues[i];
546 rte_atomic32_set(&vq->allow_queuing, allow_queuing);
547 while (rte_atomic32_read(&vq->while_queuing))
555 struct rte_eth_dev *eth_dev;
556 struct internal_list *list;
557 struct pmd_internal *internal;
558 struct vhost_queue *vq;
560 char ifname[PATH_MAX];
561 #ifdef RTE_LIBRTE_VHOST_NUMA
565 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
566 list = find_internal_resource(ifname);
568 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
572 eth_dev = list->eth_dev;
573 internal = eth_dev->data->dev_private;
575 #ifdef RTE_LIBRTE_VHOST_NUMA
576 newnode = rte_vhost_get_numa_node(vid);
578 eth_dev->data->numa_node = newnode;
581 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
582 vq = eth_dev->data->rx_queues[i];
586 vq->internal = internal;
587 vq->port = eth_dev->data->port_id;
589 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
590 vq = eth_dev->data->tx_queues[i];
594 vq->internal = internal;
595 vq->port = eth_dev->data->port_id;
598 for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
599 rte_vhost_enable_guest_notification(vid, i, 0);
601 rte_vhost_get_mtu(vid, ð_dev->data->mtu);
603 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
605 rte_atomic32_set(&internal->dev_attached, 1);
606 update_queuing_status(eth_dev);
608 RTE_LOG(INFO, PMD, "New connection established\n");
610 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
616 destroy_device(int vid)
618 struct rte_eth_dev *eth_dev;
619 struct pmd_internal *internal;
620 struct vhost_queue *vq;
621 struct internal_list *list;
622 char ifname[PATH_MAX];
624 struct rte_vhost_vring_state *state;
626 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
627 list = find_internal_resource(ifname);
629 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
632 eth_dev = list->eth_dev;
633 internal = eth_dev->data->dev_private;
635 rte_atomic32_set(&internal->dev_attached, 0);
636 update_queuing_status(eth_dev);
638 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
640 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
641 vq = eth_dev->data->rx_queues[i];
646 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
647 vq = eth_dev->data->tx_queues[i];
653 state = vring_states[eth_dev->data->port_id];
654 rte_spinlock_lock(&state->lock);
655 for (i = 0; i <= state->max_vring; i++) {
656 state->cur[i] = false;
657 state->seen[i] = false;
659 state->max_vring = 0;
660 rte_spinlock_unlock(&state->lock);
662 RTE_LOG(INFO, PMD, "Connection closed\n");
664 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
668 vring_state_changed(int vid, uint16_t vring, int enable)
670 struct rte_vhost_vring_state *state;
671 struct rte_eth_dev *eth_dev;
672 struct internal_list *list;
673 char ifname[PATH_MAX];
675 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
676 list = find_internal_resource(ifname);
678 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
682 eth_dev = list->eth_dev;
684 state = vring_states[eth_dev->data->port_id];
685 rte_spinlock_lock(&state->lock);
686 state->cur[vring] = enable;
687 state->max_vring = RTE_MAX(vring, state->max_vring);
688 rte_spinlock_unlock(&state->lock);
690 RTE_LOG(INFO, PMD, "vring%u is %s\n",
691 vring, enable ? "enabled" : "disabled");
693 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
698 static struct virtio_net_device_ops vhost_ops = {
699 .new_device = new_device,
700 .destroy_device = destroy_device,
701 .vring_state_changed = vring_state_changed,
705 rte_eth_vhost_get_queue_event(uint8_t port_id,
706 struct rte_eth_vhost_queue_event *event)
708 struct rte_vhost_vring_state *state;
712 if (port_id >= RTE_MAX_ETHPORTS) {
713 RTE_LOG(ERR, PMD, "Invalid port id\n");
717 state = vring_states[port_id];
719 RTE_LOG(ERR, PMD, "Unused port\n");
723 rte_spinlock_lock(&state->lock);
724 for (i = 0; i <= state->max_vring; i++) {
725 idx = state->index++ % (state->max_vring + 1);
727 if (state->cur[idx] != state->seen[idx]) {
728 state->seen[idx] = state->cur[idx];
729 event->queue_id = idx / 2;
731 event->enable = state->cur[idx];
732 rte_spinlock_unlock(&state->lock);
736 rte_spinlock_unlock(&state->lock);
742 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
744 struct internal_list *list;
745 struct rte_eth_dev *eth_dev;
746 struct vhost_queue *vq;
749 if (!rte_eth_dev_is_valid_port(port_id))
752 pthread_mutex_lock(&internal_list_lock);
754 TAILQ_FOREACH(list, &internal_list, next) {
755 eth_dev = list->eth_dev;
756 if (eth_dev->data->port_id == port_id) {
757 vq = eth_dev->data->rx_queues[0];
765 pthread_mutex_unlock(&internal_list_lock);
771 vhost_driver_session(void *param __rte_unused)
773 /* start event handling */
774 rte_vhost_driver_session_start();
780 vhost_driver_session_start(void)
784 ret = pthread_create(&session_th,
785 NULL, vhost_driver_session, NULL);
787 RTE_LOG(ERR, PMD, "Can't create a thread\n");
793 vhost_driver_session_stop(void)
797 ret = pthread_cancel(session_th);
799 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
801 ret = pthread_join(session_th, NULL);
803 RTE_LOG(ERR, PMD, "Can't join the thread\n");
807 eth_dev_start(struct rte_eth_dev *dev)
809 struct pmd_internal *internal = dev->data->dev_private;
811 rte_atomic32_set(&internal->started, 1);
812 update_queuing_status(dev);
818 eth_dev_stop(struct rte_eth_dev *dev)
820 struct pmd_internal *internal = dev->data->dev_private;
822 rte_atomic32_set(&internal->started, 0);
823 update_queuing_status(dev);
827 eth_dev_close(struct rte_eth_dev *dev)
829 struct pmd_internal *internal;
830 struct internal_list *list;
832 internal = dev->data->dev_private;
836 rte_vhost_driver_unregister(internal->iface_name);
838 list = find_internal_resource(internal->iface_name);
842 pthread_mutex_lock(&internal_list_lock);
843 TAILQ_REMOVE(&internal_list, list, next);
844 pthread_mutex_unlock(&internal_list_lock);
847 free(internal->dev_name);
848 free(internal->iface_name);
853 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
854 uint16_t nb_rx_desc __rte_unused,
855 unsigned int socket_id,
856 const struct rte_eth_rxconf *rx_conf __rte_unused,
857 struct rte_mempool *mb_pool)
859 struct vhost_queue *vq;
861 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
862 RTE_CACHE_LINE_SIZE, socket_id);
864 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
868 vq->mb_pool = mb_pool;
869 vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
870 dev->data->rx_queues[rx_queue_id] = vq;
876 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
877 uint16_t nb_tx_desc __rte_unused,
878 unsigned int socket_id,
879 const struct rte_eth_txconf *tx_conf __rte_unused)
881 struct vhost_queue *vq;
883 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
884 RTE_CACHE_LINE_SIZE, socket_id);
886 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
890 vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
891 dev->data->tx_queues[tx_queue_id] = vq;
897 eth_dev_info(struct rte_eth_dev *dev,
898 struct rte_eth_dev_info *dev_info)
900 struct pmd_internal *internal;
902 internal = dev->data->dev_private;
903 if (internal == NULL) {
904 RTE_LOG(ERR, PMD, "Invalid device specified\n");
908 dev_info->max_mac_addrs = 1;
909 dev_info->max_rx_pktlen = (uint32_t)-1;
910 dev_info->max_rx_queues = internal->max_queues;
911 dev_info->max_tx_queues = internal->max_queues;
912 dev_info->min_rx_bufsize = 0;
916 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
919 unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
920 unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
921 struct vhost_queue *vq;
923 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
924 i < dev->data->nb_rx_queues; i++) {
925 if (dev->data->rx_queues[i] == NULL)
927 vq = dev->data->rx_queues[i];
928 stats->q_ipackets[i] = vq->stats.pkts;
929 rx_total += stats->q_ipackets[i];
931 stats->q_ibytes[i] = vq->stats.bytes;
932 rx_total_bytes += stats->q_ibytes[i];
935 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
936 i < dev->data->nb_tx_queues; i++) {
937 if (dev->data->tx_queues[i] == NULL)
939 vq = dev->data->tx_queues[i];
940 stats->q_opackets[i] = vq->stats.pkts;
941 tx_missed_total += vq->stats.missed_pkts;
942 tx_total += stats->q_opackets[i];
944 stats->q_obytes[i] = vq->stats.bytes;
945 tx_total_bytes += stats->q_obytes[i];
948 stats->ipackets = rx_total;
949 stats->opackets = tx_total;
950 stats->oerrors = tx_missed_total;
951 stats->ibytes = rx_total_bytes;
952 stats->obytes = tx_total_bytes;
956 eth_stats_reset(struct rte_eth_dev *dev)
958 struct vhost_queue *vq;
961 for (i = 0; i < dev->data->nb_rx_queues; i++) {
962 if (dev->data->rx_queues[i] == NULL)
964 vq = dev->data->rx_queues[i];
968 for (i = 0; i < dev->data->nb_tx_queues; i++) {
969 if (dev->data->tx_queues[i] == NULL)
971 vq = dev->data->tx_queues[i];
974 vq->stats.missed_pkts = 0;
979 eth_queue_release(void *q)
985 eth_tx_done_cleanup(void *txq __rte_unused, uint32_t free_cnt __rte_unused)
988 * vHost does not hang onto mbuf. eth_vhost_tx() copies packet data
989 * and releases mbuf, so nothing to cleanup.
995 eth_link_update(struct rte_eth_dev *dev __rte_unused,
996 int wait_to_complete __rte_unused)
1001 static const struct eth_dev_ops ops = {
1002 .dev_start = eth_dev_start,
1003 .dev_stop = eth_dev_stop,
1004 .dev_close = eth_dev_close,
1005 .dev_configure = eth_dev_configure,
1006 .dev_infos_get = eth_dev_info,
1007 .rx_queue_setup = eth_rx_queue_setup,
1008 .tx_queue_setup = eth_tx_queue_setup,
1009 .rx_queue_release = eth_queue_release,
1010 .tx_queue_release = eth_queue_release,
1011 .tx_done_cleanup = eth_tx_done_cleanup,
1012 .link_update = eth_link_update,
1013 .stats_get = eth_stats_get,
1014 .stats_reset = eth_stats_reset,
1015 .xstats_reset = vhost_dev_xstats_reset,
1016 .xstats_get = vhost_dev_xstats_get,
1017 .xstats_get_names = vhost_dev_xstats_get_names,
1020 static struct rte_vdev_driver pmd_vhost_drv;
1023 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
1024 const unsigned numa_node, uint64_t flags)
1026 struct rte_eth_dev_data *data = NULL;
1027 struct pmd_internal *internal = NULL;
1028 struct rte_eth_dev *eth_dev = NULL;
1029 struct ether_addr *eth_addr = NULL;
1030 struct rte_vhost_vring_state *vring_state = NULL;
1031 struct internal_list *list = NULL;
1033 RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
1036 /* now do all data allocation - for eth_dev structure, dummy pci driver
1037 * and internal (private) data
1039 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1043 internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
1044 if (internal == NULL)
1047 list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1051 /* reserve an ethdev entry */
1052 eth_dev = rte_eth_dev_allocate(name);
1053 if (eth_dev == NULL)
1056 eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1057 if (eth_addr == NULL)
1059 *eth_addr = base_eth_addr;
1060 eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1062 vring_state = rte_zmalloc_socket(name,
1063 sizeof(*vring_state), 0, numa_node);
1064 if (vring_state == NULL)
1067 /* now put it all together
1068 * - store queue data in internal,
1069 * - store numa_node info in ethdev data
1070 * - point eth_dev_data to internals
1071 * - and point eth_dev structure to new eth_dev_data structure
1073 internal->dev_name = strdup(name);
1074 if (internal->dev_name == NULL)
1076 internal->iface_name = strdup(iface_name);
1077 if (internal->iface_name == NULL)
1080 list->eth_dev = eth_dev;
1081 pthread_mutex_lock(&internal_list_lock);
1082 TAILQ_INSERT_TAIL(&internal_list, list, next);
1083 pthread_mutex_unlock(&internal_list_lock);
1085 rte_spinlock_init(&vring_state->lock);
1086 vring_states[eth_dev->data->port_id] = vring_state;
1088 data->dev_private = internal;
1089 data->port_id = eth_dev->data->port_id;
1090 memmove(data->name, eth_dev->data->name, sizeof(data->name));
1091 data->nb_rx_queues = queues;
1092 data->nb_tx_queues = queues;
1093 internal->max_queues = queues;
1094 data->dev_link = pmd_link;
1095 data->mac_addrs = eth_addr;
1097 /* We'll replace the 'data' originally allocated by eth_dev. So the
1098 * vhost PMD resources won't be shared between multi processes.
1100 eth_dev->data = data;
1101 eth_dev->dev_ops = &ops;
1102 eth_dev->driver = NULL;
1104 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1105 data->kdrv = RTE_KDRV_NONE;
1106 data->drv_name = pmd_vhost_drv.driver.name;
1107 data->numa_node = numa_node;
1109 /* finally assign rx and tx ops */
1110 eth_dev->rx_pkt_burst = eth_vhost_rx;
1111 eth_dev->tx_pkt_burst = eth_vhost_tx;
1113 if (rte_vhost_driver_register(iface_name, flags))
1116 if (rte_vhost_driver_callback_register(iface_name, &vhost_ops) < 0) {
1117 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
1121 /* We need only one message handling thread */
1122 if (rte_atomic16_add_return(&nb_started_ports, 1) == 1) {
1123 if (vhost_driver_session_start())
1127 return data->port_id;
1131 free(internal->dev_name);
1132 rte_free(vring_state);
1135 rte_eth_dev_release_port(eth_dev);
1144 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1146 const char **iface_name = extra_args;
1151 *iface_name = value;
1157 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1159 uint16_t *n = extra_args;
1161 if (value == NULL || extra_args == NULL)
1164 *n = (uint16_t)strtoul(value, NULL, 0);
1165 if (*n == USHRT_MAX && errno == ERANGE)
1172 rte_pmd_vhost_probe(const char *name, const char *params)
1174 struct rte_kvargs *kvlist = NULL;
1179 int client_mode = 0;
1180 int dequeue_zero_copy = 0;
1182 RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1184 kvlist = rte_kvargs_parse(params, valid_arguments);
1188 if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1189 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1190 &open_iface, &iface_name);
1198 if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1199 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1200 &open_int, &queues);
1201 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1207 if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1208 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1209 &open_int, &client_mode);
1214 flags |= RTE_VHOST_USER_CLIENT;
1217 if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1218 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1219 &open_int, &dequeue_zero_copy);
1223 if (dequeue_zero_copy)
1224 flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1227 eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1230 rte_kvargs_free(kvlist);
1235 rte_pmd_vhost_remove(const char *name)
1237 struct rte_eth_dev *eth_dev = NULL;
1240 RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1242 /* find an ethdev entry */
1243 eth_dev = rte_eth_dev_allocated(name);
1244 if (eth_dev == NULL)
1247 eth_dev_stop(eth_dev);
1249 eth_dev_close(eth_dev);
1251 if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
1252 vhost_driver_session_stop();
1254 rte_free(vring_states[eth_dev->data->port_id]);
1255 vring_states[eth_dev->data->port_id] = NULL;
1257 for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1258 rte_free(eth_dev->data->rx_queues[i]);
1259 for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1260 rte_free(eth_dev->data->tx_queues[i]);
1262 rte_free(eth_dev->data->mac_addrs);
1263 rte_free(eth_dev->data);
1265 rte_eth_dev_release_port(eth_dev);
1270 static struct rte_vdev_driver pmd_vhost_drv = {
1271 .probe = rte_pmd_vhost_probe,
1272 .remove = rte_pmd_vhost_remove,
1275 RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1276 RTE_PMD_REGISTER_ALIAS(net_vhost, eth_vhost);
1277 RTE_PMD_REGISTER_PARAM_STRING(net_vhost,