Skip to contentSkip to conversation

net/macos: implement vmnet-based netdev

PATCH Superseded Akihiko Odaki 2 participants 2 messages Mar 15, 2021

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/macos: implement vmnet-based netdev

From: Phillip Tennen <[email protected]>

This patch implements a new netdev device, reachable via -netdev vmnet-macos, that’s backed by macOS’s vmnet framework.

The vmnet framework provides native bridging support, and its usage in this patch is intended as a replacement for attempts to use a tap device via the tuntaposx kernel extension. Notably, the tap/tuntaposx approach never would have worked in the first place, as QEMU interacts with the tap device via poll(), and macOS does not support polling device files.

vmnet requires either a special entitlement, granted via a provisioning profile, or root access. Otherwise attempts to create the virtual interface will fail with a “generic error” status code. QEMU may not currently be signed with an entitlement granted in a provisioning profile, as this would necessitate pre-signed binary build distribution, rather than source-code distribution. As such, using this netdev currently requires that qemu be run with root access. I’ve opened a feedback report with Apple to allow the use of the relevant entitlement with this use case:
https://openradar.appspot.com/radar?id=5007417364447232

vmnet offers three operating modes, all of which are supported by this patch via the “mode=host|shared|bridge” option:

* "Host" mode: Allows the vmnet interface to communicate with other
* vmnet
interfaces that are in host mode and also with the native host.
* "Shared" mode: Allows traffic originating from the vmnet interface to reach the Internet through a NAT. The vmnet interface can also communicate with the native host.
* "Bridged" mode: Bridges the vmnet interface with a physical network interface.

Each of these modes also provide some extra configuration that’s supported by this patch:

* "Bridged" mode: The user may specify the physical interface to bridge with. Defaults to en0.
* "Host" mode / "Shared" mode: The user may specify the DHCP range and subnet. Allocated by vmnet if not provided.

vmnet also offers some extra configuration options that are not supported by this patch:

* Enable isolation from other VMs using vmnet
* Port forwarding rules
* Enabling TCP segmentation offload
* Only applicable in "shared" mode: specifying the NAT IPv6 prefix
* Only available in "host" mode: specifying the IP address for the VM within an isolated network

Note that this patch requires macOS 10.15 as a minimum, as this is when bridging support was implemented in vmnet.framework.

Rebased to commit 3f8d1885e48e4d72eab0688f604de62e0aea7a38 by Akihiko Odaki.

---
 meson.build       |   3 +
 net/clients.h     |   5 +
 net/meson.build   |   1 +
 net/net.c         |   3 +
 net/vmnet-macos.c | 447 ++++++++++++++++++++++++++++++++++++++++++++++
 qapi/net.json     | 120 ++++++++++++-
 qemu-options.hx   |   9 +
 7 files changed, 586 insertions(+), 2 deletions(-)
 create mode 100644 net/vmnet-macos.c
meson.build +3 -0
index 8b51b51c973..fb0cebce753 100644
@@ -178,6 +178,7 @@ socket = []
 version_res = []
 coref = []
 iokit = []
+vmnet = not_found
 emulator_link_args = []
 hvf = not_found
 if targetos == 'windows'
@@ -191,6 +192,7 @@ if targetos == 'windows'
 elif targetos == 'darwin'
   coref = dependency('appleframeworks', modules: 'CoreFoundation')
   iokit = dependency('appleframeworks', modules: 'IOKit')
+  vmnet = dependency('appleframeworks', modules: 'vmnet')
 elif targetos == 'sunos'
   socket = [cc.find_library('socket'),
             cc.find_library('nsl'),
@@ -1147,6 +1149,7 @@ config_host_data.set('CONFIG_FUSE', fuse.found())
 config_host_data.set('CONFIG_FUSE_LSEEK', fuse_lseek.found())
 config_host_data.set('CONFIG_X11', x11.found())
 config_host_data.set('CONFIG_CFI', get_option('cfi'))
+config_host_data.set('CONFIG_VMNET', vmnet.found())
 config_host_data.set('QEMU_VERSION', '"@0@"'.format(meson.project_version()))
 config_host_data.set('QEMU_VERSION_MAJOR', meson.project_version().split('.')[0])
 config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1])
net/clients.h +5 -0
index 92f9b59aedc..2c2af67f82a 100644
@@ -63,4 +63,9 @@ int net_init_vhost_user(const Netdev *netdev, const char *name,
 
 int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
                         NetClientState *peer, Error **errp);
+
+#ifdef CONFIG_VMNET
+int net_init_vmnet_macos(const Netdev *netdev, const char *name,
+                        NetClientState *peer, Error **errp);
+#endif
 #endif /* QEMU_NET_CLIENTS_H */
net/meson.build +1 -0
index 1076b0a7ab4..ba6a5b7fa0b 100644
@@ -37,5 +37,6 @@ endif
 softmmu_ss.add(when: 'CONFIG_POSIX', if_true: files(tap_posix))
 softmmu_ss.add(when: 'CONFIG_WIN32', if_true: files('tap-win32.c'))
 softmmu_ss.add(when: 'CONFIG_VHOST_NET_VDPA', if_true: files('vhost-vdpa.c'))
+softmmu_ss.add(when: vmnet, if_true: files('vmnet-macos.c'))
 
 subdir('can')
net/net.c +3 -0
index 9a2a4c99a5f..16f96cdeb66 100644
@@ -977,6 +977,9 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
 #ifdef CONFIG_L2TPV3
         [NET_CLIENT_DRIVER_L2TPV3]    = net_init_l2tpv3,
 #endif
+#ifdef CONFIG_VMNET
+        [NET_CLIENT_DRIVER_VMNET_MACOS] = net_init_vmnet_macos,
+#endif
 };
 
 
net/vmnet-macos.c new file +447 -0

447 lines not on this page · raw message

qapi/net.json +118 -2

147 lines not on this page · raw message

qemu-options.hx +9 -0
index 622d3bfa5a7..c9d68a46116 100644
@@ -2508,6 +2508,15 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
 #ifdef __linux__
     "-netdev vhost-vdpa,id=str,vhostdev=/path/to/dev\n"
     "                configure a vhost-vdpa network,Establish a vhost-vdpa netdev\n"
+#endif
+#ifdef CONFIG_VMNET
+    "-netdev vmnet-macos,id=str,mode=bridged[,ifname=ifname]\n"
+    "         configure a macOS-provided vmnet network in \"physical interface bridge\" mode\n"
+    "         the physical interface to bridge with defaults to en0 if unspecified\n"
+    "-netdev vmnet-macos,id=str,mode=host|shared\n"
+    "                     [,dhcp_start_address=addr,dhcp_end_address=addr,dhcp_subnet_mask=mask]\n"
+    "         configure a macOS-provided vmnet network in \"host\" or \"shared\" mode\n"
+    "         the DHCP configuration will be set automatically if unspecified\n"
 #endif
     "-netdev hubport,id=str,hubid=n[,netdev=nd]\n"
     "                configure a hub port on the hub with ID 'n'\n", QEMU_ARCH_ALL)
Signature · 1 line
2.24.3 (Apple Git-128)
#1 lore raw
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [PATCH] net/macos: implement vmnet-based netdev

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
4ceb242 net/macos: implement vmnet-based netdev

=== OUTPUT BEGIN ===
Use of uninitialized value $acpi_testexpected in string eq at ./scripts/checkpatch.pl line 1529.
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#144:
new file mode 100644

ERROR: externs should be avoided in .c files
#178: FILE: net/vmnet-macos.c:30:
+int net_init_vmnet_macos(const Netdev *netdev, const char *name,

WARNING: architecture specific defines should be avoided #203: FILE: net/vmnet-macos.c:55:
+#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 110000

ERROR: spaces required around that '^' (ctx:ExV)
#287: FILE: net/vmnet-macos.c:139:

+        ^bool(size_t index, xpc_object_t  _Nonnull value) {
         ^

ERROR: spaces required around that '^' (ctx:ExV)
#300: FILE: net/vmnet-macos.c:152:

+            ^bool(size_t index, xpc_object_t  _Nonnull value) {
             ^

WARNING: line over 80 characters
#385: FILE: net/vmnet-macos.c:237:
+ xpc_object_t iface_desc = _construct_vmnet_interface_description(vmnet_opts);

ERROR: spaces required around that '^' (ctx:ExV)
#419: FILE: net/vmnet-macos.c:271:

+        ^(vmnet_return_t status, xpc_object_t  _Nullable interface_param) {
         ^

ERROR: spaces required around that '^' (ctx:ExV)
#500: FILE: net/vmnet-macos.c:352:

+        ^(interface_event_t event_mask, xpc_object_t  _Nonnull event) {
         ^

WARNING: Block comments use a leading /* on a separate line #538: FILE: net/vmnet-macos.c:390:
+ /* Dispatch this block to a global queue instead of the main queue,

ERROR: spaces required around that '^' (ctx:ExV)
#545: FILE: net/vmnet-macos.c:397:

+                       ^{
                        ^

total: 6 errors, 4 warnings, 654 lines checked

Commit 4ceb242f281c (net/macos: implement vmnet-based netdev) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]