Openflow parser

This commit is contained in:
Eishun Kondoh 2017-11-13 22:52:53 +09:00
parent 70b0d8919e
commit fc02a678de
338 changed files with 9081 additions and 0 deletions

View file

@ -0,0 +1 @@
Replace regexp Queue with: Group

View file

@ -0,0 +1 @@
shun159@shun159.8967:1509553730

View file

@ -0,0 +1,69 @@
defmodule Openflow.Multipart.Group.Reply do
defstruct(
version: 4,
xid: 0,
datapath_id: nil, # virtual field
flags: [],
groups: []
)
alias __MODULE__
def ofp_type, do: 18
def new(groups \\ []) do
%Reply{groups: groups}
end
def read(<<groups_bin::bytes>>) do
groups = Openflow.Multipart.Group.read(groups_bin)
%Reply{groups: groups}
end
end
defmodule Openflow.Multipart.Group do
defstruct(
group_id: 0,
ref_count: 0,
packet_count: 0,
byte_count: 0,
duration_sec: 0,
duration_nsec: 0,
bucket_stats: []
)
@ofp_group_stats_size 40
alias __MODULE__
def read(binary) do
do_read([], binary)
end
# private functions
defp do_read(acc, ""), do: Enum.reverse(acc)
defp do_read(acc, <<length::16, _binary::bytes>> = binary) do
<<group_bin::size(length)-bytes, rest::bytes>> = binary
do_read([codec(group_bin)|acc], rest)
end
defp codec(<<length::16, _::size(2)-unit(8),
group_id::32, ref_count::32,
_::size(4)-unit(8), packet_count::64,
byte_count::64, duration_sec::32,
duration_nsec::32, tail::bytes>>) do
bucket_stats_size = length - @ofp_group_stats_size
<<bucket_stats_bin::size(bucket_stats_size)-bytes, _rest::bytes>> = tail
bucket_stats = for <<packet_count::64, byte_count::64 <- bucket_stats_bin>> do
%{packet_count: packet_count, byte_count: byte_count}
end
%Group{group_id: group_id,
ref_count: ref_count,
packet_count: packet_count,
byte_count: byte_count,
duration_sec: duration_sec,
duration_nsec: duration_nsec,
bucket_stats: bucket_stats}
end
end

View file

@ -0,0 +1,29 @@
defmodule Openflow.Multipart.Group.Request do
defstruct(
version: 4,
xid: 0,
datapath_id: nil, # virtual field
flags: [],
group_id: :all
)
alias __MODULE__
def ofp_type, do: 18
def new(group_id \\ :all) do
%Request{group_id: group_id}
end
def read(<<group_id_int::32, _::size(4)-unit(8)>>) do
group_id = Openflow.Utils.get_enum(group_id_int, :group_id)
%Request{group_id: group_id}
end
def to_binary(%Request{group_id: group_id} = msg) do
group_id_int = Openflow.Utils.get_enum(group_id, :group_id)
body_bin = <<group_id_int::32, 0::size(4)-unit(8)>>
header_bin = Openflow.Multipart.Request.header(msg)
<<header_bin::bytes, body_bin::bytes>>
end
end