Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3.4 |
| 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 | |
| 23 | parser = argparse.ArgumentParser(description='MAP VPP configuration generator') |
| 24 | parser.add_argument('-t', action="store", dest="mapmode") |
| 25 | args = parser.parse_args() |
| 26 | |
| 27 | # |
| 28 | # 1:1 Shared IPv4 address, shared BR, Terastream |
| 29 | # |
| 30 | def terastream(): |
| 31 | ip4_pfx = ipaddress.ip_network('20.0.0.0/22') |
| 32 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 33 | psid_len = 6 |
| 34 | ip6_src = ipaddress.ip_address('cccc:bbbb::') |
| 35 | for i in range(ip4_pfx.num_addresses): |
| 36 | if not i % 64: |
| 37 | ip6_src = ip6_src + 1 |
| 38 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src " + str(ip6_src) + |
| 39 | " ea-bits-len 0 psid-offset 0 psid-len", psid_len) |
| 40 | for psid in range(0x1 << psid_len): |
| 41 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 42 | |
| 43 | # |
| 44 | # 1:1 Shared IPv4 address, shared BR, OTE |
| 45 | # |
| 46 | def oteshared11(): |
| 47 | ip4_pfx = ipaddress.ip_network('2.84.63.0/24') |
| 48 | dst = list(ipaddress.ip_network('2a02:580:8c00::/40').subnets(new_prefix=56)) |
| 49 | psid_len = 6 |
| 50 | ip6_src = ipaddress.ip_address('2a02::') |
| 51 | for i in range(ip4_pfx.num_addresses): |
| 52 | if not i % 64: |
| 53 | ip6_src = ip6_src + 1 |
| 54 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src " + str(ip6_src) + |
| 55 | " ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 56 | for psid in range(0x1 << psid_len): |
| 57 | enduserprefix = list(dst.pop(0).subnets(new_prefix=64))[255-1] |
| 58 | print("map add rule index", i, "psid", psid, "ip6-dst", enduserprefix[(i * (0x1<<psid_len)) + psid]) |
| 59 | |
| 60 | |
| 61 | # |
| 62 | # 1:1 Shared IPv4 address, shared BR, Terastream |
| 63 | # |
| 64 | def confdterastream(): |
| 65 | ip4_pfx = ipaddress.ip_network('20.0.0.0/22') |
| 66 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 67 | psid_len = 6 |
| 68 | ip6_src = ipaddress.ip_address('cccc:bbbb::') |
| 69 | for i in range(ip4_pfx.num_addresses): |
| 70 | if not i % 64: |
| 71 | ip6_src = ip6_src + 1 |
| 72 | print("vpp softwire softwire-instances softwire-instance", i, "br-ipv6 " + str(ip6_src) + " ipv6-prefix ::/0" + " ipv4-prefix " + str(ip4_pfx[i]) + |
| 73 | "/32 ea-len 0 psid-offset 6 psid-len", psid_len) |
| 74 | # print("vpp softwire softwire-instances softwire-instance", i, "ipv4-pfx " + str(ip4_pfx[i]) + "/32 ipv6-pfx ::/0 br-ipv6 " + str(ip6_src) + |
| 75 | # " ea-len 0 psid-offset 6 psid-len", psid_len) |
| 76 | for psid in range(0x1 << psid_len): |
| 77 | print("binding", psid, "ipv6-addr", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 78 | |
| 79 | def shared11br_yang(): |
| 80 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 81 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 82 | psid_len = 6 |
| 83 | for i in range(ip4_pfx.num_addresses): |
| 84 | print("vpp softwire softwire-instances softwire-instance " + str(i) + " ipv4-prefix " + str(ip4_pfx[i]) + "/32 " + |
| 85 | "ipv6-prefix ::/0 ea-len 0 psid-offset 6 tunnel-mtu 1234 psid-len", psid_len) |
| 86 | #print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1", |
| 87 | # "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 88 | for psid in range(0x1 << psid_len): |
| 89 | # print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 90 | print("binding", psid, "ipv6-addr", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 91 | |
| 92 | def shared11br_xml(): |
| 93 | ip4_pfx = ipaddress.ip_network('20.0.0.0/32') |
| 94 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 95 | ip6_src = ipaddress.ip_address('cccc:bbbb::') |
| 96 | psid_len = 6 |
| 97 | print('<vpp xmlns="http://www.cisco.com/yang/cisco-vpp"><softwire><softwire-instances>'); |
| 98 | count = 1024; |
| 99 | for i in range(ip4_pfx.num_addresses): |
| 100 | if not i % 64: |
| 101 | ip6_src = ip6_src + 1 |
| 102 | if count == 0: |
| 103 | break; |
| 104 | count = count - 1; |
| 105 | print('<softwire-instance>') |
| 106 | print(' <id>'+ str(i)+ '</id>') |
| 107 | print(' <ipv4-prefix>'+ str(ip4_pfx[i])+ '/32</ipv4-prefix>') |
| 108 | print(' <ipv6-prefix>::/0</ipv6-prefix>') |
| 109 | print(' <ea-len>0</ea-len>') |
| 110 | print(' <psid-offset>0</psid-offset>') |
| 111 | print(' <psid-len>'+ str(psid_len) + '</psid-len>') |
| 112 | for psid in range(0x1 << psid_len): |
| 113 | print(' <binding>') |
| 114 | print(' <psid>', psid, '</psid>') |
| 115 | print(' <ipv6-addr>'+ str(ip6_dst[(i * (0x1<<psid_len)) + psid]) + '</ipv6-addr>') |
| 116 | print(' </binding>') |
| 117 | print('</softwire-instance>') |
| 118 | print('</softwire-instances></softwire>') |
| 119 | print('</vpp>') |
| 120 | |
| 121 | # |
| 122 | # 1:1 Shared IPv4 address, shared BR |
| 123 | # |
| 124 | def shared11br(): |
| 125 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 126 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 127 | psid_len = 6 |
| 128 | for i in range(ip4_pfx.num_addresses): |
| 129 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1", |
| 130 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 131 | for psid in range(0x1 << psid_len): |
| 132 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 133 | |
| 134 | # |
| 135 | # 1:1 Shared IPv4 address, shared BR |
| 136 | # |
| 137 | def shared11br(): |
| 138 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 139 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 140 | psid_len = 6 |
| 141 | for i in range(ip4_pfx.num_addresses): |
| 142 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1", |
| 143 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 144 | for psid in range(0x1 << psid_len): |
| 145 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 146 | |
| 147 | |
| 148 | # |
| 149 | # 1:1 Shared IPv4 address |
| 150 | # |
| 151 | def shared11(): |
| 152 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 153 | ip6_src = ipaddress.ip_network('cccc:bbbb::/64') |
| 154 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 155 | psid_len = 6 |
| 156 | for i in range(ip4_pfx.num_addresses): |
| 157 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", ip6_src[i], |
| 158 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 159 | for psid in range(0x1 << psid_len): |
| 160 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 161 | |
| 162 | # |
| 163 | # 1:1 Shared IPv4 address small |
| 164 | # |
| 165 | def smallshared11(): |
| 166 | ip4_pfx = ipaddress.ip_network('20.0.0.0/24') |
| 167 | ip6_src = ipaddress.ip_network('cccc:bbbb::/64') |
| 168 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 169 | psid_len = 6 |
| 170 | for i in range(ip4_pfx.num_addresses): |
| 171 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", ip6_src[i], |
| 172 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 173 | for psid in range(0x1 << psid_len): |
| 174 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 175 | |
| 176 | # |
| 177 | # 1:1 Full IPv4 address |
| 178 | # |
| 179 | def full11(): |
| 180 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 181 | ip6_src = ipaddress.ip_network('cccc:bbbb::/64') |
| 182 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 183 | psid_len = 0 |
| 184 | for i in range(ip4_pfx.num_addresses): |
| 185 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-src", ip6_src[i], |
| 186 | "ea-bits-len 0 psid-offset 0 psid-len 0") |
| 187 | def full11br(): |
| 188 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 189 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 190 | psid_len = 0 |
| 191 | for i in range(ip4_pfx.num_addresses): |
| 192 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-shared-src cccc:bbbb::1", |
| 193 | "ea-bits-len 0 psid-offset 0 psid-len 0") |
| 194 | |
| 195 | # |
| 196 | # Algorithmic mapping Shared IPv4 address |
| 197 | # |
| 198 | def algo(): |
| 199 | print("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") |
| 200 | print("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") |
| 201 | |
| 202 | # |
| 203 | # IP4 forwarding |
| 204 | # |
| 205 | def ip4(): |
| 206 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 207 | for i in range(ip4_pfx.num_addresses): |
| 208 | print("ip route add " + str(ip4_pfx[i]) + "/32 via 172.16.0.2") |
| 209 | |
| 210 | |
| 211 | globals()[args.mapmode]() |
| 212 | |
| 213 | |