mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-22 13:38:26 +00:00
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:
parent
c0e2aacad2
commit
137e7c7242
53
qemu.go
53
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)
|
||||
|
21
qemu_test.go
21
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user