From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: passt.top; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=qYcUgWoQ; dkim-atps=neutral Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by passt.top (Postfix) with ESMTPS id CBC0A5A0271 for ; Sun, 02 Aug 2026 15:23:45 +0200 (CEST) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-495757ccbc1so11342545e9.2 for ; Sun, 02 Aug 2026 06:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785677025; x=1786281825; darn=passt.top; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MCYc0STSuTXkFiHM+T0Yh3kRe0vR2qPzgrJlRkIYyvU=; b=qYcUgWoQKKwgRYul0CMExZOUJpjv0WdpFXNMsxvxu2dfiwMw/MDsov9S/TolQc/ZOy wZUl+2KkfRfexnWv2jJZyutJhYh20n0/qqJbG1J/g3HxFGxznONRdisGIH9RU8tdaVnE HUzomyGIAX2Mr0xKguD/4Xrbh85rTzb28FaF9bSiA6ak8BCngrKp3FNYEwLQxpuAweNI PfL6u9gYpN8XVQuUnIraTAFAldYRQnPJiSWdr/imt3shOSxT+v37FlP72GUTvsYpwBVz eWoxZv3ceTVWCGJWMSMDdr3iILMqDsGRM5kS03AS/SjU7eOU4YADSu688gJ1bFw79a6y 2cDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785677025; x=1786281825; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=MC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47qm7k/jBnM4AuIJuCEM8mhtoUN+v1lF X-Received: by 2002:a05:600c:840f:b0:496:c379:b2a1 with SMTP id 5b1f17b1804b1-4980c66d7bbmr114985805e9.2.1785677025092; Sun, 02 Aug 2026 06:23:45 -0700 (PDT) Received: from localhost.localdomain ([196.137.15.128]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4980869106csm180745155e9.9.2026.08.02.06.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Aug 2026 06:23:44 -0700 (PDT) From: Ammar Yasser To: passt-dev@passt.top Subject: [RFC v3 4/8] virtio: Define the pasta vhost interface Date: Sun, 2 Aug 2026 13:21:51 +0000 Message-Id: <20260802132155.870796-5-aerosound161@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260802132155.870796-1-aerosound161@gmail.com> References: <20260802132155.870796-1-aerosound161@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: DD23PSNC4ACHFRXL3ERKVIBBBTQNDPAD X-Message-ID-Hash: DD23PSNC4ACHFRXL3ERKVIBBBTQNDPAD X-MailFrom: aerosound161@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: eperezma@redhat.com, Ammar Yasser X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: - Include the ioctls needed in opening, initializing and registering queues and fds with vhost-net - Create the vq_state type which represents from pasta's perspective our state of virtqueue processing - Define the structures that will represent the actual descriptor queues: vring_desc (the actual descriptor chain), vring_used_all (used ring for each queue), vring_avail_all (available ring for each queue), vhost_memory (the structure that carries the shape of the memory region as it should be shared with the kernel) - Increase PKT_BUF_BYTES by 1536 because the byte array that carries the frames needs to also account for virtio_net_hdr_mrg_rxbuf header sizes per each frame. - Define the signatures of functions that will be used in setting up the virtqueues with the kernel (set_vring_for_queue, setup_memory_table, setup_vhost_net, setup_eventfds). - Define the signature of rx_descriptor_handoff, which will be used to mark descriptors as available again to the driver, and vhost_kick so we can notify the driver of our updates Signed-off-by: Ammar Yasser --- passt.h | 2 +- virtio.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/passt.h b/passt.h index c729316..88a1b03 100644 --- a/passt.h +++ b/passt.h @@ -36,7 +36,7 @@ union epoll_ref; ((uint8_t [ETH_ALEN]){0x9a, 0x55, 0x9a, 0x55, 0x9a, 0x55}) /* Large enough for ~128 maximum size frames */ -#define PKT_BUF_BYTES (8UL << 20) +#define PKT_BUF_BYTES ((8UL << 20) + 1536) /* 128 * sizeof(virtio_net_hdr_mrg_rxbuf) */ extern char pkt_buf [PKT_BUF_BYTES]; diff --git a/virtio.h b/virtio.h index 8f2ae06..2d1eef1 100644 --- a/virtio.h +++ b/virtio.h @@ -9,14 +9,82 @@ #ifndef VIRTIO_H #define VIRTIO_H +#include #include +#include #include +struct ctx; + /* Maximum size of a virtqueue */ #define VIRTQUEUE_MAX_SIZE 1024 #define VNET_HLEN (sizeof(struct virtio_net_hdr_mrg_rxbuf)) +/* Keep in sync with PKT_BUF_BYTES in passt.h */ +#define PKT_BUF_BYTES ((8UL << 20) + 1536) /* 128 * sizeof(virtio_net_hdr_mrg_rxbuf) */ + +#define VHOST_NDESCS (PKT_BUF_BYTES / 65520) +static_assert(!(VHOST_NDESCS & (VHOST_NDESCS - 1)), + "Number of vhost descs must be a power of two by standard"); + + +#define VIRTQUEUE_MAX_SIZE 1024 + +#define VHOST_VIRTIO 0xAF +#define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64) +#define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64) +#define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01) +#define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory) +#define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state) +#define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr) +#define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file) +#define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file) +#define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file) +#define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64) +#define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file) + +/** + * struct vq_state - Per-virtqueue local descriptor tracking + * @num_free: Number of descriptors ready to be announced + * to the kernel as available via rx_descriptor_handoff() + * @last_used_idx: Number of used-ring entries consumed so far; + * lagging read cursor vs. vring_used->idx (the + * kernel's write cursor) + */ +extern struct vq_state { + uint16_t num_free; + uint16_t last_used_idx; + uint16_t next_free; +} vqs[2]; + + +extern struct vring_desc vring_desc[2][VHOST_NDESCS]; +union vring_avail_u { + struct vring_avail avail; + char buf[offsetof(struct vring_avail, ring[VHOST_NDESCS])]; +}; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +extern union vring_avail_u vring_avail_all[2]; +#pragma GCC diagnostic pop + +union vring_used_u { + struct vring_used used; + char buf[offsetof(struct vring_used, ring[VHOST_NDESCS])]; +}; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +extern union vring_used_u vring_used_all[2]; +#pragma GCC diagnostic pop + +#define N_VHOST_REGIONS 12 +union vhost_memory_u { + struct vhost_memory mem; + char buf[offsetof(struct vhost_memory, regions[N_VHOST_REGIONS])]; +}; +extern union vhost_memory_u vhost_memory; + /** * struct vu_ring - Virtqueue rings * @num: Size of the queue @@ -147,6 +215,7 @@ struct vu_virtq_element { struct iovec *out_sg; }; +void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq); /** * has_feature() - Check a feature bit in a features set * @features: Features set @@ -199,4 +268,11 @@ void vu_queue_fill(const struct vu_dev *vdev, struct vu_virtq *vq, unsigned int idx); void vu_queue_flush(const struct vu_dev *vdev, struct vu_virtq *vq, unsigned int count); +void set_vring_for_queue(struct ctx *c, int queue_idx, int tap_fd); +int setup_memory_table(struct ctx *c); +void setup_vhost_net(struct ctx *c); +void setup_eventfds(struct ctx *c, int queue_idx); +void rx_descriptor_handoff(struct ctx *c); +void vhost_kick(struct vring_used *used, int kick_fd); + #endif /* VIRTIO_H */ -- 2.34.1