Skip to content

Commit

Permalink
Add an example of LXC bridging
Browse files Browse the repository at this point in the history
  • Loading branch information
msantos committed Oct 30, 2013
1 parent 18ab7cf commit 7fd44a2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -296,6 +296,52 @@ Then connect over the tunnel to the second node:
ping 10.10.10.2
ssh 10.10.10.2

### Bridging

`br` is an example of a simple bridge that floods frames to all the switch
ports. `br` uses a tap device plugged into a Linux bridge as an
uplink port and 1 or more tap devices as the switch ports.

This example uses the tap devices as interfaces for Linux containers
(LXC).

* 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
```

* Start the bridge:

* `erlbr0` is the name of the tap device connected to the bridge
* `["erl0", "erl1", "erl2"]` are the tap devices used by the containers

```
br:start("erlbr0", ["erl0", "erl1", "erl2"]).
```

* In another shell, as root, bring up the uplink and attach it to the bridge:

```
# ifconfig erlbr0 up
# brctl addif br0 erlbr0
# brctl show br0
bridge name bridge id STP enabled interfaces
br0 8000.4aec6d3a44d1 no erlbr0
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
```

## TODO

Expand Down
64 changes: 64 additions & 0 deletions examples/br.erl
@@ -0,0 +1,64 @@
%% Copyright (c) 2013, Michael Santos <michael.santos@gmail.com>
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% Redistributions of source code must retain the above copyright
%% notice, this list of conditions and the following disclaimer.
%%
%% Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer in the
%% documentation and/or other materials provided with the distribution.
%%
%% Neither the name of the author nor the names of its contributors
%% may be used to endorse or promote products derived from this software
%% without specific prior written permission.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
-module(br).
-export([start/0, start/2]).


start() ->
start("erlbr0", ["erl0"]).

start(Uplink, Ifaces) ->
% Switch uplink
{ok, Br} = tuncer:create(Uplink, [tap, no_pi, {active, true}]),

% Switch ports
Dev = [ begin
{ok, N} = tuncer:create(Iface, [tap, no_pi, {active, true}]),
N
end || Iface <- Ifaces ],

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.

0 comments on commit 7fd44a2

Please sign in to comment.