def _pktParse(pkt): """ OpenFlow message parser used for logging @param pkt a packed packet. If it is an OpenFlow message, used for parsing @return parsed information as a string, error message if parsing failed """ if pkt is None: return "***NONE***" offset = 0 hdr = parse.of_header_parse(pkt[offset:]) if not hdr: res = "Could not parse OpenFlow header, pkt len: " + len(pkt) return res if hdr.length == 0: res = "Header length is zero" return res # Extract the raw message bytes rawmsg = pkt[offset : offset + hdr.length] msg = parse.of_message_parse(rawmsg) if not msg: res = "Could not parse message" return res else: return str(msg.show())
def _pktParse(pkt): """ OpenFlow message parser used for logging @param pkt a packed packet. If it is an OpenFlow message, used for parsing @return parsed information as a string, error message if parsing failed """ if pkt is None: return "***NONE***" offset = 0 hdr = parse.of_header_parse(pkt[offset:]) if not hdr: res = "Could not parse OpenFlow header, pkt len: " + len(pkt) return res if hdr.length == 0: res = "Header length is zero" return res # Extract the raw message bytes rawmsg = pkt[offset:offset + hdr.length] msg = parse.of_message_parse(rawmsg) if not msg: res = "Could not parse message" return res else: return str(msg.show())
def _hdrParse(pkt): """ OpenFlow header parser used for logging @param pkt a packed packet. If it is an OpenFlow message, used for parsing @return parsed information as a string, error message if parsing failed """ if pkt is None: return "***NONE***" hdr = parse.of_header_parse(pkt) if not hdr: res = "Could not parse OpenFlow header, pkt len: " + len(pkt) return res if hdr.length == 0: res = "Header length is zero" return res else: return str(hdr.show())
def addSwitch(parent, num, port=OFPORT, switch_features=None, nPorts=4): """ Start a fake switch and do handshake with FlowVisor and existing controllers @param parent parent must have logger (Logging object) @param num the number of this switch, starting at 0. Used for its name and dpid @param port TP port number the switch tries to connect. (Switch port on FlowVisor) @param switch_features ofp_features_reply instance @param nPorts the number of ports the switch has @return True if addSwitch was successful, False otherwise """ logprefix = "AddSwitch: " name = "switch" + str(num) dpid = num timeout=parent.timeout try: sw = fakedevice.FakeSwitch(name, host='localhost', port=port) except: parent.logger.error(logprefix + "Failed to add switch: " + name) return False parent.switches.append(sw) # Handshake with FlowVisor parent.logger.info(logprefix + "Initial handshake with FV and controllers") send_msg = message.hello().pack() sw.send(send_msg) sw.start() try: m = sw.recv_blocking(timeout=timeout) exp = send_msg if (m != exp): parent.logger.error(logprefix + "Expecting: hello") parent.logger.error(logprefix + "Raw Response : " + _b2a(m)) parent.logger.error(logprefix + "Parsed Response: " + _pktParse(m)) return False parent.logger.info(logprefix + "Got hello for " + name) m = sw.recv_blocking(timeout=timeout) exp = genFlowModFlush().pack() if (m != exp): parent.logger.error(logprefix + "Expecting: flow_mod with flush") parent.logger.error(logprefix + "Raw Response : " + _b2a(m)) parent.logger.error(logprefix + "Parsed Response: " + _pktParse(m)) return False parent.logger.info(logprefix + "Got flow_mod flush for " + name) m = sw.recv_blocking(timeout=timeout) exp = message.features_request().pack() if (m != exp): parent.logger.error(logprefix + "Expecting: features_request") parent.logger.error(logprefix + "Raw Response : " + _b2a(m)) parent.logger.error(logprefix + "Parsed Response: " + _pktParse(m)) return False parent.logger.info(logprefix + "Got features_request (from FV) for " + name) if switch_features == None: ports = [] for dataport in range(nPorts): ports.append(dataport) switch_features = genFeaturesReply(ports=ports, dpid=dpid) switch_features.header.xid = parse.of_header_parse(m).xid send_msg = switch_features.pack() sw.send(send_msg) parent.logger.info(logprefix + "Sent switch_features to flowvisor") # Handshake with fake controller for cont in parent.controllers: parent.logger.info(logprefix + "Waiting for features_request from fakeController") m = sw.recv_blocking(timeout=timeout) ref_type = parse.of_header_parse(m).type ref_xid = parse.of_header_parse(m).xid if (ref_type != ofp.OFPT_FEATURES_REQUEST): parent.logger.error(logprefix + "Failed to get features_request from fake controller " + \ " for new switch " + name) parent.logger.error(logprefix + "received" + str(ref_type)) return False parent.logger.info(logprefix + "Got features_request from fake controller for " + name) # Use the same switch_features as used for FlowVisor switch_features.header.xid = parse.of_header_parse(m).xid send_msg = switch_features.pack() sw.send(send_msg) parent.logger.info(logprefix + "Sent switch_features from " + name + " to fake controller") except (Exception),e : parent.logger.error(logprefix + "Failed: %s" %(str(e))) return False
def addSwitch(parent, num, port=OFPORT, switch_features=None, nPorts=4): """ Start a fake switch and do handshake with FlowVisor and existing controllers @param parent parent must have logger (Logging object) @param num the number of this switch, starting at 0. Used for its name and dpid @param port TP port number the switch tries to connect. (Switch port on FlowVisor) @param switch_features ofp_features_reply instance @param nPorts the number of ports the switch has @return True if addSwitch was successful, False otherwise """ logprefix = "AddSwitch: " name = "switch" + str(num) dpid = num timeout = parent.timeout try: sw = fakedevice.FakeSwitch(name, host='localhost', port=port) except: parent.logger.error(logprefix + "Failed to add switch: " + name) return False parent.switches.append(sw) # Handshake with FlowVisor parent.logger.info(logprefix + "Initial handshake with FV and controllers") send_msg = message.hello().pack() sw.send(send_msg) sw.start() try: m = sw.recv_blocking(timeout=timeout) exp = send_msg if (m != exp): parent.logger.error(logprefix + "Expecting: hello") parent.logger.error(logprefix + "Raw Response : " + _b2a(m)) parent.logger.error(logprefix + "Parsed Response: " + _pktParse(m)) return False parent.logger.info(logprefix + "Got hello for " + name) m = sw.recv_blocking(timeout=timeout) exp = genFlowModFlush().pack() if (m != exp): parent.logger.error(logprefix + "Expecting: flow_mod with flush") parent.logger.error(logprefix + "Raw Response : " + _b2a(m)) parent.logger.error(logprefix + "Parsed Response: " + _pktParse(m)) return False parent.logger.info(logprefix + "Got flow_mod flush for " + name) m = sw.recv_blocking(timeout=timeout) exp = message.features_request().pack() if (m != exp): parent.logger.error(logprefix + "Expecting: features_request") parent.logger.error(logprefix + "Raw Response : " + _b2a(m)) parent.logger.error(logprefix + "Parsed Response: " + _pktParse(m)) return False parent.logger.info(logprefix + "Got features_request (from FV) for " + name) if switch_features == None: ports = [] for dataport in range(nPorts): ports.append(dataport) switch_features = genFeaturesReply(ports=ports, dpid=dpid) switch_features.header.xid = parse.of_header_parse(m).xid send_msg = switch_features.pack() sw.send(send_msg) parent.logger.info(logprefix + "Sent switch_features to flowvisor") # Handshake with fake controller for cont in parent.controllers: parent.logger.info( logprefix + "Waiting for features_request from fakeController") m = sw.recv_blocking(timeout=timeout) ref_type = parse.of_header_parse(m).type ref_xid = parse.of_header_parse(m).xid if (ref_type != ofp.OFPT_FEATURES_REQUEST): parent.logger.error(logprefix + "Failed to get features_request from fake controller " + \ " for new switch " + name) parent.logger.error(logprefix + "received" + str(ref_type)) return False parent.logger.info( logprefix + "Got features_request from fake controller for " + name) # Use the same switch_features as used for FlowVisor switch_features.header.xid = parse.of_header_parse(m).xid send_msg = switch_features.pack() sw.send(send_msg) parent.logger.info(logprefix + "Sent switch_features from " + name + " to fake controller") except (Exception), e: parent.logger.error(logprefix + "Failed: %s" % (str(e))) return False