Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use file descriptors with virtio-serial and virtio-net #26

Merged
merged 5 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,26 @@ The `--device virtio-net` option adds a network interface to the virtual machine

#### Arguments
- `mac`: optional argument to specify the MAC address of the VM. If it's omitted, a random MAC address will be used.
- `fd`: file descriptor to attach to the guest network interface. The file descriptor must be a connected datagram socket. See [VZFileHandleNetworkDeviceAttachment](https://developer.apple.com/documentation/virtualization/vzfilehandlenetworkdeviceattachment?language=objc) for more details.
- `nat`: guest network traffic will be NAT'ed through the host. This is the default. See [VZNATNetworkDeviceAttachment](https://developer.apple.com/documentation/virtualization/vznatnetworkdeviceattachment?language=objc) for more details.
- `unixSocketPath: path to a unix socket to attach to the guest network interface. See See [VZFileHandleNetworkDeviceAttachment](https://developer.apple.com/documentation/virtualization/vzfilehandlenetworkdeviceattachment?language=objc) for more details.

`fd`, `nat`, `unixSocketPath` are mutually exclusive.

#### Example
`--device virtio-net,mac=52:54:00:70:2b:71`
`--device virtio-net,nat,mac=52:54:00:70:2b:71`


### Serial Port

#### Description

The `--device virtio-serial` option adds a serial device to the virtual machine. This is useful to redirect text output from the virtual machine to a log file.
The `logFilePath` and `stdio` arguments are mutually exclusive.

#### Arguments
- `logFilePath`: path where the serial port output should be written.
- `stdio`: uses stdin/stdout for the serial console input/output.

#### Example
`--device virtio-serial,logFilePath=/Users/virtuser/vfkit.log`
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/crc-org/vfkit
go 1.17

require (
github.com/Code-Hex/vz/v3 v3.0.0
github.com/Code-Hex/vz/v3 v3.0.4
github.com/docker/go-units v0.4.0
github.com/h2non/filetype v1.1.3
github.com/prashantgupta24/mac-sleep-notifier v1.0.1
Expand All @@ -19,7 +19,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/sys v0.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Code-Hex/vz/v3 v3.0.0 h1:UuYAZz8NF8n+R4MLfn/lpUlepgzZ8BlhKtioQ+q9LXk=
github.com/Code-Hex/vz/v3 v3.0.0/go.mod h1:+xPQOXVzNaZ4OeIIhlJFumtbFhsvRfqKwX7wuZS4dFA=
github.com/Code-Hex/vz/v3 v3.0.4 h1:oB74JSjSdIijjHkoYWG5ws7bHpx69m1w3PopFQdjFSc=
github.com/Code-Hex/vz/v3 v3.0.4/go.mod h1:+xPQOXVzNaZ4OeIIhlJFumtbFhsvRfqKwX7wuZS4dFA=
github.com/armon/go-proxyproto v0.0.0-20210323213023-7e956b284f0a/go.mod h1:QmP9hvJ91BbJmGVGSbutW19IC0Q9phDCLGaomwTJbgU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -43,8 +43,8 @@ golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down
32 changes: 32 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
Expand Down Expand Up @@ -75,6 +77,36 @@ func (vm *VirtualMachine) ToCmdLine() ([]string, error) {
return args, nil
}

func (vm *VirtualMachine) extraFiles() []*os.File {
extraFiles := []*os.File{}
for _, dev := range vm.Devices {
virtioNet, ok := dev.(*VirtioNet)
if !ok {
continue
}
if virtioNet.Socket != nil {
extraFiles = append(extraFiles, virtioNet.Socket)
}
}

return extraFiles
}

// Cmd creates an exec.Cmd to start vfkit with the configured devices.
// In particular it will set ExtraFiles appropriately when mapping
// a file with a network interface.
func (vm *VirtualMachine) Cmd(vfkitPath string) (*exec.Cmd, error) {
args, err := vm.ToCmdLine()
if err != nil {
return nil, err
}

cmd := exec.Command(vfkitPath, args...)
cmd.ExtraFiles = vm.extraFiles()

return cmd, nil
}

func (vm *VirtualMachine) AddDevicesFromCmdLine(cmdlineOpts []string) error {
for _, deviceOpts := range cmdlineOpts {
dev, err := deviceFromCmdLine(deviceOpts)
Expand Down
105 changes: 93 additions & 12 deletions pkg/config/virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"net"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -42,18 +43,21 @@ type VirtioRng struct {
// TODO: Add BridgedNetwork support
// https://github.com/Code-Hex/vz/blob/d70a0533bf8ed0fa9ab22fa4d4ca554b7c3f3ce5/network.go#L81-L82

// TODO: Add FileHandleNetwork support
// https://github.com/Code-Hex/vz/blob/d70a0533bf8ed0fa9ab22fa4d4ca554b7c3f3ce5/network.go#L109-L112

// VirtioNet configures the virtual machine networking.
type VirtioNet struct {
Nat bool
MacAddress net.HardwareAddr
// file parameter is holding a connected datagram socket.
// see https://github.com/Code-Hex/vz/blob/7f648b6fb9205d6f11792263d79876e3042c33ec/network.go#L113-L155
Socket *os.File

UnixSocketPath string
}

// VirtioSerial configures the virtual machine serial ports.
type VirtioSerial struct {
LogFile string
LogFile string
UsesStdio bool
}

// TODO: Add VirtioBalloon
Expand Down Expand Up @@ -131,10 +135,31 @@ func VirtioSerialNew(logFilePath string) (VirtioDevice, error) {
}, nil
}

func VirtioSerialNewStdio() (VirtioDevice, error) {
return &VirtioSerial{
UsesStdio: true,
}, nil
}

func (dev *VirtioSerial) validate() error {
if dev.LogFile != "" && dev.UsesStdio {
return fmt.Errorf("'logFilePath' and 'stdio' cannot be set at the same time")
}
if dev.LogFile == "" && !dev.UsesStdio {
return fmt.Errorf("One of 'logFilePath' or 'stdio' must be set")
}

return nil
}

func (dev *VirtioSerial) ToCmdLine() ([]string, error) {
if dev.LogFile == "" {
return nil, fmt.Errorf("virtio-serial needs the path to the log file")
if err := dev.validate(); err != nil {
return nil, err
}
if dev.UsesStdio {
return []string{"--device", "virtio-serial,stdio"}, nil
}

return []string{"--device", fmt.Sprintf("virtio-serial,logFilePath=%s", dev.LogFile)}, nil
}

Expand All @@ -143,16 +168,22 @@ func (dev *VirtioSerial) FromOptions(options []option) error {
switch option.key {
case "logFilePath":
dev.LogFile = option.value
case "stdio":
if option.value != "" {
return fmt.Errorf("Unexpected value for virtio-serial 'stdio' option: %s", option.value)
}
dev.UsesStdio = true
default:
return fmt.Errorf("Unknown option for virtio-serial devices: %s", option.key)
}
}
return nil

return dev.validate()
}

// VirtioNetNew creates a new network device for the virtual machine. It will
// use macAddress as its MAC address.
func VirtioNetNew(macAddress string) (VirtioDevice, error) {
func VirtioNetNew(macAddress string) (*VirtioNet, error) {
var hwAddr net.HardwareAddr

if macAddress != "" {
Expand All @@ -167,13 +198,54 @@ func VirtioNetNew(macAddress string) (VirtioDevice, error) {
}, nil
}

// Set the socket to use for the network communication
//
// This maps the virtual machine network interface to a connected datagram
// socket. This means all network traffic on this interface will go through
// file.
// file must be a connected datagram (SOCK_DGRAM) socket.
func (dev *VirtioNet) SetSocket(file *os.File) {
dev.Socket = file
dev.Nat = false
}

func (dev *VirtioNet) SetUnixSocketPath(path string) {
dev.UnixSocketPath = path
dev.Nat = false
}

func (dev *VirtioNet) validate() error {
if dev.Nat && dev.Socket != nil {
return fmt.Errorf("'nat' and 'fd' cannot be set at the same time")
}
if dev.Nat && dev.UnixSocketPath != "" {
return fmt.Errorf("'nat' and 'unixSocketPath' cannot be set at the same time")
}
if dev.Socket != nil && dev.UnixSocketPath != "" {
return fmt.Errorf("'fd' and 'unixSocketPath' cannot be set at the same time")
}
if !dev.Nat && dev.Socket == nil && dev.UnixSocketPath == "" {
return fmt.Errorf("One of 'nat' or 'fd' or 'unixSocketPath' must be set")
}

return nil
}

func (dev *VirtioNet) ToCmdLine() ([]string, error) {
if !dev.Nat {
return nil, fmt.Errorf("virtio-net only support 'nat' networking")
if err := dev.validate(); err != nil {
return nil, err
}

builder := strings.Builder{}
builder.WriteString("virtio-net")
builder.WriteString(",nat")
if dev.Nat {
builder.WriteString(",nat")
} else if dev.UnixSocketPath != "" {
fmt.Fprintf(&builder, ",unixSocketPath=%s", dev.UnixSocketPath)
} else {
fmt.Fprintf(&builder, ",fd=%d", dev.Socket.Fd())
}

if len(dev.MacAddress) != 0 {
builder.WriteString(fmt.Sprintf(",mac=%s", dev.MacAddress))
}
Expand All @@ -195,11 +267,20 @@ func (dev *VirtioNet) FromOptions(options []option) error {
return err
}
dev.MacAddress = macAddress
case "fd":
fd, err := strconv.Atoi(option.value)
if err != nil {
return err
}
dev.Socket = os.NewFile(uintptr(fd), "vfkit virtio-net socket")
case "unixSocketPath":
dev.UnixSocketPath = option.value
default:
return fmt.Errorf("Unknown option for virtio-net devices: %s", option.key)
}
}
return nil

return dev.validate()
}

// VirtioRngNew creates a new random number generator device to feed entropy
Expand Down
22 changes: 22 additions & 0 deletions pkg/config/virtio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,35 @@ var virtioDevTests = map[string]virtioDevTest{
},
expectedCmdLine: []string{"--device", "virtio-serial,logFilePath=/foo/bar.log"},
},
"NewVirtioSerialStdio": {
newDev: func() (VirtioDevice, error) { return VirtioSerialNewStdio() },
expectedDev: &VirtioSerial{
UsesStdio: true,
},
expectedCmdLine: []string{"--device", "virtio-serial,stdio"},
},
"NewVirtioNet": {
newDev: func() (VirtioDevice, error) { return VirtioNetNew("") },
expectedDev: &VirtioNet{
Nat: true,
},
expectedCmdLine: []string{"--device", "virtio-net,nat"},
},
"NewVirtioNetWithPath": {
newDev: func() (VirtioDevice, error) {
dev, err := VirtioNetNew("")
if err != nil {
return nil, err
}
dev.SetUnixSocketPath("/tmp/unix.sock")
return dev, nil
},
expectedDev: &VirtioNet{
Nat: false,
UnixSocketPath: "/tmp/unix.sock",
},
expectedCmdLine: []string{"--device", "virtio-net,unixSocketPath=/tmp/unix.sock"},
},
"NewVirtioNetWithMacAddress": {
newDev: func() (VirtioDevice, error) { return VirtioNetNew("00:11:22:33:44:55") },
expectedDev: &VirtioNet{
Expand Down
Loading