Topology
Expected result
- All PCs can access Internet
- All can see each other (directly if in the same VLAN or via host interface if in different VLANs)
Configuration
#Create namespaces and virtual switchsudo ip netns add pc1sudo ip netns add pc2sudo ip netns add pc3sudo ip netns add pc4sudo brctl addbr vSwitch #Create vEth interface pairssudo ip link add eth1 type veth peer name eth5sudo ip link add eth2 type veth peer name eth6sudo ip link add eth3 type veth peer name eth7sudo ip link add eth4 type veth peer name eth8 #Add interfaces to namespacessudo ip link set eth1 netns pc1sudo ip link set eth2 netns pc2sudo ip link set eth3 netns pc3sudo ip link set eth4 netns pc4 #Add interfaces to switchsudo brctl addif vSwitch eth5sudo brctl addif vSwitch eth6sudo brctl addif vSwitch eth7sudo brctl addif vSwitch eth8#Create vlan interface on namespacessudo ip netns exec pc1 ip link add link eth1 name vlan type vlan id 10sudo ip netns exec pc2 ip link add link eth2 name vlan type vlan id 10sudo ip netns exec pc3 ip link add link eth3 name vlan type vlan id 20sudo ip netns exec pc4 ip link add link eth4 name vlan type vlan id 20 #Set IP addresssudo ip netns exec pc1 ip addr add 10.0.0.1/25 dev vlansudo ip netns exec pc2 ip addr add 10.0.0.2/25 dev vlansudo ip netns exec pc3 ip addr add 10.0.0.203/25 dev vlansudo ip netns exec pc4 ip addr add 10.0.0.204/25 dev vlan#Turn on interfacessudo ip netns exec pc1 ip link set eth1 upsudo ip netns exec pc2 ip link set eth2 upsudo ip netns exec pc3 ip link set eth3 upsudo ip netns exec pc4 ip link set eth4 upsudo ip link set vSwitch upsudo ip link set eth5 upsudo ip link set eth6 upsudo ip link set eth7 upsudo ip link set eth8 upsudo ip netns exec pc1 ip link set vlan upsudo ip netns exec pc2 ip link set vlan upsudo ip netns exec pc3 ip link set vlan upsudo ip netns exec pc4 ip link set vlan up #Create vlan interface on Host machine and set IP addresssudo ip link add link vSwitch name vlan10 type vlan id 10sudo ip link add link vSwitch name vlan20 type vlan id 20sudo ip addr add 10.0.0.10/25 dev vlan10sudo ip addr add 10.0.0.220/25 dev vlan20sudo ip link set vlan10 upsudo ip link set vlan20 up #Set default gateway for namespacessudo ip netns exec pc1 route add default gw 10.0.0.10 vlansudo ip netns exec pc2 route add default gw 10.0.0.10 vlansudo ip netns exec pc3 route add default gw 10.0.0.220 vlansudo ip netns exec pc4 route add default gw 10.0.0.220 vlan#Set forwarding route for host machinesudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE#Check resultsudo ip netns exec pc1 ping 8.8.8.8sudo ip netns exec pc2 ping 8.8.8.8sudo ip netns exec pc3 ping 8.8.8.8sudo ip netns exec pc4 ping 8.8.8.8 |
Result
vdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 8.8.8.8PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=38.6 ms^C--- 8.8.8.8 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 38.612/38.612/38.612/0.000 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc2 ping 8.8.8.8PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=32.7 ms^C--- 8.8.8.8 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 32.771/32.771/32.771/0.000 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc3 ping 8.8.8.8PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=32.9 ms^C--- 8.8.8.8 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 32.986/32.986/32.986/0.000 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc4 ping 8.8.8.8PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=31.0 ms^C--- 8.8.8.8 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 31.066/31.066/31.066/0.000 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.203PING 10.0.0.203 (10.0.0.203) 56(84) bytes of data.64 bytes from 10.0.0.203: icmp_req=1 ttl=63 time=0.088 ms^C--- 10.0.0.203 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 0.088/0.088/0.088/0.000 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 ping 10.0.0.2PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.64 bytes from 10.0.0.2: icmp_req=1 ttl=64 time=0.450 ms^C--- 10.0.0.2 ping statistics ---1 packets transmitted, 1 received, 0% packet loss, time 0msrtt min/avg/max/mdev = 0.450/0.450/0.450/0.000 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 traceroute 10.0.0.2traceroute to 10.0.0.2 (10.0.0.2), 30 hops max, 60 byte packets 1 10.0.0.2 (10.0.0.2) 0.048 ms 0.007 ms 0.008 msvdkmai@Ubuntu32bit:~$ sudo ip netns exec pc1 traceroute 10.0.0.203traceroute to 10.0.0.203 (10.0.0.203), 30 hops max, 60 byte packets 1 10.0.0.10 (10.0.0.10) 0.047 ms 0.009 ms 0.007 ms 2 10.0.0.203 (10.0.0.203) 0.030 ms 0.015 ms 0.012 ms |
0 comments:
Post a Comment