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
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 #include "zebra/rib.h"
35 #include "pim_iface.h"
39 #include "pim_zlookup.h"
41 extern int zclient_debug
;
43 static struct zclient
*zlookup
= NULL
;
45 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
);
47 /* Connect to zebra for nexthop lookup. */
48 static int zclient_lookup_connect(struct thread
*t
)
50 struct zclient
*zlookup
;
52 zlookup
= THREAD_ARG(t
);
53 zlookup
->t_connect
= NULL
;
55 if (zlookup
->sock
>= 0) {
59 if (zclient_socket_connect(zlookup
) < 0) {
61 zlog_warn("%s: failure connecting zclient socket: failures=%d",
62 __PRETTY_FUNCTION__
, zlookup
->fail
);
65 zlookup
->fail
= 0; /* reset counter on connection */
68 zassert(!zlookup
->t_connect
);
69 if (zlookup
->sock
< 0) {
70 /* Since last connect failed, retry within 10 secs */
71 zclient_lookup_sched(zlookup
, 10);
78 /* Schedule connection with delay. */
79 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
81 zassert(!zlookup
->t_connect
);
83 THREAD_TIMER_ON(master
, zlookup
->t_connect
,
84 zclient_lookup_connect
,
87 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
88 __PRETTY_FUNCTION__
, delay
);
91 /* Schedule connection for now. */
92 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
94 zassert(!zlookup
->t_connect
);
96 zlookup
->t_connect
= thread_add_event(master
, zclient_lookup_connect
,
99 zlog_notice("%s: zclient lookup immediate connection scheduled",
100 __PRETTY_FUNCTION__
);
103 /* Schedule reconnection, if needed. */
104 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
106 if (zlookup
->t_connect
) {
110 zclient_lookup_sched_now(zlookup
);
113 static void zclient_lookup_failed(struct zclient
*zlookup
)
115 if (zlookup
->sock
>= 0) {
116 if (close(zlookup
->sock
)) {
117 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
, zlookup
->sock
,
118 errno
, safe_strerror(errno
));
123 zclient_lookup_reconnect(zlookup
);
127 zclient_lookup_new (void)
129 zlookup
= zclient_new (master
);
131 zlog_err("%s: zclient_new() failure",
132 __PRETTY_FUNCTION__
);
137 zlookup
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
138 zlookup
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
139 zlookup
->t_connect
= 0;
141 zclient_lookup_sched_now(zlookup
);
143 zlog_notice("%s: zclient lookup socket initialized",
144 __PRETTY_FUNCTION__
);
148 static int zclient_read_nexthop(struct zclient
*zlookup
,
149 struct pim_zlookup_nexthop nexthop_tab
[],
155 const uint16_t MIN_LEN
= 10; /* getipv4=4 getc=1 getl=4 getc=1 */
160 uint16_t command
= 0;
161 struct in_addr raddr
;
167 if (PIM_DEBUG_ZEBRA
) {
169 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
170 zlog_debug("%s: addr=%s",
177 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
)
180 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
183 zlog_err("%s %s: zclient_read_header() failed",
184 __FILE__
, __PRETTY_FUNCTION__
);
185 zclient_lookup_failed(zlookup
);
189 if (length
< MIN_LEN
) {
190 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
191 __FILE__
, __PRETTY_FUNCTION__
, length
, MIN_LEN
);
192 zclient_lookup_failed(zlookup
);
197 raddr
.s_addr
= stream_get_ipv4(s
);
199 if (raddr
.s_addr
!= addr
.s_addr
) {
202 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
203 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
204 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
206 addr_str
, raddr_str
);
210 distance
= stream_getc(s
);
211 metric
= stream_getl(s
);
212 nexthop_num
= stream_getc(s
);
214 if (nexthop_num
< 1) {
215 zlog_err("%s: socket %d bad nexthop_num=%d",
216 __func__
, zlookup
->sock
, nexthop_num
);
220 for (i
= 0; i
< nexthop_num
; ++i
) {
221 enum nexthop_types_t nexthop_type
;
222 struct pim_neighbor
*nbr
;
224 nexthop_type
= stream_getc(s
);
225 if (num_ifindex
>= tab_size
) {
227 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
228 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
229 __FILE__
, __PRETTY_FUNCTION__
,
230 (num_ifindex
+ 1), tab_size
, addr_str
);
233 switch (nexthop_type
) {
234 case NEXTHOP_TYPE_IFINDEX
:
235 case NEXTHOP_TYPE_IPV4_IFINDEX
:
236 case NEXTHOP_TYPE_IPV4
:
237 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
238 if (nexthop_type
== NEXTHOP_TYPE_IPV4_IFINDEX
||
239 nexthop_type
== NEXTHOP_TYPE_IPV4
) {
240 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
243 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= PIM_NET_INADDR_ANY
;
245 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
246 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
247 nexthop_tab
[num_ifindex
].route_metric
= metric
;
250 case NEXTHOP_TYPE_IPV6_IFINDEX
:
251 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
252 stream_get (&nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
, s
, 16);
253 nexthop_tab
[num_ifindex
].ifindex
= stream_getl (s
);
254 nbr
= pim_neighbor_find_if (if_lookup_by_index_vrf (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
));
257 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
258 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
= nbr
->source_addr
;
266 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
267 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
268 __FILE__
, __PRETTY_FUNCTION__
,
269 nexthop_type
, addr_str
);
279 zclient_lookup_nexthop_once (struct pim_zlookup_nexthop nexthop_tab
[],
286 if (PIM_DEBUG_ZEBRA
) {
288 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
289 zlog_debug("%s: addr=%s",
295 if (zlookup
->sock
< 0) {
296 zlog_err("%s %s: zclient lookup socket is not connected",
297 __FILE__
, __PRETTY_FUNCTION__
);
298 zclient_lookup_failed(zlookup
);
304 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, VRF_DEFAULT
);
305 stream_put_in_addr(s
, &addr
);
306 stream_putw_at(s
, 0, stream_get_endp(s
));
308 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
310 zlog_err("%s %s: writen() failure writing to zclient lookup socket",
311 __FILE__
, __PRETTY_FUNCTION__
);
312 zclient_lookup_failed(zlookup
);
316 zlog_err("%s %s: connection closed on zclient lookup socket",
317 __FILE__
, __PRETTY_FUNCTION__
);
318 zclient_lookup_failed(zlookup
);
322 return zclient_read_nexthop(zlookup
, nexthop_tab
,
327 zclient_lookup_nexthop (struct pim_zlookup_nexthop nexthop_tab
[],
333 uint32_t route_metric
= 0xFFFFFFFF;
334 uint8_t protocol_distance
= 0xFF;
336 qpim_nexthop_lookups
++;
338 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
341 struct prefix nexthop_addr
;
343 num_ifindex
= zclient_lookup_nexthop_once(nexthop_tab
,
345 if (num_ifindex
< 1) {
346 if (PIM_DEBUG_ZEBRA
) {
348 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
349 zlog_debug("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
350 __FILE__
, __PRETTY_FUNCTION__
,
351 lookup
, max_lookup
, addr_str
);
357 /* this is the non-recursive lookup - save original metric/distance */
358 route_metric
= nexthop_tab
[0].route_metric
;
359 protocol_distance
= nexthop_tab
[0].protocol_distance
;
363 * FIXME: Non-recursive nexthop ensured only for first ifindex.
364 * However, recursive route lookup should really be fixed in zebra daemon.
367 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
368 * it was being stored. This Doesn't solve all cases of
369 * recursive lookup but for the most common types it does.
371 first_ifindex
= nexthop_tab
[0].ifindex
;
372 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
373 if (first_ifindex
> 0) {
374 /* found: first ifindex is non-recursive nexthop */
377 /* Report non-recursive success after first lookup */
378 if (PIM_DEBUG_ZEBRA
) {
380 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
381 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
382 __FILE__
, __PRETTY_FUNCTION__
,
383 lookup
, max_lookup
, first_ifindex
, addr_str
,
384 nexthop_tab
[0].protocol_distance
,
385 nexthop_tab
[0].route_metric
);
388 /* use last address as nexthop address */
389 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
391 /* report original route metric/distance */
392 nexthop_tab
[0].route_metric
= route_metric
;
393 nexthop_tab
[0].protocol_distance
= protocol_distance
;
399 if (PIM_DEBUG_ZEBRA
) {
401 char nexthop_str
[100];
402 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
403 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
, sizeof(nexthop_str
));
404 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
405 __FILE__
, __PRETTY_FUNCTION__
,
406 lookup
, max_lookup
, nexthop_str
, addr_str
,
407 nexthop_tab
[0].protocol_distance
,
408 nexthop_tab
[0].route_metric
);
411 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for recursive lookup */
413 } /* for (max_lookup) */
415 if (PIM_DEBUG_ZEBRA
) {
417 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
418 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
419 __FILE__
, __PRETTY_FUNCTION__
,
420 lookup
, max_lookup
, addr_str
);
427 pim_zlookup_show_ip_multicast (struct vty
*vty
)
429 vty_out(vty
, "Zclient lookup socket: ");
431 vty_out(vty
, "%d failures=%d%s", zlookup
->sock
,
432 zlookup
->fail
, VTY_NEWLINE
);
435 vty_out(vty
, "<null zclient>%s", VTY_NEWLINE
);
440 pim_zlookup_sg_statistics (struct channel_oil
*c_oil
)
442 struct stream
*s
= zlookup
->obuf
;
443 uint16_t command
= 0;
444 unsigned long long lastused
;
448 struct interface
*ifp
= pim_if_find_by_vif_index (c_oil
->oil
.mfcc_parent
);
451 zlog_debug ("Sending Request for New Channel Oil Information");
454 zclient_create_header (s
, ZEBRA_IPMR_ROUTE_STATS
, VRF_DEFAULT
);
455 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_origin
);
456 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_mcastgrp
);
457 stream_putl (s
, ifp
->ifindex
);
458 stream_putw_at(s
, 0, stream_get_endp(s
));
460 count
= stream_get_endp (s
);
461 ret
= writen (zlookup
->sock
, s
->data
, count
);
464 zlog_err("%s %s: writen() failure writing to zclient lookup socket",
465 __FILE__
, __PRETTY_FUNCTION__
);
471 while (command
!= ZEBRA_IPMR_ROUTE_STATS
)
480 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
484 zlog_err ("%s %s: zclient_read_header() failed",
485 __FILE__
, __PRETTY_FUNCTION__
);
486 zclient_lookup_failed(zlookup
);
491 sg
.src
.s_addr
= stream_get_ipv4 (s
);
492 sg
.grp
.s_addr
= stream_get_ipv4 (s
);
493 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
||
494 sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
)
496 zlog_err ("%s: Received wrong %s information",
497 __PRETTY_FUNCTION__
, pim_str_sg_dump (&sg
));
498 zclient_lookup_failed (zlookup
);
502 stream_get (&lastused
, s
, sizeof (lastused
));
505 zlog_debug ("Received %lld for %s", lastused
, pim_str_sg_dump (&sg
));
507 c_oil
->cc
.lastused
= lastused
;