Esempio n. 1
0
    def test_recursive_address(self):
        af = VPPEnumType('vl_api_address_family_t',
                         [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                             "enumtype": "u32"
                         }])
        ip4 = VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8', 'length': 4})
        b = ip4.pack('1.1.1.1')
        self.assertEqual(len(b), 4)
        nt, size = ip4.unpack(b)

        self.assertEqual(str(nt), '1.1.1.1')

        ip6 = VPPTypeAlias('vl_api_ip6_address_t', {
            'type': 'u8',
            'length': 16
        })
        VPPUnionType(
            'vl_api_address_union_t',
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]])

        address = VPPType('vl_api_address_t',
                          [['vl_api_address_family_t', 'af'],
                           ['vl_api_address_union_t', 'un']])

        prefix = VPPType('vl_api_prefix_t',
                         [['vl_api_address_t', 'address'], ['u8', 'len']])
        message = VPPMessage('svs', [['vl_api_prefix_t', 'prefix']])
        message_addr = VPPMessage('svs_address',
                                  [['vl_api_address_t', 'address']])

        b = message_addr.pack({'address': "1::1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1::1", str(nt.address))
        b = message_addr.pack({'address': "1.1.1.1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1.1.1.1", str(nt.address))

        b = message.pack({'prefix': "1.1.1.0/24"})
        self.assertEqual(len(b), 21)
        nt, size = message.unpack(b)
        self.assertEqual("1.1.1.0/24", str(nt.prefix))

        message_array = VPPMessage('address_array',
                                   [['vl_api_ip6_address_t', 'addresses', 2]])
        b = message_array.pack({'addresses': [IPv6Address(u"1::1"), "2::2"]})
        self.assertEqual(len(b), 32)
        message_array_vla = VPPMessage(
            'address_array_vla',
            [['u32', 'num'], ['vl_api_ip6_address_t', 'addresses', 0, 'num']])
        b = message_array_vla.pack({'addresses': ["1::1", "2::2"], 'num': 2})
        self.assertEqual(len(b), 36)

        message_array4 = VPPMessage('address_array4',
                                    [['vl_api_ip4_address_t', 'addresses', 2]])
        b = message_array4.pack({'addresses': ["1.1.1.1", "2.2.2.2"]})
        self.assertEqual(len(b), 8)
        b = message_array4.pack(
            {'addresses': [IPv4Address(u"1.1.1.1"), "2.2.2.2"]})
        self.assertEqual(len(b), 8)

        message = VPPMessage('address', [['vl_api_address_t', 'address']])
        b = message.pack({'address': '1::1'})
        self.assertEqual(len(b), 20)
        b = message.pack({'address': '1.1.1.1'})
        self.assertEqual(len(b), 20)
        message = VPPMessage('prefix', [['vl_api_prefix_t', 'prefix']])
        b = message.pack({'prefix': '1::1/130'})
        self.assertEqual(len(b), 21)
        b = message.pack({'prefix': IPv6Network(u'1::/119')})
        self.assertEqual(len(b), 21)
        b = message.pack({'prefix': IPv4Network(u'1.1.0.0/16')})
        self.assertEqual(len(b), 21)
Esempio n. 2
0
    def test_abf(self):

        fib_mpls_label = VPPType(
            "vl_api_fib_mpls_label_t",
            [["u8", "is_uniform"], ["u32", "label"], ["u8", "ttl"],
             ["u8", "exp"]],
        )

        label_stack = {"is_uniform": 0, "label": 0, "ttl": 0, "exp": 0}

        b = fib_mpls_label.pack(label_stack)
        self.assertEqual(len(b), 7)

        fib_path = VPPType(
            "vl_api_fib_path_t",
            [
                ["u32", "sw_if_index"],
                ["u32", "table_id"],
                ["u8", "weight"],
                ["u8", "preference"],
                ["u8", "is_local"],
                ["u8", "is_drop"],
                ["u8", "is_udp_encap"],
                ["u8", "is_unreach"],
                ["u8", "is_prohibit"],
                ["u8", "is_resolve_host"],
                ["u8", "is_resolve_attached"],
                ["u8", "is_dvr"],
                ["u8", "is_source_lookup"],
                ["u8", "afi"],
                ["u8", "next_hop", 16],
                ["u32", "next_hop_id"],
                ["u32", "rpf_id"],
                ["u32", "via_label"],
                ["u8", "n_labels"],
                ["vl_api_fib_mpls_label_t", "label_stack", 16],
            ],
        )
        label_stack_list = []
        for i in range(16):
            label_stack_list.append(label_stack)

        paths = {
            "is_udp_encap": 0,
            "next_hop": b"\x10\x02\x02\xac",
            "table_id": 0,
            "afi": 0,
            "weight": 1,
            "next_hop_id": 4294967295,
            "label_stack": label_stack_list,
            "n_labels": 0,
            "sw_if_index": 4294967295,
            "preference": 0,
        }

        b = fib_path.pack(paths)
        self.assertEqual(len(b), (7 * 16) + 49)

        abf_policy = VPPType(
            "vl_api_abf_policy_t",
            [
                ["u32", "policy_id"],
                ["u32", "acl_index"],
                ["u8", "n_paths"],
                ["vl_api_fib_path_t", "paths", 0, "n_paths"],
            ],
        )

        policy = {
            "n_paths": 1,
            "paths": [paths],
            "acl_index": 0,
            "policy_id": 10
        }

        b = abf_policy.pack(policy)
        self.assertEqual(len(b), (7 * 16) + 49 + 9)

        abf_policy_add_del = VPPMessage(
            "abf_policy_add_del",
            [
                ["u16", "_vl_msg_id"],
                ["u32", "client_index"],
                ["u32", "context"],
                ["u8", "is_add"],
                ["vl_api_abf_policy_t", "policy"],
            ],
        )

        b = abf_policy_add_del.pack({
            "is_add": 1,
            "context": 66,
            "_vl_msg_id": 1066,
            "policy": policy
        })

        nt, size = abf_policy_add_del.unpack(b)
        self.assertEqual(
            nt.policy.paths[0].next_hop,
            b"\x10\x02\x02\xac\x00\x00\x00\x00"
            b"\x00\x00\x00\x00\x00\x00\x00\x00",
        )
Esempio n. 3
0
    def test_abf(self):

        fib_mpls_label = VPPType('vl_api_fib_mpls_label_t',
                                 [['u8', 'is_uniform'], ['u32', 'label'],
                                  ['u8', 'ttl'], ['u8', 'exp']])

        label_stack = {'is_uniform': 0, 'label': 0, 'ttl': 0, 'exp': 0}

        b = fib_mpls_label.pack(label_stack)
        self.assertEqual(len(b), 7)

        fib_path = VPPType(
            'vl_api_fib_path_t',
            [['u32', 'sw_if_index'], ['u32', 'table_id'], ['u8', 'weight'],
             ['u8', 'preference'], ['u8', 'is_local'], ['u8', 'is_drop'],
             ['u8', 'is_udp_encap'], ['u8', 'is_unreach'], [
                 'u8', 'is_prohibit'
             ], ['u8', 'is_resolve_host'], ['u8', 'is_resolve_attached'],
             ['u8', 'is_dvr'], ['u8', 'is_source_lookup'], ['u8', 'afi'],
             ['u8', 'next_hop', 16], ['u32', 'next_hop_id'], ['u32', 'rpf_id'],
             ['u32', 'via_label'], ['u8', 'n_labels'],
             ['vl_api_fib_mpls_label_t', 'label_stack', 16]])
        label_stack_list = []
        for i in range(16):
            label_stack_list.append(label_stack)

        paths = {
            'is_udp_encap': 0,
            'next_hop': b'\x10\x02\x02\xac',
            'table_id': 0,
            'afi': 0,
            'weight': 1,
            'next_hop_id': 4294967295,
            'label_stack': label_stack_list,
            'n_labels': 0,
            'sw_if_index': 4294967295,
            'preference': 0
        }

        b = fib_path.pack(paths)
        self.assertEqual(len(b), (7 * 16) + 49)

        abf_policy = VPPType(
            'vl_api_abf_policy_t',
            [['u32', 'policy_id'], ['u32', 'acl_index'], ['u8', 'n_paths'],
             ['vl_api_fib_path_t', 'paths', 0, 'n_paths']])

        policy = {
            'n_paths': 1,
            'paths': [paths],
            'acl_index': 0,
            'policy_id': 10
        }

        b = abf_policy.pack(policy)
        self.assertEqual(len(b), (7 * 16) + 49 + 9)

        abf_policy_add_del = VPPMessage(
            'abf_policy_add_del',
            [['u16', '_vl_msg_id'], ['u32', 'client_index'], [
                'u32', 'context'
            ], ['u8', 'is_add'], ['vl_api_abf_policy_t', 'policy']])

        b = abf_policy_add_del.pack({
            'is_add': 1,
            'context': 66,
            '_vl_msg_id': 1066,
            'policy': policy
        })

        nt, size = abf_policy_add_del.unpack(b)
        self.assertEqual(
            nt.policy.paths[0].next_hop, b'\x10\x02\x02\xac\x00\x00\x00\x00'
            b'\x00\x00\x00\x00\x00\x00\x00\x00')
Esempio n. 4
0
    def test_recursive_address(self):
        af = VPPEnumType(
            "vl_api_address_family_t",
            [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                "enumtype": "u32"
            }],
        )
        ip4 = VPPTypeAlias("vl_api_ip4_address_t", {"type": "u8", "length": 4})
        b = ip4.pack("1.1.1.1")
        self.assertEqual(len(b), 4)
        nt, size = ip4.unpack(b)

        self.assertEqual(str(nt), "1.1.1.1")

        ip6 = VPPTypeAlias("vl_api_ip6_address_t", {
            "type": "u8",
            "length": 16
        })
        VPPUnionType(
            "vl_api_address_union_t",
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]],
        )

        address = VPPType(
            "vl_api_address_t",
            [["vl_api_address_family_t", "af"],
             ["vl_api_address_union_t", "un"]],
        )

        prefix = VPPType("vl_api_prefix_t",
                         [["vl_api_address_t", "address"], ["u8", "len"]])
        message = VPPMessage("svs", [["vl_api_prefix_t", "prefix"]])
        message_addr = VPPMessage("svs_address",
                                  [["vl_api_address_t", "address"]])

        b = message_addr.pack({"address": "1::1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1::1", str(nt.address))
        b = message_addr.pack({"address": "1.1.1.1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1.1.1.1", str(nt.address))

        b = message.pack({"prefix": "1.1.1.0/24"})
        self.assertEqual(len(b), 21)
        nt, size = message.unpack(b)
        self.assertEqual("1.1.1.0/24", str(nt.prefix))

        message_array = VPPMessage("address_array",
                                   [["vl_api_ip6_address_t", "addresses", 2]])
        b = message_array.pack({"addresses": [IPv6Address("1::1"), "2::2"]})
        self.assertEqual(len(b), 32)
        message_array_vla = VPPMessage(
            "address_array_vla",
            [["u32", "num"], ["vl_api_ip6_address_t", "addresses", 0, "num"]],
        )
        b = message_array_vla.pack({"addresses": ["1::1", "2::2"], "num": 2})
        self.assertEqual(len(b), 36)

        message_array4 = VPPMessage("address_array4",
                                    [["vl_api_ip4_address_t", "addresses", 2]])
        b = message_array4.pack({"addresses": ["1.1.1.1", "2.2.2.2"]})
        self.assertEqual(len(b), 8)
        b = message_array4.pack(
            {"addresses": [IPv4Address("1.1.1.1"), "2.2.2.2"]})
        self.assertEqual(len(b), 8)

        message = VPPMessage("address", [["vl_api_address_t", "address"]])
        b = message.pack({"address": "1::1"})
        self.assertEqual(len(b), 20)
        b = message.pack({"address": "1.1.1.1"})
        self.assertEqual(len(b), 20)
        message = VPPMessage("prefix", [["vl_api_prefix_t", "prefix"]])
        b = message.pack({"prefix": "1::1/130"})
        self.assertEqual(len(b), 21)
        b = message.pack({"prefix": IPv6Network("1::/119")})
        self.assertEqual(len(b), 21)
        b = message.pack({"prefix": IPv4Network("1.1.0.0/16")})
        self.assertEqual(len(b), 21)