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"
55 static const char *drivername = "VHOST PMD";
57 static const char *valid_arguments[] = {
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 {
118 volatile uint16_t once;
121 struct internal_list {
122 TAILQ_ENTRY(internal_list) next;
123 struct rte_eth_dev *eth_dev;
126 TAILQ_HEAD(internal_list_head, internal_list);
127 static struct internal_list_head internal_list =
128 TAILQ_HEAD_INITIALIZER(internal_list);
130 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
132 static rte_atomic16_t nb_started_ports;
133 static pthread_t session_th;
135 static struct rte_eth_link pmd_link = {
137 .link_duplex = ETH_LINK_FULL_DUPLEX,
138 .link_status = ETH_LINK_DOWN
141 struct rte_vhost_vring_state {
144 bool cur[RTE_MAX_QUEUES_PER_PORT * 2];
145 bool seen[RTE_MAX_QUEUES_PER_PORT * 2];
147 unsigned int max_vring;
150 static struct rte_vhost_vring_state *vring_states[RTE_MAX_ETHPORTS];
152 #define VHOST_XSTATS_NAME_SIZE 64
154 struct vhost_xstats_name_off {
155 char name[VHOST_XSTATS_NAME_SIZE];
159 /* [rx]_is prepended to the name string here */
160 static const struct vhost_xstats_name_off vhost_rxport_stat_strings[] = {
162 offsetof(struct vhost_queue, stats.pkts)},
164 offsetof(struct vhost_queue, stats.bytes)},
166 offsetof(struct vhost_queue, stats.missed_pkts)},
167 {"broadcast_packets",
168 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
169 {"multicast_packets",
170 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
172 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
173 {"undersize_packets",
174 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
176 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
177 {"size_65_to_127_packets",
178 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
179 {"size_128_to_255_packets",
180 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
181 {"size_256_to_511_packets",
182 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
183 {"size_512_to_1023_packets",
184 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
185 {"size_1024_to_1522_packets",
186 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
187 {"size_1523_to_max_packets",
188 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
189 {"errors_with_bad_CRC",
190 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
191 {"fragmented_errors",
192 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_FRAGMENTED])},
194 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_JABBER])},
195 {"unknown_protos_packets",
196 offsetof(struct vhost_queue, stats.xstats[VHOST_UNKNOWN_PROTOCOL])},
199 /* [tx]_ is prepended to the name string here */
200 static const struct vhost_xstats_name_off vhost_txport_stat_strings[] = {
202 offsetof(struct vhost_queue, stats.pkts)},
204 offsetof(struct vhost_queue, stats.bytes)},
206 offsetof(struct vhost_queue, stats.missed_pkts)},
207 {"broadcast_packets",
208 offsetof(struct vhost_queue, stats.xstats[VHOST_BROADCAST_PKT])},
209 {"multicast_packets",
210 offsetof(struct vhost_queue, stats.xstats[VHOST_MULTICAST_PKT])},
212 offsetof(struct vhost_queue, stats.xstats[VHOST_UNICAST_PKT])},
213 {"undersize_packets",
214 offsetof(struct vhost_queue, stats.xstats[VHOST_UNDERSIZE_PKT])},
216 offsetof(struct vhost_queue, stats.xstats[VHOST_64_PKT])},
217 {"size_65_to_127_packets",
218 offsetof(struct vhost_queue, stats.xstats[VHOST_65_TO_127_PKT])},
219 {"size_128_to_255_packets",
220 offsetof(struct vhost_queue, stats.xstats[VHOST_128_TO_255_PKT])},
221 {"size_256_to_511_packets",
222 offsetof(struct vhost_queue, stats.xstats[VHOST_256_TO_511_PKT])},
223 {"size_512_to_1023_packets",
224 offsetof(struct vhost_queue, stats.xstats[VHOST_512_TO_1023_PKT])},
225 {"size_1024_to_1522_packets",
226 offsetof(struct vhost_queue, stats.xstats[VHOST_1024_TO_1522_PKT])},
227 {"size_1523_to_max_packets",
228 offsetof(struct vhost_queue, stats.xstats[VHOST_1523_TO_MAX_PKT])},
229 {"errors_with_bad_CRC",
230 offsetof(struct vhost_queue, stats.xstats[VHOST_ERRORS_PKT])},
233 #define VHOST_NB_XSTATS_RXPORT (sizeof(vhost_rxport_stat_strings) / \
234 sizeof(vhost_rxport_stat_strings[0]))
236 #define VHOST_NB_XSTATS_TXPORT (sizeof(vhost_txport_stat_strings) / \
237 sizeof(vhost_txport_stat_strings[0]))
240 vhost_dev_xstats_reset(struct rte_eth_dev *dev)
242 struct vhost_queue *vq = NULL;
245 for (i = 0; i < dev->data->nb_rx_queues; i++) {
246 vq = dev->data->rx_queues[i];
249 memset(&vq->stats, 0, sizeof(vq->stats));
251 for (i = 0; i < dev->data->nb_tx_queues; i++) {
252 vq = dev->data->tx_queues[i];
255 memset(&vq->stats, 0, sizeof(vq->stats));
260 vhost_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
261 struct rte_eth_xstat_name *xstats_names,
262 unsigned int limit __rte_unused)
266 int nstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
270 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
271 snprintf(xstats_names[count].name,
272 sizeof(xstats_names[count].name),
273 "rx_%s", vhost_rxport_stat_strings[t].name);
276 for (t = 0; t < VHOST_NB_XSTATS_TXPORT; t++) {
277 snprintf(xstats_names[count].name,
278 sizeof(xstats_names[count].name),
279 "tx_%s", vhost_txport_stat_strings[t].name);
286 vhost_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
291 unsigned int count = 0;
292 struct vhost_queue *vq = NULL;
293 unsigned int nxstats = VHOST_NB_XSTATS_RXPORT + VHOST_NB_XSTATS_TXPORT;
298 for (i = 0; i < dev->data->nb_rx_queues; i++) {
299 vq = dev->data->rx_queues[i];
302 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
303 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
304 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
306 for (i = 0; i < dev->data->nb_tx_queues; i++) {
307 vq = dev->data->tx_queues[i];
310 vq->stats.xstats[VHOST_UNICAST_PKT] = vq->stats.pkts
311 + vq->stats.missed_pkts
312 - (vq->stats.xstats[VHOST_BROADCAST_PKT]
313 + vq->stats.xstats[VHOST_MULTICAST_PKT]);
315 for (t = 0; t < VHOST_NB_XSTATS_RXPORT; t++) {
316 xstats[count].value = 0;
317 for (i = 0; i < dev->data->nb_rx_queues; i++) {
318 vq = dev->data->rx_queues[i];
321 xstats[count].value +=
322 *(uint64_t *)(((char *)vq)
323 + vhost_rxport_stat_strings[t].offset);
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);
343 vhost_count_multicast_broadcast(struct vhost_queue *vq,
344 struct rte_mbuf *mbuf)
346 struct ether_addr *ea = NULL;
347 struct vhost_stats *pstats = &vq->stats;
349 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
350 if (is_multicast_ether_addr(ea)) {
351 if (is_broadcast_ether_addr(ea))
352 pstats->xstats[VHOST_BROADCAST_PKT]++;
354 pstats->xstats[VHOST_MULTICAST_PKT]++;
359 vhost_update_packet_xstats(struct vhost_queue *vq,
360 struct rte_mbuf **bufs,
363 uint32_t pkt_len = 0;
366 struct vhost_stats *pstats = &vq->stats;
368 for (i = 0; i < count ; i++) {
369 pkt_len = bufs[i]->pkt_len;
371 pstats->xstats[VHOST_64_PKT]++;
372 } else if (pkt_len > 64 && pkt_len < 1024) {
373 index = (sizeof(pkt_len) * 8)
374 - __builtin_clz(pkt_len) - 5;
375 pstats->xstats[index]++;
378 pstats->xstats[VHOST_UNDERSIZE_PKT]++;
379 else if (pkt_len <= 1522)
380 pstats->xstats[VHOST_1024_TO_1522_PKT]++;
381 else if (pkt_len > 1522)
382 pstats->xstats[VHOST_1523_TO_MAX_PKT]++;
384 vhost_count_multicast_broadcast(vq, bufs[i]);
389 eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
391 struct vhost_queue *r = q;
392 uint16_t i, nb_rx = 0;
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 */
403 nb_rx = rte_vhost_dequeue_burst(r->vid,
404 r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
406 r->stats.pkts += nb_rx;
408 for (i = 0; likely(i < nb_rx); i++) {
409 bufs[i]->port = r->port;
410 r->stats.bytes += bufs[i]->pkt_len;
413 vhost_update_packet_xstats(r, bufs, nb_rx);
416 rte_atomic32_set(&r->while_queuing, 0);
422 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
424 struct vhost_queue *r = q;
425 uint16_t i, nb_tx = 0;
427 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
430 rte_atomic32_set(&r->while_queuing, 1);
432 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
435 /* Enqueue packets to guest RX queue */
436 nb_tx = rte_vhost_enqueue_burst(r->vid,
437 r->virtqueue_id, bufs, nb_bufs);
439 r->stats.pkts += nb_tx;
440 r->stats.missed_pkts += nb_bufs - nb_tx;
442 for (i = 0; likely(i < nb_tx); i++)
443 r->stats.bytes += bufs[i]->pkt_len;
445 vhost_update_packet_xstats(r, bufs, nb_tx);
447 /* According to RFC2863 page42 section ifHCOutMulticastPkts and
448 * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
449 * are increased when packets are not transmitted successfully.
451 for (i = nb_tx; i < nb_bufs; i++)
452 vhost_count_multicast_broadcast(r, bufs[i]);
454 for (i = 0; likely(i < nb_tx); i++)
455 rte_pktmbuf_free(bufs[i]);
457 rte_atomic32_set(&r->while_queuing, 0);
463 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
468 static inline struct internal_list *
469 find_internal_resource(char *ifname)
472 struct internal_list *list;
473 struct pmd_internal *internal;
478 pthread_mutex_lock(&internal_list_lock);
480 TAILQ_FOREACH(list, &internal_list, next) {
481 internal = list->eth_dev->data->dev_private;
482 if (!strcmp(internal->iface_name, ifname)) {
488 pthread_mutex_unlock(&internal_list_lock);
499 struct rte_eth_dev *eth_dev;
500 struct internal_list *list;
501 struct pmd_internal *internal;
502 struct vhost_queue *vq;
504 char ifname[PATH_MAX];
505 #ifdef RTE_LIBRTE_VHOST_NUMA
509 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
510 list = find_internal_resource(ifname);
512 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
516 eth_dev = list->eth_dev;
517 internal = eth_dev->data->dev_private;
519 #ifdef RTE_LIBRTE_VHOST_NUMA
520 newnode = rte_vhost_get_numa_node(vid);
522 eth_dev->data->numa_node = newnode;
525 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
526 vq = eth_dev->data->rx_queues[i];
530 vq->internal = internal;
531 vq->port = eth_dev->data->port_id;
533 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
534 vq = eth_dev->data->tx_queues[i];
538 vq->internal = internal;
539 vq->port = eth_dev->data->port_id;
542 for (i = 0; i < rte_vhost_get_queue_num(vid) * VIRTIO_QNUM; i++)
543 rte_vhost_enable_guest_notification(vid, i, 0);
545 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
547 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
548 vq = eth_dev->data->rx_queues[i];
551 rte_atomic32_set(&vq->allow_queuing, 1);
553 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
554 vq = eth_dev->data->tx_queues[i];
557 rte_atomic32_set(&vq->allow_queuing, 1);
560 RTE_LOG(INFO, PMD, "New connection established\n");
562 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
568 destroy_device(int vid)
570 struct rte_eth_dev *eth_dev;
571 struct vhost_queue *vq;
572 struct internal_list *list;
573 char ifname[PATH_MAX];
575 struct rte_vhost_vring_state *state;
577 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
578 list = find_internal_resource(ifname);
580 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
583 eth_dev = list->eth_dev;
585 /* Wait until rx/tx_pkt_burst stops accessing vhost device */
586 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
587 vq = eth_dev->data->rx_queues[i];
590 rte_atomic32_set(&vq->allow_queuing, 0);
591 while (rte_atomic32_read(&vq->while_queuing))
594 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
595 vq = eth_dev->data->tx_queues[i];
598 rte_atomic32_set(&vq->allow_queuing, 0);
599 while (rte_atomic32_read(&vq->while_queuing))
603 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
605 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
606 vq = eth_dev->data->rx_queues[i];
611 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
612 vq = eth_dev->data->tx_queues[i];
618 state = vring_states[eth_dev->data->port_id];
619 rte_spinlock_lock(&state->lock);
620 for (i = 0; i <= state->max_vring; i++) {
621 state->cur[i] = false;
622 state->seen[i] = false;
624 state->max_vring = 0;
625 rte_spinlock_unlock(&state->lock);
627 RTE_LOG(INFO, PMD, "Connection closed\n");
629 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC);
633 vring_state_changed(int vid, uint16_t vring, int enable)
635 struct rte_vhost_vring_state *state;
636 struct rte_eth_dev *eth_dev;
637 struct internal_list *list;
638 char ifname[PATH_MAX];
640 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
641 list = find_internal_resource(ifname);
643 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
647 eth_dev = list->eth_dev;
649 state = vring_states[eth_dev->data->port_id];
650 rte_spinlock_lock(&state->lock);
651 state->cur[vring] = enable;
652 state->max_vring = RTE_MAX(vring, state->max_vring);
653 rte_spinlock_unlock(&state->lock);
655 RTE_LOG(INFO, PMD, "vring%u is %s\n",
656 vring, enable ? "enabled" : "disabled");
658 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE);
664 rte_eth_vhost_get_queue_event(uint8_t port_id,
665 struct rte_eth_vhost_queue_event *event)
667 struct rte_vhost_vring_state *state;
671 if (port_id >= RTE_MAX_ETHPORTS) {
672 RTE_LOG(ERR, PMD, "Invalid port id\n");
676 state = vring_states[port_id];
678 RTE_LOG(ERR, PMD, "Unused port\n");
682 rte_spinlock_lock(&state->lock);
683 for (i = 0; i <= state->max_vring; i++) {
684 idx = state->index++ % (state->max_vring + 1);
686 if (state->cur[idx] != state->seen[idx]) {
687 state->seen[idx] = state->cur[idx];
688 event->queue_id = idx / 2;
690 event->enable = state->cur[idx];
691 rte_spinlock_unlock(&state->lock);
695 rte_spinlock_unlock(&state->lock);
701 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
703 struct internal_list *list;
704 struct rte_eth_dev *eth_dev;
705 struct vhost_queue *vq;
708 if (!rte_eth_dev_is_valid_port(port_id))
711 pthread_mutex_lock(&internal_list_lock);
713 TAILQ_FOREACH(list, &internal_list, next) {
714 eth_dev = list->eth_dev;
715 if (eth_dev->data->port_id == port_id) {
716 vq = eth_dev->data->rx_queues[0];
724 pthread_mutex_unlock(&internal_list_lock);
730 vhost_driver_session(void *param __rte_unused)
732 static struct virtio_net_device_ops vhost_ops;
734 /* set vhost arguments */
735 vhost_ops.new_device = new_device;
736 vhost_ops.destroy_device = destroy_device;
737 vhost_ops.vring_state_changed = vring_state_changed;
738 if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
739 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
741 /* start event handling */
742 rte_vhost_driver_session_start();
748 vhost_driver_session_start(void)
752 ret = pthread_create(&session_th,
753 NULL, vhost_driver_session, NULL);
755 RTE_LOG(ERR, PMD, "Can't create a thread\n");
761 vhost_driver_session_stop(void)
765 ret = pthread_cancel(session_th);
767 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
769 ret = pthread_join(session_th, NULL);
771 RTE_LOG(ERR, PMD, "Can't join the thread\n");
775 eth_dev_start(struct rte_eth_dev *dev)
777 struct pmd_internal *internal = dev->data->dev_private;
780 if (rte_atomic16_cmpset(&internal->once, 0, 1)) {
781 ret = rte_vhost_driver_register(internal->iface_name,
787 /* We need only one message handling thread */
788 if (rte_atomic16_add_return(&nb_started_ports, 1) == 1)
789 ret = vhost_driver_session_start();
795 eth_dev_stop(struct rte_eth_dev *dev)
797 struct pmd_internal *internal = dev->data->dev_private;
799 if (rte_atomic16_cmpset(&internal->once, 1, 0))
800 rte_vhost_driver_unregister(internal->iface_name);
802 if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
803 vhost_driver_session_stop();
807 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
808 uint16_t nb_rx_desc __rte_unused,
809 unsigned int socket_id,
810 const struct rte_eth_rxconf *rx_conf __rte_unused,
811 struct rte_mempool *mb_pool)
813 struct vhost_queue *vq;
815 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
816 RTE_CACHE_LINE_SIZE, socket_id);
818 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
822 vq->mb_pool = mb_pool;
823 vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
824 dev->data->rx_queues[rx_queue_id] = vq;
830 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
831 uint16_t nb_tx_desc __rte_unused,
832 unsigned int socket_id,
833 const struct rte_eth_txconf *tx_conf __rte_unused)
835 struct vhost_queue *vq;
837 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
838 RTE_CACHE_LINE_SIZE, socket_id);
840 RTE_LOG(ERR, PMD, "Failed to allocate memory for tx queue\n");
844 vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
845 dev->data->tx_queues[tx_queue_id] = vq;
851 eth_dev_info(struct rte_eth_dev *dev,
852 struct rte_eth_dev_info *dev_info)
854 struct pmd_internal *internal;
856 internal = dev->data->dev_private;
857 if (internal == NULL) {
858 RTE_LOG(ERR, PMD, "Invalid device specified\n");
862 dev_info->driver_name = drivername;
863 dev_info->max_mac_addrs = 1;
864 dev_info->max_rx_pktlen = (uint32_t)-1;
865 dev_info->max_rx_queues = internal->max_queues;
866 dev_info->max_tx_queues = internal->max_queues;
867 dev_info->min_rx_bufsize = 0;
871 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
874 unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
875 unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
876 struct vhost_queue *vq;
878 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
879 i < dev->data->nb_rx_queues; i++) {
880 if (dev->data->rx_queues[i] == NULL)
882 vq = dev->data->rx_queues[i];
883 stats->q_ipackets[i] = vq->stats.pkts;
884 rx_total += stats->q_ipackets[i];
886 stats->q_ibytes[i] = vq->stats.bytes;
887 rx_total_bytes += stats->q_ibytes[i];
890 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
891 i < dev->data->nb_tx_queues; i++) {
892 if (dev->data->tx_queues[i] == NULL)
894 vq = dev->data->tx_queues[i];
895 stats->q_opackets[i] = vq->stats.pkts;
896 tx_missed_total += vq->stats.missed_pkts;
897 tx_total += stats->q_opackets[i];
899 stats->q_obytes[i] = vq->stats.bytes;
900 tx_total_bytes += stats->q_obytes[i];
903 stats->ipackets = rx_total;
904 stats->opackets = tx_total;
905 stats->oerrors = tx_missed_total;
906 stats->ibytes = rx_total_bytes;
907 stats->obytes = tx_total_bytes;
911 eth_stats_reset(struct rte_eth_dev *dev)
913 struct vhost_queue *vq;
916 for (i = 0; i < dev->data->nb_rx_queues; i++) {
917 if (dev->data->rx_queues[i] == NULL)
919 vq = dev->data->rx_queues[i];
923 for (i = 0; i < dev->data->nb_tx_queues; i++) {
924 if (dev->data->tx_queues[i] == NULL)
926 vq = dev->data->tx_queues[i];
929 vq->stats.missed_pkts = 0;
934 eth_queue_release(void *q)
940 eth_link_update(struct rte_eth_dev *dev __rte_unused,
941 int wait_to_complete __rte_unused)
947 * Disable features in feature_mask. Returns 0 on success.
950 rte_eth_vhost_feature_disable(uint64_t feature_mask)
952 return rte_vhost_feature_disable(feature_mask);
956 * Enable features in feature_mask. Returns 0 on success.
959 rte_eth_vhost_feature_enable(uint64_t feature_mask)
961 return rte_vhost_feature_enable(feature_mask);
964 /* Returns currently supported vhost features */
966 rte_eth_vhost_feature_get(void)
968 return rte_vhost_feature_get();
971 static const struct eth_dev_ops ops = {
972 .dev_start = eth_dev_start,
973 .dev_stop = eth_dev_stop,
974 .dev_configure = eth_dev_configure,
975 .dev_infos_get = eth_dev_info,
976 .rx_queue_setup = eth_rx_queue_setup,
977 .tx_queue_setup = eth_tx_queue_setup,
978 .rx_queue_release = eth_queue_release,
979 .tx_queue_release = eth_queue_release,
980 .link_update = eth_link_update,
981 .stats_get = eth_stats_get,
982 .stats_reset = eth_stats_reset,
983 .xstats_reset = vhost_dev_xstats_reset,
984 .xstats_get = vhost_dev_xstats_get,
985 .xstats_get_names = vhost_dev_xstats_get_names,
989 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
990 const unsigned numa_node, uint64_t flags)
992 struct rte_eth_dev_data *data = NULL;
993 struct pmd_internal *internal = NULL;
994 struct rte_eth_dev *eth_dev = NULL;
995 struct ether_addr *eth_addr = NULL;
996 struct rte_vhost_vring_state *vring_state = NULL;
997 struct internal_list *list = NULL;
999 RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
1002 /* now do all data allocation - for eth_dev structure, dummy pci driver
1003 * and internal (private) data
1005 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1009 internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
1010 if (internal == NULL)
1013 list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
1017 /* reserve an ethdev entry */
1018 eth_dev = rte_eth_dev_allocate(name);
1019 if (eth_dev == NULL)
1022 eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1023 if (eth_addr == NULL)
1025 *eth_addr = base_eth_addr;
1026 eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1028 vring_state = rte_zmalloc_socket(name,
1029 sizeof(*vring_state), 0, numa_node);
1030 if (vring_state == NULL)
1033 TAILQ_INIT(ð_dev->link_intr_cbs);
1035 /* now put it all together
1036 * - store queue data in internal,
1037 * - store numa_node info in ethdev data
1038 * - point eth_dev_data to internals
1039 * - and point eth_dev structure to new eth_dev_data structure
1041 internal->dev_name = strdup(name);
1042 if (internal->dev_name == NULL)
1044 internal->iface_name = strdup(iface_name);
1045 if (internal->iface_name == NULL)
1047 internal->flags = flags;
1049 list->eth_dev = eth_dev;
1050 pthread_mutex_lock(&internal_list_lock);
1051 TAILQ_INSERT_TAIL(&internal_list, list, next);
1052 pthread_mutex_unlock(&internal_list_lock);
1054 rte_spinlock_init(&vring_state->lock);
1055 vring_states[eth_dev->data->port_id] = vring_state;
1057 data->dev_private = internal;
1058 data->port_id = eth_dev->data->port_id;
1059 memmove(data->name, eth_dev->data->name, sizeof(data->name));
1060 data->nb_rx_queues = queues;
1061 data->nb_tx_queues = queues;
1062 internal->max_queues = queues;
1063 data->dev_link = pmd_link;
1064 data->mac_addrs = eth_addr;
1066 /* We'll replace the 'data' originally allocated by eth_dev. So the
1067 * vhost PMD resources won't be shared between multi processes.
1069 eth_dev->data = data;
1070 eth_dev->dev_ops = &ops;
1071 eth_dev->driver = NULL;
1073 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1074 data->kdrv = RTE_KDRV_NONE;
1075 data->drv_name = internal->dev_name;
1076 data->numa_node = numa_node;
1078 /* finally assign rx and tx ops */
1079 eth_dev->rx_pkt_burst = eth_vhost_rx;
1080 eth_dev->tx_pkt_burst = eth_vhost_tx;
1082 return data->port_id;
1086 free(internal->dev_name);
1087 rte_free(vring_state);
1090 rte_eth_dev_release_port(eth_dev);
1099 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1101 const char **iface_name = extra_args;
1106 *iface_name = value;
1112 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1114 uint16_t *n = extra_args;
1116 if (value == NULL || extra_args == NULL)
1119 *n = (uint16_t)strtoul(value, NULL, 0);
1120 if (*n == USHRT_MAX && errno == ERANGE)
1127 rte_pmd_vhost_devinit(const char *name, const char *params)
1129 struct rte_kvargs *kvlist = NULL;
1134 int client_mode = 0;
1136 RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1138 kvlist = rte_kvargs_parse(params, valid_arguments);
1142 if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1143 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1144 &open_iface, &iface_name);
1152 if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1153 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1154 &open_int, &queues);
1155 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1161 if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1162 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1163 &open_int, &client_mode);
1168 flags |= RTE_VHOST_USER_CLIENT;
1171 eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1174 rte_kvargs_free(kvlist);
1179 rte_pmd_vhost_devuninit(const char *name)
1181 struct rte_eth_dev *eth_dev = NULL;
1182 struct pmd_internal *internal;
1183 struct internal_list *list;
1186 RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1188 /* find an ethdev entry */
1189 eth_dev = rte_eth_dev_allocated(name);
1190 if (eth_dev == NULL)
1193 internal = eth_dev->data->dev_private;
1194 if (internal == NULL)
1197 list = find_internal_resource(internal->iface_name);
1201 pthread_mutex_lock(&internal_list_lock);
1202 TAILQ_REMOVE(&internal_list, list, next);
1203 pthread_mutex_unlock(&internal_list_lock);
1206 eth_dev_stop(eth_dev);
1208 rte_free(vring_states[eth_dev->data->port_id]);
1209 vring_states[eth_dev->data->port_id] = NULL;
1211 free(internal->dev_name);
1212 free(internal->iface_name);
1214 for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1215 rte_free(eth_dev->data->rx_queues[i]);
1216 for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1217 rte_free(eth_dev->data->tx_queues[i]);
1219 rte_free(eth_dev->data->mac_addrs);
1220 rte_free(eth_dev->data);
1223 rte_eth_dev_release_port(eth_dev);
1228 static struct rte_vdev_driver pmd_vhost_drv = {
1229 .init = rte_pmd_vhost_devinit,
1230 .uninit = rte_pmd_vhost_devuninit,
1233 DRIVER_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1234 DRIVER_REGISTER_PARAM_STRING(net_vhost,