Skip to content

Loading…

adding read_exactly method and default gw routing. #37

Closed
wants to merge 30 commits into from

4 participants

@crotsos
MirageOS member

Adding a read_exactly method in the channel module definition.

Modifying the routing method to select the default gw in case no routing entry is matched.

@djs55
MirageOS member

It seems a bit strange to return an "ethif" from "get_netif" instead of a "netif" -- perhaps the function should be renamed?

@avsm
MirageOS member

In this pattern match, you shouldn't have when. Just match directly on the value:

match len - avail with
| 0 -> ...
| 1 -> ...
| l -> ...
@avsm
MirageOS member

I think this can just be Cstruct.copyv, which does this concatenation... ?

MirageOS member
MirageOS member
@avsm avsm commented on the diff
direct/lib/manager.ml
@@ -139,7 +139,11 @@ let i_of_dst_ip t addr =
ret := Some(i);
netmask := Ipaddr.V4.to_int32
(Ipv4.get_netmask i.ipv4)
- )
+ ) else (
+ (* In case no match is found use the default gw of my routing table*)
+ if (List.length (Ipv4.get_gateways i.ipv4) > 0) then
@avsm MirageOS member
avsm added a note

just a style thing, but it's usual to write

match Ipv4.get_gateways i.ipv4 with
| [] -> ()
| x -> ret := Some x

In this case the rest is imperative so I dont think it matters too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@samoht
MirageOS member

Is this PR still up-to-date? @avsm if we have no plan to merge it, we should close it.

@avsm
MirageOS member

The manager bits are obsolete with Conduit, but I want to cherry pick the read_exactly patch into Channel as its very useful

@samoht samoht closed this
@samoht
MirageOS member

I believe that read_exactly is now cherry-picked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 16, 2012
  1. fixing code to detach net device

    crotsos committed
Commits on Dec 17, 2012
  1. A nimor fix to make ocaml-openflow compile

    crotsos committed
Commits on Dec 23, 2012
Commits on Dec 28, 2012
  1. Adding checksumming on socket backend

    crotsos committed
  2. merging

    crotsos committed
  3. fixing manager

    crotsos committed
Commits on Dec 29, 2012
  1. add checksum module in socket net-backend

    crotsos committed
Commits on Dec 30, 2012
Commits on Jan 4, 2013
  1. match socket API with direct API

    crotsos committed
Commits on Jan 7, 2013
Commits on Jan 11, 2013
Commits on Jan 14, 2013
Commits on Jan 17, 2013
  1. adding the read_exactly method

    crotsos committed
  2. merging

    crotsos committed
Commits on Jan 24, 2013
  1. Adding read exactly method on system

    crotsos committed
Commits on Jan 29, 2013
  1. Add routing capability on the Manager

    crotsos committed
  2. add Channel.read_exactly on socket backend

    crotsos committed
  3. adding ipv4_addr_of_uint32 on socket backend

    crotsos committed
  4. Merge github.com:mirage/mirage-net

    crotsos committed
  5. fixing the manager intf on socket backend

    crotsos committed
Commits on Mar 1, 2013
  1. @crotsos
Commits on Sep 20, 2013
  1. adding a read_exactly method in Channel module

    crotsos committed
  2. merging with main mirage-net repo

    crotsos committed
  3. suppressing white space differences with mirage/mirage-net to reduce …

    crotsos committed
    …the size of the pull request
Commits on Sep 27, 2013
  1. reducing difference for pull request

    crotsos committed
This page is out of date. Refresh to see the latest.
View
39 direct/lib/channel.ml
@@ -84,8 +84,40 @@ module Make(Flow:FLOW) :
t.ibuf <- None;
return buf
end
+
+ (* Read exactly len characters from the input channel
+ and at most a full view. If not specified, read all *)
+ let read_exactly t len =
+ let rec read_exactly_inner t len =
+ lwt buf = get_ibuf t in
+ let avail = Cstruct.len buf in
+ match (len - avail) with
+ | l when l < 0 ->
+ let hd,tl = Cstruct.split buf len in
+ t.ibuf <- Some tl;
+ return [hd]
+ | l when l = 0 ->
+ t.ibuf <- None;
+ return [buf]
+ | l ->
+ t.ibuf <- None;
+ lwt rest = read_exactly_inner t l in
+ return (buf::rest)
+ in
+ lwt buf = read_exactly_inner t len in
+ match buf with
+ | [buf] -> return buf
+ | _ ->
+ let ret = OS.Io_page.to_cstruct (OS.Io_page.get 1) in
+ let a =
+ List.fold_right (
+ fun a off ->
+ let l = Cstruct.len a in
+ let _ = Cstruct.blit a 0 ret (off-l) l in
+ off - l) buf len in
+ return (Cstruct.sub ret 0 len)
- (* Read up to len characters from the input channel as a
+ (* Read up to len characters from the input channel as a
stream (and read all available if no length specified *)
let read_stream ?len t =
Lwt_stream.from (fun () ->
@@ -241,6 +273,11 @@ let read_line = function
| TCPv4 t -> TCPv4.read_line t
| Shmem t -> Shmem.read_line t
+let read_exactly t len =
+ match t with
+ | TCPv4 t -> TCPv4.read_exactly t len
+ | Shmem t -> Shmem.read_exactly t len
+
let write_char = function
| TCPv4 t -> TCPv4.write_char t
| Shmem t -> Shmem.write_char t
View
5 direct/lib/channel.mli
@@ -45,6 +45,11 @@ val read_until: t -> char -> (bool * Cstruct.t) Lwt.t
belongs to the set of characters in the channel, or reads until
EOF otherwise. *)
+val read_exactly: t -> int -> Cstruct.t Lwt.t
+(** [read_exactly len c] reads exactly [len] characters from [c] and blocks until
+ * [len] characters are available. *)
+
+
val read_line: t -> Cstruct.t list Lwt.t
(** [read_line c] returns a list of views corresponding to one line
(e.g. that finishes by LF or CRLF). *)
View
1 direct/lib/ipv4.ml
@@ -174,6 +174,7 @@ let set_netmask t netmask =
let set_gateways t gateways =
t.gateways <- gateways;
return ()
+let get_gateways t = t.gateways
let mac t = Ethif.mac t.ethif
View
1 direct/lib/ipv4.mli
@@ -31,6 +31,7 @@ val get_ip: t -> Ipaddr.V4.t
val mac: t -> Macaddr.t
val set_netmask: t -> Ipaddr.V4.t -> unit Lwt.t
val set_gateways: t -> Ipaddr.V4.t list -> unit Lwt.t
+val get_gateways: t -> Ipaddr.V4.t list
val create : Ethif.t -> t * unit Lwt.t
val attach : t ->
View
7 direct/lib/manager.ml
@@ -139,7 +139,11 @@ let i_of_dst_ip t addr =
ret := Some(i);
netmask := Ipaddr.V4.to_int32
(Ipv4.get_netmask i.ipv4)
- )
+ ) else (
+ (* In case no match is found use the default gw of my routing table*)
+ if (List.length (Ipv4.get_gateways i.ipv4) > 0) then
@avsm MirageOS member
avsm added a note

just a style thing, but it's usual to write

match Ipv4.get_gateways i.ipv4 with
| [] -> ()
| x -> ret := Some x

In this case the rest is imperative so I dont think it matters too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ ret := Some(i)
+ )
) t.listeners in
match !ret with
| None -> failwith("No_Path_dst")
@@ -176,4 +180,3 @@ let get_intf_ipv4addr t id =
let set_promiscuous t id f =
let intf = Hashtbl.find t.listeners id in
Ethif.set_promiscuous intf.ethif (f id)
-
View
1 direct/lib/nettypes.ml
@@ -75,6 +75,7 @@ module type CHANNEL = sig
val read_char: t -> char Lwt.t
val read_until: t -> char -> (bool * Cstruct.t) Lwt.t
+ val read_exactly: t -> int -> Cstruct.t Lwt.t
val read_some: ?len:int -> t -> Cstruct.t Lwt.t
val read_stream: ?len:int -> t -> Cstruct.t Lwt_stream.t
val read_line: t -> Cstruct.t list Lwt.t
View
1 direct/lib/nettypes.mli
@@ -96,6 +96,7 @@ module type CHANNEL = sig
val read_char : t -> char Lwt.t
val read_until : t -> char -> (bool * Cstruct.t) Lwt.t
+ val read_exactly : t -> int -> Cstruct.t Lwt.t
val read_some : ?len:int -> t -> Cstruct.t Lwt.t
val read_stream : ?len: int -> t -> Cstruct.t Lwt_stream.t
val read_line : t -> Cstruct.t list Lwt.t
View
35 socket/lib/channel.ml
@@ -70,6 +70,38 @@ module Make(Flow:FLOW) :
t.ibuf <- Some (Cstruct.shift buf 1);
return c
+ (* Read exactly len characters from the input channel
+ and at most a full view. If not specified, read all *)
+ let read_exactly t len =
+ let rec read_exactly_inner t len =
+ lwt buf = get_ibuf t in
+ let avail = Cstruct.len buf in
+ match (len - avail) with
+ | l when l < 0 ->
+ let hd,tl = Cstruct.split buf len in
+ t.ibuf <- Some tl;
+ return [hd]
+ | l when l = 0 ->
+ t.ibuf <- None;
+ return [buf]
+ | l ->
+ t.ibuf <- None;
+ lwt rest = read_exactly_inner t l in
+ return (buf::rest)
+ in
+ lwt buf = read_exactly_inner t len in
+ match buf with
+ | [buf] -> return buf
+ | _ ->
+ let ret = OS.Io_page.to_cstruct (OS.Io_page.get 1) in
+ let a =
+ List.fold_right (
+ fun a off ->
+ let l = Cstruct.len a in
+ let _ = Cstruct.blit a 0 ret (off-l) l in
+ off - l) buf len in
+ return (Cstruct.sub ret 0 len)
+
(* Read up to len characters from the input channel
and at most a full view. If not specified, read all *)
let read_some ?len t =
@@ -227,6 +259,9 @@ let read_until = function
let read_some ?len = function
| TCPv4 t -> TCPv4.read_some ?len t
+let read_exactly t len =
+ match t with
+ | TCPv4 t -> TCPv4.read_exactly t len
let read_stream ?len = function
| TCPv4 t -> TCPv4.read_stream ?len t
View
1 socket/lib/channel.mli
@@ -27,6 +27,7 @@ type t
val read_char: t -> char Lwt.t
val read_some: ?len:int -> t -> Cstruct.t Lwt.t
+val read_exactly: t -> int -> Cstruct.t Lwt.t
val read_until: t -> char -> (bool * Cstruct.t) Lwt.t
val read_stream: ?len:int -> t -> Cstruct.t Lwt_stream.t
val read_line: t -> Cstruct.t list Lwt.t
View
1 socket/lib/net.mlpack
@@ -3,3 +3,4 @@ Manager
Flow
Datagram
Channel
+Checksum
View
1 socket/lib/nettypes.ml
@@ -78,6 +78,7 @@ module type CHANNEL = sig
val read_char: t -> char Lwt.t
val read_until: t -> char -> (bool * Cstruct.t) Lwt.t
+ val read_exactly: t -> int -> Cstruct.t Lwt.t
val read_some: ?len:int -> t -> Cstruct.t Lwt.t
val read_stream: ?len:int -> t -> Cstruct.t Lwt_stream.t
val read_line: t -> Cstruct.t list Lwt.t
View
1 socket/lib/nettypes.mli
@@ -101,6 +101,7 @@ module type CHANNEL = sig
val read_char : t -> char Lwt.t
val read_until : t -> char -> (bool * Cstruct.t) Lwt.t
+ val read_exactly: t -> int -> Cstruct.t Lwt.t
val read_some : ?len:int -> t -> Cstruct.t Lwt.t
val read_stream : ?len: int -> t -> Cstruct.t Lwt_stream.t
val read_line : t -> Cstruct.t list Lwt.t
Something went wrong with that request. Please try again.