Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

support --vmnet-gateway=IP, e.g., 192.168.105.1 + drop support for macOS 10.14 #8

Merged
merged 3 commits into from
Jul 29, 2021
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
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## Install

Requires macOS 10.15 or later.

```console
brew install vde

Expand All @@ -20,12 +22,10 @@ The following files will be installed:
- `/usr/local/bin/vde_vmnet`
- `/Library/LaunchDaemons/io.github.virtualsquare.vde-2.vde_switch.plist`
- `/Library/LaunchDaemons/io.github.AkihiroSuda.vde_vmnet.plist`
- Configured to use `192.168.105.0/24`. Modifiy the file if it conflicts with your local network.

See ["Testing without launchd"](#testing-without-launchd) if you don't prefer to use launchd.


:warning: Known to conflict with VMware Fusion. See issue [#7](https://github.com/AkihiroSuda/vde_vmnet/issues/7).

## Usage

```console
Expand Down Expand Up @@ -58,8 +58,6 @@ The following additional files will be installed:

Use `/var/run/vde.bridged.en0.ctl` as the VDE socket path.

Needs macOS 10.15 or later.

## Advanced usage

### Testing without launchd
Expand All @@ -75,7 +73,7 @@ vde_switch --unix /tmp/vde.ctl
```

```console
sudo vde_vmnet /tmp/vde.ctl
sudo vde_vmnet --vmnet-gateway=192.168.105.1 /tmp/vde.ctl
```

Note: make sure to run `vde_vmnet` with root (`sudo`). See [FAQs](#FAQs) for the reason.
Expand Down Expand Up @@ -125,14 +123,14 @@ On the other hand, `vde_vmnet` does not require the entire QEMU process to run a

- Decide a unique MAC address for the VM, e.g. `de:ad:be:ef:00:01`.

- Decide a static IP address, e.g., "192.168.60.100"
- Decide a static IP address, e.g., "192.168.105.100"

- Create `/etc/bootptab` like this. Make sure not to drop the "%%" header.
```
# bootptab
%%
# hostname hwtype hwaddr ipaddr bootfile
tmp-vm01 1 de:ad:be:ef:00:01 192.168.60.100
tmp-vm01 1 de:ad:be:ef:00:01 192.168.105.100
```

- Reload the DHCP daemon.
Expand Down
58 changes: 47 additions & 11 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>

#include <arpa/inet.h>
#include <getopt.h>

#include <availability.h>
Expand All @@ -12,18 +13,27 @@
#define VERSION "UNKNOWN"
#endif

#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101500
#error "Requires macOS 10.15 or later"
#endif

#define CLI_DEFAULT_VDE_GROUP "staff"

static void print_usage(const char *argv0) {
printf("Usage: %s [OPTION]... VDESWITCH\n", argv0);
printf("vmnet.framework support for rootless QEMU.\n");
printf("vde_vmnet does not require QEMU to run as the root user, but "
"vde_vmnet itself has to run as the root, in most cases.\n");
printf("\n");
printf("--vde-group=GROUP VDE group name (default: "
"\"staff\")\n");
"\"" CLI_DEFAULT_VDE_GROUP "\")\n");
printf(
"--vmnet-mode=(host|shared|bridged) vmnet mode (default: \"shared\")\n");
printf("--vmnet-interface=INTERFACE interface used for "
"--vmnet=bridged, e.g., \"en0\"\n");
printf("--vmnet-gateway=IP gateway used for "
"--vmnet=(host|shared), e.g., \"192.168.105.1\" (default: decided by "
"macOS)\n");
printf("-h, --help display this help and exit\n");
printf("-v, --version display version information and "
"exit\n");
Expand All @@ -36,20 +46,20 @@ static void print_version() { puts(VERSION); }
#define CLI_OPTIONS_ID_VDE_GROUP -42
#define CLI_OPTIONS_ID_VMNET_MODE -43
#define CLI_OPTIONS_ID_VMNET_INTERFACE -44
#define CLI_OPTIONS_ID_VMNET_GATEWAY -45
struct cli_options *cli_options_parse(int argc, char *argv[]) {
struct cli_options *res = malloc(sizeof(*res));
if (res == NULL) {
goto error;
}
memset(res, 0, sizeof(*res));
res->vde_group = strdup("staff"); /* use strdup to make it freeable */
res->vmnet_mode = VMNET_SHARED_MODE;

const struct option longopts[] = {
{"vde-group", required_argument, NULL, CLI_OPTIONS_ID_VDE_GROUP},
{"vmnet-mode", required_argument, NULL, CLI_OPTIONS_ID_VMNET_MODE},
{"vmnet-interface", required_argument, NULL,
CLI_OPTIONS_ID_VMNET_INTERFACE},
{"vmnet-gateway", required_argument, NULL, CLI_OPTIONS_ID_VMNET_GATEWAY},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0},
Expand All @@ -66,13 +76,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
} else if (strcmp(optarg, "shared") == 0) {
res->vmnet_mode = VMNET_SHARED_MODE;
} else if (strcmp(optarg, "bridged") == 0) {
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
res->vmnet_mode = VMNET_BRIDGED_MODE;
#else
fprintf(stderr,
"vmnet mode \"bridged\" requires macOS 10.15 or later\n");
goto error;
#endif
} else {
fprintf(stderr, "Unknown vmnet mode \"%s\"\n", optarg);
goto error;
Expand All @@ -81,6 +85,9 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
case CLI_OPTIONS_ID_VMNET_INTERFACE:
res->vmnet_interface = strdup(optarg);
break;
case CLI_OPTIONS_ID_VMNET_GATEWAY:
res->vmnet_gateway = strdup(optarg);
break;
case 'h':
print_usage(argv[0]);
cli_options_destroy(res);
Expand All @@ -102,14 +109,41 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
goto error;
}
res->vde_switch = strdup(argv[optind]);
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500

/* fill default */
if (res->vde_group == NULL)
res->vde_group =
strdup(CLI_DEFAULT_VDE_GROUP); /* use strdup to make it freeable */
if (res->vmnet_mode == 0)
res->vmnet_mode = VMNET_SHARED_MODE;

/* validate */
if (res->vmnet_mode == VMNET_BRIDGED_MODE && res->vmnet_interface == NULL) {
fprintf(
stderr,
"vmnet mode \"bridged\" require --vmnet-interface to be specified\n");
goto error;
}
#endif
if (res->vmnet_gateway == NULL) {
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
fprintf(stderr,
"WARNING: --vmnet-gateway=IP should be explicitly specified to "
"avoid conflicting with other applications\n");
}
} else {
if (res->vmnet_mode == VMNET_BRIDGED_MODE) {
fprintf(stderr,
"vmnet mode \"bridged\" conflicts with --vmnet-gateway\n");
goto error;
}
struct in_addr dummy;
if (!inet_aton(res->vmnet_gateway, &dummy)) {
fprintf(stderr,
"invalid address \"%s\" was specified for --vmnet-gateway\n",
res->vmnet_gateway);
goto error;
}
}
return res;
error:
print_usage(argv[0]);
Expand All @@ -127,5 +161,7 @@ void cli_options_destroy(struct cli_options *x) {
free(x->vde_switch);
if (x->vmnet_interface != NULL)
free(x->vmnet_interface);
if (x->vmnet_gateway != NULL)
free(x->vmnet_gateway);
free(x);
}
14 changes: 10 additions & 4 deletions cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
#include <vmnet/vmnet.h>

struct cli_options {
char *vde_group; // --vde-group
operating_modes_t vmnet_mode; // --vmnet-mode
char *vmnet_interface; // --vmnet-interface
char *vde_switch; // arg
// --vde-group
char *vde_group;
// --vmnet-mode, corresponds to vmnet_operation_mode_key
operating_modes_t vmnet_mode;
// --vmnet-interface, corresponds to vmnet_shared_interface_name_key
char *vmnet_interface;
// --vmnet-gateway, corresponds to vmnet_start_address_key
char *vmnet_gateway;
// arg
char *vde_switch;
};

struct cli_options *cli_options_parse(int argc, char *argv[]);
Expand Down
3 changes: 2 additions & 1 deletion launchd/io.github.AkihiroSuda.vde_vmnet.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/vde_vmnet</string>
<string>--vmnet-gateway=192.168.105.1</string>
<string>/var/run/vde.ctl</string>
</array>
<key>StandardErrorPath</key>
Expand All @@ -28,4 +29,4 @@
</dict>
</dict>
</dict>
</plist>
</plist>
32 changes: 32 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
#include <stdlib.h>
#include <sys/uio.h>

#include <arpa/inet.h>

#include <vmnet/vmnet.h>

#include <libvdeplug.h>

#include "cli.h"

#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101500
#error "Requires macOS 10.15 or later"
#endif

static bool debug = false;

#define DEBUGF(fmt, ...) \
Expand Down Expand Up @@ -159,6 +165,32 @@ static interface_ref start(VDECONN *vdeconn, struct cli_options *cliopt) {
xpc_dictionary_set_string(dict, vmnet_shared_interface_name_key,
cliopt->vmnet_interface);
}
if (cliopt->vmnet_gateway != NULL) {
/* Set vmnet_start_address_key */
xpc_dictionary_set_string(dict, vmnet_start_address_key,
cliopt->vmnet_gateway);

/* Set vmnet_end_address_key with .254 */
struct in_addr sin;
if (!inet_aton(cliopt->vmnet_gateway, &sin)) {
perror("inet_aton(cliopt->vmnet_gateway)");
return NULL;
}
uint32_t h = ntohl(sin.s_addr);
h &= 0xFFFFFF00;
h |= 0x000000FE;
sin.s_addr = htonl(h);

const char *end = inet_ntoa(sin); /* static storage, do not free */
if (end == NULL) {
perror("inet_ntoa");
return NULL;
}
xpc_dictionary_set_string(dict, vmnet_end_address_key, end);

/* Set vmnet_subnet_mask_key */
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, "255.255.255.0");
}

uuid_t uuid;
// TODO: support deterministic UUID and MAC address
Expand Down
8 changes: 7 additions & 1 deletion test/test.ipxe
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ show netmask
show dns
show dhcp-server
echo </vde_vmnet:iPXE-testing>
# sleep for flushing the serial console

echo [testing Internet connection]
imgfetch http://google.com/

echo [sleeping for flushing the serial console]
sleep 1

echo [exiting]
reboot
7 changes: 6 additions & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ if ! grep -q "net0/mac:hex = 52:54:00:12:34:56" serial.log; then
exit 1
fi

if ! grep -q "net0.dhcp/ip:ipv4 = 192.168." serial.log; then
if ! grep -q "net0.dhcp/ip:ipv4 = 192.168.105." serial.log; then
echo >&2 "ERROR: net0.dhcp/ip:ipv4 not found"
exit 1
fi

if ! grep -q "net0.dhcp/gateway:ipv4 = 192.168.105.1" serial.log; then
echo >&2 "ERROR: net0.dhcp/gateway:ipv4 not found"
exit 1
fi

rm -f serial.log