aboutsummaryrefslogtreecommitdiffstats
path: root/test/asf/test_session_sdl.py
blob: c03dc83ba1ea587f386a5a218d22b02a4d02fdca (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3

import unittest

from framework import VppTestCase
from asfframework import VppTestRunner, tag_fixme_vpp_workers
from ipaddress import IPv4Network, IPv6Network

from vpp_ip_route import (
    VppIpRoute,
    VppRoutePath,
    VppIpTable,
)

from vpp_papi import VppEnum


from vpp_session_sdl import VppSessionSdl
from vpp_session_sdl import SessionSdl


@tag_fixme_vpp_workers
class TestSessionSDL(VppTestCase):
    """Session SDL Test Case"""

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

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

    def setUp(self):
        super(TestSessionSDL, self).setUp()
        self.create_loopback_interfaces(2)

        table_id = 0

        for i in self.lo_interfaces:
            i.admin_up()

            if table_id != 0:
                tbl = VppIpTable(self, table_id)
                tbl.add_vpp_config()
                tbl = VppIpTable(self, table_id, is_ip6=1)
                tbl.add_vpp_config()

            i.set_table_ip4(table_id)
            i.set_table_ip6(table_id)
            i.config_ip4()
            i.config_ip6()
            table_id += 1

    def tearDown(self):
        for i in self.lo_interfaces:
            i.unconfig_ip4()
            i.set_table_ip4(0)
            i.unconfig_ip6()
            i.set_table_ip6(0)
            i.admin_down()
        self.loop0.remove_vpp_config()
        self.loop1.remove_vpp_config()
        super(TestSessionSDL, self).tearDown()

    def create_rule(self, rmt, action_index, tag):
        return SessionSdl(rmt=rmt, action_index=action_index, tag=tag)

    def apply_rules(self, rules, is_add, appns_index):
        r = VppSessionSdl(self, rules, is_add=is_add, appns_index=appns_index)
        r.add_vpp_config()

    def test_session_sdl_ip4(self):
        """Session SDL IP4 test"""

        self.vapi.session_enable_disable_v2(
            rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL
        )

        # Configure namespaces
        self.vapi.app_namespace_add_del_v4(
            namespace_id="0", sw_if_index=self.loop0.sw_if_index
        )
        self.vapi.app_namespace_add_del_v4(
            namespace_id="1", sw_if_index=self.loop1.sw_if_index
        )

        # Add inter-table routes
        uri = "tcp://" + self.loop0.local_ip4 + "/1234"
        server_cmd = "test echo server appns 0 fifo-size 4k " + "uri " + uri
        client_cmd = (
            "test echo client bytes 100000 appns 1 "
            + "fifo-size 4k "
            + "syn-timeout 2 uri "
            + uri
        )
        ip_t01 = VppIpRoute(
            self,
            self.loop1.local_ip4,
            32,
            [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=1)],
        )
        ip_t10 = VppIpRoute(
            self,
            self.loop0.local_ip4,
            32,
            [VppRoutePath("0.0.0.0", 0xFFFFFFFF, nh_table_id=0)],
            table_id=1,
        )
        ip_t01.add_vpp_config()
        ip_t10.add_vpp_config()

        # Start builtin server for ip4 on loop0 appns 0
        self.logger.info(self.vapi.cli(server_cmd))

        # Add session filter to block loop1 (client on loop1 appns 1)
        rules = []
        rules.append(
            self.create_rule(rmt=self.loop1.local_ip4 + "/32", action_index=0, tag="")
        )
        self.apply_rules(rules, is_add=1, appns_index=0)

        filter = self.vapi.session_sdl_v2_dump()
        self.assertEqual(filter[0].rmt, IPv4Network(self.loop1.local_ip4 + "/32"))

        # irrelevant rules - add 64k entries in one API call
        rules = []
        for i in range(255):
            for j in range(255):
                prefix = "10.1.{0}.{1}/32".format(i, j)
                rules.append(self.create_rule(rmt=prefix, action_index=0, tag=""))
        self.apply_rules(rules, is_add=1, appns_index=0)

        error = self.vapi.cli_return_response(client_cmd)
        # Expecting an error because loop1 is blocked
        self.assertEqual(-1, error.retval)

        # Remove the session filter
        rules = []
        rules.append(
            self.create_rule(rmt=self.loop1.local_ip4 + "/32", action_index=0, tag="")
        )
        self.apply_rules(rules, is_add=0, appns_index=0)

        # Not expecting an error
        self.logger.info(self.vapi.cli(client_cmd))

        # Add a session filter not matching loop1
        rules = []
        rules.append(self.create_rule(rmt="172.100.1.0/24", action_index=0, tag=""))
        self.apply_rules(rules, is_add=1, appns_index=0)

        # Not expecting an error
        self.logger.info(self.vapi.cli(client_cmd))

        self.logger.info(self.vapi.cli(server_cmd + " stop"))

        self.vapi.app_namespace_add_del_v4(
            is_add=0, namespace_id="0", sw_if_index=self.loop0.sw_if_index
        )
        self.vapi.app_namespace_add_del_v4(
            is_add=0, namespace_id="1", sw_if_index=self.loop1.sw_if_index
        )
        # Delete inter-table routes
        ip_t01.remove_vpp_config()
        ip_t10.remove_vpp_config()
        self.vapi.session_enable_disable_v2(
            rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE
        )

    def test_session_sdl_ip6(self):
        """Session SDL IP6 test"""

        self.vapi.session_enable_disable_v2(
            rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL
        )

        # Configure namespaces
        self.vapi.app_namespace_add_del_v4(
            namespace_id="0", sw_if_index=self.loop0.sw_if_index
        )
        self.vapi.app_namespace_add_del_v4(
            namespace_id="1", sw_if_index=self.loop1.sw_if_index
        )

        # IP6 Test
        # Add inter-table routes
        uri = "tcp://" + self.loop0.local_ip6 + "/1235"
        client_cmd = (
            "test echo client bytes 100000 appns 1 "
            + "fifo-size 4k "
            + "syn-timeout 2 uri "
            + uri
        )
        server_cmd = "test echo server appns 0 fifo-size 4k " + "uri " + uri

        ip_t01 = VppIpRoute(
            self,
            self.loop1.local_ip6,
            128,
            [VppRoutePath("0::0", 0xFFFFFFFF, nh_table_id=1)],
        )
        ip_t10 = VppIpRoute(
            self,
            self.loop0.local_ip6,
            128,
            [VppRoutePath("0::0", 0xFFFFFFFF, nh_table_id=0)],
            table_id=1,
        )
        ip_t01.add_vpp_config()
        ip_t10.add_vpp_config()

        # Start builtin server for ip6 on loop0 appns 0
        self.logger.info(self.vapi.cli(server_cmd))

        # case 1: No filter

        # Not expecting an error
        self.logger.info(self.vapi.cli(client_cmd))

        # case 2: filter to block
        # Add session filter to block loop1, client appns 1
        rules = []
        rules.append(
            self.create_rule(rmt=self.loop1.local_ip6 + "/128", action_index=0, tag="")
        )
        self.apply_rules(rules, is_add=1, appns_index=0)
        filter = self.vapi.session_sdl_v2_dump()
        self.assertEqual(filter[0].rmt, IPv6Network(self.loop1.local_ip6 + "/128"))

        error = self.vapi.cli_return_response(client_cmd)
        # Expecting an error because loop1 is blocked
        self.assertEqual(-1, error.retval)

        # case 3: remove filter to unblock
        rules = []
        rules.append(
            self.create_rule(rmt=self.loop1.local_ip6 + "/128", action_index=0, tag="")
        )
        self.apply_rules(rules, is_add=0, appns_index=0)
        # Not expecting an error
        self.logger.info(self.vapi.cli(client_cmd))

        # stop the server
        self.logger.info(self.vapi.cli(server_cmd + " stop"))

        self.vapi.app_namespace_add_del_v4(
            is_add=0, namespace_id="0", sw_if_index=self.loop0.sw_if_index
        )
        self.vapi.app_namespace_add_del_v4(
            is_add=0, namespace_id="1", sw_if_index=self.loop1.sw_if_index
        )
        # Delete inter-table routes
        ip_t01.remove_vpp_config()
        ip_t10.remove_vpp_config()
        self.vapi.session_enable_disable_v2(
            rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE
        )

    def test_session_enable_disable(self):
        """Session SDL enable/disable test"""

        for i in range(10):
            # Enable sdl
            self.vapi.session_enable_disable_v2(
                rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL
            )

            # Disable
            self.vapi.session_enable_disable_v2(
                rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE
            )

            # Enable rule-table
            self.vapi.session_enable_disable_v2(
                rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_RULE_TABLE
            )

            # Disable
            self.vapi.session_enable_disable_v2(
                rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE
            )

            # Enable sdl
            self.vapi.session_enable_disable_v2(
                rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_SDL
            )

            # Disable
            self.vapi.session_enable_disable_v2(
                rt_engine_type=VppEnum.vl_api_rt_backend_engine_t.RT_BACKEND_ENGINE_API_DISABLE
            )


if __name__ == "__main__":
    unittest.main(testRunner=VppTestRunner)