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
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 $QuaggaId: $Format:%an, %ai, %h$ $
24 #include "zebra/rib.h"
36 #include "pim_zlookup.h"
38 extern int zclient_debug;
40 static void zclient_lookup_sched(struct zclient *zlookup, int delay);
42 /* Connect to zebra for nexthop lookup. */
43 static int zclient_lookup_connect(struct thread *t)
45 struct zclient *zlookup;
47 zlookup = THREAD_ARG(t);
48 zlookup->t_connect = NULL;
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 zassert(!zlookup->t_connect);
64 if (zlookup->sock < 0) {
65 /* Since last connect failed, retry within 10 secs */
66 zclient_lookup_sched(zlookup, 10);
73 /* Schedule connection with delay. */
74 static void zclient_lookup_sched(struct zclient *zlookup, int delay)
76 zassert(!zlookup->t_connect);
78 THREAD_TIMER_ON(master, zlookup->t_connect,
79 zclient_lookup_connect,
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);
91 zlookup->t_connect = thread_add_event(master, zclient_lookup_connect,
94 zlog_notice("%s: zclient lookup immediate connection scheduled",
98 /* Schedule reconnection, if needed. */
99 static void zclient_lookup_reconnect(struct zclient *zlookup)
101 if (zlookup->t_connect) {
105 zclient_lookup_sched_now(zlookup);
108 static void zclient_lookup_failed(struct zclient *zlookup)
110 if (zlookup->sock >= 0) {
111 if (close(zlookup->sock)) {
112 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__, zlookup->sock,
113 errno, safe_strerror(errno));
118 zclient_lookup_reconnect(zlookup);
121 struct zclient *zclient_lookup_new()
123 struct zclient *zlookup;
125 zlookup = zclient_new (master);
127 zlog_err("%s: zclient_new() failure",
128 __PRETTY_FUNCTION__);
133 zlookup->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
134 zlookup->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
135 zlookup->t_connect = 0;
137 zclient_lookup_sched_now(zlookup);
139 zlog_notice("%s: zclient lookup socket initialized",
140 __PRETTY_FUNCTION__);
145 static int zclient_read_nexthop(struct zclient *zlookup,
146 struct pim_zlookup_nexthop nexthop_tab[],
152 const uint16_t MIN_LEN = 10; /* getipv4=4 getc=1 getl=4 getc=1 */
158 struct in_addr raddr;
164 if (PIM_DEBUG_ZEBRA) {
166 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
167 zlog_debug("%s: addr=%s",
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);
191 if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
192 zlog_err("%s: socket %d command mismatch: %d",
193 __func__, zlookup->sock, command);
197 raddr.s_addr = stream_get_ipv4(s);
199 if (raddr.s_addr != addr.s_addr) {
202 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
203 pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
204 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
206 addr_str, raddr_str);
210 distance = stream_getc(s);
211 metric = stream_getl(s);
212 nexthop_num = stream_getc(s);
214 if (nexthop_num < 1) {
215 zlog_err("%s: socket %d bad nexthop_num=%d",
216 __func__, zlookup->sock, nexthop_num);
222 for (i = 0; i < nexthop_num; ++i) {
223 enum nexthop_types_t nexthop_type;
226 zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d",
227 __func__, zlookup->sock, length);
231 nexthop_type = stream_getc(s);
234 switch (nexthop_type) {
235 case ZEBRA_NEXTHOP_IFINDEX:
236 case ZEBRA_NEXTHOP_IFNAME:
237 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
238 if (num_ifindex >= tab_size) {
240 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
241 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
242 __FILE__, __PRETTY_FUNCTION__,
243 (num_ifindex + 1), tab_size, addr_str);
246 if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) {
248 zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d",
249 __func__, zlookup->sock, length);
252 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
256 nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY;
258 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
259 nexthop_tab[num_ifindex].protocol_distance = distance;
260 nexthop_tab[num_ifindex].route_metric = metric;
263 case ZEBRA_NEXTHOP_IPV4:
264 if (num_ifindex >= tab_size) {
266 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
267 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
268 __FILE__, __PRETTY_FUNCTION__,
269 (num_ifindex + 1), tab_size, addr_str);
272 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
274 nexthop_tab[num_ifindex].ifindex = 0;
275 nexthop_tab[num_ifindex].protocol_distance = distance;
276 nexthop_tab[num_ifindex].route_metric = metric;
277 if (PIM_DEBUG_ZEBRA) {
279 char nexthop_str[100];
280 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
281 pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str));
282 zlog_debug("%s %s: zebra returned recursive nexthop %s for address %s",
283 __FILE__, __PRETTY_FUNCTION__,
284 nexthop_str, addr_str);
292 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
293 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
294 __FILE__, __PRETTY_FUNCTION__,
295 nexthop_type, addr_str);
304 static int zclient_lookup_nexthop_once(struct zclient *zlookup,
305 struct pim_zlookup_nexthop nexthop_tab[],
312 if (PIM_DEBUG_ZEBRA) {
314 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
315 zlog_debug("%s: addr=%s",
321 if (zlookup->sock < 0) {
322 zlog_err("%s %s: zclient lookup socket is not connected",
323 __FILE__, __PRETTY_FUNCTION__);
324 zclient_lookup_failed(zlookup);
330 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, VRF_DEFAULT);
331 stream_put_in_addr(s, &addr);
332 stream_putw_at(s, 0, stream_get_endp(s));
334 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
336 zlog_err("%s %s: writen() failure writing to zclient lookup socket",
337 __FILE__, __PRETTY_FUNCTION__);
338 zclient_lookup_failed(zlookup);
342 zlog_err("%s %s: connection closed on zclient lookup socket",
343 __FILE__, __PRETTY_FUNCTION__);
344 zclient_lookup_failed(zlookup);
348 return zclient_read_nexthop(zlookup, nexthop_tab,
352 int zclient_lookup_nexthop(struct zclient *zlookup,
353 struct pim_zlookup_nexthop nexthop_tab[],
359 uint32_t route_metric = 0xFFFFFFFF;
360 uint8_t protocol_distance = 0xFF;
362 for (lookup = 0; lookup < max_lookup; ++lookup) {
365 struct in_addr nexthop_addr;
367 num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab,
368 PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr);
369 if ((num_ifindex < 1) && PIM_DEBUG_ZEBRA) {
371 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
372 zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
373 __FILE__, __PRETTY_FUNCTION__,
374 lookup, max_lookup, addr_str);
379 /* this is the non-recursive lookup - save original metric/distance */
380 route_metric = nexthop_tab[0].route_metric;
381 protocol_distance = nexthop_tab[0].protocol_distance;
385 FIXME: Non-recursive nexthop ensured only for first ifindex.
386 However, recursive route lookup should really be fixed in zebra daemon.
389 first_ifindex = nexthop_tab[0].ifindex;
390 nexthop_addr = nexthop_tab[0].nexthop_addr;
391 if (first_ifindex > 0) {
392 /* found: first ifindex is non-recursive nexthop */
394 if ((lookup > 0) && PIM_DEBUG_ZEBRA) {
395 /* Report non-recursive success after first lookup */
397 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
398 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
399 __FILE__, __PRETTY_FUNCTION__,
400 lookup, max_lookup, first_ifindex, addr_str,
401 nexthop_tab[0].protocol_distance,
402 nexthop_tab[0].route_metric);
404 /* use last address as nexthop address */
405 nexthop_tab[0].nexthop_addr = addr;
407 /* report original route metric/distance */
408 nexthop_tab[0].route_metric = route_metric;
409 nexthop_tab[0].protocol_distance = protocol_distance;
415 if (PIM_DEBUG_ZEBRA) {
417 char nexthop_str[100];
418 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
419 pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str));
420 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
421 __FILE__, __PRETTY_FUNCTION__,
422 lookup, max_lookup, nexthop_str, addr_str,
423 nexthop_tab[0].protocol_distance,
424 nexthop_tab[0].route_metric);
427 addr = nexthop_addr; /* use nexthop addr for recursive lookup */
429 } /* for (max_lookup) */
431 if (PIM_DEBUG_ZEBRA) {
433 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
434 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
435 __FILE__, __PRETTY_FUNCTION__,
436 lookup, max_lookup, addr_str);