quality: Add test cases for flow_stats messages

This commit is contained in:
Eishun Kondoh 2019-05-05 20:45:35 +09:00
parent 7055dfd93b
commit 2557778042
4 changed files with 256 additions and 61 deletions

View file

@ -33,9 +33,6 @@ defmodule Openflow.Multipart.Aggregate.Request do
def ofp_type, do: 18
@spec new(
version: 4,
datapath_id: String.t(),
aux_id: 0..0xFF | nil,
xid: 0..0xFFFFFFFF,
table_id: 0..0xFF | :all | :max,
out_port: Openflow.Port.no(),

View file

@ -11,12 +11,19 @@ defmodule Openflow.Multipart.Flow.Reply do
alias __MODULE__
def ofp_type, do: 18
@type t :: %Reply{
version: 4,
datapath_id: String.t() | nil,
aux_id: 0..0xFF | nil,
xid: 0..0xFFFFFFFF,
flags: [:more],
flows: [Openflow.Multipart.FlowStats.t()]
}
def new(flows \\ []) do
%Reply{flows: flows}
end
@spec ofp_type() :: 19
def ofp_type, do: 19
@spec read(<<_::16, _::_*384>>) :: t()
def read(<<flows_bin::bytes>>) do
flows = Openflow.Multipart.FlowStats.read(flows_bin)
%Reply{flows: flows}
@ -54,27 +61,53 @@ defmodule Openflow.Multipart.FlowStats do
alias __MODULE__
def read(binary) do
do_read([], binary)
end
@type t :: %FlowStats{
table_id: 0..0xFF,
duration_sec: 0..0xFFFFFFFF,
duration_nsec: 0..0xFFFFFFFF,
priority: 0..0xFFFF,
idle_timeout: 0..0xFFFF,
hard_timeout: 0..0xFFFF,
flags: [:send_flow_rem | :delete_learned | :write_result],
cookie: 0..0xFFFFFFFFFFFFFFFF,
packet_count: 0..0xFFFFFFFFFFFFFFFF,
byte_count: 0..0xFFFFFFFFFFFFFFFF,
match: Openflow.Match.new(),
instructions: [Openflow.Instruction.instruction()]
}
@spec read(<<_::_*8>>) :: [t()]
def read(binary), do: do_read([], binary)
# private functions
defp do_read(acc, ""), do: Enum.reverse(acc)
@spec do_read(acc :: [t()], binary()) :: [t()]
defp do_read(acc, ""), do: acc
@spec do_read(acc :: [t()], <<_::16, _::_*8>>) :: [t()]
defp do_read(acc, <<length::16, _tail::bytes>> = binary) do
<<flow_stats_bin::size(length)-bytes, rest::bytes>> = binary
do_read([codec(flow_stats_bin) | acc], rest)
do_read(acc ++ [codec(flow_stats_bin)], rest)
end
defp codec(
<<_length::16, table_id_int::8, 0::8, duration_sec::32, duration_nsec::32, priority::16,
idle::16, hard::16, flags_int::16, _::size(4)-unit(8), cookie::64, packet_count::64,
byte_count::64, tail::bytes>>
) do
{match, instructions_bin} = Openflow.Match.read(tail)
table_id = Openflow.Utils.get_enum(table_id_int, :table_id)
defp codec(<<
_length::16,
table_id::8,
0::8,
duration_sec::32,
duration_nsec::32,
priority::16,
idle::16,
hard::16,
flags_int::16,
_::size(4)-unit(8),
cookie::64,
packet_count::64,
byte_count::64,
tail::bytes
>>) do
flags = Openflow.Enums.int_to_flags(flags_int, :flow_mod_flags)
{match, instructions_bin} = Openflow.Match.read(tail)
instructions = Openflow.Instruction.read(instructions_bin)
%FlowStats{

View file

@ -4,58 +4,82 @@ defmodule Openflow.Multipart.Flow.Request do
xid: 0,
# virtual field
datapath_id: nil,
aux_id: nil,
flags: [],
table_id: :all,
out_port: :any,
out_group: :any,
cookie: 0,
cookie_mask: 0,
match: []
match: Openflow.Match.new()
)
alias __MODULE__
@type t :: %Request{
version: 4,
datapath_id: String.t(),
aux_id: 0..0xFF | nil,
xid: 0..0xFFFFFFFF,
table_id: 0..0xFF | :all | :max,
out_port: Openflow.Port.no(),
out_group: Openflow.GroupMod.id(),
cookie: 0..0xFFFFFFFFFFFFFFFF,
cookie_mask: 0..0xFFFFFFFFFFFFFFFF,
match: %Openflow.Match{fields: [map()], type: :oxm}
}
@spec ofp_type() :: 18
def ofp_type, do: 18
@spec new(
xid: 0..0xFFFFFFFF,
table_id: 0..0xFF | :all | :max,
out_port: Openflow.Port.no(),
out_group: Openflow.GroupMod.id(),
cookie: 0..0xFFFFFFFFFFFFFFFF,
cookie_mask: 0..0xFFFFFFFFFFFFFFFF,
match: %Openflow.Match{fields: [map()], type: :oxm}
) :: t()
def new(options \\ []) do
xid = Keyword.get(options, :xid, 0)
table_id = Keyword.get(options, :table_id, :all)
out_port = Keyword.get(options, :out_port, :any)
out_group = Keyword.get(options, :out_group, :any)
cookie = Keyword.get(options, :cookie, 0)
cookie_mask = Keyword.get(options, :cookie, 0)
match = Keyword.get(options, :match, Openflow.Match.new())
%Request{
xid: options[:xid] || 0,
table_id: options[:table_id] || :all,
out_port: options[:out_port] || :any,
out_group: options[:out_group] || :any,
cookie: options[:cookie] || 0,
cookie_mask: options[:cookie_mask] || 0,
match: options[:match] || Openflow.Match.new()
}
end
@spec read(<<_::256, _::_*8>>) :: t()
def read(<<
table_id_int::8,
_::size(3)-unit(8),
out_port_int::32,
out_group_int::32,
_::size(4)-unit(8),
cookie::64,
cookie_mask::64,
match_bin::bytes
>>) do
match =
match_bin
|> Openflow.Match.read()
|> Kernel.elem(0)
%Request{
xid: xid,
table_id: table_id,
out_port: out_port,
out_group: out_group,
cookie: cookie,
cookie_mask: cookie_mask,
match: match
}
end
def read(
<<table_id_int::8, _::size(3)-unit(8), out_port_int::32, out_group_int::32,
_::size(4)-unit(8), cookie::64, cookie_mask::64, match_bin::bytes>>
) do
table_id = Openflow.Utils.get_enum(table_id_int, :table_id)
out_port = Openflow.Utils.get_enum(out_port_int, :openflow13_port_no)
out_group = Openflow.Utils.get_enum(out_group_int, :group_id)
{match, _rest} = Openflow.Match.read(match_bin)
%Request{
table_id: table_id,
out_port: out_port,
out_group: out_group,
table_id: Openflow.Utils.get_enum(table_id_int, :table_id),
out_port: Openflow.Utils.get_enum(out_port_int, :openflow13_port_no),
out_group: Openflow.Utils.get_enum(out_group_int, :group_id),
cookie: cookie,
cookie_mask: cookie_mask,
match: match
}
end
@spec to_binary(t()) :: <<_::256, _::_*8>>
def to_binary(
%Request{
table_id: table_id,
@ -66,16 +90,16 @@ defmodule Openflow.Multipart.Flow.Request do
match: match
} = msg
) do
table_id_int = Openflow.Utils.get_enum(table_id, :table_id)
out_port_int = Openflow.Utils.get_enum(out_port, :openflow13_port_no)
out_group_int = Openflow.Utils.get_enum(out_group, :group_id)
match_bin = Openflow.Match.to_binary(match)
body_bin =
<<table_id_int::8, 0::size(3)-unit(8), out_port_int::32, out_group_int::32,
0::size(4)-unit(8), cookie::64, cookie_mask::64, match_bin::bytes>>
header_bin = Openflow.Multipart.Request.header(msg)
<<header_bin::bytes, body_bin::bytes>>
<<
Openflow.Multipart.Request.header(msg)::bytes,
Openflow.Utils.get_enum(table_id, :table_id)::8,
0::size(3)-unit(8),
Openflow.Utils.get_enum(out_port, :openflow13_port_no)::32,
Openflow.Utils.get_enum(out_group, :group_id)::32,
0::size(4)-unit(8),
cookie::64,
cookie_mask::64,
Openflow.Match.to_binary(match)::bytes
>>
end
end