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,63 @@
defmodule Openflow.Multipart.Meter.Reply do
defstruct(
version: 4,
xid: 0,
datapath_id: nil, # virtual field
flags: [],
meters: []
)
alias __MODULE__
def ofp_type, do: 18
def read(<<meters_bin::bytes>>) do
meters = Openflow.Multipart.Meter.read(meters_bin)
%Reply{meters: meters}
end
end
defmodule Openflow.Multipart.Meter do
defstruct(
meter_id: 0,
flow_count: 0,
packet_in_count: 0,
byte_in_count: 0,
duration_sec: 0,
duration_nsec: 0,
band_stats: []
)
@ofp_meter_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, <<_::32, length::16, _binary::bytes>> = binary) do
<<meter_bin::size(length)-bytes, rest::bytes>> = binary
do_read([codec(meter_bin)|acc], rest)
end
defp codec(<<meter_id::32, length::16, _::size(6)-unit(8),
flow_count::32, packet_in_count::64, byte_in_count::64,
duration_sec::32, duration_nsec::32, tail::bytes>>) do
band_stats_size = length - @ofp_meter_stats_size
<<band_stats_bin::size(band_stats_size)-bytes, _rest::bytes>> = tail
band_stats = for <<packet_band_count::64, byte_band_count::64 <- band_stats_bin>> do
%{packet_band_count: packet_band_count,byte_band_count: byte_band_count}
end
%Meter{meter_id: meter_id,
flow_count: flow_count,
packet_in_count: packet_in_count,
byte_in_count: byte_in_count,
duration_sec: duration_sec,
duration_nsec: duration_nsec,
band_stats: band_stats}
end
end

View file

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