Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
| 17 | import ipaddress |
| 18 | import argparse |
| 19 | import sys |
| 20 | |
| 21 | # map add domain ip4-pfx <pfx> ip6-pfx ::/0 ip6-src <ip6-src> ea-bits-len 0 psid-offset 6 psid-len 6 |
| 22 | # map add rule index <0> psid <psid> ip6-dst <ip6-dst> |
| 23 | |
| 24 | parser = argparse.ArgumentParser(description='MAP VPP configuration generator') |
| 25 | parser.add_argument('-t', action="store", dest="mapmode") |
| 26 | args = parser.parse_args() |
| 27 | |
| 28 | # |
| 29 | # 1:1 Shared IPv4 address, shared BR |
| 30 | # |
| 31 | def shared11br(): |
| 32 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 33 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 34 | psid_len = 6 |
| 35 | for i in range(ip4_pfx.num_addresses): |
| 36 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1", |
| 37 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 38 | for psid in range(0x1 << psid_len): |
| 39 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 40 | |
| 41 | |
| 42 | # |
| 43 | # 1:1 Shared IPv4 address |
| 44 | # |
| 45 | def shared11(): |
| 46 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 47 | ip6_src = ipaddress.ip_network('cccc:bbbb::/64') |
| 48 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 49 | psid_len = 6 |
| 50 | for i in range(ip4_pfx.num_addresses): |
| 51 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", ip6_src[i], |
| 52 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 53 | for psid in range(0x1 << psid_len): |
| 54 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 55 | |
| 56 | # |
| 57 | # 1:1 Shared IPv4 address small |
| 58 | # |
| 59 | def smallshared11(): |
| 60 | ip4_pfx = ipaddress.ip_network('20.0.0.0/24') |
| 61 | ip6_src = ipaddress.ip_network('cccc:bbbb::/64') |
| 62 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 63 | psid_len = 6 |
| 64 | for i in range(ip4_pfx.num_addresses): |
| 65 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", ip6_src[i], |
| 66 | "ea-bits-len 0 psid-offset 6 psid-len", psid_len) |
| 67 | for psid in range(0x1 << psid_len): |
| 68 | print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) |
| 69 | |
| 70 | # |
| 71 | # 1:1 Full IPv4 address |
| 72 | # |
| 73 | def full11(): |
| 74 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 75 | ip6_src = ipaddress.ip_network('cccc:bbbb::/64') |
| 76 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 77 | psid_len = 0 |
| 78 | for i in range(ip4_pfx.num_addresses): |
| 79 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-src", ip6_src[i], |
| 80 | "ea-bits-len 0 psid-offset 0 psid-len 0") |
| 81 | def full11br(): |
| 82 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 83 | ip6_dst = ipaddress.ip_network('bbbb::/32') |
| 84 | psid_len = 0 |
| 85 | for i in range(ip4_pfx.num_addresses): |
| 86 | print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-shared-src cccc:bbbb::1", |
| 87 | "ea-bits-len 0 psid-offset 0 psid-len 0") |
| 88 | |
| 89 | # |
| 90 | # Algorithmic mapping Shared IPv4 address |
| 91 | # |
| 92 | def algo(): |
| 93 | 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") |
| 94 | 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") |
| 95 | |
| 96 | # |
| 97 | # IP4 forwarding |
| 98 | # |
| 99 | def ip4(): |
| 100 | ip4_pfx = ipaddress.ip_network('20.0.0.0/16') |
| 101 | for i in range(ip4_pfx.num_addresses): |
| 102 | print("ip route add " + str(ip4_pfx[i]) + "/32 via 172.16.0.2") |
| 103 | |
| 104 | |
| 105 | globals()[args.mapmode]() |
| 106 | |
| 107 | |