From 137e7c72422f4e12baaf70d93a7e6ece1c7645f4 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Tue, 13 Sep 2016 19:52:06 +0200 Subject: [PATCH] qemu: Add a NetDevice slice to the Config structure The NetDevice structure represents a network device to be emulated by qemu. We also add the corresponding unit test. Signed-off-by: Samuel Ortiz --- qemu.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu_test.go | 21 +++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/qemu.go b/qemu.go index af09704c1f..e4824d40cd 100644 --- a/qemu.go +++ b/qemu.go @@ -175,6 +175,24 @@ type Knobs struct { NoGraphic bool } +// NetDevice represents a guest networking device +type NetDevice struct { + // Type is the netdev type (e.g. tap). + Type string + + // ID is the netdevice identifier. + ID string + + // IfName is the interface name, + IfName string + + // DownScript is the tap interface deconfiguration script. + DownScript string + + // Script is the tap interface configuration script. + Script string +} + // Config is the qemu configuration structure. // It allows for passing custom settings and parameters to the qemu API. type Config struct { @@ -202,6 +220,9 @@ type Config struct { // Devices is a list of devices for qemu to create. Devices []Device + // NetDevices is a list of networking devices for qemu to create. + NetDevices []NetDevice + // CharDevices is a list of character devices for qemu to export. CharDevices []string @@ -513,6 +534,37 @@ func appendKnobs(params []string, config Config) []string { return params } +func appendNetDevices(params []string, config Config) []string { + for _, d := range config.NetDevices { + if d.Type != "" { + var netdevParams []string + + netdevParams = append(netdevParams, fmt.Sprintf("%s", d.Type)) + + if d.ID != "" { + netdevParams = append(netdevParams, fmt.Sprintf(",id=%s", d.ID)) + } + + if d.IfName != "" { + netdevParams = append(netdevParams, fmt.Sprintf(",ifname=%s", d.IfName)) + } + + if d.DownScript != "" { + netdevParams = append(netdevParams, fmt.Sprintf(",downscript=%s", d.DownScript)) + } + + if d.Script != "" { + netdevParams = append(netdevParams, fmt.Sprintf(",script=%s", d.Script)) + } + + params = append(params, "-netdev") + params = append(params, strings.Join(netdevParams, "")) + } + } + + return params +} + // LaunchQemu can be used to launch a new qemu instance. // // The Config parameter contains a set of qemu parameters and settings. @@ -533,6 +585,7 @@ func LaunchQemu(config Config, logger QMPLog) (string, error) { params = appendMemory(params, config) params = appendCPUs(params, config) params = appendDevices(params, config) + params = appendNetDevices(params, config) params = appendCharDevices(params, config) params = appendFilesystemDevices(params, config) params = appendObjects(params, config) diff --git a/qemu_test.go b/qemu_test.go index 948d931dc1..4eef5bece8 100644 --- a/qemu_test.go +++ b/qemu_test.go @@ -81,6 +81,13 @@ func testAppend(structure interface{}, expected string, t *testing.T) { } params = appendQMPSocket([]string{}, config) + + case NetDevice: + config := Config{ + NetDevices: []NetDevice{s}, + } + + params = appendNetDevices([]string{}, config) } result := strings.Join(params, " ") @@ -258,3 +265,17 @@ func TestAppendStrings(t *testing.T) { t.Fatalf("Failed to append parameters [%s] != [%s]", result, qemuString) } } + +var netdevString = "-netdev tap,id=ceth0,ifname=ceth0,downscript=no,script=no" + +func TestAppendNetDevices(t *testing.T) { + netdev := NetDevice{ + Type: "tap", + ID: "ceth0", + IfName: "ceth0", + Script: "no", + DownScript: "no", + } + + testAppend(netdev, netdevString, t) +}