def switch_reactor(self, message, channel): import twink.ofp4.parse as ofp4p import twink.ofp4.build as ofp4b msg = ofp4p.parse(message) if msg.header.type==18 and msg.type==13: channel.send(ofp4b.ofp_multipart_reply(ofp4b.ofp_header(4, 19, None, msg.header.xid), 13, 0, ()))
def test_barrier_1(self): results = [] sc, ss = self.sc, self.ss serv = TestChannel(socket=ss) serv.start() sc.send(sc.recv(1024)) # echo back HELLO serv.recv() # consume HELLO cb1 = lambda *a: results.append(("cb1", a)) cb2 = lambda *a: results.append(("cb2", a)) echo = b.ofp_header(version=4,type=2,xid=1,length=8) serv.send(echo, callback=cb1) pkt = sc.recv(1024) b1 = p.parse(pkt[:8]) assert b1.header.type == ofp4.OFPT_BARRIER_REQUEST assert echo == pkt[8:], pkt[8:] echo = b.ofp_header(version=4,type=2,xid=2,length=8) serv.send(echo, callback=cb2) pkt = sc.recv(1024) b2 = p.parse(pkt[:8]) assert b2.header.type == ofp4.OFPT_BARRIER_REQUEST assert echo == pkt[8:], pkt[8:] echo = b.ofp_header(version=4,type=2,xid=3,length=8) serv.send(echo, callback=cb1) pkt = sc.recv(1024) b3 = p.parse(pkt[:8]) assert b3.header.type == ofp4.OFPT_BARRIER_REQUEST assert echo == pkt[8:], pkt[8:] sc.send(b.ofp_header(version=4, type=ofp4.OFPT_BARRIER_REPLY, xid=b1.header.xid, length=8)) sc.send(b.ofp_header(version=4,type=3,xid=1,length=8)) sc.send(b.ofp_header(version=4, type=ofp4.OFPT_BARRIER_REPLY, xid=b2.header.xid, length=8)) sc.send(b.ofp_header(version=4,type=3,xid=2,length=8)) sc.send(b.ofp_header(version=4, type=ofp4.OFPT_BARRIER_REPLY, xid=b3.header.xid, length=8)) sc.send(b.ofp_header(version=4,type=3,xid=3,length=8)) sc.close() serv.loop() assert [r[0] for r in results] == ["cb1", "cb2", "cb1"], [r[0] for r in results]
def switch_proc(message, channel): msg = p.parse(message) if msg.header.type == OFPT_FEATURES_REQUEST: channel.send(b.ofp_switch_features(b.ofp_header(4, OFPT_FEATURES_REPLY, 0, msg.header.xid), 1, 2, 3, 0, 0xF)) elif msg.header.type == OFPT_GET_CONFIG_REQUEST: channel.send(b.ofp_switch_config(b.ofp_header(4, OFPT_GET_CONFIG_REPLY, 0, msg.header.xid), 0, 0xffe5)) elif msg.header.type == OFPT_MULTIPART_REQUEST: if msg.type == OFPMP_FLOW: channel.send(b.ofp_multipart_reply(b.ofp_header(4, OFPT_MULTIPART_REPLY, 0, msg.header.xid), msg.type, 0, [ b.ofp_flow_stats(None, 0, 10, 20, 30, 40, 50, 0, 1, 2, 3, b.ofp_match(None, None, "".join([ oxm.build(None, oxm.OXM_OF_IN_PORT, None, None, 222), oxm.build(None, oxm.OXM_OF_ETH_SRC, None, None, binascii.a2b_hex("00112233"))])), [b.ofp_instruction_actions(OFPIT_APPLY_ACTIONS, None, [ b.ofp_action_output(None, None, 1111, OFPCML_MAX)]), ]),]))
def test_io(self): def handle(msg, ch): context["channel"] = ch # blocking server serv = twink.StreamServer(("localhost",0)) serv.channel_cls = TestChannel serv_thread = twink.sched.spawn(serv.start) context["stop"] = serv.stop ch = None socket = twink.sched.socket s = socket.create_connection(serv.server_address[:2]) ch = type("Client", (twink.OpenflowChannel, twink.LoggingChannel), {"accept_versions":[4,]})() ch.attach(s) msg = ch.recv() assert msg version, oftype, l, xid = twink.parse_ofp_header(msg) assert version==4 twink.sched.Event().wait(0.3) mon = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) mon.connect("unknown-%x.monitor" % id(context["channel"])) m = type("Client", (twink.OpenflowChannel, twink.LoggingChannel), {"accept_versions":[4,]})() m.attach(mon) assert p.parse(m.recv()).header.type == ofp4.OFPT_HELLO msg = b.ofp_header(version=4, type=2, xid=2, length=8) ch.send(msg) assert m.recv() == msg mon.close() ch.send(b.ofp_header(version=4, type=2, xid=3, length=8)) assert m.recv() == b"" ch.close() twink.sched.Event().wait(0.1) # wait for serv.loop reads the buffer serv.stop() serv_thread.join() assert context["loop_error"] is None, context["loop_error"]
def del_port(self, dev): for port_no, port_details in of_ports.iteritems(): if port_details.name == dev: ofp = of_ports[port_no] channel.send(b.ofp_port_status(b.ofp_header(4, OFPT_PORT_STATUS, 0, 0), OFPPR_DELETE, b.ofp_port(ofp.port_no, ofp.hw_addr, ofp.name, ofp.config, ofp.state, ofp.curr, ofp.advertised, ofp.supported, ofp.peer, ofp.curr_speed, ofp.max_speed ))) deinit_pktinout_port(dp, port_details.name) dp.destroy_port(dev) del of_ports[port_no] print 'Current OF ports:\n', of_ports return "Successfully deleted dev: %s which was ofport: %d" % (dev, port_no) return "Unable to locate dev: %s" % dev
def add_port(self, dev, mac): if (self.of_port_num + 1) == OFPP_LOCAL: return "Unable to add dev: %s. Reached max OF port_num" % dev self.of_port_num += 1 # Create vhost-user port ret = dp.create_port('vhost_user', dev) # Create corresponding pkt_in_out port ret, sock = init_pktinout_port(dp, dev) of_ports[self.of_port_num] = default_port._replace( port_no=self.of_port_num, hw_addr=binascii.a2b_hex(mac[:12]), name=dev, pkt_inout_socket=sock) ofp = of_ports[self.of_port_num] channel.send(b.ofp_port_status(b.ofp_header(4, OFPT_PORT_STATUS, 0, 0), OFPPR_ADD, b.ofp_port(ofp.port_no, ofp.hw_addr, ofp.name, ofp.config, ofp.state, ofp.curr, ofp.advertised, ofp.supported, ofp.peer, ofp.curr_speed, ofp.max_speed ))) print 'Current OF ports:\n', of_ports return "Successfully added dev: %s with MAC: %s as ofport:%d" % (dev, mac, self.of_port_num)
def switch_proc(message, ofchannel): msg = p.parse(message) # TODO: Acquire lock if msg.header.type == OFPT_FEATURES_REQUEST: channel.send(b.ofp_switch_features(b.ofp_header(4, OFPT_FEATURES_REPLY, 0, msg.header.xid), 1, 2, 3, 0, 0xF)) elif msg.header.type == OFPT_GET_CONFIG_REQUEST: channel.send(b.ofp_switch_config(b.ofp_header(4, OFPT_GET_CONFIG_REPLY, 0, msg.header.xid), 0, 0xffe5)) elif msg.header.type == OFPT_ROLE_REQUEST: channel.send(b.ofp_role_request(b.ofp_header(4, OFPT_ROLE_REPLY, 0, msg.header.xid), msg.role, msg.generation_id)) elif msg.header.type == OFPT_FLOW_MOD: if msg.cookie not in flows: flows[msg.cookie] = msg else: print "I already have this FlowMod: Cookie", \ msg.cookie, oxm.parse_list(flows[msg.cookie].match), (flows[msg.cookie].instructions) elif msg.header.type == OFPT_MULTIPART_REQUEST: if msg.type == OFPMP_FLOW: channel.send(b.ofp_multipart_reply(b.ofp_header(4, OFPT_MULTIPART_REPLY, 0, msg.header.xid), msg.type, 0, ["".join(b.ofp_flow_stats(None, f.table_id, 1, 2, f.priority, f.idle_timeout, f.hard_timeout, f.flags, f.cookie, 0, 0, f.match, f.instructions) for f in flows.itervalues())])) elif msg.type == OFPMP_PORT_DESC: channel.send(b.ofp_multipart_reply(b.ofp_header(4, OFPT_MULTIPART_REPLY, 0, msg.header.xid), msg.type, 0, ["".join(b.ofp_port(ofp.port_no, ofp.hw_addr, ofp.name, ofp.config, ofp.state, ofp.curr, ofp.advertised, ofp.supported, ofp.peer, ofp.curr_speed, ofp.max_speed) for ofp in of_ports.itervalues())])) elif msg.type == OFPMP_DESC: channel.send(b.ofp_multipart_reply(b.ofp_header(4, OFPT_MULTIPART_REPLY, 0, msg.header.xid), msg.type, 0, b.ofp_desc("UC Berkeley", "Intel Xeon", "BESS", "commit-6e343", None))) elif msg.header.type == OFPT_PACKET_OUT: index = msg.actions[0].port print "Packet out OF Port %d, Len:%d" % (index, len(msg.data)) sock = of_ports[index].pkt_inout_socket if sock is not None: sent = sock.send(msg.data) if sent != len(msg.data): print "Incomplete Transmission Sent:%d, Len:%d" % (sent, len(msg.data)) else: print "Packet out OF Port %d, Len:%d. Failed - Null socket" % (index, len(msg.data)) elif msg.header.type == OFPT_HELLO: pass elif msg.header.type == OFPT_SET_CONFIG: pass elif msg.header.type == OFPT_BARRIER_REQUEST: pass else: print msg assert 0