Skip to content

Commit

Permalink
Merge pull request #288 from djs55/add-mtu
Browse files Browse the repository at this point in the history
Compute TCP MSS from IP, ethif MTU
  • Loading branch information
yomimono committed Feb 13, 2017
2 parents 9e131e8 + b0733f0 commit 12f6ad2
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions _oasis
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Library "icmpv4-socket"
cstruct.lwt,
io-page.unix,
tcpip.icmpv4,
tcpip.ipv4,
tcpip.ipv6,
mirage-protocols,mirage-protocols-lwt

Library "udpv4-socket"
Expand Down
14 changes: 13 additions & 1 deletion _tags
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OASIS_START
# DO NOT EDIT (digest: f4270b7cbeb239d804a95dc1c080220b)
# DO NOT EDIT (digest: 417bae857bae4a5a1016501e56db0a47)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# useless stuff for the build process
Expand Down Expand Up @@ -186,13 +186,25 @@ true: annot, bin_annot
"unix/icmpv4-socket.cmxs": use_icmpv4-socket
<unix/*.ml{,i,y}>: pkg_cstruct
<unix/*.ml{,i,y}>: pkg_cstruct.ppx
<unix/*.ml{,i,y}>: pkg_duration
<unix/*.ml{,i,y}>: pkg_io-page
<unix/*.ml{,i,y}>: pkg_ipaddr
<unix/*.ml{,i,y}>: pkg_mirage-clock
<unix/*.ml{,i,y}>: pkg_mirage-clock-lwt
<unix/*.ml{,i,y}>: pkg_mirage-net-lwt
<unix/*.ml{,i,y}>: pkg_mirage-profile
<unix/*.ml{,i,y}>: pkg_mirage-random
<unix/*.ml{,i,y}>: pkg_mirage-time-lwt
<unix/*.ml{,i,y}>: pkg_randomconv
<unix/*.ml{,i,y}>: pkg_result
<unix/*.ml{,i,y}>: pkg_rresult
<unix/*.ml{,i,y}>: use_ethif
<unix/*.ml{,i,y}>: use_icmpv4
<unix/*.ml{,i,y}>: use_ipv4
<unix/*.ml{,i,y}>: use_ipv6
<unix/*.ml{,i,y}>: use_tcp
<unix/*.ml{,i,y}>: use_tcpip
<unix/*.ml{,i,y}>: use_udp
# Library udpv4-socket
"unix/udpv4-socket.cmxs": use_udpv4-socket
# Library udpv6-socket
Expand Down
4 changes: 2 additions & 2 deletions lib/META
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OASIS_START
# DO NOT EDIT (digest: af6a7b40c941c47bca5c459a000ce665)
# DO NOT EDIT (digest: 514c9a0cdf340349d239ac493ed47a94)
version = "3.0.0"
description =
"Implementations for network-related module types from MirageOS."
Expand Down Expand Up @@ -157,7 +157,7 @@ package "icmpv4-socket" (
description =
"Implementations for network-related module types from MirageOS."
requires =
"lwt lwt.unix ipaddr.unix cstruct.lwt io-page.unix tcpip.icmpv4 mirage-protocols mirage-protocols-lwt"
"lwt lwt.unix ipaddr.unix cstruct.lwt io-page.unix tcpip.icmpv4 tcpip.ipv4 tcpip.ipv6 mirage-protocols mirage-protocols-lwt"
archive(byte) = "icmpv4-socket.cma"
archive(byte, plugin) = "icmpv4-socket.cma"
archive(native) = "icmpv4-socket.cmxa"
Expand Down
8 changes: 6 additions & 2 deletions lib/ethif/ethif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ open Lwt.Infix
let src = Logs.Src.create "ethif" ~doc:"Mirage Ethernet"
module Log = (val Logs.src_log src : Logs.LOG)

let default_mtu = 1500

module Make(Netif : Mirage_net_lwt.S) = struct

type 'a io = 'a Lwt.t
Expand All @@ -33,9 +35,11 @@ module Make(Netif : Mirage_net_lwt.S) = struct

type t = {
netif: Netif.t;
mtu: int;
}

let mac t = Netif.mac t.netif
let mtu t = t.mtu

let input ~arpv4 ~ipv4 ~ipv6 t frame =
let open Ethif_packet in
Expand Down Expand Up @@ -73,9 +77,9 @@ module Make(Netif : Mirage_net_lwt.S) = struct
Log.warn (fun f -> f "netif writev errored %a" Netif.pp_error e) ;
Error e

let connect netif =
let connect ?(mtu = default_mtu) netif =
MProf.Trace.label "ethif.connect";
let t = { netif } in
let t = { netif; mtu } in
Log.info (fun f -> f "Connected Ethernet interface %s" (Macaddr.to_string (mac t)));
Lwt.return t

Expand Down
7 changes: 6 additions & 1 deletion lib/ethif/ethif.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@

module Make ( N:Mirage_net_lwt.S) : sig
include Mirage_protocols_lwt.ETHIF with type netif = N.t
val connect : netif -> t Lwt.t

val connect : ?mtu:int -> netif -> t Lwt.t
(** [connect ?mtu netif] connects an ethernet layer on top of the raw
network device [netif]. The Maximum Transfer Unit may be set via the
optional [?mtu] parameter, otherwise a default value of 1500 will be
used. *)
end
2 changes: 2 additions & 0 deletions lib/ipv4/static_ipv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ module Make(Ethif: Mirage_protocols_lwt.ETHIF) (Arpv4 : Mirage_protocols_lwt.ARP
let to_uipaddr ip = Ipaddr.V4 ip
let of_uipaddr = Ipaddr.to_v4

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

end
3 changes: 3 additions & 0 deletions lib/ipv6/ipv6.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ module Make (E : Mirage_protocols_lwt.ETHIF)
let allocate_frame t ~dst ~proto =
Ndpv6.allocate_frame t.ctx dst proto

let mtu t =
E.mtu t.ethif - Ipv6_wire.sizeof_ipv6

let writev t frame bufs =
let now = C.elapsed_ns t.clock in
let dst =
Expand Down
4 changes: 2 additions & 2 deletions lib/tcp/pcb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ struct
Hashtbl.add t.listens id (params.tx_isn, (pushf, (pcb, th)));
Stats.incr_listen ();
(* Queue a SYN ACK for transmission *)
let options = Options.MSS 1460 :: opts in
let options = Options.MSS (Ip.mtu t.ip - Tcp_wire.sizeof_tcp) :: opts in
TXS.output ~flags:Segment.Syn ~options pcb.txq (Cstruct.create 0) >>= fun () ->
Lwt.return (pcb, th)

Expand Down Expand Up @@ -623,7 +623,7 @@ struct
(* TODO: This is hardcoded for now - make it configurable *)
let rx_wnd_scaleoffer = wscale_default in
let options =
Options.MSS 1460 :: Options.Window_size_shift rx_wnd_scaleoffer :: []
Options.MSS (Ip.mtu t.ip - Tcp_wire.sizeof_tcp) :: Options.Window_size_shift rx_wnd_scaleoffer :: []
in
let window = 5840 in
let th, wakener = MProf.Trace.named_task "TCP connect" in
Expand Down
3 changes: 1 addition & 2 deletions lib/tcp/window.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type t = {
let count_ackd_segs = MProf.Counter.make ~name:"tcp-ackd-segs"

let default_mss = 536
let max_mss = 1460

let alpha = 0.125 (* see RFC 2988 *)
let beta = 0.25 (* see RFC 2988 *)
Expand All @@ -81,7 +80,7 @@ let t ~rx_wnd_scale ~tx_wnd_scale ~rx_wnd ~tx_wnd ~rx_isn ~tx_mss ~tx_isn =
let rx_nxt = Sequence.incr rx_isn in
let rx_nxt_inseq = Sequence.incr rx_isn in
(* TODO: improve this sanity check of tx_mss *)
let tx_mss = match tx_mss with |None -> default_mss |Some mss -> min mss max_mss in
let tx_mss = match tx_mss with |None -> default_mss |Some mss -> mss in
let snd_una = tx_nxt in
let fast_rec_th = tx_nxt in
let ack_serviced = true in
Expand Down
4 changes: 2 additions & 2 deletions myocamlbuild.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(* OASIS_START *)
(* DO NOT EDIT (digest: 61d522780b0d74636b2863a6585885db) *)
(* DO NOT EDIT (digest: 2b48a56d3f5cff3a6f50fcaa0be7ece6) *)
module OASISGettext = struct
(* # 22 "src/oasis/OASISGettext.ml" *)

Expand Down Expand Up @@ -922,7 +922,7 @@ let package_default =
];
includes =
[
("unix", ["lib/icmp"]);
("unix", ["lib/icmp"; "lib/ipv4"; "lib/ipv6"]);
("lib_test",
[
"lib";
Expand Down
8 changes: 5 additions & 3 deletions setup.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(* setup.ml generated for the first time by OASIS v0.4.5 *)

(* OASIS_START *)
(* DO NOT EDIT (digest: cbeda8f1a0d1981bd8673b1f4f130d43) *)
(* DO NOT EDIT (digest: b685ec63d44811f24db90b6ae2c9c30a) *)
(*
Regenerated by OASIS v0.4.8
Visit http://oasis.forge.ocamlcore.org for more information and
Expand Down Expand Up @@ -8667,6 +8667,8 @@ let setup_t =
FindlibPackage ("cstruct.lwt", None);
FindlibPackage ("io-page.unix", None);
InternalLibrary "icmpv4";
InternalLibrary "ipv4";
InternalLibrary "ipv6";
FindlibPackage ("mirage-protocols", None);
FindlibPackage ("mirage-protocols-lwt", None)
];
Expand Down Expand Up @@ -9796,15 +9798,15 @@ let setup_t =
};
oasis_fn = Some "_oasis";
oasis_version = "0.4.8";
oasis_digest = Some "E\1568G\151\151\152\128\020\174Eaz[\r\191";
oasis_digest = Some "\211`}\212\224?\031\023eS_\003;&\202\196";
oasis_exec = None;
oasis_setup_args = [];
setup_update = false
};;

let setup () = BaseSetup.setup setup_t;;

# 9808 "setup.ml"
# 9810 "setup.ml"
let setup_t = BaseCompat.Compat_0_4.adapt_setup_t setup_t
open BaseCompat.Compat_0_4
(* OASIS_STOP *)
Expand Down
1 change: 1 addition & 0 deletions unix/ipv4_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let pp_error = Mirage_protocols.Ip.pp_error

let to_uipaddr ip = Ipaddr.V4 ip
let of_uipaddr = Ipaddr.to_v4
let mtu _ = 1500 - Ipv4_wire.sizeof_ipv4

let id _ = ()
let disconnect _ = return_unit
Expand Down
1 change: 1 addition & 0 deletions unix/ipv6_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type uipaddr = Ipaddr.t

let to_uipaddr ip = Ipaddr.V6 ip
let of_uipaddr ip = Some (Ipaddr.to_v6 ip)
let mtu _ = 1500 - Ipv6_wire.sizeof_ipv6

let id _ = ()
let disconnect () = return_unit
Expand Down

0 comments on commit 12f6ad2

Please sign in to comment.