blob: 7964aa9a359ac067b2588dfe0cacb6d59a8f8522 [file] [log] [blame]
Ole Troan92be0df2016-01-05 20:05:52 +01001#!/usr/bin/env python3
Ed Warnickecb9cada2015-12-08 15:45:58 -07002
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
16import ipaddress
17import argparse
18import 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
Ole Troan92be0df2016-01-05 20:05:52 +010023def_ip4_pfx = '192.0.2.0/24'
24def_ip6_pfx = '2001:db8::/32'
25def_ip6_src = '2001:db8::1'
26def_psid_offset = 6
27def_psid_len = 6
Ole Troan36de0382016-01-17 18:59:44 +010028def_ea_bits_len = 0
Ole Troan92be0df2016-01-05 20:05:52 +010029
Ed Warnickecb9cada2015-12-08 15:45:58 -070030parser = argparse.ArgumentParser(description='MAP VPP configuration generator')
31parser.add_argument('-t', action="store", dest="mapmode")
Ole Troan36de0382016-01-17 18:59:44 +010032parser.add_argument('-f', action="store", dest="format", default="vpp")
Ole Troan92be0df2016-01-05 20:05:52 +010033parser.add_argument('--ip4-prefix', action="store", dest="ip4_pfx", default=def_ip4_pfx)
34parser.add_argument('--ip6-prefix', action="store", dest="ip6_pfx", default=def_ip6_pfx)
35parser.add_argument('--ip6-src', action="store", dest="ip6_src", default=def_ip6_src)
36parser.add_argument('--psid-len', action="store", dest="psid_len", default=def_psid_len)
37parser.add_argument('--psid-offset', action="store", dest="psid_offset", default=def_psid_offset)
38parser.add_argument('--ea-bits-len', action="store", dest="ea_bits_len", default=def_ea_bits_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -070039args = parser.parse_args()
40
Ole Troan36de0382016-01-17 18:59:44 +010041#
42# Print domain
43#
44def domain_print(i, ip4_pfx, ip6_pfx, ip6_src, eabits_len, psid_offset, psid_len):
45 if format == 'vpp':
46 print("map add domain ip4-pfx " + ip4_pfx + " ip6-pfx", ip6_pfx, "ip6-src " + ip6_src +
47 " ea-bits-len", eabits_len, "psid-offset", psid_offset, "psid-len", psid_len)
48 if format == 'confd':
49 print("vpp softwire softwire-instances softwire-instance", i, "br-ipv6 " + ip6_src +
50 " ipv6-prefix " + ip6_pfx + " ipv4-prefix " + ip4_pfx +
51 " ea-bits-len", eabits_len, "psid-offset", psid_offset, "psid-len", psid_len)
52 if format == 'xml':
53 print("<softwire-instance>")
54 print("<id>", i, "</id>");
55 print(" <br-ipv6>" + ip6_src + "</br-ipv6>")
56 print(" <ipv6-prefix>" + ip6_pfx + "</ipv6-prefix>")
57 print(" <ipv4-prefix>" + ip4_pfx + "</ipv4-prefix>")
58 print(" <ea-len>", eabits_len, "</ea-len>")
59 print(" <psid-len>", psid_len, "</psid-len>")
60 print(" <psid-offset>", psid_offset, "</psid-offset>")
61
62def domain_print_end():
63 if format == 'xml':
64 print("</softwire-instance>")
65
66def rule_print(i, psid, dst):
67 if format == 'vpp':
68 print("map add rule index", i, "psid", psid, "ip6-dst", dst)
69 if format == 'confd':
70 print("binding", psid, "ipv6-addr", dst)
71 if format == 'xml':
72 print(" <binding>")
73 print(" <psid>", psid, "</psid>")
74 print(" <ipv6-addr>", dst, "</ipv6-addr>")
75 print(" </binding>")
Ole Troan92be0df2016-01-05 20:05:52 +010076
Ed Warnickecb9cada2015-12-08 15:45:58 -070077#
Ole Troan92be0df2016-01-05 20:05:52 +010078# Algorithmic mapping Shared IPv4 address
Ed Warnickecb9cada2015-12-08 15:45:58 -070079#
Ole Troan36de0382016-01-17 18:59:44 +010080def algo(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False):
81 domain_print(0, ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len)
82 domain_print_end()
Ole Troan92be0df2016-01-05 20:05:52 +010083
84#
85# 1:1 Full IPv4 address
86#
Ole Troan36de0382016-01-17 18:59:44 +010087def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False):
Ole Troan92be0df2016-01-05 20:05:52 +010088 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
89 ip6_src = ipaddress.ip_address(ip6_src_str)
90 ip6_dst = ipaddress.ip_network(ip6_pfx_str)
91 psid_len = 0
92 mod = ip4_pfx.num_addresses / 1024
93
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 for i in range(ip4_pfx.num_addresses):
Ole Troan36de0382016-01-17 18:59:44 +010095 domain_print(i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0)
96 domain_print_end()
Ole Troan92be0df2016-01-05 20:05:52 +010097 if ip6_src_ecmp and not i % mod:
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 ip6_src = ip6_src + 1
Ole Troan92be0df2016-01-05 20:05:52 +010099
100#
101# 1:1 Shared IPv4 address, shared BR (16) VPP CLI
102#
Ole Troan36de0382016-01-17 18:59:44 +0100103def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False):
Ole Troan92be0df2016-01-05 20:05:52 +0100104 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
105 ip6_src = ipaddress.ip_address(ip6_src_str)
106 ip6_dst = ipaddress.ip_network(ip6_pfx_str)
Ole Troan92be0df2016-01-05 20:05:52 +0100107 mod = ip4_pfx.num_addresses / 1024
108
109 for i in range(ip4_pfx.num_addresses):
Ole Troan36de0382016-01-17 18:59:44 +0100110 domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len)
111 for psid in range(0x1 << int(psid_len)):
112 rule_print(i, psid, str(ip6_dst[(i * (0x1<<int(psid_len))) + psid]))
113 domain_print_end()
Ole Troan92be0df2016-01-05 20:05:52 +0100114 if ip6_src_ecmp and not i % mod:
115 ip6_src = ip6_src + 1
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Ole Troan36de0382016-01-17 18:59:44 +0100117
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118#
Ole Troan92be0df2016-01-05 20:05:52 +0100119# 1:1 Shared IPv4 address, shared BR
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120#
Ole Troan36de0382016-01-17 18:59:44 +0100121def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False):
Ole Troan92be0df2016-01-05 20:05:52 +0100122 ip4_pfx = ipaddress.ip_network(ip4_pfx_str)
123 ip6_src = ipaddress.ip_address(ip6_src_str)
124 ip6_dst = list(ipaddress.ip_network(ip6_pfx_str).subnets(new_prefix=56))
Ole Troan36de0382016-01-17 18:59:44 +0100125 mod = ip4_pfx.num_addresses / 1024
Ole Troan92be0df2016-01-05 20:05:52 +0100126
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 for i in range(ip4_pfx.num_addresses):
Ole Troan36de0382016-01-17 18:59:44 +0100128 domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 for psid in range(0x1 << psid_len):
Ole Troan92be0df2016-01-05 20:05:52 +0100130 enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255-1]
Ole Troan36de0382016-01-17 18:59:44 +0100131 rule_print(i, psid, enduserprefix[(i * (0x1<<psid_len)) + psid])
132 domain_print_end()
133 if ip6_src_ecmp and not i % mod:
134 ip6_src = ip6_src + 1
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Ole Troan36de0382016-01-17 18:59:44 +0100136
137def xml_header_print():
138 print('''
139<?xml version="1.0" encoding="UTF-8"?>
140 <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
141 <capabilities>
142 <capability>urn:ietf:params:netconf:base:1.0</capability>
143 </capabilities>
144 </hello>
145]]>]]>
146
147<?xml version="1.0" encoding="UTF-8"?>
148 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
149 <edit-config>
150 <target>
151 <candidate/>
152 </target>
153 <config>
154
155 <vpp xmlns="http://www.cisco.com/yang/cisco-vpp">
156 <softwire>
157 <softwire-instances>
158
159 ''')
160
161def xml_footer_print():
162 print('''
163</softwire-instances>
164</softwire>
165</vpp>
166 </config>
167 </edit-config>
168 </rpc>
169
170]]>]]>
171
172<?xml version="1.0" encoding="UTF-8"?>
173 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
174 <close-session/>
175 </rpc>
176
177]]>]]>
178 ''')
179
180
181format = args.format
182if format == 'xml':
183 xml_header_print()
184globals()[args.mapmode](args.ip4_pfx, args.ip6_pfx, args.ip6_src, args.ea_bits_len, args.psid_offset, args.psid_len)
185if format == 'xml':
186 xml_footer_print()