]> git.proxmox.com Git - mirror_frr.git/blob - babeld/babel_zebra.c
*: remove leftovers from "router zebra"
[mirror_frr.git] / babeld / babel_zebra.c
1 /*
2 Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE 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
35 void babelz_zebra_init(void);
36
37
38 /* we must use a pointer because of zclient.c's functions (new, free). */
39 struct zclient *zclient;
40
41 /* Debug types */
42 static struct {
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
57 /* Zebra route add and delete treatment (ipv6). */
58 static int
59 babel_zebra_read_ipv6 (int command, struct zclient *zclient,
60 zebra_size_t length, vrf_id_t vrf)
61 {
62 struct stream *s;
63 struct zapi_ipv6 api;
64 unsigned long ifindex = -1;
65 struct in6_addr nexthop;
66 struct prefix_ipv6 prefix;
67
68 s = zclient->ibuf;
69 ifindex = 0;
70 memset (&nexthop, 0, sizeof (struct in6_addr));
71 memset (&api, 0, sizeof(struct zapi_ipv6));
72 memset (&prefix, 0, sizeof (struct prefix_ipv6));
73
74 /* Type, flags, message. */
75 api.type = stream_getc (s);
76 api.instance = stream_getw (s);
77 api.flags = stream_getl (s);
78 api.message = stream_getc (s);
79
80 /* IPv6 prefix. */
81 prefix.family = AF_INET6;
82 prefix.prefixlen = stream_getc (s);
83 stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));
84
85 /* Nexthop, ifindex, distance, metric. */
86 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
87 api.nexthop_num = stream_getc (s);
88 stream_get (&nexthop, s, sizeof(nexthop));
89 }
90 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
91 api.ifindex_num = stream_getc (s);
92 ifindex = stream_getl (s);
93 }
94 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
95 api.distance = stream_getc (s);
96 else
97 api.distance = 0;
98 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
99 api.metric = stream_getl (s);
100 else
101 api.metric = 0;
102
103 if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
104 babel_ipv6_route_add(&api, &prefix, ifindex, &nexthop);
105 else
106 babel_ipv6_route_delete(&api, &prefix, ifindex);
107
108 return 0;
109 }
110
111 static int
112 babel_zebra_read_ipv4 (int command, struct zclient *zclient,
113 zebra_size_t length, vrf_id_t vrf)
114 {
115 struct stream *s;
116 struct zapi_ipv4 api;
117 unsigned long ifindex = -1;
118 struct in_addr nexthop;
119 struct prefix_ipv4 prefix;
120
121 s = zclient->ibuf;
122 ifindex = 0;
123 memset (&nexthop, 0, sizeof (struct in_addr));
124 memset (&api, 0, sizeof(struct zapi_ipv4));
125 memset (&prefix, 0, sizeof (struct prefix_ipv4));
126
127 /* Type, flags, message. */
128 api.type = stream_getc (s);
129 api.instance = stream_getw (s);
130 api.flags = stream_getl (s);
131 api.message = stream_getc (s);
132
133 /* IPv6 prefix. */
134 prefix.family = AF_INET;
135 prefix.prefixlen = stream_getc (s);
136 stream_get (&prefix.prefix, s, PSIZE (prefix.prefixlen));
137
138 /* Nexthop, ifindex, distance, metric. */
139 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
140 api.nexthop_num = stream_getc (s);
141 stream_get (&nexthop, s, sizeof(nexthop));
142 }
143 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
144 api.ifindex_num = stream_getc (s);
145 ifindex = stream_getl (s);
146 }
147 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
148 api.distance = stream_getc (s);
149 else
150 api.distance = 0;
151 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
152 api.metric = stream_getl (s);
153 else
154 api.metric = 0;
155
156 if (command == ZEBRA_REDISTRIBUTE_IPV4_ADD) {
157 babel_ipv4_route_add(&api, &prefix, ifindex, &nexthop);
158 } else {
159 babel_ipv4_route_delete(&api, &prefix, ifindex);
160 }
161
162 return 0;
163 }
164
165 /* [Babel Command] */
166 DEFUN (babel_redistribute_type,
167 babel_redistribute_type_cmd,
168 "[no] redistribute <ipv4 " FRR_IP_REDIST_STR_BABELD "|ipv6 " FRR_IP6_REDIST_STR_BABELD ">",
169 NO_STR
170 "Redistribute\n"
171 "Redistribute IPv4 routes\n"
172 FRR_IP_REDIST_HELP_STR_BABELD
173 "Redistribute IPv6 routes\n"
174 FRR_IP6_REDIST_HELP_STR_BABELD)
175 {
176 int negate = 0;
177 int family;
178 int afi;
179 int type;
180 int idx = 0;
181
182 if (argv_find(argv, argc, "no", &idx))
183 negate = 1;
184 argv_find(argv, argc, "redistribute", &idx);
185 family = str2family(argv[idx + 1]->text);
186 if (family < 0)
187 return CMD_WARNING_CONFIG_FAILED;
188
189 afi = family2afi(family);
190 if (!afi)
191 return CMD_WARNING_CONFIG_FAILED;
192
193 type = proto_redistnum(afi, argv[idx + 2]->text);
194 if (type < 0) {
195 vty_out (vty, "Invalid type %s\n", argv[idx + 2]->arg);
196 return CMD_WARNING_CONFIG_FAILED;
197 }
198
199 if (!negate)
200 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type, 0, VRF_DEFAULT);
201 else {
202 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, 0, VRF_DEFAULT);
203 /* perhaps should we remove xroutes having the same type... */
204 }
205 return CMD_SUCCESS;
206 }
207
208 #ifndef NO_DEBUG
209 /* [Babel Command] */
210 DEFUN (debug_babel,
211 debug_babel_cmd,
212 "debug babel <common|kernel|filter|timeout|interface|route|all>",
213 "Enable debug messages for specific or all part.\n"
214 "Babel information\n"
215 "Common messages (default)\n"
216 "Kernel messages\n"
217 "Filter messages\n"
218 "Timeout messages\n"
219 "Interface messages\n"
220 "Route messages\n"
221 "All messages\n")
222 {
223 int i;
224
225 for(i = 0; debug_type[i].str != NULL; i++) {
226 if (strncmp (debug_type[i].str, argv[2]->arg,
227 debug_type[i].str_min_len) == 0) {
228 debug |= debug_type[i].type;
229 return CMD_SUCCESS;
230 }
231 }
232
233 vty_out (vty, "Invalid type %s\n", argv[2]->arg);
234
235 return CMD_WARNING_CONFIG_FAILED;
236 }
237
238 /* [Babel Command] */
239 DEFUN (no_debug_babel,
240 no_debug_babel_cmd,
241 "no debug babel <common|kernel|filter|timeout|interface|route|all>",
242 NO_STR
243 "Disable debug messages for specific or all part.\n"
244 "Babel information\n"
245 "Common messages (default)\n"
246 "Kernel messages\n"
247 "Filter messages\n"
248 "Timeout messages\n"
249 "Interface messages\n"
250 "Route messages\n"
251 "All messages\n")
252 {
253 int i;
254
255 for (i = 0; debug_type[i].str; i++) {
256 if (strncmp(debug_type[i].str, argv[3]->arg,
257 debug_type[i].str_min_len) == 0) {
258 debug &= ~debug_type[i].type;
259 return CMD_SUCCESS;
260 }
261 }
262
263 vty_out (vty, "Invalid type %s\n", argv[3]->arg);
264
265 return CMD_WARNING_CONFIG_FAILED;
266 }
267 #endif /* NO_DEBUG */
268
269 /* Output "debug" statement lines, if necessary. */
270 int
271 debug_babel_config_write (struct vty * vty)
272 {
273 #ifdef NO_DEBUG
274 return 0;
275 #else
276 int i, lines = 0;
277
278 if (debug == BABEL_DEBUG_ALL)
279 {
280 vty_out (vty, "debug babel all\n");
281 lines++;
282 }
283 else
284 for (i = 0; debug_type[i].str != NULL; i++)
285 if
286 (
287 debug_type[i].type != BABEL_DEBUG_ALL
288 && CHECK_FLAG (debug, debug_type[i].type)
289 )
290 {
291 vty_out (vty, "debug babel %s\n", debug_type[i].str);
292 lines++;
293 }
294 if (lines)
295 {
296 vty_out (vty, "!\n");
297 lines++;
298 }
299 return lines;
300 #endif /* NO_DEBUG */
301 }
302
303 DEFUN_NOSH (show_debugging_babel,
304 show_debugging_babel_cmd,
305 "show debugging [babel]",
306 SHOW_STR
307 DEBUG_STR
308 "Babel")
309 {
310 vty_out(vty, "BABEL debugging status\n");
311
312 debug_babel_config_write(vty);
313
314 return CMD_SUCCESS;
315 }
316
317 static void
318 babel_zebra_connected (struct zclient *zclient)
319 {
320 zclient_send_reg_requests (zclient, VRF_DEFAULT);
321 }
322
323 void babelz_zebra_init(void)
324 {
325 zclient = zclient_new(master);
326 zclient_init(zclient, ZEBRA_ROUTE_BABEL, 0);
327
328 zclient->zebra_connected = babel_zebra_connected;
329 zclient->interface_add = babel_interface_add;
330 zclient->interface_delete = babel_interface_delete;
331 zclient->interface_up = babel_interface_up;
332 zclient->interface_down = babel_interface_down;
333 zclient->interface_address_add = babel_interface_address_add;
334 zclient->interface_address_delete = babel_interface_address_delete;
335 zclient->redistribute_route_ipv4_add = babel_zebra_read_ipv4;
336 zclient->redistribute_route_ipv4_del = babel_zebra_read_ipv4;
337 zclient->redistribute_route_ipv6_add = babel_zebra_read_ipv6;
338 zclient->redistribute_route_ipv6_del = babel_zebra_read_ipv6;
339
340 install_element(BABEL_NODE, &babel_redistribute_type_cmd);
341 install_element(ENABLE_NODE, &debug_babel_cmd);
342 install_element(ENABLE_NODE, &no_debug_babel_cmd);
343 install_element(CONFIG_NODE, &debug_babel_cmd);
344 install_element(CONFIG_NODE, &no_debug_babel_cmd);
345
346 install_element(VIEW_NODE, &show_debugging_babel_cmd);
347 }
348
349 void
350 babel_zebra_close_connexion(void)
351 {
352 zclient_stop(zclient);
353 zclient_free(zclient);
354 }