]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zlookup.c
Merge pull request #5494 from opensourcerouting/mlag-module
[mirror_frr.git] / pimd / pim_zlookup.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2008 Everton da Silva Marques
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21
22 #include "log.h"
23 #include "prefix.h"
24 #include "zclient.h"
25 #include "stream.h"
26 #include "network.h"
27 #include "thread.h"
28 #include "prefix.h"
29 #include "vty.h"
30 #include "lib_errors.h"
31
32 #include "pimd.h"
33 #include "pim_iface.h"
34 #include "pim_pim.h"
35 #include "pim_str.h"
36 #include "pim_oil.h"
37 #include "pim_zlookup.h"
38
39 static struct zclient *zlookup = NULL;
40 struct thread *zlookup_read;
41
42 static void zclient_lookup_sched(struct zclient *zlookup, int delay);
43 static int zclient_lookup_read_pipe(struct thread *thread);
44
45 /* Connect to zebra for nexthop lookup. */
46 static int zclient_lookup_connect(struct thread *t)
47 {
48 struct zclient *zlookup;
49
50 zlookup = THREAD_ARG(t);
51
52 if (zlookup->sock >= 0) {
53 return 0;
54 }
55
56 if (zclient_socket_connect(zlookup) < 0) {
57 ++zlookup->fail;
58 zlog_warn("%s: failure connecting zclient socket: failures=%d",
59 __PRETTY_FUNCTION__, zlookup->fail);
60 } else {
61 zlookup->fail = 0; /* reset counter on connection */
62 }
63
64 if (zlookup->sock < 0) {
65 /* Since last connect failed, retry within 10 secs */
66 zclient_lookup_sched(zlookup, 10);
67 return -1;
68 }
69
70 thread_add_timer(router->master, zclient_lookup_read_pipe, zlookup, 60,
71 &zlookup_read);
72 return 0;
73 }
74
75 /* Schedule connection with delay. */
76 static void zclient_lookup_sched(struct zclient *zlookup, int delay)
77 {
78 thread_add_timer(router->master, zclient_lookup_connect, zlookup, delay,
79 &zlookup->t_connect);
80
81 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
82 __PRETTY_FUNCTION__, delay);
83 }
84
85 /* Schedule connection for now. */
86 static void zclient_lookup_sched_now(struct zclient *zlookup)
87 {
88 thread_add_event(router->master, zclient_lookup_connect, zlookup, 0,
89 &zlookup->t_connect);
90
91 zlog_notice("%s: zclient lookup immediate connection scheduled",
92 __PRETTY_FUNCTION__);
93 }
94
95 /* Schedule reconnection, if needed. */
96 static void zclient_lookup_reconnect(struct zclient *zlookup)
97 {
98 if (zlookup->t_connect) {
99 return;
100 }
101
102 zclient_lookup_sched_now(zlookup);
103 }
104
105 static void zclient_lookup_failed(struct zclient *zlookup)
106 {
107 if (zlookup->sock >= 0) {
108 if (close(zlookup->sock)) {
109 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__,
110 zlookup->sock, errno, safe_strerror(errno));
111 }
112 zlookup->sock = -1;
113 }
114
115 zclient_lookup_reconnect(zlookup);
116 }
117
118 void zclient_lookup_free(void)
119 {
120 thread_cancel(zlookup_read);
121 zclient_stop(zlookup);
122 zclient_free(zlookup);
123 zlookup = NULL;
124 }
125
126 void zclient_lookup_new(void)
127 {
128 zlookup = zclient_new(router->master, &zclient_options_default);
129 if (!zlookup) {
130 flog_err(EC_LIB_ZAPI_SOCKET, "%s: zclient_new() failure",
131 __PRETTY_FUNCTION__);
132 return;
133 }
134
135 zlookup->sock = -1;
136 zlookup->t_connect = NULL;
137 zlookup->privs = &pimd_privs;
138
139 zclient_lookup_sched_now(zlookup);
140
141 zlog_notice("%s: zclient lookup socket initialized",
142 __PRETTY_FUNCTION__);
143 }
144
145 static int zclient_read_nexthop(struct pim_instance *pim,
146 struct zclient *zlookup,
147 struct pim_zlookup_nexthop nexthop_tab[],
148 const int tab_size, struct in_addr addr)
149 {
150 int num_ifindex = 0;
151 struct stream *s;
152 uint16_t length;
153 uint8_t marker;
154 uint8_t version;
155 vrf_id_t vrf_id;
156 uint16_t command = 0;
157 struct in_addr raddr;
158 uint8_t distance;
159 uint32_t metric;
160 int nexthop_num;
161 int i, err;
162
163 if (PIM_DEBUG_PIM_NHT_DETAIL) {
164 char addr_str[INET_ADDRSTRLEN];
165 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
166 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__, addr_str,
167 pim->vrf->name);
168 }
169
170 s = zlookup->ibuf;
171
172 while (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
173 stream_reset(s);
174 err = zclient_read_header(s, zlookup->sock, &length, &marker,
175 &version, &vrf_id, &command);
176 if (err < 0) {
177 flog_err(EC_LIB_ZAPI_MISSMATCH,
178 "%s: zclient_read_header() failed",
179 __PRETTY_FUNCTION__);
180 zclient_lookup_failed(zlookup);
181 return -1;
182 }
183 }
184
185 raddr.s_addr = stream_get_ipv4(s);
186
187 if (raddr.s_addr != addr.s_addr) {
188 char addr_str[INET_ADDRSTRLEN];
189 char raddr_str[INET_ADDRSTRLEN];
190 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
191 pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
192 zlog_warn("%s: address mismatch: addr=%s(%s) raddr=%s",
193 __PRETTY_FUNCTION__, addr_str, pim->vrf->name,
194 raddr_str);
195 /* warning only */
196 }
197
198 distance = stream_getc(s);
199 metric = stream_getl(s);
200 nexthop_num = stream_getc(s);
201
202 if (nexthop_num < 1) {
203 if (PIM_DEBUG_PIM_NHT_DETAIL)
204 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__,
205 zlookup->sock, nexthop_num);
206 return -6;
207 }
208
209 for (i = 0; i < nexthop_num; ++i) {
210 vrf_id_t nexthop_vrf_id;
211 enum nexthop_types_t nexthop_type;
212 struct pim_neighbor *nbr;
213 struct prefix p;
214
215 nexthop_vrf_id = stream_getl(s);
216 nexthop_type = stream_getc(s);
217 if (num_ifindex >= tab_size) {
218 char addr_str[INET_ADDRSTRLEN];
219 pim_inet4_dump("<addr?>", addr, addr_str,
220 sizeof(addr_str));
221 zlog_warn(
222 "%s: found too many nexthop ifindexes (%d > %d) for address %s(%s)",
223 __PRETTY_FUNCTION__, (num_ifindex + 1),
224 tab_size, addr_str, pim->vrf->name);
225 return num_ifindex;
226 }
227 nexthop_tab[num_ifindex].protocol_distance = distance;
228 nexthop_tab[num_ifindex].route_metric = metric;
229 nexthop_tab[num_ifindex].vrf_id = nexthop_vrf_id;
230 switch (nexthop_type) {
231 case NEXTHOP_TYPE_IFINDEX:
232 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
233 /*
234 * Connected route (i.e. no nexthop), use
235 * address passed in as PIM nexthop. This will
236 * allow us to work in cases where we are
237 * trying to find a route for this box.
238 */
239 nexthop_tab[num_ifindex].nexthop_addr.family = AF_INET;
240 nexthop_tab[num_ifindex].nexthop_addr.prefixlen =
241 IPV4_MAX_BITLEN;
242 nexthop_tab[num_ifindex].nexthop_addr.u.prefix4 =
243 addr;
244 ++num_ifindex;
245 break;
246 case NEXTHOP_TYPE_IPV4_IFINDEX:
247 case NEXTHOP_TYPE_IPV4:
248 nexthop_tab[num_ifindex].nexthop_addr.family = AF_INET;
249 nexthop_tab[num_ifindex].nexthop_addr.u.prefix4.s_addr =
250 stream_get_ipv4(s);
251 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
252 ++num_ifindex;
253 break;
254 case NEXTHOP_TYPE_IPV6_IFINDEX:
255 nexthop_tab[num_ifindex].nexthop_addr.family = AF_INET6;
256 stream_get(&nexthop_tab[num_ifindex]
257 .nexthop_addr.u.prefix6,
258 s, sizeof(struct in6_addr));
259 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
260
261 p.family = AF_INET6;
262 p.prefixlen = IPV6_MAX_PREFIXLEN;
263 memcpy(&p.u.prefix6,
264 &nexthop_tab[num_ifindex].nexthop_addr.u.prefix6,
265 sizeof(struct in6_addr));
266
267 /*
268 * If we are sending v6 secondary assume we receive v6
269 * secondary
270 */
271 if (pim->send_v6_secondary)
272 nbr = pim_neighbor_find_by_secondary(
273 if_lookup_by_index(
274 nexthop_tab[num_ifindex]
275 .ifindex,
276 nexthop_vrf_id),
277 &p);
278 else
279 nbr = pim_neighbor_find_if(if_lookup_by_index(
280 nexthop_tab[num_ifindex].ifindex,
281 nexthop_vrf_id));
282 if (nbr) {
283 nexthop_tab[num_ifindex].nexthop_addr.family =
284 AF_INET;
285 nexthop_tab[num_ifindex]
286 .nexthop_addr.u.prefix4 =
287 nbr->source_addr;
288 }
289 ++num_ifindex;
290 break;
291 default:
292 /* do nothing */
293 {
294 char addr_str[INET_ADDRSTRLEN];
295 pim_inet4_dump("<addr?>", addr, addr_str,
296 sizeof(addr_str));
297 zlog_warn(
298 "%s: found non-ifindex nexthop type=%d for address %s(%s)",
299 __PRETTY_FUNCTION__, nexthop_type,
300 addr_str, pim->vrf->name);
301 }
302 break;
303 }
304 }
305
306 return num_ifindex;
307 }
308
309 static int zclient_lookup_nexthop_once(struct pim_instance *pim,
310 struct pim_zlookup_nexthop nexthop_tab[],
311 const int tab_size, struct in_addr addr)
312 {
313 struct stream *s;
314 int ret;
315
316 if (PIM_DEBUG_PIM_NHT_DETAIL) {
317 char addr_str[INET_ADDRSTRLEN];
318 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
319 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__, addr_str,
320 pim->vrf->name);
321 }
322
323 /* Check socket. */
324 if (zlookup->sock < 0) {
325 flog_err(EC_LIB_ZAPI_SOCKET,
326 "%s: zclient lookup socket is not connected",
327 __PRETTY_FUNCTION__);
328 zclient_lookup_failed(zlookup);
329 return -1;
330 }
331
332 if (pim->vrf->vrf_id == VRF_UNKNOWN) {
333 zlog_notice(
334 "%s: VRF: %s does not fully exist yet, delaying lookup",
335 __PRETTY_FUNCTION__, pim->vrf->name);
336 return -1;
337 }
338
339 s = zlookup->obuf;
340 stream_reset(s);
341 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, pim->vrf_id);
342 stream_put_in_addr(s, &addr);
343 stream_putw_at(s, 0, stream_get_endp(s));
344
345 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
346 if (ret < 0) {
347 flog_err(
348 EC_LIB_SOCKET,
349 "%s: writen() failure: %d writing to zclient lookup socket",
350 __PRETTY_FUNCTION__, errno);
351 zclient_lookup_failed(zlookup);
352 return -2;
353 }
354 if (ret == 0) {
355 flog_err_sys(EC_LIB_SOCKET,
356 "%s: connection closed on zclient lookup socket",
357 __PRETTY_FUNCTION__);
358 zclient_lookup_failed(zlookup);
359 return -3;
360 }
361
362 return zclient_read_nexthop(pim, zlookup, nexthop_tab, tab_size, addr);
363 }
364
365 int zclient_lookup_read_pipe(struct thread *thread)
366 {
367 struct zclient *zlookup = THREAD_ARG(thread);
368 struct pim_instance *pim = pim_get_pim_instance(VRF_DEFAULT);
369 struct pim_zlookup_nexthop nexthop_tab[10];
370 struct in_addr l = {.s_addr = INADDR_ANY};
371
372 zclient_lookup_nexthop_once(pim, nexthop_tab, 10, l);
373 thread_add_timer(router->master, zclient_lookup_read_pipe, zlookup, 60,
374 &zlookup_read);
375
376 return 1;
377 }
378
379 int zclient_lookup_nexthop(struct pim_instance *pim,
380 struct pim_zlookup_nexthop nexthop_tab[],
381 const int tab_size, struct in_addr addr,
382 int max_lookup)
383 {
384 int lookup;
385 uint32_t route_metric = 0xFFFFFFFF;
386 uint8_t protocol_distance = 0xFF;
387
388 pim->nexthop_lookups++;
389
390 for (lookup = 0; lookup < max_lookup; ++lookup) {
391 int num_ifindex;
392 int first_ifindex;
393 struct prefix nexthop_addr;
394
395 num_ifindex = zclient_lookup_nexthop_once(pim, nexthop_tab,
396 tab_size, addr);
397 if (num_ifindex < 1) {
398 if (PIM_DEBUG_PIM_NHT) {
399 char addr_str[INET_ADDRSTRLEN];
400 pim_inet4_dump("<addr?>", addr, addr_str,
401 sizeof(addr_str));
402 zlog_debug(
403 "%s: lookup=%d/%d: could not find nexthop ifindex for address %s(%s)",
404 __PRETTY_FUNCTION__, lookup, max_lookup,
405 addr_str, pim->vrf->name);
406 }
407 return -1;
408 }
409
410 if (lookup < 1) {
411 /* this is the non-recursive lookup - save original
412 * metric/distance */
413 route_metric = nexthop_tab[0].route_metric;
414 protocol_distance = nexthop_tab[0].protocol_distance;
415 }
416
417 /*
418 * FIXME: Non-recursive nexthop ensured only for first ifindex.
419 * However, recursive route lookup should really be fixed in
420 * zebra daemon.
421 * See also TODO T24.
422 *
423 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
424 * it was being stored. This Doesn't solve all cases of
425 * recursive lookup but for the most common types it does.
426 */
427 first_ifindex = nexthop_tab[0].ifindex;
428 nexthop_addr = nexthop_tab[0].nexthop_addr;
429 if (first_ifindex > 0) {
430 /* found: first ifindex is non-recursive nexthop */
431
432 if (lookup > 0) {
433 /* Report non-recursive success after first
434 * lookup */
435 if (PIM_DEBUG_PIM_NHT) {
436 char addr_str[INET_ADDRSTRLEN];
437 pim_inet4_dump("<addr?>", addr,
438 addr_str,
439 sizeof(addr_str));
440 zlog_debug(
441 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %s(%s) dist=%d met=%d",
442 __PRETTY_FUNCTION__, lookup,
443 max_lookup, first_ifindex,
444 addr_str, pim->vrf->name,
445 nexthop_tab[0]
446 .protocol_distance,
447 nexthop_tab[0].route_metric);
448 }
449
450 /* use last address as nexthop address */
451 nexthop_tab[0].nexthop_addr.u.prefix4 = addr;
452
453 /* report original route metric/distance */
454 nexthop_tab[0].route_metric = route_metric;
455 nexthop_tab[0].protocol_distance =
456 protocol_distance;
457 }
458
459 return num_ifindex;
460 }
461
462 if (PIM_DEBUG_PIM_NHT) {
463 char addr_str[INET_ADDRSTRLEN];
464 char nexthop_str[PREFIX_STRLEN];
465 pim_inet4_dump("<addr?>", addr, addr_str,
466 sizeof(addr_str));
467 pim_addr_dump("<nexthop?>", &nexthop_addr, nexthop_str,
468 sizeof(nexthop_str));
469 zlog_debug(
470 "%s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s(%s) dist=%d met=%d",
471 __PRETTY_FUNCTION__, lookup, max_lookup,
472 nexthop_str, addr_str, pim->vrf->name,
473 nexthop_tab[0].protocol_distance,
474 nexthop_tab[0].route_metric);
475 }
476
477 addr = nexthop_addr.u.prefix4; /* use nexthop addr for
478 recursive lookup */
479
480 } /* for (max_lookup) */
481
482 if (PIM_DEBUG_PIM_NHT) {
483 char addr_str[INET_ADDRSTRLEN];
484 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
485 zlog_warn(
486 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s(%s)",
487 __PRETTY_FUNCTION__, lookup, max_lookup, addr_str,
488 pim->vrf->name);
489 }
490
491 return -2;
492 }
493
494 void pim_zlookup_show_ip_multicast(struct vty *vty)
495 {
496 vty_out(vty, "Zclient lookup socket: ");
497 if (zlookup) {
498 vty_out(vty, "%d failures=%d\n", zlookup->sock, zlookup->fail);
499 } else {
500 vty_out(vty, "<null zclient>\n");
501 }
502 }
503
504 int pim_zlookup_sg_statistics(struct channel_oil *c_oil)
505 {
506 struct stream *s = zlookup->obuf;
507 uint16_t command = 0;
508 unsigned long long lastused;
509 struct prefix_sg sg;
510 int count = 0;
511 int ret;
512 struct interface *ifp =
513 pim_if_find_by_vif_index(c_oil->pim, c_oil->oil.mfcc_parent);
514
515 if (PIM_DEBUG_ZEBRA) {
516 struct prefix_sg more;
517
518 more.src = c_oil->oil.mfcc_origin;
519 more.grp = c_oil->oil.mfcc_mcastgrp;
520 zlog_debug(
521 "Sending Request for New Channel Oil Information%s VIIF %d(%s)",
522 pim_str_sg_dump(&more), c_oil->oil.mfcc_parent,
523 c_oil->pim->vrf->name);
524 }
525
526 if (!ifp)
527 return -1;
528
529 stream_reset(s);
530 zclient_create_header(s, ZEBRA_IPMR_ROUTE_STATS, c_oil->pim->vrf_id);
531 stream_put_in_addr(s, &c_oil->oil.mfcc_origin);
532 stream_put_in_addr(s, &c_oil->oil.mfcc_mcastgrp);
533 stream_putl(s, ifp->ifindex);
534 stream_putw_at(s, 0, stream_get_endp(s));
535
536 count = stream_get_endp(s);
537 ret = writen(zlookup->sock, s->data, count);
538 if (ret <= 0) {
539 flog_err(
540 EC_LIB_SOCKET,
541 "%s: writen() failure: %d writing to zclient lookup socket",
542 __PRETTY_FUNCTION__, errno);
543 return -1;
544 }
545
546 s = zlookup->ibuf;
547
548 while (command != ZEBRA_IPMR_ROUTE_STATS) {
549 int err;
550 uint16_t length = 0;
551 vrf_id_t vrf_id;
552 uint8_t marker;
553 uint8_t version;
554
555 stream_reset(s);
556 err = zclient_read_header(s, zlookup->sock, &length, &marker,
557 &version, &vrf_id, &command);
558 if (err < 0) {
559 flog_err(EC_LIB_ZAPI_MISSMATCH,
560 "%s: zclient_read_header() failed",
561 __PRETTY_FUNCTION__);
562 zclient_lookup_failed(zlookup);
563 return -1;
564 }
565 }
566
567 sg.src.s_addr = stream_get_ipv4(s);
568 sg.grp.s_addr = stream_get_ipv4(s);
569 if (sg.src.s_addr != c_oil->oil.mfcc_origin.s_addr
570 || sg.grp.s_addr != c_oil->oil.mfcc_mcastgrp.s_addr) {
571 if (PIM_DEBUG_ZEBRA) {
572 struct prefix_sg more;
573
574 more.src = c_oil->oil.mfcc_origin;
575 more.grp = c_oil->oil.mfcc_mcastgrp;
576 flog_err(
577 EC_LIB_ZAPI_MISSMATCH,
578 "%s: Received wrong %s(%s) information requested",
579 __PRETTY_FUNCTION__, pim_str_sg_dump(&more),
580 c_oil->pim->vrf->name);
581 }
582 zclient_lookup_failed(zlookup);
583 return -3;
584 }
585
586 stream_get(&lastused, s, sizeof(lastused));
587 stream_getl(s);
588
589 c_oil->cc.lastused = lastused;
590
591 return 0;
592 }