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