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

Commit

Permalink
support --vmnet-gateway=IP, e.g., 192.168.105.1
Browse files Browse the repository at this point in the history
`vde_vmnet` binary itself does not have any default value,
but the launchd plist file is updated to use 192.168.105.1
as the default, so as to avoid conflicting with VMware Fusion.

Fix #7

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Jul 29, 2021
1 parent fc85aaa commit 046d346
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,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 @@ -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
31 changes: 31 additions & 0 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 Down Expand Up @@ -30,6 +31,9 @@ static void print_usage(const char *argv0) {
"--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 @@ -42,6 +46,7 @@ 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) {
Expand All @@ -54,6 +59,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
{"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 @@ -79,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 Down Expand Up @@ -115,6 +124,26 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
"vmnet mode \"bridged\" require --vmnet-interface to be specified\n");
goto error;
}
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 @@ -132,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>
28 changes: 28 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <stdlib.h>
#include <sys/uio.h>

#include <arpa/inet.h>

#include <vmnet/vmnet.h>

#include <libvdeplug.h>
Expand Down Expand Up @@ -159,6 +161,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

0 comments on commit 046d346

Please sign in to comment.