summaryrefslogtreecommitdiffhomepage
path: root/test/scenario_test/flow_spec_test.py
blob: 9d18aa47081db3d1a5477071b3ec33036e4441bd (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import sys
import time
import unittest

import nose

from lib.noseplugin import OptionParser, parser_option

from lib import base
from lib.base import BGP_FSM_ESTABLISHED, local
from lib.gobgp import GoBGPContainer
from lib.exabgp import ExaBGPContainer
from lib.yabgp import YABGPContainer


class FlowSpecTest(unittest.TestCase):

    """
    Test case for Flow Specification.
    """
    # +------------+            +------------+
    # | G1(GoBGP)  |---(iBGP)---| E1(ExaBGP) |
    # | 172.17.0.2 |            | 172.17.0.3 |
    # +------------+            +------------+
    #      |
    #    (iBGP)
    #      |
    # +------------+
    # | Y1(YABGP)  |
    # | 172.17.0.4 |
    # +------------+

    @classmethod
    def setUpClass(cls):
        gobgp_ctn_image_name = parser_option.gobgp_image
        base.TEST_PREFIX = parser_option.test_prefix

        cls.g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1',
                                ctn_image_name=gobgp_ctn_image_name,
                                log_level=parser_option.gobgp_log_level)

        cls.e1 = ExaBGPContainer(name='e1', asn=65000, router_id='192.168.0.2')

        cls.y1 = YABGPContainer(name='y1', asn=65000, router_id='192.168.0.3')

        ctns = [cls.g1, cls.e1, cls.y1]
        initial_wait_time = max(ctn.run() for ctn in ctns)
        time.sleep(initial_wait_time)

        # Add FlowSpec routes into GoBGP.
        cls.g1.add_route(
            route='ipv4/all',
            rf='ipv4-flowspec',
            matchs=[
                'destination 11.1.0.0/24',
                'source 11.2.0.0/24',
                "protocol '==tcp &=udp icmp >igmp >=egp <ipip <=rsvp !=gre'",
                "port '==80 &=90 8080 >9090 >=8180 <9190 <=8081 !=9091 &!443'",
                'destination-port 80',
                'source-port 8080',
                'icmp-type 0',
                'icmp-code 2',
                "tcp-flags '==S &=SA A !F !=U =!R'",
                'packet-length 100',
                'dscp 12',
                'fragment dont-fragment is-fragment+first-fragment',
            ],
            thens=['discard'])
        cls.g1.add_route(
            route='ipv6/dst/src/label',  # others are tested on IPv4
            rf='ipv6-flowspec',
            matchs=[
                'destination 2001:1::/64 10',
                'source 2001:2::/64 15',
                'label 12',
            ],
            thens=['discard'])

        cls.g1.add_peer(cls.e1, flowspec=True)
        cls.e1.add_peer(cls.g1, flowspec=True)
        cls.g1.add_peer(cls.y1, flowspec=True)
        cls.y1.add_peer(cls.g1, flowspec=True)

        cls.g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=cls.e1)
        cls.g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=cls.y1)

        # Add FlowSpec routes into ExaBGP.
        cls.e1.add_route(
            route='ipv4/all',
            rf='ipv4-flowspec',
            matchs=[
                'destination 12.1.0.0/24',
                'source 12.2.0.0/24',
                'protocol =tcp',
                'port >=80',
                'destination-port >5000',
                'source-port 8080',
                'icmp-type <1',
                'icmp-code <=2',
                "tcp-flags FIN",
                'packet-length >100&<200',
                'dscp 12',
                'fragment dont-fragment',
            ],
            thens=['discard'])
        cls.e1.add_route(
            route='ipv6/dst/src/protocol/label',  # others are tested on IPv4
            rf='ipv6-flowspec',
            matchs=[
                'destination 2002:1::/64/10',
                'source 2002:2::/64/15',
                'next-header udp',
                'flow-label >100',
            ],
            thens=['discard'])

        # Add FlowSpec routes into YABGP.
        cls.y1.add_route(
            route='ipv4/all',
            rf='ipv4-flowspec',
            matchs=[
                'destination 13.1.0.0/24',
                'source 13.2.0.0/24',
                "protocol =6|=17",
                # "port",  # not seem to be supported
                'destination-port =80',
                'source-port <8080|>9090',
                'icmp-type >=1',
                'icmp-code <2',
                # "tcp-flags",  # not seem to be supported via REST API
                'packet-length <=100',
                'dscp =12',
                # "fragment",  # not seem to be supported via REST API
            ],
            thens=['traffic-rate:0:0'])  # 'discard'
        # IPv6 FlowSpec: not supported with YABGP v0.4.0
        # cls.y1.add_route(
        #     route='ipv6/dst/src/label',  # others are tested on IPv4
        #     rf='ipv6-flowspec',
        #     matchs=[
        #         'destination 2003:1::/64/10',
        #         'source 2003:2::/64/15',
        #         'label 12',
        #     ],
        #     thens=['traffic-rate:0:0'])  # 'discard'

    def test_01_ipv4_exabgp_adj_rib_in(self):
        rib = self.e1.get_adj_rib_in(self.g1, rf='ipv4-flowspec')
        self.assertEqual(1, len(rib))
        nlri = list(rib)[0]  # advertised from G1(GoBGP)
        _exp_fmt = (
            # INPUTS:
            # 'destination 11.1.0.0/24',
            "destination-ipv4 11.1.0.0/24 "
            # 'source 11.2.0.0/24',
            "source-ipv4 11.2.0.0/24 "
            # "protocol '==tcp &=udp icmp >igmp >=egp <ipip <=rsvp !=gre'",
            "protocol [ =tcp&=udp =icmp >igmp >=egp <ipip <=rsvp !=gre ] "
            # "port '==80 &=90 8080 >9090 >=8180 <9190 <=8081 !=9091 &!443'",
            "port [ =80&=90 =8080 >9090 >=8180 <9190 <=8081 !=9091&!=443 ] "
            # 'destination-port 80',
            "destination-port =80 "
            # 'source-port 8080',
            "source-port =8080 "
            # 'icmp-type 0',
            "icmp-type =echo-reply "
            # 'icmp-code 2',
            "icmp-code =2 "
            # "tcp-flags '==S &=SA A !F !=U =!R'",
            "tcp-flags [ =syn&=%s ack !fin !=urgent !=rst ] "
            # 'packet-length 100',
            "packet-length =100 "
            # 'dscp 12',
            "dscp =12 "
            # 'fragment dont-fragment is-fragment+first-fragment',
            "fragment [ dont-fragment is-fragment+first-fragment ]"
        )
        # Note: Considers variants of SYN + ACK
        expected_list = (_exp_fmt % 'syn+ack', _exp_fmt % 'ack+syn')
        self.assertIn(nlri, expected_list)

    def test_02_ipv6_exabgp_adj_rib_in(self):
        rib = self.e1.get_adj_rib_in(self.g1, rf='ipv6-flowspec')
        self.assertEqual(1, len(rib))
        nlri = list(rib)[0]  # advertised from G1(GoBGP)
        expected = (
            # INPUTS:
            # 'destination 2001:1::/64 10',
            "destination-ipv6 2001:1::/64/10 "
            # 'source 2001:2::/64 15',
            "source-ipv6 2001:2::/64/15 "
            # 'label 12',
            "flow-label =12"
        )
        self.assertEqual(expected, nlri)

    def test_03_ipv4_yabgp_adj_rib_in(self):
        rib = self.y1.get_adj_rib_in(peer=self.g1, rf='flowspec')
        self.assertEqual(1, len(rib))
        nlri = list(rib)[0]  # advertised from G1(GoBGP)
        expected = (
            # INPUTS:
            # 'destination 11.1.0.0/24',
            '{"1": "11.1.0.0/24",'
            # 'source 11.2.0.0/24',
            ' "2": "11.2.0.0/24",'
            # "protocol '==tcp &=udp icmp >igmp >=egp <ipip <=rsvp !=gre'",
            ' "3": "=6&=17|=1|>2|>=8|<94|<=46|><47",'
            # "port '==80 &=90 8080 >9090 >=8180 <9190 <=8081 !=9091 &!443'",
            ' "4": "=80&=90|=8080|>9090|>=8180|<9190|<=8081|><9091&><443",'
            # 'destination-port 80',
            ' "5": "=80",'
            # 'source-port 8080',
            ' "6": "=8080",'
            # 'icmp-type 0',
            ' "7": "=0",'
            # 'icmp-code 2',
            ' "8": "=2",'
            # "tcp-flags '==S &=SA A !F !=U =!R'",
            ' "9": "=2&=18|16|>1|>=32|>=4",'
            # 'packet-length 100',
            ' "10": "=100",'
            # 'dscp 12',
            ' "11": "=12",'
            # 'fragment dont-fragment is-fragment+first-fragment',
            ' "12": "1|6"}'
        )
        self.assertEqual(expected, nlri)

    def test_04_ipv6_yabgp_adj_rib_in(self):
        # IPv6 FlowSpec: not supported with YABGP v0.4.0
        pass

    def test_05_ipv4_gobgp_global_rib(self):
        rib = self.g1.get_global_rib(rf='ipv4-flowspec')
        self.assertEqual(3, len(rib))
        output_nlri_list = [r['prefix'] for r in rib]
        nlri_g1 = (
            # INPUTS:
            # 'destination 11.1.0.0/24',
            "[destination: 11.1.0.0/24]"
            # 'source 11.2.0.0/24',
            "[source: 11.2.0.0/24]"
            # "protocol '==tcp &=udp icmp >igmp >=egp <ipip <=rsvp !=gre'",
            "[protocol: ==tcp&==udp ==icmp >igmp >=egp <ipip <=rsvp !=gre]"
            # "port '==80 &=90 8080 >9090 >=8180 <9190 <=8081 !=9091 &!443'",
            "[port: ==80&==90 ==8080 >9090 >=8180 <9190 <=8081 !=9091&!=443]"
            # 'destination-port 80',
            "[destination-port: ==80]"
            # 'source-port 8080',
            "[source-port: ==8080]"
            # 'icmp-type 0',
            "[icmp-type: ==0]"
            # 'icmp-code 2',
            "[icmp-code: ==2]"
            # "tcp-flags '==S &=SA A !F !=U =!R'",
            "[tcp-flags: =S&=SA A !F !=U !=R]"
            # 'packet-length 100',
            "[packet-length: ==100]"
            # 'dscp 12',
            "[dscp: ==12]"
            # 'fragment dont-fragment is-fragment+first-fragment',
            "[fragment: dont-fragment is-fragment+first-fragment]"
        )
        nlri_e1 = (
            # INPUTS:
            # 'destination 12.1.0.0/24',
            '[destination: 12.1.0.0/24]'
            # 'source 12.2.0.0/24',
            '[source: 12.2.0.0/24]'
            # 'protocol =tcp',
            '[protocol: ==tcp]'
            # 'port >=80',
            '[port: >=80]'
            # 'destination-port >5000',
            '[destination-port: >5000]'
            # 'source-port 8080',
            '[source-port: ==8080]'
            # 'icmp-type <1',
            '[icmp-type: <1]'
            # 'icmp-code <=2',
            '[icmp-code: <=2]'
            # "tcp-flags FIN",
            '[tcp-flags: F]'
            # 'packet-length >100&<200',
            '[packet-length: >100&<200]'
            # 'dscp 12',
            '[dscp: ==12]'
            # 'fragment dont-fragment',
            '[fragment: dont-fragment]'
        )
        nlri_y1 = (
            # INPUTS:
            # 'destination 13.1.0.0/24',
            "[destination: 13.1.0.0/24]"
            # 'source 13.2.0.0/24',
            "[source: 13.2.0.0/24]"
            # "protocol =6|=17",
            "[protocol: ==tcp ==udp]"
            # 'destination-port =80',
            "[destination-port: ==80]"
            # 'source-port <8080|>9090',
            "[source-port: <8080 >9090]"
            # 'icmp-type >=1',
            "[icmp-type: >=1]"
            # 'icmp-code <2',
            "[icmp-code: <2]"
            # 'packet-length <=100',
            "[packet-length: <=100]"
            # 'dscp =12',
            "[dscp: ==12]"
        )
        for nlri in [nlri_g1, nlri_e1, nlri_y1]:
            self.assertIn(nlri, output_nlri_list)

    def test_06_ipv6_gobgp_global_rib(self):
        rib = self.g1.get_global_rib(rf='ipv6-flowspec')
        import json
        self.assertEqual(2, len(rib), json.dumps(rib))
        output_nlri_list = [r['prefix'] for r in rib]
        nlri_g1 = (
            # INPUTS:
            # 'destination 2001:1::/64 10',
            "[destination: 2001:1::/64/10]"
            # 'source 2001:2::/64 15',
            "[source: 2001:2::/64/15]"
            # 'label 12',
            "[label: ==12]"
        )
        nlri_e1 = (
            # INPUTS:
            # 'destination 2002:1::/64/10',
            '[destination: 2002:1::/64/10]'
            # 'source 2002:2::/64/15',
            '[source: 2002:2::/64/15]'
            # 'next-header udp',
            '[protocol: ==udp]'
            # 'flow-label >100',
            '[label: >100]'
        )
        for nlri in [nlri_g1, nlri_e1]:
            self.assertIn(nlri, output_nlri_list)

    def test_07_ipv4_exabgp_delete_route(self):
        # Delete a route on E1(ExaBGP)
        self.e1.del_route(route='ipv4/all')
        time.sleep(1)
        # Test if the route is deleted or not
        rib = self.g1.get_adj_rib_in(peer=self.e1, rf='ipv4-flowspec')
        self.assertEqual(0, len(rib))

    def test_08_ipv6_exabgp_delete_route(self):
        # Delete a route on E1(ExaBGP)
        self.e1.del_route(route='ipv6/dst/src/protocol/label')
        time.sleep(1)
        # Test if the route is deleted or not
        rib = self.g1.get_adj_rib_in(peer=self.e1, rf='ipv6-flowspec')
        self.assertEqual(0, len(rib))

    def test_09_ipv4_yabgp_delete_route(self):
        # Delete a route on Y1(YABGP)
        self.y1.del_route(route='ipv4/all')
        time.sleep(1)
        # Test if the route is deleted or not
        rib = self.g1.get_adj_rib_in(peer=self.y1, rf='ipv4-flowspec')
        self.assertEqual(0, len(rib))

    def test_10_ipv6_yabgp_delete_route(self):
        # IPv6 FlowSpec: not supported with YABGP v0.4.0
        pass

    def test_11_ipv4_gobgp_delete_route(self):
        # Delete a route on G1(GoBGP)
        self.g1.del_route(route='ipv4/all')
        time.sleep(1)
        # Test if the route is deleted or not
        rib_e1 = self.e1.get_adj_rib_in(peer=self.g1, rf='ipv4-flowspec')
        self.assertEqual(0, len(rib_e1))
        rib_y1 = self.y1.get_adj_rib_in(peer=self.g1, rf='ipv4-flowspec')
        self.assertEqual(0, len(rib_y1))

    def test_12_ipv6_gobgp_delete_route(self):
        # Delete a route on G1(GoBGP)
        self.g1.del_route(route='ipv6/dst/src/label')
        time.sleep(1)
        # Test if the route is deleted or not
        rib_e1 = self.e1.get_adj_rib_in(peer=self.g1, rf='ipv6-flowspec')
        self.assertEqual(0, len(rib_e1))
        rib_y1 = self.y1.get_adj_rib_in(peer=self.g1, rf='ipv6-flowspec')
        self.assertEqual(0, len(rib_y1))


if __name__ == '__main__':
    output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
    if int(output) != 0:
        print("docker not found")
        sys.exit(1)

    nose.main(argv=sys.argv, addplugins=[OptionParser()],
              defaultTest=sys.argv[0])