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
);
59 zlookup
->fail
= 0; /* reset counter on connection */
62 if (zlookup
->sock
< 0) {
63 /* Since last connect failed, retry within 10 secs */
64 zclient_lookup_sched(zlookup
, 10);
71 /* Schedule connection with delay. */
72 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
74 thread_add_timer(master
, zclient_lookup_connect
, zlookup
, delay
,
77 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
78 __PRETTY_FUNCTION__
, delay
);
81 /* Schedule connection for now. */
82 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
84 thread_add_event(master
, zclient_lookup_connect
, zlookup
, 0,
87 zlog_notice("%s: zclient lookup immediate connection scheduled",
91 /* Schedule reconnection, if needed. */
92 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
94 if (zlookup
->t_connect
) {
98 zclient_lookup_sched_now(zlookup
);
101 static void zclient_lookup_failed(struct zclient
*zlookup
)
103 if (zlookup
->sock
>= 0) {
104 if (close(zlookup
->sock
)) {
105 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
106 zlookup
->sock
, errno
, safe_strerror(errno
));
111 zclient_lookup_reconnect(zlookup
);
114 void zclient_lookup_free(void)
116 zclient_stop(zlookup
);
117 zclient_free(zlookup
);
121 void zclient_lookup_new(void)
123 zlookup
= zclient_new_notify(master
, &zclient_options_default
);
125 zlog_err("%s: zclient_new() failure", __PRETTY_FUNCTION__
);
130 zlookup
->t_connect
= NULL
;
131 zlookup
->privs
= &pimd_privs
;
133 zclient_lookup_sched_now(zlookup
);
135 zlog_notice("%s: zclient lookup socket initialized",
136 __PRETTY_FUNCTION__
);
139 static int zclient_read_nexthop(struct pim_instance
*pim
,
140 struct zclient
*zlookup
,
141 struct pim_zlookup_nexthop nexthop_tab
[],
142 const int tab_size
, struct in_addr addr
)
150 uint16_t command
= 0;
151 struct in_addr raddr
;
157 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
158 char addr_str
[INET_ADDRSTRLEN
];
159 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
160 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__
, addr_str
,
166 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
) {
168 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
169 &version
, &vrf_id
, &command
);
171 zlog_err("%s: zclient_read_header() failed",
172 __PRETTY_FUNCTION__
);
173 zclient_lookup_failed(zlookup
);
178 raddr
.s_addr
= stream_get_ipv4(s
);
180 if (raddr
.s_addr
!= addr
.s_addr
) {
181 char addr_str
[INET_ADDRSTRLEN
];
182 char raddr_str
[INET_ADDRSTRLEN
];
183 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
184 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
185 zlog_warn("%s: address mismatch: addr=%s(%s) raddr=%s",
186 __PRETTY_FUNCTION__
, addr_str
, pim
->vrf
->name
,
191 distance
= stream_getc(s
);
192 metric
= stream_getl(s
);
193 nexthop_num
= stream_getc(s
);
195 if (nexthop_num
< 1) {
196 if (PIM_DEBUG_PIM_NHT_DETAIL
)
197 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__
,
198 zlookup
->sock
, nexthop_num
);
202 for (i
= 0; i
< nexthop_num
; ++i
) {
203 enum nexthop_types_t nexthop_type
;
204 struct pim_neighbor
*nbr
;
207 nexthop_type
= stream_getc(s
);
208 if (num_ifindex
>= tab_size
) {
209 char addr_str
[INET_ADDRSTRLEN
];
210 pim_inet4_dump("<addr?>", addr
, addr_str
,
213 "%s: found too many nexthop ifindexes (%d > %d) for address %s(%s)",
214 __PRETTY_FUNCTION__
, (num_ifindex
+ 1),
215 tab_size
, addr_str
, pim
->vrf
->name
);
218 switch (nexthop_type
) {
219 case NEXTHOP_TYPE_IFINDEX
:
220 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
222 * Connected route (i.e. no nexthop), use
223 * address passed in as PIM nexthop. This will
224 * allow us to work in cases where we are
225 * trying to find a route for this box.
227 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
228 nexthop_tab
[num_ifindex
].nexthop_addr
.prefixlen
=
230 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
=
234 case NEXTHOP_TYPE_IPV4_IFINDEX
:
235 case NEXTHOP_TYPE_IPV4
:
236 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
237 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
=
239 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
242 case NEXTHOP_TYPE_IPV6_IFINDEX
:
243 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
244 stream_get(&nexthop_tab
[num_ifindex
]
245 .nexthop_addr
.u
.prefix6
,
246 s
, sizeof(struct in6_addr
));
247 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
250 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
252 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
253 sizeof(struct in6_addr
));
256 * If we are sending v6 secondary assume we receive v6
259 if (pim
->send_v6_secondary
)
260 nbr
= pim_neighbor_find_by_secondary(
262 nexthop_tab
[num_ifindex
]
267 nbr
= pim_neighbor_find_if(if_lookup_by_index(
268 nexthop_tab
[num_ifindex
].ifindex
,
271 nexthop_tab
[num_ifindex
].nexthop_addr
.family
=
273 nexthop_tab
[num_ifindex
]
274 .nexthop_addr
.u
.prefix4
=
279 case NEXTHOP_TYPE_IPV6
:
280 case NEXTHOP_TYPE_BLACKHOLE
:
284 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
285 nexthop_tab
[num_ifindex
].route_metric
= metric
;
291 static int zclient_lookup_nexthop_once(struct pim_instance
*pim
,
292 struct pim_zlookup_nexthop nexthop_tab
[],
293 const int tab_size
, struct in_addr addr
)
298 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
299 char addr_str
[INET_ADDRSTRLEN
];
300 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
301 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__
, addr_str
,
306 if (zlookup
->sock
< 0) {
307 zlog_err("%s: zclient lookup socket is not connected",
308 __PRETTY_FUNCTION__
);
309 zclient_lookup_failed(zlookup
);
313 if (pim
->vrf
->vrf_id
== VRF_UNKNOWN
) {
315 "%s: VRF: %s does not fully exist yet, delaying lookup",
316 __PRETTY_FUNCTION__
, pim
->vrf
->name
);
322 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
, pim
->vrf_id
);
323 stream_put_in_addr(s
, &addr
);
324 stream_putw_at(s
, 0, stream_get_endp(s
));
326 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
329 "%s: writen() failure: %d writing to zclient lookup socket",
330 __PRETTY_FUNCTION__
, errno
);
331 zclient_lookup_failed(zlookup
);
335 zlog_err("%s: connection closed on zclient lookup socket",
336 __PRETTY_FUNCTION__
);
337 zclient_lookup_failed(zlookup
);
341 return zclient_read_nexthop(pim
, zlookup
, nexthop_tab
, tab_size
, addr
);
344 int zclient_lookup_nexthop(struct pim_instance
*pim
,
345 struct pim_zlookup_nexthop nexthop_tab
[],
346 const int tab_size
, struct in_addr addr
,
350 uint32_t route_metric
= 0xFFFFFFFF;
351 uint8_t protocol_distance
= 0xFF;
353 pim
->nexthop_lookups
++;
355 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
358 struct prefix nexthop_addr
;
360 num_ifindex
= zclient_lookup_nexthop_once(pim
, nexthop_tab
,
362 if (num_ifindex
< 1) {
363 if (PIM_DEBUG_PIM_NHT
) {
364 char addr_str
[INET_ADDRSTRLEN
];
365 pim_inet4_dump("<addr?>", addr
, addr_str
,
368 "%s: lookup=%d/%d: could not find nexthop ifindex for address %s(%s)",
369 __PRETTY_FUNCTION__
, lookup
, max_lookup
,
370 addr_str
, pim
->vrf
->name
);
376 /* this is the non-recursive lookup - save original
378 route_metric
= nexthop_tab
[0].route_metric
;
379 protocol_distance
= nexthop_tab
[0].protocol_distance
;
383 * FIXME: Non-recursive nexthop ensured only for first ifindex.
384 * However, recursive route lookup should really be fixed in
388 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
389 * it was being stored. This Doesn't solve all cases of
390 * recursive lookup but for the most common types it does.
392 first_ifindex
= nexthop_tab
[0].ifindex
;
393 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
394 if (first_ifindex
> 0) {
395 /* found: first ifindex is non-recursive nexthop */
398 /* Report non-recursive success after first
400 if (PIM_DEBUG_PIM_NHT
) {
401 char addr_str
[INET_ADDRSTRLEN
];
402 pim_inet4_dump("<addr?>", addr
,
406 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %s(%s) dist=%d met=%d",
407 __PRETTY_FUNCTION__
, lookup
,
408 max_lookup
, first_ifindex
,
409 addr_str
, pim
->vrf
->name
,
412 nexthop_tab
[0].route_metric
);
415 /* use last address as nexthop address */
416 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
418 /* report original route metric/distance */
419 nexthop_tab
[0].route_metric
= route_metric
;
420 nexthop_tab
[0].protocol_distance
=
427 if (PIM_DEBUG_PIM_NHT
) {
428 char addr_str
[INET_ADDRSTRLEN
];
429 char nexthop_str
[PREFIX_STRLEN
];
430 pim_inet4_dump("<addr?>", addr
, addr_str
,
432 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
,
433 sizeof(nexthop_str
));
435 "%s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s(%s) dist=%d met=%d",
436 __PRETTY_FUNCTION__
, lookup
, max_lookup
,
437 nexthop_str
, addr_str
, pim
->vrf
->name
,
438 nexthop_tab
[0].protocol_distance
,
439 nexthop_tab
[0].route_metric
);
442 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for
445 } /* for (max_lookup) */
447 if (PIM_DEBUG_PIM_NHT
) {
448 char addr_str
[INET_ADDRSTRLEN
];
449 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
451 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s(%s)",
452 __PRETTY_FUNCTION__
, lookup
, max_lookup
, addr_str
,
459 void pim_zlookup_show_ip_multicast(struct vty
*vty
)
461 vty_out(vty
, "Zclient lookup socket: ");
463 vty_out(vty
, "%d failures=%d\n", zlookup
->sock
, zlookup
->fail
);
465 vty_out(vty
, "<null zclient>\n");
469 int pim_zlookup_sg_statistics(struct channel_oil
*c_oil
)
471 struct stream
*s
= zlookup
->obuf
;
472 uint16_t command
= 0;
473 unsigned long long lastused
;
477 struct interface
*ifp
=
478 pim_if_find_by_vif_index(c_oil
->pim
, c_oil
->oil
.mfcc_parent
);
480 if (PIM_DEBUG_ZEBRA
) {
481 struct prefix_sg more
;
483 more
.src
= c_oil
->oil
.mfcc_origin
;
484 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
486 "Sending Request for New Channel Oil Information(%s) VIIF %d(%s)",
487 pim_str_sg_dump(&more
), c_oil
->oil
.mfcc_parent
,
488 c_oil
->pim
->vrf
->name
);
495 zclient_create_header(s
, ZEBRA_IPMR_ROUTE_STATS
, c_oil
->pim
->vrf_id
);
496 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_origin
);
497 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_mcastgrp
);
498 stream_putl(s
, ifp
->ifindex
);
499 stream_putw_at(s
, 0, stream_get_endp(s
));
501 count
= stream_get_endp(s
);
502 ret
= writen(zlookup
->sock
, s
->data
, count
);
505 "%s: writen() failure: %d writing to zclient lookup socket",
506 __PRETTY_FUNCTION__
, errno
);
512 while (command
!= ZEBRA_IPMR_ROUTE_STATS
) {
520 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
521 &version
, &vrf_id
, &command
);
523 zlog_err("%s: zclient_read_header() failed",
524 __PRETTY_FUNCTION__
);
525 zclient_lookup_failed(zlookup
);
530 sg
.src
.s_addr
= stream_get_ipv4(s
);
531 sg
.grp
.s_addr
= stream_get_ipv4(s
);
532 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
533 || sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
) {
534 if (PIM_DEBUG_ZEBRA
) {
535 struct prefix_sg more
;
537 more
.src
= c_oil
->oil
.mfcc_origin
;
538 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
540 "%s: Received wrong %s(%s) information requested",
541 __PRETTY_FUNCTION__
, pim_str_sg_dump(&more
),
542 c_oil
->pim
->vrf
->name
);
544 zclient_lookup_failed(zlookup
);
548 stream_get(&lastused
, s
, sizeof(lastused
));
551 c_oil
->cc
.lastused
= lastused
;