Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 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 |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 28 | def_ea_bits_len = 0 |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 29 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | parser = argparse.ArgumentParser(description='MAP VPP configuration generator') |
| 31 | parser.add_argument('-t', action="store", dest="mapmode") |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 32 | parser.add_argument('-f', action="store", dest="format", default="vpp") |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 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) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | args = parser.parse_args() |
| 40 | |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 41 | # |
| 42 | # Print domain |
| 43 | # |
| 44 | def 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 | |
| 62 | def domain_print_end(): |
| 63 | if format == 'xml': |
| 64 | print("</softwire-instance>") |
| 65 | |
| 66 | def 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 Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 76 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | # |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 78 | # Algorithmic mapping Shared IPv4 address |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | # |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 80 | def 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 Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 83 | |
| 84 | # |
| 85 | # 1:1 Full IPv4 address |
| 86 | # |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 87 | def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 88 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | for i in range(ip4_pfx.num_addresses): |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 95 | domain_print(i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0) |
| 96 | domain_print_end() |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 97 | if ip6_src_ecmp and not i % mod: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | ip6_src = ip6_src + 1 |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 99 | |
| 100 | # |
| 101 | # 1:1 Shared IPv4 address, shared BR (16) VPP CLI |
| 102 | # |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 103 | def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 104 | 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 Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 107 | mod = ip4_pfx.num_addresses / 1024 |
| 108 | |
| 109 | for i in range(ip4_pfx.num_addresses): |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 110 | 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 Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 114 | if ip6_src_ecmp and not i % mod: |
| 115 | ip6_src = ip6_src + 1 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 117 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | # |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 119 | # 1:1 Shared IPv4 address, shared BR |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | # |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 121 | def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 122 | 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 Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 125 | mod = ip4_pfx.num_addresses / 1024 |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 126 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | for i in range(ip4_pfx.num_addresses): |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 128 | domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | for psid in range(0x1 << psid_len): |
Ole Troan | 92be0df | 2016-01-05 20:05:52 +0100 | [diff] [blame] | 130 | enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255-1] |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 131 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Ole Troan | 36de038 | 2016-01-17 18:59:44 +0100 | [diff] [blame] | 136 | |
| 137 | def 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 | |
| 161 | def 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 | |
| 181 | format = args.format |
| 182 | if format == 'xml': |
| 183 | xml_header_print() |
| 184 | globals()[args.mapmode](args.ip4_pfx, args.ip6_pfx, args.ip6_src, args.ea_bits_len, args.psid_offset, args.psid_len) |
| 185 | if format == 'xml': |
| 186 | xml_footer_print() |