Skip to content

Instantly share code, notes, and snippets.

@msantos
Last active December 26, 2015 23:29
Show Gist options
  • Save msantos/7231332 to your computer and use it in GitHub Desktop.
Save msantos/7231332 to your computer and use it in GitHub Desktop.
Simple Erlang switch for LXC
-module(br).
-export([start/0]).
% git clone https://github.com/msantos/tunctl
% sudo ./start.sh
start() ->
% Switch uplink
{ok, Br} = tuncer:create("erlbr", [tap, no_pi, {active, true}]),
% Switch port
{ok, Dev} = tuncer:create("erl0", [tap, no_pi, {active, true}]),
switch(Br, [Dev]).
switch(Br, Dev) ->
receive
{tuntap, Br, Data} ->
% Data received on uplink: flood to ports
error_logger:info_report([{br, Br}, {data, Data}]),
[ ok = tuncer:send(N, Data) || N <- Dev ],
switch(Br, Dev);
{tuntap, Port, Data} ->
% Data received on port: flood to all other ports and uplink
error_logger:info_report([{dev, Port}, {data, Data}]),
[ ok = tuncer:send(N, Data) || N <- Dev ++ [Br], N =/= Port ],
switch(Br, Dev);
Error ->
error_logger:error_report([{error, Error}])
end.
  • Create a bridge and attach the physical ethernet interface
# /etc/network/interfaces                                                                                                                                                                                           
iface br0 inet dhcp                                                                                                                                                                                                 
    bridge_ports eth0                                                                                                                                                                                               
    bridge_stp off                                                                                                                                                                                                  
    bridge_fd 0                                                                                                                                                                                                     
    bridge_maxwait 0
  • Compile and run the code
erlc br.erl                                                                                                                                                                                                         
sudo ./start.sh                                                                                                                                                                                                     
> br:start().          
  • In another shell, as root, bring up the uplink and attach it to the bridge:
# ifconfig erlbr up                                                                                                                                                                                                 
# brctl addif br0 erlbr                                                                                                                                                                                             
# brctl show br0                                                                                                                                                                                                    
bridge name     bridge id               STP enabled     interfaces                                                                                                                                                  
br0             8000.4aec6d3a44d1       no              erlbr                                                                                                                                                       
                                                        eth0            
  • Move the switch port interface into the container. The interface name inside the container will be known as "erl0".
lxc.network.type=phys                                                                                                                                                                                               
lxc.network.link=erl0                                                                                                                                                                                               
lxc.network.flags=up       
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment