aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/nat/test/test_nat66.py
blob: bd1b50b9ee2efa1d2dc37f9db3e7335cf9630283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3

import ipaddress
import random
import socket
import struct
import unittest
from io import BytesIO
from time import sleep

import scapy.compat
from framework import VppTestCase, VppTestRunner, running_extended_tests
from ipfix import IPFIX, Set, Template, Data, IPFIXDecoder
from scapy.all import bind_layers, Packet, ByteEnumField, ShortField, \
    IPField, IntField, LongField, XByteField, FlagsField, FieldLenField, \
    PacketListField
from scapy.data import IP_PROTOS
from scapy.layers.inet import IP, TCP, UDP, ICMP
from scapy.layers.inet import IPerror, TCPerror, UDPerror, ICMPerror
from scapy.layers.inet6 import ICMPv6DestUnreach, IPerror6, IPv6ExtHdrFragment
from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6EchoReply, \
    ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, fragment6
from scapy.layers.l2 import Ether, ARP, GRE
from scapy.packet import Raw
from syslog_rfc5424_parser import SyslogMessage, ParseError
from syslog_rfc5424_parser.constants import SyslogSeverity
from util import ip4_range
from util import ppc, ppp
from vpp_acl import AclRule, VppAcl, VppAclInterface
from vpp_ip_route import VppIpRoute, VppRoutePath
from vpp_neighbor import VppNeighbor
from vpp_papi import VppEnum


class MethodHolder(VppTestCase):
    """ NAT create capture and verify method holder """
    @property
    def config_flags(self):
        return VppEnum.vl_api_nat_config_flags_t


class TestNAT66(MethodHolder):
    """ NAT66 Test Cases """

    @classmethod
    def setUpClass(cls):
        super(TestNAT66, cls).setUpClass()

        cls.nat_addr = 'fd01:ff::2'

        cls.create_pg_interfaces(range(2))
        cls.interfaces = list(cls.pg_interfaces)

        for i in cls.interfaces:
            i.admin_up()
            i.config_ip6()
            i.configure_ipv6_neighbors()

    @classmethod
    def tearDownClass(cls):
        super(TestNAT66, cls).tearDownClass()

    def test_static(self):
        """ 1:1 NAT66 test """
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat66_add_del_interface(is_add=1, flags=flags,
                                          sw_if_index=self.pg0.sw_if_index)
        self.vapi.nat66_add_del_interface(is_add=1,
                                          sw_if_index=self.pg1.sw_if_index)
        self.vapi.nat66_add_del_static_mapping(
            local_ip_address=self.pg0.remote_ip6,
            external_ip_address=self.nat_addr,
            is_add=1)

        # in2out
        pkts = []
        p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
             IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
             TCP())
        pkts.append(p)
        p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
             IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
             UDP())
        pkts.append(p)
        p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
             IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
             ICMPv6EchoRequest())
        pkts.append(p)
        p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
             IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
             GRE() / IP() / TCP())
        pkts.append(p)
        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(len(pkts))

        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, self.nat_addr)
                self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6)
                self.assert_packet_checksums_valid(packet)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

        # out2in
        pkts = []
        p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
             IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
             TCP())
        pkts.append(p)
        p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
             IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
             UDP())
        pkts.append(p)
        p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
             IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
             ICMPv6EchoReply())
        pkts.append(p)
        p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
             IPv6(src=self.pg1.remote_ip6, dst=self.nat_addr) /
             GRE() / IP() / TCP())
        pkts.append(p)
        self.pg1.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(len(pkts))
        for packet in capture:
            try:
                self.assertEqual(packet[IPv6].src, self.pg1.remote_ip6)
                self.assertEqual(packet[IPv6].dst, self.pg0.remote_ip6)
                self.assert_packet_checksums_valid(packet)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise

        sm = self.vapi.nat66_static_mapping_dump()
        self.assertEqual(len(sm), 1)
        self.assertEqual(sm[0].total_pkts, 8)

    def test_check_no_translate(self):
        """ NAT66 translate only when egress interface is outside interface """
        flags = self.config_flags.NAT_IS_INSIDE
        self.vapi.nat66_add_del_interface(is_add=1, flags=flags,
                                          sw_if_index=self.pg0.sw_if_index)
        self.vapi.nat66_add_del_interface(is_add=1, flags=flags,
                                          sw_if_index=self.pg1.sw_if_index)
        self.vapi.nat66_add_del_static_mapping(
            local_ip_address=self.pg0.remote_ip6,
            external_ip_address=self.nat_addr,
            is_add=1)

        # in2out
        p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
             IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6) /
             UDP())
        self.pg0.add_stream([p])
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg1.get_capture(1)
        packet = capture[0]
        try:
            self.assertEqual(packet[IPv6].src, self.pg0.remote_ip6)
            self.assertEqual(packet[IPv6].dst, self.pg1.remote_ip6)
        except:
            self.logger.error(ppp("Unexpected or invalid packet:", packet))
            raise

    def clear_nat66(self):
        """
        Clear NAT66 configuration.
        """
        interfaces = self.vapi.nat66_interface_dump()
        for intf in interfaces:
            self.vapi.nat66_add_del_interface(is_add=0, flags=intf.flags,
                                              sw_if_index=intf.sw_if_index)

        static_mappings = self.vapi.nat66_static_mapping_dump()
        for sm in static_mappings:
            self.vapi.nat66_add_del_static_mapping(
                local_ip_address=sm.local_ip_address,
                external_ip_address=sm.external_ip_address, vrf_id=sm.vrf_id,
                is_add=0)

    def tearDown(self):
        super(TestNAT66, self).tearDown()
        self.clear_nat66()

    def show_commands_at_teardown(self):
        self.logger.info(self.vapi.cli("show nat66 interfaces"))
        self.logger.info(self.vapi.cli("show nat66 static mappings"))


if __name__ == '__main__':
    unittest.main(testRunner=VppTestRunner)
class="p">[ii], sizeof (*t)); t->arc_head = arc_head; t->sw_if_index = arc_head ? sw_if_index[ii] : ~0; t->feature_bitmap = vnet_buffer (b[ii])->l2.feature_bitmap; t->ethertype = arc_head ? ethertype[ii] : 0; t->next_index = next[ii]; } } always_inline uword l2_in_out_feat_arc_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame, int is_output, vlib_node_registration_t * fa_node, int arc_head, int do_trace) { u32 n_left, *from; u16 nexts[VLIB_FRAME_SIZE], *next; u16 ethertypes[VLIB_FRAME_SIZE], *ethertype; vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index; l2_in_out_feat_arc_main_t *fam = &l2_in_out_feat_arc_main; u8 ip4_arc_index = fam->ip4_feat_arc_index[is_output]; u8 ip6_arc_index = fam->ip6_feat_arc_index[is_output]; u8 nonip_arc_index = fam->nonip_feat_arc_index[is_output]; u32 *next_node_indices = fam->feat_next_node_index[is_output]; from = vlib_frame_vector_args (frame); vlib_get_buffers (vm, from, bufs, frame->n_vectors); /* set the initial values for the current buffer the next pointers */ b = bufs; next = nexts; ethertype = ethertypes; sw_if_index = sw_if_indices; n_left = frame->n_vectors; CLIB_PREFETCH (next_node_indices, 2 * CLIB_CACHE_LINE_BYTES, LOAD); while (n_left > 3 * L2_FEAT_ARC_VEC_SIZE) { const int vec_sz = L2_FEAT_ARC_VEC_SIZE; /* prefetch next N buffers */ buffer_prefetch_xN (vec_sz, b + 2 * vec_sz); if (arc_head) { get_sw_if_index_xN (vec_sz, is_output, b, sw_if_index); get_ethertype_xN (vec_sz, is_output, b, ethertype); set_next_in_arc_head_xN (vec_sz, is_output, next_node_indices, b, sw_if_index, ethertype, ip4_arc_index, ip6_arc_index, nonip_arc_index, next); } else { set_next_in_arc_tail_xN (vec_sz, is_output, next_node_indices, b, next); } if (do_trace) maybe_trace_xN (vec_sz, arc_head, vm, node, b, sw_if_index, ethertype, next); next += vec_sz; b += vec_sz; sw_if_index += vec_sz; ethertype += vec_sz; n_left -= vec_sz; } while (n_left > 0) { const int vec_sz = 1; if (arc_head) { get_sw_if_index_xN (vec_sz, is_output, b, sw_if_index); get_ethertype_xN (vec_sz, is_output, b, ethertype); set_next_in_arc_head_xN (vec_sz, is_output, next_node_indices, b, sw_if_index, ethertype, ip4_arc_index, ip6_arc_index, nonip_arc_index, next); } else { set_next_in_arc_tail_xN (vec_sz, is_output, next_node_indices, b, next); } if (do_trace) maybe_trace_xN (vec_sz, arc_head, vm, node, b, sw_if_index, ethertype, next); next += vec_sz; b += vec_sz; sw_if_index += vec_sz; ethertype += vec_sz; n_left -= vec_sz; } vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); return frame->n_vectors; } VLIB_NODE_FN (l2_in_feat_arc_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP, &l2_in_feat_arc_node, 1, 1); else return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP, &l2_in_feat_arc_node, 1, 0); } VLIB_NODE_FN (l2_out_feat_arc_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP, &l2_out_feat_arc_node, 1, 1); else return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP, &l2_out_feat_arc_node, 1, 0); } VLIB_NODE_FN (l2_in_feat_arc_end_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP, &l2_in_feat_arc_end_node, 0, 1); else return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP, &l2_in_feat_arc_end_node, 0, 0); } VLIB_NODE_FN (l2_out_feat_arc_end_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP, &l2_out_feat_arc_end_node, 0, 1); else return l2_in_out_feat_arc_node_fn (vm, node, frame, IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP, &l2_out_feat_arc_end_node, 0, 0); } #ifndef CLIB_MARCH_VARIANT void vnet_l2_in_out_feat_arc_enable_disable (u32 sw_if_index, int is_output, int enable_disable) { if (is_output) l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_OUTPUT_FEAT_ARC, (u32) enable_disable); else l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_INPUT_FEAT_ARC, (u32) enable_disable); } #endif /* CLIB_MARCH_VARIANT */ /* *INDENT-OFF* */ VNET_FEATURE_ARC_INIT (l2_in_ip4_arc, static) = { .arc_name = "l2-input-ip4", .start_nodes = VNET_FEATURES ("l2-input-feat-arc"), .arc_index_ptr = &l2_in_out_feat_arc_main.ip4_feat_arc_index[IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP], }; VNET_FEATURE_ARC_INIT (l2_out_ip4_arc, static) = { .arc_name = "l2-output-ip4", .start_nodes = VNET_FEATURES ("l2-output-feat-arc"), .arc_index_ptr = &l2_in_out_feat_arc_main.ip4_feat_arc_index[IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP], }; VNET_FEATURE_ARC_INIT (l2_out_ip6_arc, static) = { .arc_name = "l2-input-ip6", .start_nodes = VNET_FEATURES ("l2-input-feat-arc"), .arc_index_ptr = &l2_in_out_feat_arc_main.ip6_feat_arc_index[IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP], }; VNET_FEATURE_ARC_INIT (l2_in_ip6_arc, static) = { .arc_name = "l2-output-ip6", .start_nodes = VNET_FEATURES ("l2-output-feat-arc"), .arc_index_ptr = &l2_in_out_feat_arc_main.ip6_feat_arc_index[IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP], }; VNET_FEATURE_ARC_INIT (l2_out_nonip_arc, static) = { .arc_name = "l2-input-nonip", .start_nodes = VNET_FEATURES ("l2-input-feat-arc"), .arc_index_ptr = &l2_in_out_feat_arc_main.nonip_feat_arc_index[IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP], }; VNET_FEATURE_ARC_INIT (l2_in_nonip_arc, static) = { .arc_name = "l2-output-nonip", .start_nodes = VNET_FEATURES ("l2-output-feat-arc"), .arc_index_ptr = &l2_in_out_feat_arc_main.nonip_feat_arc_index[IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP], }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_REGISTER_NODE (l2_in_feat_arc_node) = { .name = "l2-input-feat-arc", .vector_size = sizeof (u32), .format_trace = format_l2_in_feat_arc_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = ARRAY_LEN(l2_in_feat_arc_error_strings), .error_strings = l2_in_feat_arc_error_strings, }; VLIB_REGISTER_NODE (l2_out_feat_arc_node) = { .name = "l2-output-feat-arc", .vector_size = sizeof (u32), .format_trace = format_l2_out_feat_arc_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = ARRAY_LEN(l2_out_feat_arc_error_strings), .error_strings = l2_out_feat_arc_error_strings, }; VLIB_REGISTER_NODE (l2_in_feat_arc_end_node) = { .name = "l2-input-feat-arc-end", .vector_size = sizeof (u32), .format_trace = format_l2_in_feat_arc_trace, .sibling_of = "l2-input-feat-arc", }; VLIB_REGISTER_NODE (l2_out_feat_arc_end_node) = { .name = "l2-output-feat-arc-end", .vector_size = sizeof (u32), .format_trace = format_l2_out_feat_arc_trace, .sibling_of = "l2-output-feat-arc", }; VNET_FEATURE_INIT (l2_in_ip4_arc_end, static) = { .arc_name = "l2-input-ip4", .node_name = "l2-input-feat-arc-end", .runs_before = 0, /* not before any other features */ }; VNET_FEATURE_INIT (l2_out_ip4_arc_end, static) = { .arc_name = "l2-output-ip4", .node_name = "l2-output-feat-arc-end", .runs_before = 0, /* not before any other features */ }; VNET_FEATURE_INIT (l2_in_ip6_arc_end, static) = { .arc_name = "l2-input-ip6", .node_name = "l2-input-feat-arc-end", .runs_before = 0, /* not before any other features */ }; VNET_FEATURE_INIT (l2_out_ip6_arc_end, static) = { .arc_name = "l2-output-ip6", .node_name = "l2-output-feat-arc-end", .runs_before = 0, /* not before any other features */ }; VNET_FEATURE_INIT (l2_in_nonip_arc_end, static) = { .arc_name = "l2-input-nonip", .node_name = "l2-input-feat-arc-end", .runs_before = 0, /* not before any other features */ }; VNET_FEATURE_INIT (l2_out_nonip_arc_end, static) = { .arc_name = "l2-output-nonip", .node_name = "l2-output-feat-arc-end", .runs_before = 0, /* not before any other features */ }; /* *INDENT-ON* */ #ifndef CLIB_MARCH_VARIANT clib_error_t * l2_in_out_feat_arc_init (vlib_main_t * vm) { l2_in_out_feat_arc_main_t *mp = &l2_in_out_feat_arc_main; /* Initialize the feature next-node indexes */ feat_bitmap_init_next_nodes (vm, l2_in_feat_arc_end_node.index, L2INPUT_N_FEAT, l2input_get_feat_names (), mp->feat_next_node_index [IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP]); feat_bitmap_init_next_nodes (vm, l2_out_feat_arc_end_node.index, L2OUTPUT_N_FEAT, l2output_get_feat_names (), mp->feat_next_node_index [IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP]); return 0; } static int l2_has_features (u32 sw_if_index, int is_output) { int has_features = 0; l2_in_out_feat_arc_main_t *mp = &l2_in_out_feat_arc_main; has_features += vnet_have_features (mp->ip4_feat_arc_index[is_output], sw_if_index); has_features += vnet_have_features (mp->ip6_feat_arc_index[is_output], sw_if_index); has_features += vnet_have_features (mp->nonip_feat_arc_index[is_output], sw_if_index); return has_features > 0; } static int l2_is_output_arc (u8 arc_index) { l2_in_out_feat_arc_main_t *mp = &l2_in_out_feat_arc_main; int idx = IN_OUT_FEAT_ARC_OUTPUT_TABLE_GROUP; return (mp->ip4_feat_arc_index[idx] == arc_index || mp->ip6_feat_arc_index[idx] == arc_index || mp->nonip_feat_arc_index[idx] == arc_index); } static int l2_is_input_arc (u8 arc_index) { l2_in_out_feat_arc_main_t *mp = &l2_in_out_feat_arc_main; int idx = IN_OUT_FEAT_ARC_INPUT_TABLE_GROUP; return (mp->ip4_feat_arc_index[idx] == arc_index || mp->ip6_feat_arc_index[idx] == arc_index || mp->nonip_feat_arc_index[idx] == arc_index); } int vnet_l2_feature_enable_disable (const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes) { u8 arc_index = vnet_get_feature_arc_index (arc_name); if (arc_index == (u8) ~ 0) return VNET_API_ERROR_INVALID_VALUE; /* check the state before we tried to enable/disable */ int had_features = vnet_have_features (arc_index, sw_if_index); int ret = vnet_feature_enable_disable (arc_name, node_name, sw_if_index, enable_disable, feature_config, n_feature_config_bytes); if (ret) return ret; int has_features = vnet_have_features (arc_index, sw_if_index); if (had_features != has_features) { if (l2_is_output_arc (arc_index)) { vnet_l2_in_out_feat_arc_enable_disable (sw_if_index, 1, l2_has_features (sw_if_index, 1)); } if (l2_is_input_arc (arc_index)) { vnet_l2_in_out_feat_arc_enable_disable (sw_if_index, 0, l2_has_features (sw_if_index, 0)); } } return 0; } VLIB_INIT_FUNCTION (l2_in_out_feat_arc_init); #endif /* CLIB_MARCH_VARIANT */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */