]> git.proxmox.com Git - mirror_frr.git/blame - babeld/babel_zebra.c
babeld: changes for code maintainability
[mirror_frr.git] / babeld / babel_zebra.c
CommitLineData
ca10883e
DS
1/*
2Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21*/
22
23/* FRR's includes */
24#include <zebra.h>
25#include "command.h"
26#include "zclient.h"
27#include "stream.h"
28
29/* babel's includes*/
30#include "babel_zebra.h"
31#include "babel_interface.h"
32#include "xroute.h"
33#include "util.h"
34
35void babelz_zebra_init(void);
36
37
38/* we must use a pointer because of zclient.c's functions (new, free). */
39struct zclient *zclient;
ca10883e
DS
40
41/* Debug types */
2b64873d 42static const struct {
ca10883e
DS
43 int type;
44 int str_min_len;
45 const char *str;
46} debug_type[] = {
47 {BABEL_DEBUG_COMMON, 1, "common"},
48 {BABEL_DEBUG_KERNEL, 1, "kernel"},
49 {BABEL_DEBUG_FILTER, 1, "filter"},
50 {BABEL_DEBUG_TIMEOUT, 1, "timeout"},
51 {BABEL_DEBUG_IF, 1, "interface"},
52 {BABEL_DEBUG_ROUTE, 1, "route"},
53 {BABEL_DEBUG_ALL, 1, "all"},
54 {0, 0, NULL}
55};
56
74489921 57/* Zebra route add and delete treatment. */
ca10883e 58static int
121f9dee 59babel_zebra_read_route (ZAPI_CALLBACK_ARGS)
ca10883e 60{
74489921 61 struct zapi_route api;
ca10883e 62
74489921
RW
63 if (zapi_route_decode(zclient->ibuf, &api) < 0)
64 return -1;
ca10883e 65
74489921
RW
66 /* we completely ignore srcdest routes for now. */
67 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
68 return 0;
ca10883e 69
121f9dee 70 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
74489921 71 babel_route_add(&api);
ca10883e 72 } else {
74489921 73 babel_route_delete(&api);
ca10883e
DS
74 }
75
76 return 0;
77}
78
ca10883e
DS
79/* [Babel Command] */
80DEFUN (babel_redistribute_type,
81 babel_redistribute_type_cmd,
dd8765ca
RW
82 "[no] redistribute <ipv4 " FRR_IP_REDIST_STR_BABELD "|ipv6 " FRR_IP6_REDIST_STR_BABELD ">",
83 NO_STR
ca10883e 84 "Redistribute\n"
dd8765ca
RW
85 "Redistribute IPv4 routes\n"
86 FRR_IP_REDIST_HELP_STR_BABELD
87 "Redistribute IPv6 routes\n"
88 FRR_IP6_REDIST_HELP_STR_BABELD)
ca10883e 89{
dd8765ca
RW
90 int negate = 0;
91 int family;
92 int afi;
ca10883e 93 int type;
dd8765ca 94 int idx = 0;
ca10883e 95
dd8765ca
RW
96 if (argv_find(argv, argc, "no", &idx))
97 negate = 1;
98 argv_find(argv, argc, "redistribute", &idx);
99 family = str2family(argv[idx + 1]->text);
100 if (family < 0)
f1a05de9 101 return CMD_WARNING_CONFIG_FAILED;
ca10883e 102
dd8765ca
RW
103 afi = family2afi(family);
104 if (!afi)
105 return CMD_WARNING_CONFIG_FAILED;
ca10883e 106
dd8765ca 107 type = proto_redistnum(afi, argv[idx + 2]->text);
ca10883e 108 if (type < 0) {
dd8765ca 109 vty_out (vty, "Invalid type %s\n", argv[idx + 2]->arg);
f1a05de9 110 return CMD_WARNING_CONFIG_FAILED;
ca10883e
DS
111 }
112
dd8765ca
RW
113 if (!negate)
114 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type, 0, VRF_DEFAULT);
115 else {
116 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, 0, VRF_DEFAULT);
117 /* perhaps should we remove xroutes having the same type... */
118 }
ca10883e
DS
119 return CMD_SUCCESS;
120}
121
122#ifndef NO_DEBUG
123/* [Babel Command] */
124DEFUN (debug_babel,
125 debug_babel_cmd,
f7bf422e 126 "debug babel <common|kernel|filter|timeout|interface|route|all>",
ca10883e
DS
127 "Enable debug messages for specific or all part.\n"
128 "Babel information\n"
129 "Common messages (default)\n"
130 "Kernel messages\n"
131 "Filter messages\n"
132 "Timeout messages\n"
133 "Interface messages\n"
134 "Route messages\n"
135 "All messages\n")
136{
137 int i;
138
139 for(i = 0; debug_type[i].str != NULL; i++) {
140 if (strncmp (debug_type[i].str, argv[2]->arg,
141 debug_type[i].str_min_len) == 0) {
03b7208c 142 SET_FLAG(debug, debug_type[i].type);
ca10883e
DS
143 return CMD_SUCCESS;
144 }
145 }
146
5c7571d4 147 vty_out (vty, "Invalid type %s\n", argv[2]->arg);
ca10883e 148
f1a05de9 149 return CMD_WARNING_CONFIG_FAILED;
ca10883e
DS
150}
151
152/* [Babel Command] */
153DEFUN (no_debug_babel,
154 no_debug_babel_cmd,
f7bf422e 155 "no debug babel <common|kernel|filter|timeout|interface|route|all>",
ca10883e
DS
156 NO_STR
157 "Disable debug messages for specific or all part.\n"
158 "Babel information\n"
159 "Common messages (default)\n"
160 "Kernel messages\n"
161 "Filter messages\n"
162 "Timeout messages\n"
163 "Interface messages\n"
164 "Route messages\n"
165 "All messages\n")
166{
167 int i;
168
169 for (i = 0; debug_type[i].str; i++) {
170 if (strncmp(debug_type[i].str, argv[3]->arg,
171 debug_type[i].str_min_len) == 0) {
03b7208c 172 UNSET_FLAG(debug, debug_type[i].type);
ca10883e
DS
173 return CMD_SUCCESS;
174 }
175 }
176
5c7571d4 177 vty_out (vty, "Invalid type %s\n", argv[3]->arg);
ca10883e 178
f1a05de9 179 return CMD_WARNING_CONFIG_FAILED;
ca10883e
DS
180}
181#endif /* NO_DEBUG */
182
183/* Output "debug" statement lines, if necessary. */
184int
185debug_babel_config_write (struct vty * vty)
186{
187#ifdef NO_DEBUG
188 return 0;
189#else
190 int i, lines = 0;
191
192 if (debug == BABEL_DEBUG_ALL)
193 {
5c7571d4 194 vty_out (vty, "debug babel all\n");
ca10883e
DS
195 lines++;
196 }
197 else
03b7208c 198 {
ca10883e 199 for (i = 0; debug_type[i].str != NULL; i++)
03b7208c 200 {
201 if (debug_type[i].type != BABEL_DEBUG_ALL
202 && CHECK_FLAG (debug, debug_type[i].type))
ca10883e 203 {
5c7571d4 204 vty_out (vty, "debug babel %s\n", debug_type[i].str);
ca10883e
DS
205 lines++;
206 }
03b7208c 207 }
208 }
209
ca10883e
DS
210 if (lines)
211 {
5c7571d4 212 vty_out (vty, "!\n");
ca10883e
DS
213 lines++;
214 }
215 return lines;
216#endif /* NO_DEBUG */
217}
218
87f6dc50
DS
219DEFUN_NOSH (show_debugging_babel,
220 show_debugging_babel_cmd,
221 "show debugging [babel]",
222 SHOW_STR
223 DEBUG_STR
224 "Babel")
225{
226 vty_out(vty, "BABEL debugging status\n");
227
228 debug_babel_config_write(vty);
229
cf00164b
DS
230 cmd_show_lib_debugs(vty);
231
87f6dc50
DS
232 return CMD_SUCCESS;
233}
234
abdcf266
DS
235static void
236babel_zebra_connected (struct zclient *zclient)
237{
238 zclient_send_reg_requests (zclient, VRF_DEFAULT);
239}
240
a243d1db
DL
241static zclient_handler *const babel_handlers[] = {
242 [ZEBRA_INTERFACE_ADDRESS_ADD] = babel_interface_address_add,
243 [ZEBRA_INTERFACE_ADDRESS_DELETE] = babel_interface_address_delete,
244 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = babel_zebra_read_route,
245 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = babel_zebra_read_route,
246};
247
ca10883e
DS
248void babelz_zebra_init(void)
249{
a243d1db
DL
250 zclient = zclient_new(master, &zclient_options_default, babel_handlers,
251 array_size(babel_handlers));
342213ea 252 zclient_init(zclient, ZEBRA_ROUTE_BABEL, 0, &babeld_privs);
ca10883e 253
abdcf266 254 zclient->zebra_connected = babel_zebra_connected;
ca10883e 255
ca10883e 256 install_element(BABEL_NODE, &babel_redistribute_type_cmd);
ca10883e
DS
257 install_element(ENABLE_NODE, &debug_babel_cmd);
258 install_element(ENABLE_NODE, &no_debug_babel_cmd);
259 install_element(CONFIG_NODE, &debug_babel_cmd);
260 install_element(CONFIG_NODE, &no_debug_babel_cmd);
87f6dc50 261
dd73744d 262 install_element(ENABLE_NODE, &show_debugging_babel_cmd);
ca10883e
DS
263}
264
ca10883e
DS
265void
266babel_zebra_close_connexion(void)
267{
268 zclient_stop(zclient);
6b02aecb 269 zclient_free(zclient);
ca10883e 270}