Skip to content

Commit

Permalink
Merge pull request #426 from hannesm/ipaddr-5.0.0
Browse files Browse the repository at this point in the history
adapt to ipaddr 5.0.0 changes: Static_ipv4.connect only needs a Ipaddr.V4.Prefix.t
  • Loading branch information
hannesm authored Jun 19, 2020
2 parents 29a95d4 + bb529a5 commit cab6b83
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 63 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
- POST_INSTALL_HOOK="sh ./.travis-ci.sh"
- PACKAGE=tcpip
matrix:
- DISTRO=alpine OCAML_VERSION=4.06 EXTRA_ENV="MIRAGE_MODE=xen"
- DISTRO=alpine OCAML_VERSION=4.07 EXTRA_ENV="MIRAGE_MODE=unix"
- DISTRO=alpine OCAML_VERSION=4.08 EXTRA_ENV="MIRAGE_MODE=qubes"
- DISTRO=alpine OCAML_VERSION=4.09 EXTRA_ENV="MIRAGE_MODE=virtio"
- DISTRO=alpine OCAML_VERSION=4.08 EXTRA_ENV="MIRAGE_MODE=xen"
- DISTRO=alpine OCAML_VERSION=4.09 EXTRA_ENV="MIRAGE_MODE=unix"
- DISTRO=alpine OCAML_VERSION=4.10 EXTRA_ENV="MIRAGE_MODE=qubes"
- DISTRO=alpine OCAML_VERSION=4.10 EXTRA_ENV="MIRAGE_MODE=virtio"
27 changes: 12 additions & 15 deletions src/ipv4/static_ipv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ module Make (R: Mirage_random.S) (C: Mirage_clock.MCLOCK) (Ethernet: Mirage_prot
type t = {
ethif : Ethernet.t;
arp : Arpv4.t;
mutable ip: Ipaddr.V4.t;
network: Ipaddr.V4.Prefix.t;
mutable gateway: Ipaddr.V4.t option;
cidr: Ipaddr.V4.Prefix.t;
gateway: Ipaddr.V4.t option;
mutable cache: Fragments.Cache.t;
}

let write t ?(fragment = true) ?(ttl = 38) ?src dst proto ?(size = 0) headerf bufs =
Routing.destination_mac t.network t.gateway t.arp dst >>= function
Routing.destination_mac t.cidr t.gateway t.arp dst >>= function
| Error `Local ->
Log.warn (fun f -> f "Could not find %a on the local network" Ipaddr.V4.pp dst);
Lwt.return @@ Error (`No_route "no response for IP on local network")
Expand Down Expand Up @@ -73,7 +72,7 @@ module Make (R: Mirage_random.S) (C: Mirage_clock.MCLOCK) (Ethernet: Mirage_prot
| false, true -> assert false (* handled by conditional above *)
in
let hdr =
let src = match src with None -> t.ip | Some x -> x in
let src = match src with None -> Ipaddr.V4.Prefix.address t.cidr | Some x -> x in
let id = if multiple then Randomconv.int16 R.generate else 0 in
Ipv4_packet.{
options = Cstruct.empty ;
Expand Down Expand Up @@ -134,9 +133,9 @@ module Make (R: Mirage_random.S) (C: Mirage_clock.MCLOCK) (Ethernet: Mirage_prot
Lwt.return_unit
| Ok (packet, payload) ->
let of_interest ip =
Ipaddr.V4.(compare ip t.ip = 0
Ipaddr.V4.(compare ip (Prefix.address t.cidr) = 0
|| compare ip broadcast = 0
|| compare ip (Prefix.broadcast t.network) = 0)
|| compare ip (Prefix.broadcast t.cidr) = 0)
in
if not (of_interest packet.dst) then begin
Log.debug (fun m -> m "dropping IP fragment not for us or broadcast %a"
Expand All @@ -159,22 +158,20 @@ module Make (R: Mirage_random.S) (C: Mirage_clock.MCLOCK) (Ethernet: Mirage_prot
| Some `ICMP | None -> default ~proto:packet.proto ~src ~dst payload

let connect
~ip:(network, ip) ?gateway ?(fragment_cache_size = 1024 * 256) ethif arp =
Arpv4.set_ips arp [ip] >>= fun () ->
(* TODO currently hardcoded to 256KB, should be configurable
and maybe limited per-src/dst-ip as well? *)
~cidr ?gateway ?(fragment_cache_size = 1024 * 256) ethif arp =
Arpv4.set_ips arp [Ipaddr.V4.Prefix.address cidr] >>= fun () ->
let cache = Fragments.Cache.empty fragment_cache_size in
Lwt.return { ethif; arp; ip; network; gateway ; cache }
Lwt.return { ethif; arp; cidr; gateway; cache }

let disconnect _ = Lwt.return_unit

let get_ip t = [t.ip]
let get_ip t = [Ipaddr.V4.Prefix.address t.cidr]

let pseudoheader t ?src dst proto len =
let src = match src with None -> t.ip | Some x -> x in
let src = match src with None -> Ipaddr.V4.Prefix.address t.cidr | Some x -> x in
Ipv4_packet.Marshal.pseudoheader ~src ~dst ~proto len

let src t ~dst:_ = t.ip
let src t ~dst:_ = Ipaddr.V4.Prefix.address t.cidr

let mtu t = Ethernet.mtu t.ethif - Ipv4_wire.sizeof_ipv4

Expand Down
6 changes: 3 additions & 3 deletions src/ipv4/static_ipv4.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
module Make (R: Mirage_random.S) (C: Mirage_clock.MCLOCK) (E: Mirage_protocols.ETHERNET) (A: Mirage_protocols.ARP) : sig
include Mirage_protocols.IP with type ipaddr = Ipaddr.V4.t

val connect : ip:(Ipaddr.V4.Prefix.t * Ipaddr.V4.t) -> ?gateway:Ipaddr.V4.t ->
val connect : cidr:Ipaddr.V4.Prefix.t -> ?gateway:Ipaddr.V4.t ->
?fragment_cache_size:int -> E.t -> A.t -> t Lwt.t
(** [connect ~ip ~gateway ~fragment_cache_size eth arp] connects the ipv4
device using [ip] and [gateway] for network communication. The size of
(** [connect ~cidr ~gateway ~fragment_cache_size eth arp] connects the ipv4
device using [cidr] and [gateway] for network communication. The size of
the IPv4 fragment cache (for reassembly) can be provided in byte-size of
fragments (defaults to 256kB). *)
end
2 changes: 1 addition & 1 deletion tcpip.opam
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ depends: [
"mirage-stack" {>= "2.0.0"}
"mirage-protocols" {>= "4.0.0"}
"mirage-time" {>= "2.0.0"}
"ipaddr" {>= "4.0.0"}
"ipaddr" {>= "5.0.0"}
"macaddr" {>="4.0.0"}
"macaddr-cstruct"
"mirage-profile" {>= "0.5"}
Expand Down
11 changes: 5 additions & 6 deletions test/test_connect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ module Test_connect (B : Vnetif_backends.Backend) = struct
module V = VNETIF_STACK (B)

let gateway = Ipaddr.V4.of_string_exn "10.0.0.1"
let client_ip = Ipaddr.V4.of_string_exn "10.0.0.101"
let server_ip = Ipaddr.V4.of_string_exn "10.0.0.100"
let network = Ipaddr.V4.Prefix.make 24 client_ip
let client_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.101/24"
let server_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.100/24"
let test_string = "Hello world from Mirage 123456789...."
let backend = V.create_backend ()

Expand Down Expand Up @@ -62,16 +61,16 @@ module Test_connect (B : Vnetif_backends.Backend) = struct
(Lwt_unix.sleep timeout >>= fun () ->
failf "connect test timedout after %f seconds" timeout) ;

(V.create_stack ~ip:(network, server_ip) ~gateway backend >>= fun s1 ->
(V.create_stack ~cidr:server_cidr ~gateway backend >>= fun s1 ->
V.Stackv4.listen_tcpv4 s1 ~port:80 (fun f -> accept f test_string);
V.Stackv4.listen s1) ;

(Lwt_unix.sleep 0.1 >>= fun () ->
V.create_stack ~ip:(network, client_ip) ~gateway backend >>= fun s2 ->
V.create_stack ~cidr:client_cidr ~gateway backend >>= fun s2 ->
Lwt.pick [
V.Stackv4.listen s2;
(let conn = V.Stackv4.TCPV4.create_connection (V.Stackv4.tcpv4 s2) in
or_error "connect" conn (server_ip, 80) >>= fun flow ->
or_error "connect" conn (Ipaddr.V4.Prefix.address server_cidr, 80) >>= fun flow ->
Log.debug (fun f -> f "Connected to other end...");

V.Stackv4.TCPV4.write flow (Cstruct.of_string test_string) >>= function
Expand Down
11 changes: 6 additions & 5 deletions test/test_deadlock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ struct
type stack = TCPIP.t

let server_ip = Ipaddr.V4.of_string_exn "192.168.10.10"
let server_cidr = Ipaddr.V4.Prefix.make 24 server_ip
let client_ip = Ipaddr.V4.of_string_exn "192.168.10.20"
let network = Ipaddr.V4.Prefix.make 24 server_ip
let client_cidr = Ipaddr.V4.Prefix.make 24 client_ip

let make ~ip ?gateway netif =
let make ~cidr ?gateway netif =
ETHIF.connect netif >>= fun ethif ->
ARPV4.connect ethif >>= fun arpv4 ->
IPV4.connect ~ip:(network, ip) ?gateway ethif arpv4 >>= fun ipv4 ->
IPV4.connect ~cidr ?gateway ethif arpv4 >>= fun ipv4 ->
ICMPV4.connect ipv4 >>= fun icmpv4 ->
UDPV4.connect ipv4 >>= fun udpv4 ->
TCPV4.connect ipv4 >>= fun tcpv4 ->
Expand All @@ -55,8 +56,8 @@ struct
let tcpip t = t

let make role netif = match role with
| `Server -> make ~ip:server_ip netif
| `Client -> make ~ip:client_ip netif
| `Server -> make ~cidr:server_cidr netif
| `Client -> make ~cidr:client_cidr netif

type conn = M.NETIF.t

Expand Down
4 changes: 2 additions & 2 deletions test/test_icmpv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ let header_size = Ethernet_wire.sizeof_ethernet
let get_stack ?(backend = B.create ~use_async_readers:true
~yield:(fun() -> Lwt_main.yield ()) ())
ip =
let network = Ipaddr.V4.Prefix.make 24 listener_address in
let cidr = Ipaddr.V4.Prefix.make 24 ip in
V.connect backend >>= fun netif ->
E.connect netif >>= fun ethif ->
Static_arp.connect ethif >>= fun arp ->
Ip.connect ~ip:(network, ip) ethif arp >>= fun ip ->
Ip.connect ~cidr ethif arp >>= fun ip ->
Icmp.connect ip >>= fun icmp ->
Udp.connect ip >>= fun udp ->
Lwt.return { backend; netif; ethif; arp; ip; icmp; udp }
Expand Down
8 changes: 4 additions & 4 deletions test/test_iperf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module Test_iperf (B : Vnetif_backends.Backend) = struct
module V = VNETIF_STACK (B)

let gateway = Ipaddr.V4.of_string_exn "10.0.0.1"
let client_ip = Ipaddr.V4.Prefix.of_address_string_exn "10.0.0.101/24"
let server_ip = Ipaddr.V4.Prefix.of_address_string_exn "10.0.0.100/24"
let client_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.101/24"
let server_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.100/24"

type stats = {
mutable bytes: int64;
Expand All @@ -44,8 +44,8 @@ module Test_iperf (B : Vnetif_backends.Backend) = struct
}

let default_network ?(backend = B.create ()) () =
V.create_stack ~ip:client_ip ~gateway backend >>= fun client ->
V.create_stack ~ip:server_ip ~gateway backend >>= fun server ->
V.create_stack ~cidr:client_cidr ~gateway backend >>= fun client ->
V.create_stack ~cidr:server_cidr ~gateway backend >>= fun server ->
Lwt.return {backend; server; client}

let msg =
Expand Down
11 changes: 5 additions & 6 deletions test/test_keepalive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ module Test_connect = struct
module V = VNETIF_STACK (Vnetif_backends.On_off_switch)

let gateway = Ipaddr.V4.of_string_exn "10.0.0.1"
let client_ip = Ipaddr.V4.of_string_exn "10.0.0.101"
let server_ip = Ipaddr.V4.of_string_exn "10.0.0.100"
let network = Ipaddr.V4.Prefix.make 24 client_ip
let client_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.101/24"
let server_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.100/24"
let backend = V.create_backend ()

let err_read_eof () = failf "accept got EOF while reading"
Expand All @@ -101,17 +100,17 @@ module Test_connect = struct
(Lwt_unix.sleep timeout >>= fun () ->
failf "connect test timedout after %f seconds" timeout) ;

(V.create_stack ~ip:(network, server_ip) ~gateway backend >>= fun s1 ->
(V.create_stack ~cidr:server_cidr ~gateway backend >>= fun s1 ->
V.Stackv4.listen_tcpv4 s1 ~port:80 (fun f -> accept f);
V.Stackv4.listen s1) ;

(Lwt_unix.sleep 0.1 >>= fun () ->
V.create_stack ~ip:(network, client_ip) ~gateway backend >>= fun s2 ->
V.create_stack ~cidr:client_cidr ~gateway backend >>= fun s2 ->
Lwt.pick [
V.Stackv4.listen s2;
let keepalive = { Mirage_protocols.Keepalive.after = 0L; interval = Duration.of_sec 1; probes = 3 } in
(let conn = V.Stackv4.TCPV4.create_connection ~keepalive (V.Stackv4.tcpv4 s2) in
or_error "connect" conn (server_ip, 80) >>= fun flow ->
or_error "connect" conn (Ipaddr.V4.Prefix.address server_cidr, 80) >>= fun flow ->
Logs.debug (fun f -> f "Connected to other end...");
Vnetif_backends.On_off_switch.send_packets := false;
V.Stackv4.TCPV4.read flow >>= function
Expand Down
11 changes: 5 additions & 6 deletions test/test_mtus.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
open Lwt.Infix

let server_ip = Ipaddr.V4.of_string_exn "192.168.1.254"
let client_ip = Ipaddr.V4.of_string_exn "192.168.1.10"
let network = Ipaddr.V4.Prefix.make 24 server_ip
let server_cidr = Ipaddr.V4.Prefix.of_string_exn "192.168.1.254/24"
let client_cidr = Ipaddr.V4.Prefix.of_string_exn "192.168.1.10/24"

let server_port = 7

Expand Down Expand Up @@ -34,8 +33,8 @@ let read_one flow =
let get_stacks ?client_mtu ?server_mtu backend =
let or_default = function | None -> default_mtu | Some n -> n in
let client_mtu, server_mtu = or_default client_mtu, or_default server_mtu in
Stack.create_stack ~ip:(network, client_ip) ~mtu:client_mtu backend >>= fun client ->
Stack.create_stack ~ip:(network, server_ip) ~mtu:server_mtu backend >>= fun server ->
Stack.create_stack ~cidr:client_cidr ~mtu:client_mtu backend >>= fun client ->
Stack.create_stack ~cidr:server_cidr ~mtu:server_mtu backend >>= fun server ->
let max_mtu = max client_mtu server_mtu in
Backend.set_mtu max_mtu;
Lwt.return (server, client)
Expand All @@ -45,7 +44,7 @@ let start_server ~f server =
Stack.Stackv4.listen server

let start_client client =
Stack.Stackv4.TCPV4.create_connection (Stack.Stackv4.tcpv4 client) (server_ip, server_port) >>= function
Stack.Stackv4.TCPV4.create_connection (Stack.Stackv4.tcpv4 client) (Ipaddr.V4.Prefix.address server_cidr, server_port) >>= function
| Ok connection -> Lwt.return connection
| Error e -> err_fail e

Expand Down
12 changes: 6 additions & 6 deletions test/test_rfc5961.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ module Tcp_wire = Tcp.Tcp_wire
module Tcp_unmarshal = Tcp.Tcp_packet.Unmarshal
module Sequence = Tcp.Sequence

let sut_ip = Ipaddr.V4.of_string_exn "10.0.0.101"
let sut_cidr = Ipaddr.V4.Prefix.of_string_exn "10.0.0.101/24"
let server_ip = Ipaddr.V4.of_string_exn "10.0.0.100"
let network = Ipaddr.V4.Prefix.make 24 server_ip
let server_cidr = Ipaddr.V4.Prefix.make 24 server_ip
let gateway = Ipaddr.V4.of_string_exn "10.0.0.1"

let header_size = Ethernet_wire.sizeof_ethernet
Expand All @@ -47,13 +47,13 @@ let options = []
let window = 5120

let create_sut_stack backend =
VNETIF_STACK.create_stack ~ip:(network, sut_ip) ~gateway backend
VNETIF_STACK.create_stack ~cidr:sut_cidr ~gateway backend

let create_raw_stack ip backend =
let create_raw_stack backend =
V.connect backend >>= fun netif ->
E.connect netif >>= fun ethif ->
A.connect ethif >>= fun arpv4 ->
I.connect ~ip:(network, ip) ~gateway ethif arpv4 >>= fun ip ->
I.connect ~cidr:server_cidr ~gateway ethif arpv4 >>= fun ip ->
Lwt.return (netif, ethif, arpv4, ip)

type 'state fsm_result =
Expand All @@ -67,7 +67,7 @@ type 'state fsm_result =
let run backend fsm sut () =
let initial_state, fsm_handler = fsm in
create_sut_stack backend >>= fun stackv4 ->
create_raw_stack server_ip backend >>= fun (netif, ethif, arp, rawip) ->
create_raw_stack backend >>= fun (netif, ethif, arp, rawip) ->
let error_mbox = Lwt_mvar.create_empty () in
let stream, pushf = Lwt_stream.create () in
Lwt.pick [
Expand Down
4 changes: 2 additions & 2 deletions test/test_udp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type stack = {
let get_stack ?(backend = B.create ~use_async_readers:true
~yield:(fun() -> Lwt_main.yield ()) ()) ip =
let open Lwt.Infix in
let network = Ipaddr.V4.Prefix.make 24 ip in
let cidr = Ipaddr.V4.Prefix.make 24 ip in
V.connect backend >>= fun netif ->
E.connect netif >>= fun ethif ->
Static_arp.connect ethif >>= fun arp ->
Ip.connect ~ip:(network, ip) ethif arp >>= fun ip ->
Ip.connect ~cidr ethif arp >>= fun ip ->
Udp.connect ip >>= fun udp ->
Lwt.return { backend; netif; ethif; arp; ip; udp }

Expand Down
6 changes: 3 additions & 3 deletions test/vnetif_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sig
val create_backend : unit -> backend

(** Create a new stack connected to an existing backend *)
val create_stack : ?mtu:int -> ip:(Ipaddr.V4.Prefix.t * Ipaddr.V4.t) ->
val create_stack : ?mtu:int -> cidr:Ipaddr.V4.Prefix.t ->
?gateway:Ipaddr.V4.t -> backend -> Stackv4.t Lwt.t

(** [create_stack backend ?mtu ip netmask gateway] adds a listener
Expand Down Expand Up @@ -75,12 +75,12 @@ struct
let create_backend () =
B.create ()

let create_stack ?mtu ~ip ?gateway backend =
let create_stack ?mtu ~cidr ?gateway backend =
let size_limit = match mtu with None -> None | Some x -> Some x in
V.connect ?size_limit backend >>= fun netif ->
E.connect netif >>= fun ethif ->
A.connect ethif >>= fun arpv4 ->
Ip.connect ~ip ?gateway ethif arpv4 >>= fun ipv4 ->
Ip.connect ~cidr ?gateway ethif arpv4 >>= fun ipv4 ->
Icmp.connect ipv4 >>= fun icmpv4 ->
U.connect ipv4 >>= fun udpv4 ->
T.connect ipv4 >>= fun tcpv4 ->
Expand Down

0 comments on commit cab6b83

Please sign in to comment.