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"
33 #include "pim_iface.h"
37 #include "pim_zlookup.h"
39 static struct zclient
*zlookup
= NULL
;
41 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
);
43 /* Connect to zebra for nexthop lookup. */
44 static int zclient_lookup_connect(struct thread
*t
)
46 struct zclient
*zlookup
;
48 zlookup
= THREAD_ARG(t
);
49 zlookup
->t_connect
= NULL
;
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
);
61 zlookup
->fail
= 0; /* reset counter on connection */
64 zassert(!zlookup
->t_connect
);
65 if (zlookup
->sock
< 0) {
66 /* Since last connect failed, retry within 10 secs */
67 zclient_lookup_sched(zlookup
, 10);
74 /* Schedule connection with delay. */
75 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
77 zassert(!zlookup
->t_connect
);
79 thread_add_timer(master
, zclient_lookup_connect
, zlookup
, delay
,
82 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
83 __PRETTY_FUNCTION__
, delay
);
86 /* Schedule connection for now. */
87 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
89 zassert(!zlookup
->t_connect
);
90 thread_add_event(master
, zclient_lookup_connect
, zlookup
, 0,
93 zlog_notice("%s: zclient lookup immediate connection scheduled",
97 /* Schedule reconnection, if needed. */
98 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
100 if (zlookup
->t_connect
) {
104 zclient_lookup_sched_now(zlookup
);
107 static void zclient_lookup_failed(struct zclient
*zlookup
)
109 if (zlookup
->sock
>= 0) {
110 if (close(zlookup
->sock
)) {
111 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
, zlookup
->sock
,
112 errno
, safe_strerror(errno
));
117 zclient_lookup_reconnect(zlookup
);
121 zclient_lookup_free (void)
123 zclient_free (zlookup
);
128 zclient_lookup_new (void)
130 zlookup
= zclient_new (master
);
132 zlog_err("%s: zclient_new() failure",
133 __PRETTY_FUNCTION__
);
138 zlookup
->t_connect
= NULL
;
140 zclient_lookup_sched_now(zlookup
);
142 zlog_notice("%s: zclient lookup socket initialized",
143 __PRETTY_FUNCTION__
);
147 static int zclient_read_nexthop(struct zclient
*zlookup
,
148 struct pim_zlookup_nexthop nexthop_tab
[],
154 const uint16_t MIN_LEN
= 10; /* getipv4=4 getc=1 getl=4 getc=1 */
159 uint16_t command
= 0;
160 struct in_addr raddr
;
166 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
167 char addr_str
[INET_ADDRSTRLEN
];
168 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
169 zlog_debug("%s: addr=%s",
176 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
)
179 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
182 zlog_err("%s %s: zclient_read_header() failed",
183 __FILE__
, __PRETTY_FUNCTION__
);
184 zclient_lookup_failed(zlookup
);
188 if (length
< MIN_LEN
) {
189 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
190 __FILE__
, __PRETTY_FUNCTION__
, length
, MIN_LEN
);
191 zclient_lookup_failed(zlookup
);
196 raddr
.s_addr
= stream_get_ipv4(s
);
198 if (raddr
.s_addr
!= addr
.s_addr
) {
199 char addr_str
[INET_ADDRSTRLEN
];
200 char raddr_str
[INET_ADDRSTRLEN
];
201 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
202 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
203 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
205 addr_str
, raddr_str
);
209 distance
= stream_getc(s
);
210 metric
= stream_getl(s
);
211 nexthop_num
= stream_getc(s
);
213 if (nexthop_num
< 1) {
214 zlog_err("%s: socket %d bad nexthop_num=%d",
215 __func__
, zlookup
->sock
, nexthop_num
);
219 for (i
= 0; i
< nexthop_num
; ++i
) {
220 enum nexthop_types_t nexthop_type
;
221 struct pim_neighbor
*nbr
;
224 nexthop_type
= stream_getc(s
);
225 if (num_ifindex
>= tab_size
) {
226 char addr_str
[INET_ADDRSTRLEN
];
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
,
254 sizeof(struct in6_addr
));
255 nexthop_tab
[num_ifindex
].ifindex
= stream_getl (s
);
258 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
259 memcpy (&p
.u
.prefix6
,
260 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
261 sizeof(struct in6_addr
));
264 * If we are sending v6 secondary assume we receive v6 secondary
266 if (pimg
->send_v6_secondary
)
267 nbr
= pim_neighbor_find_by_secondary(if_lookup_by_index (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
), &p
);
269 nbr
= pim_neighbor_find_if (if_lookup_by_index (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
));
272 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
273 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
= nbr
->source_addr
;
280 char addr_str
[INET_ADDRSTRLEN
];
281 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
282 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
283 __FILE__
, __PRETTY_FUNCTION__
,
284 nexthop_type
, addr_str
);
294 zclient_lookup_nexthop_once (struct pim_zlookup_nexthop nexthop_tab
[],
301 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
302 char addr_str
[INET_ADDRSTRLEN
];
303 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
304 zlog_debug("%s: addr=%s",
310 if (zlookup
->sock
< 0) {
311 zlog_err("%s %s: zclient lookup socket is not connected",
312 __FILE__
, __PRETTY_FUNCTION__
);
313 zclient_lookup_failed(zlookup
);
319 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, VRF_DEFAULT
);
320 stream_put_in_addr(s
, &addr
);
321 stream_putw_at(s
, 0, stream_get_endp(s
));
323 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
325 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
326 __FILE__
, __PRETTY_FUNCTION__
, errno
);
327 zclient_lookup_failed(zlookup
);
331 zlog_err("%s %s: connection closed on zclient lookup socket",
332 __FILE__
, __PRETTY_FUNCTION__
);
333 zclient_lookup_failed(zlookup
);
337 return zclient_read_nexthop(zlookup
, nexthop_tab
,
342 zclient_lookup_nexthop (struct pim_zlookup_nexthop nexthop_tab
[],
348 uint32_t route_metric
= 0xFFFFFFFF;
349 uint8_t protocol_distance
= 0xFF;
351 qpim_nexthop_lookups
++;
353 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
356 struct prefix nexthop_addr
;
358 num_ifindex
= zclient_lookup_nexthop_once(nexthop_tab
,
360 if (num_ifindex
< 1) {
361 if (PIM_DEBUG_ZEBRA
) {
362 char addr_str
[INET_ADDRSTRLEN
];
363 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
364 zlog_debug("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
365 __FILE__
, __PRETTY_FUNCTION__
,
366 lookup
, max_lookup
, addr_str
);
372 /* this is the non-recursive lookup - save original metric/distance */
373 route_metric
= nexthop_tab
[0].route_metric
;
374 protocol_distance
= nexthop_tab
[0].protocol_distance
;
378 * FIXME: Non-recursive nexthop ensured only for first ifindex.
379 * However, recursive route lookup should really be fixed in zebra daemon.
382 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
383 * it was being stored. This Doesn't solve all cases of
384 * recursive lookup but for the most common types it does.
386 first_ifindex
= nexthop_tab
[0].ifindex
;
387 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
388 if (first_ifindex
> 0) {
389 /* found: first ifindex is non-recursive nexthop */
392 /* Report non-recursive success after first lookup */
393 if (PIM_DEBUG_ZEBRA
) {
394 char addr_str
[INET_ADDRSTRLEN
];
395 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
396 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
397 __FILE__
, __PRETTY_FUNCTION__
,
398 lookup
, max_lookup
, first_ifindex
, addr_str
,
399 nexthop_tab
[0].protocol_distance
,
400 nexthop_tab
[0].route_metric
);
403 /* use last address as nexthop address */
404 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
406 /* report original route metric/distance */
407 nexthop_tab
[0].route_metric
= route_metric
;
408 nexthop_tab
[0].protocol_distance
= protocol_distance
;
414 if (PIM_DEBUG_ZEBRA
) {
415 char addr_str
[INET_ADDRSTRLEN
];
416 char nexthop_str
[PREFIX_STRLEN
];
417 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
418 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
, sizeof(nexthop_str
));
419 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
420 __FILE__
, __PRETTY_FUNCTION__
,
421 lookup
, max_lookup
, nexthop_str
, addr_str
,
422 nexthop_tab
[0].protocol_distance
,
423 nexthop_tab
[0].route_metric
);
426 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for recursive lookup */
428 } /* for (max_lookup) */
430 if (PIM_DEBUG_ZEBRA
) {
431 char addr_str
[INET_ADDRSTRLEN
];
432 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
433 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
434 __FILE__
, __PRETTY_FUNCTION__
,
435 lookup
, max_lookup
, addr_str
);
442 pim_zlookup_show_ip_multicast (struct vty
*vty
)
444 vty_out(vty
, "Zclient lookup socket: ");
446 vty_out(vty
, "%d failures=%d%s", zlookup
->sock
,
447 zlookup
->fail
, VTY_NEWLINE
);
450 vty_out(vty
, "<null zclient>%s", VTY_NEWLINE
);
455 pim_zlookup_sg_statistics (struct channel_oil
*c_oil
)
457 struct stream
*s
= zlookup
->obuf
;
458 uint16_t command
= 0;
459 unsigned long long lastused
;
463 struct interface
*ifp
= pim_if_find_by_vif_index (c_oil
->oil
.mfcc_parent
);
467 struct prefix_sg more
;
469 more
.src
= c_oil
->oil
.mfcc_origin
;
470 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
471 zlog_debug ("Sending Request for New Channel Oil Information(%s) VIIF %d",
472 pim_str_sg_dump (&more
), c_oil
->oil
.mfcc_parent
);
479 zclient_create_header (s
, ZEBRA_IPMR_ROUTE_STATS
, VRF_DEFAULT
);
480 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_origin
);
481 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_mcastgrp
);
482 stream_putl (s
, ifp
->ifindex
);
483 stream_putw_at(s
, 0, stream_get_endp(s
));
485 count
= stream_get_endp (s
);
486 ret
= writen (zlookup
->sock
, s
->data
, count
);
489 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
490 __FILE__
, __PRETTY_FUNCTION__
, errno
);
496 while (command
!= ZEBRA_IPMR_ROUTE_STATS
)
505 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
509 zlog_err ("%s %s: zclient_read_header() failed",
510 __FILE__
, __PRETTY_FUNCTION__
);
511 zclient_lookup_failed(zlookup
);
516 sg
.src
.s_addr
= stream_get_ipv4 (s
);
517 sg
.grp
.s_addr
= stream_get_ipv4 (s
);
518 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
||
519 sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
)
521 zlog_err ("%s: Received wrong %s information",
522 __PRETTY_FUNCTION__
, pim_str_sg_dump (&sg
));
523 zclient_lookup_failed (zlookup
);
527 stream_get (&lastused
, s
, sizeof (lastused
));
528 ret
= stream_getl (s
);
531 zlog_debug ("Received %lld for %s success: %d", lastused
, pim_str_sg_dump (&sg
), ret
);
533 c_oil
->cc
.lastused
= lastused
;