Creating a Linux bridge

A network bridge helps you connect 2 different networks with the same characteristics (Ethernet for example). Its very easy to implement it in linux using some tools

Example

We run Qemu  image with tap interface – we can connect from the qemu image to the linux host but we cant access the entire network (internet)

Qemu script:

sudo qemu-system-x86_64 -net tap -net nic  -kernel bzImage -m 1G -append “console=tty1 root=/dev/nfs nfsroot=192.168.0.1:/home/developer/x86system/outfs ip=192.168.0.2:192.168.0.1:192.168.0.1:255.255.255.0:qemu:eth0

here we define a virtual network card (tun mode). The network card connects qemu and linux for NFS file system and for communication so if we ping 192.168.0.1 from qemu we get response but if we ping other ip address from external network we don’t get any response

 

to solve this lets define a bridge to connect qemu to the internet:

first we add another network adapter to the qemu image:

qemu-system-x86_64  \
-netdev tap,id=tap0 -device e1000,netdev=tap0 \
-netdev tap,id=tap1 -device e1000,netdev=tap1 \
-kernel bzImage -m 1G \
-append "console=tty1 root=/dev/nfs" \
 "nfsroot=192.168.0.1:/home/developer/x86system/outfs" \
 "ip=192.168.0.20:192.168.0.1:192.168.0.1:255.255.255.0:qemu:eth0"

 

While running the emulator Linux creates 2 adapters (tap0, tap1)

 

tap0      Link encap:Ethernet  HWaddr e2:79:e0:bd:5d:ae 
          inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::e079:e0ff:febd:5dae/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:881 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1885 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:128996 (128.9 KB)  TX bytes:2200876 (2.2 MB)

tap1      Link encap:Ethernet  HWaddr 12:33:21:83:e1:9e 
             inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::1033:21ff:fe83:e19e/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:0 (0.0 B)  TX bytes:441 (441.0 B)

to connect to second adapter we need to set an ip address in qemu:

# ifconfig eth1 192.168.1.20

Now Qemu connected to linux host with 2 adapters , one for NFS and the second we want to configure with the bridge

 

Create a new bridge:

# sudo ip link add name br1 type bridge

Set it up

# sudo ip link set br1 up

Add the network interface and set it master

# sudo ip link set eth0 master br1

Add the tap interface to the bridge

# sudo brctl addif br1 tap1

Now if you ping the same address , it will work

 

Delete the bridge:

# sudo ip link set br1 down
# sudo brctl delbr br1

 

Tagged