Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. from mininet.node import Controller, RemoteController, OVSController
  2. from mininet.node import CPULimitedHost, Host, Node
  3. from mininet.node import OVSKernelSwitch, UserSwitch
  4. from mininet.node import OVSSwitch
  5. from mininet.net import Mininet
  6. from mininet.cli import CLI
  7. from mininet.log import setLogLevel, info
  8. from mininet.link import TCLink, Intf
  9. import datetime
  10. import os, sys, time
  11.  
  12.  
  13. def customRing2(upper, lower):
  14.  
  15. net = Mininet( controller=RemoteController, switch=OVSKernelSwitch, autoStaticArp=True, autoSetMacs=True)
  16.  
  17. info( "*** Creating (reference) controller\n" )
  18.  
  19. c1=net.addController(name='c1',controller=RemoteController,ip='0.0.0.0',port=6633)
  20.  
  21. info( "*** Creating switches\n" )
  22. s1 = net.addSwitch( 's1' )
  23. s2 = net.addSwitch( 's2' )
  24. s3 = net.addSwitch( 's3' )
  25.  
  26. info( "*** Creating h\n" )
  27. h1 = net.addHost( 'h1')
  28. h2 = net.addHost( 'h2')
  29. h3 = net.addHost( 'h3')
  30. h4 = net.addHost( 'h4')
  31. h5 = net.addHost( 'h5')
  32. h6 = net.addHost( 'h6')
  33.  
  34. info( "*** Creating links\n" )
  35. link = net.addLink(s1, h1)
  36. link.intf1.config(bw=0.03)
  37. link = net.addLink(s1, h2)
  38. link.intf1.config(bw=0.03)
  39. link = net.addLink(s2, h3)
  40. link.intf1.config(bw=0.03)
  41. link = net.addLink(s2, h4)
  42. link.intf1.config(bw=0.03)
  43. link = net.addLink(s3, h5)
  44. link.intf1.config(bw=0.03)
  45. link = net.addLink(s3, h6)
  46. link.intf1.config(bw=0.03)
  47. link = net.addLink(s1, s2)
  48. link.intf1.config(bw=0.03)
  49. link = net.addLink(s2, s3)
  50. link.intf1.config(bw=0.03)
  51. link = net.addLink(s1, s3)
  52. link.intf1.config(bw=0.03)
  53.  
  54.  
  55. info( "*** Starting network\n" )
  56. c1.start()
  57.  
  58. s1.start( [c1] )
  59. s2.start( [c1] )
  60. s3.start( [c1] )
  61.  
  62. net.build()
  63.  
  64. info( "*** Please, run controller and ryu using the command: $ryu-manager ryu.app.qos_rest_router ryu.app.rest_qos ryu.app.rest_conf_switch\n" )
  65. CLI( net )
  66.  
  67. #configuring switches
  68. s1.cmd('ovs-vsctl set Bridge s1 protocols=OpenFlow13')
  69. s1.cmd('ovs-vsctl set-manager ptcp:6632')
  70. s2.cmd('ovs-vsctl set Bridge s2 protocols=OpenFlow13')
  71. s2.cmd('ovs-vsctl set-manager ptcp:6632')
  72. s3.cmd('ovs-vsctl set Bridge s3 protocols=OpenFlow13')
  73. s3.cmd('ovs-vsctl set-manager ptcp:6632')
  74.  
  75. #configuring hosts
  76. h1.cmd('ifconfig h1-eth0 inet 1.1.1.2/30 && ifconfig h1-eth0 up')
  77. h2.cmd('ifconfig h2-eth0 inet 1.1.2.2/30 && ifconfig h2-eth0 up')
  78. h3.cmd('ifconfig h3-eth0 inet 2.2.1.2/30 && ifconfig h3-eth0 up')
  79. h4.cmd('ifconfig h4-eth0 inet 2.2.2.2/30 && ifconfig h4-eth0 up')
  80. h5.cmd('ifconfig h5-eth0 inet 3.3.1.2/30 && ifconfig h5-eth0 up')
  81. h6.cmd('ifconfig h6-eth0 inet 3.3.2.2/30 && ifconfig h6-eth0 up')
  82.  
  83. #configure hosts connections
  84. h1.cmd("ip route add default via 1.1.1.1")
  85. h2.cmd("ip route add default via 1.1.2.1")
  86. h3.cmd("ip route add default via 2.2.1.1")
  87. h4.cmd("ip route add default via 2.2.2.1")
  88. h5.cmd("ip route add default via 3.3.1.1")
  89. h6.cmd("ip route add default via 3.3.2.1")
  90.  
  91.  
  92. #local gateway networks
  93. c1.cmd("curl -X POST -d '{\"address\": \"1.1.1.1/30\"}' http://localhost:8080/router/0000000000000001")
  94. c1.cmd("curl -X POST -d '{\"address\": \"1.1.2.1/30\"}' http://localhost:8080/router/0000000000000001")
  95.  
  96. c1.cmd("curl -X POST -d '{\"address\": \"2.2.1.1/30\"}' http://localhost:8080/router/0000000000000002")
  97. c1.cmd("curl -X POST -d '{\"address\": \"2.2.2.1/30\"}' http://localhost:8080/router/0000000000000002")
  98.  
  99. c1.cmd("curl -X POST -d '{\"address\": \"3.3.1.1/30\"}' http://localhost:8080/router/0000000000000003")
  100. c1.cmd("curl -X POST -d '{\"address\": \"3.3.2.1/30\"}' http://localhost:8080/router/0000000000000003")
  101.  
  102. #outer gateway networks
  103. c1.cmd("curl -X POST -d '{\"address\": \"13.13.13.1/30\"}' http://localhost:8080/router/0000000000000001")
  104. c1.cmd("curl -X POST -d '{\"address\": \"20.6.20.2/30\"}' http://localhost:8080/router/0000000000000001")
  105.  
  106. c1.cmd("curl -X POST -d '{\"address\": \"20.6.20.1/30\"}' http://localhost:8080/router/0000000000000002")
  107. c1.cmd("curl -X POST -d '{\"address\": \"45.45.45.2/30\"}' http://localhost:8080/router/0000000000000002")
  108.  
  109. c1.cmd("curl -X POST -d '{\"address\": \"45.45.45.1/30\"}' http://localhost:8080/router/0000000000000003")
  110. c1.cmd("curl -X POST -d '{\"address\": \"13.13.13.2/30\"}' http://localhost:8080/router/0000000000000003")
  111.  
  112. #configure inter switches connections
  113. c1.cmd("curl -X POST -d '{\"gateway\": \"20.6.20.1\"}' http://localhost:8080/router/0000000000000001")
  114. c1.cmd("curl -X POST -d '{\"gateway\": \"45.45.45.1\"}' http://localhost:8080/router/0000000000000002")
  115. c1.cmd("curl -X POST -d '{\"gateway\": \"13.13.13.1\"}' http://localhost:8080/router/0000000000000003")
  116.  
  117. # #configure static routes
  118. # #inter servers
  119. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.1.2,ip,nw_dst=3.3.1.2,actions=drop")
  120. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.1.2,ip,nw_dst=1.1.1.2,actions=drop")
  121. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.1.2,ip,nw_dst=2.2.1.2,actions=drop")
  122.  
  123. # #server 1 - clients
  124. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.1.2,ip,nw_dst=1.1.2.2,actions=drop")
  125. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.1.2,ip,nw_dst=2.2.2.2,actions=drop")
  126. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.1.2,ip,nw_dst=3.3.2.2,actions=drop")
  127.  
  128. # #server 2 - clients
  129. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.1.2,ip,nw_dst=1.1.2.2,actions=drop")
  130. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.1.2,ip,nw_dst=2.2.2.2,actions=drop")
  131. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.1.2,ip,nw_dst=3.3.2.2,actions=drop")
  132.  
  133. # #server 3 - clients
  134. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.1.2,ip,nw_dst=1.1.2.2,actions=drop")
  135. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.1.2,ip,nw_dst=2.2.2.2,actions=drop")
  136. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.1.2,ip,nw_dst=3.3.2.2,actions=drop")
  137.  
  138. # #client 1 - servers
  139. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.2.2,ip,nw_dst=1.1.1.2,actions=drop")
  140. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.2.2,ip,nw_dst=2.2.1.2,actions=drop")
  141. # os.system("ovs-ofctl -O OpenFlow13 add-flow s1 priority=65535,nw_src=1.1.2.2,ip,nw_dst=3.3.1.2,actions=drop")
  142.  
  143. # #client 2 - servers
  144. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.2.2,ip,nw_dst=1.1.1.2,actions=drop")
  145. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.2.2,ip,nw_dst=2.2.1.2,actions=drop")
  146. # os.system("ovs-ofctl -O OpenFlow13 add-flow s2 priority=65535,nw_src=2.2.2.2,ip,nw_dst=3.3.1.2,actions=drop")
  147.  
  148. # #client 3 - servers
  149. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.2.2,ip,nw_dst=1.1.1.2,actions=drop")
  150. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.2.2,ip,nw_dst=2.2.1.2,actions=drop")
  151. # os.system("ovs-ofctl -O OpenFlow13 add-flow s3 priority=65535,nw_src=3.3.2.2,ip,nw_dst=3.3.1.2,actions=drop")
  152.  
  153.  
  154. #configuring controller
  155. c1.cmd('curl -X PUT -d \'"tcp:127.0.0.1:6632"\' http://localhost:8080/v1.0/conf/switches/0000000000000001/ovsdb_addr')
  156. c1.cmd('curl -X PUT -d \'"tcp:127.0.0.1:6632"\' http://localhost:8080/v1.0/conf/switches/0000000000000002/ovsdb_addr')
  157. c1.cmd('curl -X PUT -d \'"tcp:127.0.0.1:6632"\' http://localhost:8080/v1.0/conf/switches/0000000000000003/ovsdb_addr')
  158.  
  159.  
  160. if(datetime.datetime.now().time().hour >= lower and datetime.datetime.now().time().hour < upper):
  161. info("*** Setting QOS rules.\n")
  162. c1.cmd('curl -X POST -d \'{"port_name": "s1-eth1", "type": "linux-htb", "max_rate": "30000", "queues": [{"min_rate": "20000"}]}\' http://localhost:8080/qos/queue/0000000000000001')
  163. c1.cmd('curl -X POST -d \'{"port_name": "s2-eth1", "type": "linux-htb", "max_rate": "30000", "queues": [{"min_rate": "20000"}]}\' http://localhost:8080/qos/queue/0000000000000002')
  164. c1.cmd('curl -X POST -d \'{"port_name": "s3-eth1", "type": "linux-htb", "max_rate": "30000", "queues": [{"min_rate": "20000"}]}\' http://localhost:8080/qos/queue/0000000000000003')
  165.  
  166. c1.cmd('curl -X POST -d \'{"match": {"nw_dst": "1.1.1.2", "nw_proto": "TCP", "tp_dst": "1234"}, "actions":{"queue": "0"} }\' http://localhost:8080/qos/rules/0000000000000003')
  167. c1.cmd('curl -X POST -d \'{"match": {"nw_dst": "2.2.1.2", "nw_proto": "TCP", "tp_dst": "1234"}, "actions":{"queue": "0"} }\' http://localhost:8080/qos/rules/0000000000000001')
  168. c1.cmd('curl -X POST -d \'{"match": {"nw_dst": "3.3.1.2", "nw_proto": "TCP", "tp_dst": "1234"}, "actions":{"queue": "0"} }\' http://localhost:8080/qos/rules/0000000000000002')
  169.  
  170.  
  171. info( "*** Running CLI\n" )
  172. CLI( net )
  173.  
  174. info( "*** Stopping network\n" )
  175. net.stop()
  176.  
  177. if __name__ == '__main__':
  178. setLogLevel( 'info' )
  179.  
  180. hourInterval = sys.argv[1]
  181.  
  182. upper = int(hourInterval.split(":")[1])
  183. lower = int(hourInterval.split(":")[0])
  184.  
  185. customRing2(upper, lower)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement