Add simple LearningSwitch Example
This commit is contained in:
parent
ab21f6e240
commit
f2768496c8
11 changed files with 334 additions and 0 deletions
20
examples/learning_switch/lib/learning_switch/application.ex
Normal file
20
examples/learning_switch/lib/learning_switch/application.ex
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
defmodule LearningSwitch.Application do
|
||||
# See https://hexdocs.pm/elixir/Application.html
|
||||
# for more information on OTP Applications
|
||||
@moduledoc false
|
||||
|
||||
use Application
|
||||
|
||||
def start(_type, _args) do
|
||||
# List all child processes to be supervised
|
||||
children = [
|
||||
# Starts a worker by calling: LearningSwitch.Worker.start_link(arg)
|
||||
# {LearningSwitch.Worker, arg},
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: LearningSwitch.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
end
|
||||
66
examples/learning_switch/lib/learning_switch/fdb.ex
Normal file
66
examples/learning_switch/lib/learning_switch/fdb.ex
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
defmodule LearningSwitch.FDB do
|
||||
use Agent
|
||||
|
||||
defmodule Entry do
|
||||
defstruct [
|
||||
mac: nil,
|
||||
port_no: nil,
|
||||
age_max: 0,
|
||||
last_update: 0
|
||||
]
|
||||
|
||||
def new(mac, port_no, age_max \\ 180) do
|
||||
%Entry{
|
||||
mac: mac,
|
||||
port_no: port_no,
|
||||
age_max: age_max,
|
||||
last_update: :os.timestamp
|
||||
}
|
||||
end
|
||||
|
||||
def update(self, port_no) do
|
||||
%{self|port_no: port_no, last_update: :os.timestamp}
|
||||
end
|
||||
|
||||
def aged_out?(self) do
|
||||
:timer.now_diff(:os.timestamp, self.last_update) > (1000 * self.age_max)
|
||||
end
|
||||
end
|
||||
|
||||
def start_link do
|
||||
Agent.start_link(&Map.new/0)
|
||||
end
|
||||
|
||||
def lookup(self, mac) do
|
||||
entry = Agent.get(self, &Map.get(&1, mac))
|
||||
entry && entry.port_no
|
||||
end
|
||||
|
||||
def learn(self, mac, port_no) do
|
||||
entry = Agent.get(self, &Map.get(&1, mac))
|
||||
entry = if entry do
|
||||
Entry.update(entry, port_no)
|
||||
else
|
||||
Entry.new(mac, port_no)
|
||||
end
|
||||
Agent.update(self, &Map.put(&1, mac, entry))
|
||||
end
|
||||
|
||||
def age(self) do
|
||||
mac_addrs = Agent.get(self, &find_aged_entries(&1))
|
||||
for mac <- mac_addrs do
|
||||
Agent.update(self, &Map.delete(&1, mac))
|
||||
end
|
||||
end
|
||||
|
||||
# private function
|
||||
|
||||
defp find_aged_entries(map) do
|
||||
Enum.flat_map(map, fn({mac, entry}) ->
|
||||
case Entry.aged_out?(entry) do
|
||||
true -> [mac]
|
||||
false -> []
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
138
examples/learning_switch/lib/learning_switch/ofctl.ex
Normal file
138
examples/learning_switch/lib/learning_switch/ofctl.ex
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
defmodule LearningSwitch.Ofctl do
|
||||
use GenServer
|
||||
use Tres.Controller
|
||||
|
||||
import Logger
|
||||
|
||||
alias LearningSwitch.FDB
|
||||
|
||||
@ingress_filtering_table_id 0
|
||||
@forwarding_table_id 1
|
||||
|
||||
@aging_time 180
|
||||
|
||||
@mcast {"010000000000", "110000000000"}
|
||||
@bcast "ffffffffffff"
|
||||
@ipv6_mcast {"333300000000", "ffff00000000"}
|
||||
|
||||
defmodule State do
|
||||
defstruct [
|
||||
datapath_id: nil,
|
||||
conn_ref: nil,
|
||||
fdb_pid: nil
|
||||
]
|
||||
end
|
||||
|
||||
def start_link(datapath_id, args) do
|
||||
GenServer.start_link(__MODULE__, [datapath_id, args])
|
||||
end
|
||||
|
||||
def init([datapath_id, _args]) do
|
||||
:ok = debug("Switch Ready: datapath_id: #{inspect(datapath_id)}")
|
||||
conn_ref = SwitchRegistry.monitor(datapath_id)
|
||||
{:ok, pid} = FDB.start_link
|
||||
init_datapath(datapath_id)
|
||||
state = %State{
|
||||
datapath_id: datapath_id,
|
||||
conn_ref: conn_ref,
|
||||
fdb_pid: pid
|
||||
}
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
def handle_info(%PacketIn{} = packet_in, state) do
|
||||
<<_dhost::6-bytes, shost::6-bytes, _rest::bytes>> = packet_in.data
|
||||
eth_src = Openflow.Utils.to_hex_string(shost)
|
||||
FDB.learn(state.fdb_pid, eth_src, packet_in.in_port)
|
||||
add_forwarding_flow_and_packet_out(packet_in, state)
|
||||
:ok = debug("PacketIn: eth_src: #{eth_src} datapath_id: #{inspect(state.datapath_id)}")
|
||||
{:noreply, state}
|
||||
end
|
||||
def handle_info({:'DOWN', ref, :process, _pid, _reason}, %State{conn_ref: ref} = state) do
|
||||
:ok = debug("Switch Disconnected: datapath_id: #{inspect(state.datapath_id)}")
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
def handle_info(info, state) do
|
||||
:ok = warn("Unhandled message #{inspect(info)}: #{inspect(state.datapath_id)}")
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
# private functions
|
||||
|
||||
defp init_datapath(datapath_id) do
|
||||
init_flow_tables(datapath_id)
|
||||
end
|
||||
|
||||
defp init_flow_tables(datapath_id) do
|
||||
for flow_options <- [
|
||||
add_default_broadcast_flow_entry(),
|
||||
add_default_flooding_flow_entry(),
|
||||
add_multicast_mac_drop_flow_entry(),
|
||||
add_ipv6_multicast_mac_drop_flow_entry(),
|
||||
add_default_forwarding_flow_entry()] do
|
||||
send_flow_mod_add(datapath_id, flow_options)
|
||||
end
|
||||
end
|
||||
|
||||
defp add_forwarding_flow_and_packet_out(packet_in, state) do
|
||||
<<dhost::6-bytes, _shost::6-bytes, _rest::bytes>> = packet_in.data
|
||||
eth_dst = Openflow.Utils.to_hex_string(dhost)
|
||||
port_no = FDB.lookup(state.fdb_pid, eth_dst)
|
||||
add_forwarding_flow_entry(packet_in, port_no)
|
||||
packet_out(packet_in, port_no || :flood)
|
||||
end
|
||||
|
||||
defp packet_out(%PacketIn{datapath_id: datapath_id, data: data}, port_no) do
|
||||
send_packet_out(
|
||||
datapath_id,
|
||||
data: data,
|
||||
actions: [Output.new(port_no)]
|
||||
)
|
||||
end
|
||||
|
||||
defp add_forwarding_flow_entry(_packet_in, nil), do: :noop
|
||||
defp add_forwarding_flow_entry(%PacketIn{datapath_id: datapath_id, data: data} = packet_in, port_no) do
|
||||
<<dhost::6-bytes, shost::6-bytes, _rest::bytes>> = data
|
||||
send_flow_mod_add(
|
||||
datapath_id,
|
||||
idle_timeout: @aging_time,
|
||||
priority: 2,
|
||||
match: Match.new(
|
||||
in_port: packet_in.in_port,
|
||||
eth_dst: Openflow.Utils.to_hex_string(dhost),
|
||||
eth_src: Openflow.Utils.to_hex_string(shost)),
|
||||
instructions: [ApplyActions.new(Output.new(port_no))]
|
||||
)
|
||||
end
|
||||
|
||||
defp add_default_broadcast_flow_entry do
|
||||
[table_id: @forwarding_table_id,
|
||||
priority: 3,
|
||||
match: Match.new(eth_dst: @bcast),
|
||||
instructions: [ApplyActions.new(Output.new(:flood))]]
|
||||
end
|
||||
|
||||
defp add_default_flooding_flow_entry do
|
||||
[table_id: @forwarding_table_id,
|
||||
priority: 1,
|
||||
instructions: [ApplyActions.new(Output.new(:controller))]]
|
||||
end
|
||||
|
||||
defp add_multicast_mac_drop_flow_entry do
|
||||
[table_id: @ingress_filtering_table_id,
|
||||
priority: 2,
|
||||
match: Match.new(eth_dst: @mcast)]
|
||||
end
|
||||
|
||||
defp add_ipv6_multicast_mac_drop_flow_entry do
|
||||
[table_id: @ingress_filtering_table_id,
|
||||
priority: 2,
|
||||
match: Match.new(eth_dst: @ipv6_mcast)]
|
||||
end
|
||||
|
||||
defp add_default_forwarding_flow_entry do
|
||||
[table_id: @ingress_filtering_table_id,
|
||||
priority: 1,
|
||||
instructions: [GotoTable.new(@forwarding_table_id)]]
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue