]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zlookup.c
Merge pull request #770 from donaldsharp/zclient
[mirror_frr.git] / pimd / pim_zlookup.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2008 Everton da Silva Marques
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include <zebra.h>
21 #include "zebra/rib.h"
22
23 #include "log.h"
24 #include "prefix.h"
25 #include "zclient.h"
26 #include "stream.h"
27 #include "network.h"
28 #include "thread.h"
29 #include "prefix.h"
30 #include "vty.h"
31
32 #include "pimd.h"
33 #include "pim_iface.h"
34 #include "pim_pim.h"
35 #include "pim_str.h"
36 #include "pim_oil.h"
37 #include "pim_zlookup.h"
38
39 static struct zclient *zlookup = NULL;
40
41 static void zclient_lookup_sched(struct zclient *zlookup, int delay);
42
43 /* Connect to zebra for nexthop lookup. */
44 static int zclient_lookup_connect(struct thread *t)
45 {
46 struct zclient *zlookup;
47
48 zlookup = THREAD_ARG(t);
49
50 if (zlookup->sock >= 0) {
51 return 0;
52 }
53
54 if (zclient_socket_connect(zlookup) < 0) {
55 ++zlookup->fail;
56 zlog_warn("%s: failure connecting zclient socket: failures=%d",
57 __PRETTY_FUNCTION__, zlookup->fail);
58 }
59 else {
60 zlookup->fail = 0; /* reset counter on connection */
61 }
62
63 if (zlookup->sock < 0) {
64 /* Since last connect failed, retry within 10 secs */
65 zclient_lookup_sched(zlookup, 10);
66 return -1;
67 }
68
69 return 0;
70 }
71
72 /* Schedule connection with delay. */
73 static void zclient_lookup_sched(struct zclient *zlookup, int delay)
74 {
75 thread_add_timer(master, zclient_lookup_connect, zlookup, delay,
76 &zlookup->t_connect);
77
78 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
79 __PRETTY_FUNCTION__, delay);
80 }
81
82 /* Schedule connection for now. */
83 static void zclient_lookup_sched_now(struct zclient *zlookup)
84 {
85 thread_add_event(master, zclient_lookup_connect, zlookup, 0,
86 &zlookup->t_connect);
87
88 zlog_notice("%s: zclient lookup immediate connection scheduled",
89 __PRETTY_FUNCTION__);
90 }
91
92 /* Schedule reconnection, if needed. */
93 static void zclient_lookup_reconnect(struct zclient *zlookup)
94 {
95 if (zlookup->t_connect) {
96 return;
97 }
98
99 zclient_lookup_sched_now(zlookup);
100 }
101
102 static void zclient_lookup_failed(struct zclient *zlookup)
103 {
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));
108 }
109 zlookup->sock = -1;
110 }
111
112 zclient_lookup_reconnect(zlookup);
113 }
114
115 void
116 zclient_lookup_free (void)
117 {
118 zclient_stop (zlookup);
119 zclient_free (zlookup);
120 zlookup = NULL;
121 }
122
123 void
124 zclient_lookup_new (void)
125 {
126 zlookup = zclient_new (master);
127 if (!zlookup) {
128 zlog_err("%s: zclient_new() failure",
129 __PRETTY_FUNCTION__);
130 return;
131 }
132
133 zlookup->sock = -1;
134 zlookup->t_connect = NULL;
135
136 zclient_lookup_sched_now(zlookup);
137
138 zlog_notice("%s: zclient lookup socket initialized",
139 __PRETTY_FUNCTION__);
140
141 }
142
143 static int zclient_read_nexthop(struct zclient *zlookup,
144 struct pim_zlookup_nexthop nexthop_tab[],
145 const int tab_size,
146 struct in_addr addr)
147 {
148 int num_ifindex = 0;
149 struct stream *s;
150 const uint16_t MIN_LEN = 10; /* getipv4=4 getc=1 getl=4 getc=1 */
151 uint16_t length;
152 u_char marker;
153 u_char version;
154 vrf_id_t vrf_id;
155 uint16_t command = 0;
156 struct in_addr raddr;
157 uint8_t distance;
158 uint32_t metric;
159 int nexthop_num;
160 int i, err;
161
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",
166 __PRETTY_FUNCTION__,
167 addr_str);
168 }
169
170 s = zlookup->ibuf;
171
172 while (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB)
173 {
174 stream_reset(s);
175 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
176 &vrf_id, &command);
177 if (err < 0) {
178 zlog_err("%s %s: zclient_read_header() failed",
179 __FILE__, __PRETTY_FUNCTION__);
180 zclient_lookup_failed(zlookup);
181 return -1;
182 }
183
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);
188 return -2;
189 }
190 }
191
192 raddr.s_addr = stream_get_ipv4(s);
193
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",
200 __PRETTY_FUNCTION__,
201 addr_str, raddr_str);
202 /* warning only */
203 }
204
205 distance = stream_getc(s);
206 metric = stream_getl(s);
207 nexthop_num = stream_getc(s);
208
209 if (nexthop_num < 1) {
210 zlog_err("%s: socket %d bad nexthop_num=%d",
211 __func__, zlookup->sock, nexthop_num);
212 return -6;
213 }
214
215 for (i = 0; i < nexthop_num; ++i) {
216 enum nexthop_types_t nexthop_type;
217 struct pim_neighbor *nbr;
218 struct prefix p;
219
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);
227 return num_ifindex;
228 }
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);
237 }
238 else {
239 nexthop_tab[num_ifindex].nexthop_addr.u.prefix4.s_addr = PIM_NET_INADDR_ANY;
240 }
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;
244 ++num_ifindex;
245 break;
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,
249 s,
250 sizeof(struct in6_addr));
251 nexthop_tab[num_ifindex].ifindex = stream_getl (s);
252
253 p.family = AF_INET6;
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));
258
259 /*
260 * If we are sending v6 secondary assume we receive v6 secondary
261 */
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);
264 else
265 nbr = pim_neighbor_find_if (if_lookup_by_index (nexthop_tab[num_ifindex].ifindex, VRF_DEFAULT));
266 if (nbr)
267 {
268 nexthop_tab[num_ifindex].nexthop_addr.family = AF_INET;
269 nexthop_tab[num_ifindex].nexthop_addr.u.prefix4 = nbr->source_addr;
270 }
271 ++num_ifindex;
272 break;
273 default:
274 /* do nothing */
275 {
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);
281 }
282 break;
283 }
284 }
285
286 return num_ifindex;
287 }
288
289 static int
290 zclient_lookup_nexthop_once (struct pim_zlookup_nexthop nexthop_tab[],
291 const int tab_size,
292 struct in_addr addr)
293 {
294 struct stream *s;
295 int ret;
296
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",
301 __PRETTY_FUNCTION__,
302 addr_str);
303 }
304
305 /* Check socket. */
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);
310 return -1;
311 }
312
313 s = zlookup->obuf;
314 stream_reset(s);
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));
318
319 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
320 if (ret < 0) {
321 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
322 __FILE__, __PRETTY_FUNCTION__, errno);
323 zclient_lookup_failed(zlookup);
324 return -2;
325 }
326 if (ret == 0) {
327 zlog_err("%s %s: connection closed on zclient lookup socket",
328 __FILE__, __PRETTY_FUNCTION__);
329 zclient_lookup_failed(zlookup);
330 return -3;
331 }
332
333 return zclient_read_nexthop(zlookup, nexthop_tab,
334 tab_size, addr);
335 }
336
337 int
338 zclient_lookup_nexthop (struct pim_zlookup_nexthop nexthop_tab[],
339 const int tab_size,
340 struct in_addr addr,
341 int max_lookup)
342 {
343 int lookup;
344 uint32_t route_metric = 0xFFFFFFFF;
345 uint8_t protocol_distance = 0xFF;
346
347 qpim_nexthop_lookups++;
348
349 for (lookup = 0; lookup < max_lookup; ++lookup) {
350 int num_ifindex;
351 int first_ifindex;
352 struct prefix nexthop_addr;
353
354 num_ifindex = zclient_lookup_nexthop_once(nexthop_tab,
355 tab_size, addr);
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);
363 }
364 return -1;
365 }
366
367 if (lookup < 1) {
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;
371 }
372
373 /*
374 * FIXME: Non-recursive nexthop ensured only for first ifindex.
375 * However, recursive route lookup should really be fixed in zebra daemon.
376 * See also TODO T24.
377 *
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.
381 */
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 */
386
387 if (lookup > 0) {
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);
397 }
398
399 /* use last address as nexthop address */
400 nexthop_tab[0].nexthop_addr.u.prefix4 = addr;
401
402 /* report original route metric/distance */
403 nexthop_tab[0].route_metric = route_metric;
404 nexthop_tab[0].protocol_distance = protocol_distance;
405 }
406
407 return num_ifindex;
408 }
409
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);
420 }
421
422 addr = nexthop_addr.u.prefix4; /* use nexthop addr for recursive lookup */
423
424 } /* for (max_lookup) */
425
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);
432 }
433
434 return -2;
435 }
436
437 void
438 pim_zlookup_show_ip_multicast (struct vty *vty)
439 {
440 vty_out(vty, "Zclient lookup socket: ");
441 if (zlookup) {
442 vty_outln (vty, "%d failures=%d", zlookup->sock,
443 zlookup->fail);
444 }
445 else {
446 vty_outln (vty, "<null zclient>");
447 }
448 }
449
450 int
451 pim_zlookup_sg_statistics (struct channel_oil *c_oil)
452 {
453 struct stream *s = zlookup->obuf;
454 uint16_t command = 0;
455 unsigned long long lastused;
456 struct prefix_sg sg;
457 int count = 0;
458 int ret;
459 struct interface *ifp = pim_if_find_by_vif_index (c_oil->oil.mfcc_parent);
460
461 if (PIM_DEBUG_ZEBRA)
462 {
463 struct prefix_sg more;
464
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);
469 }
470
471 if (!ifp)
472 return -1;
473
474 stream_reset (s);
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));
480
481 count = stream_get_endp (s);
482 ret = writen (zlookup->sock, s->data, count);
483 if (ret <= 0)
484 {
485 zlog_err("%s %s: writen() failure: %d writing to zclient lookup socket",
486 __FILE__, __PRETTY_FUNCTION__, errno);
487 return -1;
488 }
489
490 s = zlookup->ibuf;
491
492 while (command != ZEBRA_IPMR_ROUTE_STATS)
493 {
494 int err;
495 uint16_t length = 0;
496 vrf_id_t vrf_id;
497 u_char marker;
498 u_char version;
499
500 stream_reset (s);
501 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
502 &vrf_id, &command);
503 if (err < 0)
504 {
505 zlog_err ("%s %s: zclient_read_header() failed",
506 __FILE__, __PRETTY_FUNCTION__);
507 zclient_lookup_failed(zlookup);
508 return -1;
509 }
510 }
511
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)
516 {
517 zlog_err ("%s: Received wrong %s information",
518 __PRETTY_FUNCTION__, pim_str_sg_dump (&sg));
519 zclient_lookup_failed (zlookup);
520 return -3;
521 }
522
523 stream_get (&lastused, s, sizeof (lastused));
524 ret = stream_getl (s);
525
526 if (PIM_DEBUG_ZEBRA)
527 zlog_debug ("Received %lld for %s success: %d", lastused, pim_str_sg_dump (&sg), ret);
528
529 c_oil->cc.lastused = lastused;
530
531 return 0;
532
533 }