-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhub.ml
200 lines (178 loc) · 8.27 KB
/
hub.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
(* vim:sw=4 ts=4 sts=4 expandtab spell spelllang=en
*)
(* Copyright 2012, Cedric Cellier
*
* This file is part of RobiNet.
*
* RobiNet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RobiNet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with RobiNet. If not, see <http://www.gnu.org/licenses/>.
*)
open Batteries
open Bitstring
open Tools
(** A Repeater (or HUB) is a device that receives Eth frames and blindly mirrors them
to several locations (but the one from which the frame came from) *)
module Repeater =
struct
type t = { ifaces : (bitstring -> unit) array ;
is_connected : bool array ;
name : string ;
logger : Log.logger ;
ingress : Metric.Counter.t ;
egress : Metric.Counter.t }
let print oc t =
Printf.fprintf oc "repeater %s with %d ifaces" t.name (Array.length t.ifaces)
let make ?(parent_logger=Log.default) n name =
let logger = Log.sub parent_logger name in
{ ifaces = Array.make n (ignore_bits ~logger) ;
is_connected = Array.make n false ;
name ; logger ;
ingress = Metric.Counter.make (logger.full_name ^"/ingress") "bytes" ;
egress = Metric.Counter.make (logger.full_name ^"/egress") "bytes" }
let forward_from (t : t) n pld =
Array.iteri (fun i emit ->
if i <> n then (
Log.(log t.logger Debug (lazy (Printf.sprintf "Forward to iface %d/%d" i (Array.length t.ifaces)))) ;
Metric.(Counter.add t.egress ~params:(Params.singleton "port" (Param.Int i)) (bytelength pld)) ;
Clock.asap emit pld
)) t.ifaces
let write (t : t) n pld =
Log.(log t.logger Debug (lazy (Printf.sprintf "Rx from iface %d/%d" n (Array.length t.ifaces)))) ;
Metric.(Counter.add t.ingress ~params:(Params.singleton "port" (Param.Int n)) (bytelength pld)) ;
forward_from t n pld
let set_read (t : t) n f =
Log.(log t.logger Debug (lazy (Printf.sprintf "Setting reader for iface %d" n))) ;
t.is_connected.(n) <- true ;
t.ifaces.(n) <- f
(** Turns a iface into a device *)
let iface t n =
{ write = write t n ; set_read = set_read t n }
let t_printer _paren oc t =
Printf.fprintf oc "%d" (Array.length t.ifaces)
let first_free_iface t =
try Some (Array.findi not t.is_connected)
with Not_found -> None
end
(** A Switch is a device that will forward Ethernet frames based on the observed
location of the destination. *)
module Switch =
struct
module R = Repeater
type mac_entry =
{ mutable addr : Eth.Addr.t option ;
mutable iface : int }
type t =
{ hub : R.t ;
macs : mac_entry OrdArray.t ;
(* Mapping from mac to position in the OrdArray [macs] *)
macs_h : int BitHash.t ;
name : string ;
logger : Log.logger ;
mac_size : Metric.Gauge.t ;
mac_hits : Metric.Atomic.t ;
mac_misses : Metric.Atomic.t }
let print oc t =
Printf.fprintf oc "switch %s with %d ifaces" t.name (Array.length t.hub.ifaces)
(* [num_macs] is the maximum number of remembered MACs. *)
let make ?(parent_logger=Log.default) num_ifaces num_macs name =
let logger = Log.sub parent_logger name in
{ hub = R.make ~parent_logger:logger num_ifaces "hub" ;
macs = OrdArray.init num_macs (fun _ -> { addr = None ; iface = 0 }) ;
macs_h = BitHash.create (num_macs/10) ;
name ; logger ;
mac_size = Metric.Gauge.make (logger.full_name ^"/macs") ;
mac_hits = Metric.Atomic.make (logger.full_name ^"/hits") ;
mac_misses = Metric.Atomic.make (logger.full_name ^"/misses") }
let update_macs t src ins =
match BitHash.find_option t.macs_h src with
| None ->
Log.(log t.logger Debug (lazy (Printf.sprintf "New mac %s" (Eth.Addr.to_string (Eth.Addr.o src))))) ;
let last_idx = OrdArray.last t.macs in
let last = OrdArray.get t.macs last_idx in
(match last.addr with
| None ->
Metric.Gauge.add t.mac_size 1
| Some addr ->
(* This MAC which has not been used for long leaves the switch
* memory: *)
BitHash.remove t.macs_h (addr :> bitstring)) ;
last.addr <- Some (Eth.Addr.o src) ;
last.iface <- ins ;
BitHash.add t.macs_h src last_idx ;
OrdArray.promote t.macs last_idx
| Some n ->
let mac = OrdArray.get t.macs n in
if mac.iface <> ins then (
Log.(log t.logger Debug (lazy (Printf.sprintf "Host %s changed from iface %d to %d" (Eth.Addr.to_string (Eth.Addr.o src)) mac.iface ins))) ;
mac.iface <- ins
) ;
OrdArray.promote t.macs n
let forward_from t ins bits = match%bitstring bits with
| {| dst : 6*8 : bitstring ;
src : 6*8 : bitstring |} ->
(* update mac table for source (before forwarding!) *)
update_macs t src ins ;
(* TODO: addresses reserved by 802.1d should not be forwarded. *)
(* now forward *)
let do_broadcast () =
Log.(log t.logger Debug (lazy (Printf.sprintf "Forwarding to all ifaces (but %d)" ins))) ;
R.forward_from t.hub ins bits in
if Eth.Addr.is_broadcast (Eth.Addr.o dst) then
do_broadcast ()
else (
match BitHash.find_option t.macs_h dst with
| None ->
Log.(log t.logger Debug (lazy (Printf.sprintf "Unknown dest %s, broadcasting" (Eth.Addr.to_string (Eth.Addr.o dst))))) ;
Metric.Atomic.fire t.mac_misses ;
do_broadcast ()
| Some n ->
Metric.Atomic.fire t.mac_hits ;
let mac = OrdArray.get t.macs n in
if mac.iface <> ins then (
Log.(log t.logger Debug (lazy (Printf.sprintf "Known dest %s, will forward to iface %d" (Eth.Addr.to_string (Eth.Addr.o dst)) mac.iface))) ;
Clock.asap t.hub.Repeater.ifaces.(mac.iface) bits ;
OrdArray.promote t.macs n
) else
Log.(log t.logger Debug (lazy (Printf.sprintf "Known dest %s is located on iface %d, dropping" (Eth.Addr.to_string (Eth.Addr.o dst)) mac.iface)))
)
| {| _ |} ->
Log.(log t.logger Debug (lazy (Printf.sprintf "Drop incoming frame without destination")))
let write (t : t) n pld =
Log.(log t.logger Debug (lazy (Printf.sprintf "Rx from iface %d/%d" n (Array.length t.hub.ifaces)))) ;
forward_from t n pld
let set_read (t : t) n f =
Log.(log t.logger Debug (lazy (Printf.sprintf "Setting emitter for iface %d/%d" n (Array.length t.hub.ifaces)))) ;
Repeater.set_read t.hub n f
(** Turns a iface into a device *)
let iface (t : t) n =
{ write = write t n ; set_read = set_read t n }
let first_free_iface t =
R.first_free_iface t.hub
end
(** A Tap is a 2 ifaces repeater which mirror each packet to a user function.
It can be used as a transparent TRX. *)
module Tap =
struct
type t = { trx : trx ;
logger : Log.logger }
let make ?(parent_logger=Log.default) mirror =
let logger = Log.sub parent_logger "tap" in
let emit_ins = ref (ignore_bits ~logger)
and emit_out = ref (ignore_bits ~logger) in
let trx =
{ ins = { write = (fun bits -> mirror bits ; !emit_out bits) ;
set_read = fun f -> emit_ins := f } ;
out = { write = (fun bits -> mirror bits ; !emit_ins bits) ;
set_read = fun f -> emit_out := f } } in
{ trx ; logger }
end