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

refactor(vsock): call proxy.Close when vm stop #74

Merged
merged 1 commit into from
Jan 24, 2024
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
8 changes: 6 additions & 2 deletions cmd/vfkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
}
log.Infof("virtual machine is running")

for _, vsock := range vmConfig.VirtioVsockDevices() {
vsockDevs := vmConfig.VirtioVsockDevices()
for _, vsock := range vsockDevs {
port := vsock.Port
socketURL := vsock.SocketURL
if socketURL == "" {
Expand All @@ -163,9 +164,12 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
listenStr = " (listening)"
}
log.Infof("Exposing vsock port %d on %s%s", port, socketURL, listenStr)
if err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen); err != nil {
closer, err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen)
if err != nil {
log.Warnf("error exposing vsock port %d: %v", port, err)
continue
}
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
defer closer.Close()
}

if err := setupGuestTimeSync(vm, vmConfig.TimeSync()); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions pkg/vf/vsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vf
import (
"context"
"fmt"
"io"
"net"
"net/url"
"strconv"
Expand All @@ -11,7 +12,7 @@ import (
"inet.af/tcpproxy"
)

func ExposeVsock(vm *vz.VirtualMachine, port uint, vsockPath string, listen bool) error {
func ExposeVsock(vm *vz.VirtualMachine, port uint, vsockPath string, listen bool) (io.Closer, error) {
if listen {
return listenVsock(vm, port, vsockPath)
}
Expand All @@ -36,7 +37,7 @@ func ConnectVsockSync(vm *vz.VirtualMachine, port uint) (net.Conn, error) {

// connectVsock proxies connections from a host unix socket to a vsock port
// This allows the host to initiate connections to the guest over vsock
func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) (io.Closer, error) {

var proxy tcpproxy.Proxy
// listen for connections on the host unix socket
Expand Down Expand Up @@ -70,12 +71,12 @@ func connectVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
}
},
})
return proxy.Start()
return &proxy, proxy.Start()
}

// listenVsock proxies connections from a vsock port to a host unix socket.
// This allows the guest to initiate connections to the host over vsock
func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) (io.Closer, error) {
var proxy tcpproxy.Proxy
// listen for connections on the vsock port
proxy.ListenFunc = func(_, laddr string) (net.Listener, error) {
Expand Down Expand Up @@ -116,6 +117,6 @@ func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
}
},
})
// FIXME: defer proxy.Close()
return proxy.Start()

return &proxy, proxy.Start()
}
Loading