Add support NXT_RESUME (#9)

This commit is contained in:
Eishun Kondoh 2018-08-29 07:17:51 +09:00 committed by GitHub
parent 4bd8ba994f
commit a3ec836e42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 730 additions and 71 deletions

View file

@ -14,17 +14,40 @@ defmodule Openflow.NxPacketIn2 do
metadata: nil,
userdata: nil,
# continuation properties:
continuation_bridge: "",
continuation_stack: [],
continuation_conntracked: false,
continuation_bridge: nil,
continuation_stack: nil,
continuation_conntracked: nil,
continuation_mirrors: nil,
continuation_table_id: nil,
continuation_cookie: nil,
continuation_actions: [],
continuation_actions: nil,
continuation_action_set: nil
)
alias __MODULE__
@encode_keys ~w(
packet
full_len
buffer_id
table_id
cookie
reason
metadata
userdata
)a
@continuation_keys ~w(
continuation_bridge
continuation_stack
continuation_conntracked
continuation_table_id
continuation_cookie
continuation_actions
continuation_mirrors
continuation_action_set
)a
@experimenter 0x00002320
@nx_type 30
@ -51,6 +74,12 @@ defmodule Openflow.NxPacketIn2 do
def ofp_type, do: 4
def to_binary(%NxPacketIn2{} = pin) do
props_bin = encode_props("", pin, @encode_keys)
continuations_bin = encode_continuations("", pin, @continuation_keys)
<<@experimenter::32, @nx_type::32, props_bin::bytes, continuations_bin::bytes>>
end
def read(<<@experimenter::32, @nx_type::32, props_bin::bytes>>) do
%NxPacketIn2{}
|> decode_props(props_bin)
@ -58,6 +87,189 @@ defmodule Openflow.NxPacketIn2 do
## private functions
defp encode_props(acc, _pin, []), do: acc
defp encode_props(acc, %NxPacketIn2{packet: packet} = pin, [:packet | rest])
when not is_nil(packet) and is_binary(packet) do
length = @prop_header_length + byte_size(packet)
pad_length = Openflow.Utils.pad_length(length, 8)
binary = <<@packet::16, length::16, packet::bytes, 0::size(pad_length)-unit(8)>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, %NxPacketIn2{full_len: full_len} = pin, [:full_len | rest])
when not is_nil(full_len) and is_integer(full_len) do
length = @prop_header_length + 4
binary = <<@full_len::16, length::16, full_len::32>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, %NxPacketIn2{buffer_id: buffer_id} = pin, [:buffer_id | rest])
when not is_nil(buffer_id) and is_integer(buffer_id) do
length = @prop_header_length + 4
binary = <<@buffer_id::16, length::16, buffer_id::32>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, %NxPacketIn2{table_id: table_id} = pin, [:table_id | rest])
when not is_nil(table_id) and is_integer(table_id) do
length = @prop_header_length + 4
binary = <<@table_id::16, length::16, table_id::8, 0::24>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, %NxPacketIn2{cookie: cookie} = pin, [:cookie | rest])
when not is_nil(cookie) and is_integer(cookie) do
length = @prop_header_length + 4
binary = <<@cookie::16, length::16, 0::32, cookie::64>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, %NxPacketIn2{metadata: metadata} = pin, [:metadata | rest])
when not is_nil(metadata) and is_list(metadata) do
pad_match_bin =
metadata
|> Openflow.Match.new()
|> Openflow.Match.to_binary()
<<1::16, match_len_with_header::16, padded_match_bin::bytes>> = pad_match_bin
match_len = match_len_with_header - 4
<<match_bin::size(match_len)-bytes, _pad::bytes>> = padded_match_bin
length = @prop_header_length + match_len
pad_length = Openflow.Utils.pad_length(length, 8)
binary = <<@metadata::16, length::16, match_bin::bytes, 0::size(pad_length)-unit(8)>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, %NxPacketIn2{userdata: userdata} = pin, [:userdata | rest])
when not is_nil(userdata) and is_binary(userdata) do
length = @prop_header_length + byte_size(userdata)
pad_length = Openflow.Utils.pad_length(length, 8)
binary = <<@userdata::16, length::16, userdata::bytes, 0::size(pad_length)-unit(8)>>
encode_props(<<acc::bytes, binary::bytes>>, pin, rest)
end
defp encode_props(acc, pin, [_ | rest]) do
encode_props(acc, pin, rest)
end
defp encode_continuations(acc, _pin, []) do
if byte_size(acc) > 0 do
length = @prop_header_length + byte_size(acc)
pad_length = Openflow.Utils.pad_length(length, 8)
<<@continuation::16, acc::bytes, 0::size(pad_length)-unit(8)>>
else
<<>>
end
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_bridge: br} = pin,
[:continuation_bridge | rest]
)
when not is_nil(br) do
length = @prop_header_length + byte_size(br)
pad_length = Openflow.Utils.pad_length(length, 8)
bridge_bin = <<br::bytes, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_bridge::16, length::16, bridge_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_stack: stack} = pin,
[:continuation_stack | rest]
)
when not is_nil(stack) do
length = @prop_header_length + byte_size(stack)
pad_length = Openflow.Utils.pad_length(length, 8)
stack_bin = <<stack::bytes, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_stack::16, length::16, stack_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_mirrors: mirrors} = pin,
[:continuation_mirrors | rest]
)
when not is_nil(mirrors) do
length = @prop_header_length + byte_size(mirrors)
pad_length = Openflow.Utils.pad_length(length, 8)
mirrors_bin = <<mirrors::bytes, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_mirrors::16, length::16, mirrors_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_conntracked: true} = pin,
[:continuation_conntracked | rest]
) do
length = @prop_header_length + 1
pad_length = Openflow.Utils.pad_length(length, 8)
conntracked_bin = <<1::size(1)-unit(8), 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_conntracked::16, length::16, conntracked_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_table_id: table_id} = pin,
[:continuation_table_id | rest]
)
when not is_nil(table_id) do
length = @prop_header_length + 1
pad_length = Openflow.Utils.pad_length(length, 8)
table_id_bin = <<table_id::8, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_table_id::16, length::16, table_id_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_cookie: cookie} = pin,
[:continuation_cookie | rest]
)
when not is_nil(cookie) do
length = @prop_header_length + 8
pad_length = Openflow.Utils.pad_length(length, 8)
cookie_bin = <<cookie::64, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_cookie::16, length::16, cookie_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_actions: actions} = pin,
[:continuation_actions | rest]
) do
actions_bin = Openflow.Action.to_binary(actions)
length = @prop_header_length + byte_size(actions_bin)
pad_length = Openflow.Utils.pad_length(length, 8)
actions_bin = <<actions_bin::bytes, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_actions::16, length::16, actions_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(
acc,
%NxPacketIn2{continuation_action_set: action_set} = pin,
[:continuation_action_set | rest]
)
when not is_nil(action_set) do
length = @prop_header_length + byte_size(action_set)
pad_length = Openflow.Utils.pad_length(length, 8)
action_set_bin = <<action_set::bytes, 0::size(pad_length)-unit(8), acc::bytes>>
binary = <<@nxcpt_action_set::16, length::16, action_set_bin::bytes, acc::bytes>>
encode_continuations(binary, pin, rest)
end
defp encode_continuations(acc, pin, [_ | rest]) do
encode_continuations(acc, pin, rest)
end
defp decode_props(pktin, ""), do: pktin
defp decode_props(pktin, <<@packet::16, length::16, tail::bytes>>) do