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
);
50 if (zlookup
->sock
>= 0) {
54 if (zclient_socket_connect(zlookup
) < 0) {
56 zlog_warn("%s: failure connecting zclient socket: failures=%d",
57 __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__
, zlookup
->sock
,
107 errno
, safe_strerror(errno
));
112 zclient_lookup_reconnect(zlookup
);
116 zclient_lookup_free (void)
118 zclient_stop (zlookup
);
119 zclient_free (zlookup
);
124 zclient_lookup_new (void)
126 zlookup
= zclient_new (master
);
128 zlog_err("%s: zclient_new() failure",
129 __PRETTY_FUNCTION__
);
134 zlookup
->t_connect
= NULL
;
136 zclient_lookup_sched_now(zlookup
);
138 zlog_notice("%s: zclient lookup socket initialized",
139 __PRETTY_FUNCTION__
);
143 static int zclient_read_nexthop(struct zclient
*zlookup
,
144 struct pim_zlookup_nexthop nexthop_tab
[],
150 const uint16_t MIN_LEN
= 10; /* getipv4=4 getc=1 getl=4 getc=1 */
155 uint16_t command
= 0;
156 struct in_addr raddr
;
162 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
163 char addr_str
[INET_ADDRSTRLEN
];
164 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
165 zlog_debug("%s: addr=%s",
172 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
)
175 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
178 zlog_err("%s %s: zclient_read_header() failed",
179 __FILE__
, __PRETTY_FUNCTION__
);
180 zclient_lookup_failed(zlookup
);
184 if (length
< MIN_LEN
) {
185 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
186 __FILE__
, __PRETTY_FUNCTION__
, length
, MIN_LEN
);
187 zclient_lookup_failed(zlookup
);
192 raddr
.s_addr
= stream_get_ipv4(s
);
194 if (raddr
.s_addr
!= addr
.s_addr
) {
195 char addr_str
[INET_ADDRSTRLEN
];
196 char raddr_str
[INET_ADDRSTRLEN
];
197 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
198 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
199 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
201 addr_str
, raddr_str
);
205 distance
= stream_getc(s
);
206 metric
= stream_getl(s
);
207 nexthop_num
= stream_getc(s
);
209 if (nexthop_num
< 1) {
210 zlog_err("%s: socket %d bad nexthop_num=%d",
211 __func__
, zlookup
->sock
, nexthop_num
);
215 for (i
= 0; i
< nexthop_num
; ++i
) {
216 enum nexthop_types_t nexthop_type
;
217 struct pim_neighbor
*nbr
;
220 nexthop_type
= stream_getc(s
);
221 if (num_ifindex
>= tab_size
) {
222 char addr_str
[INET_ADDRSTRLEN
];
223 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
224 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
225 __FILE__
, __PRETTY_FUNCTION__
,
226 (num_ifindex
+ 1), tab_size
, addr_str
);
229 switch (nexthop_type
) {
230 case NEXTHOP_TYPE_IFINDEX
:
231 case NEXTHOP_TYPE_IPV4_IFINDEX
:
232 case NEXTHOP_TYPE_IPV4
:
233 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
234 if (nexthop_type
== NEXTHOP_TYPE_IPV4_IFINDEX
||
235 nexthop_type
== NEXTHOP_TYPE_IPV4
) {
236 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
239 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= PIM_NET_INADDR_ANY
;
241 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
242 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
243 nexthop_tab
[num_ifindex
].route_metric
= metric
;
246 case NEXTHOP_TYPE_IPV6_IFINDEX
:
247 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
248 stream_get (&nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
250 sizeof(struct in6_addr
));
251 nexthop_tab
[num_ifindex
].ifindex
= stream_getl (s
);
254 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
255 memcpy (&p
.u
.prefix6
,
256 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
257 sizeof(struct in6_addr
));
260 * If we are sending v6 secondary assume we receive v6 secondary
262 if (pimg
->send_v6_secondary
)
263 nbr
= pim_neighbor_find_by_secondary(if_lookup_by_index (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
), &p
);
265 nbr
= pim_neighbor_find_if (if_lookup_by_index (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
));
268 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
269 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
= nbr
->source_addr
;
276 char addr_str
[INET_ADDRSTRLEN
];
277 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
278 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
279 __FILE__
, __PRETTY_FUNCTION__
,
280 nexthop_type
, addr_str
);
290 zclient_lookup_nexthop_once (struct pim_zlookup_nexthop nexthop_tab
[],
297 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
298 char addr_str
[INET_ADDRSTRLEN
];
299 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
300 zlog_debug("%s: addr=%s",
306 if (zlookup
->sock
< 0) {
307 zlog_err("%s %s: zclient lookup socket is not connected",
308 __FILE__
, __PRETTY_FUNCTION__
);
309 zclient_lookup_failed(zlookup
);
315 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, VRF_DEFAULT
);
316 stream_put_in_addr(s
, &addr
);
317 stream_putw_at(s
, 0, stream_get_endp(s
));
319 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
321 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
322 __FILE__
, __PRETTY_FUNCTION__
, errno
);
323 zclient_lookup_failed(zlookup
);
327 zlog_err("%s %s: connection closed on zclient lookup socket",
328 __FILE__
, __PRETTY_FUNCTION__
);
329 zclient_lookup_failed(zlookup
);
333 return zclient_read_nexthop(zlookup
, nexthop_tab
,
338 zclient_lookup_nexthop (struct pim_zlookup_nexthop nexthop_tab
[],
344 uint32_t route_metric
= 0xFFFFFFFF;
345 uint8_t protocol_distance
= 0xFF;
347 qpim_nexthop_lookups
++;
349 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
352 struct prefix nexthop_addr
;
354 num_ifindex
= zclient_lookup_nexthop_once(nexthop_tab
,
356 if (num_ifindex
< 1) {
357 if (PIM_DEBUG_ZEBRA
) {
358 char addr_str
[INET_ADDRSTRLEN
];
359 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
360 zlog_debug("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
361 __FILE__
, __PRETTY_FUNCTION__
,
362 lookup
, max_lookup
, addr_str
);
368 /* this is the non-recursive lookup - save original metric/distance */
369 route_metric
= nexthop_tab
[0].route_metric
;
370 protocol_distance
= nexthop_tab
[0].protocol_distance
;
374 * FIXME: Non-recursive nexthop ensured only for first ifindex.
375 * However, recursive route lookup should really be fixed in zebra daemon.
378 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
379 * it was being stored. This Doesn't solve all cases of
380 * recursive lookup but for the most common types it does.
382 first_ifindex
= nexthop_tab
[0].ifindex
;
383 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
384 if (first_ifindex
> 0) {
385 /* found: first ifindex is non-recursive nexthop */
388 /* Report non-recursive success after first lookup */
389 if (PIM_DEBUG_ZEBRA
) {
390 char addr_str
[INET_ADDRSTRLEN
];
391 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
392 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
393 __FILE__
, __PRETTY_FUNCTION__
,
394 lookup
, max_lookup
, first_ifindex
, addr_str
,
395 nexthop_tab
[0].protocol_distance
,
396 nexthop_tab
[0].route_metric
);
399 /* use last address as nexthop address */
400 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
402 /* report original route metric/distance */
403 nexthop_tab
[0].route_metric
= route_metric
;
404 nexthop_tab
[0].protocol_distance
= protocol_distance
;
410 if (PIM_DEBUG_ZEBRA
) {
411 char addr_str
[INET_ADDRSTRLEN
];
412 char nexthop_str
[PREFIX_STRLEN
];
413 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
414 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
, sizeof(nexthop_str
));
415 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
416 __FILE__
, __PRETTY_FUNCTION__
,
417 lookup
, max_lookup
, nexthop_str
, addr_str
,
418 nexthop_tab
[0].protocol_distance
,
419 nexthop_tab
[0].route_metric
);
422 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for recursive lookup */
424 } /* for (max_lookup) */
426 if (PIM_DEBUG_ZEBRA
) {
427 char addr_str
[INET_ADDRSTRLEN
];
428 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
429 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
430 __FILE__
, __PRETTY_FUNCTION__
,
431 lookup
, max_lookup
, addr_str
);
438 pim_zlookup_show_ip_multicast (struct vty
*vty
)
440 vty_out(vty
, "Zclient lookup socket: ");
442 vty_outln (vty
, "%d failures=%d", zlookup
->sock
,
446 vty_outln (vty
, "<null zclient>");
451 pim_zlookup_sg_statistics (struct channel_oil
*c_oil
)
453 struct stream
*s
= zlookup
->obuf
;
454 uint16_t command
= 0;
455 unsigned long long lastused
;
459 struct interface
*ifp
= pim_if_find_by_vif_index (c_oil
->oil
.mfcc_parent
);
463 struct prefix_sg more
;
465 more
.src
= c_oil
->oil
.mfcc_origin
;
466 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
467 zlog_debug ("Sending Request for New Channel Oil Information(%s) VIIF %d",
468 pim_str_sg_dump (&more
), c_oil
->oil
.mfcc_parent
);
475 zclient_create_header (s
, ZEBRA_IPMR_ROUTE_STATS
, VRF_DEFAULT
);
476 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_origin
);
477 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_mcastgrp
);
478 stream_putl (s
, ifp
->ifindex
);
479 stream_putw_at(s
, 0, stream_get_endp(s
));
481 count
= stream_get_endp (s
);
482 ret
= writen (zlookup
->sock
, s
->data
, count
);
485 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
486 __FILE__
, __PRETTY_FUNCTION__
, errno
);
492 while (command
!= ZEBRA_IPMR_ROUTE_STATS
)
501 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
505 zlog_err ("%s %s: zclient_read_header() failed",
506 __FILE__
, __PRETTY_FUNCTION__
);
507 zclient_lookup_failed(zlookup
);
512 sg
.src
.s_addr
= stream_get_ipv4 (s
);
513 sg
.grp
.s_addr
= stream_get_ipv4 (s
);
514 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
||
515 sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
)
517 zlog_err ("%s: Received wrong %s information",
518 __PRETTY_FUNCTION__
, pim_str_sg_dump (&sg
));
519 zclient_lookup_failed (zlookup
);
523 stream_get (&lastused
, s
, sizeof (lastused
));
524 ret
= stream_getl (s
);
527 zlog_debug ("Received %lld for %s success: %d", lastused
, pim_str_sg_dump (&sg
), ret
);
529 c_oil
->cc
.lastused
= lastused
;