Skip to content

Commit 054eacc

Browse files
vyasgunpraveenkumar
authored andcommitted
Use virtio-net device for forwarding networking data between the guest and the host
1 parent 695cab8 commit 054eacc

File tree

8 files changed

+77
-18
lines changed

8 files changed

+77
-18
lines changed

cmd/crc/cmd/daemon.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func createNewVirtualNetworkConfig(providedConfig *crcConfig.Config) types.Confi
8181
GatewayIP: constants.VSockGateway,
8282
GatewayMacAddress: "5a:94:ef:e4:0c:dd",
8383
DHCPStaticLeases: map[string]string{
84-
"192.168.127.2": "5a:94:ef:e4:0c:ee",
84+
"192.168.127.2": constants.VsockMacAddress,
8585
},
8686
DNS: []types.Zone{
8787
{
@@ -146,11 +146,6 @@ func createNewVirtualNetworkConfig(providedConfig *crcConfig.Config) types.Confi
146146
}
147147

148148
func run(configuration *types.Configuration) error {
149-
vsockListener, err := vsockListener()
150-
if err != nil {
151-
return err
152-
}
153-
154149
vn, err := virtualnetwork.New(configuration)
155150
if err != nil {
156151
return err
@@ -213,6 +208,21 @@ func run(configuration *types.Configuration) error {
213208
}
214209
}()
215210

211+
go func() {
212+
_, err := unixgramListener(vn)
213+
if err != nil {
214+
// Don't return an error if the connection is closed
215+
if !errors.Is(err, net.ErrClosed) {
216+
errCh <- err
217+
}
218+
return
219+
}
220+
}()
221+
222+
vsockListener, err := vsockListener()
223+
if err != nil {
224+
return err
225+
}
216226
go func() {
217227
mux := http.NewServeMux()
218228
mux.Handle(types.ConnectPath, vn.Mux())

cmd/crc/cmd/daemon_darwin.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package cmd
22

33
import (
4+
"context"
5+
"fmt"
46
"net"
57
"os"
68

9+
"github.com/containers/gvisor-tap-vsock/pkg/transport"
10+
"github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
711
"github.com/crc-org/crc/v2/pkg/crc/constants"
812
"github.com/crc-org/crc/v2/pkg/crc/logging"
13+
"github.com/pkg/errors"
914
)
1015

1116
func vsockListener() (net.Listener, error) {
@@ -28,6 +33,21 @@ func httpListener() (net.Listener, error) {
2833
return ln, nil
2934
}
3035

36+
func unixgramListener(vn *virtualnetwork.VirtualNetwork) (*net.UnixConn, error) {
37+
_ = os.Remove(constants.UnixgramSocketPath)
38+
conn, err := transport.ListenUnixgram(fmt.Sprintf("unixgram://%v", constants.UnixgramSocketPath))
39+
if err != nil {
40+
return conn, errors.Wrap(err, "failed to listen unixgram")
41+
}
42+
logging.Infof("listening on %s:", constants.UnixgramSocketPath)
43+
vfkitConn, err := transport.AcceptVfkit(conn)
44+
if err != nil {
45+
return conn, errors.Wrap(err, "failed to accept vfkit connection")
46+
}
47+
err = vn.AcceptVfkit(context.Background(), vfkitConn)
48+
return conn, errors.Wrap(err, "failed to accept vfkit connection")
49+
}
50+
3151
func checkIfDaemonIsRunning() (bool, error) {
3252
return checkDaemonVersion()
3353
}

cmd/crc/cmd/daemon_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
"github.com/containers/gvisor-tap-vsock/pkg/transport"
9+
"github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
910
"github.com/crc-org/crc/v2/pkg/crc/constants"
1011
"github.com/crc-org/crc/v2/pkg/crc/logging"
1112

@@ -124,6 +125,10 @@ func httpListener() (net.Listener, error) {
124125
return ln, nil
125126
}
126127

128+
func unixgramListener(_ *virtualnetwork.VirtualNetwork) (*net.UnixConn, error) {
129+
return nil, nil
130+
}
131+
127132
func startupDone() {
128133
_, _ = daemon.SdNotify(false, daemon.SdNotifyReady)
129134
}

cmd/crc/cmd/daemon_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/Microsoft/go-winio"
77
"github.com/containers/gvisor-tap-vsock/pkg/transport"
8+
"github.com/containers/gvisor-tap-vsock/pkg/virtualnetwork"
89
"github.com/crc-org/crc/v2/pkg/crc/constants"
910
"github.com/crc-org/crc/v2/pkg/crc/logging"
1011
)
@@ -35,5 +36,9 @@ func checkIfDaemonIsRunning() (bool, error) {
3536
return checkDaemonVersion()
3637
}
3738

39+
func unixgramListener(_ *virtualnetwork.VirtualNetwork) (*net.UnixConn, error) {
40+
return nil, nil
41+
}
42+
3843
func startupDone() {
3944
}

pkg/crc/constants/constants.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ const (
3939
RootlessPodmanSocket = "/run/user/1000/podman/podman.sock"
4040
RootfulPodmanSocket = "/run/podman/podman.sock"
4141

42-
VSockGateway = "192.168.127.1"
43-
VsockSSHPort = 2222
44-
LocalIP = "127.0.0.1"
42+
VSockGateway = "192.168.127.1"
43+
VsockSSHPort = 2222
44+
LocalIP = "127.0.0.1"
45+
VsockMacAddress = "5a:94:ef:e4:0c:ee"
4546

4647
OkdPullSecret = `{"auths":{"fake":{"auth": "Zm9vOmJhcgo="}}}` // #nosec G101
4748

pkg/crc/constants/constants_darwin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ const (
1414
var (
1515
TapSocketPath = filepath.Join(CrcBaseDir, "tap.sock")
1616
DaemonHTTPSocketPath = filepath.Join(CrcBaseDir, "crc-http.sock")
17+
UnixgramSocketPath = filepath.Join(CrcBaseDir, "crc-unixgram.sock")
1718
)

pkg/crc/constants/constants_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ const (
77
DaemonHTTPNamedPipe = `\\.\pipe\crc-http`
88
DaemonTaskName = "crcDaemon"
99
AdminHelperServiceName = "crcAdminHelper"
10+
UnixgramSocketPath = ""
1011
)

pkg/drivers/vfkit/driver_darwin.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type Driver struct {
4747
VsockPath string
4848
DaemonVsockPort uint
4949
QemuGAVsockPort uint
50+
51+
UnixgramMacAddress string
52+
UnixgramSockPath string
5053
}
5154

5255
func NewDriver(hostName, storePath string) *Driver {
@@ -63,7 +66,9 @@ func NewDriver(hostName, storePath string) *Driver {
6366
},
6467
// needed when loading a VM which was created before
6568
// DaemonVsockPort was introduced
66-
DaemonVsockPort: constants.DaemonVsockPort,
69+
DaemonVsockPort: constants.DaemonVsockPort,
70+
UnixgramSockPath: constants.UnixgramSocketPath,
71+
UnixgramMacAddress: constants.VsockMacAddress,
6772
}
6873
}
6974

@@ -246,14 +251,25 @@ func (d *Driver) Start() error {
246251
return err
247252
}
248253

249-
// virtio-vsock device
250-
dev, err = config.VirtioVsockNew(d.DaemonVsockPort, d.VsockPath, true)
251-
if err != nil {
252-
return err
253-
}
254-
err = vm.AddDevice(dev)
255-
if err != nil {
256-
return err
254+
if d.UnixgramSockPath == "" {
255+
dev, err = config.VirtioVsockNew(d.DaemonVsockPort, d.VsockPath, true)
256+
if err != nil {
257+
return err
258+
}
259+
err = vm.AddDevice(dev)
260+
if err != nil {
261+
return err
262+
}
263+
} else {
264+
netDev, err := config.VirtioNetNew(d.UnixgramMacAddress)
265+
if err != nil {
266+
return err
267+
}
268+
netDev.SetUnixSocketPath(d.UnixgramSockPath)
269+
err = vm.AddDevice(netDev)
270+
if err != nil {
271+
return err
272+
}
257273
}
258274

259275
// when loading a VM created by a crc version predating this commit,

0 commit comments

Comments
 (0)