3 * Copyright (C) 2008 Everton da Silva Marques
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.
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.
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
30 #include "lib_errors.h"
33 #include "pim_iface.h"
34 #include "pim_neighbor.h"
38 #include "pim_zlookup.h"
40 static struct zclient
*zlookup
= NULL
;
41 struct thread
*zlookup_read
;
43 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
);
44 static int zclient_lookup_read_pipe(struct thread
*thread
);
46 /* Connect to zebra for nexthop lookup. */
47 static int zclient_lookup_connect(struct thread
*t
)
49 struct zclient
*zlookup
;
51 zlookup
= THREAD_ARG(t
);
53 if (zlookup
->sock
>= 0) {
57 if (zclient_socket_connect(zlookup
) < 0) {
59 zlog_warn("%s: failure connecting zclient socket: failures=%d",
60 __func__
, zlookup
->fail
);
62 zlookup
->fail
= 0; /* reset counter on connection */
65 if (zclient_send_hello(zlookup
) == ZCLIENT_SEND_FAILURE
) {
66 if (close(zlookup
->sock
)) {
67 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
68 zlookup
->sock
, errno
, safe_strerror(errno
));
73 if (zlookup
->sock
< 0) {
74 /* Since last connect failed, retry within 10 secs */
75 zclient_lookup_sched(zlookup
, 10);
79 thread_add_timer(router
->master
, zclient_lookup_read_pipe
, zlookup
, 60,
84 /* Schedule connection with delay. */
85 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
87 thread_add_timer(router
->master
, zclient_lookup_connect
, zlookup
, delay
,
90 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
94 /* Schedule connection for now. */
95 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
97 thread_add_event(router
->master
, zclient_lookup_connect
, zlookup
, 0,
100 zlog_notice("%s: zclient lookup immediate connection scheduled",
104 /* Schedule reconnection, if needed. */
105 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
107 if (zlookup
->t_connect
) {
111 zclient_lookup_sched_now(zlookup
);
114 static void zclient_lookup_failed(struct zclient
*zlookup
)
116 if (zlookup
->sock
>= 0) {
117 if (close(zlookup
->sock
)) {
118 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
119 zlookup
->sock
, errno
, safe_strerror(errno
));
124 zclient_lookup_reconnect(zlookup
);
127 void zclient_lookup_free(void)
129 THREAD_OFF(zlookup_read
);
130 zclient_stop(zlookup
);
131 zclient_free(zlookup
);
135 void zclient_lookup_new(void)
137 struct zclient_options options
= zclient_options_default
;
138 options
.synchronous
= true;
140 zlookup
= zclient_new(router
->master
, &options
, NULL
, 0);
142 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: zclient_new() failure",
148 zlookup
->t_connect
= NULL
;
149 zlookup
->privs
= &pimd_privs
;
151 zclient_lookup_sched_now(zlookup
);
153 zlog_notice("%s: zclient lookup socket initialized", __func__
);
156 static int zclient_read_nexthop(struct pim_instance
*pim
,
157 struct zclient
*zlookup
,
158 struct pim_zlookup_nexthop nexthop_tab
[],
159 const int tab_size
, struct in_addr addr
)
167 uint16_t command
= 0;
168 struct in_addr raddr
;
174 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
175 char addr_str
[INET_ADDRSTRLEN
];
177 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
178 zlog_debug("%s: addr=%s(%s)", __func__
, addr_str
,
184 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
) {
186 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
187 &version
, &vrf_id
, &command
);
189 flog_err(EC_LIB_ZAPI_MISSMATCH
,
190 "%s: zclient_read_header() failed", __func__
);
191 zclient_lookup_failed(zlookup
);
195 if (command
== ZEBRA_ERROR
) {
196 enum zebra_error_types error
;
198 zapi_error_decode(s
, &error
);
199 /* Do nothing with it for now */
204 raddr
.s_addr
= stream_get_ipv4(s
);
206 if (raddr
.s_addr
!= addr
.s_addr
) {
207 char addr_str
[INET_ADDRSTRLEN
];
208 char raddr_str
[INET_ADDRSTRLEN
];
209 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
210 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
211 zlog_warn("%s: address mismatch: addr=%s(%s) raddr=%s",
212 __func__
, addr_str
, pim
->vrf
->name
, raddr_str
);
216 distance
= stream_getc(s
);
217 metric
= stream_getl(s
);
218 nexthop_num
= stream_getc(s
);
220 if (nexthop_num
< 1) {
221 if (PIM_DEBUG_PIM_NHT_DETAIL
)
222 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__
,
223 zlookup
->sock
, nexthop_num
);
227 for (i
= 0; i
< nexthop_num
; ++i
) {
228 vrf_id_t nexthop_vrf_id
;
229 enum nexthop_types_t nexthop_type
;
230 struct pim_neighbor
*nbr
;
233 nexthop_vrf_id
= stream_getl(s
);
234 nexthop_type
= stream_getc(s
);
235 if (num_ifindex
>= tab_size
) {
236 char addr_str
[INET_ADDRSTRLEN
];
237 pim_inet4_dump("<addr?>", addr
, addr_str
,
240 "%s: found too many nexthop ifindexes (%d > %d) for address %s(%s)",
241 __func__
, (num_ifindex
+ 1), tab_size
, addr_str
,
245 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
246 nexthop_tab
[num_ifindex
].route_metric
= metric
;
247 nexthop_tab
[num_ifindex
].vrf_id
= nexthop_vrf_id
;
248 switch (nexthop_type
) {
249 case NEXTHOP_TYPE_IFINDEX
:
250 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
252 * Connected route (i.e. no nexthop), use
253 * address passed in as PIM nexthop. This will
254 * allow us to work in cases where we are
255 * trying to find a route for this box.
257 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
258 nexthop_tab
[num_ifindex
].nexthop_addr
.prefixlen
=
260 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
=
264 case NEXTHOP_TYPE_IPV4_IFINDEX
:
265 case NEXTHOP_TYPE_IPV4
:
266 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
267 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
=
269 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
272 case NEXTHOP_TYPE_IPV6_IFINDEX
:
273 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
274 stream_get(&nexthop_tab
[num_ifindex
]
275 .nexthop_addr
.u
.prefix6
,
276 s
, sizeof(struct in6_addr
));
277 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
280 p
.prefixlen
= IPV6_MAX_BITLEN
;
282 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
283 sizeof(struct in6_addr
));
286 * If we are sending v6 secondary assume we receive v6
289 struct interface
*ifp
= if_lookup_by_index(
290 nexthop_tab
[num_ifindex
].ifindex
,
295 else if (pim
->send_v6_secondary
)
296 nbr
= pim_neighbor_find_by_secondary(ifp
, &p
);
298 nbr
= pim_neighbor_find_if(ifp
);
301 nexthop_tab
[num_ifindex
].nexthop_addr
.family
=
303 nexthop_tab
[num_ifindex
]
304 .nexthop_addr
.u
.prefix4
=
312 char addr_str
[INET_ADDRSTRLEN
];
313 pim_inet4_dump("<addr?>", addr
, addr_str
,
316 "%s: found non-ifindex nexthop type=%d for address %s(%s)",
317 __func__
, nexthop_type
, addr_str
,
327 static int zclient_lookup_nexthop_once(struct pim_instance
*pim
,
328 struct pim_zlookup_nexthop nexthop_tab
[],
329 const int tab_size
, struct in_addr addr
)
334 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
335 char addr_str
[INET_ADDRSTRLEN
];
336 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
337 zlog_debug("%s: addr=%s(%s)", __func__
, addr_str
,
342 if (zlookup
->sock
< 0) {
343 flog_err(EC_LIB_ZAPI_SOCKET
,
344 "%s: zclient lookup socket is not connected",
346 zclient_lookup_failed(zlookup
);
350 if (pim
->vrf
->vrf_id
== VRF_UNKNOWN
) {
352 "%s: VRF: %s does not fully exist yet, delaying lookup",
353 __func__
, pim
->vrf
->name
);
359 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
,
361 stream_put_in_addr(s
, &addr
);
362 stream_putw_at(s
, 0, stream_get_endp(s
));
364 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
368 "%s: writen() failure: %d writing to zclient lookup socket",
370 zclient_lookup_failed(zlookup
);
374 flog_err_sys(EC_LIB_SOCKET
,
375 "%s: connection closed on zclient lookup socket",
377 zclient_lookup_failed(zlookup
);
381 return zclient_read_nexthop(pim
, zlookup
, nexthop_tab
, tab_size
, addr
);
384 int zclient_lookup_read_pipe(struct thread
*thread
)
386 struct zclient
*zlookup
= THREAD_ARG(thread
);
387 struct pim_instance
*pim
= pim_get_pim_instance(VRF_DEFAULT
);
388 struct pim_zlookup_nexthop nexthop_tab
[10];
389 struct in_addr l
= {.s_addr
= INADDR_ANY
};
391 zclient_lookup_nexthop_once(pim
, nexthop_tab
, 10, l
);
392 thread_add_timer(router
->master
, zclient_lookup_read_pipe
, zlookup
, 60,
398 int zclient_lookup_nexthop(struct pim_instance
*pim
,
399 struct pim_zlookup_nexthop nexthop_tab
[],
400 const int tab_size
, struct in_addr addr
,
404 uint32_t route_metric
= 0xFFFFFFFF;
405 uint8_t protocol_distance
= 0xFF;
407 pim
->nexthop_lookups
++;
409 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
412 struct prefix nexthop_addr
;
414 num_ifindex
= zclient_lookup_nexthop_once(pim
, nexthop_tab
,
416 if (num_ifindex
< 1) {
417 if (PIM_DEBUG_PIM_NHT
) {
418 char addr_str
[INET_ADDRSTRLEN
];
419 pim_inet4_dump("<addr?>", addr
, addr_str
,
422 "%s: lookup=%d/%d: could not find nexthop ifindex for address %s(%s)",
423 __func__
, lookup
, max_lookup
, addr_str
,
430 /* this is the non-recursive lookup - save original
432 route_metric
= nexthop_tab
[0].route_metric
;
433 protocol_distance
= nexthop_tab
[0].protocol_distance
;
437 * FIXME: Non-recursive nexthop ensured only for first ifindex.
438 * However, recursive route lookup should really be fixed in
442 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
443 * it was being stored. This Doesn't solve all cases of
444 * recursive lookup but for the most common types it does.
446 first_ifindex
= nexthop_tab
[0].ifindex
;
447 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
448 if (first_ifindex
> 0) {
449 /* found: first ifindex is non-recursive nexthop */
452 /* Report non-recursive success after first
454 if (PIM_DEBUG_PIM_NHT
) {
455 char addr_str
[INET_ADDRSTRLEN
];
456 pim_inet4_dump("<addr?>", addr
,
460 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %s(%s) dist=%d met=%d",
461 __func__
, lookup
, max_lookup
,
462 first_ifindex
, addr_str
,
466 nexthop_tab
[0].route_metric
);
469 /* use last address as nexthop address */
470 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
472 /* report original route metric/distance */
473 nexthop_tab
[0].route_metric
= route_metric
;
474 nexthop_tab
[0].protocol_distance
=
481 if (PIM_DEBUG_PIM_NHT
) {
482 char addr_str
[INET_ADDRSTRLEN
];
483 char nexthop_str
[PREFIX_STRLEN
];
484 pim_inet4_dump("<addr?>", addr
, addr_str
,
486 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
,
487 sizeof(nexthop_str
));
489 "%s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s(%s) dist=%d met=%d",
490 __func__
, lookup
, max_lookup
, nexthop_str
,
491 addr_str
, pim
->vrf
->name
,
492 nexthop_tab
[0].protocol_distance
,
493 nexthop_tab
[0].route_metric
);
496 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for
499 } /* for (max_lookup) */
501 if (PIM_DEBUG_PIM_NHT
) {
502 char addr_str
[INET_ADDRSTRLEN
];
503 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
505 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s(%s)",
506 __func__
, lookup
, max_lookup
, addr_str
, pim
->vrf
->name
);
512 void pim_zlookup_show_ip_multicast(struct vty
*vty
)
514 vty_out(vty
, "Zclient lookup socket: ");
516 vty_out(vty
, "%d failures=%d\n", zlookup
->sock
, zlookup
->fail
);
518 vty_out(vty
, "<null zclient>\n");
522 int pim_zlookup_sg_statistics(struct channel_oil
*c_oil
)
524 struct stream
*s
= zlookup
->obuf
;
525 uint16_t command
= 0;
526 unsigned long long lastused
;
530 struct interface
*ifp
=
531 pim_if_find_by_vif_index(c_oil
->pim
, c_oil
->oil
.mfcc_parent
);
533 if (PIM_DEBUG_ZEBRA
) {
536 more
.src
= c_oil
->oil
.mfcc_origin
;
537 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
539 "Sending Request for New Channel Oil Information%s VIIF %d(%s)",
540 pim_str_sg_dump(&more
), c_oil
->oil
.mfcc_parent
,
541 c_oil
->pim
->vrf
->name
);
548 zclient_create_header(s
, ZEBRA_IPMR_ROUTE_STATS
,
549 c_oil
->pim
->vrf
->vrf_id
);
550 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_origin
);
551 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_mcastgrp
);
552 stream_putl(s
, ifp
->ifindex
);
553 stream_putw_at(s
, 0, stream_get_endp(s
));
555 count
= stream_get_endp(s
);
556 ret
= writen(zlookup
->sock
, s
->data
, count
);
560 "%s: writen() failure: %d writing to zclient lookup socket",
567 while (command
!= ZEBRA_IPMR_ROUTE_STATS
) {
575 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
576 &version
, &vrf_id
, &command
);
578 flog_err(EC_LIB_ZAPI_MISSMATCH
,
579 "%s: zclient_read_header() failed", __func__
);
580 zclient_lookup_failed(zlookup
);
585 sg
.src
.s_addr
= stream_get_ipv4(s
);
586 sg
.grp
.s_addr
= stream_get_ipv4(s
);
587 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
588 || sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
) {
589 if (PIM_DEBUG_ZEBRA
) {
592 more
.src
= c_oil
->oil
.mfcc_origin
;
593 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
595 EC_LIB_ZAPI_MISSMATCH
,
596 "%s: Received wrong %s(%s) information requested",
597 __func__
, pim_str_sg_dump(&more
),
598 c_oil
->pim
->vrf
->name
);
600 zclient_lookup_failed(zlookup
);
604 stream_get(&lastused
, s
, sizeof(lastused
));
605 /* signed success value from netlink_talk; currently unused */
606 (void)stream_getl(s
);
608 c_oil
->cc
.lastused
= lastused
;