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
21 #include "zebra/rib.h"
31 #include "lib_errors.h"
34 #include "pim_iface.h"
38 #include "pim_zlookup.h"
40 static struct zclient
*zlookup
= NULL
;
42 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
);
44 /* Connect to zebra for nexthop lookup. */
45 static int zclient_lookup_connect(struct thread
*t
)
47 struct zclient
*zlookup
;
49 zlookup
= THREAD_ARG(t
);
51 if (zlookup
->sock
>= 0) {
55 if (zclient_socket_connect(zlookup
) < 0) {
57 zlog_warn("%s: failure connecting zclient socket: failures=%d",
58 __PRETTY_FUNCTION__
, zlookup
->fail
);
60 zlookup
->fail
= 0; /* reset counter on connection */
63 if (zlookup
->sock
< 0) {
64 /* Since last connect failed, retry within 10 secs */
65 zclient_lookup_sched(zlookup
, 10);
72 /* Schedule connection with delay. */
73 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
75 thread_add_timer(master
, zclient_lookup_connect
, zlookup
, delay
,
78 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
79 __PRETTY_FUNCTION__
, delay
);
82 /* Schedule connection for now. */
83 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
85 thread_add_event(master
, zclient_lookup_connect
, zlookup
, 0,
88 zlog_notice("%s: zclient lookup immediate connection scheduled",
92 /* Schedule reconnection, if needed. */
93 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
95 if (zlookup
->t_connect
) {
99 zclient_lookup_sched_now(zlookup
);
102 static void zclient_lookup_failed(struct zclient
*zlookup
)
104 if (zlookup
->sock
>= 0) {
105 if (close(zlookup
->sock
)) {
106 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
107 zlookup
->sock
, errno
, safe_strerror(errno
));
112 zclient_lookup_reconnect(zlookup
);
115 void zclient_lookup_free(void)
117 zclient_stop(zlookup
);
118 zclient_free(zlookup
);
122 void zclient_lookup_new(void)
124 zlookup
= zclient_new_notify(master
, &zclient_options_default
);
126 flog_err(LIB_ERR_ZAPI_SOCKET
, "%s: zclient_new() failure",
127 __PRETTY_FUNCTION__
);
132 zlookup
->t_connect
= NULL
;
133 zlookup
->privs
= &pimd_privs
;
135 zclient_lookup_sched_now(zlookup
);
137 zlog_notice("%s: zclient lookup socket initialized",
138 __PRETTY_FUNCTION__
);
141 static int zclient_read_nexthop(struct pim_instance
*pim
,
142 struct zclient
*zlookup
,
143 struct pim_zlookup_nexthop nexthop_tab
[],
144 const int tab_size
, struct in_addr addr
)
152 uint16_t command
= 0;
153 struct in_addr raddr
;
159 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
160 char addr_str
[INET_ADDRSTRLEN
];
161 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
162 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__
, addr_str
,
168 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
) {
170 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
171 &version
, &vrf_id
, &command
);
173 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
174 "%s: zclient_read_header() failed",
175 __PRETTY_FUNCTION__
);
176 zclient_lookup_failed(zlookup
);
181 raddr
.s_addr
= stream_get_ipv4(s
);
183 if (raddr
.s_addr
!= addr
.s_addr
) {
184 char addr_str
[INET_ADDRSTRLEN
];
185 char raddr_str
[INET_ADDRSTRLEN
];
186 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
187 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
188 zlog_warn("%s: address mismatch: addr=%s(%s) raddr=%s",
189 __PRETTY_FUNCTION__
, addr_str
, pim
->vrf
->name
,
194 distance
= stream_getc(s
);
195 metric
= stream_getl(s
);
196 nexthop_num
= stream_getc(s
);
198 if (nexthop_num
< 1) {
199 if (PIM_DEBUG_PIM_NHT_DETAIL
)
200 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__
,
201 zlookup
->sock
, nexthop_num
);
205 for (i
= 0; i
< nexthop_num
; ++i
) {
206 enum nexthop_types_t nexthop_type
;
207 struct pim_neighbor
*nbr
;
210 nexthop_type
= stream_getc(s
);
211 if (num_ifindex
>= tab_size
) {
212 char addr_str
[INET_ADDRSTRLEN
];
213 pim_inet4_dump("<addr?>", addr
, addr_str
,
216 "%s: found too many nexthop ifindexes (%d > %d) for address %s(%s)",
217 __PRETTY_FUNCTION__
, (num_ifindex
+ 1),
218 tab_size
, addr_str
, pim
->vrf
->name
);
221 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
222 nexthop_tab
[num_ifindex
].route_metric
= metric
;
223 switch (nexthop_type
) {
224 case NEXTHOP_TYPE_IFINDEX
:
225 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
227 * Connected route (i.e. no nexthop), use
228 * address passed in as PIM nexthop. This will
229 * allow us to work in cases where we are
230 * trying to find a route for this box.
232 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
233 nexthop_tab
[num_ifindex
].nexthop_addr
.prefixlen
=
235 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
=
239 case NEXTHOP_TYPE_IPV4_IFINDEX
:
240 case NEXTHOP_TYPE_IPV4
:
241 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
242 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
=
244 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
247 case NEXTHOP_TYPE_IPV6_IFINDEX
:
248 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
249 stream_get(&nexthop_tab
[num_ifindex
]
250 .nexthop_addr
.u
.prefix6
,
251 s
, sizeof(struct in6_addr
));
252 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
255 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
257 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
258 sizeof(struct in6_addr
));
261 * If we are sending v6 secondary assume we receive v6
264 if (pim
->send_v6_secondary
)
265 nbr
= pim_neighbor_find_by_secondary(
267 nexthop_tab
[num_ifindex
]
272 nbr
= pim_neighbor_find_if(if_lookup_by_index(
273 nexthop_tab
[num_ifindex
].ifindex
,
276 nexthop_tab
[num_ifindex
].nexthop_addr
.family
=
278 nexthop_tab
[num_ifindex
]
279 .nexthop_addr
.u
.prefix4
=
287 char addr_str
[INET_ADDRSTRLEN
];
288 pim_inet4_dump("<addr?>", addr
, addr_str
,
291 "%s: found non-ifindex nexthop type=%d for address %s(%s)",
292 __PRETTY_FUNCTION__
, nexthop_type
,
293 addr_str
, pim
->vrf
->name
);
302 static int zclient_lookup_nexthop_once(struct pim_instance
*pim
,
303 struct pim_zlookup_nexthop nexthop_tab
[],
304 const int tab_size
, struct in_addr addr
)
309 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
310 char addr_str
[INET_ADDRSTRLEN
];
311 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
312 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__
, addr_str
,
317 if (zlookup
->sock
< 0) {
318 flog_err(LIB_ERR_ZAPI_SOCKET
,
319 "%s: zclient lookup socket is not connected",
320 __PRETTY_FUNCTION__
);
321 zclient_lookup_failed(zlookup
);
325 if (pim
->vrf
->vrf_id
== VRF_UNKNOWN
) {
327 "%s: VRF: %s does not fully exist yet, delaying lookup",
328 __PRETTY_FUNCTION__
, pim
->vrf
->name
);
334 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, pim
->vrf_id
);
335 stream_put_in_addr(s
, &addr
);
336 stream_putw_at(s
, 0, stream_get_endp(s
));
338 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
342 "%s: writen() failure: %d writing to zclient lookup socket",
343 __PRETTY_FUNCTION__
, errno
);
344 zclient_lookup_failed(zlookup
);
348 flog_err_sys(LIB_ERR_SOCKET
,
349 "%s: connection closed on zclient lookup socket",
350 __PRETTY_FUNCTION__
);
351 zclient_lookup_failed(zlookup
);
355 return zclient_read_nexthop(pim
, zlookup
, nexthop_tab
, tab_size
, addr
);
358 int zclient_lookup_nexthop(struct pim_instance
*pim
,
359 struct pim_zlookup_nexthop nexthop_tab
[],
360 const int tab_size
, struct in_addr addr
,
364 uint32_t route_metric
= 0xFFFFFFFF;
365 uint8_t protocol_distance
= 0xFF;
367 pim
->nexthop_lookups
++;
369 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
372 struct prefix nexthop_addr
;
374 num_ifindex
= zclient_lookup_nexthop_once(pim
, nexthop_tab
,
376 if (num_ifindex
< 1) {
377 if (PIM_DEBUG_PIM_NHT
) {
378 char addr_str
[INET_ADDRSTRLEN
];
379 pim_inet4_dump("<addr?>", addr
, addr_str
,
382 "%s: lookup=%d/%d: could not find nexthop ifindex for address %s(%s)",
383 __PRETTY_FUNCTION__
, lookup
, max_lookup
,
384 addr_str
, pim
->vrf
->name
);
390 /* this is the non-recursive lookup - save original
392 route_metric
= nexthop_tab
[0].route_metric
;
393 protocol_distance
= nexthop_tab
[0].protocol_distance
;
397 * FIXME: Non-recursive nexthop ensured only for first ifindex.
398 * However, recursive route lookup should really be fixed in
402 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
403 * it was being stored. This Doesn't solve all cases of
404 * recursive lookup but for the most common types it does.
406 first_ifindex
= nexthop_tab
[0].ifindex
;
407 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
408 if (first_ifindex
> 0) {
409 /* found: first ifindex is non-recursive nexthop */
412 /* Report non-recursive success after first
414 if (PIM_DEBUG_PIM_NHT
) {
415 char addr_str
[INET_ADDRSTRLEN
];
416 pim_inet4_dump("<addr?>", addr
,
420 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %s(%s) dist=%d met=%d",
421 __PRETTY_FUNCTION__
, lookup
,
422 max_lookup
, first_ifindex
,
423 addr_str
, pim
->vrf
->name
,
426 nexthop_tab
[0].route_metric
);
429 /* use last address as nexthop address */
430 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
432 /* report original route metric/distance */
433 nexthop_tab
[0].route_metric
= route_metric
;
434 nexthop_tab
[0].protocol_distance
=
441 if (PIM_DEBUG_PIM_NHT
) {
442 char addr_str
[INET_ADDRSTRLEN
];
443 char nexthop_str
[PREFIX_STRLEN
];
444 pim_inet4_dump("<addr?>", addr
, addr_str
,
446 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
,
447 sizeof(nexthop_str
));
449 "%s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s(%s) dist=%d met=%d",
450 __PRETTY_FUNCTION__
, lookup
, max_lookup
,
451 nexthop_str
, addr_str
, pim
->vrf
->name
,
452 nexthop_tab
[0].protocol_distance
,
453 nexthop_tab
[0].route_metric
);
456 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for
459 } /* for (max_lookup) */
461 if (PIM_DEBUG_PIM_NHT
) {
462 char addr_str
[INET_ADDRSTRLEN
];
463 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
465 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s(%s)",
466 __PRETTY_FUNCTION__
, lookup
, max_lookup
, addr_str
,
473 void pim_zlookup_show_ip_multicast(struct vty
*vty
)
475 vty_out(vty
, "Zclient lookup socket: ");
477 vty_out(vty
, "%d failures=%d\n", zlookup
->sock
, zlookup
->fail
);
479 vty_out(vty
, "<null zclient>\n");
483 int pim_zlookup_sg_statistics(struct channel_oil
*c_oil
)
485 struct stream
*s
= zlookup
->obuf
;
486 uint16_t command
= 0;
487 unsigned long long lastused
;
491 struct interface
*ifp
=
492 pim_if_find_by_vif_index(c_oil
->pim
, c_oil
->oil
.mfcc_parent
);
494 if (PIM_DEBUG_ZEBRA
) {
495 struct prefix_sg more
;
497 more
.src
= c_oil
->oil
.mfcc_origin
;
498 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
500 "Sending Request for New Channel Oil Information(%s) VIIF %d(%s)",
501 pim_str_sg_dump(&more
), c_oil
->oil
.mfcc_parent
,
502 c_oil
->pim
->vrf
->name
);
509 zclient_create_header(s
, ZEBRA_IPMR_ROUTE_STATS
, c_oil
->pim
->vrf_id
);
510 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_origin
);
511 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_mcastgrp
);
512 stream_putl(s
, ifp
->ifindex
);
513 stream_putw_at(s
, 0, stream_get_endp(s
));
515 count
= stream_get_endp(s
);
516 ret
= writen(zlookup
->sock
, s
->data
, count
);
520 "%s: writen() failure: %d writing to zclient lookup socket",
521 __PRETTY_FUNCTION__
, errno
);
527 while (command
!= ZEBRA_IPMR_ROUTE_STATS
) {
535 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
536 &version
, &vrf_id
, &command
);
538 flog_err(LIB_ERR_ZAPI_MISSMATCH
,
539 "%s: zclient_read_header() failed",
540 __PRETTY_FUNCTION__
);
541 zclient_lookup_failed(zlookup
);
546 sg
.src
.s_addr
= stream_get_ipv4(s
);
547 sg
.grp
.s_addr
= stream_get_ipv4(s
);
548 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
549 || sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
) {
550 if (PIM_DEBUG_ZEBRA
) {
551 struct prefix_sg more
;
553 more
.src
= c_oil
->oil
.mfcc_origin
;
554 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
556 LIB_ERR_ZAPI_MISSMATCH
,
557 "%s: Received wrong %s(%s) information requested",
558 __PRETTY_FUNCTION__
, pim_str_sg_dump(&more
),
559 c_oil
->pim
->vrf
->name
);
561 zclient_lookup_failed(zlookup
);
565 stream_get(&lastused
, s
, sizeof(lastused
));
568 c_oil
->cc
.lastused
= lastused
;