Skip to content

Commit

Permalink
refactor(vsock): call proxy.Close when vm stop
Browse files Browse the repository at this point in the history
When other projects use the `vf.ExposeVsock` method, there will be unexpected issues due to the absence of a close proxy.

Signed-off-by: Black-Hole1 <[email protected]>
  • Loading branch information
BlackHole1 committed Dec 26, 2023
1 parent 7572104 commit 9a07b63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 12 additions & 2 deletions cmd/vfkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e
}
log.Infof("virtual machine is running")

for _, vsock := range vmConfig.VirtioVsockDevices() {
vsockDevs := vmConfig.VirtioVsockDevices()
releases := make([]func(), 0, len(vsockDevs))
for _, vsock := range vsockDevs {
port := vsock.Port
socketURL := vsock.SocketURL
if socketURL == "" {
Expand All @@ -168,11 +170,19 @@ 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 {
if release, err := vf.ExposeVsock(vm, port, socketURL, vsock.Listen); err != nil {
log.Warnf("error exposing vsock port %d: %v", port, err)
} else {
releases = append(releases, release)
}
}

defer func() {
for _, r := range releases {
r()
}
}()

if err := setupGuestTimeSync(vm, vmConfig.TimeSync()); err != nil {
log.Warnf("Error configuring guest time synchronization")
log.Debugf("%v", err)
Expand Down
16 changes: 10 additions & 6 deletions pkg/vf/vsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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) (release func(), err error) {
if listen {
return listenVsock(vm, port, vsockPath)
}
Expand All @@ -36,7 +36,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) (release func(), err error) {

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

return func() {
_ = proxy.Close()
}, 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) (release func(), err 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 +119,7 @@ func listenVsock(vm *vz.VirtualMachine, port uint, vsockPath string) error {
}
},
})
// FIXME: defer proxy.Close()
return proxy.Start()
return func() {
_ = proxy.Close()
}, proxy.Start()
}

0 comments on commit 9a07b63

Please sign in to comment.