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_free (zlookup
);
123 zclient_lookup_new (void)
125 zlookup
= zclient_new (master
);
127 zlog_err("%s: zclient_new() failure",
128 __PRETTY_FUNCTION__
);
133 zlookup
->t_connect
= NULL
;
135 zclient_lookup_sched_now(zlookup
);
137 zlog_notice("%s: zclient lookup socket initialized",
138 __PRETTY_FUNCTION__
);
142 static int zclient_read_nexthop(struct zclient
*zlookup
,
143 struct pim_zlookup_nexthop nexthop_tab
[],
149 const uint16_t MIN_LEN
= 10; /* getipv4=4 getc=1 getl=4 getc=1 */
154 uint16_t command
= 0;
155 struct in_addr raddr
;
161 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
162 char addr_str
[INET_ADDRSTRLEN
];
163 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
164 zlog_debug("%s: addr=%s",
171 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
)
174 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
177 zlog_err("%s %s: zclient_read_header() failed",
178 __FILE__
, __PRETTY_FUNCTION__
);
179 zclient_lookup_failed(zlookup
);
183 if (length
< MIN_LEN
) {
184 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
185 __FILE__
, __PRETTY_FUNCTION__
, length
, MIN_LEN
);
186 zclient_lookup_failed(zlookup
);
191 raddr
.s_addr
= stream_get_ipv4(s
);
193 if (raddr
.s_addr
!= addr
.s_addr
) {
194 char addr_str
[INET_ADDRSTRLEN
];
195 char raddr_str
[INET_ADDRSTRLEN
];
196 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
197 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
198 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
200 addr_str
, raddr_str
);
204 distance
= stream_getc(s
);
205 metric
= stream_getl(s
);
206 nexthop_num
= stream_getc(s
);
208 if (nexthop_num
< 1) {
209 zlog_err("%s: socket %d bad nexthop_num=%d",
210 __func__
, zlookup
->sock
, nexthop_num
);
214 for (i
= 0; i
< nexthop_num
; ++i
) {
215 enum nexthop_types_t nexthop_type
;
216 struct pim_neighbor
*nbr
;
219 nexthop_type
= stream_getc(s
);
220 if (num_ifindex
>= tab_size
) {
221 char addr_str
[INET_ADDRSTRLEN
];
222 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
223 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
224 __FILE__
, __PRETTY_FUNCTION__
,
225 (num_ifindex
+ 1), tab_size
, addr_str
);
228 switch (nexthop_type
) {
229 case NEXTHOP_TYPE_IFINDEX
:
230 case NEXTHOP_TYPE_IPV4_IFINDEX
:
231 case NEXTHOP_TYPE_IPV4
:
232 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
233 if (nexthop_type
== NEXTHOP_TYPE_IPV4_IFINDEX
||
234 nexthop_type
== NEXTHOP_TYPE_IPV4
) {
235 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= stream_get_ipv4(s
);
238 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
= PIM_NET_INADDR_ANY
;
240 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
241 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
242 nexthop_tab
[num_ifindex
].route_metric
= metric
;
245 case NEXTHOP_TYPE_IPV6_IFINDEX
:
246 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
247 stream_get (&nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
249 sizeof(struct in6_addr
));
250 nexthop_tab
[num_ifindex
].ifindex
= stream_getl (s
);
253 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
254 memcpy (&p
.u
.prefix6
,
255 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
256 sizeof(struct in6_addr
));
259 * If we are sending v6 secondary assume we receive v6 secondary
261 if (pimg
->send_v6_secondary
)
262 nbr
= pim_neighbor_find_by_secondary(if_lookup_by_index (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
), &p
);
264 nbr
= pim_neighbor_find_if (if_lookup_by_index (nexthop_tab
[num_ifindex
].ifindex
, VRF_DEFAULT
));
267 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
268 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
= nbr
->source_addr
;
275 char addr_str
[INET_ADDRSTRLEN
];
276 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
277 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
278 __FILE__
, __PRETTY_FUNCTION__
,
279 nexthop_type
, addr_str
);
289 zclient_lookup_nexthop_once (struct pim_zlookup_nexthop nexthop_tab
[],
296 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
297 char addr_str
[INET_ADDRSTRLEN
];
298 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
299 zlog_debug("%s: addr=%s",
305 if (zlookup
->sock
< 0) {
306 zlog_err("%s %s: zclient lookup socket is not connected",
307 __FILE__
, __PRETTY_FUNCTION__
);
308 zclient_lookup_failed(zlookup
);
314 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, VRF_DEFAULT
);
315 stream_put_in_addr(s
, &addr
);
316 stream_putw_at(s
, 0, stream_get_endp(s
));
318 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
320 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
321 __FILE__
, __PRETTY_FUNCTION__
, errno
);
322 zclient_lookup_failed(zlookup
);
326 zlog_err("%s %s: connection closed on zclient lookup socket",
327 __FILE__
, __PRETTY_FUNCTION__
);
328 zclient_lookup_failed(zlookup
);
332 return zclient_read_nexthop(zlookup
, nexthop_tab
,
337 zclient_lookup_nexthop (struct pim_zlookup_nexthop nexthop_tab
[],
343 uint32_t route_metric
= 0xFFFFFFFF;
344 uint8_t protocol_distance
= 0xFF;
346 qpim_nexthop_lookups
++;
348 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
351 struct prefix nexthop_addr
;
353 num_ifindex
= zclient_lookup_nexthop_once(nexthop_tab
,
355 if (num_ifindex
< 1) {
356 if (PIM_DEBUG_ZEBRA
) {
357 char addr_str
[INET_ADDRSTRLEN
];
358 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
359 zlog_debug("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
360 __FILE__
, __PRETTY_FUNCTION__
,
361 lookup
, max_lookup
, addr_str
);
367 /* this is the non-recursive lookup - save original metric/distance */
368 route_metric
= nexthop_tab
[0].route_metric
;
369 protocol_distance
= nexthop_tab
[0].protocol_distance
;
373 * FIXME: Non-recursive nexthop ensured only for first ifindex.
374 * However, recursive route lookup should really be fixed in zebra daemon.
377 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
378 * it was being stored. This Doesn't solve all cases of
379 * recursive lookup but for the most common types it does.
381 first_ifindex
= nexthop_tab
[0].ifindex
;
382 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
383 if (first_ifindex
> 0) {
384 /* found: first ifindex is non-recursive nexthop */
387 /* Report non-recursive success after first lookup */
388 if (PIM_DEBUG_ZEBRA
) {
389 char addr_str
[INET_ADDRSTRLEN
];
390 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
391 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
392 __FILE__
, __PRETTY_FUNCTION__
,
393 lookup
, max_lookup
, first_ifindex
, addr_str
,
394 nexthop_tab
[0].protocol_distance
,
395 nexthop_tab
[0].route_metric
);
398 /* use last address as nexthop address */
399 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
401 /* report original route metric/distance */
402 nexthop_tab
[0].route_metric
= route_metric
;
403 nexthop_tab
[0].protocol_distance
= protocol_distance
;
409 if (PIM_DEBUG_ZEBRA
) {
410 char addr_str
[INET_ADDRSTRLEN
];
411 char nexthop_str
[PREFIX_STRLEN
];
412 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
413 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
, sizeof(nexthop_str
));
414 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
415 __FILE__
, __PRETTY_FUNCTION__
,
416 lookup
, max_lookup
, nexthop_str
, addr_str
,
417 nexthop_tab
[0].protocol_distance
,
418 nexthop_tab
[0].route_metric
);
421 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for recursive lookup */
423 } /* for (max_lookup) */
425 if (PIM_DEBUG_ZEBRA
) {
426 char addr_str
[INET_ADDRSTRLEN
];
427 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
428 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
429 __FILE__
, __PRETTY_FUNCTION__
,
430 lookup
, max_lookup
, addr_str
);
437 pim_zlookup_show_ip_multicast (struct vty
*vty
)
439 vty_out(vty
, "Zclient lookup socket: ");
441 vty_out(vty
, "%d failures=%d%s", zlookup
->sock
,
442 zlookup
->fail
, VTY_NEWLINE
);
445 vty_out(vty
, "<null zclient>%s", VTY_NEWLINE
);
450 pim_zlookup_sg_statistics (struct channel_oil
*c_oil
)
452 struct stream
*s
= zlookup
->obuf
;
453 uint16_t command
= 0;
454 unsigned long long lastused
;
458 struct interface
*ifp
= pim_if_find_by_vif_index (c_oil
->oil
.mfcc_parent
);
462 struct prefix_sg more
;
464 more
.src
= c_oil
->oil
.mfcc_origin
;
465 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
466 zlog_debug ("Sending Request for New Channel Oil Information(%s) VIIF %d",
467 pim_str_sg_dump (&more
), c_oil
->oil
.mfcc_parent
);
474 zclient_create_header (s
, ZEBRA_IPMR_ROUTE_STATS
, VRF_DEFAULT
);
475 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_origin
);
476 stream_put_in_addr (s
, &c_oil
->oil
.mfcc_mcastgrp
);
477 stream_putl (s
, ifp
->ifindex
);
478 stream_putw_at(s
, 0, stream_get_endp(s
));
480 count
= stream_get_endp (s
);
481 ret
= writen (zlookup
->sock
, s
->data
, count
);
484 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
485 __FILE__
, __PRETTY_FUNCTION__
, errno
);
491 while (command
!= ZEBRA_IPMR_ROUTE_STATS
)
500 err
= zclient_read_header (s
, zlookup
->sock
, &length
, &marker
, &version
,
504 zlog_err ("%s %s: zclient_read_header() failed",
505 __FILE__
, __PRETTY_FUNCTION__
);
506 zclient_lookup_failed(zlookup
);
511 sg
.src
.s_addr
= stream_get_ipv4 (s
);
512 sg
.grp
.s_addr
= stream_get_ipv4 (s
);
513 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
||
514 sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
)
516 zlog_err ("%s: Received wrong %s information",
517 __PRETTY_FUNCTION__
, pim_str_sg_dump (&sg
));
518 zclient_lookup_failed (zlookup
);
522 stream_get (&lastused
, s
, sizeof (lastused
));
523 ret
= stream_getl (s
);
526 zlog_debug ("Received %lld for %s success: %d", lastused
, pim_str_sg_dump (&sg
), ret
);
528 c_oil
->cc
.lastused
= lastused
;