Bhyve supports SLIRP networking using the following syntax:

 -s 12:0,e1000,slirp,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 <[email protected]>
---
 src/bhyve/bhyve_command.c                     |  6 +++-
 .../bhyvexml2argv-slirp.args                  | 10 ++++++
 .../bhyvexml2argv-slirp.ldargs                |  4 +++
 .../bhyvexml2argvdata/bhyvexml2argv-slirp.xml | 21 ++++++++++++
 tests/bhyvexml2argvtest.c                     |  1 +
 .../bhyvexml2xmlout-slirp.xml                 | 32 +++++++++++++++++++
 tests/bhyvexml2xmltest.c                      |  1 +
 7 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args
 create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs
 create mode 100644 tests/bhyvexml2argvdata/bhyvexml2argv-slirp.xml
 create mode 100644 tests/bhyvexml2xmloutdata/bhyvexml2xmlout-slirp.xml

diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
index e42e7b1ecc..b7e47084e6 100644
--- a/src/bhyve/bhyve_command.c
+++ b/src/bhyve/bhyve_command.c
@@ -93,9 +93,13 @@ bhyveBuildNetArgStr(const virDomainDef *def,
             goto cleanup;
         }
         break;
+    case VIR_DOMAIN_NET_TYPE_USER:
+        virCommandAddArg(cmd, "-s");
+        virCommandAddArgFormat(cmd, "%d:0,%s,slirp,open",
+                               net->info.addr.pci.slot, nic_model);
+        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:
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args 
b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args
new file mode 100644
index 0000000000..ff9a90ebc9
--- /dev/null
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.args
@@ -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,slirp,open \
+bhyve
diff --git a/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs 
b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs
new file mode 100644
index 0000000000..5905f4b3e6
--- /dev/null
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.ldargs
@@ -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
index 0000000000..5cf13d2a20
--- /dev/null
+++ b/tests/bhyvexml2argvdata/bhyvexml2argv-slirp.xml
@@ -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>
diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c
index 650313c2da..ee3d677947 100644
--- a/tests/bhyvexml2argvtest.c
+++ b/tests/bhyvexml2argvtest.c
@@ -270,6 +270,7 @@ mymain(void)
     DO_TEST("sata-rotation-rate");
     DO_TEST_FAILURE("disk-virtio-rotation-rate");
     DO_TEST_FAILURE("disk-virtio-queue-opts");
+    DO_TEST("slirp");
 
     /* 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
index 0000000000..44d2616f6c
--- /dev/null
+++ b/tests/bhyvexml2xmloutdata/bhyvexml2xmlout-slirp.xml
@@ -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>
diff --git a/tests/bhyvexml2xmltest.c b/tests/bhyvexml2xmltest.c
index c2458b9fc4..0abc50b0de 100644
--- a/tests/bhyvexml2xmltest.c
+++ b/tests/bhyvexml2xmltest.c
@@ -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");
-- 
2.51.2

Reply via email to