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 <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2016-09-13 19:52:06 +02:00
parent c0e2aacad2
commit 137e7c7242
2 changed files with 74 additions and 0 deletions

53
qemu.go
View File

@ -175,6 +175,24 @@ type Knobs struct {
NoGraphic bool 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. // Config is the qemu configuration structure.
// It allows for passing custom settings and parameters to the qemu API. // It allows for passing custom settings and parameters to the qemu API.
type Config struct { type Config struct {
@ -202,6 +220,9 @@ type Config struct {
// Devices is a list of devices for qemu to create. // Devices is a list of devices for qemu to create.
Devices []Device 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 is a list of character devices for qemu to export.
CharDevices []string CharDevices []string
@ -513,6 +534,37 @@ func appendKnobs(params []string, config Config) []string {
return params 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. // LaunchQemu can be used to launch a new qemu instance.
// //
// The Config parameter contains a set of qemu parameters and settings. // 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 = appendMemory(params, config)
params = appendCPUs(params, config) params = appendCPUs(params, config)
params = appendDevices(params, config) params = appendDevices(params, config)
params = appendNetDevices(params, config)
params = appendCharDevices(params, config) params = appendCharDevices(params, config)
params = appendFilesystemDevices(params, config) params = appendFilesystemDevices(params, config)
params = appendObjects(params, config) params = appendObjects(params, config)

View File

@ -81,6 +81,13 @@ func testAppend(structure interface{}, expected string, t *testing.T) {
} }
params = appendQMPSocket([]string{}, config) params = appendQMPSocket([]string{}, config)
case NetDevice:
config := Config{
NetDevices: []NetDevice{s},
}
params = appendNetDevices([]string{}, config)
} }
result := strings.Join(params, " ") result := strings.Join(params, " ")
@ -258,3 +265,17 @@ func TestAppendStrings(t *testing.T) {
t.Fatalf("Failed to append parameters [%s] != [%s]", result, qemuString) 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)
}