aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_srv6_mobile.py
blob: 9d39f194015c71ef7c552d2e64c950ae918a47d6 (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
#!/usr/bin/env python3

from framework import VppTestCase
from ipaddress import IPv4Address
from ipaddress import IPv6Address
from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto, VppIpTable

from vpp_srv6_mobile import (
    SRv6MobileNhtype,
    VppSRv6MobilePolicy,
    VppSRv6MobileLocalSID,
)

from scapy.contrib.gtp import *
from scapy.all import *


class TestSRv6EndMGTP4E(VppTestCase):
    """SRv6 End.M.GTP4.E (SRv6 -> GTP-U)"""

    @classmethod
    def setUpClass(cls):
        super(TestSRv6EndMGTP4E, cls).setUpClass()
        try:
            cls.create_pg_interfaces(range(2))
            cls.pg_if_i = cls.pg_interfaces[0]
            cls.pg_if_o = cls.pg_interfaces[1]

            cls.pg_if_i.config_ip6()
            cls.pg_if_o.config_ip4()

            cls.ip4_dst = cls.pg_if_o.remote_ip4
            # cls.ip4_src = cls.pg_if_o.local_ip4
            cls.ip4_src = "192.168.192.10"

            for pg_if in cls.pg_interfaces:
                pg_if.admin_up()
                pg_if.resolve_arp()

        except Exception:
            super(TestSRv6EndMGTP4E, cls).tearDownClass()
            raise

    def create_packets(self, inner):
        ip4_dst = IPv4Address(str(self.ip4_dst))
        # 32bit prefix + 32bit IPv4 DA + 8bit + 32bit TEID + 24bit
        dst = b"\xaa" * 4 + ip4_dst.packed + b"\x11" + b"\xbb" * 4 + b"\x11" * 3
        ip6_dst = IPv6Address(dst)

        ip4_src = IPv4Address(str(self.ip4_src))
        # 64bit prefix + 32bit IPv4 SA + 16 bit port + 16bit
        src = b"\xcc" * 8 + ip4_src.packed + b"\xdd" * 2 + b"\x11" * 2
        ip6_src = IPv6Address(src)

        self.logger.info("ip4 dst: {}".format(ip4_dst))
        self.logger.info("ip4 src: {}".format(ip4_src))
        self.logger.info("ip6 dst (remote srgw): {}".format(ip6_dst))
        self.logger.info("ip6 src (local  srgw): {}".format(ip6_src))

        pkts = list()
        for d, s in inner:
            pkt = (
                Ether()
                / IPv6(dst=str(ip6_dst), src=str(ip6_src))
                / IPv6ExtHdrSegmentRouting()
                / IPv6(dst=d, src=s)
                / UDP(sport=1000, dport=23)
            )
            self.logger.info(pkt.show2(dump=True))
            pkts.append(pkt)

        return pkts

    def test_srv6_mobile(self):
        """test_srv6_mobile"""
        pkts = self.create_packets([("A::1", "B::1"), ("C::1", "D::1")])

        # "sr localsid address {} behavior end.m.gtp4.e v4src_position 64 fib-table 0"
        # ".format(pkts[0]["IPv6"].dst)
        localsid = VppSRv6MobileLocalSID(
            self,
            # address params case is length 0
            localsid_prefix="{}/{}".format(pkts[0]["IPv6"].dst, 0),
            behavior="end.m.gtp4.e",
            v4src_position=64,
            fib_table=0,
        )
        localsid.add_vpp_config()

        # log the localsids
        self.logger.info(self.vapi.cli("show sr localsid"))

        self.vapi.cli("clear errors")

        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        self.logger.info(self.vapi.cli("show errors"))
        self.logger.info(self.vapi.cli("show int address"))

        capture = self.pg1.get_capture(len(pkts))

        for pkt in capture:
            self.logger.info(pkt.show2(dump=True))
            self.assertEqual(pkt[IP].dst, self.ip4_dst)
            self.assertEqual(pkt[IP].src, self.ip4_src)
            self.assertEqual(pkt[GTP_U_Header].teid, 0xBBBBBBBB)


class TestSRv6TMGTP4D(VppTestCase):
    """SRv6 T.M.GTP4.D (GTP-U -> SRv6)"""

    @classmethod
    def setUpClass(cls):
        super(TestSRv6TMGTP4D, cls).setUpClass()
        try:
            cls.create_pg_interfaces(range(2))
            cls.pg_if_i = cls.pg_interfaces[0]
            cls.pg_if_o = cls.pg_interfaces[1]

            cls.pg_if_i.config_ip4()
            cls.pg_if_i.config_ip6()
            cls.pg_if_o.config_ip4()
            cls.pg_if_o.config_ip6()

            cls.ip4_dst = "1.1.1.1"
            cls.ip4_src = "2.2.2.2"

            cls.ip6_dst = cls.pg_if_o.remote_ip6

            for pg_if in cls.pg_interfaces:
                pg_if.admin_up()
                pg_if.resolve_arp()
                pg_if.resolve_ndp(timeout=5)

        except Exception:
            super(TestSRv6TMGTP4D, cls).tearDownClass()
            raise

    def create_packets(self, inner):
        ip4_dst = IPv4Address(str(self.ip4_dst))

        ip4_src = IPv4Address(str(self.ip4_src))

        self.logger.info("ip4 dst: {}".format(ip4_dst))
        self.logger.info("ip4 src: {}".format(ip4_src))

        pkts = list()
        for d, s in inner:
            pkt = (
                Ether()
                / IP(dst=str(ip4_dst), src=str(ip4_src))
                / UDP(sport=2152, dport=2152)
                / GTP_U_Header(gtp_type="g_pdu", teid=200)
                / IPv6(dst=d, src=s)
                / UDP(sport=1000, dport=23)
            )
            self.logger.info(pkt.show2(dump=True))
            pkts.append(pkt)

        return pkts

    def test_srv6_mobile(self):
        """test_srv6_mobile"""
        pkts = self.create_packets([("A::1", "B::1"), ("C::1", "D::1")])

        self.vapi.cli("set sr encaps source addr A1::1")
        self.vapi.cli("sr policy add bsid D4:: next D2:: next D3::")

        # sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv6 fib-table 0 drop-in
        policy = VppSRv6MobilePolicy(
            self,
            bsid_addr="D5::",
            behavior="t.m.gtp4.d",
            sr_prefix="{}/{}".format("D4::", 32),
            v6src_prefix="{}/{}".format("C1::", 64),
            nhtype=SRv6MobileNhtype.SRV6_NHTYPE_API_IPV6,
            fib_table=0,
            drop_in=1,
        )
        policy.add_vpp_config()

        self.vapi.cli("sr steer l3 {}/32 via bsid D5::".format(self.ip4_dst))

        # "ip route add D2::/32 via {}".format(self.ip6_dst)
        route = VppIpRoute(
            self, "D2::", 32, [VppRoutePath(self.ip6_dst, self.pg1.sw_if_index)]
        )
        route.add_vpp_config()
        self.logger.info(self.vapi.cli("show ip6 fib"))
        self.logger.info(self.vapi.cli("show sr steer"))
        self.logger.info(self.vapi.cli("show sr policies"))

        self.vapi.cli("clear errors")

        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        self.logger.info(self.vapi.cli("show errors"))
        self.logger.info(self.vapi.cli("show int address"))

        capture = self.pg1.get_capture(len(pkts))

        for pkt in capture:
            self.logger.info(pkt.show2(dump=True))
            self.logger.info(
                "GTP4.D Address={}".format(
                    str(pkt[IPv6ExtHdrSegmentRouting].addresses[0])
                )
            )
            self.assertEqual(
                str(pkt[IPv6ExtHdrSegmentRouting].addresses[0]), "d4:0:101:101::c800:0"
            )


class TestSRv6EndMGTP6E(VppTestCase):
    """SRv6 End.M.GTP6.E"""

    @classmethod
    def setUpClass(cls):
        super(TestSRv6EndMGTP6E, cls).setUpClass()
        try:
            cls.create_pg_interfaces(range(2))
            cls.pg_if_i = cls.pg_interfaces[0]
            cls.pg_if_o = cls.pg_interfaces[1]

            cls.pg_if_i.config_ip6()
            cls.pg_if_o.config_ip6()

            cls.ip6_nhop = cls.pg_if_o.remote_ip6

            for pg_if in cls.pg_interfaces:
                pg_if.admin_up()
                pg_if.resolve_ndp(timeout=5)

        except Exception:
            super(TestSRv6EndMGTP6E, cls).tearDownClass()
            raise

    def create_packets(self, inner):
        # 64bit prefix + 8bit QFI + 32bit TEID + 24bit
        dst = b"\xaa" * 8 + b"\x00" + b"\xbb" * 4 + b"\x00" * 3
        ip6_dst = IPv6Address(dst)

        self.ip6_dst = ip6_dst

        src = b"\xcc" * 8 + b"\xdd" * 4 + b"\x11" * 4
        ip6_src = IPv6Address(src)

        self.ip6_src = ip6_src

        pkts = list()
        for d, s in inner:
            pkt = (
                Ether()
                / IPv6(dst=str(ip6_dst), src=str(ip6_src))
                / IPv6ExtHdrSegmentRouting(
                    segleft=1, lastentry=0, tag=0, addresses=["a1::1"]
                )
                / IPv6(dst=d, src=s)
                / UDP(sport=1000, dport=23)
            )
            self.logger.info(pkt.show2(dump=True))
            pkts.append(pkt)

        return pkts

    def test_srv6_mobile(self):
        """test_srv6_mobile"""
        pkts = self.create_packets([("A::1", "B::1"), ("C::1", "D::1")])

        # "sr localsid prefix {}/64 behavior end.m.gtp6.e fib-table 0"
        # .format(pkts[0]["IPv6"].dst)
        localsid = VppSRv6MobileLocalSID(
            self,
            localsid_prefix="{}/{}".format(pkts[0]["IPv6"].dst, 64),
            behavior="end.m.gtp6.e",
            fib_table=0,
        )
        localsid.add_vpp_config()

        # "ip route add a1::/64 via {}".format(self.ip6_nhop)
        route = VppIpRoute(
            self, "a1::", 64, [VppRoutePath(self.ip6_nhop, self.pg1.sw_if_index)]
        )
        route.add_vpp_config()
        self.logger.info(self.vapi.cli("show sr localsid"))

        self.vapi.cli("clear errors")

        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        self.logger.info(self.vapi.cli("show errors"))
        self.logger.info(self.vapi.cli("show int address"))

        capture = self.pg1.get_capture(len(pkts))

        for pkt in capture:
            self.logger.info(pkt.show2(dump=True))
            self.assertEqual(pkt[IPv6].dst, "a1::1")
            self.assertEqual(pkt[IPv6].src, str(self.ip6_src))
            self.assertEqual(pkt[GTP_U_Header].teid, 0xBBBBBBBB)


class TestSRv6EndMGTP6D(VppTestCase):
    """SRv6 End.M.GTP6.D"""

    @classmethod
    def setUpClass(cls):
        super(TestSRv6EndMGTP6D, cls).setUpClass()
        try:
            cls.create_pg_interfaces(range(2))
            cls.pg_if_i = cls.pg_interfaces[0]
            cls.pg_if_o = cls.pg_interfaces[1]

            cls.pg_if_i.config_ip6()
            cls.pg_if_o.config_ip6()

            cls.ip6_nhop = cls.pg_if_o.remote_ip6

            cls.ip6_dst = "2001::1"
            cls.ip6_src = "2002::1"

            for pg_if in cls.pg_interfaces:
                pg_if.admin_up()
                pg_if.resolve_ndp(timeout=5)

        except Exception:
            super(TestSRv6EndMGTP6D, cls).tearDownClass()
            raise

    def create_packets(self, inner):
        ip6_dst = IPv6Address(str(self.ip6_dst))

        ip6_src = IPv6Address(str(self.ip6_src))

        self.logger.info("ip6 dst: {}".format(ip6_dst))
        self.logger.info("ip6 src: {}".format(ip6_src))

        pkts = list()
        for d, s in inner:
            pkt = (
                Ether()
                / IPv6(dst=str(ip6_dst), src=str(ip6_src))
                / UDP(sport=2152, dport=2152)
                / GTP_U_Header(gtp_type="g_pdu", teid=200)
                / IPv6(dst=d, src=s)
                / UDP(sport=1000, dport=23)
            )
            self.logger.info(pkt.show2(dump=True))
            pkts.append(pkt)

        return pkts

    def test_srv6_mobile(self):
        """test_srv6_mobile"""
        pkts = self.create_packets([("A::1", "B::1"), ("C::1", "D::1")])

        self.vapi.cli("set sr encaps source addr A1::1")
        self.vapi.cli("sr policy add bsid D4:: next D2:: next D3::")

        # "sr localsid prefix 2001::/64 behavior end.m.gtp6.d 4::/64 fib-table 0 drop-in"
        # .format(self.ip6_nhop)
        localsid = VppSRv6MobileLocalSID(
            self,
            localsid_prefix="{}/{}".format("2001::", 64),
            behavior="end.m.gtp6.d",
            fib_table=0,
            drop_in=1,
            sr_prefix="{}/{}".format("D4::", 64),
        )
        localsid.add_vpp_config()

        # "ip route add D2::/64 via {}"
        # .format(self.ip6_nhop))
        route = VppIpRoute(
            self, "D2::", 64, [VppRoutePath(self.ip6_nhop, self.pg1.sw_if_index)]
        )
        route.add_vpp_config()
        self.logger.info(self.vapi.cli("show sr policies"))
        self.logger.info(self.vapi.cli("show sr localsid"))

        self.vapi.cli("clear errors")

        self.pg0.add_stream(pkts)
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        self.logger.info(self.vapi.cli("show errors"))
        self.logger.info(self.vapi.cli("show int address"))

        capture = self.pg1.get_capture(len(pkts))

        for pkt in capture:
            self.logger.info(pkt.show2(dump=True))
            self.logger.info(
                "GTP6.D SID0={}".format(str(pkt[IPv6ExtHdrSegmentRouting].addresses[0]))
            )
            self.logger.info(
                "GTP6.D SID1={}".format(str(pkt[IPv6ExtHdrSegmentRouting].addresses[1]))
            )
            self.assertEqual(str(pkt[IPv6ExtHdrSegmentRouting].addresses[0]), "2001::1")
            self.assertEqual(
                str(pkt[IPv6ExtHdrSegmentRouting].addresses[1]), "d4::c800:0"
            )