Clean up script to generate LW46 bindings / MAP-E rules.
[vpp.git] / vnet / vnet / map / examples / gen-rules.py
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2015 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import ipaddress
17 import argparse
18 import sys
19
20 # map add domain ip4-pfx <pfx> ip6-pfx ::/0 ip6-src <ip6-src> ea-bits-len 0 psid-offset 6 psid-len 6
21 # map add rule index <0> psid <psid> ip6-dst <ip6-dst>
22
23 def_ip4_pfx = '192.0.2.0/24'
24 def_ip6_pfx = '2001:db8::/32'
25 def_ip6_src = '2001:db8::1'
26 def_psid_offset = 6
27 def_psid_len = 6
28 def_ea_bits_len = 14
29
30
31 parser = argparse.ArgumentParser(description='MAP VPP configuration generator')
32 parser.add_argument('-t', action="store", dest="mapmode")
33 parser.add_argument('--ip4-prefix', action="store", dest="ip4_pfx", default=def_ip4_pfx)
34 parser.add_argument('--ip6-prefix', action="store", dest="ip6_pfx", default=def_ip6_pfx)
35 parser.add_argument('--ip6-src', action="store", dest="ip6_src", default=def_ip6_src)
36 parser.add_argument('--psid-len', action="store", dest="psid_len", default=def_psid_len)
37 parser.add_argument('--psid-offset', action="store", dest="psid_offset", default=def_psid_offset)
38 parser.add_argument('--ea-bits-len', action="store", dest="ea_bits_len", default=def_ea_bits_len)
39 args = parser.parse_args()
40
41
42 #
43 # Algorithmic mapping Shared IPv4 address
44 #
45 def algo(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False):
46     print("map add domain ip4-pfx " + ip4_pfx_str + " ip6-pfx " + ip6_pfx_str + " ip6-src " + ip6_src_str +
47           " ea-bits-len " + str(ea_bits_len) + " psid-offset 6 psid-len " + str(psid_len))
48
49 #
50 # 1:1 Full IPv4 address
51 #
52 def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False):
53     ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
54     ip6_src = ipaddress.ip_address(ip6_src_str)
55     ip6_dst = ipaddress.ip_network(ip6_pfx_str)
56     psid_len = 0
57     mod = ip4_pfx.num_addresses / 1024
58
59     for i in range(ip4_pfx.num_addresses):
60         print("map add domain ip4-pfx " + str(ip4_pfx[i]) +  "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-src",
61               ip6_src, "ea-bits-len 0 psid-offset 0 psid-len 0")
62         if ip6_src_ecmp and not i % mod:
63             ip6_src = ip6_src + 1
64
65 #
66 # 1:1 Shared IPv4 address, shared BR (16) VPP CLI
67 #
68 def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False):
69     ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
70     ip6_src = ipaddress.ip_address(ip6_src_str)
71     ip6_dst = ipaddress.ip_network(ip6_pfx_str)
72
73     mod = ip4_pfx.num_addresses / 1024
74
75     for i in range(ip4_pfx.num_addresses):
76         print("map add domain ip4-pfx " + str(ip4_pfx[i]) +  "/32 ip6-pfx ::/0 ip6-src " + str(ip6_src) +
77               " ea-bits-len 0 psid-offset 0 psid-len", psid_len)
78         for psid in range(0x1 << psid_len):
79             print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid])
80         if ip6_src_ecmp and not i % mod:
81             ip6_src = ip6_src + 1
82
83 #
84 # 1:1 Shared IPv4 address, shared BR
85 #
86 def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False):
87     ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
88     ip6_src = ipaddress.ip_address(ip6_src_str)
89     ip6_dst = list(ipaddress.ip_network(ip6_pfx_str).subnets(new_prefix=56))
90     psid_len = 6
91
92     for i in range(ip4_pfx.num_addresses):
93         if not i % 64:
94             ip6_src = ip6_src + 1
95         print("map add domain ip4-pfx " + str(ip4_pfx[i]) +  "/32 ip6-pfx ::/0 ip6-src " + str(ip6_src) +
96               " ea-bits-len 0 psid-offset 6 psid-len", psid_len)
97         for psid in range(0x1 << psid_len):
98             enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255-1]
99             print("map add rule index", i, "psid", psid, "ip6-dst", enduserprefix[(i * (0x1<<psid_len)) + psid])
100
101 globals()[args.mapmode](args.ip4_pfx, args.ip6_pfx, args.ip6_src, args.psid_len, args.psid_offset,
102                         args.ea_bits_len)