]> git.proxmox.com Git - mirror_frr.git/blame - lib/route_types.pl
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / lib / route_types.pl
CommitLineData
e0ca5fde 1#!/usr/bin/perl
47a3a827 2# SPDX-License-Identifier: GPL-2.0-or-later
e0ca5fde
DL
3##
4## Scan a file of route-type definitions (see eg route_types.txt) and
5## generate a corresponding header file with:
6##
7## - enum of Zserv route-types
8## - redistribute strings for the various Quagga daemons
9##
10## See route_types.txt for the format.
11##
12##
13## Copyright (C) 2009 David Lamparter.
e0ca5fde
DL
14##
15
16use strict;
b3b4dfa5 17use Getopt::Long;
e0ca5fde
DL
18
19# input processing
20#
21my @protos;
22my %protodetail;
23
24my %daemons;
25
b3b4dfa5
IR
26my @enabled;
27
28GetOptions ("enabled=s" => \@enabled);
29@enabled = split(/,/,join(',',@enabled));
30
e0ca5fde
DL
31while (<STDIN>) {
32 # skip comments and empty lines
33 next if (/^\s*(#|$)/);
34
35 # strip whitespace
36 chomp;
37 $_ =~ s/^\s*//;
38 $_ =~ s/\s*$//;
39
40 # match help strings
41 if (/^(ZEBRA_ROUTE_[^\s]+)\s*,\s*"(.*)"$/) {
42 $protodetail{$1}->{'longhelp'} = $2;
43 next;
44 }
45
46 $_ =~ s/\s*,\s*/,/g;
47
82557cf0 48 # else: 8-field line
e0ca5fde 49 my @f = split(/,/, $_);
b3b4dfa5 50 unless (@f == 9 || @f == 10) {
e0ca5fde
DL
51 die "invalid input on route_types line $.\n";
52 }
53
54 my $proto = $f[0];
55 $f[3] = $1 if ($f[3] =~ /^'(.*)'$/);
82557cf0 56 $f[7] = $1 if ($f[7] =~ /^"(.*)"$/);
e0ca5fde
DL
57
58 $protodetail{$proto} = {
59 "number" => scalar @protos,
60 "type" => $f[0],
61 "cname" => $f[1],
62 "daemon" => $f[2],
63 "char" => $f[3],
64 "ipv4" => int($f[4]),
65 "ipv6" => int($f[5]),
82557cf0
DS
66 "redist" => int($f[6]),
67 "shorthelp" => $f[7],
b3b4dfa5
IR
68 "enabled" => $f[8],
69 "restrict2" => $f[9],
e0ca5fde
DL
70 };
71 push @protos, $proto;
72 $daemons{$f[2]} = {
73 "ipv4" => int($f[4]),
74 "ipv6" => int($f[5])
75 } unless ($f[2] eq "NULL");
76}
77
78# output
79printf <<EOF, $ARGV[0];
80/* Auto-generated from route_types.txt by %s. */
81/* Do not edit! */
82
ab0181ee
DL
83#ifndef _FRR_ROUTE_TYPES_H
84#define _FRR_ROUTE_TYPES_H
e0ca5fde 85
9f0ea7d4 86/* Zebra route's' types. */
e0ca5fde
DL
87EOF
88
89push @protos, "ZEBRA_ROUTE_MAX";
90my (@protosv4, @protosv6) = ((), ());
91for (my $c = 0; $c < @protos; $c++) {
92 my $p = $protos[$c];
93 printf "#define %-32s %d\n", $p, $c;
94 push @protosv4, $p if ($protodetail{$p}->{"ipv4"});
95 push @protosv6, $p if ($protodetail{$p}->{"ipv6"});
96}
97pop @protos;
98
99sub codelist {
100 my (@protos) = @_;
101 my (@lines) = ();
102 my $str = " \"Codes: ";
103 for my $p (@protos) {
b3b4dfa5 104 next unless (grep $_ eq $protodetail{$p}->{"enabled"}, @enabled);
e0ca5fde
DL
105 my $s = sprintf("%s - %s, ",
106 $protodetail{$p}->{"char"},
107 $protodetail{$p}->{"shorthelp"});
108 if (length($str . $s) > 70) {
109 $str =~ s/ $//;
625e016d 110 push @lines, $str . "\\n\" \\\n";
e0ca5fde
DL
111 $str = " \" ";
112 }
113 $str .= $s;
114 }
115 $str =~ s/ $//;
625e016d 116 push @lines, $str . "\\n\" \\\n";
5a3cf853 117 push @lines, " \" > - selected route, * - FIB route, q - queued, r - rejected, b - backup\\n\"";
e3490d1f 118 push @lines, " \" t - trapped, o - offload failure\\n\\n\"";
5a3cf853
DS
119
120
625e016d 121 return join("", @lines);
e0ca5fde
DL
122}
123
124print "\n";
125printf "#define SHOW_ROUTE_V4_HEADER \\\n%s\n", codelist(@protosv4);
126printf "#define SHOW_ROUTE_V6_HEADER \\\n%s\n", codelist(@protosv6);
127print "\n";
128
129sub collect {
9f0ea7d4 130 my ($daemon, $ipv4, $ipv6, $any) = @_;
e0ca5fde
DL
131 my (@names, @help) = ((), ());
132 for my $p (@protos) {
133 next if ($protodetail{$p}->{"daemon"} eq $daemon && $daemon ne "zebra");
5ee62c66
PZ
134 next if ($protodetail{$p}->{"restrict2"} ne "" &&
135 $protodetail{$p}->{"restrict2"} ne $daemon);
82557cf0 136 next if ($protodetail{$p}->{"redist"} eq 0);
b3b4dfa5 137 next unless (grep $_ eq $protodetail{$p}->{"enabled"}, @enabled);
e0ca5fde 138 next unless (($ipv4 && $protodetail{$p}->{"ipv4"})
82557cf0 139 || ($ipv6 && $protodetail{$p}->{"ipv6"}));
e0ca5fde
DL
140 push @names, $protodetail{$p}->{"cname"};
141 push @help, " \"".$protodetail{$p}->{"longhelp"}."\\n\"";
142 }
9f0ea7d4
DS
143 if ($any == 1) {
144 push @names, "any";
145 push @help, " \"Any of the above protocols\\n\"";
146 }
c3d89003 147 return ("\"<" . join("|", @names) . ">\"", join(" \\\n", @help));
e0ca5fde
DL
148}
149
150for my $daemon (sort keys %daemons) {
151 next unless ($daemons{$daemon}->{"ipv4"} || $daemons{$daemon}->{"ipv6"});
152 printf "/* %s */\n", $daemon;
153 if ($daemons{$daemon}->{"ipv4"} && $daemons{$daemon}->{"ipv6"}) {
9f0ea7d4 154 my ($names, $help) = collect($daemon, 1, 1, 0);
ab0181ee
DL
155 printf "#define FRR_REDIST_STR_%s \\\n %s\n", uc $daemon, $names;
156 printf "#define FRR_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
de69c444 157
9f0ea7d4 158 ($names, $help) = collect($daemon, 1, 0, 0);
ab0181ee
DL
159 printf "#define FRR_IP_REDIST_STR_%s \\\n %s\n", uc $daemon, $names;
160 printf "#define FRR_IP_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
de69c444 161
9f0ea7d4 162 ($names, $help) = collect($daemon, 0, 1, 0);
ab0181ee
DL
163 printf "#define FRR_IP6_REDIST_STR_%s \\\n %s\n", uc $daemon, $names;
164 printf "#define FRR_IP6_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
de69c444 165
9f0ea7d4 166 if ($daemon eq "zebra") {
de69c444 167 ($names, $help) = collect($daemon, 1, 0, 1);
ab0181ee
DL
168 printf "#define FRR_IP_PROTOCOL_MAP_STR_%s \\\n %s\n", uc $daemon, $names;
169 printf "#define FRR_IP_PROTOCOL_MAP_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
de69c444
DW
170
171 ($names, $help) = collect($daemon, 0, 1, 1);
ab0181ee
DL
172 printf "#define FRR_IP6_PROTOCOL_MAP_STR_%s \\\n %s\n", uc $daemon, $names;
173 printf "#define FRR_IP6_PROTOCOL_MAP_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
9f0ea7d4 174 }
e0ca5fde
DL
175 } else {
176 my ($names, $help) = collect($daemon,
9f0ea7d4 177 $daemons{$daemon}->{"ipv4"}, $daemons{$daemon}->{"ipv6"}, 0);
ab0181ee
DL
178 printf "#define FRR_REDIST_STR_%s \\\n %s\n", uc $daemon, $names;
179 printf "#define FRR_REDIST_HELP_STR_%s \\\n%s\n", uc $daemon, $help;
e0ca5fde
DL
180 }
181 print "\n";
182}
183
184print <<EOF;
185
ab0181ee 186#ifdef FRR_DEFINE_DESC_TABLE
e0ca5fde
DL
187
188struct zebra_desc_table
189{
190 unsigned int type;
191 const char *string;
192 char chr;
193};
194
195#define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) }
196static const struct zebra_desc_table route_types[] = {
197EOF
198
199for (my $c = 0; $c < @protos; $c++) {
200 my $p = $protos[$c];
201 printf " DESC_ENTRY\t(%s\t \"%s\",\t'%s' ),\n",
202 $p.",", $protodetail{$p}->{"cname"}, $protodetail{$p}->{"char"};
203}
204
205print <<EOF;
206};
207#undef DESC_ENTRY
208
ab0181ee 209#endif /* FRR_DEFINE_DESC_TABLE */
e0ca5fde 210
ab0181ee 211#endif /* _FRR_ROUTE_TYPES_H */
e0ca5fde
DL
212EOF
213