Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FlowRemoved messages in OpenFlow 0x01 #52

Merged
merged 7 commits into from
Dec 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ env:
- FRENETIC_DEPENDS="ocaml-packet"
matrix:
- OCAML_VERSION=4.01.0 OPAM_VERSION=1.1.0
- OCAML_VERSION=4.00.1 OPAM_VERSION=1.0.0
- OCAML_VERSION=4.00.1 OPAM_VERSION=1.1.0
notifications:
irc:
channels:
Expand Down
1 change: 1 addition & 0 deletions _oasis
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
OASISFormat: 0.3
OCamlVersion: >= 4.01.0
Name: openflow
Version: 0.2.0
Synopsis: Serialization library for OpenFlow
Expand Down
172 changes: 139 additions & 33 deletions lib/OpenFlow0x01.ml
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,25 @@ module Action = struct

end

module Timeout = struct

type t = timeout

let to_string t = match t with
| Permanent -> "Permanent"
| ExpiresAfter n -> Printf.sprintf "ExpiresAfter %d" n

let size_of _ = 2

let to_int x = match x with
| Permanent -> 0
| ExpiresAfter w -> w

let of_int d =
if d = 0 then Permanent else ExpiresAfter d
end


module FlowMod = struct

module Command = struct
Expand Down Expand Up @@ -644,25 +663,6 @@ module FlowMod = struct

end

module Timeout = struct

type t = timeout

let to_string t = match t with
| Permanent -> "Permanent"
| ExpiresAfter n -> Printf.sprintf "ExpiresAfter %d" n

let size_of _ = 2

let to_int x = match x with
| Permanent -> 0
| ExpiresAfter w -> w

let of_int d =
if d = 0 then Permanent else ExpiresAfter d

end

type t = flowMod

cstruct ofp_flow_mod {
Expand All @@ -676,7 +676,7 @@ module FlowMod = struct
uint16_t flags
} as big_endian

let to_string m = Printf.sprintf
let to_string (m:t) = Printf.sprintf
"{ command = %s; match = %s; priority = %d; actions = %s; cookie = %Ld;\
idle_timeout = %s; hard_timeout = %s; notify_when_removed = %B;\
apply_to_packet = %s; out_port = %s; check_overlap = %B }"
Expand All @@ -692,7 +692,7 @@ module FlowMod = struct
(Misc.string_of_option PseudoPort.to_string m.out_port)
m.check_overlap

let size_of msg =
let size_of (msg:flowMod) =
(Match.size_of msg.pattern)
+ sizeof_ofp_flow_mod
+ (Action.size_of_sequence msg.actions)
Expand Down Expand Up @@ -741,27 +741,27 @@ module FlowMod = struct
else Some (make out_port 0));
check_overlap = check_overlap_of_flags flags }

let marshal m bits =
let bits = Cstruct.shift bits (Match.marshal m.pattern bits) in
set_ofp_flow_mod_cookie bits (m.cookie);
set_ofp_flow_mod_command bits (Command.to_int m.command);
set_ofp_flow_mod_idle_timeout bits (Timeout.to_int m.idle_timeout);
set_ofp_flow_mod_hard_timeout bits (Timeout.to_int m.hard_timeout);
set_ofp_flow_mod_priority bits (m.priority);
let marshal (msg:flowMod) bits =
let bits = Cstruct.shift bits (Match.marshal msg.pattern bits) in
set_ofp_flow_mod_cookie bits (msg.cookie);
set_ofp_flow_mod_command bits (Command.to_int msg.command);
set_ofp_flow_mod_idle_timeout bits (Timeout.to_int msg.idle_timeout);
set_ofp_flow_mod_hard_timeout bits (Timeout.to_int msg.hard_timeout);
set_ofp_flow_mod_priority bits (msg.priority);
set_ofp_flow_mod_buffer_id bits
(match m.apply_to_packet with
(match msg.apply_to_packet with
| None -> -1l
| Some bufId -> bufId);
set_ofp_flow_mod_out_port bits (PseudoPort.marshal_optional m.out_port);
set_ofp_flow_mod_out_port bits (PseudoPort.marshal_optional msg.out_port);
set_ofp_flow_mod_flags bits
(flags_to_int m.check_overlap m.notify_when_removed);
(flags_to_int msg.check_overlap msg.notify_when_removed);
let bits = Cstruct.shift bits sizeof_ofp_flow_mod in
let _ = List.fold_left
(fun bits act ->
Cstruct.shift bits (Action.marshal act bits))
bits
(Action.move_controller_last m.actions) in
size_of m
(Action.move_controller_last msg.actions) in
size_of msg

end

Expand Down Expand Up @@ -868,6 +868,104 @@ module PacketIn = struct
size_of pi
end

module FlowRemoved = struct

module Reason = struct

type t = flowRemovedReason

cenum ofp_flow_removed_reason {
IDLE_TIMEOUT = 0;
HARD_TIMEOUT = 1;
DELETE = 2
} as uint8_t

let of_int d = match int_to_ofp_flow_removed_reason d with
| Some IDLE_TIMEOUT -> IdleTimeout
| Some HARD_TIMEOUT -> HardTimeout
| Some DELETE -> Delete
| None -> raise (Unparsable (sprintf "bad reason in flow_removed (%d)" d))

let to_int r = match r with
| IdleTimeout -> ofp_flow_removed_reason_to_int IDLE_TIMEOUT
| HardTimeout -> ofp_flow_removed_reason_to_int HARD_TIMEOUT
| Delete -> ofp_flow_removed_reason_to_int DELETE

let to_string r = match r with
| IdleTimeout -> "IdleTimeout"
| HardTimeout -> "HardTimeout"
| Delete -> "Delete"

let size_of _ = 1

end

type t = flowRemoved

cstruct ofp_flow_removed {
uint64_t cookie;
uint16_t priority;
uint8_t reason;
uint8_t pad[1];
uint32_t duration_sec;
uint32_t duration_nsec;
uint16_t idle_timeout;
uint8_t pad2[2];
uint64_t packet_count;
uint64_t byte_count
} as big_endian

let parse bits =
let pattern = Match.parse bits in
let bits = Cstruct.shift bits (Match.size_of pattern) in
let pattern = Match.parse bits in
let cookie = get_ofp_flow_removed_cookie bits in
let priority = get_ofp_flow_removed_priority bits in
let reason = Reason.of_int (get_ofp_flow_removed_reason bits) in
let duration_sec = get_ofp_flow_removed_duration_sec bits in
let duration_nsec = get_ofp_flow_removed_duration_nsec bits in
let idle_timeout = Timeout.of_int (get_ofp_flow_removed_idle_timeout bits) in
let packet_count = get_ofp_flow_removed_packet_count bits in
let byte_count = get_ofp_flow_removed_byte_count bits in
{ pattern = pattern
; cookie = cookie
; priority = priority
; reason = reason
; duration_sec = duration_sec
; duration_nsec = duration_nsec
; idle_timeout = idle_timeout
; packet_count = packet_count
; byte_count = byte_count }

let to_string msg = Printf.sprintf
"{ flow = %s; cookie = %Ld; priority = %d; reason = %s; duration_sec = %ld;\
duration_nsec = %ld; idle_timeout = %s; packet_count = %Ld; byte_count = %Ld }"
(Match.to_string msg.pattern)
msg.cookie
msg.priority
(Reason.to_string msg.reason)
msg.duration_sec
msg.duration_nsec
(Timeout.to_string msg.idle_timeout)
msg.packet_count
msg.byte_count

let size_of _ =
sizeof_ofp_flow_removed

let marshal (msg:t) (bits:Cstruct.t) : int =
let bits = Cstruct.shift bits (Match.marshal msg.pattern bits) in
set_ofp_flow_removed_cookie bits (msg.cookie);
set_ofp_flow_removed_priority bits (msg.priority);
set_ofp_flow_removed_reason bits (Reason.to_int msg.reason);
set_ofp_flow_removed_duration_sec bits (msg.duration_sec);
set_ofp_flow_removed_duration_nsec bits (msg.duration_nsec);
set_ofp_flow_removed_idle_timeout bits (Timeout.to_int msg.idle_timeout);
set_ofp_flow_removed_packet_count bits (msg.packet_count);
set_ofp_flow_removed_byte_count bits (msg.byte_count);
size_of msg
end

module PacketOut = struct

type t = packetOut
Expand Down Expand Up @@ -2389,6 +2487,7 @@ module Message = struct
| SwitchFeaturesReply of SwitchFeatures.t
| FlowModMsg of FlowMod.t
| PacketInMsg of PacketIn.t
| FlowRemovedMsg of FlowRemoved.t
| PortStatusMsg of PortStatus.t
| PacketOutMsg of PacketOut.t
| BarrierRequest
Expand All @@ -2410,6 +2509,7 @@ module Message = struct
| FEATURES_REQ -> SwitchFeaturesRequest
| FEATURES_RESP -> SwitchFeaturesReply (SwitchFeatures.parse buf)
| PACKET_IN -> PacketInMsg (PacketIn.parse buf)
| FLOW_REMOVED -> FlowRemovedMsg (FlowRemoved.parse buf)
| PORT_STATUS -> PortStatusMsg (PortStatus.parse buf)
| BARRIER_REQ -> BarrierRequest
| BARRIER_RESP -> BarrierReply
Expand Down Expand Up @@ -2438,6 +2538,7 @@ module Message = struct
| PacketOutMsg _ -> PACKET_OUT
| PortStatusMsg _ -> PORT_STATUS
| PacketInMsg _ -> PACKET_IN
| FlowRemovedMsg _ -> FLOW_REMOVED
| BarrierRequest -> BARRIER_REQ
| BarrierReply -> BARRIER_RESP
| StatsRequestMsg _ -> STATS_REQ
Expand All @@ -2458,6 +2559,7 @@ module Message = struct
| PacketOutMsg _ -> "PacketOut"
| PortStatusMsg _ -> "PortStatus"
| PacketInMsg _ -> "PacketIn"
| FlowRemovedMsg _ -> "FlowRemoved"
| BarrierRequest -> "BarrierRequest"
| BarrierReply -> "BarrierReply"
| StatsRequestMsg _ -> "StatsRequest"
Expand All @@ -2482,6 +2584,7 @@ module Message = struct
| BarrierReply -> 0
| StatsRequestMsg msg -> StatsRequest.size_of msg
| PacketInMsg msg -> PacketIn.size_of msg
| FlowRemovedMsg msg -> FlowRemoved.size_of msg
| SetConfig msg -> SwitchConfig.size_of msg
| ConfigRequestMsg -> 0
| ConfigReplyMsg msg -> ConfigReply.size_of msg
Expand Down Expand Up @@ -2519,6 +2622,9 @@ module Message = struct
| PacketInMsg msg ->
let _ = PacketIn.marshal msg out in
()
| FlowRemovedMsg msg ->
let _ = FlowRemoved.marshal msg out in
()
| SetConfig msg ->
let _ = SwitchConfig.marshal msg out in
()
Expand Down
41 changes: 27 additions & 14 deletions lib/OpenFlow0x01.mli
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ module Action : sig

end

(** The type of flow rule timeouts. See Section 5.3.3 of the OpenFlow 1.0
specification. *)
module Timeout : sig

type t = timeout

(** [to_string v] pretty-prints [v]. *)
val to_string : t -> string

val to_int : t -> int16
val of_int : int16 -> t
end

(** A flow modification data structure. See Section 5.3.3 of the OpenFlow 1.0
specification. *)
module FlowMod : sig
Expand All @@ -101,20 +114,6 @@ module FlowMod : sig

end

(** The type of flow rule timeouts. See Section 5.3.3 of the OpenFlow 1.0
specification. *)
module Timeout : sig

type t = timeout

(** [to_string v] pretty-prints [v]. *)
val to_string : t -> string

val to_int : t -> int16
val of_int : int16 -> t

end

type t = flowMod


Expand Down Expand Up @@ -147,6 +146,19 @@ module PacketIn : sig

end

(** Flow removed data structure. See section 5.4.3 of the OpenFlow 1.0 specification. *)
module FlowRemoved : sig

module Reason : sig

type t = flowRemovedReason

end

type t = flowRemoved

end

module PacketOut : sig

type t = packetOut
Expand Down Expand Up @@ -565,6 +577,7 @@ module Message : sig
| SwitchFeaturesReply of SwitchFeatures.t
| FlowModMsg of FlowMod.t
| PacketInMsg of PacketIn.t
| FlowRemovedMsg of FlowRemoved.t
| PortStatusMsg of PortStatus.t
| PacketOutMsg of PacketOut.t
| BarrierRequest
Expand Down
17 changes: 17 additions & 0 deletions lib/OpenFlow0x01_Core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ type packetOut =
; apply_actions : action list
}

type flowRemovedReason =
| IdleTimeout
| HardTimeout
| Delete

type flowRemoved =
{ pattern : pattern
; cookie : int64
; priority : int16
; reason : flowRemovedReason
; duration_sec : int32
; duration_nsec : int32
; idle_timeout : timeout
; packet_count : int64
; byte_count : int64
}

let add_flow prio pat actions =
{ command = AddFlow;
pattern = pat;
Expand Down
19 changes: 19 additions & 0 deletions lib/OpenFlow0x01_Core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ type packetIn =
; reason : packetInReason (** Reason packet is being sent. *)
}

type flowRemovedReason =
| IdleTimeout
| HardTimeout
| Delete

(** A flow-removed message. See Section 5.4.2 of the OpenFlow 1.0
specification. *)
type flowRemoved =
{ pattern : pattern;
cookie : int64;
priority : int16;
reason : flowRemovedReason;
duration_sec : int32;
duration_nsec : int32;
idle_timeout : timeout;
packet_count : int64;
byte_count : int64
}

(** A send-packet message. See Section 5.3.6 of the OpenFlow 1.0
specification. *)
type packetOut =
Expand Down
Loading