diff options
-rwxr-xr-x | vnet/vnet/map/examples/gen-rules.py | 130 | ||||
-rw-r--r-- | vnet/vnet/map/examples/health_check.c | 109 |
2 files changed, 216 insertions, 23 deletions
diff --git a/vnet/vnet/map/examples/gen-rules.py b/vnet/vnet/map/examples/gen-rules.py index ba6dde09..7964aa9a 100755 --- a/vnet/vnet/map/examples/gen-rules.py +++ b/vnet/vnet/map/examples/gen-rules.py @@ -25,11 +25,11 @@ def_ip6_pfx = '2001:db8::/32' def_ip6_src = '2001:db8::1' def_psid_offset = 6 def_psid_len = 6 -def_ea_bits_len = 14 - +def_ea_bits_len = 0 parser = argparse.ArgumentParser(description='MAP VPP configuration generator') parser.add_argument('-t', action="store", dest="mapmode") +parser.add_argument('-f', action="store", dest="format", default="vpp") parser.add_argument('--ip4-prefix', action="store", dest="ip4_pfx", default=def_ip4_pfx) parser.add_argument('--ip6-prefix', action="store", dest="ip6_pfx", default=def_ip6_pfx) parser.add_argument('--ip6-src', action="store", dest="ip6_src", default=def_ip6_src) @@ -38,18 +38,53 @@ parser.add_argument('--psid-offset', action="store", dest="psid_offset", default parser.add_argument('--ea-bits-len', action="store", dest="ea_bits_len", default=def_ea_bits_len) args = parser.parse_args() +# +# Print domain +# +def domain_print(i, ip4_pfx, ip6_pfx, ip6_src, eabits_len, psid_offset, psid_len): + if format == 'vpp': + print("map add domain ip4-pfx " + ip4_pfx + " ip6-pfx", ip6_pfx, "ip6-src " + ip6_src + + " ea-bits-len", eabits_len, "psid-offset", psid_offset, "psid-len", psid_len) + if format == 'confd': + print("vpp softwire softwire-instances softwire-instance", i, "br-ipv6 " + ip6_src + + " ipv6-prefix " + ip6_pfx + " ipv4-prefix " + ip4_pfx + + " ea-bits-len", eabits_len, "psid-offset", psid_offset, "psid-len", psid_len) + if format == 'xml': + print("<softwire-instance>") + print("<id>", i, "</id>"); + print(" <br-ipv6>" + ip6_src + "</br-ipv6>") + print(" <ipv6-prefix>" + ip6_pfx + "</ipv6-prefix>") + print(" <ipv4-prefix>" + ip4_pfx + "</ipv4-prefix>") + print(" <ea-len>", eabits_len, "</ea-len>") + print(" <psid-len>", psid_len, "</psid-len>") + print(" <psid-offset>", psid_offset, "</psid-offset>") + +def domain_print_end(): + if format == 'xml': + print("</softwire-instance>") + +def rule_print(i, psid, dst): + if format == 'vpp': + print("map add rule index", i, "psid", psid, "ip6-dst", dst) + if format == 'confd': + print("binding", psid, "ipv6-addr", dst) + if format == 'xml': + print(" <binding>") + print(" <psid>", psid, "</psid>") + print(" <ipv6-addr>", dst, "</ipv6-addr>") + print(" </binding>") # # Algorithmic mapping Shared IPv4 address # -def algo(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False): - print("map add domain ip4-pfx " + ip4_pfx_str + " ip6-pfx " + ip6_pfx_str + " ip6-src " + ip6_src_str + - " ea-bits-len " + str(ea_bits_len) + " psid-offset 6 psid-len " + str(psid_len)) +def algo(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): + domain_print(0, ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len) + domain_print_end() # # 1:1 Full IPv4 address # -def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False): +def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): ip4_pfx = ipaddress.ip_network(ip4_pfx_str) ip6_src = ipaddress.ip_address(ip6_src_str) ip6_dst = ipaddress.ip_network(ip6_pfx_str) @@ -57,46 +92,95 @@ def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_e mod = ip4_pfx.num_addresses / 1024 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-src", - ip6_src, "ea-bits-len 0 psid-offset 0 psid-len 0") + domain_print(i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0) + domain_print_end() if ip6_src_ecmp and not i % mod: ip6_src = ip6_src + 1 # # 1:1 Shared IPv4 address, shared BR (16) VPP CLI # -def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False): +def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): ip4_pfx = ipaddress.ip_network(ip4_pfx_str) ip6_src = ipaddress.ip_address(ip6_src_str) ip6_dst = ipaddress.ip_network(ip6_pfx_str) - mod = ip4_pfx.num_addresses / 1024 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src " + str(ip6_src) + - " ea-bits-len 0 psid-offset 0 psid-len", psid_len) - for psid in range(0x1 << psid_len): - print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) + domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len) + for psid in range(0x1 << int(psid_len)): + rule_print(i, psid, str(ip6_dst[(i * (0x1<<int(psid_len))) + psid])) + domain_print_end() if ip6_src_ecmp and not i % mod: ip6_src = ip6_src + 1 + # # 1:1 Shared IPv4 address, shared BR # -def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, psid_len, ea_bits_len, ip6_src_ecmp = False): +def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): ip4_pfx = ipaddress.ip_network(ip4_pfx_str) ip6_src = ipaddress.ip_address(ip6_src_str) ip6_dst = list(ipaddress.ip_network(ip6_pfx_str).subnets(new_prefix=56)) - psid_len = 6 + mod = ip4_pfx.num_addresses / 1024 for i in range(ip4_pfx.num_addresses): - if not i % 64: - ip6_src = ip6_src + 1 - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src " + str(ip6_src) + - " ea-bits-len 0 psid-offset 6 psid-len", psid_len) + domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len) for psid in range(0x1 << psid_len): enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255-1] - print("map add rule index", i, "psid", psid, "ip6-dst", enduserprefix[(i * (0x1<<psid_len)) + psid]) + rule_print(i, psid, enduserprefix[(i * (0x1<<psid_len)) + psid]) + domain_print_end() + if ip6_src_ecmp and not i % mod: + ip6_src = ip6_src + 1 + -globals()[args.mapmode](args.ip4_pfx, args.ip6_pfx, args.ip6_src, args.psid_len, args.psid_offset, - args.ea_bits_len) +def xml_header_print(): + print(''' +<?xml version="1.0" encoding="UTF-8"?> + <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> + <capabilities> + <capability>urn:ietf:params:netconf:base:1.0</capability> + </capabilities> + </hello> +]]>]]> + +<?xml version="1.0" encoding="UTF-8"?> + <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"> + <edit-config> + <target> + <candidate/> + </target> + <config> + + <vpp xmlns="http://www.cisco.com/yang/cisco-vpp"> + <softwire> + <softwire-instances> + + ''') + +def xml_footer_print(): + print(''' +</softwire-instances> +</softwire> +</vpp> + </config> + </edit-config> + </rpc> + +]]>]]> + +<?xml version="1.0" encoding="UTF-8"?> + <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2"> + <close-session/> + </rpc> + +]]>]]> + ''') + + +format = args.format +if format == 'xml': + xml_header_print() +globals()[args.mapmode](args.ip4_pfx, args.ip6_pfx, args.ip6_src, args.ea_bits_len, args.psid_offset, args.psid_len) +if format == 'xml': + xml_footer_print() diff --git a/vnet/vnet/map/examples/health_check.c b/vnet/vnet/map/examples/health_check.c new file mode 100644 index 00000000..5f0d85fe --- /dev/null +++ b/vnet/vnet/map/examples/health_check.c @@ -0,0 +1,109 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <netinet/in.h> +#include <netinet/ip.h> +#include <netinet/ip_icmp.h> +#include <arpa/inet.h> +#include <net/if.h> +#include <stdbool.h> +#include <errno.h> + +static void +usage (void) { + fprintf(stderr, + "Usage: health_check" + " -d debug" + " -I interface" + "\n"); + exit(2); +} + +int +main (int argc, char **argv) +{ + int sd, ch; + uint8_t *opt, *pkt; + struct ifreq ifr; + char *interface = NULL; + bool debug = false; + + while ((ch = getopt(argc, argv, "h?" "I:" "d")) != EOF) { + switch(ch) { + case 'I': + interface = optarg; + break; + case 'd': + debug = true; + break; + default: + usage(); + break; + } + } + + argc -= optind; + argv += optind; + + if (!interface) + usage(); + + /* Request a socket descriptor sd. */ + if ((sd = socket (AF_INET6, SOCK_RAW, IPPROTO_IPIP)) < 0) { + perror ("Failed to get socket descriptor "); + exit (EXIT_FAILURE); + } + + memset(&ifr, 0, sizeof(ifr)); + snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", interface); + + /* Bind socket to interface of this node. */ + if (setsockopt (sd, SOL_SOCKET, SO_BINDTODEVICE, (void *) &ifr, sizeof (ifr)) < 0) { + perror ("SO_BINDTODEVICE failed"); + exit (EXIT_FAILURE); + } + if (debug) printf("Binding to interface %s\n", interface); + + while (1) { + struct sockaddr_in6 src_addr; + socklen_t addrlen = sizeof(src_addr); + char source[INET6_ADDRSTRLEN+1]; + int len; + uint8_t inpack[IP_MAXPACKET]; + + if ((len = recvfrom(sd, inpack, sizeof(inpack), 0, (struct sockaddr *)&src_addr, &addrlen)) < 0) { + perror("recvfrom failed "); + } + if (inet_ntop(AF_INET6, &src_addr.sin6_addr, source, INET6_ADDRSTRLEN) == NULL) { + perror("inet_ntop() failed."); + exit(EXIT_FAILURE); + } + + /* Reply */ + struct iphdr *ip = (struct iphdr *)inpack; + uint32_t saddr; + struct icmphdr *icmp; + + saddr = ip->saddr; + ip->saddr = ip->daddr; + ip->daddr = saddr; + + switch (ip->protocol) { + case 1: + if (debug) printf ("ICMP Echo request from %s\n", source); + icmp = (struct icmphdr *)&ip[1]; + icmp->type = ICMP_ECHOREPLY; + break; + default: + fprintf(stderr, "Unsupported protocol %d", ip->protocol); + } + if (len = sendto(sd, inpack, len, 0, (struct sockaddr *)&src_addr, addrlen) < 0) { + perror("sendto failed "); + } + } + + close (sd); + + return (EXIT_SUCCESS); +} |