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,
22 #include "zebra/rib.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
);
50 zlookup
->t_connect
= NULL
;
52 if (zlookup
->sock
>= 0) {
56 if (zclient_socket_connect(zlookup
) < 0) {
58 zlog_warn("%s: failure connecting zclient socket: failures=%d",
59 __PRETTY_FUNCTION__
, zlookup
->fail
);
62 zlookup
->fail
= 0; /* reset counter on connection */
65 zassert(!zlookup
->t_connect
);
66 if (zlookup
->sock
< 0) {
67 /* Since last connect failed, retry within 10 secs */
68 zclient_lookup_sched(zlookup
, 10);
75 /* Schedule connection with delay. */
76 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
78 zassert(!zlookup
->t_connect
);
80 THREAD_TIMER_ON(master
, zlookup
->t_connect
,
81 zclient_lookup_connect
,
84 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
85 __PRETTY_FUNCTION__
, delay
);
88 /* Schedule connection for now. */
89 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
91 zassert(!zlookup
->t_connect
);
93 zlookup
->t_connect
= thread_add_event(master
, zclient_lookup_connect
,
96 zlog_notice("%s: zclient lookup immediate connection scheduled",
100 /* Schedule reconnection, if needed. */
101 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
103 if (zlookup
->t_connect
) {
107 zclient_lookup_sched_now(zlookup
);
110 static void zclient_lookup_failed(struct zclient
*zlookup
)
112 if (zlookup
->sock
>= 0) {
113 if (close(zlookup
->sock
)) {
114 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
, zlookup
->sock
,
115 errno
, safe_strerror(errno
));
120 zclient_lookup_reconnect(zlookup
);
124 zclient_lookup_free (void)
126 zclient_free (zlookup
);
131 zclient_lookup_new (void)
133 zlookup
= zclient_new (master
);
135 zlog_err("%s: zclient_new() failure",
136 __PRETTY_FUNCTION__
);
141 zlookup
->t_connect
= NULL
;
143 zclient_lookup_sched_now(zlookup
);
145 zlog_notice("%s: zclient lookup socket initialized",
146 __PRETTY_FUNCTION__
);
150 static int zclient_read_nexthop(struct zclient
*zlookup
,
151 struct pim_zlookup_nexthop nexthop_tab
[],
157 const uint16_t MIN_LEN
= 10; /* getipv4=4 getc=1 getl=4 getc=1 */
162 uint16_t command
= 0;
163 struct in_addr raddr
;
169 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
170 char addr_str
[INET_ADDRSTRLEN
];
171 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
172 zlog_debug("%s: addr=%s",
179 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
)
182 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
185 zlog_err("%s %s: zclient_read_header() failed",
186 __FILE__
, __PRETTY_FUNCTION__
);
187 zclient_lookup_failed(zlookup
);
191 if (length
< MIN_LEN
) {
192 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
193 __FILE__
, __PRETTY_FUNCTION__
, length
, MIN_LEN
);
194 zclient_lookup_failed(zlookup
);
199 raddr
.s_addr
= stream_get_ipv4(s
);
201 if (raddr
.s_addr
!= addr
.s_addr
) {
202 char addr_str
[INET_ADDRSTRLEN
];
203 char raddr_str
[INET_ADDRSTRLEN
];
204 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
205 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
206 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
208 addr_str
, raddr_str
);
212 distance
= stream_getc(s
);
213 metric
= stream_getl(s
);
214 nexthop_num
= stream_getc(s
);
216 if (nexthop_num
< 1) {
217 zlog_err("%s: socket %d bad nexthop_num=%d",
218 __func__
, zlookup
->sock
, nexthop_num
);
222 for (i
= 0; i
< nexthop_num
; ++i
) {
223 enum nexthop_types_t nexthop_type
;
224 struct pim_neighbor
*nbr
;
226 nexthop_type
= stream_getc(s
);
227 if (num_ifindex
>= tab_size
) {
228 char addr_str
[INET_ADDRSTRLEN
];
229 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
230 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
231 __FILE__
, __PRETTY_FUNCTION__
,
232 (num_ifindex
+ 1), tab_size
, addr_str
);
235 switch (nexthop_type
) {
236 case NEXTHOP_TYPE_IFINDEX
:
237 case NEXTHOP_TYPE_IPV4_IFINDEX
:
238 case NEXTHOP_TYPE_IPV4
:
239 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
240 if (nexthop_type
== NEXTHOP_TYPE_IPV4_IFINDEX
||
241 nexthop_type
== NEXTHOP_TYPE_IPV4
) {
242 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
245 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= PIM_NET_INADDR_ANY
;
247 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
248 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
249 nexthop_tab
[num_ifindex
].route_metric
= metric
;
252 case NEXTHOP_TYPE_IPV6_IFINDEX
:
253 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
254 stream_get (&nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
, s
, 16);
255 nexthop_tab
[num_ifindex
].ifindex
= stream_getl (s
);
256 nbr
= pim_neighbor_find_if (if_lookup_by_index_vrf (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
));
259 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
260 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
= nbr
->source_addr
;
267 char addr_str
[INET_ADDRSTRLEN
];
268 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
269 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
270 __FILE__
, __PRETTY_FUNCTION__
,
271 nexthop_type
, addr_str
);
281 zclient_lookup_nexthop_once (struct pim_zlookup_nexthop nexthop_tab
[],
288 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
289 char addr_str
[INET_ADDRSTRLEN
];
290 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
291 zlog_debug("%s: addr=%s",
297 if (zlookup
->sock
< 0) {
298 zlog_err("%s %s: zclient lookup socket is not connected",
299 __FILE__
, __PRETTY_FUNCTION__
);
300 zclient_lookup_failed(zlookup
);
306 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, VRF_DEFAULT
);
307 stream_put_in_addr(s
, &addr
);
308 stream_putw_at(s
, 0, stream_get_endp(s
));
310 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
312 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
313 __FILE__
, __PRETTY_FUNCTION__
, errno
);
314 zclient_lookup_failed(zlookup
);
318 zlog_err("%s %s: connection closed on zclient lookup socket",
319 __FILE__
, __PRETTY_FUNCTION__
);
320 zclient_lookup_failed(zlookup
);
324 return zclient_read_nexthop(zlookup
, nexthop_tab
,
329 zclient_lookup_nexthop (struct pim_zlookup_nexthop nexthop_tab
[],
335 uint32_t route_metric
= 0xFFFFFFFF;
336 uint8_t protocol_distance
= 0xFF;
338 qpim_nexthop_lookups
++;
340 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
343 struct prefix nexthop_addr
;
345 num_ifindex
= zclient_lookup_nexthop_once(nexthop_tab
,
347 if (num_ifindex
< 1) {
348 if (PIM_DEBUG_ZEBRA
) {
349 char addr_str
[INET_ADDRSTRLEN
];
350 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
351 zlog_debug("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
352 __FILE__
, __PRETTY_FUNCTION__
,
353 lookup
, max_lookup
, addr_str
);
359 /* this is the non-recursive lookup - save original metric/distance */
360 route_metric
= nexthop_tab
[0].route_metric
;
361 protocol_distance
= nexthop_tab
[0].protocol_distance
;
365 * FIXME: Non-recursive nexthop ensured only for first ifindex.
366 * However, recursive route lookup should really be fixed in zebra daemon.
369 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
370 * it was being stored. This Doesn't solve all cases of
371 * recursive lookup but for the most common types it does.
373 first_ifindex
= nexthop_tab
[0].ifindex
;
374 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
375 if (first_ifindex
> 0) {
376 /* found: first ifindex is non-recursive nexthop */
379 /* Report non-recursive success after first lookup */
380 if (PIM_DEBUG_ZEBRA
) {
381 char addr_str
[INET_ADDRSTRLEN
];
382 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
383 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
384 __FILE__
, __PRETTY_FUNCTION__
,
385 lookup
, max_lookup
, first_ifindex
, addr_str
,
386 nexthop_tab
[0].protocol_distance
,
387 nexthop_tab
[0].route_metric
);
390 /* use last address as nexthop address */
391 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
393 /* report original route metric/distance */
394 nexthop_tab
[0].route_metric
= route_metric
;
395 nexthop_tab
[0].protocol_distance
= protocol_distance
;
401 if (PIM_DEBUG_ZEBRA
) {
402 char addr_str
[INET_ADDRSTRLEN
];
403 char nexthop_str
[PREFIX_STRLEN
];
404 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
405 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
, sizeof(nexthop_str
));
406 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
407 __FILE__
, __PRETTY_FUNCTION__
,
408 lookup
, max_lookup
, nexthop_str
, addr_str
,
409 nexthop_tab
[0].protocol_distance
,
410 nexthop_tab
[0].route_metric
);
413 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for recursive lookup */
415 } /* for (max_lookup) */
417 if (PIM_DEBUG_ZEBRA
) {
418 char addr_str
[INET_ADDRSTRLEN
];
419 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
420 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
421 __FILE__
, __PRETTY_FUNCTION__
,
422 lookup
, max_lookup
, addr_str
);
429 pim_zlookup_show_ip_multicast (struct vty
*vty
)
431 vty_out(vty
, "Zclient lookup socket: ");
433 vty_out(vty
, "%d failures=%d%s", zlookup
->sock
,
434 zlookup
->fail
, VTY_NEWLINE
);
437 vty_out(vty
, "<null zclient>%s", VTY_NEWLINE
);
442 pim_zlookup_sg_statistics (struct channel_oil
*c_oil
)
444 struct stream
*s
= zlookup
->obuf
;
445 uint16_t command
= 0;
446 unsigned long long lastused
;
450 struct interface
*ifp
= pim_if_find_by_vif_index (c_oil
->oil
.mfcc_parent
);
454 struct prefix_sg more
;
456 more
.src
= c_oil
->oil
.mfcc_origin
;
457 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
458 zlog_debug ("Sending Request for New Channel Oil Information(%s) VIIF %d",
459 pim_str_sg_dump (&more
), c_oil
->oil
.mfcc_parent
);
466 zclient_create_header (s
, ZEBRA_IPMR_ROUTE_STATS
, VRF_DEFAULT
);
467 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_origin
);
468 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_mcastgrp
);
469 stream_putl (s
, ifp
->ifindex
);
470 stream_putw_at(s
, 0, stream_get_endp(s
));
472 count
= stream_get_endp (s
);
473 ret
= writen (zlookup
->sock
, s
->data
, count
);
476 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
477 __FILE__
, __PRETTY_FUNCTION__
, errno
);
483 while (command
!= ZEBRA_IPMR_ROUTE_STATS
)
492 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
496 zlog_err ("%s %s: zclient_read_header() failed",
497 __FILE__
, __PRETTY_FUNCTION__
);
498 zclient_lookup_failed(zlookup
);
503 sg
.src
.s_addr
= stream_get_ipv4 (s
);
504 sg
.grp
.s_addr
= stream_get_ipv4 (s
);
505 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
||
506 sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
)
508 zlog_err ("%s: Received wrong %s information",
509 __PRETTY_FUNCTION__
, pim_str_sg_dump (&sg
));
510 zclient_lookup_failed (zlookup
);
514 stream_get (&lastused
, s
, sizeof (lastused
));
515 ret
= stream_getl (s
);
518 zlog_debug ("Received %lld for %s success: %d", lastused
, pim_str_sg_dump (&sg
), ret
);
520 c_oil
->cc
.lastused
= lastused
;