Skip to content

Instantly share code, notes, and snippets.

@pichuang
Last active December 13, 2022 21:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pichuang/b1fc5ebfab471ef896b3 to your computer and use it in GitHub Desktop.
Save pichuang/b1fc5ebfab471ef896b3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.log import setLogLevel, info
from mininet.node import Controller, RemoteController
from mininet.cli import CLI
from mininet.link import Intf
from mininet.util import dumpNodeConnections
'''
two_vlan.py
teacher{1,2}: vlan 100
student{1,2}: vlan 200
'''
REMOTE_CONTROLLER_IP="140.113.215.6"
def MininetTopo():
'''
Prepare Your Topology
'''
net = Mininet (topo=None, build=False)
controller = net.addController(name='controller0',
controller=RemoteController,
ip=REMOTE_CONTROLLER_IP,
port=6633)
info("Create Host node\n")
teacher1 = net.addHost('teacher1', ip='10.0.0.1')
teacher2 = net.addHost('teacher2', ip='10.0.0.2')
student1 = net.addHost('student1', ip='10.0.0.3')
student2 = net.addHost('student2', ip='10.0.0.4')
info("Create Switch node\n")
switch1 = net.addSwitch('ovs1', protocols='OpenFlow13')
switch2 = net.addSwitch('ovs2', protocols='OpenFlow13')
switch3 = net.addSwitch('ovs3', protocols='OpenFlow13')
info("Link switch to host\n")
net.addLink(switch1, student1)
net.addLink(switch3, student2)
net.addLink(switch1, teacher1)
net.addLink(switch3, teacher2)
net.addLink(switch1, switch2)
net.addLink(switch2, switch3)
'''
Working your topology
'''
info("Start network\n")
net.start()
info("Dumping host connections\n")
dumpNodeConnections(net.hosts)
info("Testing network connectivity\n")
net.pingAll()
info("Set vlan id\n")
'''
ovs1 ofport list:
teacher1 ofport 1
student1 ofport 2
ovs2 ofport 3
'''
#Ingress
switch1.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs1 in_port=1,dl_vlan=0xffff,actions=push_vlan:0x8100,set_field:100-\>vlan_vid,output:3')
switch1.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs1 in_port=2,dl_vlan=0xffff,actions=push_vlan:0x8100,set_field:200-\>vlan_vid,output:3')
#Egress
switch1.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs1 in_port=3,dl_vlan=100,actions=pop_vlan,output:1')
switch1.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs1 in_port=3,dl_vlan=200,actions=pop_vlan,output:2')
'''
ovs3 ofport list:
teacher2 ofport 1
student2 ofport 2
ovs2 ofport 3
'''
#Ingress
switch3.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs3 in_port=1,dl_vlan=0xffff,actions=push_vlan:0x8100,set_field:100-\>vlan_vid,output:3')
switch3.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs3 in_port=2,dl_vlan=0xffff,actions=push_vlan:0x8100,set_field:200-\>vlan_vid,output:3')
#Egress
switch3.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs3 in_port=3,dl_vlan=100,actions=pop_vlan,output:1')
switch3.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs3 in_port=3,dl_vlan=200,actions=pop_vlan,output:2')
'''
ovs2 ofport list:
ovs2 ofport 1
ovs1 ofport 2
'''
switch2.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs2 in_port=1,actions=output:2')
switch2.cmdPrint('ovs-ofctl -O OpenFlow13 add-flow ovs2 in_port=2,actions=output:1')
info("Try vlan\n")
net.pingAll()
CLI(net)
'''
Clean mininet
'''
net.stop()
if __name__ == '__main__':
setLogLevel('info')
MininetTopo()
#!/usr/bin/env python2
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.log import setLogLevel, info
from mininet.node import Controller, RemoteController
from mininet.cli import CLI
from mininet.link import Intf
from mininet.util import dumpNodeConnections
'''
two_vlan.py
teacher{1,2}: vlan 100
student{1,2}: vlan 200
'''
REMOTE_CONTROLLER_IP="10.211.55.56"
def MininetTopo():
'''
Prepare Your Topology
'''
net = Mininet (topo=None, build=False)
controller = net.addController(name='controller0',
controller=RemoteController,
ip=REMOTE_CONTROLLER_IP,
port=6633)
info("Create Host node\n")
teacher1 = net.addHost('teacher1', ip='10.0.0.1')
teacher2 = net.addHost('teacher2', ip='10.0.0.2')
student1 = net.addHost('student1', ip='10.0.0.3')
student2 = net.addHost('student2', ip='10.0.0.4')
info("Create Switch node\n")
switch1 = net.addSwitch('ovs1')
switch2 = net.addSwitch('ovs2')
switch3 = net.addSwitch('ovs3')
info("Link switch to host\n")
net.addLink(switch1, student1)
net.addLink(switch3, student2)
net.addLink(switch1, teacher1)
net.addLink(switch3, teacher2)
net.addLink(switch1, switch2)
net.addLink(switch2, switch3)
'''
Working your topology
'''
info("Start network\n")
net.start()
info("Dumping host connections\n")
dumpNodeConnections(net.hosts)
info("Testing network connectivity\n")
net.pingAll()
info("Set vlan id\n")
'''
ovs1 ofport list:
teacher1 ofport 1
student1 ofport 2
ovs2 ofport 3
'''
#Ingress
switch1.cmdPrint('ovs-ofctl add-flow ovs1 in_port=1,dl_vlan=0xffff,actions=mod_vlan_vid:100,output:3')
switch1.cmdPrint('ovs-ofctl add-flow ovs1 in_port=2,dl_vlan=0xffff,actions=mod_vlan_vid:200,output:3')
#Egress
switch1.cmdPrint('ovs-ofctl add-flow ovs1 in_port=3,dl_vlan=100,actions=strip_vlan,output:1')
switch1.cmdPrint('ovs-ofctl add-flow ovs1 in_port=3,dl_vlan=200,actions=strip_vlan,output:2')
'''
ovs3 ofport list:
teacher2 ofport 1
student2 ofport 2
ovs2 ofport 3
'''
#Ingress
switch3.cmdPrint('ovs-ofctl add-flow ovs3 in_port=1,dl_vlan=0xffff,actions=mod_vlan_vid:100,output:3')
switch3.cmdPrint('ovs-ofctl add-flow ovs3 in_port=2,dl_vlan=0xffff,actions=mod_vlan_vid:200,output:3')
#Egress
switch3.cmdPrint('ovs-ofctl add-flow ovs3 in_port=3,dl_vlan=100,actions=strip_vlan,output:1')
switch3.cmdPrint('ovs-ofctl add-flow ovs3 in_port=3,dl_vlan=200,actions=strip_vlan,output:2')
'''
ovs2 ofport list:
ovs2 ofport 1
ovs1 ofport 2
'''
switch2.cmdPrint('ovs-ofctl add-flow ovs2 in_port=1,actions=output:2')
switch2.cmdPrint('ovs-ofctl add-flow ovs2 in_port=2,actions=output:1')
info("Try vlan\n")
net.pingAll()
CLI(net)
'''
Clean mininet
'''
net.stop()
if __name__ == '__main__':
setLogLevel('info')
MininetTopo()
@pichuang
Copy link
Author

  • dl_vlan=0xffff 代表封包不帶 vlan tag
  • mod_vlan_vid=100 代表上 vlan_id=100 tag
  • strip_vlan 代表拔掉vlan tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment