From 6dc3cfd500d7948d4c0edcc2ce226d1e3c7490db Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Wed, 24 Jul 2019 02:05:39 +0900 Subject: [PATCH] openflow/learn_flow_spec: Fix to use src field if dst field is omitted, as dst field. --- .../lib/nx_learning_switch.ex | 48 ++++++++++--------- lib/openflow/actions/nx_flow_spec_match.ex | 2 +- test/lib/openflow/ofp_action_test.exs | 17 ++++--- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/examples/nx_learning_switch/lib/nx_learning_switch.ex b/examples/nx_learning_switch/lib/nx_learning_switch.ex index 2565ec2..9c3d9b9 100644 --- a/examples/nx_learning_switch/lib/nx_learning_switch.ex +++ b/examples/nx_learning_switch/lib/nx_learning_switch.ex @@ -56,29 +56,7 @@ defmodule NxLearningSwitch do send_flow_mod_add( datapath_id, table_id: 0, - instructions: ApplyActions.new([ - NxLearn.new( - table_id: 1, - priority: 2, - hard_timeout: 10, - flow_specs: [ - NxFlowSpecMatch.new( - src: :eth_src, - dst: :eth_dst - ), - NxFlowSpecMatch.new( - src: :vlan_vid, - dst: :vlan_vid, - offset: 0, - n_bits: 12 - ), - NxFlowSpecOutput.new( - src: :nx_in_port - ) - ] - ), - NxResubmitTable.new(1) - ]) + instructions: l2_learning_instr() ) end @@ -90,4 +68,28 @@ defmodule NxLearningSwitch do instructions: ApplyActions.new(Output.new(:flood)) ) end + + defp l2_learning_instr do + ApplyActions.new([ + l2_learning_action(), + NxResubmitTable.new(1) + ]) + end + + defp l2_learning_action do + NxLearn.new( + table_id: 1, + priority: 2, + hard_timeout: 10, + flow_specs: l2_learning_flow_specs() + ) + end + + defp l2_learning_flow_specs do + [ + NxFlowSpecMatch.new(src: :eth_src, dst: :eth_dst), + NxFlowSpecMatch.new(src: :vlan_vid, n_bits: 12), + NxFlowSpecOutput.new(src: :nx_in_port) + ] + end end diff --git a/lib/openflow/actions/nx_flow_spec_match.ex b/lib/openflow/actions/nx_flow_spec_match.ex index d9b76b3..546aeb8 100644 --- a/lib/openflow/actions/nx_flow_spec_match.ex +++ b/lib/openflow/actions/nx_flow_spec_match.ex @@ -29,8 +29,8 @@ defmodule Openflow.Action.NxFlowSpecMatch do dst_offset: non_neg_integer() ) :: t() def new(options \\ []) do - dst = options[:dst] || raise ":dst must be specified" src = options[:src] || raise ":src must be specified" + dst = options[:dst] || src n_bits = options[:n_bits] || Openflow.Match.n_bits_of(dst) %NxFlowSpecMatch{ diff --git a/test/lib/openflow/ofp_action_test.exs b/test/lib/openflow/ofp_action_test.exs index dc24c96..1edc5d8 100644 --- a/test/lib/openflow/ofp_action_test.exs +++ b/test/lib/openflow/ofp_action_test.exs @@ -741,7 +741,7 @@ defmodule OfpActionTest do fin_idle_timeout: 2, fin_hard_timeout: 4, flow_specs: [ - Openflow.Action.NxFlowSpecMatch.new(src: :nx_vlan_tci, dst: :nx_vlan_tci, n_bits: 12), + Openflow.Action.NxFlowSpecMatch.new(src: :nx_vlan_tci, n_bits: 12), Openflow.Action.NxFlowSpecMatch.new(src: :nx_eth_src, dst: :nx_eth_dst), Openflow.Action.NxFlowSpecOutput.new(src: :nx_in_port) ] @@ -773,7 +773,7 @@ defmodule OfpActionTest do result_dst: :reg0, result_dst_offset: 8, flow_specs: [ - Openflow.Action.NxFlowSpecMatch.new(src: :nx_vlan_tci, dst: :nx_vlan_tci, n_bits: 12), + Openflow.Action.NxFlowSpecMatch.new(src: :nx_vlan_tci, n_bits: 12), Openflow.Action.NxFlowSpecMatch.new(src: :nx_eth_src, dst: :nx_eth_dst), Openflow.Action.NxFlowSpecOutput.new(src: :nx_in_port) ] @@ -846,22 +846,21 @@ defmodule OfpActionTest do end test "with no option" do - assert_raise RuntimeError, ":dst must be specified", fn -> + assert_raise RuntimeError, ":src must be specified", fn -> Openflow.Action.NxFlowSpecMatch.new() end end - test "with no dst option" do - assert_raise RuntimeError, ":dst must be specified", fn -> - Openflow.Action.NxFlowSpecMatch.new(src: :in_port) - end - end - test "with no src option" do assert_raise RuntimeError, ":src must be specified", fn -> Openflow.Action.NxFlowSpecMatch.new(dst: :reg0) end end + + test "with no dst option" do + learn = Openflow.Action.NxFlowSpecMatch.new(src: :in_port) + assert learn.dst == :in_port + end end describe "Openflow.Action.NxFlowSpecLoad" do