summaryrefslogtreecommitdiffstats
path: root/test/test_mtu.py
blob: 922d83dc5ef7f1a76b33ba2d4cc33d413b31b8bc (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
197
198
199
200
201
#!/usr/bin/env python3
"""IP4 and IP6 MTU functional tests"""

#
# Add tests for:
# - sub interfaces
# - Verify that adjacencies inherit MTU correctly
# - Verify that sub-interfaces inherit MTU correctly
# - Different types of interfaces?
#
import unittest
from scapy.layers.inet6 import IPv6, Ether, IP, UDP, ICMPv6PacketTooBig
from scapy.layers.inet import ICMP
from framework import VppTestCase, VppTestRunner
from vpp_ip import DpoProto
from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto
from socket import AF_INET, AF_INET6, inet_pton
from util import reassemble4


""" Test_mtu is a subclass of VPPTestCase classes.
    MTU tests.
"""


class TestMTU(VppTestCase):
    """MTU Test Case"""

    maxDiff = None

    @classmethod
    def setUpClass(cls):
        super(TestMTU, cls).setUpClass()
        cls.create_pg_interfaces(range(2))
        cls.interfaces = list(cls.pg_interfaces)

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

    def setUp(self):
        super(TestMTU, self).setUp()
        for i in self.interfaces:
            i.admin_up()
            i.config_ip4()
            i.config_ip6()
            i.disable_ipv6_ra()
            i.resolve_arp()
            i.resolve_ndp()

    def tearDown(self):
        super(TestMTU, self).tearDown()
        if not self.vpp_dead:
            for i in self.pg_interfaces:
                i.unconfig_ip4()
                i.unconfig_ip6()
                i.admin_down()

    def validate(self, rx, expected):
        self.assertEqual(rx, expected.__class__(expected))

    def validate_bytes(self, rx, expected):
        self.assertEqual(rx, expected)

    def payload(self, len):
        return "x" * len

    def get_mtu(self, sw_if_index):
        rv = self.vapi.sw_interface_dump(sw_if_index=sw_if_index)
        for i in rv:
            if i.sw_if_index == sw_if_index:
                return i.mtu[0]
        return 0

    def test_ip4_mtu(self):
        """IP4 MTU test"""

        p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
        p_ip4 = IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, flags="DF")

        current_mtu = self.get_mtu(self.pg1.sw_if_index)

        p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 20 - 8)

        p4 = p_ether / p_ip4 / p_payload
        p4_reply = p_ip4 / p_payload
        p4_reply.ttl -= 1
        rx = self.send_and_expect(self.pg0, p4 * 11, self.pg1)
        for p in rx:
            self.validate(p[1], p4_reply)

        # MTU
        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
        self.assertEqual(576, self.get_mtu(self.pg1.sw_if_index))

        # Should fail. Too large MTU
        p_icmp4 = ICMP(
            type="dest-unreach",
            code="fragmentation-needed",
            nexthopmtu=576,
            chksum=0x2DBB,
        )
        icmp4_reply = (
            IP(src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, ttl=254, len=576, id=0)
            / p_icmp4
            / p_ip4
            / p_payload
        )
        n = icmp4_reply.__class__(icmp4_reply)
        s = bytes(icmp4_reply)
        icmp4_reply = s[0:576]
        rx = self.send_and_expect_some(self.pg0, p4 * 11, self.pg0)
        for p in rx:
            # p.show2()
            # n.show2()
            self.validate_bytes(bytes(p[1]), icmp4_reply)

        # Now with DF off. Expect fragments.
        # First go with 1500 byte packets.
        p_payload = UDP(sport=1234, dport=1234) / self.payload(1500 - 20 - 8)
        p4 = p_ether / p_ip4 / p_payload
        p4.flags = 0
        p4_reply = p_ip4 / p_payload
        p4_reply.ttl = p_ip4.ttl - 1
        p4_reply.flags = 0
        p4_reply.id = 256
        self.pg_enable_capture()
        self.pg0.add_stream(p4 * 1)
        self.pg_start()
        rx = self.pg1.get_capture(3)
        reass_pkt = reassemble4(rx)
        self.validate(reass_pkt, p4_reply)

        """
        # Now what happens with a 9K frame
        p_payload = UDP(sport=1234, dport=1234) / self.payload(
            current_mtu - 20 - 8)
        p4 = p_ether / p_ip4 / p_payload
        p4.flags = 0
        p4_reply = p_ip4 / p_payload
        p4_reply.ttl = 62 # check this
        p4_reply.flags = 0
        p4_reply.id = 512

        self.pg_enable_capture()
        self.pg0.add_stream(p4*1)
        self.pg_start()
        rx = self.pg1.get_capture(16)
        reass_pkt = reassemble4(rx)
        reass_pkt.show2()
        p4_reply.show2()
        self.validate(reass_pkt, p4_reply)
        """

        # Reset MTU
        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])

    def test_ip6_mtu(self):
        """IP6 MTU test"""

        current_mtu = self.get_mtu(self.pg1.sw_if_index)

        p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
        p_ip6 = IPv6(src=self.pg0.remote_ip6, dst=self.pg1.remote_ip6)

        p_payload = UDP(sport=1234, dport=1234) / self.payload(current_mtu - 40 - 8)

        p6 = p_ether / p_ip6 / p_payload
        p6_reply = p_ip6 / p_payload
        p6_reply.hlim -= 1
        rx = self.send_and_expect(self.pg0, p6 * 9, self.pg1)
        for p in rx:
            self.validate(p[1], p6_reply)

        # MTU (only checked on encap)
        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
        self.assertEqual(1280, self.get_mtu(self.pg1.sw_if_index))

        # Should fail. Too large MTU
        p_icmp6 = ICMPv6PacketTooBig(mtu=1280, cksum=0x4C7A)
        icmp6_reply = (
            IPv6(src=self.pg0.local_ip6, dst=self.pg0.remote_ip6, hlim=255, plen=1240)
            / p_icmp6
            / p_ip6
            / p_payload
        )
        icmp6_reply[2].hlim -= 1
        n = icmp6_reply.__class__(icmp6_reply)
        s = bytes(icmp6_reply)
        icmp6_reply_str = s[0:1280]

        rx = self.send_and_expect_some(self.pg0, p6 * 9, self.pg0)
        for p in rx:
            self.validate_bytes(bytes(p[1]), icmp6_reply_str)

        # Reset MTU
        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [current_mtu, 0, 0, 0])


if __name__ == "__main__":
    unittest.main(testRunner=VppTestRunner)
n class="kt">void mactime_send_create_entry_message (u8 * mac_address) { mactime_main_t *mm = &mactime_main; api_main_t *am; vl_shmem_hdr_t *shmem_hdr; u8 *name; vl_api_mactime_add_del_range_t *mp; am = &api_main; shmem_hdr = am->shmem_hdr; mp = vl_msg_api_alloc_as_if_client (sizeof (*mp)); clib_memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (VL_API_MACTIME_ADD_DEL_RANGE + mm->msg_id_base); name = format (0, "mac-%U", format_mac_address, mac_address); memcpy (mp->device_name, name, vec_len (name)); memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address)); /* $$$ config: create allow / drop / range */ mp->allow = 1; mp->is_add = 1; vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & mp); } /** Add or delete static / dynamic accept/drop configuration for a src mac */ static void vl_api_mactime_add_del_range_t_handler (vl_api_mactime_add_del_range_t * mp) { mactime_main_t *mm = &mactime_main; vl_api_mactime_add_del_range_reply_t *rmp; mactime_device_t *dp; clib_bihash_kv_8_8_t kv; int found = 1; clib_bihash_8_8_t *lut = &mm->lookup_table; int i, rv = 0; feature_init (mm); clib_memset (&kv, 0, sizeof (kv)); memcpy (&kv.key, mp->mac_address, sizeof (mp->mac_address)); /* See if we have a lookup table entry for this src mac address */ if (clib_bihash_search_8_8 (lut, &kv, &kv) < 0) found = 0; /* Add an entry? */ if (mp->is_add) { /* Create the device entry? */ if (found == 0) { pool_get (mm->devices, dp); clib_memset (dp, 0, sizeof (*dp)); vlib_validate_combined_counter (&mm->allow_counters, dp - mm->devices); vlib_zero_combined_counter (&mm->allow_counters, dp - mm->devices); vlib_validate_combined_counter (&mm->drop_counters, dp - mm->devices); vlib_zero_combined_counter (&mm->drop_counters, dp - mm->devices); mp->device_name[ARRAY_LEN (mp->device_name) - 1] = 0; dp->device_name = format (0, "%s%c", mp->device_name, 0); memcpy (dp->mac_address, mp->mac_address, sizeof (mp->mac_address)); for (i = 0; i < clib_net_to_host_u32 (mp->count); i++) { clib_timebase_range_t _r, *r = &_r; r->start = mp->ranges[i].start; r->end = mp->ranges[i].end; vec_add1 (dp->ranges, r[0]); } /* If we found some time ranges */ if (i) { /* Set allow/drop based on msg flags */ if (mp->drop) dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_DROP; if (mp->allow) dp->flags = MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW; } else { /* no ranges, it's a static allow/drop */ if (mp->drop) dp->flags = MACTIME_DEVICE_FLAG_STATIC_DROP; if (mp->allow) dp->flags = MACTIME_DEVICE_FLAG_STATIC_ALLOW; } /* Add the hash table entry */ kv.value = dp - mm->devices; clib_bihash_add_del_8_8 (lut, &kv, 1 /* is_add */ ); } else /* add more ranges */ { dp = pool_elt_at_index (mm->devices, kv.value); for (i = 0; i < clib_net_to_host_u32 (mp->count); i++) { clib_timebase_range_t _r, *r = &_r; r->start = mp->ranges[i].start; r->end = mp->ranges[i].end; vec_add1 (dp->ranges, r[0]); } } } else /* delete case */ { if (found == 0) { rv = VNET_API_ERROR_NO_SUCH_ENTRY; goto reply; } /* find the device entry */ dp = pool_elt_at_index (mm->devices, kv.value); /* Remove it from the lookup table */ clib_bihash_add_del_8_8 (lut, &kv, 0 /* is_add */ ); vec_free (dp->ranges); pool_put (mm->devices, dp); } reply: REPLY_MACRO (VL_API_MACTIME_ADD_DEL_RANGE_REPLY); } /* Set up the API message handling tables */ static clib_error_t * mactime_plugin_api_hookup (vlib_main_t * vm) { mactime_main_t *mm = &mactime_main; #define _(N,n) \ vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \ #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1); foreach_mactime_plugin_api_msg; #undef _ return 0; } #define vl_msg_name_crc_list #include <mactime/mactime_all_api_h.h> #undef vl_msg_name_crc_list static void setup_message_id_table (mactime_main_t * mm, api_main_t * am) { #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base); foreach_vl_msg_name_crc_mactime; #undef _ } static clib_error_t * mactime_init (vlib_main_t * vm) { mactime_main_t *mm = &mactime_main; clib_error_t *error = 0; u8 *name; mm->vlib_main = vm; mm->vnet_main = vnet_get_main (); name = format (0, "mactime_%08x%c", api_version, 0); /* Ask for a correctly-sized block of API message decode slots */ mm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE); error = mactime_plugin_api_hookup (vm); /* Add our API messages to the global name_crc hash table */ setup_message_id_table (mm, &api_main); vec_free (name); mm->lookup_table_num_buckets = MACTIME_NUM_BUCKETS; mm->lookup_table_memory_size = MACTIME_MEMORY_SIZE; mm->timezone_offset = -5; /* US EST / EDT */ return error; } VLIB_INIT_FUNCTION (mactime_init); static clib_error_t * mactime_config (vlib_main_t * vm, unformat_input_t * input) { mactime_main_t *mm = &mactime_main; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "lookup-table-buckets %u", &mm->lookup_table_num_buckets)) ; else if (unformat (input, "lookup-table-memory %U", unformat_memory_size, &mm->lookup_table_memory_size)) ; else if (unformat (input, "timezone_offset %d", &mm->timezone_offset)) ; else { return clib_error_return (0, "unknown input '%U'", format_unformat_error, input); } } return 0; } VLIB_CONFIG_FUNCTION (mactime_config, "mactime"); /* *INDENT-OFF* */ VNET_FEATURE_INIT (mactime, static) = { .arc_name = "device-input", .node_name = "mactime", .runs_before = VNET_FEATURES ("ethernet-input"), }; /* *INDENT-ON */ /* *INDENT-OFF* */ VNET_FEATURE_INIT (mactime_tx, static) = { .arc_name = "interface-output", .node_name = "mactime-tx", .runs_before = VNET_FEATURES ("interface-tx"), }; /* *INDENT-ON */ /* *INDENT-OFF* */ VLIB_PLUGIN_REGISTER () = { .version = VPP_BUILD_VER, .description = "Time-based MAC source-address filter", }; /* *INDENT-ON* */ static clib_error_t * show_mactime_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { mactime_main_t *mm = &mactime_main; mactime_device_t *dp; u8 *macstring = 0; char *status_string; u32 *pool_indices = 0; int verbose = 0; int current_status = 99; int i, j; f64 now; vlib_counter_t allow, drop; ethernet_arp_ip4_entry_t *n, *pool; vec_reset_length (mm->arp_cache_copy); pool = ip4_neighbors_pool (); /* *INDENT-OFF* */ pool_foreach (n, pool, ({ vec_add1 (mm->arp_cache_copy, n[0]); })); /* *INDENT-ON* */ now = clib_timebase_now (&mm->timebase); if (PREDICT_FALSE ((now - mm->sunday_midnight) > 86400.0 * 7.0)) mm->sunday_midnight = clib_timebase_find_sunday_midnight (now); if (unformat (input, "verbose %d", &verbose)) ; if (unformat (input, "verbose")) verbose = 1; if (verbose) vlib_cli_output (vm, "Time now: %U", format_clib_timebase_time, now); /* *INDENT-OFF* */ pool_foreach (dp, mm->devices, ({ vec_add1 (pool_indices, dp - mm->devices); })); /* *INDENT-ON* */ vlib_cli_output (vm, "%-15s %18s %14s %10s %10s %10s", "Device Name", "Addresses", "Status", "AllowPkt", "AllowByte", "DropPkt"); for (i = 0; i < vec_len (pool_indices); i++) { dp = pool_elt_at_index (mm->devices, pool_indices[i]); /* Check dynamic ranges */ for (j = 0; j < vec_len (dp->ranges); j++) { clib_timebase_range_t *r = dp->ranges + j; f64 start0, end0; start0 = r->start + mm->sunday_midnight; end0 = r->end + mm->sunday_midnight; if (verbose > 1) vlib_cli_output (vm, " Range %d: %U - %U", j, format_clib_timebase_time, start0, format_clib_timebase_time, end0); if (now >= start0 && now <= end0) { if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW) current_status = 3; else current_status = 2; if (verbose) { vlib_cli_output (vm, " Time in range %d:", j); vlib_cli_output (vm, " %U - %U", format_clib_timebase_time, start0, format_clib_timebase_time, end0); } goto print; } } if (verbose && j) vlib_cli_output (vm, " No range match."); if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_DROP) current_status = 0; if (dp->flags & MACTIME_DEVICE_FLAG_STATIC_ALLOW) current_status = 1; if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_ALLOW) current_status = 2; if (dp->flags & MACTIME_DEVICE_FLAG_DYNAMIC_DROP) current_status = 3; print: vec_reset_length (macstring); macstring = format (0, "%U", format_mac_address, dp->mac_address); switch (current_status) { case 0: status_string = "static drop"; break; case 1: status_string = "static allow"; break; case 2: status_string = "dynamic drop"; break; case 3: status_string = "dynamic allow"; break; default: status_string = "code bug!"; break; } vlib_get_combined_counter (&mm->allow_counters, dp - mm->devices, &allow); vlib_get_combined_counter (&mm->drop_counters, dp - mm->devices, &drop); vlib_cli_output (vm, "%-15s %18s %14s %10lld %10lld %10lld", dp->device_name, macstring, status_string, allow.packets, allow.bytes, drop.packets); /* This is really only good for small N... */ for (j = 0; j < vec_len (mm->arp_cache_copy); j++) { n = mm->arp_cache_copy + j; if (!memcmp (dp->mac_address, n->mac.bytes, sizeof (n->mac))) { vlib_cli_output (vm, "%17s%U", " ", format_ip4_address, &n->ip4_address); } } } vec_free (macstring); vec_free (pool_indices); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_mactime_command, static) = { .path = "show mactime", .short_help = "show mactime [verbose]", .function = show_mactime_command_fn, }; /* *INDENT-ON* */ static clib_error_t * clear_mactime_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { mactime_main_t *mm = &mactime_main; if (mm->feature_initialized == 0) return clib_error_return (0, "feature not enabled"); vlib_clear_combined_counters (&mm->allow_counters); vlib_clear_combined_counters (&mm->drop_counters); vlib_cli_output (vm, "Mactime counters cleared..."); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (clear_mactime_command, static) = { .path = "clear mactime", .short_help = "clear mactime counters", .function = clear_mactime_command_fn, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */