- OCaml 61%
- C 39%
vmnet #
macOS vmnet.framework host networking, exposing a virtual interface as an Eio
flow of raw Ethernet frames.
A virtual machine on macOS needs a network interface on the host side, and
Apple's vmnet.framework is the supported way to get one without a kernel
extension. The framework hands the host a virtual link-layer interface that a
guest attaches to: frames written to it reach the guest, and frames the guest
sends are read from it. This library starts such an interface and wraps it as
an Eio.Flow.two_way, so a frame processor (a guest's NIC, a bridge or a
software network stack) reads and writes raw Ethernet directly.
Three modes control how much of the host network the interface exposes:
- Shared NATs guest traffic to the outside world (the common case).
- Host keeps traffic host-local: guest-to-guest and guest-to-host only.
- Bridged joins an existing physical interface so the guest is on the LAN;
shared_interface_listenumerates the interfaces that support it.
The C stubs over vmnet_start_interface, vmnet_read and vmnet_write are
vendored from mirage/ocaml-vmnet. This
package replaces the upstream Lwt layer with an Eio flow whose reads wait for
the framework's packets-available event on a system thread, so other fibers
keep running while a read waits.
The library runs on macOS only. Starting an interface needs root or the
com.apple.vm.networking entitlement (see
vmnet.entitlements). An entitlement is a key in a
macOS binary's code signature that grants it a capability the system
otherwise refuses. Some, such as com.apple.security.hypervisor, can be
signed ad hoc by anyone, while com.apple.vm.networking is restricted: Apple
must grant it in a provisioning profile. Port forwarding and the listing of
bridgeable interfaces need macOS 10.15 or later.
Installation #
$ opam install vmnet
If opam cannot find the package, it has not reached the public
opam-repository yet. Add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install vmnet
Features #
- Start a Shared (NAT), Host or Bridged interface, optionally with a fixed UUID (so a guest keeps its DHCP lease) and a private IPv4 range.
- The interface as an
Eio.Flow.two_wayof raw Ethernet frames. - Read back the MAC address, MTU, maximum packet size and UUID.
- List the physical interfaces that support bridged mode.
- Add, remove and list NAT port-forwarding rules.
Usage #
Starting an interface needs privilege, so this example is wrapped in a function rather than run inline:
let main () =
Eio_main.run @@ fun _env ->
Eio.Switch.run @@ fun sw ->
let net =
Vmnet.v ~sw ~mode:Vmnet.Shared
~ipv4_config:
{
start_address = Ipaddr.V4.of_string_exn "192.168.64.1";
end_address = Ipaddr.V4.of_string_exn "192.168.64.254";
netmask = Ipaddr.V4.of_string_exn "255.255.255.0";
}
()
in
Format.printf "interface %a, mtu %d@." Vmnet.pp net (Vmnet.mtu net);
let flow = Vmnet.flow net in
(* read and write raw Ethernet frames on [flow] *)
ignore flow
Each Eio.Flow.single_read yields one Ethernet frame, so read into a buffer of
at least Vmnet.max_packet_size bytes; each buffer written is sent as one
frame. A caller that prefers the bytesrw streaming interface can bridge the
flow with Bytesrw_eio.bytes_reader_of_flow (Vmnet.flow net).
Related work #
- mirage/ocaml-vmnet holds the upstream bindings the C stubs come from, with an Lwt interface.
tuntapis the TUN/TAP host networking path, with the sameEio.Flow.two_wayof raw Ethernet frames on Linux and macOS.vzbinds Apple's Virtualization.framework; a VM built with it can be attached to avmnetinterface.
License #
ISC. See LICENSE.md.