2015-12-30 08:59:13 -05:00
|
|
|
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
|
|
|
|
See the README file for details. *)
|
|
|
|
|
|
|
|
open Lwt.Infix
|
|
|
|
open Utils
|
|
|
|
|
|
|
|
module Eth = Ethif.Make(Netif)
|
|
|
|
|
|
|
|
let src = Logs.Src.create "uplink" ~doc:"Network connection to NetVM"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
|
|
|
|
|
|
|
module Make(Clock : V1.CLOCK) = struct
|
|
|
|
module Arp = Arpv4.Make(Eth)(Clock)(OS.Time)
|
|
|
|
module IPv4 = Ipv4.Make(Eth)(Arp)
|
|
|
|
|
|
|
|
type t = {
|
|
|
|
net : Netif.t;
|
|
|
|
eth : Eth.t;
|
|
|
|
arp : Arp.t;
|
|
|
|
interface : interface;
|
|
|
|
}
|
|
|
|
|
2016-01-01 06:32:57 -05:00
|
|
|
class netvm_iface eth mac ~my_ip ~other_ip : interface = object
|
2015-12-30 08:59:13 -05:00
|
|
|
method my_mac = Eth.mac eth
|
2016-01-01 06:32:57 -05:00
|
|
|
method my_ip = my_ip
|
|
|
|
method other_ip = other_ip
|
2015-12-30 08:59:13 -05:00
|
|
|
method writev ip =
|
|
|
|
mac >>= fun dst ->
|
|
|
|
let eth_hdr = eth_header_ipv4 ~src:(Eth.mac eth) ~dst in
|
2015-12-30 11:07:16 -05:00
|
|
|
Eth.writev eth (eth_hdr :: ip)
|
2015-12-30 08:59:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let listen t router =
|
|
|
|
Netif.listen t.net (fun frame ->
|
2015-12-30 09:28:28 -05:00
|
|
|
(* Handle one Ethernet frame from NetVM *)
|
|
|
|
Eth.input t.eth
|
2015-12-30 08:59:13 -05:00
|
|
|
~arpv4:(Arp.input t.arp)
|
2015-12-30 11:07:16 -05:00
|
|
|
~ipv4:(fun _ip -> Firewall.ipv4_from_netvm router frame)
|
|
|
|
~ipv6:(fun _ip -> return ())
|
2015-12-30 09:28:28 -05:00
|
|
|
frame
|
2015-12-30 08:59:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
let interface t = t.interface
|
|
|
|
|
|
|
|
let connect config =
|
|
|
|
let ip = config.Dao.uplink_our_ip in
|
2016-01-01 05:55:34 -05:00
|
|
|
Netif.connect "tap0" >>= or_fail "Can't connect uplink device" >>= fun net ->
|
2015-12-30 08:59:13 -05:00
|
|
|
Eth.connect net >>= or_fail "Can't make Ethernet device for tap" >>= fun eth ->
|
|
|
|
Arp.connect eth >>= or_fail "Can't add ARP" >>= fun arp ->
|
|
|
|
Arp.add_ip arp ip >>= fun () ->
|
|
|
|
let netvm_mac = Arp.query arp config.Dao.uplink_netvm_ip >|= function
|
|
|
|
| `Timeout -> failwith "ARP timeout getting MAC of our NetVM"
|
|
|
|
| `Ok netvm_mac -> netvm_mac in
|
2016-01-01 06:32:57 -05:00
|
|
|
let interface = new netvm_iface eth netvm_mac
|
|
|
|
~my_ip:ip
|
|
|
|
~other_ip:config.Dao.uplink_netvm_ip in
|
|
|
|
return { net; eth; arp; interface }
|
2015-12-30 08:59:13 -05:00
|
|
|
end
|