tests: replace pycodestyle with black
[vpp.git] / src / plugins / map / 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
17 import ipaddress
18 import argparse
19 import sys
20
21 # map add domain ip4-pfx <pfx> ip6-pfx ::/0 ip6-src <ip6-src> ea-bits-len 0 psid-offset 6 psid-len 6
22 # map add rule index <0> psid <psid> ip6-dst <ip6-dst>
23
24 parser = argparse.ArgumentParser(description="MAP VPP configuration generator")
25 parser.add_argument("-t", action="store", dest="mapmode")
26 args = parser.parse_args()
27
28 #
29 # 1:1 Shared IPv4 address, shared BR
30 #
31 def shared11br():
32     ip4_pfx = ipaddress.ip_network("20.0.0.0/16")
33     ip6_dst = ipaddress.ip_network("bbbb::/32")
34     psid_len = 6
35     for i in range(ip4_pfx.num_addresses):
36         print(
37             "map add domain ip4-pfx "
38             + str(ip4_pfx[i])
39             + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1",
40             "ea-bits-len 0 psid-offset 6 psid-len",
41             psid_len,
42         )
43         for psid in range(0x1 << psid_len):
44             print(
45                 "map add rule index",
46                 i,
47                 "psid",
48                 psid,
49                 "ip6-dst",
50                 ip6_dst[(i * (0x1 << psid_len)) + psid],
51             )
52
53
54 #
55 # 1:1 Shared IPv4 address
56 #
57 def shared11():
58     ip4_pfx = ipaddress.ip_network("20.0.0.0/16")
59     ip6_src = ipaddress.ip_network("cccc:bbbb::/64")
60     ip6_dst = ipaddress.ip_network("bbbb::/32")
61     psid_len = 6
62     for i in range(ip4_pfx.num_addresses):
63         print(
64             "map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src",
65             ip6_src[i],
66             "ea-bits-len 0 psid-offset 6 psid-len",
67             psid_len,
68         )
69         for psid in range(0x1 << psid_len):
70             print(
71                 "map add rule index",
72                 i,
73                 "psid",
74                 psid,
75                 "ip6-dst",
76                 ip6_dst[(i * (0x1 << psid_len)) + psid],
77             )
78
79
80 #
81 # 1:1 Shared IPv4 address small
82 #
83 def smallshared11():
84     ip4_pfx = ipaddress.ip_network("20.0.0.0/24")
85     ip6_src = ipaddress.ip_network("cccc:bbbb::/64")
86     ip6_dst = ipaddress.ip_network("bbbb::/32")
87     psid_len = 6
88     for i in range(ip4_pfx.num_addresses):
89         print(
90             "map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src",
91             ip6_src[i],
92             "ea-bits-len 0 psid-offset 6 psid-len",
93             psid_len,
94         )
95         for psid in range(0x1 << psid_len):
96             print(
97                 "map add rule index",
98                 i,
99                 "psid",
100                 psid,
101                 "ip6-dst",
102                 ip6_dst[(i * (0x1 << psid_len)) + psid],
103             )
104
105
106 #
107 # 1:1 Full IPv4 address
108 #
109 def full11():
110     ip4_pfx = ipaddress.ip_network("20.0.0.0/16")
111     ip6_src = ipaddress.ip_network("cccc:bbbb::/64")
112     ip6_dst = ipaddress.ip_network("bbbb::/32")
113     psid_len = 0
114     for i in range(ip4_pfx.num_addresses):
115         print(
116             "map add domain ip4-pfx "
117             + str(ip4_pfx[i])
118             + "/32 ip6-pfx "
119             + str(ip6_dst[i])
120             + "/128 ip6-src",
121             ip6_src[i],
122             "ea-bits-len 0 psid-offset 0 psid-len 0",
123         )
124
125
126 def full11br():
127     ip4_pfx = ipaddress.ip_network("20.0.0.0/16")
128     ip6_dst = ipaddress.ip_network("bbbb::/32")
129     psid_len = 0
130     for i in range(ip4_pfx.num_addresses):
131         print(
132             "map add domain ip4-pfx "
133             + str(ip4_pfx[i])
134             + "/32 ip6-pfx "
135             + str(ip6_dst[i])
136             + "/128 ip6-shared-src cccc:bbbb::1",
137             "ea-bits-len 0 psid-offset 0 psid-len 0",
138         )
139
140
141 #
142 # Algorithmic mapping Shared IPv4 address
143 #
144 def algo():
145     print(
146         "map add domain ip4-pfx 20.0.0.0/24 ip6-pfx bbbb::/32 ip6-src cccc:bbbb::1 ea-bits-len 16 psid-offset 6 psid-len 8"
147     )
148     print(
149         "map add domain ip4-pfx 20.0.1.0/24 ip6-pfx bbbb:1::/32 ip6-src cccc:bbbb::2 ea-bits-len 8 psid-offset 0 psid-len 0"
150     )
151
152
153 #
154 # IP4 forwarding
155 #
156 def ip4():
157     ip4_pfx = ipaddress.ip_network("20.0.0.0/16")
158     for i in range(ip4_pfx.num_addresses):
159         print("ip route add " + str(ip4_pfx[i]) + "/32 via 172.16.0.2")
160
161
162 globals()[args.mapmode]()