Skip to content

Commit

Permalink
Use vpnkit socket for the communication between the daemon and the VM…
Browse files Browse the repository at this point in the history
… on macOS
  • Loading branch information
guillaumerose authored and praveenkumar committed Sep 20, 2021
1 parent 61e85b5 commit cf173bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
37 changes: 32 additions & 5 deletions cmd/crc/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"regexp"
"runtime"
"syscall"
"time"

Expand Down Expand Up @@ -65,7 +66,7 @@ var daemonCmd = &cobra.Command{
virtualNetworkConfig := types.Configuration{
Debug: false, // never log packets
CaptureFile: os.Getenv("CRC_DAEMON_PCAP_FILE"),
MTU: 4000, // Large packets slightly improve the performance. Less small packets.
MTU: mtu(), // Large packets slightly improve the performance. Less small packets.
Subnet: "192.168.127.0/24",
GatewayIP: constants.VSockGateway,
GatewayMacAddress: "5a:94:ef:e4:0c:dd",
Expand Down Expand Up @@ -99,6 +100,9 @@ var daemonCmd = &cobra.Command{
},
},
},
VpnKitUUIDMacAddresses: map[string]string{
"c3d68012-0208-11ea-9fd7-f2189899ab08": "5a:94:ef:e4:0c:ee",
},
Protocol: types.HyperKitProtocol,
}
if config.Get(crcConfig.HostNetworkAccess).AsBool() {
Expand All @@ -124,6 +128,13 @@ var daemonCmd = &cobra.Command{
},
}

func mtu() int {
if runtime.GOOS == "darwin" {
return 1500
}
return 4000
}

func run(configuration *types.Configuration) error {
vsockListener, err := vsockListener()
if err != nil {
Expand Down Expand Up @@ -166,10 +177,26 @@ func run(configuration *types.Configuration) error {
}()

go func() {
mux := http.NewServeMux()
mux.Handle(types.ConnectPath, vn.Mux())
if err := http.Serve(vsockListener, mux); err != nil {
errCh <- errors.Wrap(err, "virtualnetwork http.Serve failed")
if runtime.GOOS == "darwin" {
for {
conn, err := vsockListener.Accept()
if err != nil {
log.Errorf("vpnkit accept error: %s", err)
continue
}
go func() {
if err := vn.AcceptVpnKit(conn); err != nil {
log.Errorf("vpnkit accept error: %s", err)

}
}()
}
} else {
mux := http.NewServeMux()
mux.Handle(types.ConnectPath, vn.Mux())
if err := http.Serve(vsockListener, mux); err != nil {
errCh <- errors.Wrap(err, "virtualnetwork http.Serve failed")
}
}
}()

Expand Down
2 changes: 2 additions & 0 deletions pkg/crc/machine/hyperkit/driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func CreateHost(machineConfig config.MachineConfig) *hyperkit.Driver {
hyperkitDriver.QcowToolPath = filepath.Join(constants.BinDir(), QcowToolCommand)

hyperkitDriver.VMNet = machineConfig.NetworkMode == network.SystemNetworkingMode
hyperkitDriver.VpnKitSock = constants.TapSocketPath
hyperkitDriver.VpnKitUUID = "c3d68012-0208-11ea-9fd7-f2189899ab08"

return hyperkitDriver
}
24 changes: 0 additions & 24 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -247,12 +246,6 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
return nil, errors.Wrap(err, "Error starting machine")
}

if runtime.GOOS == "darwin" && client.useVSock() {
if err := makeDaemonVisibleToHyperkit(client.name); err != nil {
return nil, err
}
}

// Post-VM start
vmState, err = host.Driver.GetState()
if err != nil {
Expand Down Expand Up @@ -516,23 +509,6 @@ func (client *client) validateStartConfig(startConfig types.StartConfig) error {
return nil
}

// makeDaemonVisibleToHyperkit crc daemon is launched in background and doesn't know where hyperkit is running.
// In order to vsock to work with hyperkit, we need to put the unix socket in the hyperkit working directory with a
// special name. The name is the hex representation of the cid and the vsock port.
// This function adds the unix socket in the hyperkit directory.
func makeDaemonVisibleToHyperkit(name string) error {
dst := filepath.Join(constants.MachineInstanceDir, name, "00000002.00000400")
if _, err := os.Stat(dst); err != nil {
if !os.IsNotExist(err) {
return errors.Wrap(err, "VSock listener error")
}
if err := os.Symlink(constants.TapSocketPath, dst); err != nil {
return errors.Wrap(err, "VSock listener error")
}
}
return nil
}

func createHost(api libmachine.API, machineConfig config.MachineConfig) error {
vm, err := newHost(api, machineConfig)
if err != nil {
Expand Down

0 comments on commit cf173bc

Please sign in to comment.