├── BFlows.py ├── README.md ├── __init__.py ├── fattree.py ├── network_awareness.py ├── network_monitor.py └── setting.py /BFlows.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Huang MaChi at Chongqing University 2 | # of Posts and Telecommunications, Chongqing, China. 3 | # Copyright (C) 2016 Li Cheng at Beijing University of Posts 4 | # and Telecommunications. www.muzixing.com 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | from ryu import cfg 20 | from ryu.base import app_manager 21 | from ryu.controller import ofp_event 22 | from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER 23 | from ryu.controller.handler import set_ev_cls 24 | from ryu.ofproto import ofproto_v1_3 25 | from ryu.lib.packet import packet 26 | from ryu.lib.packet import ethernet 27 | from ryu.lib.packet import arp 28 | from ryu.lib.packet import ipv4 29 | from ryu.lib.packet import tcp 30 | from ryu.lib.packet import udp 31 | 32 | import network_awareness 33 | import network_monitor 34 | import setting 35 | 36 | 37 | CONF = cfg.CONF 38 | 39 | 40 | class ShortestForwarding(app_manager.RyuApp): 41 | """ 42 | ShortestForwarding is a Ryu app for forwarding packets on shortest path. 43 | This App does not defined the path computation method. 44 | To get shortest path, this module depends on network awareness and 45 | network monitor modules. 46 | """ 47 | 48 | OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] 49 | _CONTEXTS = { 50 | "network_awareness": network_awareness.NetworkAwareness, 51 | "network_monitor": network_monitor.NetworkMonitor} 52 | 53 | WEIGHT_MODEL = {'hop': 'weight', 'fnum': 'fnum'} 54 | 55 | def __init__(self, *args, **kwargs): 56 | super(ShortestForwarding, self).__init__(*args, **kwargs) 57 | self.name = "shortest_forwarding" 58 | self.awareness = kwargs["network_awareness"] 59 | self.monitor = kwargs["network_monitor"] 60 | self.datapaths = {} 61 | self.weight = self.WEIGHT_MODEL[CONF.weight] 62 | 63 | @set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER]) 64 | def _state_change_handler(self, ev): 65 | """ 66 | Collect datapath information. 67 | """ 68 | datapath = ev.datapath 69 | if ev.state == MAIN_DISPATCHER: 70 | if not datapath.id in self.datapaths: 71 | self.logger.debug('register datapath: %016x', datapath.id) 72 | self.datapaths[datapath.id] = datapath 73 | elif ev.state == DEAD_DISPATCHER: 74 | if datapath.id in self.datapaths: 75 | self.logger.debug('unregister datapath: %016x', datapath.id) 76 | del self.datapaths[datapath.id] 77 | 78 | @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) 79 | def _packet_in_handler(self, ev): 80 | ''' 81 | In packet_in handler, we need to learn access_table by ARP and IP packets. 82 | ''' 83 | msg = ev.msg 84 | pkt = packet.Packet(msg.data) 85 | arp_pkt = pkt.get_protocol(arp.arp) 86 | ip_pkt = pkt.get_protocol(ipv4.ipv4) 87 | 88 | if isinstance(arp_pkt, arp.arp): 89 | self.logger.debug("ARP processing") 90 | self.arp_forwarding(msg, arp_pkt.src_ip, arp_pkt.dst_ip) 91 | 92 | if isinstance(ip_pkt, ipv4.ipv4): 93 | self.logger.debug("IPV4 processing") 94 | if len(pkt.get_protocols(ethernet.ethernet)): 95 | eth_type = pkt.get_protocols(ethernet.ethernet)[0].ethertype 96 | self.shortest_forwarding(msg, eth_type, ip_pkt.src, ip_pkt.dst) 97 | 98 | def add_flow(self, dp, priority, match, actions, idle_timeout=0, hard_timeout=0): 99 | """ 100 | Send a flow entry to datapath. 101 | """ 102 | ofproto = dp.ofproto 103 | parser = dp.ofproto_parser 104 | inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)] 105 | mod = parser.OFPFlowMod(datapath=dp, priority=priority, 106 | idle_timeout=idle_timeout, 107 | hard_timeout=hard_timeout, 108 | match=match, instructions=inst) 109 | dp.send_msg(mod) 110 | 111 | def _build_packet_out(self, datapath, buffer_id, src_port, dst_port, data): 112 | """ 113 | Build packet out object. 114 | """ 115 | actions = [] 116 | if dst_port: 117 | actions.append(datapath.ofproto_parser.OFPActionOutput(dst_port)) 118 | 119 | msg_data = None 120 | if buffer_id == datapath.ofproto.OFP_NO_BUFFER: 121 | if data is None: 122 | return None 123 | msg_data = data 124 | 125 | out = datapath.ofproto_parser.OFPPacketOut( 126 | datapath=datapath, buffer_id=buffer_id, 127 | data=msg_data, in_port=src_port, actions=actions) 128 | return out 129 | 130 | def send_packet_out(self, datapath, buffer_id, src_port, dst_port, data): 131 | """ 132 | Send packet out packet to assigned datapath. 133 | """ 134 | out = self._build_packet_out(datapath, buffer_id, 135 | src_port, dst_port, data) 136 | if out: 137 | datapath.send_msg(out) 138 | 139 | def get_port(self, dst_ip, access_table): 140 | """ 141 | Get access port of dst host. 142 | access_table = {(sw,port):(ip, mac),} 143 | """ 144 | if access_table: 145 | if isinstance(access_table.values()[0], tuple): 146 | for key in access_table.keys(): 147 | if dst_ip == access_table[key][0]: # Use the IP address only, not the MAC address. (hmc) 148 | dst_port = key[1] 149 | return dst_port 150 | return None 151 | 152 | def get_port_pair_from_link(self, link_to_port, src_dpid, dst_dpid): 153 | """ 154 | Get port pair of link, so that controller can install flow entry. 155 | link_to_port = {(src_dpid,dst_dpid):(src_port,dst_port),} 156 | """ 157 | if (src_dpid, dst_dpid) in link_to_port: 158 | return link_to_port[(src_dpid, dst_dpid)] 159 | else: 160 | self.logger.info("Link from dpid:%s to dpid:%s is not in links" % 161 | (src_dpid, dst_dpid)) 162 | return None 163 | 164 | def flood(self, msg): 165 | """ 166 | Flood packet to the access ports which have no record of host. 167 | access_ports = {dpid:set(port_num,),} 168 | access_table = {(sw,port):(ip, mac),} 169 | """ 170 | datapath = msg.datapath 171 | ofproto = datapath.ofproto 172 | 173 | for dpid in self.awareness.access_ports: 174 | for port in self.awareness.access_ports[dpid]: 175 | if (dpid, port) not in self.awareness.access_table.keys(): 176 | datapath = self.datapaths[dpid] 177 | out = self._build_packet_out( 178 | datapath, ofproto.OFP_NO_BUFFER, 179 | ofproto.OFPP_CONTROLLER, port, msg.data) 180 | datapath.send_msg(out) 181 | self.logger.debug("Flooding packet to access port") 182 | 183 | def arp_forwarding(self, msg, src_ip, dst_ip): 184 | """ 185 | Send ARP packet to the destination host if the dst host record 186 | is existed, else flow it to the unknow access port. 187 | result = (datapath, port) 188 | """ 189 | datapath = msg.datapath 190 | ofproto = datapath.ofproto 191 | 192 | result = self.awareness.get_host_location(dst_ip) 193 | if result: 194 | # Host has been recorded in access table. 195 | datapath_dst, out_port = result[0], result[1] 196 | datapath = self.datapaths[datapath_dst] 197 | out = self._build_packet_out(datapath, ofproto.OFP_NO_BUFFER, 198 | ofproto.OFPP_CONTROLLER, 199 | out_port, msg.data) 200 | datapath.send_msg(out) 201 | self.logger.debug("Deliver ARP packet to knew host") 202 | else: 203 | # Flood is not good. 204 | self.flood(msg) 205 | 206 | def get_path(self, src, dst, weight): 207 | """ 208 | Get shortest path from network_awareness module. 209 | generator (nx.shortest_simple_paths( )) produces 210 | lists of simple paths, in order from shortest to longest. 211 | """ 212 | shortest_paths = self.awareness.shortest_paths 213 | graph = self.awareness.graph 214 | 215 | if weight == self.WEIGHT_MODEL['hop']: 216 | return shortest_paths.get(src).get(dst)[0] 217 | elif weight == self.WEIGHT_MODEL['fnum']: 218 | # Because all paths will be calculated when we call self.monitor.get_best_path_by_fnum, 219 | # so we just need to call it once in a period, and then, we can get path directly. 220 | # If path is existed just return it, else calculate and return it. 221 | try: 222 | path = self.monitor.best_paths.get(src).get(dst) 223 | return path 224 | except: 225 | result = self.monitor.get_best_path_by_fnum(graph, shortest_paths) 226 | paths = result 227 | best_path = paths.get(src).get(dst) 228 | return best_path 229 | else: 230 | pass 231 | 232 | def get_sw(self, dpid, in_port, src, dst): 233 | """ 234 | Get pair of source and destination switches. 235 | """ 236 | src_sw = dpid 237 | dst_sw = None 238 | src_location = self.awareness.get_host_location(src) # src_location = (dpid, port) 239 | if in_port in self.awareness.access_ports[dpid]: 240 | if (dpid, in_port) == src_location: 241 | src_sw = src_location[0] 242 | else: 243 | return None 244 | dst_location = self.awareness.get_host_location(dst) # dst_location = (dpid, port) 245 | if dst_location: 246 | dst_sw = dst_location[0] 247 | if src_sw and dst_sw: 248 | return src_sw, dst_sw 249 | else: 250 | return None 251 | 252 | def send_flow_mod(self, datapath, flow_info, src_port, dst_port): 253 | """ 254 | Build flow entry, and send it to datapath. 255 | flow_info = (eth_type, src_ip, dst_ip, in_port) 256 | or 257 | flow_info = (eth_type, src_ip, dst_ip, in_port, ip_proto, Flag, L4_port) 258 | """ 259 | parser = datapath.ofproto_parser 260 | actions = [] 261 | actions.append(parser.OFPActionOutput(dst_port)) 262 | if len(flow_info) == 7: 263 | if flow_info[-3] == 6: 264 | if flow_info[-2] == 'src': 265 | match = parser.OFPMatch( 266 | in_port=src_port, eth_type=flow_info[0], 267 | ipv4_src=flow_info[1], ipv4_dst=flow_info[2], 268 | ip_proto=6, tcp_src=flow_info[-1]) 269 | elif flow_info[-2] == 'dst': 270 | match = parser.OFPMatch( 271 | in_port=src_port, eth_type=flow_info[0], 272 | ipv4_src=flow_info[1], ipv4_dst=flow_info[2], 273 | ip_proto=6, tcp_dst=flow_info[-1]) 274 | else: 275 | pass 276 | elif flow_info[-3] == 17: 277 | if flow_info[-2] == 'src': 278 | match = parser.OFPMatch( 279 | in_port=src_port, eth_type=flow_info[0], 280 | ipv4_src=flow_info[1], ipv4_dst=flow_info[2], 281 | ip_proto=17, udp_src=flow_info[-1]) 282 | elif flow_info[-2] == 'dst': 283 | match = parser.OFPMatch( 284 | in_port=src_port, eth_type=flow_info[0], 285 | ipv4_src=flow_info[1], ipv4_dst=flow_info[2], 286 | ip_proto=17, udp_dst=flow_info[-1]) 287 | else: 288 | pass 289 | elif len(flow_info) == 4: 290 | match = parser.OFPMatch( 291 | in_port=src_port, eth_type=flow_info[0], 292 | ipv4_src=flow_info[1], ipv4_dst=flow_info[2]) 293 | else: 294 | pass 295 | 296 | self.add_flow(datapath, 30, match, actions, 297 | idle_timeout=5, hard_timeout=0) 298 | 299 | def install_flow(self, datapaths, link_to_port, path, flow_info, buffer_id, data=None): 300 | ''' 301 | Install flow entries for datapaths. 302 | path=[dpid1, dpid2, ...] 303 | flow_info = (eth_type, src_ip, dst_ip, in_port) 304 | or 305 | flow_info = (eth_type, src_ip, dst_ip, in_port, ip_proto, Flag, L4_port) 306 | ''' 307 | if path is None or len(path) == 0: 308 | self.logger.info("Path error!") 309 | return 310 | in_port = flow_info[3] 311 | first_dp = datapaths[path[0]] 312 | out_port = first_dp.ofproto.OFPP_LOCAL 313 | 314 | # Install flow entry for intermediate datapaths. 315 | for i in xrange(1, len(path) - 2): 316 | port = self.get_port_pair_from_link(link_to_port, path[i-1], path[i]) 317 | port_next = self.get_port_pair_from_link(link_to_port, path[i], path[i+1]) 318 | if port and port_next: 319 | src_port, dst_port = port[1], port_next[0] 320 | datapath = datapaths[path[i]] 321 | self.send_flow_mod(datapath, flow_info, src_port, dst_port) 322 | 323 | # Install flow entry for the first datapath. 324 | port_pair = self.get_port_pair_from_link(link_to_port, path[0], path[1]) 325 | if port_pair is None: 326 | self.logger.info("Port not found in first hop.") 327 | return 328 | out_port = port_pair[0] 329 | self.send_flow_mod(first_dp, flow_info, in_port, out_port) 330 | # Send packet_out to the first datapath. 331 | self.send_packet_out(first_dp, buffer_id, in_port, out_port, data) 332 | 333 | def get_L4_info(self, tcp_pkt, udp_pkt): 334 | """ 335 | Get ip_proto and L4 port number. 336 | """ 337 | ip_proto = None 338 | L4_port = None 339 | Flag = None 340 | if tcp_pkt: 341 | ip_proto = 6 342 | if tcp_pkt.src_port and tcp_pkt.src_port in setting.bw_sensitive_port_list: 343 | L4_port = tcp_pkt.src_port 344 | Flag = 'src' 345 | elif tcp_pkt.dst_port and tcp_pkt.dst_port in setting.bw_sensitive_port_list: 346 | L4_port = tcp_pkt.dst_port 347 | Flag = 'dst' 348 | else: 349 | pass 350 | elif udp_pkt: 351 | ip_proto = 17 352 | if udp_pkt.src_port and udp_pkt.src_port in setting.bw_sensitive_port_list: 353 | L4_port = udp_pkt.src_port 354 | Flag = 'src' 355 | elif udp_pkt.dst_port and udp_pkt.dst_port in setting.bw_sensitive_port_list: 356 | L4_port = udp_pkt.dst_port 357 | Flag = 'dst' 358 | else: 359 | pass 360 | else: 361 | pass 362 | return (ip_proto, L4_port, Flag) 363 | 364 | def shortest_forwarding(self, msg, eth_type, ip_src, ip_dst): 365 | """ 366 | Calculate shortest forwarding path and Install them into datapaths. 367 | flow_info = (eth_type, ip_src, ip_dst, in_port) 368 | or 369 | flow_info = (eth_type, ip_src, ip_dst, in_port, ip_proto, Flag, L4_port) 370 | """ 371 | datapath = msg.datapath 372 | in_port = msg.match['in_port'] 373 | result = self.get_sw(datapath.id, in_port, ip_src, ip_dst) # result = (src_sw, dst_sw) 374 | if result: 375 | src_sw, dst_sw = result[0], result[1] 376 | if dst_sw: 377 | # Path has already been calculated, just get it. 378 | path = self.get_path(src_sw, dst_sw, weight=self.weight) 379 | if setting.enable_Flow_Entry_L4Port: 380 | pkt = packet.Packet(msg.data) 381 | tcp_pkt = pkt.get_protocol(tcp.tcp) 382 | udp_pkt = pkt.get_protocol(udp.udp) 383 | # Get ip_proto and L4 port number. 384 | ip_proto, L4_port, Flag = self.get_L4_info(tcp_pkt, udp_pkt) 385 | if ip_proto and L4_port and Flag: 386 | if ip_proto == 6: 387 | L4_Proto = 'TCP' 388 | elif ip_proto == 17: 389 | L4_Proto = 'UDP' 390 | else: 391 | pass 392 | self.logger.info("[PATH]%s<-->%s(%s Port:%d): %s" % (ip_src, ip_dst, L4_Proto, L4_port, path)) 393 | flow_info = (eth_type, ip_src, ip_dst, in_port, ip_proto, Flag, L4_port) 394 | else: 395 | self.logger.info("[PATH]%s<-->%s: %s" % (ip_src, ip_dst, path)) 396 | flow_info = (eth_type, ip_src, ip_dst, in_port) 397 | # Install flow entries to datapaths along the path. 398 | self.install_flow(self.datapaths, 399 | self.awareness.link_to_port, 400 | path, flow_info, msg.buffer_id, msg.data) 401 | else: 402 | # Flood is not good. 403 | self.flood(msg) 404 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## BFlows 2 | 3 | BFlows is a SDN-based traffic schduling application. It includes a set of Ryu applications collecting basic network information, such as topology and free bandwidth of links. 4 | You can specify the mode of computing shortest paths when starting Ryu by adding "weight" argument. Moreover, you can set "k_paths" argument to support K-Shortest paths computing. 5 | Fortunately, our application supports load balancing based on dynamic traffic information. 6 | 7 | The detailed information of the modules is shown below: 8 | 9 | * Fattree is topology modules; 10 | 11 | * Network Awareness is a module for collecting network information; 12 | 13 | * Network Monitor is a module for collecting traffic information; 14 | 15 | * BFlows is the main module of the application; 16 | 17 | * Setting is the module including common setting. 18 | 19 | We make use of networkx's data structure to store topology. Meanwhile, we also utilize networkx's built-in algorithm to calculate shortest paths. 20 | 21 | 22 | ### Prerequisites 23 | 24 | The following softwares should have been installed in your machine. 25 | * Mininet: git clone git://github.com/mininet/mininet; mininet/util/install.sh -a 26 | * Ryu: git clone git://github.com/osrg/ryu.git; cd ryu; pip install . 27 | * Networkx: pip install networkx 28 | 29 | 30 | ### Download 31 | 32 | Download files into Ryu directory, for instance, 'ryu/ryu/app/BFlows' is OK. 33 | 34 | 35 | ### Make some change 36 | 37 | To register parsing parameters, you NEED to add the following code into the end of ryu/ryu/flags.py. 38 | 39 | CONF.register_cli_opts([ 40 | # k_shortest_forwarding 41 | cfg.IntOpt('k_paths', default=4, help='number of candidate paths of KSP.'), 42 | cfg.StrOpt('weight', default='bw', help='weight type of computing shortest path.'), 43 | cfg.IntOpt('fanout', default=4, help='switch fanout number.')]) 44 | 45 | 46 | ### Reinstall Ryu 47 | 48 | You must reinstall Ryu, so that you can run the new code. In the top directory of Ryu project: 49 | 50 | sudo python setup.py install 51 | 52 | 53 | ### Start 54 | 55 | Note: Before doing the experiment, you should change the controller's IP address from '192.168.56.101' to your own machine's eth0 IP address in the fattree.py module in each application, because '192.168.56.101' is my Ubuntu's eth0 IP address (Try 'ifconfig' in your Ubuntu to find out the eth0's IP address). Otherwise, the switches can't connect to the controller. 56 | 57 | Firstly, start up the network. An example is shown below: 58 | 59 | $ sudo python ryu/ryu/app/BFlows/fattree.py --k 4 60 | 61 | And then, start a new terminal and go into the top directory of Ryu, then run the application. You are suggested to add arguments when starting Ryu. An example is shown below: 62 | 63 | $ cd ryu 64 | $ ryu-manager --observe-links ryu/app/BFlows/BFlows.py --k_paths=4 --weight=fnum --fanout=4 65 | 66 | NOTE: After these, we should wait for the network to complete the initiation for several seconds, because LLDP needs some time to discovery the network topology. We can't operate the network until "[GET NETWORK TOPOLOGY]" is printed in the terminal of the Ryu controller, otherwise, some error will occur. It may be about 10 seconds for fattree4, and a little longer for fattree8. 67 | 68 | After that, test the correctness of BFlows: 69 | 70 | mininet> pingall 71 | mininet> iperf 72 | 73 | If you want to show the collected information, you can set the parameters in setting.py. Also, you can change the setting as you like. 74 | After that, you can see the information shown in the terminal. 75 | 76 | 77 | ### Authors 78 | 79 | Brought to you by Huang MaChi (Chongqing University of Posts and Telecommunications, Chongqing, China.) and Li Cheng (Beijing University of Posts and Telecommunications. www.muzixing.com). 80 | 81 | If you have any question, you can email me at huangmachi@foxmail.com. Don't forget to STAR this repository! 82 | 83 | Enjoy it! 84 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | "For loading module" 2 | -------------------------------------------------------------------------------- /fattree.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Huang MaChi at Chongqing University 2 | # of Posts and Telecommunications, Chongqing, China. 3 | # Copyright (C) 2016 Li Cheng at Beijing University of Posts 4 | # and Telecommunications. www.muzixing.com 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | from mininet.net import Mininet 20 | from mininet.node import Controller, RemoteController 21 | from mininet.cli import CLI 22 | from mininet.log import setLogLevel 23 | from mininet.link import Link, Intf, TCLink 24 | from mininet.topo import Topo 25 | 26 | import os 27 | import logging 28 | import argparse 29 | import time 30 | 31 | 32 | parser = argparse.ArgumentParser(description="Parameters importation") 33 | parser.add_argument('--k', dest='k', type=int, default=4, choices=[4, 8], help="Switch fanout number") 34 | args = parser.parse_args() 35 | 36 | 37 | class Fattree(Topo): 38 | """ 39 | Class of Fattree Topology. 40 | """ 41 | CoreSwitchList = [] 42 | AggSwitchList = [] 43 | EdgeSwitchList = [] 44 | HostList = [] 45 | 46 | def __init__(self, k, density): 47 | self.pod = k 48 | self.density = density 49 | self.iCoreLayerSwitch = (k/2)**2 50 | self.iAggLayerSwitch = k*k/2 51 | self.iEdgeLayerSwitch = k*k/2 52 | self.iHost = self.iEdgeLayerSwitch * density 53 | 54 | # Topo initiation 55 | Topo.__init__(self) 56 | 57 | def createNodes(self): 58 | self.createCoreLayerSwitch(self.iCoreLayerSwitch) 59 | self.createAggLayerSwitch(self.iAggLayerSwitch) 60 | self.createEdgeLayerSwitch(self.iEdgeLayerSwitch) 61 | self.createHost(self.iHost) 62 | 63 | def _addSwitch(self, number, level, switch_list): 64 | """ 65 | Create switches. 66 | """ 67 | for i in xrange(1, number+1): 68 | PREFIX = str(level) + "00" 69 | if i >= 10: 70 | PREFIX = str(level) + "0" 71 | switch_list.append(self.addSwitch(PREFIX + str(i))) 72 | 73 | def createCoreLayerSwitch(self, NUMBER): 74 | self._addSwitch(NUMBER, 1, self.CoreSwitchList) 75 | 76 | def createAggLayerSwitch(self, NUMBER): 77 | self._addSwitch(NUMBER, 2, self.AggSwitchList) 78 | 79 | def createEdgeLayerSwitch(self, NUMBER): 80 | self._addSwitch(NUMBER, 3, self.EdgeSwitchList) 81 | 82 | def createHost(self, NUMBER): 83 | """ 84 | Create hosts. 85 | """ 86 | for i in xrange(1, NUMBER+1): 87 | if i >= 100: 88 | PREFIX = "h" 89 | elif i >= 10: 90 | PREFIX = "h0" 91 | else: 92 | PREFIX = "h00" 93 | self.HostList.append(self.addHost(PREFIX + str(i), cpu=1.0/float(NUMBER))) 94 | 95 | def createLinks(self, bw_c2a=10, bw_a2e=10, bw_e2h=10): 96 | """ 97 | Add network links. 98 | """ 99 | # Core to Agg 100 | end = self.pod/2 101 | for x in xrange(0, self.iAggLayerSwitch, end): 102 | for i in xrange(0, end): 103 | for j in xrange(0, end): 104 | self.addLink( 105 | self.CoreSwitchList[i*end+j], 106 | self.AggSwitchList[x+i], 107 | bw=bw_c2a, max_queue_size=1000) # use_htb=False 108 | 109 | # Agg to Edge 110 | for x in xrange(0, self.iAggLayerSwitch, end): 111 | for i in xrange(0, end): 112 | for j in xrange(0, end): 113 | self.addLink( 114 | self.AggSwitchList[x+i], self.EdgeSwitchList[x+j], 115 | bw=bw_a2e, max_queue_size=1000) # use_htb=False 116 | 117 | # Edge to Host 118 | for x in xrange(0, self.iEdgeLayerSwitch): 119 | for i in xrange(0, self.density): 120 | self.addLink( 121 | self.EdgeSwitchList[x], 122 | self.HostList[self.density * x + i], 123 | bw=bw_e2h, max_queue_size=1000) # use_htb=False 124 | 125 | def set_ovs_protocol_13(self,): 126 | """ 127 | Set the OpenFlow version for switches. 128 | """ 129 | self._set_ovs_protocol_13(self.CoreSwitchList) 130 | self._set_ovs_protocol_13(self.AggSwitchList) 131 | self._set_ovs_protocol_13(self.EdgeSwitchList) 132 | 133 | def _set_ovs_protocol_13(self, sw_list): 134 | for sw in sw_list: 135 | cmd = "sudo ovs-vsctl set bridge %s protocols=OpenFlow13" % sw 136 | os.system(cmd) 137 | 138 | 139 | def set_host_ip(net, topo): 140 | hostlist = [] 141 | for k in xrange(len(topo.HostList)): 142 | hostlist.append(net.get(topo.HostList[k])) 143 | i = 1 144 | j = 1 145 | for host in hostlist: 146 | host.setIP("10.%d.0.%d" % (i, j)) 147 | j += 1 148 | if j == topo.density+1: 149 | j = 1 150 | i += 1 151 | 152 | def create_subnetList(topo, num): 153 | """ 154 | Create the subnet list of the certain Pod. 155 | """ 156 | subnetList = [] 157 | remainder = num % (topo.pod/2) 158 | if topo.pod == 4: 159 | if remainder == 0: 160 | subnetList = [num-1, num] 161 | elif remainder == 1: 162 | subnetList = [num, num+1] 163 | else: 164 | pass 165 | elif topo.pod == 8: 166 | if remainder == 0: 167 | subnetList = [num-3, num-2, num-1, num] 168 | elif remainder == 1: 169 | subnetList = [num, num+1, num+2, num+3] 170 | elif remainder == 2: 171 | subnetList = [num-1, num, num+1, num+2] 172 | elif remainder == 3: 173 | subnetList = [num-2, num-1, num, num+1] 174 | else: 175 | pass 176 | else: 177 | pass 178 | return subnetList 179 | 180 | def install_proactive(net, topo): 181 | """ 182 | Install direct flow entries for edge switches. 183 | """ 184 | # Edge Switch 185 | for sw in topo.EdgeSwitchList: 186 | num = int(sw[-2:]) 187 | 188 | # Downstream 189 | for i in xrange(1, topo.density+1): 190 | cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \ 191 | 'table=0,idle_timeout=0,hard_timeout=0,priority=10,arp, \ 192 | nw_dst=10.%d.0.%d,actions=output:%d'" % (sw, num, i, topo.pod/2+i) 193 | os.system(cmd) 194 | cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \ 195 | 'table=0,idle_timeout=0,hard_timeout=0,priority=10,ip, \ 196 | nw_dst=10.%d.0.%d,actions=output:%d'" % (sw, num, i, topo.pod/2+i) 197 | os.system(cmd) 198 | 199 | # Aggregate Switch 200 | # Downstream 201 | for sw in topo.AggSwitchList: 202 | num = int(sw[-2:]) 203 | subnetList = create_subnetList(topo, num) 204 | 205 | k = 1 206 | for i in subnetList: 207 | cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \ 208 | 'table=0,idle_timeout=0,hard_timeout=0,priority=10,arp, \ 209 | nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, topo.pod/2+k) 210 | os.system(cmd) 211 | cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \ 212 | 'table=0,idle_timeout=0,hard_timeout=0,priority=10,ip, \ 213 | nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, topo.pod/2+k) 214 | os.system(cmd) 215 | k += 1 216 | 217 | # Core Switch 218 | for sw in topo.CoreSwitchList: 219 | j = 1 220 | k = 1 221 | for i in xrange(1, len(topo.EdgeSwitchList)+1): 222 | cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \ 223 | 'table=0,idle_timeout=0,hard_timeout=0,priority=10,arp, \ 224 | nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, j) 225 | os.system(cmd) 226 | cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \ 227 | 'table=0,idle_timeout=0,hard_timeout=0,priority=10,ip, \ 228 | nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, j) 229 | os.system(cmd) 230 | k += 1 231 | if k == topo.pod/2 + 1: 232 | j += 1 233 | k = 1 234 | 235 | def run_experiment(pod, density, ip="127.0.0.1", port=6633, bw_c2a=10, bw_a2e=10, bw_e2h=10): 236 | """ 237 | Firstly, start up Mininet; 238 | secondly, start up Ryu controller; 239 | thirdly, generate traffics and test the performance of the network. 240 | """ 241 | # Create Topo. 242 | topo = Fattree(pod, density) 243 | topo.createNodes() 244 | topo.createLinks(bw_c2a=bw_c2a, bw_a2e=bw_a2e, bw_e2h=bw_e2h) 245 | 246 | # 1. Start Mininet. 247 | CONTROLLER_IP = ip 248 | CONTROLLER_PORT = port 249 | net = Mininet(topo=topo, link=TCLink, controller=None, autoSetMacs=True) 250 | net.addController( 251 | 'controller', controller=RemoteController, 252 | ip=CONTROLLER_IP, port=CONTROLLER_PORT) 253 | net.start() 254 | 255 | # Set the OpenFlow version for switches as 1.3.0. 256 | topo.set_ovs_protocol_13() 257 | # Set the IP addresses for hosts. 258 | set_host_ip(net, topo) 259 | # Install proactive flow entries. 260 | install_proactive(net, topo) 261 | 262 | CLI(net) 263 | 264 | # Stop Mininet. 265 | net.stop() 266 | 267 | if __name__ == '__main__': 268 | setLogLevel('info') 269 | if os.getuid() != 0: 270 | logging.warning("You are NOT root!") 271 | elif os.getuid() == 0: 272 | # run_experiment(4, 2) or run_experiment(8, 4) 273 | run_experiment(args.k, args.k/2) 274 | -------------------------------------------------------------------------------- /network_awareness.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Huang MaChi at Chongqing University 2 | # of Posts and Telecommunications, Chongqing, China. 3 | # Copyright (C) 2016 Li Cheng at Beijing University of Posts 4 | # and Telecommunications. www.muzixing.com 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | import networkx as nx 20 | import matplotlib.pyplot as plt 21 | import time 22 | 23 | from ryu import cfg 24 | from ryu.base import app_manager 25 | from ryu.controller import ofp_event 26 | from ryu.controller.handler import MAIN_DISPATCHER 27 | from ryu.controller.handler import CONFIG_DISPATCHER 28 | from ryu.controller.handler import set_ev_cls 29 | from ryu.ofproto import ofproto_v1_3 30 | from ryu.lib.packet import packet 31 | from ryu.lib.packet import ethernet 32 | from ryu.lib.packet import ipv4 33 | from ryu.lib.packet import arp 34 | from ryu.lib import hub 35 | from ryu.topology import event 36 | from ryu.topology.api import get_switch, get_link 37 | 38 | import setting 39 | 40 | 41 | CONF = cfg.CONF 42 | 43 | 44 | class NetworkAwareness(app_manager.RyuApp): 45 | """ 46 | NetworkAwareness is a Ryu app for discovering topology information. 47 | This App can provide many data services for other App, such as 48 | link_to_port, access_table, switch_port_table, access_ports, 49 | interior_ports, topology graph and shortest paths. 50 | """ 51 | OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] 52 | 53 | # List the event list should be listened. 54 | events = [event.EventSwitchEnter, 55 | event.EventSwitchLeave, event.EventPortAdd, 56 | event.EventPortDelete, event.EventPortModify, 57 | event.EventLinkAdd, event.EventLinkDelete] 58 | 59 | def __init__(self, *args, **kwargs): 60 | super(NetworkAwareness, self).__init__(*args, **kwargs) 61 | self.topology_api_app = self 62 | self.name = "awareness" 63 | self.link_to_port = {} # {(src_dpid,dst_dpid):(src_port,dst_port),} 64 | self.access_table = {} # {(sw,port):(ip, mac),} 65 | self.switch_port_table = {} # {dpid:set(port_num,),} 66 | self.access_ports = {} # {dpid:set(port_num,),} 67 | self.interior_ports = {} # {dpid:set(port_num,),} 68 | self.switches = [] # self.switches = [dpid,] 69 | self.shortest_paths = {} # {dpid:{dpid:[[path],],},} 70 | self.pre_link_to_port = {} 71 | self.pre_access_table = {} 72 | 73 | # Directed graph can record the loading condition of links more accurately. 74 | # self.graph = nx.Graph() 75 | self.graph = nx.DiGraph() 76 | self.start_time = time.time() 77 | 78 | # Start a green thread to discover network resource. 79 | self.discover_thread = hub.spawn(self._discover) 80 | 81 | def _discover(self): 82 | i = 0 83 | while True: 84 | self.show_topology() 85 | if i == 2: # Reload topology every 20 seconds. 86 | self.get_topology(None) 87 | i = 0 88 | hub.sleep(setting.DISCOVERY_PERIOD) 89 | i = i + 1 90 | 91 | def add_flow(self, dp, priority, match, actions, idle_timeout=0, hard_timeout=0): 92 | ofproto = dp.ofproto 93 | parser = dp.ofproto_parser 94 | inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, 95 | actions)] 96 | mod = parser.OFPFlowMod(datapath=dp, priority=priority, 97 | idle_timeout=idle_timeout, 98 | hard_timeout=hard_timeout, 99 | match=match, instructions=inst) 100 | dp.send_msg(mod) 101 | 102 | @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) 103 | def switch_features_handler(self, ev): 104 | """ 105 | Install table-miss flow entry to datapaths. 106 | """ 107 | datapath = ev.msg.datapath 108 | ofproto = datapath.ofproto 109 | parser = datapath.ofproto_parser 110 | self.logger.info("switch:%s connected", datapath.id) 111 | 112 | # Install table-miss flow entry. 113 | match = parser.OFPMatch() 114 | actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, 115 | ofproto.OFPCML_NO_BUFFER)] 116 | self.add_flow(datapath, 0, match, actions) 117 | 118 | @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) 119 | def _packet_in_handler(self, ev): 120 | """ 121 | Handle the packet_in packet, and register the access info. 122 | """ 123 | msg = ev.msg 124 | datapath = msg.datapath 125 | in_port = msg.match['in_port'] 126 | pkt = packet.Packet(msg.data) 127 | arp_pkt = pkt.get_protocol(arp.arp) 128 | ip_pkt = pkt.get_protocol(ipv4.ipv4) 129 | 130 | if arp_pkt: 131 | arp_src_ip = arp_pkt.src_ip 132 | mac = arp_pkt.src_mac 133 | # Record the access infomation. 134 | self.register_access_info(datapath.id, in_port, arp_src_ip, mac) 135 | elif ip_pkt: 136 | ip_src_ip = ip_pkt.src 137 | eth = pkt.get_protocols(ethernet.ethernet)[0] 138 | mac = eth.src 139 | # Record the access infomation. 140 | self.register_access_info(datapath.id, in_port, ip_src_ip, mac) 141 | else: 142 | pass 143 | 144 | @set_ev_cls(events) 145 | def get_topology(self, ev): 146 | """ 147 | Get topology info and calculate shortest paths. 148 | Note: In looped network, we should get the topology 149 | 20 or 30 seconds after the network went up. 150 | """ 151 | present_time = time.time() 152 | if present_time - self.start_time < setting.get_topology_delay: 153 | return 154 | 155 | self.logger.info("[GET NETWORK TOPOLOGY]") 156 | switch_list = get_switch(self.topology_api_app, None) 157 | self.create_port_map(switch_list) 158 | self.switches = [sw.dp.id for sw in switch_list] 159 | links = get_link(self.topology_api_app, None) 160 | self.create_interior_links(links) 161 | self.create_access_ports() 162 | self.graph = self.get_graph(self.link_to_port.keys()) 163 | self.shortest_paths = self.all_k_shortest_paths( 164 | self.graph, weight='weight', k=CONF.k_paths) 165 | 166 | def get_host_location(self, host_ip): 167 | """ 168 | Get host location info ((datapath, port)) according to the host ip. 169 | self.access_table = {(sw,port):(ip, mac),} 170 | """ 171 | for key in self.access_table.keys(): 172 | if self.access_table[key][0] == host_ip: 173 | return key 174 | self.logger.info("%s location is not found." % host_ip) 175 | return None 176 | 177 | def get_graph(self, link_list): 178 | """ 179 | Get Adjacency matrix from link_to_port. 180 | """ 181 | _graph = self.graph.copy() 182 | for src in self.switches: 183 | for dst in self.switches: 184 | if src == dst: 185 | _graph.add_edge(src, dst, weight=0) 186 | elif (src, dst) in link_list: 187 | _graph.add_edge(src, dst, weight=1) 188 | else: 189 | pass 190 | return _graph 191 | 192 | def create_port_map(self, switch_list): 193 | """ 194 | Create interior_port table and access_port table. 195 | """ 196 | for sw in switch_list: 197 | dpid = sw.dp.id 198 | self.switch_port_table.setdefault(dpid, set()) 199 | # switch_port_table is equal to interior_ports plus access_ports. 200 | self.interior_ports.setdefault(dpid, set()) 201 | self.access_ports.setdefault(dpid, set()) 202 | for port in sw.ports: 203 | # switch_port_table = {dpid:set(port_num,),} 204 | self.switch_port_table[dpid].add(port.port_no) 205 | 206 | def create_interior_links(self, link_list): 207 | """ 208 | Get links' srouce port to dst port from link_list. 209 | link_to_port = {(src_dpid,dst_dpid):(src_port,dst_port),} 210 | """ 211 | for link in link_list: 212 | src = link.src 213 | dst = link.dst 214 | self.link_to_port[(src.dpid, dst.dpid)] = (src.port_no, dst.port_no) 215 | # Find the access ports and interior ports. 216 | if link.src.dpid in self.switches: 217 | self.interior_ports[link.src.dpid].add(link.src.port_no) 218 | if link.dst.dpid in self.switches: 219 | self.interior_ports[link.dst.dpid].add(link.dst.port_no) 220 | 221 | def create_access_ports(self): 222 | """ 223 | Get ports without link into access_ports. 224 | """ 225 | for sw in self.switch_port_table: 226 | all_port_table = self.switch_port_table[sw] 227 | interior_port = self.interior_ports[sw] 228 | # That comes the access port of the switch. 229 | self.access_ports[sw] = all_port_table - interior_port 230 | 231 | def k_shortest_paths(self, graph, src, dst, weight='weight', k=5): 232 | """ 233 | Creat K shortest paths from src to dst. 234 | generator produces lists of simple paths, in order from shortest to longest. 235 | """ 236 | generator = nx.shortest_simple_paths(graph, source=src, target=dst, weight=weight) 237 | shortest_paths = [] 238 | try: 239 | for path in generator: 240 | if k <= 0: 241 | break 242 | shortest_paths.append(path) 243 | k -= 1 244 | return shortest_paths 245 | except: 246 | self.logger.debug("No path between %s and %s" % (src, dst)) 247 | 248 | def all_k_shortest_paths(self, graph, weight='weight', k=8): 249 | """ 250 | Creat all K shortest paths between datapaths. 251 | Note: We get shortest paths for bandwidth-sensitive 252 | traffic from bandwidth-sensitive switches. 253 | """ 254 | _graph = graph.copy() 255 | paths = {} 256 | # Find k shortest paths in graph. 257 | for src in _graph.nodes(): 258 | paths.setdefault(src, {src: [[src] for i in xrange(k)]}) 259 | for dst in _graph.nodes(): 260 | if src == dst: 261 | continue 262 | paths[src].setdefault(dst, []) 263 | paths[src][dst] = self.k_shortest_paths(_graph, src, dst, weight=weight, k=k) 264 | return paths 265 | 266 | def register_access_info(self, dpid, in_port, ip, mac): 267 | """ 268 | Register access host info into access table. 269 | """ 270 | if in_port in self.access_ports[dpid]: 271 | if (dpid, in_port) in self.access_table: 272 | if self.access_table[(dpid, in_port)] == (ip, mac): 273 | return 274 | else: 275 | self.access_table[(dpid, in_port)] = (ip, mac) 276 | return 277 | else: 278 | self.access_table.setdefault((dpid, in_port), None) 279 | self.access_table[(dpid, in_port)] = (ip, mac) 280 | return 281 | 282 | def show_topology(self): 283 | if self.pre_link_to_port != self.link_to_port and setting.TOSHOW_topo: 284 | # It means the link_to_port table has changed. 285 | _graph = self.graph.copy() 286 | print "\n---------------------Link Port---------------------" 287 | print '%6s' % ('switch'), 288 | for node in sorted([node for node in _graph.nodes()], key=lambda node: node): 289 | print '%6d' % node, 290 | print 291 | for node1 in sorted([node for node in _graph.nodes()], key=lambda node: node): 292 | print '%6d' % node1, 293 | for node2 in sorted([node for node in _graph.nodes()], key=lambda node: node): 294 | if (node1, node2) in self.link_to_port.keys(): 295 | print '%6s' % str(self.link_to_port[(node1, node2)]), 296 | else: 297 | print '%6s' % '/', 298 | print 299 | print 300 | self.pre_link_to_port = self.link_to_port.copy() 301 | 302 | if self.pre_access_table != self.access_table and setting.TOSHOW_topo: 303 | # It means the access_table has changed. 304 | print "\n----------------Access Host-------------------" 305 | print '%10s' % 'switch', '%10s' % 'port', '%22s' % 'Host' 306 | if not self.access_table.keys(): 307 | print " NO found host" 308 | else: 309 | for sw in sorted(self.access_table.keys()): 310 | print '%10d' % sw[0], '%10d ' % sw[1], self.access_table[sw] 311 | print 312 | self.pre_access_table = self.access_table.copy() 313 | 314 | # nx.draw(self.graph) 315 | # plt.savefig("/home/huangmc/exe/matplotlib/%d.png" % int(time.time())) 316 | -------------------------------------------------------------------------------- /network_monitor.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Huang MaChi at Chongqing University 2 | # of Posts and Telecommunications, Chongqing, China. 3 | # Copyright (C) 2016 Li Cheng at Beijing University of Posts 4 | # and Telecommunications. www.muzixing.com 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | from __future__ import division 20 | import copy 21 | from operator import attrgetter 22 | 23 | from ryu import cfg 24 | from ryu.base import app_manager 25 | from ryu.base.app_manager import lookup_service_brick 26 | from ryu.controller import ofp_event 27 | from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER 28 | from ryu.controller.handler import set_ev_cls 29 | from ryu.ofproto import ofproto_v1_3 30 | from ryu.lib import hub 31 | 32 | import setting 33 | 34 | 35 | CONF = cfg.CONF 36 | 37 | 38 | class NetworkMonitor(app_manager.RyuApp): 39 | """ 40 | NetworkMonitor is a Ryu app for collecting traffic information. 41 | """ 42 | OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] 43 | 44 | def __init__(self, *args, **kwargs): 45 | super(NetworkMonitor, self).__init__(*args, **kwargs) 46 | self.name = 'monitor' 47 | self.datapaths = {} 48 | self.port_stats = {} 49 | self.port_speed = {} 50 | self.stats = {} 51 | self.port_features = {} 52 | self.flow_num = {} # self.flow_num = {dpid:{port_no:fnum,},} 53 | self.free_bandwidth = {} # self.free_bandwidth = {dpid:{port_no:free_bw,},} unit:Kbit/s 54 | self.awareness = lookup_service_brick('awareness') 55 | self.graph = None 56 | self.best_paths = None 57 | 58 | # Start to green thread to monitor traffic and calculating 59 | # flow number of links respectively. 60 | self.monitor_thread = hub.spawn(self._monitor) 61 | self.save_fnum_thread = hub.spawn(self._save_fnum_graph) 62 | 63 | def _monitor(self): 64 | """ 65 | Main entry method of monitoring traffic. 66 | """ 67 | while CONF.weight == 'fnum': 68 | self.stats['port'] = {} 69 | for dp in self.datapaths.values(): 70 | self.port_features.setdefault(dp.id, {}) 71 | self._request_stats(dp) 72 | # Refresh data. 73 | self.best_paths = None 74 | hub.sleep(setting.MONITOR_PERIOD) 75 | if self.stats['port']: 76 | self.show_stat() 77 | hub.sleep(1) 78 | 79 | def _save_fnum_graph(self): 80 | """ 81 | Save flow number data into networkx graph object. 82 | """ 83 | while CONF.weight == 'fnum': 84 | self.graph = self.create_fnum_graph(self.flow_num) 85 | self.logger.debug("save flow number") 86 | self.create_bw_graph(self.graph, self.free_bandwidth) 87 | self.logger.debug("save free bandwidth") 88 | hub.sleep(setting.MONITOR_PERIOD) 89 | 90 | @set_ev_cls(ofp_event.EventOFPStateChange, 91 | [MAIN_DISPATCHER, DEAD_DISPATCHER]) 92 | def _state_change_handler(self, ev): 93 | """ 94 | Record datapath information. 95 | """ 96 | datapath = ev.datapath 97 | if ev.state == MAIN_DISPATCHER: 98 | if not datapath.id in self.datapaths: 99 | self.logger.debug('register datapath: %016x', datapath.id) 100 | self.datapaths[datapath.id] = datapath 101 | elif ev.state == DEAD_DISPATCHER: 102 | if datapath.id in self.datapaths: 103 | self.logger.debug('unregister datapath: %016x', datapath.id) 104 | del self.datapaths[datapath.id] 105 | else: 106 | pass 107 | 108 | @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER) 109 | def _flow_stats_reply_handler(self, ev): 110 | """ 111 | Calculate flow speed and Save it. 112 | Note: table-miss, LLDP and ARP flow entries are not what we need, just filter them. 113 | """ 114 | body = ev.msg.body 115 | dpid = ev.msg.datapath.id 116 | self.flow_num.setdefault(dpid, {}) 117 | for stat in sorted([flow for flow in body if (flow.priority not in [0, 65535])]): 118 | # Get flow's speed and record it. 119 | duration = self._get_time(stat.duration_sec, stat.duration_nsec) 120 | if duration < 0.1: 121 | duration = 0.1 122 | speed = float(stat.byte_count) / duration # unit: byte/s 123 | _speed = speed * 8.0 / (setting.MAX_CAPACITY * 1000) 124 | if _speed >= 0.05: 125 | self._save_fnum(dpid, stat.instructions[0].actions[0].port) 126 | 127 | @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER) 128 | def _port_stats_reply_handler(self, ev): 129 | """ 130 | Save port's stats information into self.port_stats. 131 | Calculate port speed and Save it. 132 | self.port_stats = {(dpid, port_no):[(tx_bytes, rx_bytes, rx_errors, duration_sec, duration_nsec),],} 133 | self.port_speed = {(dpid, port_no):[speed,],} 134 | Note: The transmit performance and receive performance are independent of a port. 135 | We calculate the load of a port only using tx_bytes. 136 | """ 137 | body = ev.msg.body 138 | dpid = ev.msg.datapath.id 139 | self.stats['port'][dpid] = body 140 | self.free_bandwidth.setdefault(dpid, {}) 141 | 142 | for stat in sorted(body, key=attrgetter('port_no')): 143 | port_no = stat.port_no 144 | if port_no != ofproto_v1_3.OFPP_LOCAL: 145 | key = (dpid, port_no) 146 | value = (stat.tx_bytes, stat.rx_bytes, stat.rx_errors, 147 | stat.duration_sec, stat.duration_nsec) 148 | self._save_stats(self.port_stats, key, value, 5) 149 | 150 | # Get port speed and Save it. 151 | pre = 0 152 | period = setting.MONITOR_PERIOD 153 | tmp = self.port_stats[key] 154 | if len(tmp) > 1: 155 | # Calculate only the tx_bytes, not the rx_bytes. (hmc) 156 | pre = tmp[-2][0] 157 | period = self._get_period(tmp[-1][3], tmp[-1][4], tmp[-2][3], tmp[-2][4]) 158 | speed = self._get_speed(self.port_stats[key][-1][0], pre, period) 159 | self._save_stats(self.port_speed, key, speed, 5) 160 | self._save_freebandwidth(dpid, port_no, speed) 161 | 162 | @set_ev_cls(ofp_event.EventOFPPortDescStatsReply, MAIN_DISPATCHER) 163 | def port_desc_stats_reply_handler(self, ev): 164 | """ 165 | Save port description info. 166 | """ 167 | msg = ev.msg 168 | dpid = msg.datapath.id 169 | ofproto = msg.datapath.ofproto 170 | 171 | config_dict = {ofproto.OFPPC_PORT_DOWN: "Down", 172 | ofproto.OFPPC_NO_RECV: "No Recv", 173 | ofproto.OFPPC_NO_FWD: "No Farward", 174 | ofproto.OFPPC_NO_PACKET_IN: "No Packet-in"} 175 | 176 | state_dict = {ofproto.OFPPS_LINK_DOWN: "Down", 177 | ofproto.OFPPS_BLOCKED: "Blocked", 178 | ofproto.OFPPS_LIVE: "Live"} 179 | 180 | ports = [] 181 | for p in ev.msg.body: 182 | ports.append('port_no=%d hw_addr=%s name=%s config=0x%08x ' 183 | 'state=0x%08x curr=0x%08x advertised=0x%08x ' 184 | 'supported=0x%08x peer=0x%08x curr_speed=%d ' 185 | 'max_speed=%d' % 186 | (p.port_no, p.hw_addr, 187 | p.name, p.config, 188 | p.state, p.curr, p.advertised, 189 | p.supported, p.peer, p.curr_speed, 190 | p.max_speed)) 191 | 192 | if p.config in config_dict: 193 | config = config_dict[p.config] 194 | else: 195 | config = "up" 196 | 197 | if p.state in state_dict: 198 | state = state_dict[p.state] 199 | else: 200 | state = "up" 201 | 202 | # Recording data. 203 | port_feature = (config, state, p.curr_speed) 204 | self.port_features[dpid][p.port_no] = port_feature 205 | 206 | @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER) 207 | def _port_status_handler(self, ev): 208 | """ 209 | Handle the port status changed event. 210 | """ 211 | msg = ev.msg 212 | ofproto = msg.datapath.ofproto 213 | reason = msg.reason 214 | dpid = msg.datapath.id 215 | port_no = msg.desc.port_no 216 | 217 | reason_dict = {ofproto.OFPPR_ADD: "added", 218 | ofproto.OFPPR_DELETE: "deleted", 219 | ofproto.OFPPR_MODIFY: "modified", } 220 | 221 | if reason in reason_dict: 222 | print "switch%d: port %s %s" % (dpid, reason_dict[reason], port_no) 223 | else: 224 | print "switch%d: Illeagal port state %s %s" % (dpid, port_no, reason) 225 | 226 | def _request_stats(self, datapath): 227 | """ 228 | Sending request msg to datapath 229 | """ 230 | self.logger.debug('send stats request: %016x', datapath.id) 231 | ofproto = datapath.ofproto 232 | parser = datapath.ofproto_parser 233 | req = parser.OFPPortDescStatsRequest(datapath, 0) 234 | datapath.send_msg(req) 235 | req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY) 236 | datapath.send_msg(req) 237 | req = parser.OFPFlowStatsRequest(datapath) 238 | datapath.send_msg(req) 239 | 240 | def get_max_fnum_of_links(self, graph, path, max_fnum): 241 | """ 242 | Get flow number of path. 243 | """ 244 | _len = len(path) 245 | if _len > 1: 246 | max_flownum = max_fnum 247 | for i in xrange(_len-1): 248 | pre, curr = path[i], path[i+1] 249 | if 'fnum' in graph[pre][curr]: 250 | _fnum = graph[pre][curr]['fnum'] 251 | max_flownum = max(_fnum, max_flownum) 252 | else: 253 | continue 254 | return max_flownum 255 | else: 256 | return max_fnum 257 | 258 | def get_min_bw_of_links(self, graph, path, min_bw): 259 | """ 260 | Getting bandwidth of path. Actually, the mininum bandwidth 261 | of links is the path's bandwith, because it is the bottleneck of path. 262 | """ 263 | _len = len(path) 264 | if _len > 1: 265 | minimal_band_width = min_bw 266 | for i in xrange(_len-1): 267 | pre, curr = path[i], path[i+1] 268 | if 'bandwidth' in graph[pre][curr]: 269 | bw = graph[pre][curr]['bandwidth'] 270 | minimal_band_width = min(bw, minimal_band_width) 271 | else: 272 | continue 273 | return minimal_band_width 274 | else: 275 | return min_bw 276 | 277 | def get_best_path_by_fnum(self, graph, paths): 278 | """ 279 | Get best path by comparing paths. 280 | Note: This function is called in BFlows module. 281 | """ 282 | best_paths = copy.deepcopy(paths) 283 | fnum_of_paths = copy.deepcopy(paths) 284 | 285 | # Reset the fnum_of_paths data structure. 286 | for src in fnum_of_paths.keys(): 287 | for dst in fnum_of_paths[src].keys(): 288 | fnum_of_paths[src][dst] = {} 289 | 290 | # Calculate the flow number of each path and save it. 291 | for src in paths: 292 | for dst in paths[src]: 293 | if src == dst: 294 | best_paths[src][src] = [src] 295 | else: 296 | for path in paths[src][dst]: 297 | max_fnum = 0 298 | max_fnum = self.get_max_fnum_of_links(graph, path, max_fnum) 299 | fnum_of_paths[src][dst].setdefault(max_fnum, []) 300 | fnum_of_paths[src][dst][max_fnum].append(path) 301 | 302 | # Get the least flow number paths and find the lightest-load one of them. 303 | min_fnum = min(fnum_of_paths[src][dst].keys()) 304 | max_bw_of_paths = 0 305 | best_path = fnum_of_paths[src][dst][min_fnum][0] 306 | for path in fnum_of_paths[src][dst][min_fnum]: 307 | min_bw = setting.MAX_CAPACITY 308 | min_bw = self.get_min_bw_of_links(graph, path, min_bw) 309 | if min_bw > max_bw_of_paths: 310 | max_bw_of_paths = min_bw 311 | best_path = path 312 | best_paths[src][dst] = best_path 313 | 314 | self.best_paths = best_paths 315 | return best_paths 316 | 317 | def zero_dictionary(self, fnum_dict): 318 | for sw in fnum_dict.keys(): 319 | for port in fnum_dict[sw].keys(): 320 | fnum_dict[sw][port] = 0 321 | 322 | def create_fnum_graph(self, fnum_dict): 323 | """ 324 | Save flow number data into networkx graph object. 325 | self.flow_num = {dpid:{port_no:fnum,},} 326 | """ 327 | try: 328 | graph = self.awareness.graph 329 | link_to_port = self.awareness.link_to_port 330 | for link, port in link_to_port.items(): 331 | (src_dpid, dst_dpid) = link 332 | (src_port, dst_port) = port 333 | if fnum_dict.has_key(src_dpid) and fnum_dict[src_dpid].has_key(src_port): 334 | fnum = fnum_dict[src_dpid][src_port] 335 | # Add key-value pair of flow number into graph. 336 | if graph.has_edge(src_dpid, dst_dpid): 337 | graph[src_dpid][dst_dpid]['fnum'] = fnum 338 | else: 339 | graph.add_edge(src_dpid, dst_dpid) 340 | graph[src_dpid][dst_dpid]['fnum'] = fnum 341 | else: 342 | if graph.has_edge(src_dpid, dst_dpid): 343 | graph[src_dpid][dst_dpid]['fnum'] = 0 344 | else: 345 | graph.add_edge(src_dpid, dst_dpid) 346 | graph[src_dpid][dst_dpid]['fnum'] = 0 347 | # print 'fnum_dict:', fnum_dict 348 | # Zero the flow_num dictionary. 349 | self.zero_dictionary(fnum_dict) 350 | return graph 351 | except: 352 | self.logger.info("Create flow number graph exception") 353 | if self.awareness is None: 354 | self.awareness = lookup_service_brick('awareness') 355 | # print 'fnum_dict:', fnum_dict 356 | # Zero the flow_num dictionary. 357 | self.zero_dictionary(fnum_dict) 358 | return self.awareness.graph 359 | 360 | def _save_fnum(self, dpid, port_no): 361 | """ 362 | Record flow number of port. 363 | port_feature = (config, state, p.curr_speed) 364 | self.port_features[dpid][p.port_no] = port_feature 365 | self.flow_num = {dpid:{port_no:num,},} 366 | """ 367 | port_state = self.port_features.get(dpid).get(port_no) 368 | if port_state: 369 | self.flow_num[dpid].setdefault(port_no, 0) 370 | self.flow_num[dpid][port_no] += 1 371 | else: 372 | self.logger.info("Port is Down") 373 | 374 | def create_bw_graph(self, graph, bw_dict): 375 | """ 376 | Save bandwidth data into networkx graph object. 377 | """ 378 | try: 379 | link_to_port = self.awareness.link_to_port 380 | for link, port in link_to_port.items(): 381 | (src_dpid, dst_dpid) = link 382 | (src_port, dst_port) = port 383 | if src_dpid in bw_dict and dst_dpid in bw_dict: 384 | bandwidth = bw_dict[src_dpid][src_port] 385 | # Add key-value pair of bandwidth into graph. 386 | if graph.has_edge(src_dpid, dst_dpid): 387 | graph[src_dpid][dst_dpid]['bandwidth'] = bandwidth 388 | else: 389 | graph.add_edge(src_dpid, dst_dpid) 390 | graph[src_dpid][dst_dpid]['bandwidth'] = bandwidth 391 | else: 392 | if graph.has_edge(src_dpid, dst_dpid): 393 | graph[src_dpid][dst_dpid]['bandwidth'] = 0 394 | else: 395 | graph.add_edge(src_dpid, dst_dpid) 396 | graph[src_dpid][dst_dpid]['bandwidth'] = 0 397 | except: 398 | self.logger.info("Create bw graph exception") 399 | if self.awareness is None: 400 | self.awareness = lookup_service_brick('awareness') 401 | 402 | def _save_freebandwidth(self, dpid, port_no, speed): 403 | """ 404 | Calculate free bandwidth of port and Save it. 405 | port_feature = (config, state, p.curr_speed) 406 | self.port_features[dpid][p.port_no] = port_feature 407 | self.free_bandwidth = {dpid:{port_no:free_bw,},} 408 | """ 409 | port_state = self.port_features.get(dpid).get(port_no) 410 | if port_state: 411 | capacity = 10000 # The true bandwidth of link, instead of 'curr_speed'. 412 | free_bw = self._get_free_bw(capacity, speed) 413 | self.free_bandwidth[dpid].setdefault(port_no, None) 414 | self.free_bandwidth[dpid][port_no] = free_bw 415 | else: 416 | self.logger.info("Port is Down") 417 | 418 | def _save_stats(self, _dict, key, value, length=5): 419 | if key not in _dict: 420 | _dict[key] = [] 421 | _dict[key].append(value) 422 | if len(_dict[key]) > length: 423 | _dict[key].pop(0) 424 | 425 | def _get_free_bw(self, capacity, speed): 426 | # freebw: Kbit/s 427 | return max(capacity - speed * 8 / 1000.0, 0) 428 | 429 | def _get_speed(self, now, pre, period): 430 | if period: 431 | return (now - pre) / (period) 432 | else: 433 | return 0 434 | 435 | def _get_time(self, sec, nsec): 436 | return sec + nsec / 1000000000.0 437 | 438 | def _get_period(self, n_sec, n_nsec, p_sec, p_nsec): 439 | return self._get_time(n_sec, n_nsec) - self._get_time(p_sec, p_nsec) 440 | 441 | def show_stat(self): 442 | ''' 443 | Show statistics information. 444 | ''' 445 | if setting.TOSHOW_port_stat is False: 446 | return 447 | 448 | bodys = self.stats['port'] 449 | print('\ndatapath port ' 450 | ' rx-pkts rx-bytes '' tx-pkts tx-bytes ' 451 | ' port-bw(Kb/s) port-speed(b/s) port-freebw(Kb/s) ' 452 | ' port-state link-state') 453 | print('-------- ---- ' 454 | '--------- ----------- ''--------- ----------- ' 455 | '------------- --------------- ----------------- ' 456 | '---------- ----------') 457 | _format = '%8d %4x %9d %11d %9d %11d %13d %15.1f %17.1f %10s %10s' 458 | for dpid in sorted(bodys.keys()): 459 | for stat in sorted(bodys[dpid], key=attrgetter('port_no')): 460 | if stat.port_no != ofproto_v1_3.OFPP_LOCAL: 461 | print(_format % ( 462 | dpid, stat.port_no, 463 | stat.rx_packets, stat.rx_bytes, 464 | stat.tx_packets, stat.tx_bytes, 465 | setting.MAX_CAPACITY, 466 | abs(self.port_speed[(dpid, stat.port_no)][-1] * 8), 467 | self.free_bandwidth[dpid][stat.port_no], 468 | self.port_features[dpid][stat.port_no][0], 469 | self.port_features[dpid][stat.port_no][1])) 470 | print 471 | -------------------------------------------------------------------------------- /setting.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Huang MaChi at Chongqing University 2 | # of Posts and Telecommunications, Chongqing, China. 3 | # Copyright (C) 2016 Li Cheng at Beijing University of Posts 4 | # and Telecommunications. www.muzixing.com 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 | # implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | 19 | """ 20 | Common Setting for EFattree. 21 | """ 22 | 23 | DISCOVERY_PERIOD = 10 # For discovering topology. 24 | 25 | MONITOR_PERIOD = 5 # For monitoring traffic. 26 | 27 | TOSHOW_topo = True # For showing network topology in terminal 28 | TOSHOW_stat = True # For showing statistics in terminal 29 | TOSHOW_flow_stat = True # For showing flow statistics in terminal 30 | TOSHOW_port_stat = False # For showing port statistics in terminal 31 | 32 | enable_Flow_Entry_L4Port = False # For including L4 port in the installing flow entries or not. 33 | 34 | MAX_CAPACITY = 10000 # Max capacity of link. (kbit/s) 35 | 36 | get_topology_delay = 30 37 | --------------------------------------------------------------------------------