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()
48 def domain_print(i, ip4_pfx, ip6_pfx, ip6_src, eabits_len, psid_offset, psid_len):
51 "map add domain ip4-pfx " + ip4_pfx + " ip6-pfx",
53 "ip6-src " + ip6_src + " ea-bits-len",
62 "vpp softwire softwire-instances softwire-instance",
78 print("<softwire-instance>")
79 print("<id>", i, "</id>")
80 print(" <br-ipv6>" + ip6_src + "</br-ipv6>")
81 print(" <ipv6-prefix>" + ip6_pfx + "</ipv6-prefix>")
82 print(" <ipv4-prefix>" + ip4_pfx + "</ipv4-prefix>")
83 print(" <ea-len>", eabits_len, "</ea-len>")
84 print(" <psid-len>", psid_len, "</psid-len>")
85 print(" <psid-offset>", psid_offset, "</psid-offset>")
88 def domain_print_end():
90 print("</softwire-instance>")
93 def rule_print(i, psid, dst):
95 print("map add rule index", i, "psid", psid, "ip6-dst", dst)
97 print("binding", psid, "ipv6-addr", dst)
100 print(" <psid>", psid, "</psid>")
101 print(" <ipv6-addr>", dst, "</ipv6-addr>")
106 # Algorithmic mapping Shared IPv4 address
118 0, ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len
124 # 1:1 Full IPv4 address
135 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
136 ip6_src = ipaddress.ip_address(ip6_src_str)
137 ip6_dst = ipaddress.ip_network(ip6_pfx_str)
139 mod = ip4_pfx.num_addresses / 1024
141 for i in range(ip4_pfx.num_addresses):
143 i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0
146 if ip6_src_ecmp and not i % mod:
147 ip6_src = ip6_src + 1
151 # 1:1 Shared IPv4 address, shared BR (16) VPP CLI
162 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
163 ip6_src = ipaddress.ip_address(ip6_src_str)
164 ip6_dst = ipaddress.ip_network(ip6_pfx_str)
165 mod = ip4_pfx.num_addresses / 1024
167 for i in range(ip4_pfx.num_addresses):
168 domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len)
169 for psid in range(0x1 << int(psid_len)):
170 rule_print(i, psid, str(ip6_dst[(i * (0x1 << int(psid_len))) + psid]))
172 if ip6_src_ecmp and not i % mod:
173 ip6_src = ip6_src + 1
177 # 1:1 Shared IPv4 address, shared BR
188 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
189 ip6_src = ipaddress.ip_address(ip6_src_str)
190 ip6_dst = list(ipaddress.ip_network(ip6_pfx_str).subnets(new_prefix=56))
191 mod = ip4_pfx.num_addresses / 1024
193 for i in range(ip4_pfx.num_addresses):
194 domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len)
195 for psid in range(0x1 << psid_len):
196 enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255 - 1]
197 rule_print(i, psid, enduserprefix[(i * (0x1 << psid_len)) + psid])
199 if ip6_src_ecmp and not i % mod:
200 ip6_src = ip6_src + 1
203 def xml_header_print():
206 <?xml version="1.0" encoding="UTF-8"?>
207 <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
209 <capability>urn:ietf:params:netconf:base:1.0</capability>
214 <?xml version="1.0" encoding="UTF-8"?>
215 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
222 <vpp xmlns="http://www.cisco.com/yang/cisco-vpp">
230 def xml_footer_print():
233 </softwire-instances>
242 <?xml version="1.0" encoding="UTF-8"?>
243 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
255 globals()[args.mapmode](