diff --git a/bin/enum_gen b/bin/enum_gen index d9aa60b..55d683a 100755 Binary files a/bin/enum_gen and b/bin/enum_gen differ diff --git a/lib/tres/application.ex b/lib/tres/application.ex index 1d2e1ed..462e22c 100644 --- a/lib/tres/application.ex +++ b/lib/tres/application.ex @@ -4,12 +4,14 @@ defmodule Tres.Application do use Application alias Tres.SwitchRegistry + alias Tres.HandlerRegistry def start(_type, _args) do import Supervisor.Spec children = [ worker(Registry, [[keys: :unique, name: SwitchRegistry]], id: SwitchRegistry), + worker(Registry, [[keys: :unique, name: HandlerRegistry]], id: HandlerRegistry), supervisor(Tres.MessageHandlerSup, [], id: MessageHandlerSup), supervisor(OVSDB, [], id: OVSDB) ] diff --git a/lib/tres/controller.ex b/lib/tres/controller.ex index 19abd7f..323355e 100644 --- a/lib/tres/controller.ex +++ b/lib/tres/controller.ex @@ -3,6 +3,7 @@ defmodule Tres.Controller do quote location: :keep do import Tres.SwitchRegistry, only: [ + lookup_handler_pid: 1, send_message: 2, send_message: 3, blocking_send_message: 2, diff --git a/lib/tres/message_handler_sup.ex b/lib/tres/message_handler_sup.ex index 24bb2f2..6797ffb 100644 --- a/lib/tres/message_handler_sup.ex +++ b/lib/tres/message_handler_sup.ex @@ -19,7 +19,9 @@ defmodule Tres.MessageHandlerSup do def start_child(dpid) do {cb_mod, _cb_args} = Tres.Utils.get_callback_module() child_spec = cb_mod.handler_spec(dpid) - Supervisor.start_child(__MODULE__, child_spec) + {:ok, pid} = Supervisor.start_child(__MODULE__, child_spec) + :ok = Tres.SwitchRegistry.register_handler_pid(dpid, pid) + {:ok, pid} end def terminate_child(dpid) do diff --git a/lib/tres/secure_channel.ex b/lib/tres/secure_channel.ex index 69a3cd3..f8520e3 100644 --- a/lib/tres/secure_channel.ex +++ b/lib/tres/secure_channel.ex @@ -609,19 +609,19 @@ defmodule Tres.SecureChannel do defp close_connection(:handler_error, state_data) do debug("connection terminated: Got handler error") - Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id}) + _ = send(state_data.handler_pid, {:switch_disconnected, :handler_error}) {:stop, :normal, %{state_data | socket: nil}} end defp close_connection(:ping_failed, state_data) do debug("connection terminated: Exceeded to max_ping_fail_count") - Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id}) + _ = send(state_data.handler_pid, {:switch_disconnected, :ping_failed}) {:stop, :normal, %{state_data | socket: nil}} end defp close_connection({:main_closed, reason}, state_data) do debug("connection terminated: Main connection down by #{inspect(reason)}") - Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id}) + _ = send(state_data.handler_pid, {:switch_disconnected, :main_closed}) {:stop, :normal, %{state_data | socket: nil}} end @@ -632,19 +632,19 @@ defmodule Tres.SecureChannel do defp close_connection({:trap_detected, reason}, state_data) do debug("connection terminated: Trapped by #{inspect(reason)}") - Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id}) + _ = send(state_data.handler_pid, {:switch_disconnected, :trap_detected}) {:stop, :normal, %{state_data | socket: nil}} end defp close_connection(:tcp_closed, state_data) do debug("connection terminated: TCP Closed by peer") - Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id}) + _ = send(state_data.handler_pid, {:switch_disconnected, :tcp_closed}) {:stop, :normal, %{state_data | socket: nil}} end defp close_connection({:tcp_error, reason}, state_data) do debug("connection terminated: TCP Error occured: #{inspect(reason)}") - Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id}) + _ = send(state_data.handler_pid, {:switch_disconnected, :tcp_error}) {:stop, :normal, %{state_data | socket: nil}} end end diff --git a/lib/tres/switch_registry.ex b/lib/tres/switch_registry.ex index 30fb857..7e68492 100644 --- a/lib/tres/switch_registry.ex +++ b/lib/tres/switch_registry.ex @@ -3,6 +3,29 @@ defmodule Tres.SwitchRegistry do Dispatcher """ + # For DatapathHandler + + def register_handler_pid({_dpid, _aux_id} = datapath_id, pid) do + case Registry.register(Tres.HandlerRegistry, datapath_id, pid) do + {:ok, _owner} -> + :ok + {:error, {:already_registered, _owner}} -> + :ok + end + end + + def lookup_handler_pid({_dpid, _aux_id} = datapath_id) do + case Registry.lookup(Tres.HandlerRegistry, datapath_id) do + [{_owner, pid}] -> pid + [] -> nil + end + end + + def lookup_handler_pid(datapath_id) when is_binary(datapath_id), + do: lookup_handler({datapath_id, 0}) + + # For Datapath + def register({_dpid, _aux_id} = datapath_id) do {:ok, _} = Registry.register(__MODULE__, datapath_id, []) end