[lib] Centralise Zserv route type information, auto-generate redist strings

2006-05-23 Paul Jakma <paul.jakma@sun.com>

	* route_types.txt: New file, table of ZEBRA_ROUTE definitions.
	* route_types.awk: New script, to parse previous and generate
	  (for now) redistribute string defines.
	* Makefile.am: build route_types.h using previous two, ala
	  memtypes.h, include the script and table file in EXTRA_DIST.
	* command.h: pull in route_types.h, add a REDIST_STR define.
diff --git a/lib/ChangeLog b/lib/ChangeLog
index d031d8f..37c1324 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -28,6 +28,15 @@
 	* if.h: (struct connected) Document the meaning of the
 	  ZEBRA_IFC_REAL and ZEBRA_IFC_CONFIGURED flags.
 
+2006-05-23 Paul Jakma <paul.jakma@sun.com>
+
+	* route_types.txt: New file, table of ZEBRA_ROUTE definitions.
+	* route_types.awk: New script, to parse previous and generate
+	  (for now) redistribute string defines.
+	* Makefile.am: build route_types.h using previous two, ala
+	  memtypes.h, include the script and table file in EXTRA_DIST.
+	* command.h: pull in route_types.h, add a REDIST_STR define.
+
 2006-05-15 Paul Jakma <paul.jakma@sun.com>
 
 	* log.c: (general) Generalise struct zebra_route_desc into
diff --git a/lib/Makefile.am b/lib/Makefile.am
index ec7ca00..315e919 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -14,7 +14,7 @@
 	zclient.c sockopt.c smux.c md5.c if_rmap.c keychain.c privs.c \
 	sigevent.c pqueue.c jhash.c memtypes.c workqueue.c
 
-BUILT_SOURCES = memtypes.h
+BUILT_SOURCES = memtypes.h route_types.h
 
 libzebra_la_DEPENDENCIES = @LIB_REGEX@
 
@@ -27,9 +27,12 @@
 	str.h stream.h table.h thread.h vector.h version.h vty.h zebra.h \
 	plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \
 	privs.h sigevent.h pqueue.h jhash.h zassert.h memtypes.h \
-	privs.h sigevent.h pqueue.h jhash.h zassert.h memtypes.h workqueue.h
+	workqueue.h route_types.h
 
-EXTRA_DIST = regex.c regex-gnu.h memtypes.awk
+EXTRA_DIST = regex.c regex-gnu.h memtypes.awk route_types.awk route_types.txt
 
 memtypes.h: $(srcdir)/memtypes.c $(srcdir)/memtypes.awk
-	($(GAWK) -f $(srcdir)/memtypes.awk $(srcdir)/memtypes.c > memtypes.h)
+	($(GAWK) -f $(srcdir)/memtypes.awk $(srcdir)/memtypes.c > $@)
+
+route_types.h: $(srcdir)/route_types.txt $(srcdir)/route_types.awk
+	($(GAWK) -f $(srcdir)/route_types.awk $(srcdir)/route_types.txt > $@)
diff --git a/lib/command.h b/lib/command.h
index 99aec33..ce18731 100644
--- a/lib/command.h
+++ b/lib/command.h
@@ -25,6 +25,7 @@
 
 #include "vector.h"
 #include "vty.h"
+#include "lib/route_types.h"
 
 /* Host configuration variable */
 struct host
@@ -271,6 +272,7 @@
 #define IP_STR "IP information\n"
 #define IPV6_STR "IPv6 information\n"
 #define NO_STR "Negate a command or set its defaults\n"
+#define REDIST_STR "Redistribute information from another routing protocol\n"
 #define CLEAR_STR "Reset functions\n"
 #define RIP_STR "RIP information\n"
 #define BGP_STR "BGP information\n"
diff --git a/lib/route_types.awk b/lib/route_types.awk
new file mode 100644
index 0000000..26d1c0c
--- /dev/null
+++ b/lib/route_types.awk
@@ -0,0 +1,187 @@
+# $Id$
+#
+# Scan a file of route-type definitions (see eg route_types.txt) and
+# generate a corresponding header file with:
+#
+# - enum of Zserv route-types
+# - redistribute strings for the various Quagga daemons
+#
+# See route_types.txt for the format.
+#
+#
+
+BEGIN {
+	FS="[,]";
+	
+	# globals
+	exitret = 0;
+	tcount = 0;
+	
+	# 'bare' redistribute specifier, <1-255>, help string.
+	redist_bare_help = "Numeric Zserv route type";
+	
+	# formats for output
+	redist_def_fmt = "#define QUAGGA_REDIST_STR_%s \\\n";
+	redist_str_fmt = "\"(%s<1-255>)\"\n";
+	redist_help_def_fmt = "#define QUAGGA_REDIST_HELP_STR_%s";
+	redist_help_str_fmt = " \\\n  \"%s\\n\"";
+	
+	# header
+	header = "/* Auto-generated from route_types.txt by " ARGV[0] ". */\n";
+	header = header "/* Do not edit! */\n";
+	header = header "\n#ifndef _QUAGGA_ROUTE_TYPES_H\n";
+	header = header "#define _QUAGGA_ROUTE_TYPES_H\n";
+	footer = "#endif /* _QUAGGA_ROUTE_TYPES_H */\n";
+	printf ("%s\n", header);
+}
+
+# Chomp comment lines
+($0 ~ /^#/) { 
+	next;
+}
+
+# get rid of the commas, leading/trailling whitespace and
+# quotes
+{
+	for (i = 1; i <= NF; i++) {
+		#print "before:" $i;
+		$i = gensub(/^[[:blank:]]*(.*)[,]*.*/, "\\1", "g",$i);
+		$i = gensub(/^["](.*)["]$/, "\\1", "g", $i);
+		#print "after :" $i;
+	}
+}
+
+# 7 field format:
+#  type                 cname      daemon  C    4  6  short help
+(NF >= 7) {
+	#print "7", $1, $0;
+	
+	if ($1 in types) {
+		print "error: attempt to redefine", $1;
+		exitret = 1;
+		exit exitret;
+	}
+	
+	typesbynum[tcount] = $1;
+	types[$1,"num"] = tcount++; 
+	types[$1,"cname"] = $2;
+	types[$1,"daemon"] = $3;
+	types[$1,"C"] = $4;
+	types[$1,"4"] = strtonum($5);
+	types[$1,"6"] = strtonum($6);
+	types[$1,"shelp"] = $7;
+	
+	#print "num   :", types[$1,"num"]
+	#print "cname :", types[$1,"cname"]
+	#print "daemon:", types[$1,"daemon"];
+	#print "char  :", types[$1,"C"];
+};
+
+# 2 field: type "long description"
+(NF == 2) {
+	#print "2", $1, $2;
+	
+	if (!(($1 SUBSEP "num") in types)) {
+		print "error: type", $1, "must be defined before help str";
+		exitret = 2;
+		exit exitret;
+	}
+	
+	types[$1,"lhelp"] = $2;
+}
+
+END {
+	if (exitret)
+		exit exitret;
+	
+	# The enums
+	# not yet...
+	#printf("enum\n{\n");
+	#for (i = 0; i < tcount; i++) {
+	#	type = typesbynum[i];
+	#	if (type != "" && types[type,"num"] == i)
+	#		printf ("  %s,\n", type);
+	#}
+	#printf ("  ZEBRA_ROUTE_MAX,\n};\n\n");
+	
+	# the redistribute defines
+	for (i = 0; i < tcount; i++) {
+		type = typesbynum[i];
+		
+		# must be a type, and must cross-check against recorded type
+		if (type == "" || types[type,"num"] != i)
+			continue;
+		
+		# ignore route types that can't be redistributed
+		if (!(types[type,"4"] || types[type,"6"]))
+			continue;
+		
+		# must have a daemon name
+		if (!((type SUBSEP "daemon") in types))
+			continue;
+		if (!(daemon = types[type,"daemon"]))
+			continue;
+		
+		# might have done this daemon already?
+		if (daemon in seen_daemons)
+			continue;
+		
+		cname = types[type,"cname"];
+		all = all "|" cname;
+		rstr = "";
+		hstr = "";
+		
+		# add it to the others
+		for (j = 0; j < tcount; j++) {
+			# ignore self
+			if (i == j)
+				continue;
+			
+			type2 = typesbynum[j];
+			
+			# type2 must be valid, and self-check.
+			if (type2 == "" || types[type2,"num"] != j)
+				continue;
+			
+			# ignore different route types for the same daemon
+			# (eg system/kernel/connected)
+			if (types[type2,"daemon"] == daemon)
+				continue;
+			
+			if ((types[type2,"4"] && types[type,"4"]) \
+			    || (types[type2,"6"] && types[type,"6"])) {
+
+				rstr = rstr types[type2,"cname"] "|";
+				
+				if ((type2 SUBSEP "lhelp") in types)
+				  hstr2 = types[type2,"lhelp"];
+				else if ((type2 SUBSEP "shelp") in types)
+				  hstr2 = types[type2,"shelp"];
+				else
+				  hstr2 = types[type2,"cname"];
+				
+				hstr = hstr sprintf(redist_help_str_fmt, hstr2);
+			}
+		}
+		
+		# dont double-process daemons.
+		seen_daemons[daemon] = 1;
+		
+		printf("/* %s */\n", daemon);
+		printf(redist_def_fmt, toupper(daemon));
+		printf(redist_str_fmt, rstr);
+		printf(redist_help_def_fmt, toupper(daemon));
+		printf("%s", hstr);
+		printf(redist_help_str_fmt, redist_bare_help);
+		print("\n");
+		
+	}
+	
+	#printf("#define QUAGGA_REDIST_STR_ALL %s\n",all);
+			
+#	for (i = 0; i < lcount; i++) {
+#		if (mlists[i] != "")
+#			printf (mlistformat "\n", mlists[i]);
+#	}
+	printf (footer);
+}
diff --git a/lib/route_types.txt b/lib/route_types.txt
new file mode 100644
index 0000000..e99cacd
--- /dev/null
+++ b/lib/route_types.txt
@@ -0,0 +1,74 @@
+# Canonical Zserv route types information registry for Quagga.
+#
+# Used to construct route_types.c and route_types.h
+#
+# comma-seperated fields of either 2 fields (help strings) or 7 fields.
+# White space before and after the comma seperators is stripped.
+# Lines /beginning/ with # are comments.
+#
+####
+# 7 field line has format:
+# ZServ route type, canonical name, daemon, route char, ipv4, ipv6, short desc
+#
+# Zserv route type: 	Corresponding with zebra.h. Key field.
+# canonical name:	Typically derived from the route type definition.
+#			Used in 'redistribute' commands in daemons.
+#			Key field.
+# daemon:		The daemon which may originates this route type
+#			for redistribution to other daemons.
+#			NULL if not applicable.
+#			M:N definitions of type:daemon are allowed.
+#			Used to construct vty command strings.
+# route char:		Single character to denote the route, if applicable.
+#			Used to denote route type where space is tight,
+#			e.g. 'show ip route' / 'show ipv6 route'.
+#			'X' is reserved as the 'not needed' placeholder.
+# ipv4:			IPv4 capable? yes/no, or 1/0.
+# ipv6:			IPv6 capable? ditto.
+# short desc:		Very brief description. Used in header of
+#			'show ip route'. May be specified as NULL
+#			if the canonical name suffices.
+#
+# Key fields obviously must be a unique ASCII alpha-numeric word.
+#   Lower-case is required, brevity is optional but highly desirable.
+#
+####
+# 2 field format:
+#
+# Zserv route type, Long description
+#
+# Long description:     Full description, but should try fit on a line.
+####
+
+##  type                cname      daemon  C    4  6  short help
+ZEBRA_ROUTE_SYSTEM,     system,    NULL,   'X', 0, 0, "Reserved"
+ZEBRA_ROUTE_KERNEL,     kernel,    zebra,  'K', 1, 1, NULL
+ZEBRA_ROUTE_CONNECT,    connected, zebra,  'C', 1, 1, NULL
+ZEBRA_ROUTE_STATIC,     static,    zebra,  'S', 1, 1, NULL
+ZEBRA_ROUTE_RIP,        rip,       ripd,   'R', 1, 0, "RIP"
+ZEBRA_ROUTE_RIPNG,      ripng,     ripngd, 'R', 0, 1, "RIPng"
+ZEBRA_ROUTE_OSPF,       ospf,      ospfd,  'O', 1, 0, "OSPF"
+ZEBRA_ROUTE_OSPF6,      ospf6,     ospf6d, 'O', 0, 1, "OSPF"
+ZEBRA_ROUTE_ISIS,       isis,      isisd,  'I', 1, 1, "IS-IS"
+ZEBRA_ROUTE_BGP,        bgp,       bgpd,   'B', 1, 1, "BGP"
+# HSLS and OLSR both are AFI independent (so: 1, 1), however
+# we want to disable for them for general Quagga distribution.
+# This at least makes it trivial for users of these protocols
+# to 'switch on' redist support (direct numeric entry remaining
+# possible).
+ZEBRA_ROUTE_HSLS,       hsls,      hslsd,  'H', 0, 0, "HSLS"
+ZEBRA_ROUTE_OLSR,       olsr,      oslrd,  'o', 0, 0, "OLSR"
+
+## help strings
+ZEBRA_ROUTE_SYSTEM, "Reserved route type, for internal use only"
+ZEBRA_ROUTE_KERNEL, "Kernel routes (not installed via the zebra RIB)"
+ZEBRA_ROUTE_CONNECT,"Connected routes (directly attached subnet or host)"
+ZEBRA_ROUTE_STATIC, "Statically configured routes"
+ZEBRA_ROUTE_RIP,    "Routing Information Protocol (RIP)"
+ZEBRA_ROUTE_RIPNG,  "Routing Information Protocol next-generation (IPv6) (RIPng)"
+ZEBRA_ROUTE_OSPF,   "Open Shortest Path First (OSPFv2)"
+ZEBRA_ROUTE_OSPF6,  "Open Shortest Path First (IPv6) (OSPFv3)"
+ZEBRA_ROUTE_ISIS,   "Intermediate System to Intermediate System (IS-IS)"
+ZEBRA_ROUTE_BGP,    "Border Gateway Protocol (BGP)"
+ZEBRA_ROUTE_HSLS,   "Hazy-Sighted Link State Protocol (HSLS)"
+ZEBRA_ROUTE_OLSR,   "Optimised Link State Routing (OLSR)"