def on_dpport_config(self, evt): """ Process DP Port Config event evt.msg: ryu.controller.Port """ if fibclog.dump_msg(): _LOG.debug("%s", evt.msg) try: port = fibcdbm.portmap().find_by_dp(dp_id=evt.dp_id, port_id=evt.port_id) if evt.enter: port["dpenter"] = True self.send_port_status_if_ready(port, "UP") else: self.send_port_status_if_ready(port, "DOWN") port["dpenter"] = False except KeyError as expt: _LOG.warn("dp port not registered. dpid:%d, port:%d", evt.dp_id, evt.port_id) except Exception as expt: _LOG.exception(expt)
def on_vsport_config(self, evt): """ Process VS Port Config event evt.msg: fibc.net.ffpacket.FFPacket """ pkt = evt.msg if fibclog.dump_msg(): _LOG.debug("%s", pkt) try: hw_addr = fibcdbm.dps().find_port(evt.vs_id, evt.port_id).hw_addr except: hw_addr = "00:00:00:00:00:{0:02X}".format(evt.port_id & 0xff) try: port = fibcdbm.portmap().find_by_name(re_id=pkt.re_id, name=pkt.ifname) if port.update_vs(evt.vs_id, evt.port_id, hw_addr): self.send_port_status_if_ready(port, "UP") except KeyError: _LOG.warn("vs port not registered. re_id:%s, ifname:%s", pkt.re_id, pkt.ifname) except Exception as expt: _LOG.exception(expt)
def on_port_config(self, evt): """ Process PortConfig event evt.msg: instance of pb.PortConfig """ msg = evt.msg if fibclog.dump_msg(): _LOG.debug("%s", msg) try: if msg.cmd == pb.PortConfig.ADD: self._on_port_config_add(msg) if msg.cmd == pb.PortConfig.MODIFY: self._on_port_config_add(msg) elif msg.cmd == pb.PortConfig.DELETE: hub.spawn_after(_PORT_CONFIG_DELETE_DELAY_SEC, self._on_port_config_del, msg) else: pass except KeyError: _LOG.warn("vm port not registered. re_id:%s, ifname:%s", msg.re_id, msg.ifname) except Exception as expt: _LOG.exception(expt)
def on_packet_in(self, evt): """ Process PacketIN event. """ try: if fibclog.dump_msg(): _LOG.debug("packet_in(%s)", evt.msg) msg = evt.msg dp_id = msg.datapath.id port_id = get_in_port(msg) pkt = packet.Packet(msg.data) ffpkt = pkt.get_protocol(ffpacket.FFPacket) if ffpkt is not None: _LOG.debug("%s, (%d, %d)", ffpkt, dp_id, port_id) ffpkt_evt = fibcevt.EventFIBCVsPortConfig( ffpkt, dp_id, port_id) self.send_event_to_observers(ffpkt_evt) else: _LOG.debug("PacketIN (%d, %d)", dp_id, port_id) if fibclog.dump_pkt(): _LOG.debug("%s", pkt) self.forward_pkt(pkt, dp_id, port_id) except Exception as expt: _LOG.exception(expt) hexdump(msg.data)
def on_port_config(self, evt): """ Process PortConfig event evt.msg: instance of pb.PortConfig """ msg = evt.msg if fibclog.dump_msg(): _LOG.debug("%s", msg) try: port = fibcdbm.portmap().find_by_name(re_id=msg.re_id, name=msg.ifname) if msg.cmd == pb.PortConfig.ADD: port.update_vm(msg.value) self.send_port_status_if_ready(port, "UP") elif msg.cmd == pb.PortConfig.DELETE: self.send_port_status_if_ready(port, "DOWN") port.update_vm(0) else: pass except KeyError: _LOG.warn("vm port not registered. re_id:%s, ifname:%s", msg.re_id, msg.ifname) except Exception as expt: _LOG.exception(expt)
def on_ff_port_mod(self, evt): """ Process PortMod event """ mod = evt.msg if fibclog.dump_msg(): _LOG.debug(mod) try: dpath, mode = fibcdbm.dps().find_by_id(mod.dp_id) func = ofc.port_mod(mode) func(dpath, mod, ofctl) except Exception as expt: _LOG.exception(expt)
def send_to_vm(self, mtype, msg, xid=0): """ Send message to ribc. """ cid = msg.re_id if fibclog.dump_msg(): _LOG.debug("%s %s %s %s", cid, mtype, msg, xid) try: data = msg.SerializeToString() client = self.clients[cid] self.send_to_monitor(mtype, data, xid) client.send_data(mtype, data, xid) except KeyError as err: _LOG.warn("client not exist. id:%s, %s", cid, err)
def on_group_mod(self, evt): """ Process GroupMod event """ mod = evt.msg if fibclog.dump_msg(): _LOG.debug(mod) try: dpath, mode = _find_dp_by_re_id(mod.re_id) if dpath is not None: fibccnv.conv_group(mod, fibcdbm.portmap()) func = ofc.group(mode, mod.g_type) func(dpath, mod, ofctl) sleep(_SEND_MOD_WAIT_SEC) except Exception as expt: _LOG.exception(expt)
def on_flow_mod(self, evt): """ Process FlowMod event """ mod = evt.msg if fibclog.dump_msg(): _LOG.debug("%s", mod) try: dpath, mode = fibcdbm.find_dp_by_re_id(mod.re_id) if dpath is not None: fibccnv.conv_flow(mod, fibcdbm.portmap()) func = ofc.flow(mode, mod.table) func(dpath, mod, ofctl) sleep(_SEND_MOD_WAIT_SEC) except Exception as expt: _LOG.exception(expt)
def on_connect(self, soc, addr): """ Receive and process message from clinet. - addr: (ip, port) """ _LOG.info("NewConnection %s", addr) hdr, data = fibcnet.read_fib_msg(soc) try: if hdr[0] == pb.HELLO: ctl = FIBCApiVmController(soc, data, self) elif hdr[0] == pb.FF_HELLO: ctl = FIBCApiDpController(soc, data, self) else: raise TypeError() if fibclog.dump_msg(): _LOG.debug("%s", ctl.hello) ctl.initialize() while True: try: hdr, data = fibcnet.read_fib_msg(soc) if hdr is None: _LOG.info("Disconnected %s", addr) break _LOG.debug("Recv %s", hdr) ctl.dispatch(hdr, data) except Exception as ex: # pylint: disable=broad-except _LOG.exception("%s", ex) ctl.finalize() except Exception as ex: # pylint: disable=broad-except _LOG.exception("Invalid message. %s %s", hdr, ex) finally: soc.close() _LOG.debug("Connection closed %s", addr)
def packetout(self, pkt, dp_id, port_id, strip_vlan=False): """ Send Packetout message. """ dpath = fibcdbm.dps().find_by_id(dp_id) actions = [dpath.ofproto_parser.OFPActionOutput(port_id)] if strip_vlan: actions.insert(0, dpath.ofproto_parser.OFPActionPopVlan()) msg = dpath.ofproto_parser.OFPPacketOut( datapath=dpath, buffer_id=dpath.ofproto.OFP_NO_BUFFER, in_port=dpath.ofproto.OFPP_ANY, actions=actions, data=pkt.data) if fibclog.dump_msg(): _LOG.debug("PacketOUT(%s)", msg) dpath.send_msg(msg)
def on_connect(self, soc, addr): """ Receive and process message from clinet. - addr: (ip, port) """ _LOG.info("NewConnection %s", addr) hdr, data = fibcnet.read_fib_msg(soc) if hdr is None or hdr[0] != pb.HELLO: _LOG.error("Invalid message. %s", hdr) soc.close() return hello = fibcapi.parse_hello(data) if fibclog.dump_msg(): _LOG.debug("%s", hello) if not self.clients.register(hello.re_id, soc, addr): _LOG.error("re_id:%s already exist", hello.re_id) soc.close() return vm_evt = fibcevt.EventFIBCVmConfig(hello, True) self.send_evt(vm_evt) while True: try: hdr, data = fibcnet.read_fib_msg(soc) if hdr is None: _LOG.info("Disconnected %s", addr) break self.dispatch(hdr, data) except Exception as ex: _LOG.exception("%s", ex) self.clients.unregister(hello.re_id) vm_evt = fibcevt.EventFIBCVmConfig(hello, False) self.send_evt(vm_evt)
def dispatch(self, hdr, data): """ Dispatch mmessage. """ mtype = hdr[0] if mtype == pb.FF_MULTIPART_REPLY: msg = fibcapi.parse_ff_multipart_reply(data) if msg.mp_type == pb.FFMultipart.PORT_DESC and msg.port_desc.internal: # pylint: disable=no-member self.ports = msg.port_desc.port # pylint: disable=no-member evt = fibcevt.EventFIBCEnterDP(self.dpath, True, msg.port_desc.port) # pylint: disable=no-member self._send_evt(evt) else: evt = fibcevt.EventFIBCMultipartReply( self.dpath, msg, fibcnet.get_fib_header_xid(hdr)) self._send_evt(evt) elif mtype == pb.FF_PACKET_IN: msg = fibcapi.parse_ff_packet_in(data) evt = fibcevt.EventFIBCPacketIn(self.dpath, msg, fibcnet.get_fib_header_xid(hdr)) self._send_evt(evt) elif mtype == pb.FF_PORT_STATUS: msg = fibcapi.parse_ff_port_status(data) msg = fibcryu.FFPortStatus(self.dpath, msg.reason, msg.port) # pylint: disable=no-member evt = fibcevt.EventFIBCFFPortStatus(msg) self._send_evt(evt) elif mtype == pb.FF_L2ADDR_STATUS: msg = fibcapi.parse_ff_l2addr_status(data) evt = fibcevt.EventFIBCFFL2AddrStatus(self.dpath, msg) self._send_evt(evt) else: _LOG.warn("Unknown message dp_id:%d %s", self.dpath.id, hdr) if fibclog.dump_msg(): _LOG.debug("%s", data)
def on_port_map(self, evt): """ Process PortMap event """ if fibclog.dump_msg(): _LOG.debug("%s", evt.msg) tbl = evt.table cmd = evt.cmd msg = evt.msg if tbl == "dp": self._set_dpmap(cmd, msg) elif tbl == "port": self._set_portmap(cmd, msg) elif tbl == "idmap": self._set_idmap(cmd, msg) else: _LOG.error("invalid portmap table. %s", tbl)
def on_dpport_config(self, evt): """ Process DP Port Config event evt.msg: ryu.controller.Port """ if fibclog.dump_msg(): _LOG.debug("%s", evt.msg) dp_id = evt.dp_id port_id = evt.port_id link_up = (evt.state & 0x01) == 0 # 0x01:ofp.OFPPS_LINK_DOWN try: port = fibcdbm.portmap().find_by_dp(dp_id, port_id) vs_port = port["vs"] vs_dp, mode = fibcdbm.dps().find_by_id(vs_port.id) func = ofc.port_mod(mode) status = pb.PortStatus.UP if link_up else pb.PortStatus.DOWN mod = pb.FFPortMod( dp_id=vs_port.id, port_no=vs_port.port, hw_addr=port["vs_hw_addr"], status=status, ) func(vs_dp, mod, ofctl) return except KeyError as expt: _LOG.warn("dp port not registered. dpid:%d, port:%d", dp_id, port_id) except Exception as expt: _LOG.exception(expt)
def dispatch(self, hdr, data): """ Dispatch mmessage. """ mtype = hdr[0] if mtype == pb.PORT_CONFIG: port_conf = fibcapi.parse_port_config(data) port_conf_evt = fibcevt.EventFIBCPortConfig(port_conf) self._send_evt(port_conf_evt) elif mtype == pb.FLOW_MOD: flow_mod = fibcapi.parse_flow_mod(data) flow_mod_evt = fibcevt.EventFIBCFlowMod(flow_mod) self._send_evt(flow_mod_evt) elif mtype == pb.GROUP_MOD: group_mod = fibcapi.parse_group_mod(data) group_mod_evt = fibcevt.EventFIBCGroupMod(group_mod) self._send_evt(group_mod_evt) else: _LOG.warn("Unknown message %s", hdr) if fibclog.dump_msg(): _LOG.debug("%s", data)
def _on_packet_in(self, msg, dp_id, port_id): try: if fibclog.dump_msg(): _LOG.debug("packet_in(%s)", msg) ffpkt, vlan_hdr = _parse_pkt_hdr(msg.data) if ffpkt is not None: _LOG.debug("%s, (%d, %d)", ffpkt, dp_id, port_id) ffpkt_evt = fibcevt.EventFIBCVsPortConfig( ffpkt, dp_id, port_id) self.send_event_to_observers(ffpkt_evt) else: _LOG.debug("PacketIN (%d, %d)", dp_id, port_id) if fibclog.dump_pkt(): hexdump(msg.data) self.forward_pkt(vlan_hdr, msg.data, dp_id, port_id) except Exception as expt: _LOG.exception(expt) hexdump(msg.data)