Skip to content

Instantly share code, notes, and snippets.

@shun159
Created March 4, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shun159/35594bb37a1b5d944ce7 to your computer and use it in GitHub Desktop.
Save shun159/35594bb37a1b5d944ce7 to your computer and use it in GitHub Desktop.
-module(learning_switch_controller).
-behaviour(gen_server).
-define(SERVER, ?MODULE).
-include_lib("deps/flexx/include/flexx.hrl").
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([start_link/0]).
%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
init(Args) ->
flexx_dispatcher:join(self(), [{ datapath, connected }]),
{ok, Args}.
handle_call({{datapath, connected}, DatapathId, _Msg }, _From, State) ->
io:format("[~p] datapath connected(~p)~n", [?MODULE, DatapathId]),
FlowModTable = flow_mod_table_id(),
LearningFlow = learning_flow(),
Flood = flood_flow(),
flexx:send(DatapathId, FlowModTable),
flexx:send(DatapathId, LearningFlow),
flexx:send(DatapathId, Flood),
{reply, ok, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
flow_mod_table_id() ->
NXData = #nicira_header{ sub_type = flow_mod_table_id,
body = #nx_flow_mod_table_id{ set = true }},
Body = #ofp_vendor_header{ vendor = nicira,
data = NXData },
#ofp_header{ type = vendor, body = Body }.
learning_flow() ->
FMS1 = #learn_match_field{
src = #nxm_field_header{ vendor = nxm0,
field = vlan_tci,
has_mask = false },
dst = #nxm_field_header{ vendor = nxm0,
field = vlan_tci,
has_mask = false }},
FMS2 = #learn_match_field{
src = #nxm_field_header{ vendor = nxm0,
field = eth_src,
has_mask = false },
dst = #nxm_field_header{ vendor = nxm0,
field = eth_dst,
has_mask = false}},
FMS3 = #learn_output_action{
port = #nxm_field_header{ vendor = nxm0,
field = in_port,
has_mask = false }},
Learn = #ofp_action_header{
type = vendor,
body = #ofp_action_vendor{
vendor = nicira,
data = #nx_action_learn{
idle_timeout = 0,
hard_timeout = 0,
priority = 2,
cookie = <<0:64>>,
flags = [],
table_id = 1,
fin_idle_timeout = 0,
fin_hard_timeout = 0,
flow_mod_spec = [FMS1, FMS2, FMS3]}}},
Resubmit = #ofp_action_header{
type = vendor,
body = #ofp_action_vendor{
vendor = nicira,
data = #nx_action_resubmit{
subtype = resubmit_table,
in_port = in_port,
table_id = 1 }}},
FlowMod = #nx_flow_mod{
command = add,
table_id = 0,
priority = 1,
actions = [Learn, Resubmit] },
NXData = #nicira_header{ sub_type = flow_mod,
body = FlowMod },
Body = #ofp_vendor_header{ vendor = nicira,
data = NXData },
#ofp_header{ type = vendor, body = Body }.
flood_flow() ->
Output = #ofp_action_header{ type = output,
body = #ofp_action_output{
port = flood }},
FlowMod = #nx_flow_mod{
command = add,
table_id = 1,
priority = 1,
actions = [Output] },
NXData = #nicira_header{ sub_type = flow_mod,
body = FlowMod },
Body = #ofp_vendor_header{ vendor = nicira,
data = NXData },
#ofp_header{ type = vendor, body = Body }.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment