]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
bhyve: implement SLIRP networking
authorRoman Bogorodskiy <bogorodskiy@gmail.com>
Sun, 11 May 2025 15:42:26 +0000 (17:42 +0200)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Tue, 16 Dec 2025 18:25:15 +0000 (19:25 +0100)
Bhyve supports SLIRP networking using the following syntax:

 -s 12:0,e1000,slirp,mac=<macaddr>,open

Where "e1000" is a NIC model, "slirp" is the SLIRP backend and "open"
specifies open mode where external network is available to the guest.

The "open" mode is a recent addition in FreeBSD -CURRENT.
Unfortunately, bhyve does not provide a way to probe whether
the open mode is supported, so users will have to make sure
it's supported on their own.

For the reference, without the "open" mode, the guest will have no
outside network connectivity. To make this mode useful,
it is possible to configure forwarding from the host to the guest,
but it is not covered by this patch.

Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
12 files changed:
src/bhyve/bhyve_command.c
src/bhyve/bhyve_domain.c
tests/bhyvexml2argvdata/bhyvexml2argv-slirp-ip.xml [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.args [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.ldargs [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.xml [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs [new file with mode: 0644]
tests/bhyvexml2argvdata/bhyvexml2argv-slirp.xml [new file with mode: 0644]
tests/bhyvexml2argvtest.c
tests/bhyvexml2xmloutdata/bhyvexml2xmlout-slirp.xml [new file with mode: 0644]
tests/bhyvexml2xmltest.c

index e42e7b1eccce034c2ed4aa361bc00a65aebe2a1e..bc37f4cef97bbc4b49799ea92c5a1d4162e5aace 100644 (file)
@@ -83,6 +83,8 @@ bhyveBuildNetArgStr(const virDomainDef *def,
     if (virDomainActualNetDefValidate(net) < 0)
         return -1;
 
+    virMacAddrFormat(&net->mac, macaddr);
+
     switch (actualType) {
     case VIR_DOMAIN_NET_TYPE_NETWORK:
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
@@ -93,9 +95,13 @@ bhyveBuildNetArgStr(const virDomainDef *def,
             goto cleanup;
         }
         break;
+    case VIR_DOMAIN_NET_TYPE_USER:
+        virCommandAddArg(cmd, "-s");
+        virCommandAddArgFormat(cmd, "%d:0,%s,slirp,mac=%s,open",
+                               net->info.addr.pci.slot, nic_model, macaddr);
+        return 0;
     case VIR_DOMAIN_NET_TYPE_ETHERNET:
     case VIR_DOMAIN_NET_TYPE_DIRECT:
-    case VIR_DOMAIN_NET_TYPE_USER:
     case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
     case VIR_DOMAIN_NET_TYPE_SERVER:
     case VIR_DOMAIN_NET_TYPE_CLIENT:
@@ -144,7 +150,7 @@ bhyveBuildNetArgStr(const virDomainDef *def,
     virCommandAddArg(cmd, "-s");
     virCommandAddArgFormat(cmd, "%d:0,%s,%s,mac=%s",
                            net->info.addr.pci.slot, nic_model,
-                           realifname, virMacAddrFormat(&net->mac, macaddr));
+                           realifname, macaddr);
 
     ret = 0;
  cleanup:
index 4c9ed29333b4739d9228da9b2d394595dc0f18bd..b9f9761b431125c46bd7a4284c249f19c8e93052 100644 (file)
@@ -325,6 +325,17 @@ bhyveDomainDeviceDefValidate(const virDomainDeviceDef *dev,
 
         break;
     }
+    case VIR_DOMAIN_DEVICE_NET: {
+        virDomainNetDef *net = dev->data.net;
+
+        if (net->type == VIR_DOMAIN_NET_TYPE_USER) {
+            if (net->guestIP.nips) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("setting IP addresses for SLIRP networking is not supported"));
+                return -1;
+            }
+        }
+    }
     case VIR_DOMAIN_DEVICE_AUDIO:
     case VIR_DOMAIN_DEVICE_CRYPTO:
     case VIR_DOMAIN_DEVICE_FS:
@@ -337,7 +348,6 @@ bhyveDomainDeviceDefValidate(const virDomainDeviceDef *dev,
     case VIR_DOMAIN_DEVICE_LEASE:
     case VIR_DOMAIN_DEVICE_MEMBALLOON:
     case VIR_DOMAIN_DEVICE_MEMORY:
-    case VIR_DOMAIN_DEVICE_NET:
     case VIR_DOMAIN_DEVICE_NONE:
     case VIR_DOMAIN_DEVICE_NVRAM:
     case VIR_DOMAIN_DEVICE_PANIC:
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-ip.xml b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-ip.xml
new file mode 100644 (file)
index 0000000..0152066
--- /dev/null
@@ -0,0 +1,22 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
+  <memory>219136</memory>
+  <vcpu>1</vcpu>
+  <os>
+    <type>hvm</type>
+  </os>
+  <devices>
+    <disk type='file'>
+      <driver name='file' type='raw'/>
+      <source file='/tmp/freebsd.img'/>
+      <target dev='hda' bus='sata'/>
+      <address type='drive' controller='0' bus='0' target='2' unit='0'/>
+    </disk>
+    <interface type='user'>
+      <model type='e1000'/>
+      <ip family='ipv4' address='172.17.1.1' prefix='16'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
+    </interface>
+  </devices>
+</domain>
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.args b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.args
new file mode 100644 (file)
index 0000000..0c766d1
--- /dev/null
@@ -0,0 +1,10 @@
+bhyve \
+-c 1 \
+-m 214 \
+-u \
+-H \
+-P \
+-s 0:0,hostbridge \
+-s 2:0,ahci,hd:/tmp/freebsd.img \
+-s 3:0,e1000,mac=00:11:22:33:44:56,slirp,open \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.ldargs b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.ldargs
new file mode 100644 (file)
index 0000000..5905f4b
--- /dev/null
@@ -0,0 +1,4 @@
+bhyveload \
+-m 214 \
+-d /tmp/freebsd.img \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.xml b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp-mac-addr.xml
new file mode 100644 (file)
index 0000000..b573d72
--- /dev/null
@@ -0,0 +1,22 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
+  <memory>219136</memory>
+  <vcpu>1</vcpu>
+  <os>
+    <type>hvm</type>
+  </os>
+  <devices>
+    <disk type='file'>
+      <driver name='file' type='raw'/>
+      <source file='/tmp/freebsd.img'/>
+      <target dev='hda' bus='sata'/>
+      <address type='drive' controller='0' bus='0' target='2' unit='0'/>
+    </disk>
+    <interface type='user'>
+      <model type='e1000'/>
+      <mac address="00:11:22:33:44:56"/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
+    </interface>
+  </devices>
+</domain>
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args
new file mode 100644 (file)
index 0000000..9866625
--- /dev/null
@@ -0,0 +1,10 @@
+bhyve \
+-c 1 \
+-m 214 \
+-u \
+-H \
+-P \
+-s 0:0,hostbridge \
+-s 2:0,ahci,hd:/tmp/freebsd.img \
+-s 3:0,e1000,mac=52:54:00:00:00:00,slirp,open \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs
new file mode 100644 (file)
index 0000000..5905f4b
--- /dev/null
@@ -0,0 +1,4 @@
+bhyveload \
+-m 214 \
+-d /tmp/freebsd.img \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.xml b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.xml
new file mode 100644 (file)
index 0000000..5cf13d2
--- /dev/null
@@ -0,0 +1,21 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
+  <memory>219136</memory>
+  <vcpu>1</vcpu>
+  <os>
+    <type>hvm</type>
+  </os>
+  <devices>
+    <disk type='file'>
+      <driver name='file' type='raw'/>
+      <source file='/tmp/freebsd.img'/>
+      <target dev='hda' bus='sata'/>
+      <address type='drive' controller='0' bus='0' target='2' unit='0'/>
+    </disk>
+    <interface type='user'>
+      <model type='e1000'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
+    </interface>
+  </devices>
+</domain>
index 650313c2da1ec1cbed02b672fc7954fc127d3a42..b83051d3cd97750d4d30b4d840e94272e088ccc2 100644 (file)
@@ -270,6 +270,9 @@ mymain(void)
     DO_TEST("sata-rotation-rate");
     DO_TEST_FAILURE("disk-virtio-rotation-rate");
     DO_TEST_FAILURE("disk-virtio-queue-opts");
+    DO_TEST("slirp");
+    DO_TEST("slirp-mac-addr");
+    DO_TEST_FAILURE("slirp-ip");
 
     /* Address allocation tests */
     DO_TEST("addr-single-sata-disk");
diff --git a/tests/bhyvexml2xmloutdata/bhyvexml2xmlout-slirp.xml b/tests/bhyvexml2xmloutdata/bhyvexml2xmlout-slirp.xml
new file mode 100644 (file)
index 0000000..44d2616
--- /dev/null
@@ -0,0 +1,32 @@
+<domain type='bhyve'>
+  <name>bhyve</name>
+  <uuid>df3be7e7-a104-11e3-aeb0-50e5492bd3dc</uuid>
+  <memory unit='KiB'>219136</memory>
+  <currentMemory unit='KiB'>219136</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <driver name='file' type='raw'/>
+      <source file='/tmp/freebsd.img'/>
+      <target dev='hda' bus='sata'/>
+      <address type='drive' controller='0' bus='0' target='2' unit='0'/>
+    </disk>
+    <controller type='pci' index='0' model='pci-root'/>
+    <controller type='sata' index='0'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
+    </controller>
+    <interface type='user'>
+      <mac address='52:54:00:00:00:00'/>
+      <model type='e1000'/>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
+    </interface>
+  </devices>
+</domain>
index c2458b9fc4f1b2a7d52c7045f91abeb2e1459e3a..0abc50b0de8e014a89804be6bd56423e7866f419 100644 (file)
@@ -122,6 +122,7 @@ mymain(void)
     DO_TEST_DIFFERENT("nvme");
     DO_TEST_DIFFERENT("2-nvme-2-controllers");
     DO_TEST_DIFFERENT("passthru-multiple-devs");
+    DO_TEST_DIFFERENT("slirp");
 
     /* Address allocation tests */
     DO_TEST_DIFFERENT("addr-single-sata-disk");