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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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>
23 def_ip4_pfx = "192.0.2.0/24"
24 def_ip6_pfx = "2001:db8::/32"
25 def_ip6_src = "2001:db8::1"
30 parser = argparse.ArgumentParser(description="MAP VPP configuration generator")
31 parser.add_argument("-t", action="store", dest="mapmode")
32 parser.add_argument("-f", action="store", dest="format", default="vpp")
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)
38 "--psid-offset", action="store", dest="psid_offset", default=def_psid_offset
41 "--ea-bits-len", action="store", dest="ea_bits_len", default=def_ea_bits_len
43 args = parser.parse_args()
49 def domain_print(i, ip4_pfx, ip6_pfx, ip6_src, eabits_len, psid_offset, psid_len):
52 "map add domain ip4-pfx " + ip4_pfx + " ip6-pfx",
54 "ip6-src " + ip6_src + " ea-bits-len",
63 "vpp softwire softwire-instances softwire-instance",
79 print("<softwire-instance>")
80 print("<id>", i, "</id>")
81 print(" <br-ipv6>" + ip6_src + "</br-ipv6>")
82 print(" <ipv6-prefix>" + ip6_pfx + "</ipv6-prefix>")
83 print(" <ipv4-prefix>" + ip4_pfx + "</ipv4-prefix>")
84 print(" <ea-len>", eabits_len, "</ea-len>")
85 print(" <psid-len>", psid_len, "</psid-len>")
86 print(" <psid-offset>", psid_offset, "</psid-offset>")
89 def domain_print_end():
91 print("</softwire-instance>")
94 def rule_print(i, psid, dst):
96 print("map add rule index", i, "psid", psid, "ip6-dst", dst)
98 print("binding", psid, "ipv6-addr", dst)
101 print(" <psid>", psid, "</psid>")
102 print(" <ipv6-addr>", dst, "</ipv6-addr>")
107 # Algorithmic mapping Shared IPv4 address
119 0, ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len
125 # 1:1 Full IPv4 address
136 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
137 ip6_src = ipaddress.ip_address(ip6_src_str)
138 ip6_dst = ipaddress.ip_network(ip6_pfx_str)
140 mod = ip4_pfx.num_addresses / 1024
142 for i in range(ip4_pfx.num_addresses):
144 i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0
147 if ip6_src_ecmp and not i % mod:
148 ip6_src = ip6_src + 1
152 # 1:1 Shared IPv4 address, shared BR (16) VPP CLI
163 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
164 ip6_src = ipaddress.ip_address(ip6_src_str)
165 ip6_dst = ipaddress.ip_network(ip6_pfx_str)
166 mod = ip4_pfx.num_addresses / 1024
168 for i in range(ip4_pfx.num_addresses):
169 domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len)
170 for psid in range(0x1 << int(psid_len)):
171 rule_print(i, psid, str(ip6_dst[(i * (0x1 << int(psid_len))) + psid]))
173 if ip6_src_ecmp and not i % mod:
174 ip6_src = ip6_src + 1
178 # 1:1 Shared IPv4 address, shared BR
189 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
190 ip6_src = ipaddress.ip_address(ip6_src_str)
191 ip6_dst = list(ipaddress.ip_network(ip6_pfx_str).subnets(new_prefix=56))
192 mod = ip4_pfx.num_addresses / 1024
194 for i in range(ip4_pfx.num_addresses):
195 domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len)
196 for psid in range(0x1 << psid_len):
197 enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255 - 1]
198 rule_print(i, psid, enduserprefix[(i * (0x1 << psid_len)) + psid])
200 if ip6_src_ecmp and not i % mod:
201 ip6_src = ip6_src + 1
204 def xml_header_print():
207 <?xml version="1.0" encoding="UTF-8"?>
208 <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
210 <capability>urn:ietf:params:netconf:base:1.0</capability>
215 <?xml version="1.0" encoding="UTF-8"?>
216 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
223 <vpp xmlns="http://www.cisco.com/yang/cisco-vpp">
231 def xml_footer_print():
234 </softwire-instances>
243 <?xml version="1.0" encoding="UTF-8"?>
244 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
256 globals()[args.mapmode](