summaryrefslogtreecommitdiffstats
path: root/test/test_interface_crud.py
blob: 63917047ed400d8e14c88c30ee69d586ea48cf7b (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
#!/usr/bin/env python
"""CRUD tests of APIs (Create, Read, Update, Delete) HLD:

- interface up/down/add/delete - interface type:
    - pg (TBD)
    - loopback
    - vhostuser (TBD)
    - af_packet (TBD)
    - netmap (TBD)
    - tuntap (root privileges needed)
    - vxlan (TBD)
"""

import unittest

from scapy.layers.inet import IP, ICMP
from scapy.layers.l2 import Ether

from framework import VppTestCase, VppTestRunner


class TestLoopbackInterfaceCRUD(VppTestCase):
    """CRUD Loopback

    """

    @classmethod
    def setUpClass(cls):
        super(TestLoopbackInterfaceCRUD, cls).setUpClass()
        try:
            cls.create_pg_interfaces(range(1))
            for i in cls.pg_interfaces:
                i.config_ip4()
                i.resolve_arp()
        except:
            cls.tearDownClass()
            raise

    @staticmethod
    def create_icmp_stream(src_if, dst_ifs):
        """

        :param VppInterface src_if: Packets are send to this interface,
            using this interfaces remote host.
        :param list dst_ifs: IPv4 ICMP requests are send to interfaces
            addresses.
        :return: List of generated packets.
        """
        pkts = []
        for i in dst_ifs:
            p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
                 IP(src=src_if.remote_ip4, dst=i.local_ip4) /
                 ICMP(id=i.sw_if_index, type='echo-request'))
            pkts.append(p)
        return pkts

    def verify_icmp(self, capture, request_src_if, dst_ifs):
        """

        :param capture: Capture to verify.
        :param VppInterface request_src_if: Interface where was send packets.
        :param list dst_ifs: Interfaces where was generated IPv4 ICMP requests.
        """
        rcvd_icmp_pkts = []
        for pkt in capture:
            try:
                ip = pkt[IP]
                icmp = pkt[ICMP]
            except IndexError:
                pass
            else:
                info = (ip.src, ip.dst, icmp.type, icmp.id)
                rcvd_icmp_pkts.append(info)

        for i in dst_ifs:
            # 0 - icmp echo response
            info = (i.local_ip4, request_src_if.remote_ip4, 0, i.sw_if_index)
            self.assertIn(info, rcvd_icmp_pkts)

    def test_crud(self):
        # create
        loopbacks = self.create_loopback_interfaces(range(20))
        for i in loopbacks:
            i.local_ip4_prefix_len = 32
            i.config_ip4()
            i.admin_up()

        # read (check sw if dump, ip4 fib, ip6 fib)
        if_dump = self.vapi.sw_interface_dump()
        fib4_dump = self.vapi.ip_fib_dump()
        for i in loopbacks:
            self.assertTrue(i.is_interface_config_in_dump(if_dump))
            self.assertTrue(i.is_ip4_entry_in_fib_dump(fib4_dump))

        # check ping
        stream = self.create_icmp_stream(self.pg0, loopbacks)
        self.pg0.add_stream(stream)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        capture = self.pg0.get_capture(expected_count=len(stream))

        self.verify_icmp(capture, self.pg0, loopbacks)

        # delete
        for i in loopbacks:
            i.remove_vpp_config()

        # read (check not in sw if dump, ip4 fib, ip6 fib)
        if_dump = self.vapi.sw_interface_dump()
        fib4_dump = self.vapi.ip_fib_dump()
        for i in loopbacks:
            self.assertFalse(i.is_interface_config_in_dump(if_dump))
            self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))

        #  check not ping
        stream = self.create_icmp_stream(self.pg0, loopbacks)
        self.pg0.add_stream(stream)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.pg0.assert_nothing_captured()

    def test_down(self):
        # create
        loopbacks = self.create_loopback_interfaces(range(20))
        for i in loopbacks:
            i.local_ip4_prefix_len = 32
            i.config_ip4()
            i.admin_up()

        # disable
        for i in loopbacks:
            i.admin_down()
            i.unconfig_ip4()

        # read (check not in sw if dump, ip4 fib, ip6 fib)
        if_dump = self.vapi.sw_interface_dump()
        fib4_dump = self.vapi.ip_fib_dump()
        for i in loopbacks:
            self.assertTrue(i.is_interface_config_in_dump(if_dump))
            self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))

        #  check not ping
        stream = self.create_icmp_stream(self.pg0, loopbacks)
        self.pg0.add_stream(stream)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.pg0.assert_nothing_captured()


if __name__ == '__main__':
    unittest.main(testRunner=VppTestRunner)
ber incremented occasionally or always (if meticulous) */ u32 local_seq_number; /** remote sequence number */ u32 remote_seq_number; /** set to 1 if remote sequence number is known */ u8 remote_seq_number_known; /** current key ID sent out in bfd packet */ u8 curr_bfd_key_id; /** key ID to use when switched to next_key */ u8 next_bfd_key_id; /** * set to 1 if delayed action is pending, which might be activation * of authentication, change of key or deactivation */ u8 is_delayed; } auth; /** transport type for this session */ bfd_transport_e transport; /** union of transport-specific data */ union { bfd_udp_session_t udp; }; } bfd_session_t; /** * listener events */ #define foreach_bfd_listen_event(F) \ F (CREATE, "sesion-created") \ F (UPDATE, "session-updated") \ F (DELETE, "session-deleted") typedef enum { #define F(sym, str) BFD_LISTEN_EVENT_##sym, foreach_bfd_listen_event (F) #undef F } bfd_listen_event_e; /** * session nitification call back function type */ typedef void (*bfd_notify_fn_t) (bfd_listen_event_e, const bfd_session_t *); typedef struct { /** pool of bfd sessions context data */ bfd_session_t *sessions; /** timing wheel for scheduling timeouts */ timing_wheel_t wheel; /** timing wheel inaccuracy, in clocks */ u64 wheel_inaccuracy; /** hashmap - bfd session by discriminator */ u32 *session_by_disc; /** background process node index */ u32 bfd_process_node_index; /** convenience variables */ vlib_main_t *vlib_main; vnet_main_t *vnet_main; /** cpu clocks per second */ f64 cpu_cps; /** default desired min tx in clocks */ u64 default_desired_min_tx_clocks; /** minimum required min rx while echo function is active - clocks */ u64 min_required_min_rx_while_echo_clocks; /** for generating random numbers */ u32 random_seed; /** pool of authentication keys */ bfd_auth_key_t *auth_keys; /** hashmap - index in pool auth_keys by conf_key_id */ u32 *auth_key_by_conf_key_id; /** A vector of callback notification functions */ bfd_notify_fn_t *listeners; } bfd_main_t; extern bfd_main_t bfd_main; /** Packet counters */ #define foreach_bfd_error(F) \ F (NONE, "good bfd packets (processed)") \ F (BAD, "invalid bfd packets") \ F (DISABLED, "bfd packets received on disabled interfaces") typedef enum { #define F(sym, str) BFD_ERROR_##sym, foreach_bfd_error (F) #undef F BFD_N_ERROR, } bfd_error_t; /** bfd packet trace capture */ typedef struct { u32 len; u8 data[400]; } bfd_input_trace_t; enum { BFD_EVENT_RESCHEDULE = 1, BFD_EVENT_NEW_SESSION, BFD_EVENT_CONFIG_CHANGED, } bfd_process_event_e; /* *INDENT-OFF* */ /** echo packet structure */ typedef CLIB_PACKED (struct { /** local discriminator */ u32 discriminator; /** expire time of this packet - clocks */ u64 expire_time_clocks; /** checksum - based on discriminator, local secret and expire time */ u64 checksum; }) bfd_echo_pkt_t; /* *INDENT-ON* */ u8 *bfd_input_format_trace (u8 * s, va_list * args); bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_e t); void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs); bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx); bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc); void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs); void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx); int bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b); int bfd_verify_pkt_common (const bfd_pkt_t * pkt); int bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs); void bfd_event (bfd_main_t * bm, bfd_session_t * bs); void bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b, bfd_main_t * bm, bfd_session_t * bs, int is_local); u8 *format_bfd_session (u8 * s, va_list * args); u8 *format_bfd_auth_key (u8 * s, va_list * args); void bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down); unsigned bfd_auth_type_supported (bfd_auth_type_e auth_type); vnet_api_error_t bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id, u8 bfd_key_id, u8 is_delayed); vnet_api_error_t bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed); vnet_api_error_t bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs, u32 desired_min_tx_usec, u32 required_min_rx_usec, u8 detect_mult); u32 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks); const char *bfd_poll_state_string (bfd_poll_state_e state); #define USEC_PER_MS 1000LL #define USEC_PER_SECOND (1000 * USEC_PER_MS) /** default, slow transmission interval for BFD packets, per spec at least 1s */ #define BFD_DEFAULT_DESIRED_MIN_TX_USEC USEC_PER_SECOND /** * minimum required min rx set locally when echo function is used, per spec * should be set to at least 1s */ #define BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO USEC_PER_SECOND /** * Register a callback function to receive session notifications. */ void bfd_register_listener (bfd_notify_fn_t fn); #endif /* __included_bfd_main_h__ */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */