From fadfcd76bdf89a04f2b743dc154c0aac898b30e0 Mon Sep 17 00:00:00 2001 From: Eishun Kondoh Date: Tue, 30 Jan 2018 22:38:54 +0900 Subject: [PATCH] Fix def function order of enum macro, and more human readable disconnected handler --- lib/openflow/enums.ex | 9 +++++- lib/openflow/features/reply.ex | 2 +- lib/openflow/meter_band/experimenter.ex | 2 +- lib/tres/example_handler.ex | 37 ++++++------------------- lib/tres/secure_channel.ex | 28 ++++++++++++++----- mix.exs | 2 -- 6 files changed, 39 insertions(+), 41 deletions(-) diff --git a/lib/openflow/enums.ex b/lib/openflow/enums.ex index 4d43189..b29f59d 100644 --- a/lib/openflow/enums.ex +++ b/lib/openflow/enums.ex @@ -1071,6 +1071,7 @@ defmodule Openflow.Enums do for {enum_name, enum_def} <- @enums do enum_name = to_string(enum_name) + to_int_fn_name = String.to_atom(enum_name <> "_to_int") to_atom_fn_name = String.to_atom(enum_name <> "_to_atom") @@ -1091,6 +1092,7 @@ defmodule Openflow.Enums do end end end + def to_int(_int, unquote(String.to_atom(enum_name))) do throw(:bad_enum) end @@ -1101,9 +1103,14 @@ defmodule Openflow.Enums do for {key, value} <- enum_def do def unquote(to_int_fn_name)(unquote(key)), do: unquote(value) + end + + def unquote(to_int_fn_name)(_), do: throw(:bad_enum) + + for {key, value} <- enum_def do def unquote(to_atom_fn_name)(unquote(value)), do: unquote(key) end - def unquote(to_int_fn_name)(_), do: throw(:bad_enum) + def unquote(to_atom_fn_name)(_), do: throw(:bad_enum) def int_to_flags(int, unquote(String.to_atom(enum_name))) do diff --git a/lib/openflow/features/reply.ex b/lib/openflow/features/reply.ex index 1bac1a5..b22b384 100644 --- a/lib/openflow/features/reply.ex +++ b/lib/openflow/features/reply.ex @@ -6,7 +6,7 @@ defmodule Openflow.Features.Reply do n_buffers: 0, n_tables: 0, aux_id: 0, - capabilities: [], + capabilities: [] ) alias __MODULE__ diff --git a/lib/openflow/meter_band/experimenter.ex b/lib/openflow/meter_band/experimenter.ex index fa70d4d..8a73171 100644 --- a/lib/openflow/meter_band/experimenter.ex +++ b/lib/openflow/meter_band/experimenter.ex @@ -2,7 +2,7 @@ defmodule Openflow.MeterBand.Experimenter do defstruct( rate: 0, burst_size: 0, - experimenter: 0, + experimenter: 0 ) alias __MODULE__ diff --git a/lib/tres/example_handler.ex b/lib/tres/example_handler.ex index deaf722..ded3129 100644 --- a/lib/tres/example_handler.ex +++ b/lib/tres/example_handler.ex @@ -20,20 +20,13 @@ defmodule Tres.ExampleHandler do info("[#{__MODULE__}] Switch Ready: " <> "datapath_id: #{datapath_id} " <> "aux_id: #{aux_id} " - <> "in #{inspect(self())}") - _ = send_flows_for_test(datapath_id) - _ = send_flow_stats_request(datapath_id) + <> "on #{inspect(self())}") _ = send_desc_stats_request(datapath_id) _ = send_port_desc_stats_request(datapath_id) - conn_ref = SwitchRegistry.monitor(datapath_id) - state = %State{datapath_id: datapath_id, aux_id: aux_id, conn_ref: conn_ref} + state = %State{datapath_id: datapath_id, aux_id: aux_id} {:ok, state} end - def handle_info(%Flow.Reply{datapath_id: datapath_id} = desc, state) do - handle_flow_stats_reply(desc, datapath_id) - {:noreply, state} - end def handle_info(%PortDesc.Reply{datapath_id: datapath_id} = desc, state) do handle_port_desc_stats_reply(desc, datapath_id) {:noreply, state} @@ -42,13 +35,14 @@ defmodule Tres.ExampleHandler do handle_desc_stats_reply(desc, datapath_id) {:noreply, state} end - - # To prevent process leakage, following section is required. - def handle_info({:'DOWN', ref, :process, _pid, _reason}, %State{conn_ref: ref} = state) do - :ok = warn("[#{__MODULE__}] Switch Disconnected: datapath_id: #{state.datapath_id}") + def handle_info({:switch_disconnected, reason}, state) do + :ok = warn("[#{__MODULE__}] Switch Disconnected: datapath_id: #{state.datapath_id} by #{reason}") {:stop, :normal, state} end - + def handle_info({:switch_hang, _datapath_id}, state) do + :ok = warn("[#{__MODULE__}] Switch possible hang: datapath_id: #{state.datapath_id}") + {:noreply, state} + end # `Catch all` function is required. def handle_info(info, state) do :ok = warn("[#{__MODULE__}] unhandled message #{inspect(info)}: #{state.datapath_id}") @@ -57,17 +51,6 @@ defmodule Tres.ExampleHandler do # private functions - defp send_flows_for_test(datapath_id) do - for count <- Range.new(1, 1024) do - send_flow_mod_add(datapath_id, match: Match.new(metadata: count)) - end - end - - defp send_flow_stats_request(datapath_id) do - Flow.Request.new - |> send_message(datapath_id) - end - defp send_desc_stats_request(datapath_id) do Desc.Request.new |> send_message(datapath_id) @@ -78,10 +61,6 @@ defmodule Tres.ExampleHandler do |> send_message(datapath_id) end - defp handle_flow_stats_reply(desc, datapath_id) do - info("[#{__MODULE__}] Switch #{length(desc.flows)} installed on #{datapath_id}") - end - defp handle_desc_stats_reply(desc, datapath_id) do info( "[#{__MODULE__}] Switch Desc: " diff --git a/lib/tres/secure_channel.ex b/lib/tres/secure_channel.ex index 1e3153a..78c3350 100644 --- a/lib/tres/secure_channel.ex +++ b/lib/tres/secure_channel.ex @@ -433,32 +433,46 @@ defmodule Tres.SecureChannel do warn("[#{__MODULE__}] connection terminated: Features handshake timed out") {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection(:handler_error, state_data) do + defp close_connection(:handler_error = disconnected_reason, state_data) do warn("[#{__MODULE__}] connection terminated: Got handler error") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection(:ping_failed, state_data) do + defp close_connection(:ping_failed = disconnected_reason, state_data) do warn("[#{__MODULE__}] connection terminated: Exceeded to max_ping_fail_count") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection({:main_closed, reason}, state_data) do + defp close_connection({:main_closed = disconnected_reason, reason}, state_data) do warn("[#{__MODULE__}] connection terminated: Main connection down by #{reason}") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection({:handler_down, reason}, state_data) do + defp close_connection({:handler_down = disconnected_reason, reason}, state_data) do warn("[#{__MODULE__}] connection terminated: Handler process down by #{reason}") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection({:trap_detected, reason}, state_data) do + defp close_connection({:trap_detected = disconnected_reason, reason}, state_data) do warn("[#{__MODULE__}] connection terminated: Trapped by #{reason}") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection(:tcp_closed, state_data) do + defp close_connection(:tcp_closed = disconnected_reason, state_data) do warn("[#{__MODULE__}] connection terminated: TCP Closed by peer") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end - defp close_connection({:tcp_error, reason}, state_data) do + defp close_connection({:tcp_error, reason} = disconnected_reason, state_data) do warn("[#{__MODULE__}] connection terminated: TCP Error occured: #{reason}") + %State{handler_pid: handler_pid} = state_data + send(handler_pid, {:switch_disconnected, disconnected_reason}) {:stop, :normal, %{state_data|socket: nil}} end end diff --git a/mix.exs b/mix.exs index 069e374..ffbf3d9 100644 --- a/mix.exs +++ b/mix.exs @@ -21,8 +21,6 @@ defmodule Tres.Mixfile do defp deps do [{:ranch, "~> 1.4.0"}, {:eovsdb, github: "shun159/eovsdb", branch: "master"}, - {:binpp, github: "jtendo/binpp", branch: "master"}, - {:pkt, github: "msantos/pkt", ref: "3afb196", only: :test, override: true}, {:epcap, github: "msantos/epcap", branch: "master", only: :test}] end end