blob: 30645e8298176a45db6f3f73adf62a8102ec7097 [file] [log] [blame]
Carmelo Casconeb34d8e12020-09-28 16:16:59 -07001#!/usr/bin/python3
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -08002from mininet.topo import Topo
3from mininet.net import Mininet
4from mininet.cli import CLI
5from mininet.log import setLogLevel, info, debug
6from mininet.node import Host, RemoteController, OVSSwitch
7
8QUAGGA_DIR = '/usr/lib/quagga'
9# Must exist and be owned by quagga user (quagga:quagga by default on Ubuntu)
10QUAGGA_RUN_DIR = '/var/run/quagga'
Kunihiro Ishiguroc470e102015-01-20 15:51:29 -080011CONFIG_DIR = 'configs-ipv6'
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -080012
13class SdnIpHost(Host):
14 def __init__(self, name, ip, route, *args, **kwargs):
15 Host.__init__(self, name, ip=ip, *args, **kwargs)
16
Kunihiro Ishiguroc470e102015-01-20 15:51:29 -080017 self.name = name
18 self.ip = ip
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -080019 self.route = route
20
21 def config(self, **kwargs):
22 Host.config(self, **kwargs)
23
24 debug("configuring route %s" % self.route)
25
Kunihiro Ishiguroc470e102015-01-20 15:51:29 -080026 self.cmd('ip addr add %s dev %s-eth0' % (self.ip, self.name))
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -080027 self.cmd('ip route add default via %s' % self.route)
28
29class Router(Host):
30 def __init__(self, name, quaggaConfFile, zebraConfFile, intfDict, *args, **kwargs):
31 Host.__init__(self, name, *args, **kwargs)
32
33 self.quaggaConfFile = quaggaConfFile
34 self.zebraConfFile = zebraConfFile
35 self.intfDict = intfDict
36
37 def config(self, **kwargs):
38 Host.config(self, **kwargs)
39 self.cmd('sysctl net.ipv4.ip_forward=1')
40 self.cmd('sysctl net.ipv6.conf.all.forwarding=1')
41
Carmelo Casconeb34d8e12020-09-28 16:16:59 -070042 for intf, attrs in list(self.intfDict.items()):
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -080043 self.cmd('ip addr flush dev %s' % intf)
44 if 'mac' in attrs:
45 self.cmd('ip link set %s down' % intf)
46 self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
47 self.cmd('ip link set %s up ' % intf)
48 for addr in attrs['ipAddrs']:
49 self.cmd('ip addr add %s dev %s' % (addr, intf))
50
51 self.cmd('/usr/lib/quagga/zebra -d -f %s -z %s/zebra%s.api -i %s/zebra%s.pid' % (self.zebraConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
52 self.cmd('/usr/lib/quagga/bgpd -d -f %s -z %s/zebra%s.api -i %s/bgpd%s.pid' % (self.quaggaConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
53
54
55 def terminate(self):
56 self.cmd("ps ax | egrep 'bgpd%s.pid|zebra%s.pid' | awk '{print $1}' | xargs kill" % (self.name, self.name))
57
58 Host.terminate(self)
59
60class SdnSwitch(OVSSwitch):
61 def __init__(self, name, dpid, *args, **kwargs):
62 OVSSwitch.__init__(self, name, dpid=dpid, *args, **kwargs)
63
64 def start(self, controllers):
65 OVSSwitch.start(self, controllers)
66 self.cmd("ovs-vsctl set Bridge %s protocols=OpenFlow13" % self.name)
67
68
69class SdnIpTopo( Topo ):
70 "SDN-IP tutorial topology"
Phil Huangf5d6c912015-12-21 15:43:06 +080071
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -080072 def build( self ):
73 s1 = self.addSwitch('s1', cls=SdnSwitch, dpid='00000000000000a1')
74 s2 = self.addSwitch('s2', cls=SdnSwitch, dpid='00000000000000a2')
75 s3 = self.addSwitch('s3', cls=SdnSwitch, dpid='00000000000000a3')
76 s4 = self.addSwitch('s4', cls=SdnSwitch, dpid='00000000000000a4')
77 s5 = self.addSwitch('s5', cls=SdnSwitch, dpid='00000000000000a5')
78 s6 = self.addSwitch('s6', cls=SdnSwitch, dpid='00000000000000a6')
79
80 zebraConf = '%s/zebra.conf' % CONFIG_DIR
81
82 # Switches we want to attach our routers to, in the correct order
83 attachmentSwitches = [s1, s2, s5, s6]
84
85 for i in range(1, 4+1):
86 name = 'r%s' % i
87
88 eth0 = { 'mac' : '00:00:00:00:0%s:01' % i,
Kunihiro Ishiguroc470e102015-01-20 15:51:29 -080089 'ipAddrs' : ['2001:%s::1/48' % i] }
90 eth1 = { 'ipAddrs' : ['2001:10%s::101/48' % i] }
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -080091 intfs = { '%s-eth0' % name : eth0,
92 '%s-eth1' % name : eth1 }
93
94 quaggaConf = '%s/quagga%s.conf' % (CONFIG_DIR, i)
95
96 router = self.addHost(name, cls=Router, quaggaConfFile=quaggaConf,
97 zebraConfFile=zebraConf, intfDict=intfs)
Phil Huangf5d6c912015-12-21 15:43:06 +080098
Kunihiro Ishiguroc470e102015-01-20 15:51:29 -080099 host = self.addHost('h%s' % i, cls=SdnIpHost,
100 ip='2001:10%s::1/48' % i,
101 route='2001:10%s::101' % i)
Phil Huangf5d6c912015-12-21 15:43:06 +0800102
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -0800103 self.addLink(router, attachmentSwitches[i-1])
104 self.addLink(router, host)
105
106 # Set up the internal BGP speaker
Phil Huangf5d6c912015-12-21 15:43:06 +0800107 bgpEth0 = { 'mac':'00:00:00:00:00:01',
Kunihiro Ishiguroc470e102015-01-20 15:51:29 -0800108 'ipAddrs' : ['2001:1::101/48',
109 '2001:2::101/48',
110 '2001:3::101/48',
111 '2001:4::101/48',] }
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -0800112 bgpEth1 = { 'ipAddrs' : ['10.10.10.1/24'] }
113 bgpIntfs = { 'bgp-eth0' : bgpEth0,
114 'bgp-eth1' : bgpEth1 }
Phil Huangf5d6c912015-12-21 15:43:06 +0800115
116 bgp = self.addHost( "bgp", cls=Router,
117 quaggaConfFile = '%s/quagga-sdn.conf' % CONFIG_DIR,
118 zebraConfFile = zebraConf,
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -0800119 intfDict=bgpIntfs )
Phil Huangf5d6c912015-12-21 15:43:06 +0800120
Kunihiro Ishigurof5df2842015-01-15 12:59:36 -0800121 self.addLink( bgp, s3 )
122
123 # Connect BGP speaker to the root namespace so it can peer with ONOS
124 root = self.addHost( 'root', inNamespace=False, ip='10.10.10.2/24' )
125 self.addLink( root, bgp )
126
127
128 # Wire up the switches in the topology
129 self.addLink( s1, s2 )
130 self.addLink( s1, s3 )
131 self.addLink( s2, s4 )
132 self.addLink( s3, s4 )
133 self.addLink( s3, s5 )
134 self.addLink( s4, s6 )
135 self.addLink( s5, s6 )
136
137topos = { 'sdnip' : SdnIpTopo }
138
139if __name__ == '__main__':
140 setLogLevel('debug')
141 topo = SdnIpTopo()
142
143 net = Mininet(topo=topo, controller=RemoteController)
144
145 net.start()
146
147 CLI(net)
148
149 net.stop()
150
151 info("done\n")