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