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 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;
395 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
398 rte_atomic32_set(&r->while_queuing, 1);
400 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
403 /* Dequeue packets from guest TX queue */
404 nb_rx = rte_vhost_dequeue_burst(r->vid,
405 r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
407 r->stats.pkts += nb_rx;
409 for (i = 0; likely(i < nb_rx); i++) {
410 bufs[i]->port = r->port;
411 r->stats.bytes += bufs[i]->pkt_len;
414 vhost_update_packet_xstats(r, bufs, nb_rx);
417 rte_atomic32_set(&r->while_queuing, 0);
423 eth_vhost_tx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
425 struct vhost_queue *r = q;
426 uint16_t i, nb_tx = 0;
428 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
431 rte_atomic32_set(&r->while_queuing, 1);
433 if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
436 /* Enqueue packets to guest RX queue */
437 nb_tx = rte_vhost_enqueue_burst(r->vid,
438 r->virtqueue_id, bufs, nb_bufs);
440 r->stats.pkts += nb_tx;
441 r->stats.missed_pkts += nb_bufs - nb_tx;
443 for (i = 0; likely(i < nb_tx); i++)
444 r->stats.bytes += bufs[i]->pkt_len;
446 vhost_update_packet_xstats(r, bufs, nb_tx);
448 /* According to RFC2863 page42 section ifHCOutMulticastPkts and
449 * ifHCOutBroadcastPkts, the counters "multicast" and "broadcast"
450 * are increased when packets are not transmitted successfully.
452 for (i = nb_tx; i < nb_bufs; i++)
453 vhost_count_multicast_broadcast(r, bufs[i]);
455 for (i = 0; likely(i < nb_tx); i++)
456 rte_pktmbuf_free(bufs[i]);
458 rte_atomic32_set(&r->while_queuing, 0);
464 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
469 static inline struct internal_list *
470 find_internal_resource(char *ifname)
473 struct internal_list *list;
474 struct pmd_internal *internal;
479 pthread_mutex_lock(&internal_list_lock);
481 TAILQ_FOREACH(list, &internal_list, next) {
482 internal = list->eth_dev->data->dev_private;
483 if (!strcmp(internal->iface_name, ifname)) {
489 pthread_mutex_unlock(&internal_list_lock);
500 struct rte_eth_dev *eth_dev;
501 struct internal_list *list;
502 struct pmd_internal *internal;
503 struct vhost_queue *vq;
505 char ifname[PATH_MAX];
506 #ifdef RTE_LIBRTE_VHOST_NUMA
510 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
511 list = find_internal_resource(ifname);
513 RTE_LOG(INFO, PMD, "Invalid device name: %s\n", ifname);
517 eth_dev = list->eth_dev;
518 internal = eth_dev->data->dev_private;
520 #ifdef RTE_LIBRTE_VHOST_NUMA
521 newnode = rte_vhost_get_numa_node(vid);
523 eth_dev->data->numa_node = newnode;
526 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
527 vq = eth_dev->data->rx_queues[i];
531 vq->internal = internal;
532 vq->port = eth_dev->data->port_id;
534 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
535 vq = eth_dev->data->tx_queues[i];
539 vq->internal = internal;
540 vq->port = eth_dev->data->port_id;
543 for (i = 0; i < rte_vhost_get_queue_num(vid) * VIRTIO_QNUM; i++)
544 rte_vhost_enable_guest_notification(vid, i, 0);
546 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
548 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
549 vq = eth_dev->data->rx_queues[i];
552 rte_atomic32_set(&vq->allow_queuing, 1);
554 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
555 vq = eth_dev->data->tx_queues[i];
558 rte_atomic32_set(&vq->allow_queuing, 1);
561 RTE_LOG(INFO, PMD, "New connection established\n");
563 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
569 destroy_device(int vid)
571 struct rte_eth_dev *eth_dev;
572 struct vhost_queue *vq;
573 struct internal_list *list;
574 char ifname[PATH_MAX];
576 struct rte_vhost_vring_state *state;
578 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
579 list = find_internal_resource(ifname);
581 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
584 eth_dev = list->eth_dev;
586 /* Wait until rx/tx_pkt_burst stops accessing vhost device */
587 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
588 vq = eth_dev->data->rx_queues[i];
591 rte_atomic32_set(&vq->allow_queuing, 0);
592 while (rte_atomic32_read(&vq->while_queuing))
595 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
596 vq = eth_dev->data->tx_queues[i];
599 rte_atomic32_set(&vq->allow_queuing, 0);
600 while (rte_atomic32_read(&vq->while_queuing))
604 eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
606 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
607 vq = eth_dev->data->rx_queues[i];
612 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
613 vq = eth_dev->data->tx_queues[i];
619 state = vring_states[eth_dev->data->port_id];
620 rte_spinlock_lock(&state->lock);
621 for (i = 0; i <= state->max_vring; i++) {
622 state->cur[i] = false;
623 state->seen[i] = false;
625 state->max_vring = 0;
626 rte_spinlock_unlock(&state->lock);
628 RTE_LOG(INFO, PMD, "Connection closed\n");
630 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
634 vring_state_changed(int vid, uint16_t vring, int enable)
636 struct rte_vhost_vring_state *state;
637 struct rte_eth_dev *eth_dev;
638 struct internal_list *list;
639 char ifname[PATH_MAX];
641 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
642 list = find_internal_resource(ifname);
644 RTE_LOG(ERR, PMD, "Invalid interface name: %s\n", ifname);
648 eth_dev = list->eth_dev;
650 state = vring_states[eth_dev->data->port_id];
651 rte_spinlock_lock(&state->lock);
652 state->cur[vring] = enable;
653 state->max_vring = RTE_MAX(vring, state->max_vring);
654 rte_spinlock_unlock(&state->lock);
656 RTE_LOG(INFO, PMD, "vring%u is %s\n",
657 vring, enable ? "enabled" : "disabled");
659 _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_QUEUE_STATE, NULL);
665 rte_eth_vhost_get_queue_event(uint8_t port_id,
666 struct rte_eth_vhost_queue_event *event)
668 struct rte_vhost_vring_state *state;
672 if (port_id >= RTE_MAX_ETHPORTS) {
673 RTE_LOG(ERR, PMD, "Invalid port id\n");
677 state = vring_states[port_id];
679 RTE_LOG(ERR, PMD, "Unused port\n");
683 rte_spinlock_lock(&state->lock);
684 for (i = 0; i <= state->max_vring; i++) {
685 idx = state->index++ % (state->max_vring + 1);
687 if (state->cur[idx] != state->seen[idx]) {
688 state->seen[idx] = state->cur[idx];
689 event->queue_id = idx / 2;
691 event->enable = state->cur[idx];
692 rte_spinlock_unlock(&state->lock);
696 rte_spinlock_unlock(&state->lock);
702 rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
704 struct internal_list *list;
705 struct rte_eth_dev *eth_dev;
706 struct vhost_queue *vq;
709 if (!rte_eth_dev_is_valid_port(port_id))
712 pthread_mutex_lock(&internal_list_lock);
714 TAILQ_FOREACH(list, &internal_list, next) {
715 eth_dev = list->eth_dev;
716 if (eth_dev->data->port_id == port_id) {
717 vq = eth_dev->data->rx_queues[0];
725 pthread_mutex_unlock(&internal_list_lock);
731 vhost_driver_session(void *param __rte_unused)
733 static struct virtio_net_device_ops vhost_ops;
735 /* set vhost arguments */
736 vhost_ops.new_device = new_device;
737 vhost_ops.destroy_device = destroy_device;
738 vhost_ops.vring_state_changed = vring_state_changed;
739 if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
740 RTE_LOG(ERR, PMD, "Can't register callbacks\n");
742 /* start event handling */
743 rte_vhost_driver_session_start();
749 vhost_driver_session_start(void)
753 ret = pthread_create(&session_th,
754 NULL, vhost_driver_session, NULL);
756 RTE_LOG(ERR, PMD, "Can't create a thread\n");
762 vhost_driver_session_stop(void)
766 ret = pthread_cancel(session_th);
768 RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
770 ret = pthread_join(session_th, NULL);
772 RTE_LOG(ERR, PMD, "Can't join the thread\n");
776 eth_dev_start(struct rte_eth_dev *dev __rte_unused)
782 eth_dev_stop(struct rte_eth_dev *dev __rte_unused)
787 eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
788 uint16_t nb_rx_desc __rte_unused,
789 unsigned int socket_id,
790 const struct rte_eth_rxconf *rx_conf __rte_unused,
791 struct rte_mempool *mb_pool)
793 struct vhost_queue *vq;
795 vq = rte_zmalloc_socket(NULL, sizeof(struct vhost_queue),
796 RTE_CACHE_LINE_SIZE, socket_id);
798 RTE_LOG(ERR, PMD, "Failed to allocate memory for rx queue\n");
802 vq->mb_pool = mb_pool;
803 vq->virtqueue_id = rx_queue_id * VIRTIO_QNUM + VIRTIO_TXQ;
804 dev->data->rx_queues[rx_queue_id] = vq;
810 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
811 uint16_t nb_tx_desc __rte_unused,
812 unsigned int socket_id,
813 const struct rte_eth_txconf *tx_conf __rte_unused)
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 tx queue\n");
824 vq->virtqueue_id = tx_queue_id * VIRTIO_QNUM + VIRTIO_RXQ;
825 dev->data->tx_queues[tx_queue_id] = vq;
831 eth_dev_info(struct rte_eth_dev *dev,
832 struct rte_eth_dev_info *dev_info)
834 struct pmd_internal *internal;
836 internal = dev->data->dev_private;
837 if (internal == NULL) {
838 RTE_LOG(ERR, PMD, "Invalid device specified\n");
842 dev_info->driver_name = drivername;
843 dev_info->max_mac_addrs = 1;
844 dev_info->max_rx_pktlen = (uint32_t)-1;
845 dev_info->max_rx_queues = internal->max_queues;
846 dev_info->max_tx_queues = internal->max_queues;
847 dev_info->min_rx_bufsize = 0;
851 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
854 unsigned long rx_total = 0, tx_total = 0, tx_missed_total = 0;
855 unsigned long rx_total_bytes = 0, tx_total_bytes = 0;
856 struct vhost_queue *vq;
858 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
859 i < dev->data->nb_rx_queues; i++) {
860 if (dev->data->rx_queues[i] == NULL)
862 vq = dev->data->rx_queues[i];
863 stats->q_ipackets[i] = vq->stats.pkts;
864 rx_total += stats->q_ipackets[i];
866 stats->q_ibytes[i] = vq->stats.bytes;
867 rx_total_bytes += stats->q_ibytes[i];
870 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
871 i < dev->data->nb_tx_queues; i++) {
872 if (dev->data->tx_queues[i] == NULL)
874 vq = dev->data->tx_queues[i];
875 stats->q_opackets[i] = vq->stats.pkts;
876 tx_missed_total += vq->stats.missed_pkts;
877 tx_total += stats->q_opackets[i];
879 stats->q_obytes[i] = vq->stats.bytes;
880 tx_total_bytes += stats->q_obytes[i];
883 stats->ipackets = rx_total;
884 stats->opackets = tx_total;
885 stats->oerrors = tx_missed_total;
886 stats->ibytes = rx_total_bytes;
887 stats->obytes = tx_total_bytes;
891 eth_stats_reset(struct rte_eth_dev *dev)
893 struct vhost_queue *vq;
896 for (i = 0; i < dev->data->nb_rx_queues; i++) {
897 if (dev->data->rx_queues[i] == NULL)
899 vq = dev->data->rx_queues[i];
903 for (i = 0; i < dev->data->nb_tx_queues; i++) {
904 if (dev->data->tx_queues[i] == NULL)
906 vq = dev->data->tx_queues[i];
909 vq->stats.missed_pkts = 0;
914 eth_queue_release(void *q)
920 eth_link_update(struct rte_eth_dev *dev __rte_unused,
921 int wait_to_complete __rte_unused)
927 * Disable features in feature_mask. Returns 0 on success.
930 rte_eth_vhost_feature_disable(uint64_t feature_mask)
932 return rte_vhost_feature_disable(feature_mask);
936 * Enable features in feature_mask. Returns 0 on success.
939 rte_eth_vhost_feature_enable(uint64_t feature_mask)
941 return rte_vhost_feature_enable(feature_mask);
944 /* Returns currently supported vhost features */
946 rte_eth_vhost_feature_get(void)
948 return rte_vhost_feature_get();
951 static const struct eth_dev_ops ops = {
952 .dev_start = eth_dev_start,
953 .dev_stop = eth_dev_stop,
954 .dev_configure = eth_dev_configure,
955 .dev_infos_get = eth_dev_info,
956 .rx_queue_setup = eth_rx_queue_setup,
957 .tx_queue_setup = eth_tx_queue_setup,
958 .rx_queue_release = eth_queue_release,
959 .tx_queue_release = eth_queue_release,
960 .link_update = eth_link_update,
961 .stats_get = eth_stats_get,
962 .stats_reset = eth_stats_reset,
963 .xstats_reset = vhost_dev_xstats_reset,
964 .xstats_get = vhost_dev_xstats_get,
965 .xstats_get_names = vhost_dev_xstats_get_names,
969 eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
970 const unsigned numa_node, uint64_t flags)
972 struct rte_eth_dev_data *data = NULL;
973 struct pmd_internal *internal = NULL;
974 struct rte_eth_dev *eth_dev = NULL;
975 struct ether_addr *eth_addr = NULL;
976 struct rte_vhost_vring_state *vring_state = NULL;
977 struct internal_list *list = NULL;
979 RTE_LOG(INFO, PMD, "Creating VHOST-USER backend on numa socket %u\n",
982 /* now do all data allocation - for eth_dev structure, dummy pci driver
983 * and internal (private) data
985 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
989 internal = rte_zmalloc_socket(name, sizeof(*internal), 0, numa_node);
990 if (internal == NULL)
993 list = rte_zmalloc_socket(name, sizeof(*list), 0, numa_node);
997 /* reserve an ethdev entry */
998 eth_dev = rte_eth_dev_allocate(name);
1002 eth_addr = rte_zmalloc_socket(name, sizeof(*eth_addr), 0, numa_node);
1003 if (eth_addr == NULL)
1005 *eth_addr = base_eth_addr;
1006 eth_addr->addr_bytes[5] = eth_dev->data->port_id;
1008 vring_state = rte_zmalloc_socket(name,
1009 sizeof(*vring_state), 0, numa_node);
1010 if (vring_state == NULL)
1013 /* now put it all together
1014 * - store queue data in internal,
1015 * - store numa_node info in ethdev data
1016 * - point eth_dev_data to internals
1017 * - and point eth_dev structure to new eth_dev_data structure
1019 internal->dev_name = strdup(name);
1020 if (internal->dev_name == NULL)
1022 internal->iface_name = strdup(iface_name);
1023 if (internal->iface_name == NULL)
1026 list->eth_dev = eth_dev;
1027 pthread_mutex_lock(&internal_list_lock);
1028 TAILQ_INSERT_TAIL(&internal_list, list, next);
1029 pthread_mutex_unlock(&internal_list_lock);
1031 rte_spinlock_init(&vring_state->lock);
1032 vring_states[eth_dev->data->port_id] = vring_state;
1034 data->dev_private = internal;
1035 data->port_id = eth_dev->data->port_id;
1036 memmove(data->name, eth_dev->data->name, sizeof(data->name));
1037 data->nb_rx_queues = queues;
1038 data->nb_tx_queues = queues;
1039 internal->max_queues = queues;
1040 data->dev_link = pmd_link;
1041 data->mac_addrs = eth_addr;
1043 /* We'll replace the 'data' originally allocated by eth_dev. So the
1044 * vhost PMD resources won't be shared between multi processes.
1046 eth_dev->data = data;
1047 eth_dev->dev_ops = &ops;
1048 eth_dev->driver = NULL;
1050 RTE_ETH_DEV_DETACHABLE | RTE_ETH_DEV_INTR_LSC;
1051 data->kdrv = RTE_KDRV_NONE;
1052 data->drv_name = internal->dev_name;
1053 data->numa_node = numa_node;
1055 /* finally assign rx and tx ops */
1056 eth_dev->rx_pkt_burst = eth_vhost_rx;
1057 eth_dev->tx_pkt_burst = eth_vhost_tx;
1059 if (rte_vhost_driver_register(iface_name, flags))
1062 /* We need only one message handling thread */
1063 if (rte_atomic16_add_return(&nb_started_ports, 1) == 1) {
1064 if (vhost_driver_session_start())
1068 return data->port_id;
1072 free(internal->dev_name);
1073 rte_free(vring_state);
1076 rte_eth_dev_release_port(eth_dev);
1085 open_iface(const char *key __rte_unused, const char *value, void *extra_args)
1087 const char **iface_name = extra_args;
1092 *iface_name = value;
1098 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1100 uint16_t *n = extra_args;
1102 if (value == NULL || extra_args == NULL)
1105 *n = (uint16_t)strtoul(value, NULL, 0);
1106 if (*n == USHRT_MAX && errno == ERANGE)
1113 rte_pmd_vhost_probe(const char *name, const char *params)
1115 struct rte_kvargs *kvlist = NULL;
1120 int client_mode = 0;
1121 int dequeue_zero_copy = 0;
1123 RTE_LOG(INFO, PMD, "Initializing pmd_vhost for %s\n", name);
1125 kvlist = rte_kvargs_parse(params, valid_arguments);
1129 if (rte_kvargs_count(kvlist, ETH_VHOST_IFACE_ARG) == 1) {
1130 ret = rte_kvargs_process(kvlist, ETH_VHOST_IFACE_ARG,
1131 &open_iface, &iface_name);
1139 if (rte_kvargs_count(kvlist, ETH_VHOST_QUEUES_ARG) == 1) {
1140 ret = rte_kvargs_process(kvlist, ETH_VHOST_QUEUES_ARG,
1141 &open_int, &queues);
1142 if (ret < 0 || queues > RTE_MAX_QUEUES_PER_PORT)
1148 if (rte_kvargs_count(kvlist, ETH_VHOST_CLIENT_ARG) == 1) {
1149 ret = rte_kvargs_process(kvlist, ETH_VHOST_CLIENT_ARG,
1150 &open_int, &client_mode);
1155 flags |= RTE_VHOST_USER_CLIENT;
1158 if (rte_kvargs_count(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY) == 1) {
1159 ret = rte_kvargs_process(kvlist, ETH_VHOST_DEQUEUE_ZERO_COPY,
1160 &open_int, &dequeue_zero_copy);
1164 if (dequeue_zero_copy)
1165 flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
1168 eth_dev_vhost_create(name, iface_name, queues, rte_socket_id(), flags);
1171 rte_kvargs_free(kvlist);
1176 rte_pmd_vhost_remove(const char *name)
1178 struct rte_eth_dev *eth_dev = NULL;
1179 struct pmd_internal *internal;
1180 struct internal_list *list;
1183 RTE_LOG(INFO, PMD, "Un-Initializing pmd_vhost for %s\n", name);
1185 /* find an ethdev entry */
1186 eth_dev = rte_eth_dev_allocated(name);
1187 if (eth_dev == NULL)
1190 internal = eth_dev->data->dev_private;
1191 if (internal == NULL)
1194 list = find_internal_resource(internal->iface_name);
1198 pthread_mutex_lock(&internal_list_lock);
1199 TAILQ_REMOVE(&internal_list, list, next);
1200 pthread_mutex_unlock(&internal_list_lock);
1203 eth_dev_stop(eth_dev);
1205 rte_vhost_driver_unregister(internal->iface_name);
1207 if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
1208 vhost_driver_session_stop();
1210 rte_free(vring_states[eth_dev->data->port_id]);
1211 vring_states[eth_dev->data->port_id] = NULL;
1213 free(internal->dev_name);
1214 free(internal->iface_name);
1216 for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
1217 rte_free(eth_dev->data->rx_queues[i]);
1218 for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1219 rte_free(eth_dev->data->tx_queues[i]);
1221 rte_free(eth_dev->data->mac_addrs);
1222 rte_free(eth_dev->data);
1225 rte_eth_dev_release_port(eth_dev);
1230 static struct rte_vdev_driver pmd_vhost_drv = {
1231 .probe = rte_pmd_vhost_probe,
1232 .remove = rte_pmd_vhost_remove,
1235 RTE_PMD_REGISTER_VDEV(net_vhost, pmd_vhost_drv);
1236 RTE_PMD_REGISTER_ALIAS(net_vhost, eth_vhost);
1237 RTE_PMD_REGISTER_PARAM_STRING(net_vhost,