def make_packet(pkt_size, outer_vlan=0, inner_vlan=0, vni=0, eth_src=None, eth_dst=None, udp_src_port=None, udp_dst_port=None, eth_type=None, ip_src=None, ip_dst=None, ip_proto=None): ip_src = IP_SRC if ip_src is None else ip_src ip_dst = IP_DST if ip_dst is None else ip_dst ip_proto = IP_PROTO if ip_proto is None else ip_proto eth_src = DL_SRC if eth_src is None else eth_src eth_dst = DL_SRC if eth_dst is None else eth_dst eth_type = ether.ETH_TYPE_IP if eth_type is None else eth_type udp_src_port = UDP_SRC_PORT if udp_src_port is None else udp_src_port udp_dst_port = UDP_DST_PORT if udp_dst_port is None else udp_dst_port if outer_vlan or inner_vlan: eth_type = ether.ETH_TYPE_8021Q e = ethernet.ethernet(eth_dst, eth_src, eth_type) i = ipv4.ipv4(total_length=0, src=ip_src, dst=ip_dst, proto=ip_proto, ttl=1) if vni: udp_dst_port = 4789 u = udp.udp(src_port=udp_src_port, dst_port=udp_dst_port) outer_len = 0 outer_tag = None if outer_vlan: outer_tag = vlan.vlan(vid=outer_vlan, ethertype=ether.ETH_TYPE_8021Q, cfi=1) outer_len = len(outer_tag) inner_len = 0 inner_tag = None if inner_vlan: inner_tag = vlan.vlan(vid=inner_vlan, ethertype=ether.ETH_TYPE_8021Q, cfi=1) inner_len = len(inner_tag) vxlan_len = 0 vxlan_tag = None if vni: vxlan_tag = vxlan.vxlan(vni) vxlan_len = len(vxlan_tag) payload_size = pkt_size - (len(e) + len(i) + len(u) + inner_len + outer_len + vxlan_len) payload = bytearray(payload_size if payload_size > 0 else 0) p = packet.Packet() p.add_protocol(e) if outer_tag: p.add_protocol(outer_tag) if inner_tag: p.add_protocol(inner_tag) p.add_protocol(i) p.add_protocol(u) if vxlan_tag: p.add_protocol(vxlan_tag) p.add_protocol(payload) return p
class Test_vxlan(unittest.TestCase): """ Test case for VXLAN (RFC 7348) header encoder/decoder class. """ vni = 0x123456 buf = ( b'\x08\x00\x00\x00' # flags = R|R|R|R|I|R|R|R (8 bits) b'\x12\x34\x56\x00' # vni = 0x123456 (24 bits) b'test_payload' # for test ) pkt = vxlan.vxlan(vni) jsondict = {'vxlan': {'vni': vni}} def test_init(self): eq_(self.vni, self.pkt.vni) def test_parser(self): parsed_pkt, next_proto_cls, rest_buf = vxlan.vxlan.parser(self.buf) eq_(self.vni, parsed_pkt.vni) eq_(ethernet.ethernet, next_proto_cls) eq_(b'test_payload', rest_buf) @raises(AssertionError) def test_invalid_flags(self): invalid_flags_bug = ( b'\x00\x00\x00\x00' # all bits are set to zero b'\x12\x34\x56\x00' # vni = 0x123456 (24 bits) ) vxlan.vxlan.parser(invalid_flags_bug) def test_serialize(self): serialized_buf = self.pkt.serialize(payload=None, prev=None) eq_(self.buf[:vxlan.vxlan._MIN_LEN], serialized_buf) def test_from_jsondict(self): pkt_from_json = vxlan.vxlan.from_jsondict( self.jsondict[vxlan.vxlan.__name__]) eq_(self.vni, pkt_from_json.vni) def test_to_jsondict(self): jsondict_from_pkt = self.pkt.to_jsondict() eq_(self.jsondict, jsondict_from_pkt) def test_vni_from_bin(self): vni = vxlan.vni_from_bin(b'\x12\x34\x56') eq_(self.vni, vni) def test_vni_to_bin(self): eq_(b'\x12\x34\x56', vxlan.vni_to_bin(self.vni))