From b32cfff395b1f42dcf6e8d8fefea141b092b98ba Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Wed, 1 May 2019 23:54:40 +0900 Subject: [PATCH] openflow: Fix aggregate request --- lib/openflow/flow_mod.ex | 2 +- lib/openflow/multipart/aggregate/request.ex | 113 +++++++++++------- test/{ => lib/openflow}/ofp_action_test.exs | 0 test/{ => lib/openflow}/ofp_barrier_test.exs | 0 test/{ => lib/openflow}/ofp_echo_test.exs | 0 test/{ => lib/openflow}/ofp_error_test.exs | 0 test/{ => lib/openflow}/ofp_features_test.exs | 0 test/{ => lib/openflow}/ofp_flow_mod_test.exs | 0 .../openflow}/ofp_flow_removed_test.exs | 0 .../{ => lib/openflow}/ofp_get_async_test.exs | 0 .../openflow}/ofp_get_config_test.exs | 0 .../{ => lib/openflow}/ofp_group_mod_test.exs | 0 test/{ => lib/openflow}/ofp_hello_test.exs | 0 .../openflow}/ofp_instruction_test.exs | 0 .../{ => lib/openflow}/ofp_meter_mod_test.exs | 0 .../openflow}/ofp_packet_in2_test.exs | 0 .../{ => lib/openflow}/ofp_packet_in_test.exs | 0 .../openflow}/ofp_packet_out_test.exs | 0 test/{ => lib/openflow}/ofp_port_mod_test.exs | 0 .../openflow}/ofp_port_status_test.exs | 0 .../openflow}/ofp_set_config_test.exs | 0 .../{ => lib/openflow}/ofp_table_mod_test.exs | 0 22 files changed, 71 insertions(+), 44 deletions(-) rename test/{ => lib/openflow}/ofp_action_test.exs (100%) rename test/{ => lib/openflow}/ofp_barrier_test.exs (100%) rename test/{ => lib/openflow}/ofp_echo_test.exs (100%) rename test/{ => lib/openflow}/ofp_error_test.exs (100%) rename test/{ => lib/openflow}/ofp_features_test.exs (100%) rename test/{ => lib/openflow}/ofp_flow_mod_test.exs (100%) rename test/{ => lib/openflow}/ofp_flow_removed_test.exs (100%) rename test/{ => lib/openflow}/ofp_get_async_test.exs (100%) rename test/{ => lib/openflow}/ofp_get_config_test.exs (100%) rename test/{ => lib/openflow}/ofp_group_mod_test.exs (100%) rename test/{ => lib/openflow}/ofp_hello_test.exs (100%) rename test/{ => lib/openflow}/ofp_instruction_test.exs (100%) rename test/{ => lib/openflow}/ofp_meter_mod_test.exs (100%) rename test/{ => lib/openflow}/ofp_packet_in2_test.exs (100%) rename test/{ => lib/openflow}/ofp_packet_in_test.exs (100%) rename test/{ => lib/openflow}/ofp_packet_out_test.exs (100%) rename test/{ => lib/openflow}/ofp_port_mod_test.exs (100%) rename test/{ => lib/openflow}/ofp_port_status_test.exs (100%) rename test/{ => lib/openflow}/ofp_set_config_test.exs (100%) rename test/{ => lib/openflow}/ofp_table_mod_test.exs (100%) diff --git a/lib/openflow/flow_mod.ex b/lib/openflow/flow_mod.ex index 2607a5f..7725b9a 100644 --- a/lib/openflow/flow_mod.ex +++ b/lib/openflow/flow_mod.ex @@ -40,7 +40,7 @@ defmodule Openflow.FlowMod do aux_id: 0..0xF | nil, cookie: 0..0xFFFFFFFFFFFFFFFF, cookie_mask: 0..0xFFFFFFFFFFFFFFFF, - table_id: 0..0xF, + table_id: 0..0xFF, command: command(), idle_timeout: 0..0xFFFF, hard_timeout: 0..0xFFFF, diff --git a/lib/openflow/multipart/aggregate/request.ex b/lib/openflow/multipart/aggregate/request.ex index 0335cb9..c211684 100644 --- a/lib/openflow/multipart/aggregate/request.ex +++ b/lib/openflow/multipart/aggregate/request.ex @@ -4,58 +4,85 @@ defmodule Openflow.Multipart.Aggregate.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 - 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, []) + @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(), + out_group: Openflow.GroupMod.id(), + cookie: 0..0xFFFFFFFFFFFFFFFF, + cookie_mask: 0..0xFFFFFFFFFFFFFFFF, + match: %Openflow.Match{fields: [map()], type: :oxm} + ) :: t() + def new(options \\ []) do + %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( - <> - ) 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 +93,16 @@ defmodule Openflow.Multipart.Aggregate.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 = - <> - - header_bin = Openflow.Multipart.Request.header(msg) - <> + << + 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 diff --git a/test/ofp_action_test.exs b/test/lib/openflow/ofp_action_test.exs similarity index 100% rename from test/ofp_action_test.exs rename to test/lib/openflow/ofp_action_test.exs diff --git a/test/ofp_barrier_test.exs b/test/lib/openflow/ofp_barrier_test.exs similarity index 100% rename from test/ofp_barrier_test.exs rename to test/lib/openflow/ofp_barrier_test.exs diff --git a/test/ofp_echo_test.exs b/test/lib/openflow/ofp_echo_test.exs similarity index 100% rename from test/ofp_echo_test.exs rename to test/lib/openflow/ofp_echo_test.exs diff --git a/test/ofp_error_test.exs b/test/lib/openflow/ofp_error_test.exs similarity index 100% rename from test/ofp_error_test.exs rename to test/lib/openflow/ofp_error_test.exs diff --git a/test/ofp_features_test.exs b/test/lib/openflow/ofp_features_test.exs similarity index 100% rename from test/ofp_features_test.exs rename to test/lib/openflow/ofp_features_test.exs diff --git a/test/ofp_flow_mod_test.exs b/test/lib/openflow/ofp_flow_mod_test.exs similarity index 100% rename from test/ofp_flow_mod_test.exs rename to test/lib/openflow/ofp_flow_mod_test.exs diff --git a/test/ofp_flow_removed_test.exs b/test/lib/openflow/ofp_flow_removed_test.exs similarity index 100% rename from test/ofp_flow_removed_test.exs rename to test/lib/openflow/ofp_flow_removed_test.exs diff --git a/test/ofp_get_async_test.exs b/test/lib/openflow/ofp_get_async_test.exs similarity index 100% rename from test/ofp_get_async_test.exs rename to test/lib/openflow/ofp_get_async_test.exs diff --git a/test/ofp_get_config_test.exs b/test/lib/openflow/ofp_get_config_test.exs similarity index 100% rename from test/ofp_get_config_test.exs rename to test/lib/openflow/ofp_get_config_test.exs diff --git a/test/ofp_group_mod_test.exs b/test/lib/openflow/ofp_group_mod_test.exs similarity index 100% rename from test/ofp_group_mod_test.exs rename to test/lib/openflow/ofp_group_mod_test.exs diff --git a/test/ofp_hello_test.exs b/test/lib/openflow/ofp_hello_test.exs similarity index 100% rename from test/ofp_hello_test.exs rename to test/lib/openflow/ofp_hello_test.exs diff --git a/test/ofp_instruction_test.exs b/test/lib/openflow/ofp_instruction_test.exs similarity index 100% rename from test/ofp_instruction_test.exs rename to test/lib/openflow/ofp_instruction_test.exs diff --git a/test/ofp_meter_mod_test.exs b/test/lib/openflow/ofp_meter_mod_test.exs similarity index 100% rename from test/ofp_meter_mod_test.exs rename to test/lib/openflow/ofp_meter_mod_test.exs diff --git a/test/ofp_packet_in2_test.exs b/test/lib/openflow/ofp_packet_in2_test.exs similarity index 100% rename from test/ofp_packet_in2_test.exs rename to test/lib/openflow/ofp_packet_in2_test.exs diff --git a/test/ofp_packet_in_test.exs b/test/lib/openflow/ofp_packet_in_test.exs similarity index 100% rename from test/ofp_packet_in_test.exs rename to test/lib/openflow/ofp_packet_in_test.exs diff --git a/test/ofp_packet_out_test.exs b/test/lib/openflow/ofp_packet_out_test.exs similarity index 100% rename from test/ofp_packet_out_test.exs rename to test/lib/openflow/ofp_packet_out_test.exs diff --git a/test/ofp_port_mod_test.exs b/test/lib/openflow/ofp_port_mod_test.exs similarity index 100% rename from test/ofp_port_mod_test.exs rename to test/lib/openflow/ofp_port_mod_test.exs diff --git a/test/ofp_port_status_test.exs b/test/lib/openflow/ofp_port_status_test.exs similarity index 100% rename from test/ofp_port_status_test.exs rename to test/lib/openflow/ofp_port_status_test.exs diff --git a/test/ofp_set_config_test.exs b/test/lib/openflow/ofp_set_config_test.exs similarity index 100% rename from test/ofp_set_config_test.exs rename to test/lib/openflow/ofp_set_config_test.exs diff --git a/test/ofp_table_mod_test.exs b/test/lib/openflow/ofp_table_mod_test.exs similarity index 100% rename from test/ofp_table_mod_test.exs rename to test/lib/openflow/ofp_table_mod_test.exs