#! /usr/bin/env python3 # # SPDX-License-Identifier: GPL-2.0-or-later # # PASST - Plug A Simple Socket Transport # for qemu/UNIX domain socket mode # # PASTA - Pack A Subtle Tap Abstraction # for network namespace/tap device mode # # test/vhost_user/multibuf.py - Guest frames split over several descriptors # # A minimal vhost-user front-end, standing in for qemu and a guest driver, that # sends a single UDP datagram with the frame split over two descriptors: # virtio-net header plus Ethernet, IP and UDP headers in the first one, payload # in the second one. This is how the Windows NetKVM driver submits frames with # VIRTIO_F_ANY_LAYOUT, while Linux guests use a single buffer for the frame. import contextlib import mmap import os import socket import struct import subprocess import tempfile import time from typing import Iterator import exeter PASST = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'passt') # vhost-user requests GET_FEATURES, SET_FEATURES, SET_OWNER, SET_MEM_TABLE = 1, 2, 3, 5 SET_VRING_NUM, SET_VRING_ADDR, SET_VRING_BASE = 8, 9, 10 SET_VRING_KICK, SET_VRING_CALL = 12, 13 F_VERSION_1, F_MRG_RXBUF = 1 << 32, 1 << 15 QSIZE = 16 MEMSIZE = 1 << 20 QVA = 0x7f0000000000 # Address of guest memory for the "front-end" DESC, AVAIL, USED = 0x0, 0x1000, 0x2000 # Ring offsets in guest memory DATA = 0x10000 # Packet buffers in guest memory GUEST_IP, GW_IP = '10.0.2.15', '10.0.2.2' # GW_IP mapped to host loopback GUEST_MAC = bytes.fromhex('525400123456') PASST_MAC = bytes.fromhex('9a559a559a55') def ip_csum(hdr: bytes) -> int: s: int = sum(struct.unpack(f'!{len(hdr) // 2}H', hdr)) while s >> 16: s = (s & 0xffff) + (s >> 16) return ~s & 0xffff def udp_frame(src: str, dst: str, port: int, payload: bytes) -> bytes: """Ethernet, IPv4 and UDP headers for payload (UDP checksum disabled)""" eth = PASST_MAC + GUEST_MAC + b'\x08\x00' ip = struct.pack('!BBHHHBBH4s4s', 0x45, 0, 20 + 8 + len(payload), 0, 0x4000, 64, socket.IPPROTO_UDP, 0, socket.inet_aton(src), socket.inet_aton(dst)) ip = ip[:10] + struct.pack('!H', ip_csum(ip)) + ip[12:] udp = struct.pack('!HHHH', 40000, port, 8 + len(payload), 0) return eth + ip + udp class VhostUser: def __init__(self, path: str) -> None: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.sock.connect(path) self.memfd = os.memfd_create('guest', 0) os.ftruncate(self.memfd, MEMSIZE) self.mem = mmap.mmap(self.memfd, MEMSIZE) def msg(self, req: int, payload: bytes = b'', fds: tuple[int, ...] = (), reply: bool = False) -> bytes: hdr = struct.pack(' None: feat = struct.unpack(' None: """Queue one frame on TX as a descriptor chain, one buffer each""" base, addr = 0x8000, DATA for i, b in enumerate(bufs): self.mem[addr:addr + len(b)] = b more = i < len(bufs) - 1 struct.pack_into(' Iterator[VhostUser]: with tempfile.TemporaryDirectory() as tmpdir: path = os.path.join(tmpdir, 'vu.sock') proc = subprocess.Popen([PASST, '-f', '-q', '--vhost-user', '--socket', path, '-4', '-a', GUEST_IP, '-n', '24', '-g', GW_IP, '--map-host-loopback', GW_IP]) try: for _ in range(100): if os.path.exists(path): break time.sleep(0.05) yield VhostUser(path) finally: proc.kill() proc.wait() def guest_udp(bufs_of: str) -> None: payload = bytes(range(32)) host = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) host.bind(('127.0.0.1', 0)) host.settimeout(1) port = host.getsockname()[1] with passt_vu() as vu: vu.setup() hdrs = udp_frame(GUEST_IP, GW_IP, port, payload) vnet = bytes(12) if bufs_of == 'single': vu.send([vnet + hdrs + payload]) else: vu.send([vnet + hdrs, payload]) got = [] with contextlib.suppress(socket.timeout): while True: got.append(host.recv(65536)) assert got == [payload], f'expected one datagram {payload!r}, got {got!r}' @exeter.test def udp_single_buffer() -> None: """Guest UDP datagram in one descriptor reaches host once, intact""" guest_udp('single') @exeter.test def udp_split_headers_payload() -> None: """Guest UDP datagram split over two descriptors reaches host once""" guest_udp('split') if __name__ == '__main__': exeter.main()