def handle(self): desc = ofproto_protocol.ProtocolDesc() residue = b'' while True: if residue: data = residue residue = b'' else: data = self.request.recv(1024) if data == b'': break if self.verbose: print(data) h = ofproto_parser.header(data) if self.verbose: print(h) version, msg_type, msg_len, xid = h residue = data[msg_len:] desc.set_version(version=version) if msg_type == desc.ofproto.OFPT_HELLO: hello = desc.ofproto_parser.OFPHello(desc) hello.serialize() self.request.send(hello.buf) elif msg_type == desc.ofproto.OFPT_FLOW_MOD: self._add_msg_to_buf(data, msg_len) elif version == 4 and msg_type == desc.ofproto.OFPT_EXPERIMENTER: # This is for OF13 Ext-230 bundle # TODO: support bundle for OF>1.3 exp = desc.ofproto_parser.OFPExperimenter.parser( object(), version, msg_type, msg_len, xid, data) self._add_msg_to_buf(data, msg_len) if isinstance(exp, desc.ofproto_parser.ONFBundleCtrlMsg): ctrlrep = desc.ofproto_parser.ONFBundleCtrlMsg( desc, exp.bundle_id, exp.type + 1, 0, []) ctrlrep.xid = xid ctrlrep.serialize() self.request.send(ctrlrep.buf) elif msg_type == desc.ofproto.OFPT_BARRIER_REQUEST: brep = desc.ofproto_parser.OFPBarrierReply(desc) brep.xid = xid brep.serialize() self.request.send(brep.buf)
def parser(cls, buf): from os_ken.ofproto import ofproto_parser from os_ken.ofproto import ofproto_protocol (version, msg_type, msg_len, xid) = ofproto_parser.header(buf) msg_parser = ofproto_parser._MSG_PARSERS.get(version) if msg_parser is None: msg = OFPUnparseableMsg( None, version, msg_type, msg_len, xid, buf[cls._MIN_LEN:msg_len]) return cls(msg), cls, buf[msg_len:] datapath = ofproto_protocol.ProtocolDesc(version=version) try: msg = msg_parser(datapath, version, msg_type, msg_len, xid, buf[:msg_len]) except: msg = OFPUnparseableMsg( datapath, version, msg_type, msg_len, xid, buf[datapath.ofproto.OFP_HEADER_SIZE:msg_len]) return cls(msg), cls, buf[msg_len:]
def _test_msg(self, name, wire_msg, json_str): def bytes_eq(buf1, buf2): if buf1 != buf2: msg = 'EOF in either data' for i in range(0, min(len(buf1), len(buf2))): c1 = six.indexbytes(six.binary_type(buf1), i) c2 = six.indexbytes(six.binary_type(buf2), i) if c1 != c2: msg = 'differs at chr %d, %d != %d' % (i, c1, c2) break assert buf1 == buf2, "%r != %r, %s" % (buf1, buf2, msg) json_dict = json.loads(json_str) # on-wire -> OFPxxx -> json (version, msg_type, msg_len, xid) = ofproto_parser.header(wire_msg) try: has_parser, has_serializer = implemented[version][msg_type] except KeyError: has_parser = True has_serializer = True dp = ofproto_protocol.ProtocolDesc(version=version) if has_parser: try: msg = ofproto_parser.msg(dp, version, msg_type, msg_len, xid, wire_msg) json_dict2 = self._msg_to_jsondict(msg) except exception.OFPTruncatedMessage as e: json_dict2 = { 'OFPTruncatedMessage': self._msg_to_jsondict(e.ofpmsg) } # XXXdebug code open(('/tmp/%s.json' % name), 'w').write(json.dumps(json_dict2)) eq_(json_dict, json_dict2) if 'OFPTruncatedMessage' in json_dict2: return # json -> OFPxxx -> json xid = json_dict[list(json_dict.keys())[0]].pop('xid', None) msg2 = self._jsondict_to_msg(dp, json_dict) msg2.set_xid(xid) if has_serializer: msg2.serialize() eq_(self._msg_to_jsondict(msg2), json_dict) bytes_eq(wire_msg, msg2.buf) # check if "len" "length" fields can be omitted def _remove(d, names): f = lambda x: _remove(x, names) if isinstance(d, list): return list(map(f, d)) if isinstance(d, dict): d2 = {} for k, v in d.items(): if k in names: continue d2[k] = f(v) return d2 return d json_dict3 = _remove(json_dict, ['len', 'length']) msg3 = self._jsondict_to_msg(dp, json_dict3) msg3.set_xid(xid) msg3.serialize() bytes_eq(wire_msg, msg3.buf) msg2.serialize() bytes_eq(wire_msg, msg2.buf)
import socket from struct import * from nose.tools import * from os_ken.ofproto.ofproto_v1_3_parser import * from os_ken.ofproto import ofproto_v1_3_parser from os_ken.ofproto import ofproto_v1_3 from os_ken.ofproto import ofproto_protocol from os_ken.ofproto import ether from os_ken.ofproto.ofproto_parser import MsgBase from os_ken import utils from os_ken.lib import addrconv LOG = logging.getLogger('test_ofproto_v13') _Datapath = ofproto_protocol.ProtocolDesc(version=ofproto_v1_3.OFP_VERSION) class TestOFPMatch(unittest.TestCase): """ Test case for ofproto_v1_3_parser.OFPMatch """ def test_init(self): res = OFPMatch() # wc check eq_(res._wc.vlan_vid_mask, 0) # flow check eq_(res._flow.vlan_vid, 0)