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
30 #include "lib_errors.h"
33 #include "pim_iface.h"
37 #include "pim_zlookup.h"
39 static struct zclient
*zlookup
= NULL
;
40 struct thread
*zlookup_read
;
42 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
);
43 static int zclient_lookup_read_pipe(struct thread
*thread
);
45 /* Connect to zebra for nexthop lookup. */
46 static int zclient_lookup_connect(struct thread
*t
)
48 struct zclient
*zlookup
;
50 zlookup
= THREAD_ARG(t
);
52 if (zlookup
->sock
>= 0) {
56 if (zclient_socket_connect(zlookup
) < 0) {
58 zlog_warn("%s: failure connecting zclient socket: failures=%d",
59 __func__
, zlookup
->fail
);
61 zlookup
->fail
= 0; /* reset counter on connection */
64 if (zclient_send_hello(zlookup
) < 0) {
65 if (close(zlookup
->sock
)) {
66 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
67 zlookup
->sock
, errno
, safe_strerror(errno
));
72 if (zlookup
->sock
< 0) {
73 /* Since last connect failed, retry within 10 secs */
74 zclient_lookup_sched(zlookup
, 10);
78 thread_add_timer(router
->master
, zclient_lookup_read_pipe
, zlookup
, 60,
83 /* Schedule connection with delay. */
84 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
86 thread_add_timer(router
->master
, zclient_lookup_connect
, zlookup
, delay
,
89 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
93 /* Schedule connection for now. */
94 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
96 thread_add_event(router
->master
, zclient_lookup_connect
, zlookup
, 0,
99 zlog_notice("%s: zclient lookup immediate connection scheduled",
103 /* Schedule reconnection, if needed. */
104 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
106 if (zlookup
->t_connect
) {
110 zclient_lookup_sched_now(zlookup
);
113 static void zclient_lookup_failed(struct zclient
*zlookup
)
115 if (zlookup
->sock
>= 0) {
116 if (close(zlookup
->sock
)) {
117 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
118 zlookup
->sock
, errno
, safe_strerror(errno
));
123 zclient_lookup_reconnect(zlookup
);
126 void zclient_lookup_free(void)
128 THREAD_OFF(zlookup_read
);
129 zclient_stop(zlookup
);
130 zclient_free(zlookup
);
134 void zclient_lookup_new(void)
136 struct zclient_options options
= zclient_options_default
;
137 options
.synchronous
= true;
139 zlookup
= zclient_new(router
->master
, &options
);
141 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: zclient_new() failure",
147 zlookup
->t_connect
= NULL
;
148 zlookup
->privs
= &pimd_privs
;
150 zclient_lookup_sched_now(zlookup
);
152 zlog_notice("%s: zclient lookup socket initialized", __func__
);
155 static int zclient_read_nexthop(struct pim_instance
*pim
,
156 struct zclient
*zlookup
,
157 struct pim_zlookup_nexthop nexthop_tab
[],
158 const int tab_size
, struct in_addr addr
)
166 uint16_t command
= 0;
167 struct in_addr raddr
;
173 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
174 char addr_str
[INET_ADDRSTRLEN
];
176 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
177 zlog_debug("%s: addr=%s(%s)", __func__
, addr_str
,
183 while (command
!= ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
) {
185 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
186 &version
, &vrf_id
, &command
);
188 flog_err(EC_LIB_ZAPI_MISSMATCH
,
189 "%s: zclient_read_header() failed", __func__
);
190 zclient_lookup_failed(zlookup
);
194 if (command
== ZEBRA_ERROR
) {
195 enum zebra_error_types error
;
197 zapi_error_decode(s
, &error
);
198 /* Do nothing with it for now */
203 raddr
.s_addr
= stream_get_ipv4(s
);
205 if (raddr
.s_addr
!= addr
.s_addr
) {
206 char addr_str
[INET_ADDRSTRLEN
];
207 char raddr_str
[INET_ADDRSTRLEN
];
208 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
209 pim_inet4_dump("<raddr?>", raddr
, raddr_str
, sizeof(raddr_str
));
210 zlog_warn("%s: address mismatch: addr=%s(%s) raddr=%s",
211 __func__
, addr_str
, pim
->vrf
->name
, raddr_str
);
215 distance
= stream_getc(s
);
216 metric
= stream_getl(s
);
217 nexthop_num
= stream_getc(s
);
219 if (nexthop_num
< 1) {
220 if (PIM_DEBUG_PIM_NHT_DETAIL
)
221 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__
,
222 zlookup
->sock
, nexthop_num
);
226 for (i
= 0; i
< nexthop_num
; ++i
) {
227 vrf_id_t nexthop_vrf_id
;
228 enum nexthop_types_t nexthop_type
;
229 struct pim_neighbor
*nbr
;
232 nexthop_vrf_id
= stream_getl(s
);
233 nexthop_type
= stream_getc(s
);
234 if (num_ifindex
>= tab_size
) {
235 char addr_str
[INET_ADDRSTRLEN
];
236 pim_inet4_dump("<addr?>", addr
, addr_str
,
239 "%s: found too many nexthop ifindexes (%d > %d) for address %s(%s)",
240 __func__
, (num_ifindex
+ 1), tab_size
, addr_str
,
244 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
245 nexthop_tab
[num_ifindex
].route_metric
= metric
;
246 nexthop_tab
[num_ifindex
].vrf_id
= nexthop_vrf_id
;
247 switch (nexthop_type
) {
248 case NEXTHOP_TYPE_IFINDEX
:
249 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
251 * Connected route (i.e. no nexthop), use
252 * address passed in as PIM nexthop. This will
253 * allow us to work in cases where we are
254 * trying to find a route for this box.
256 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
257 nexthop_tab
[num_ifindex
].nexthop_addr
.prefixlen
=
259 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
=
263 case NEXTHOP_TYPE_IPV4_IFINDEX
:
264 case NEXTHOP_TYPE_IPV4
:
265 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET
;
266 nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix4
.s_addr
=
268 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
271 case NEXTHOP_TYPE_IPV6_IFINDEX
:
272 nexthop_tab
[num_ifindex
].nexthop_addr
.family
= AF_INET6
;
273 stream_get(&nexthop_tab
[num_ifindex
]
274 .nexthop_addr
.u
.prefix6
,
275 s
, sizeof(struct in6_addr
));
276 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
279 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
281 &nexthop_tab
[num_ifindex
].nexthop_addr
.u
.prefix6
,
282 sizeof(struct in6_addr
));
285 * If we are sending v6 secondary assume we receive v6
288 struct interface
*ifp
= if_lookup_by_index(
289 nexthop_tab
[num_ifindex
].ifindex
,
294 else if (pim
->send_v6_secondary
)
295 nbr
= pim_neighbor_find_by_secondary(ifp
, &p
);
297 nbr
= pim_neighbor_find_if(ifp
);
300 nexthop_tab
[num_ifindex
].nexthop_addr
.family
=
302 nexthop_tab
[num_ifindex
]
303 .nexthop_addr
.u
.prefix4
=
311 char addr_str
[INET_ADDRSTRLEN
];
312 pim_inet4_dump("<addr?>", addr
, addr_str
,
315 "%s: found non-ifindex nexthop type=%d for address %s(%s)",
316 __func__
, nexthop_type
, addr_str
,
326 static int zclient_lookup_nexthop_once(struct pim_instance
*pim
,
327 struct pim_zlookup_nexthop nexthop_tab
[],
328 const int tab_size
, struct in_addr addr
)
333 if (PIM_DEBUG_PIM_NHT_DETAIL
) {
334 char addr_str
[INET_ADDRSTRLEN
];
335 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
336 zlog_debug("%s: addr=%s(%s)", __func__
, addr_str
,
341 if (zlookup
->sock
< 0) {
342 flog_err(EC_LIB_ZAPI_SOCKET
,
343 "%s: zclient lookup socket is not connected",
345 zclient_lookup_failed(zlookup
);
349 if (pim
->vrf
->vrf_id
== VRF_UNKNOWN
) {
351 "%s: VRF: %s does not fully exist yet, delaying lookup",
352 __func__
, pim
->vrf
->name
);
358 zclient_create_header(s
, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
,
360 stream_put_in_addr(s
, &addr
);
361 stream_putw_at(s
, 0, stream_get_endp(s
));
363 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
367 "%s: writen() failure: %d writing to zclient lookup socket",
369 zclient_lookup_failed(zlookup
);
373 flog_err_sys(EC_LIB_SOCKET
,
374 "%s: connection closed on zclient lookup socket",
376 zclient_lookup_failed(zlookup
);
380 return zclient_read_nexthop(pim
, zlookup
, nexthop_tab
, tab_size
, addr
);
383 int zclient_lookup_read_pipe(struct thread
*thread
)
385 struct zclient
*zlookup
= THREAD_ARG(thread
);
386 struct pim_instance
*pim
= pim_get_pim_instance(VRF_DEFAULT
);
387 struct pim_zlookup_nexthop nexthop_tab
[10];
388 struct in_addr l
= {.s_addr
= INADDR_ANY
};
390 zclient_lookup_nexthop_once(pim
, nexthop_tab
, 10, l
);
391 thread_add_timer(router
->master
, zclient_lookup_read_pipe
, zlookup
, 60,
397 int zclient_lookup_nexthop(struct pim_instance
*pim
,
398 struct pim_zlookup_nexthop nexthop_tab
[],
399 const int tab_size
, struct in_addr addr
,
403 uint32_t route_metric
= 0xFFFFFFFF;
404 uint8_t protocol_distance
= 0xFF;
406 pim
->nexthop_lookups
++;
408 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
411 struct prefix nexthop_addr
;
413 num_ifindex
= zclient_lookup_nexthop_once(pim
, nexthop_tab
,
415 if (num_ifindex
< 1) {
416 if (PIM_DEBUG_PIM_NHT
) {
417 char addr_str
[INET_ADDRSTRLEN
];
418 pim_inet4_dump("<addr?>", addr
, addr_str
,
421 "%s: lookup=%d/%d: could not find nexthop ifindex for address %s(%s)",
422 __func__
, lookup
, max_lookup
, addr_str
,
429 /* this is the non-recursive lookup - save original
431 route_metric
= nexthop_tab
[0].route_metric
;
432 protocol_distance
= nexthop_tab
[0].protocol_distance
;
436 * FIXME: Non-recursive nexthop ensured only for first ifindex.
437 * However, recursive route lookup should really be fixed in
441 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
442 * it was being stored. This Doesn't solve all cases of
443 * recursive lookup but for the most common types it does.
445 first_ifindex
= nexthop_tab
[0].ifindex
;
446 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
447 if (first_ifindex
> 0) {
448 /* found: first ifindex is non-recursive nexthop */
451 /* Report non-recursive success after first
453 if (PIM_DEBUG_PIM_NHT
) {
454 char addr_str
[INET_ADDRSTRLEN
];
455 pim_inet4_dump("<addr?>", addr
,
459 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %s(%s) dist=%d met=%d",
460 __func__
, lookup
, max_lookup
,
461 first_ifindex
, addr_str
,
465 nexthop_tab
[0].route_metric
);
468 /* use last address as nexthop address */
469 nexthop_tab
[0].nexthop_addr
.u
.prefix4
= addr
;
471 /* report original route metric/distance */
472 nexthop_tab
[0].route_metric
= route_metric
;
473 nexthop_tab
[0].protocol_distance
=
480 if (PIM_DEBUG_PIM_NHT
) {
481 char addr_str
[INET_ADDRSTRLEN
];
482 char nexthop_str
[PREFIX_STRLEN
];
483 pim_inet4_dump("<addr?>", addr
, addr_str
,
485 pim_addr_dump("<nexthop?>", &nexthop_addr
, nexthop_str
,
486 sizeof(nexthop_str
));
488 "%s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s(%s) dist=%d met=%d",
489 __func__
, lookup
, max_lookup
, nexthop_str
,
490 addr_str
, pim
->vrf
->name
,
491 nexthop_tab
[0].protocol_distance
,
492 nexthop_tab
[0].route_metric
);
495 addr
= nexthop_addr
.u
.prefix4
; /* use nexthop addr for
498 } /* for (max_lookup) */
500 if (PIM_DEBUG_PIM_NHT
) {
501 char addr_str
[INET_ADDRSTRLEN
];
502 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
504 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s(%s)",
505 __func__
, lookup
, max_lookup
, addr_str
, pim
->vrf
->name
);
511 void pim_zlookup_show_ip_multicast(struct vty
*vty
)
513 vty_out(vty
, "Zclient lookup socket: ");
515 vty_out(vty
, "%d failures=%d\n", zlookup
->sock
, zlookup
->fail
);
517 vty_out(vty
, "<null zclient>\n");
521 int pim_zlookup_sg_statistics(struct channel_oil
*c_oil
)
523 struct stream
*s
= zlookup
->obuf
;
524 uint16_t command
= 0;
525 unsigned long long lastused
;
529 struct interface
*ifp
=
530 pim_if_find_by_vif_index(c_oil
->pim
, c_oil
->oil
.mfcc_parent
);
532 if (PIM_DEBUG_ZEBRA
) {
533 struct prefix_sg more
;
535 more
.src
= c_oil
->oil
.mfcc_origin
;
536 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
538 "Sending Request for New Channel Oil Information%s VIIF %d(%s)",
539 pim_str_sg_dump(&more
), c_oil
->oil
.mfcc_parent
,
540 c_oil
->pim
->vrf
->name
);
547 zclient_create_header(s
, ZEBRA_IPMR_ROUTE_STATS
, c_oil
->pim
->vrf_id
);
548 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_origin
);
549 stream_put_in_addr(s
, &c_oil
->oil
.mfcc_mcastgrp
);
550 stream_putl(s
, ifp
->ifindex
);
551 stream_putw_at(s
, 0, stream_get_endp(s
));
553 count
= stream_get_endp(s
);
554 ret
= writen(zlookup
->sock
, s
->data
, count
);
558 "%s: writen() failure: %d writing to zclient lookup socket",
565 while (command
!= ZEBRA_IPMR_ROUTE_STATS
) {
573 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
574 &version
, &vrf_id
, &command
);
576 flog_err(EC_LIB_ZAPI_MISSMATCH
,
577 "%s: zclient_read_header() failed", __func__
);
578 zclient_lookup_failed(zlookup
);
583 sg
.src
.s_addr
= stream_get_ipv4(s
);
584 sg
.grp
.s_addr
= stream_get_ipv4(s
);
585 if (sg
.src
.s_addr
!= c_oil
->oil
.mfcc_origin
.s_addr
586 || sg
.grp
.s_addr
!= c_oil
->oil
.mfcc_mcastgrp
.s_addr
) {
587 if (PIM_DEBUG_ZEBRA
) {
588 struct prefix_sg more
;
590 more
.src
= c_oil
->oil
.mfcc_origin
;
591 more
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
593 EC_LIB_ZAPI_MISSMATCH
,
594 "%s: Received wrong %s(%s) information requested",
595 __func__
, pim_str_sg_dump(&more
),
596 c_oil
->pim
->vrf
->name
);
598 zclient_lookup_failed(zlookup
);
602 stream_get(&lastused
, s
, sizeof(lastused
));
605 c_oil
->cc
.lastused
= lastused
;