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.
36 #ifdef RTE_LIBRTE_VHOST_NUMA
41 #include <rte_ethdev.h>
42 #include <rte_malloc.h>
43 #include <rte_memcpy.h>
45 #include <rte_kvargs.h>
46 #include <rte_virtio_net.h>
47 #include <rte_spinlock.h>
49 #include "rte_eth_vhost.h"
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"
56 static const char *drivername = "VHOST PMD";
58 static const char *valid_arguments[] = {
62 ETH_VHOST_DEQUEUE_ZERO_COPY,
66 static struct ether_addr base_eth_addr = {
77 enum vhost_xstats_pkts {
78 VHOST_UNDERSIZE_PKT = 0,
83 VHOST_512_TO_1023_PKT,
84 VHOST_1024_TO_1522_PKT,
85 VHOST_1523_TO_MAX_PKT,
90 VHOST_ERRORS_FRAGMENTED,
92 VHOST_UNKNOWN_PROTOCOL,
100 uint64_t xstats[VHOST_XSTATS_MAX];
105 rte_atomic32_t allow_queuing;
106 rte_atomic32_t while_queuing;
107 struct pmd_internal *internal;
108 struct rte_mempool *mb_pool;
110 uint16_t virtqueue_id;
111 struct vhost_stats stats;
114 struct pmd_internal {
120 volatile uint16_t once;
123 struct internal_list {
124 TAILQ_ENTRY(internal_list) next;
125 struct rte_eth_dev *eth_dev;
128 TAILQ_HEAD(internal_list_head, internal_list);
129 static struct internal_list_head internal_list =
130 TAILQ_HEAD_INITIALIZER(internal_list);
132 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
134 static rte_atomic16_t nb_started_ports;
135 static pthread_t session_th;
137 static struct rte_eth_link pmd_link = {
139 .link_duplex = ETH_LINK_FULL_DUPLEX,
140 .link_status = ETH_LINK_DOWN
143 struct rte_vhost_vring_state {
146 bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
147 bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
149 unsigned int max_vring;
152 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
154 #define VHOST_XSTATS_NAME_SIZE 64
156 struct vhost_xstats_name_off {
157 char name[VHOST_XSTATS_NAME_SIZE];
161 /* [rx]_is prepended to the name string here */
162 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
164 offsetof(struct vhost_queue, stats.pkts)},
166 offsetof(struct vhost_queue, stats.bytes)},
168 offsetof(struct vhost_queue, stats.missed_pkts)},
169 {"broadcast_packets",
170 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
171 {"multicast_packets",
172 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
174 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
175 {"undersize_packets",
176 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
178 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
179 {"size_65_to_127_packets",
180 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
181 {"size_128_to_255_packets",
182 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
183 {"size_256_to_511_packets",
184 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
185 {"size_512_to_1023_packets",
186 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
187 {"size_1024_to_1522_packets",
188 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
189 {"size_1523_to_max_packets",
190 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
191 {"errors_with_bad_CRC",
192 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
193 {"fragmented_errors",
194 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
196 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
197 {"unknown_protos_packets",
198 offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
201 /* [tx]_ is prepended to the name string here */
202 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
204 offsetof(struct vhost_queue, stats.pkts)},
206 offsetof(struct vhost_queue, stats.bytes)},
208 offsetof(struct vhost_queue, stats.missed_pkts)},
209 {"broadcast_packets",
210 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
211 {"multicast_packets",
212 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
214 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
215 {"undersize_packets",
216 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
218 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
219 {"size_65_to_127_packets",
220 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
221 {"size_128_to_255_packets",
222 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
223 {"size_256_to_511_packets",
224 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
225 {"size_512_to_1023_packets",
226 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
227 {"size_1024_to_1522_packets",
228 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
229 {"size_1523_to_max_packets",
230 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
231 {"errors_with_bad_CRC",
232 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
235 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
236 sizeof(vhost_rxport_stat_strings[0]))
238 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
239 sizeof(vhost_txport_stat_strings[0]))
242 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
244 struct vhost_queue *vq = NULL;
247 for (i = 0; i < dev->data->nb_rx_queues; i++) {
248 vq = dev->data->rx_queues[i];
251 memset(&vq->stats, 0, sizeof(vq->stats));
253 for (i = 0; i < dev->data->nb_tx_queues; i++) {
254 vq = dev->data->tx_queues[i];
257 memset(&vq->stats, 0, sizeof(vq->stats));
262 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
263 struct rte_eth_xstat_name *xstats_names,
264 unsigned int limit __rte_unused)
268 int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
272 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
273 snprintf(xstats_names[count].name,
274 sizeof(xstats_names[count].name),
275 "rx_%s", vhost_rxport_stat_strings[t].name);
278 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
279 snprintf(xstats_names[count].name,
280 sizeof(xstats_names[count].name),
281 "tx_%s", vhost_txport_stat_strings[t].name);
288 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
293 unsigned int count = 0;
294 struct vhost_queue *vq = NULL;
295 unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
300 for (i = 0; i < dev->data->nb_rx_queues; i++) {
301 vq = dev->data->rx_queues[i];
304 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
305 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
306 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
308 for (i = 0; i < dev->data->nb_tx_queues; i++) {
309 vq = dev->data->tx_queues[i];
312 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
313 + vq->stats.missed_pkts
314 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
315 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
317 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
318 xstats[count].value = 0;
319 for (i = 0; i < dev->data->nb_rx_queues; i++) {
320 vq = dev->data->rx_queues[i];
323 xstats[count].value +=
324 *(uint64_t *)(((char *)vq)
325 + vhost_rxport_stat_strings[t].offset);
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);
345 vhost_count_multicast_broadcast(struct vhost_queue *vq,
346 struct rte_mbuf *mbuf)
348 struct ether_addr *ea = NULL;
349 struct vhost_stats *pstats = &vq->stats;
351 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
352 if (is_multicast_ether_addr(ea)) {
353 if (is_broadcast_ether_addr(ea))
354 pstats->xstats[VHOST_BROADCAST_PKT]++;
356 pstats->xstats[VHOST_MULTICAST_PKT]++;
361 vhost_update_packet_xstats(struct vhost_queue *vq,
362 struct rte_mbuf **bufs,
365 uint32_t pkt_len = 0;
368 struct vhost_stats *pstats = &vq->stats;
370 for (i = 0; i < count ; i++) {
371 pkt_len = bufs[i]->pkt_len;
373 pstats->xstats[VHOST_64_PKT]++;
374 } else if (pkt_len > 64 && pkt_len < 1024) {
375 index = (sizeof(pkt_len) * 8)
376 - __builtin_clz(pkt_len) - 5;
377 pstats->xstats[index]++;
380 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
381 else if (pkt_len <= 1522)
382 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
383 else if (pkt_len > 1522)
384 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
386 vhost_count_multicast_broadcast(vq, bufs[i]);
391 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
393 struct vhost_queue *r = q;
394 uint16_t i, nb_rx = 0;
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 */
405 nb_rx = rte_vhost_dequeue_burst(r->vid,
406 r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
408 r->stats.pkts += nb_rx;
410 for (i = 0; likely(i < nb_rx); i++) {
411 bufs[i]->port = r->port;
412 r->stats.bytes += bufs[i]->pkt_len;
415 vhost_update_packet_xstats(r, bufs, nb_rx);
418 rte_atomic32_set(&r->while_queuing, 0);
424 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
426 struct vhost_queue *r = q;
427 uint16_t i, nb_tx = 0;
429 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
432 rte_atomic32_set(&r->while_queuing, 1);
434 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
437 /* Enqueue packets to guest RX queue */
438 nb_tx = rte_vhost_enqueue_burst(r->vid,
439 r->virtqueue_id, bufs, nb_bufs);
441 r->stats.pkts += nb_tx;
442 r->stats.missed_pkts += nb_bufs - nb_tx;
444 for (i = 0; likely(i < nb_tx); i++)
445 r->stats.bytes += bufs[i]->pkt_len;
447 vhost_update_packet_xstats(r, bufs, nb_tx);
449 /* According to RFC2863 page42 section ifHCOutMulticastPkts and
450 * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
451 * are increased when packets are not transmitted successfully.
453 for (i = nb_tx; i < nb_bufs; i++)
454 vhost_count_multicast_broadcast(r, bufs[i]);
456 for (i = 0; likely(i < nb_tx); i++)
457 rte_pktmbuf_free(bufs[i]);
459 rte_atomic32_set(&r->while_queuing, 0);
465 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
470 static inline struct internal_list *
471 find_internal_resource(char *ifname)
474 struct internal_list *list;
475 struct pmd_internal *internal;
480 pthread_mutex_lock(&internal_list_lock);
482 TAILQ_FOREACH(list, &internal_list, next) {
483 internal = list->eth_dev->data->dev_private;
484 if (!strcmp(internal->iface_name, ifname)) {
490 pthread_mutex_unlock(&internal_list_lock);
501 struct rte_eth_dev *eth_dev;
502 struct internal_list *list;
503 struct pmd_internal *internal;
504 struct vhost_queue *vq;
506 char ifname[PATH_MAX];
507 #ifdef RTE_LIBRTE_VHOST_NUMA
511 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
512 list = find_internal_resource(ifname);
514 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
518 eth_dev = list->eth_dev;
519 internal = eth_dev->data->dev_private;
521 #ifdef RTE_LIBRTE_VHOST_NUMA
522 newnode = rte_vhost_get_numa_node(vid);
524 eth_dev->data->numa_node = newnode;
527 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
528 vq = eth_dev->data->rx_queues[i];
532 vq->internal = internal;
533 vq->port = eth_dev->data->port_id;
535 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
536 vq = eth_dev->data->tx_queues[i];
540 vq->internal = internal;
541 vq->port = eth_dev->data->port_id;
544 for (i = 0; i < rte_vhost_get_queue_num(vid) * VIRTIO_QNUM; i++)
545 rte_vhost_enable_guest_notification(vid, i, 0);
547 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
549 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
550 vq = eth_dev->data->rx_queues[i];
553 rte_atomic32_set(&vq->allow_queuing, 1);
555 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
556 vq = eth_dev->data->tx_queues[i];
559 rte_atomic32_set(&vq->allow_queuing, 1);
562 RTE_LOG(INFO, PMD, "New connection established\n");
564 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
570 destroy_device(int vid)
572 struct rte_eth_dev *eth_dev;
573 struct vhost_queue *vq;
574 struct internal_list *list;
575 char ifname[PATH_MAX];
577 struct rte_vhost_vring_state *state;
579 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
580 list = find_internal_resource(ifname);
582 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
585 eth_dev = list->eth_dev;
587 /* Wait until rx/tx_pkt_burst stops accessing vhost device */
588 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
589 vq = eth_dev->data->rx_queues[i];
592 rte_atomic32_set(&vq->allow_queuing, 0);
593 while (rte_atomic32_read(&vq->while_queuing))
596 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
597 vq = eth_dev->data->tx_queues[i];
600 rte_atomic32_set(&vq->allow_queuing, 0);
601 while (rte_atomic32_read(&vq->while_queuing))
605 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
607 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
608 vq = eth_dev->data->rx_queues[i];
613 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
614 vq = eth_dev->data->tx_queues[i];
620 state = vring_states[eth_dev->data->port_id];
621 rte_spinlock_lock(&state->lock);
622 for (i = 0; i <= state->max_vring; i++) {
623 state->cur[i] = false;
624 state->seen[i] = false;
626 state->max_vring = 0;
627 rte_spinlock_unlock(&state->lock);
629 RTE_LOG(INFO, PMD, "Connection closed\n");
631 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
635 vring_state_changed(int vid, uint16_t vring, int enable)
637 struct rte_vhost_vring_state *state;
638 struct rte_eth_dev *eth_dev;
639 struct internal_list *list;
640 char ifname[PATH_MAX];
642 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
643 list = find_internal_resource(ifname);
645 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
649 eth_dev = list->eth_dev;
651 state = vring_states[eth_dev->data->port_id];
652 rte_spinlock_lock(&state->lock);
653 state->cur[vring] = enable;
654 state->max_vring = RTE_MAX(vring, state->max_vring);
655 rte_spinlock_unlock(&state->lock);
657 RTE_LOG(INFO, PMD, "vring%u is %s\n",
658 vring, enable ? "enabled" : "disabled");
660 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE);
666 rte_eth_vhost_get_queue_event(uint8_t port_id,
667 struct rte_eth_vhost_queue_event *event)
669 struct rte_vhost_vring_state *state;
673 if (port_id >= RTE_MAX_ETHPORTS) {
674 RTE_LOG(ERR, PMD, "Invalid port id\n");
678 state = vring_states[port_id];
680 RTE_LOG(ERR, PMD, "Unused port\n");
684 rte_spinlock_lock(&state->lock);
685 for (i = 0; i <= state->max_vring; i++) {
686 idx = state->index++ % (state->max_vring + 1);
688 if (state->cur[idx] != state->seen[idx]) {
689 state->seen[idx] = state->cur[idx];
690 event->queue_id = idx / 2;
692 event->enable = state->cur[idx];
693 rte_spinlock_unlock(&state->lock);
697 rte_spinlock_unlock(&state->lock);
703 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
705 struct internal_list *list;
706 struct rte_eth_dev *eth_dev;
707 struct vhost_queue *vq;
710 if (!rte_eth_dev_is_valid_port(port_id))
713 pthread_mutex_lock(&internal_list_lock);
715 TAILQ_FOREACH(list, &internal_list, next) {
716 eth_dev = list->eth_dev;
717 if (eth_dev->data->port_id == port_id) {
718 vq = eth_dev->data->rx_queues[0];
726 pthread_mutex_unlock(&internal_list_lock);
732 vhost_driver_session(void *param __rte_unused)
734 static struct virtio_net_device_ops vhost_ops;
736 /* set vhost arguments */
737 vhost_ops.new_device = new_device;
738 vhost_ops.destroy_device = destroy_device;
739 vhost_ops.vring_state_changed = vring_state_changed;
740 if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
741 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
743 /* start event handling */
744 rte_vhost_driver_session_start();
750 vhost_driver_session_start(void)
754 ret = pthread_create(&session_th,
755 NULL, vhost_driver_session, NULL);
757 RTE_LOG(ERR, PMD, "Can't create a thread\n");
763 vhost_driver_session_stop(void)
767 ret = pthread_cancel(session_th);
769 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
771 ret = pthread_join(session_th, NULL);
773 RTE_LOG(ERR, PMD, "Can't join the thread\n");
777 eth_dev_start(struct rte_eth_dev *dev)
779 struct pmd_internal *internal = dev->data->dev_private;
782 if (rte_atomic16_cmpset(&internal->once, 0, 1)) {
783 ret = rte_vhost_driver_register(internal->iface_name,
789 /* We need only one message handling thread */
790 if (rte_atomic16_add_return(&nb_started_ports, 1) == 1)
791 ret = vhost_driver_session_start();
797 eth_dev_stop(struct rte_eth_dev *dev)
799 struct pmd_internal *internal = dev->data->dev_private;
801 if (rte_atomic16_cmpset(&internal->once, 1, 0))
802 rte_vhost_driver_unregister(internal->iface_name);
804 if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
805 vhost_driver_session_stop();
809 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
810 uint16_t nb_rx_desc __rte_unused,
811 unsigned int socket_id,
812 const struct rte_eth_rxconf *rx_conf __rte_unused,
813 struct rte_mempool *mb_pool)
815 struct vhost_queue *vq;
817 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
818 RTE_CACHE_LINE_SIZE, socket_id);
820 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
824 vq->mb_pool = mb_pool;
825 vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
826 dev->data->rx_queues[rx_queue_id] = vq;
832 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
833 uint16_t nb_tx_desc __rte_unused,
834 unsigned int socket_id,
835 const struct rte_eth_txconf *tx_conf __rte_unused)
837 struct vhost_queue *vq;
839 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
840 RTE_CACHE_LINE_SIZE, socket_id);
842 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
846 vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
847 dev->data->tx_queues[tx_queue_id] = vq;
853 eth_dev_info(struct rte_eth_dev *dev,
854 struct rte_eth_dev_info *dev_info)
856 struct pmd_internal *internal;
858 internal = dev->data->dev_private;
859 if (internal == NULL) {
860 RTE_LOG(ERR, PMD, "Invalid device specified\n");
864 dev_info->driver_name = drivername;
865 dev_info->max_mac_addrs = 1;
866 dev_info->max_rx_pktlen = (uint32_t)-1;
867 dev_info->max_rx_queues = internal->max_queues;
868 dev_info->max_tx_queues = internal->max_queues;
869 dev_info->min_rx_bufsize = 0;
873 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
876 unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
877 unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
878 struct vhost_queue *vq;
880 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
881 i < dev->data->nb_rx_queues; i++) {
882 if (dev->data->rx_queues[i] == NULL)
884 vq = dev->data->rx_queues[i];
885 stats->q_ipackets[i] = vq->stats.pkts;
886 rx_total += stats->q_ipackets[i];
888 stats->q_ibytes[i] = vq->stats.bytes;
889 rx_total_bytes += stats->q_ibytes[i];
892 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
893 i < dev->data->nb_tx_queues; i++) {
894 if (dev->data->tx_queues[i] == NULL)
896 vq = dev->data->tx_queues[i];
897 stats->q_opackets[i] = vq->stats.pkts;
898 tx_missed_total += vq->stats.missed_pkts;
899 tx_total += stats->q_opackets[i];
901 stats->q_obytes[i] = vq->stats.bytes;
902 tx_total_bytes += stats->q_obytes[i];
905 stats->ipackets = rx_total;
906 stats->opackets = tx_total;
907 stats->oerrors = tx_missed_total;
908 stats->ibytes = rx_total_bytes;
909 stats->obytes = tx_total_bytes;
913 eth_stats_reset(struct rte_eth_dev *dev)
915 struct vhost_queue *vq;
918 for (i = 0; i < dev->data->nb_rx_queues; i++) {
919 if (dev->data->rx_queues[i] == NULL)
921 vq = dev->data->rx_queues[i];
925 for (i = 0; i < dev->data->nb_tx_queues; i++) {
926 if (dev->data->tx_queues[i] == NULL)
928 vq = dev->data->tx_queues[i];
931 vq->stats.missed_pkts = 0;
936 eth_queue_release(void *q)
942 eth_link_update(struct rte_eth_dev *dev __rte_unused,
943 int wait_to_complete __rte_unused)
949 * Disable features in feature_mask. Returns 0 on success.
952 rte_eth_vhost_feature_disable(uint64_t feature_mask)
954 return rte_vhost_feature_disable(feature_mask);
958 * Enable features in feature_mask. Returns 0 on success.
961 rte_eth_vhost_feature_enable(uint64_t feature_mask)
963 return rte_vhost_feature_enable(feature_mask);
966 /* Returns currently supported vhost features */
968 rte_eth_vhost_feature_get(void)
970 return rte_vhost_feature_get();
973 static const struct eth_dev_ops ops = {
974 .dev_start = eth_dev_start,
975 .dev_stop = eth_dev_stop,
976 .dev_configure = eth_dev_configure,
977 .dev_infos_get = eth_dev_info,
978 .rx_queue_setup = eth_rx_queue_setup,
979 .tx_queue_setup = eth_tx_queue_setup,
980 .rx_queue_release = eth_queue_release,
981 .tx_queue_release = eth_queue_release,
982 .link_update = eth_link_update,
983 .stats_get = eth_stats_get,
984 .stats_reset = eth_stats_reset,
985 .xstats_reset = vhost_dev_xstats_reset,
986 .xstats_get = vhost_dev_xstats_get,
987 .xstats_get_names = vhost_dev_xstats_get_names,
991 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
992 const unsigned numa_node, uint64_t flags)
994 struct rte_eth_dev_data *data = NULL;
995 struct pmd_internal *internal = NULL;
996 struct rte_eth_dev *eth_dev = NULL;
997 struct ether_addr *eth_addr = NULL;
998 struct rte_vhost_vring_state *vring_state = NULL;
999 struct internal_list *list = NULL;
1001 RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
1004 /* now do all data allocation - for eth_dev structure, dummy pci driver
1005 * and internal (private) data
1007 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1011 internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
1012 if (internal == NULL)
1015 list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1019 /* reserve an ethdev entry */
1020 eth_dev = rte_eth_dev_allocate(name);
1021 if (eth_dev == NULL)
1024 eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1025 if (eth_addr == NULL)
1027 *eth_addr = base_eth_addr;
1028 eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1030 vring_state = rte_zmalloc_socket(name,
1031 sizeof(*vring_state), 0, numa_node);
1032 if (vring_state == NULL)
1035 TAILQ_INIT(ð_dev->link_intr_cbs);
1037 /* now put it all together
1038 * - store queue data in internal,
1039 * - store numa_node info in ethdev data
1040 * - point eth_dev_data to internals
1041 * - and point eth_dev structure to new eth_dev_data structure
1043 internal->dev_name = strdup(name);
1044 if (internal->dev_name == NULL)
1046 internal->iface_name = strdup(iface_name);
1047 if (internal->iface_name == NULL)
1049 internal->flags = flags;
1051 list->eth_dev = eth_dev;
1052 pthread_mutex_lock(&internal_list_lock);
1053 TAILQ_INSERT_TAIL(&internal_list, list, next);
1054 pthread_mutex_unlock(&internal_list_lock);
1056 rte_spinlock_init(&vring_state->lock);
1057 vring_states[eth_dev->data->port_id] = vring_state;
1059 data->dev_private = internal;
1060 data->port_id = eth_dev->data->port_id;
1061 memmove(data->name, eth_dev->data->name, sizeof(data->name));
1062 data->nb_rx_queues = queues;
1063 data->nb_tx_queues = queues;
1064 internal->max_queues = queues;
1065 data->dev_link = pmd_link;
1066 data->mac_addrs = eth_addr;
1068 /* We'll replace the 'data' originally allocated by eth_dev. So the
1069 * vhost PMD resources won't be shared between multi processes.
1071 eth_dev->data = data;
1072 eth_dev->dev_ops = &ops;
1073 eth_dev->driver = NULL;
1075 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1076 data->kdrv = RTE_KDRV_NONE;
1077 data->drv_name = internal->dev_name;
1078 data->numa_node = numa_node;
1080 /* finally assign rx and tx ops */
1081 eth_dev->rx_pkt_burst = eth_vhost_rx;
1082 eth_dev->tx_pkt_burst = eth_vhost_tx;
1084 return data->port_id;
1088 free(internal->dev_name);
1089 rte_free(vring_state);
1092 rte_eth_dev_release_port(eth_dev);
1101 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1103 const char **iface_name = extra_args;
1108 *iface_name = value;
1114 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1116 uint16_t *n = extra_args;
1118 if (value == NULL || extra_args == NULL)
1121 *n = (uint16_t)strtoul(value, NULL, 0);
1122 if (*n == USHRT_MAX && errno == ERANGE)
1129 rte_pmd_vhost_probe(const char *name, const char *params)
1131 struct rte_kvargs *kvlist = NULL;
1136 int client_mode = 0;
1137 int dequeue_zero_copy = 0;
1139 RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1141 kvlist = rte_kvargs_parse(params, valid_arguments);
1145 if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1146 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1147 &open_iface, &iface_name);
1155 if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1156 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1157 &open_int, &queues);
1158 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1164 if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1165 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1166 &open_int, &client_mode);
1171 flags |= RTE_VHOST_USER_CLIENT;
1174 if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1175 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1176 &open_int, &dequeue_zero_copy);
1180 if (dequeue_zero_copy)
1181 flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1184 eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1187 rte_kvargs_free(kvlist);
1192 rte_pmd_vhost_remove(const char *name)
1194 struct rte_eth_dev *eth_dev = NULL;
1195 struct pmd_internal *internal;
1196 struct internal_list *list;
1199 RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1201 /* find an ethdev entry */
1202 eth_dev = rte_eth_dev_allocated(name);
1203 if (eth_dev == NULL)
1206 internal = eth_dev->data->dev_private;
1207 if (internal == NULL)
1210 list = find_internal_resource(internal->iface_name);
1214 pthread_mutex_lock(&internal_list_lock);
1215 TAILQ_REMOVE(&internal_list, list, next);
1216 pthread_mutex_unlock(&internal_list_lock);
1219 eth_dev_stop(eth_dev);
1221 rte_free(vring_states[eth_dev->data->port_id]);
1222 vring_states[eth_dev->data->port_id] = NULL;
1224 free(internal->dev_name);
1225 free(internal->iface_name);
1227 for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1228 rte_free(eth_dev->data->rx_queues[i]);
1229 for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1230 rte_free(eth_dev->data->tx_queues[i]);
1232 rte_free(eth_dev->data->mac_addrs);
1233 rte_free(eth_dev->data);
1236 rte_eth_dev_release_port(eth_dev);
1241 static struct rte_vdev_driver pmd_vhost_drv = {
1242 .probe = rte_pmd_vhost_probe,
1243 .remove = rte_pmd_vhost_remove,
1246 DRIVER_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1247 DRIVER_REGISTER_PARAM_STRING(net_vhost,