2015-12-30 04:52:24 -05:00
|
|
|
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
|
|
|
|
See the README file for details. *)
|
|
|
|
|
2015-12-30 08:48:13 -05:00
|
|
|
open Lwt.Infix
|
2015-12-30 04:52:24 -05:00
|
|
|
open Utils
|
|
|
|
|
2015-12-30 08:48:13 -05:00
|
|
|
module Netback = Netchannel.Backend.Make(Netchannel.Xenstore.Make(OS.Xs))
|
|
|
|
module ClientEth = Ethif.Make(Netback)
|
2015-12-30 04:52:24 -05:00
|
|
|
|
2016-09-25 10:25:51 -04:00
|
|
|
let src = Logs.Src.create "client_net" ~doc:"Client networking"
|
2015-12-30 08:48:13 -05:00
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
2015-12-30 04:52:24 -05:00
|
|
|
|
2016-09-25 10:25:51 -04:00
|
|
|
let writev eth data =
|
|
|
|
Lwt.catch
|
|
|
|
(fun () -> ClientEth.writev eth data)
|
|
|
|
(fun ex ->
|
|
|
|
(* Usually Netback_shutdown, because the client disconnected *)
|
|
|
|
Log.err (fun f -> f "uncaught exception trying to send to client:@\n@[<v2> %a@]@\nException: @[%s@]"
|
|
|
|
Cstruct.hexdump_pp (Cstruct.concat data) (Printexc.to_string ex));
|
|
|
|
Lwt.return ()
|
|
|
|
)
|
|
|
|
|
2016-01-01 06:32:57 -05:00
|
|
|
class client_iface eth ~gateway_ip ~client_ip client_mac : client_link = object
|
2016-01-07 06:24:35 -05:00
|
|
|
val queue = FrameQ.create (Ipaddr.V4.to_string client_ip)
|
2015-12-30 08:48:13 -05:00
|
|
|
method my_mac = ClientEth.mac eth
|
2015-12-30 11:07:16 -05:00
|
|
|
method other_mac = client_mac
|
2016-01-01 06:32:57 -05:00
|
|
|
method my_ip = gateway_ip
|
2015-12-30 11:07:16 -05:00
|
|
|
method other_ip = client_ip
|
2015-12-30 08:48:13 -05:00
|
|
|
method writev ip =
|
2016-01-07 06:24:35 -05:00
|
|
|
FrameQ.send queue (fun () ->
|
|
|
|
let eth_hdr = eth_header_ipv4 ~src:(ClientEth.mac eth) ~dst:client_mac in
|
2016-09-25 10:25:51 -04:00
|
|
|
writev eth (fixup_checksums (Cstruct.concat (eth_hdr :: ip)))
|
2016-01-07 06:24:35 -05:00
|
|
|
)
|
2015-12-30 04:52:24 -05:00
|
|
|
end
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2016-10-01 09:42:27 -04:00
|
|
|
let clients : Cleanup.t Dao.VifMap.t ref = ref Dao.VifMap.empty
|
2015-12-30 08:48:13 -05:00
|
|
|
|
2015-12-31 09:56:24 -05:00
|
|
|
(** Handle an ARP message from the client. *)
|
2015-12-31 10:56:56 -05:00
|
|
|
let input_arp ~fixed_arp ~eth request =
|
|
|
|
match Client_eth.ARP.input fixed_arp request with
|
2015-12-31 09:56:24 -05:00
|
|
|
| None -> return ()
|
2016-09-25 10:25:51 -04:00
|
|
|
| Some response -> writev eth [response]
|
2015-12-31 09:56:24 -05:00
|
|
|
|
|
|
|
(** Handle an IPv4 packet from the client. *)
|
|
|
|
let input_ipv4 ~client_ip ~router frame packet =
|
|
|
|
let src = Wire_structs.Ipv4_wire.get_ipv4_src packet |> Ipaddr.V4.of_int32 in
|
2016-01-01 05:55:34 -05:00
|
|
|
if src = client_ip then Firewall.ipv4_from_client router frame
|
2015-12-31 09:56:24 -05:00
|
|
|
else (
|
2016-01-08 06:31:27 -05:00
|
|
|
Log.warn (fun f -> f "Incorrect source IP %a in IP packet from %a (dropping)"
|
|
|
|
Ipaddr.V4.pp_hum src Ipaddr.V4.pp_hum client_ip);
|
2015-12-31 09:56:24 -05:00
|
|
|
return ()
|
|
|
|
)
|
|
|
|
|
|
|
|
(** Connect to a new client's interface and listen for incoming frames. *)
|
2016-10-01 09:42:27 -04:00
|
|
|
let add_vif { Dao.ClientVif.domid; device_id } ~client_ip ~router ~cleanup_tasks =
|
2015-12-31 09:56:24 -05:00
|
|
|
Netback.make ~domid ~device_id >>= fun backend ->
|
2016-01-08 06:31:27 -05:00
|
|
|
Log.info (fun f -> f "Client %d (IP: %s) ready" domid (Ipaddr.V4.to_string client_ip));
|
2015-12-31 09:56:24 -05:00
|
|
|
ClientEth.connect backend >>= or_fail "Can't make Ethernet device" >>= fun eth ->
|
|
|
|
let client_mac = Netback.mac backend in
|
2016-01-01 06:32:57 -05:00
|
|
|
let client_eth = router.Router.client_eth in
|
|
|
|
let gateway_ip = Client_eth.client_gw client_eth in
|
|
|
|
let iface = new client_iface eth ~gateway_ip ~client_ip client_mac in
|
2016-09-25 10:14:16 -04:00
|
|
|
Router.add_client router iface >>= fun () ->
|
2015-12-31 09:56:24 -05:00
|
|
|
Cleanup.on_cleanup cleanup_tasks (fun () -> Router.remove_client router iface);
|
2016-01-01 06:32:57 -05:00
|
|
|
let fixed_arp = Client_eth.ARP.create ~net:client_eth iface in
|
2015-12-31 09:56:24 -05:00
|
|
|
Netback.listen backend (fun frame ->
|
2015-12-31 10:56:56 -05:00
|
|
|
match Wire_structs.parse_ethernet_frame frame with
|
2016-01-08 06:31:27 -05:00
|
|
|
| None -> Log.warn (fun f -> f "Invalid Ethernet frame"); return ()
|
2015-12-31 10:56:56 -05:00
|
|
|
| Some (typ, _destination, payload) ->
|
|
|
|
match typ with
|
|
|
|
| Some Wire_structs.ARP -> input_arp ~fixed_arp ~eth payload
|
|
|
|
| Some Wire_structs.IPv4 -> input_ipv4 ~client_ip ~router frame payload
|
|
|
|
| Some Wire_structs.IPv6 -> return ()
|
2016-01-08 06:31:27 -05:00
|
|
|
| None -> Logs.warn (fun f -> f "Unknown Ethernet type"); Lwt.return_unit
|
2015-12-31 09:56:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
(** A new client VM has been found in XenStore. Find its interface and connect to it. *)
|
2016-10-01 09:42:27 -04:00
|
|
|
let add_client ~router vif client_ip =
|
2015-12-30 08:48:13 -05:00
|
|
|
let cleanup_tasks = Cleanup.create () in
|
2016-10-01 09:42:27 -04:00
|
|
|
Log.info (fun f -> f "add client vif %a" Dao.ClientVif.pp vif);
|
2015-12-30 08:48:13 -05:00
|
|
|
Lwt.async (fun () ->
|
2016-10-01 09:42:27 -04:00
|
|
|
Lwt.catch (fun () ->
|
|
|
|
add_vif vif ~client_ip ~router ~cleanup_tasks
|
|
|
|
)
|
|
|
|
(fun ex ->
|
|
|
|
Log.warn (fun f -> f "Error connecting client %a: %s"
|
|
|
|
Dao.ClientVif.pp vif (Printexc.to_string ex));
|
|
|
|
return ()
|
|
|
|
)
|
|
|
|
);
|
2015-12-30 08:48:13 -05:00
|
|
|
cleanup_tasks
|
|
|
|
|
2015-12-31 09:56:24 -05:00
|
|
|
(** Watch XenStore for notifications of new clients. *)
|
2015-12-30 08:48:13 -05:00
|
|
|
let listen router =
|
|
|
|
Dao.watch_clients (fun new_set ->
|
|
|
|
(* Check for removed clients *)
|
2016-10-01 09:42:27 -04:00
|
|
|
!clients |> Dao.VifMap.iter (fun key cleanup ->
|
|
|
|
if not (Dao.VifMap.mem key new_set) then (
|
|
|
|
clients := !clients |> Dao.VifMap.remove key;
|
|
|
|
Log.info (fun f -> f "client %a has gone" Dao.ClientVif.pp key);
|
2015-12-30 08:48:13 -05:00
|
|
|
Cleanup.cleanup cleanup
|
|
|
|
)
|
|
|
|
);
|
|
|
|
(* Check for added clients *)
|
2016-10-01 09:42:27 -04:00
|
|
|
new_set |> Dao.VifMap.iter (fun key ip_addr ->
|
|
|
|
if not (Dao.VifMap.mem key !clients) then (
|
|
|
|
let cleanup = add_client ~router key ip_addr in
|
|
|
|
clients := !clients |> Dao.VifMap.add key cleanup
|
2015-12-30 08:48:13 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|