Skip to content

Commit 11490b7

Browse files
committed
port from jbuilder to dune and use virtual libraries
This changes the external ocamlfind interface to break out the virtual checksum stubs library into a separate `tcpip-checksum` library. This library is then depended on by the rest of the tcpip stack.
1 parent 04fd73c commit 11490b7

File tree

19 files changed

+665
-71
lines changed

19 files changed

+665
-71
lines changed

CHANGES.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### dev
2+
3+
* Port from Jbuilder to Dune and use virtual libraries to cleanup the stub compilation
4+
of checksums. (@avsm).
5+
16
### v3.7.0 (2019-02-02)
27

38
* Use `Lwt_dllist` instead of `Lwt_sequence`, due to the latter being deprecated
@@ -21,6 +26,8 @@
2126
* Use `Ipaddr.pp` instead of `Ipaddr.pp_hum` due to upstream
2227
interface changes (#385 @hannesm).
2328

29+
* Use `Ipaddr.pp` instead of `Ipaddr.pp_hum` due to upstream interface changes (@avsm).
30+
2431
### v3.5.1 (2018-11-16)
2532

2633
* socket stack (tcp/udp): catch exception in recv_from and accept (#376 @hannesm)

dune-project

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(lang dune 1.0)
1+
(lang dune 1.7)
22
(name tcpip)

examples/ping/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(executables
22
(names ping)
3-
(libraries cmdliner logs logs.fmt tcpip.icmpv4-socket))
3+
(libraries cmdliner logs logs.fmt tcpip.icmpv4-socket tcpip-checksum.unix))

src/icmp/dune

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(library
22
(name tcpip_icmpv4)
33
(public_name tcpip.icmpv4)
4-
(libraries mirage-protocols-lwt rresult logs tcpip mirage-profile tcpip.udp)
5-
(preprocess
6-
(pps ppx_cstruct))
4+
(libraries mirage-protocols-lwt rresult logs tcpip-checksum mirage-profile tcpip.udp)
5+
(preprocess (pps ppx_cstruct))
76
(wrapped false))

src/ipv4/dune

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
(library
2-
(name tcpip_ipv4)
2+
(name tcpip_ipv4)
33
(public_name tcpip.ipv4)
4-
(libraries logs io-page mirage-protocols-lwt ipaddr cstruct rresult tcpip
5-
ethernet tcpip.udp mirage-random mirage-clock randomconv lru)
6-
(preprocess
7-
(pps ppx_cstruct))
4+
(libraries logs io-page mirage-protocols-lwt ipaddr cstruct rresult
5+
tcpip-checksum ethernet tcpip.udp mirage-random mirage-clock randomconv lru)
6+
(preprocess (pps ppx_cstruct))
87
(wrapped false))

src/ipv6/dune

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
(library
22
(name tcpip_ipv6)
33
(public_name tcpip.ipv6)
4-
(libraries logs io-page mirage-protocols-lwt mirage-time-lwt
5-
mirage-clock-lwt duration ipaddr cstruct rresult mirage-random tcpip
6-
randomconv ethernet)
7-
(preprocess
8-
(pps ppx_cstruct))
4+
(libraries logs io-page mirage-protocols-lwt mirage-time-lwt mirage-clock-lwt duration ipaddr cstruct rresult
5+
mirage-random tcpip-checksum randomconv ethernet)
6+
(preprocess (pps ppx_cstruct))
97
(wrapped false))

src/tcp/dune

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(library
22
(name tcp)
33
(public_name tcpip.tcp)
4-
(libraries logs mirage-protocols-lwt ipaddr cstruct lwt-dllist rresult
5-
mirage-profile io-page tcpip duration randomconv fmt mirage-time-lwt
6-
mirage-clock mirage-random)
7-
(preprocess
8-
(pps ppx_cstruct)))
4+
(libraries logs mirage-protocols-lwt ipaddr cstruct
5+
rresult mirage-profile io-page tcpip-checksum duration
6+
randomconv fmt mirage-time-lwt mirage-clock
7+
mirage-random lwt-dllist)
8+
(preprocess (pps ppx_cstruct)))

src/tcpip_checksum/dune

+4-35
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,6 @@
11
(library
2-
(name tcpip)
3-
(public_name tcpip)
2+
(name tcpip_checksum)
3+
(public_name tcpip-checksum)
44
(modules tcpip_checksum)
5-
(libraries cstruct tcpip.unix)
6-
(wrapped false))
7-
8-
(library
9-
(name tcpip_xen)
10-
(public_name tcpip.xen)
11-
(libraries tcpip)
12-
(modules tcpip_xen)
13-
(c_names checksum_stubs_xen)
14-
(c_flags
15-
(:include c_flags_xen.sexp))
16-
(wrapped false))
17-
18-
(rule
19-
(targets c_flags_xen.sexp)
20-
(deps
21-
(:< ../config/discover.exe))
22-
(action
23-
(run %{<} -ocamlc %{ocamlc})))
24-
25-
(rule
26-
(targets checksum_stubs_xen.c)
27-
(deps
28-
(:< checksum_stubs.c))
29-
(action
30-
(copy# %{<} %{targets})))
31-
32-
(library
33-
(name tcpip_unix)
34-
(public_name tcpip.unix)
35-
(modules tcpip_unix)
36-
(c_names checksum_stubs)
37-
(wrapped false))
5+
(virtual_modules tcpip_checksum)
6+
(libraries cstruct))

src/tcpip_checksum/ocaml/dune

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(library
2+
(name tcpip_checksum_ocaml)
3+
(public_name tcpip-checksum.ocaml)
4+
(implements tcpip_checksum))
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
(*
2+
* Copyright (c) 2010-2011 Anil Madhavapeddy <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*)
16+
17+
let rec finalise_checksum cs =
18+
assert (cs >= 0);
19+
if cs < 0x10000 then
20+
lnot cs land 0xffff
21+
else
22+
finalise_checksum ((cs land 0xffff) + (cs lsr 16))
23+
24+
let ones_complement (buffer: Cstruct.t) =
25+
let len = Cstruct.len buffer in
26+
let rec do_checksum checksum offset =
27+
if offset + 1 < len then (
28+
let checksum = checksum + Cstruct.BE.get_uint16 buffer offset in
29+
do_checksum checksum (offset + 2)
30+
) else if offset + 1 = len then (
31+
let checksum = checksum + (Cstruct.get_uint8 buffer offset lsl 8) in
32+
finalise_checksum checksum
33+
) else
34+
finalise_checksum checksum
35+
in
36+
do_checksum 0 0
37+
38+
let ones_complement_list buffers =
39+
let rec do_checksum checksum offset len buffer buffers =
40+
if offset + 1 < len then (
41+
let checksum = checksum + Cstruct.BE.get_uint16 buffer offset in
42+
do_checksum checksum (offset + 2) len buffer buffers
43+
) else (
44+
let extra_single_byte = offset + 1 = len in
45+
match buffers with
46+
| [] ->
47+
let checksum =
48+
checksum +
49+
if extra_single_byte then Cstruct.get_uint8 buffer offset lsl 8 else 0
50+
in
51+
finalise_checksum checksum
52+
| next_buffer :: buffers ->
53+
let checksum =
54+
checksum +
55+
if extra_single_byte
56+
then Cstruct.get_uint8 next_buffer 0 + (Cstruct.get_uint8 buffer offset lsl 8)
57+
else 0
58+
in
59+
let offset = if extra_single_byte then 1 else 0 in
60+
let len = Cstruct.len next_buffer in
61+
do_checksum checksum offset len next_buffer buffers
62+
)
63+
in
64+
match buffers with
65+
| buffer :: buffers ->
66+
let len = Cstruct.len buffer in
67+
do_checksum 0 0 len buffer buffers
68+
| [] -> finalise_checksum 0

src/tcpip_checksum/tcpip_xen.ml

Whitespace-only changes.

src/tcpip_checksum/unix/dune

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(library
2+
(name tcpip_checksum_unix)
3+
(public_name tcpip-checksum.unix)
4+
(implements tcpip_checksum)
5+
(c_names checksum_stubs))
6+
7+
(copy_files# ../checksum_stubs.c)
8+
9+

src/tcpip_checksum/tcpip_checksum.ml src/tcpip_checksum/unix/tcpip_checksum.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(*
2-
* Copyright (c) 2010-2011 Anil Madhavapeddy <[email protected]>
2+
* Copyright (c) 2010-2018 Anil Madhavapeddy <[email protected]>
33
*
44
* Permission to use, copy, modify, and distribute this software for any
55
* purpose with or without fee is hereby granted, provided that the above
@@ -15,6 +15,7 @@
1515
*)
1616

1717
(** One's complement checksum, RFC1071 *)
18-
external ones_complement: Cstruct.t -> int = "caml_tcpip_ones_complement_checksum"
1918

19+
external ones_complement: Cstruct.t -> int = "caml_tcpip_ones_complement_checksum"
2020
external ones_complement_list: Cstruct.t list -> int = "caml_tcpip_ones_complement_checksum_list"
21+

src/tcpip_checksum/xen/dune

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(library
2+
(name tcpip_checksum_xen)
3+
(public_name tcpip-checksum.xen)
4+
(implements tcpip_checksum)
5+
(c_names checksum_stubs)
6+
(c_flags (:include c_flags_xen.sexp)))
7+
8+
(rule
9+
(targets c_flags_xen.sexp)
10+
(deps ../../config/discover.exe)
11+
(action (run %{deps})))
12+
13+
(copy_files# ../checksum_stubs.c)
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(*
2+
* Copyright (c) 2010-2018 Anil Madhavapeddy <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*)
16+
17+
(** One's complement checksum, RFC1071 *)
18+
19+
external ones_complement: Cstruct.t -> int = "caml_tcpip_ones_complement_checksum"
20+
external ones_complement_list: Cstruct.t list -> int = "caml_tcpip_ones_complement_checksum_list"
21+

src/udp/dune

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(library
22
(name tcpip_udpv4)
33
(public_name tcpip.udp)
4-
(libraries mirage-protocols-lwt mirage-random rresult logs tcpip randomconv)
5-
(preprocess
6-
(pps ppx_cstruct))
4+
(libraries mirage-protocols-lwt mirage-random rresult logs tcpip-checksum randomconv)
5+
(preprocess (pps ppx_cstruct))
76
(wrapped false))
File renamed without changes.

test/dune

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
(executables
22
(names test)
3-
(libraries alcotest mirage-random-test lwt.unix io-page-unix tcpip.unix logs
4-
logs.fmt mirage-profile mirage-flow mirage-vnetif mirage-clock-unix
5-
pcap-format duration mirage-random rresult mirage-protocols-lwt
6-
mirage-stack-lwt arp arp-mirage ethernet tcpip.ipv4 tcpip.tcp tcpip.udp
7-
tcpip.stack-direct tcpip.icmpv4 tcpip.udpv4-socket tcpip.tcpv4-socket
8-
tcpip.icmpv4-socket tcpip.stack-socket tcpip.ipv6))
3+
(libraries
4+
alcotest mirage-random-test lwt.unix io-page-unix tcpip-checksum.unix
5+
logs logs.fmt mirage-profile mirage-flow mirage-vnetif
6+
mirage-clock-unix pcap-format duration mirage-random
7+
rresult mirage-protocols-lwt mirage-stack-lwt
8+
ethernet arp-mirage tcpip.ipv4 tcpip.tcp tcpip.udp
9+
tcpip.stack-direct tcpip.icmpv4
10+
tcpip.udpv4-socket tcpip.tcpv4-socket tcpip.icmpv4-socket
11+
tcpip.stack-socket tcpip.ipv6))
912

1013
(alias
11-
(name runtest)
12-
(deps
13-
(:< test.exe))
14-
(action
15-
(run %{<} -q -e --color=always)))
14+
(name runtest)
15+
(deps test.exe)
16+
(action (run %{deps} -q -e --color=always)))

0 commit comments

Comments
 (0)