]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_nht.c
pim6d: Handle mrib_nexthop_addr & pim_zlookup_nexthop in pim_nht
[mirror_frr.git] / pimd / pim_nht.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2017 Cumulus Networks, Inc.
4 * Chirag Shah
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21 #include "network.h"
22 #include "zclient.h"
23 #include "stream.h"
24 #include "nexthop.h"
25 #include "if.h"
26 #include "hash.h"
27 #include "jhash.h"
28
29 #include "lib/printfrr.h"
30
31 #include "pimd.h"
32 #include "pimd/pim_nht.h"
33 #include "log.h"
34 #include "pim_time.h"
35 #include "pim_oil.h"
36 #include "pim_ifchannel.h"
37 #include "pim_mroute.h"
38 #include "pim_zebra.h"
39 #include "pim_upstream.h"
40 #include "pim_join.h"
41 #include "pim_jp_agg.h"
42 #include "pim_zebra.h"
43 #include "pim_zlookup.h"
44 #include "pim_rp.h"
45 #include "pim_addr.h"
46
47 /**
48 * pim_sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
49 * command to Zebra.
50 */
51 void pim_sendmsg_zebra_rnh(struct pim_instance *pim, struct zclient *zclient,
52 struct pim_nexthop_cache *pnc, int command)
53 {
54 struct prefix *p;
55 int ret;
56
57 p = &(pnc->rpf.rpf_addr);
58 ret = zclient_send_rnh(zclient, command, p, SAFI_UNICAST, false, false,
59 pim->vrf->vrf_id);
60 if (ret == ZCLIENT_SEND_FAILURE)
61 zlog_warn("sendmsg_nexthop: zclient_send_message() failed");
62
63 if (PIM_DEBUG_PIM_NHT)
64 zlog_debug(
65 "%s: NHT %sregistered addr %pFX(%s) with Zebra ret:%d ",
66 __func__,
67 (command == ZEBRA_NEXTHOP_REGISTER) ? " " : "de", p,
68 pim->vrf->name, ret);
69
70 return;
71 }
72
73 struct pim_nexthop_cache *pim_nexthop_cache_find(struct pim_instance *pim,
74 struct pim_rpf *rpf)
75 {
76 struct pim_nexthop_cache *pnc = NULL;
77 struct pim_nexthop_cache lookup;
78
79 lookup.rpf.rpf_addr = rpf->rpf_addr;
80 pnc = hash_lookup(pim->rpf_hash, &lookup);
81
82 return pnc;
83 }
84
85 static struct pim_nexthop_cache *pim_nexthop_cache_add(struct pim_instance *pim,
86 struct pim_rpf *rpf_addr)
87 {
88 struct pim_nexthop_cache *pnc;
89 char hash_name[64];
90
91 pnc = XCALLOC(MTYPE_PIM_NEXTHOP_CACHE,
92 sizeof(struct pim_nexthop_cache));
93 pnc->rpf.rpf_addr = rpf_addr->rpf_addr;
94
95 pnc = hash_get(pim->rpf_hash, pnc, hash_alloc_intern);
96
97 pnc->rp_list = list_new();
98 pnc->rp_list->cmp = pim_rp_list_cmp;
99
100 snprintfrr(hash_name, sizeof(hash_name), "PNC %pFX(%s) Upstream Hash",
101 &pnc->rpf.rpf_addr, pim->vrf->name);
102 pnc->upstream_hash = hash_create_size(8192, pim_upstream_hash_key,
103 pim_upstream_equal, hash_name);
104
105 return pnc;
106 }
107
108 static struct pim_nexthop_cache *pim_nht_get(struct pim_instance *pim,
109 struct prefix *addr)
110 {
111 struct pim_nexthop_cache *pnc = NULL;
112 struct pim_rpf rpf;
113 struct zclient *zclient = NULL;
114
115 zclient = pim_zebra_zclient_get();
116 memset(&rpf, 0, sizeof(struct pim_rpf));
117 rpf.rpf_addr = *addr;
118
119 pnc = pim_nexthop_cache_find(pim, &rpf);
120 if (!pnc) {
121 pnc = pim_nexthop_cache_add(pim, &rpf);
122 pim_sendmsg_zebra_rnh(pim, zclient, pnc,
123 ZEBRA_NEXTHOP_REGISTER);
124 if (PIM_DEBUG_PIM_NHT_DETAIL)
125 zlog_debug(
126 "%s: NHT cache and zebra notification added for %pFX(%s)",
127 __func__, addr, pim->vrf->name);
128 }
129
130 return pnc;
131 }
132
133 /* TBD: this does several distinct things and should probably be split up.
134 * (checking state vs. returning pnc vs. adding upstream vs. adding rp)
135 */
136 int pim_find_or_track_nexthop(struct pim_instance *pim, struct prefix *addr,
137 struct pim_upstream *up, struct rp_info *rp,
138 struct pim_nexthop_cache *out_pnc)
139 {
140 struct pim_nexthop_cache *pnc;
141 struct listnode *ch_node = NULL;
142
143 pnc = pim_nht_get(pim, addr);
144
145 assertf(up || rp, "addr=%pFX", addr);
146
147 if (rp != NULL) {
148 ch_node = listnode_lookup(pnc->rp_list, rp);
149 if (ch_node == NULL)
150 listnode_add_sort(pnc->rp_list, rp);
151 }
152
153 if (up != NULL)
154 hash_get(pnc->upstream_hash, up, hash_alloc_intern);
155
156 if (CHECK_FLAG(pnc->flags, PIM_NEXTHOP_VALID)) {
157 if (out_pnc)
158 memcpy(out_pnc, pnc, sizeof(struct pim_nexthop_cache));
159 return 1;
160 }
161
162 return 0;
163 }
164
165 #if PIM_IPV == 4
166 void pim_nht_bsr_add(struct pim_instance *pim, struct in_addr addr)
167 {
168 struct pim_nexthop_cache *pnc;
169 struct prefix pfx;
170
171 pfx.family = AF_INET;
172 pfx.prefixlen = IPV4_MAX_BITLEN;
173 pfx.u.prefix4 = addr;
174
175 pnc = pim_nht_get(pim, &pfx);
176
177 pnc->bsr_count++;
178 }
179 #endif /* PIM_IPV == 4 */
180
181 static void pim_nht_drop_maybe(struct pim_instance *pim,
182 struct pim_nexthop_cache *pnc)
183 {
184 if (PIM_DEBUG_PIM_NHT)
185 zlog_debug(
186 "%s: NHT %pFX(%s) rp_list count:%d upstream count:%ld BSR count:%u",
187 __func__, &pnc->rpf.rpf_addr, pim->vrf->name,
188 pnc->rp_list->count, pnc->upstream_hash->count,
189 pnc->bsr_count);
190
191 if (pnc->rp_list->count == 0 && pnc->upstream_hash->count == 0
192 && pnc->bsr_count == 0) {
193 struct zclient *zclient = pim_zebra_zclient_get();
194
195 pim_sendmsg_zebra_rnh(pim, zclient, pnc,
196 ZEBRA_NEXTHOP_UNREGISTER);
197
198 list_delete(&pnc->rp_list);
199 hash_free(pnc->upstream_hash);
200
201 hash_release(pim->rpf_hash, pnc);
202 if (pnc->nexthop)
203 nexthops_free(pnc->nexthop);
204 XFREE(MTYPE_PIM_NEXTHOP_CACHE, pnc);
205 }
206 }
207
208 void pim_delete_tracked_nexthop(struct pim_instance *pim, struct prefix *addr,
209 struct pim_upstream *up, struct rp_info *rp)
210 {
211 struct pim_nexthop_cache *pnc = NULL;
212 struct pim_nexthop_cache lookup;
213 struct pim_upstream *upstream = NULL;
214
215 /* Remove from RPF hash if it is the last entry */
216 lookup.rpf.rpf_addr = *addr;
217 pnc = hash_lookup(pim->rpf_hash, &lookup);
218 if (!pnc) {
219 zlog_warn("attempting to delete nonexistent NHT entry %pFX",
220 addr);
221 return;
222 }
223
224 if (rp) {
225 /* Release the (*, G)upstream from pnc->upstream_hash,
226 * whose Group belongs to the RP getting deleted
227 */
228 frr_each (rb_pim_upstream, &pim->upstream_head, upstream) {
229 struct prefix grp;
230 struct rp_info *trp_info;
231
232 if (!pim_addr_is_any(upstream->sg.src))
233 continue;
234
235 pim_addr_to_prefix(&grp, upstream->sg.grp);
236 trp_info = pim_rp_find_match_group(pim, &grp);
237 if (trp_info == rp)
238 hash_release(pnc->upstream_hash, upstream);
239 }
240 listnode_delete(pnc->rp_list, rp);
241 }
242
243 if (up)
244 hash_release(pnc->upstream_hash, up);
245
246 pim_nht_drop_maybe(pim, pnc);
247 }
248
249 #if PIM_IPV == 4
250 void pim_nht_bsr_del(struct pim_instance *pim, struct in_addr addr)
251 {
252 struct pim_nexthop_cache *pnc = NULL;
253 struct pim_nexthop_cache lookup;
254
255 /*
256 * Nothing to do here if the address to unregister
257 * is 0.0.0.0 as that the BSR has not been registered
258 * for tracking yet.
259 */
260 if (addr.s_addr == INADDR_ANY)
261 return;
262
263 lookup.rpf.rpf_addr.family = AF_INET;
264 lookup.rpf.rpf_addr.prefixlen = IPV4_MAX_BITLEN;
265 lookup.rpf.rpf_addr.u.prefix4 = addr;
266
267 pnc = hash_lookup(pim->rpf_hash, &lookup);
268
269 if (!pnc) {
270 zlog_warn("attempting to delete nonexistent NHT BSR entry %pI4",
271 &addr);
272 return;
273 }
274
275 assertf(pnc->bsr_count > 0, "addr=%pI4", &addr);
276 pnc->bsr_count--;
277
278 pim_nht_drop_maybe(pim, pnc);
279 }
280
281 bool pim_nht_bsr_rpf_check(struct pim_instance *pim, struct in_addr bsr_addr,
282 struct interface *src_ifp, pim_addr src_ip)
283 {
284 struct pim_nexthop_cache *pnc = NULL;
285 struct pim_nexthop_cache lookup;
286 struct pim_neighbor *nbr = NULL;
287 struct nexthop *nh;
288 struct interface *ifp;
289
290 lookup.rpf.rpf_addr.family = AF_INET;
291 lookup.rpf.rpf_addr.prefixlen = IPV4_MAX_BITLEN;
292 lookup.rpf.rpf_addr.u.prefix4 = bsr_addr;
293
294 pnc = hash_lookup(pim->rpf_hash, &lookup);
295 if (!pnc || !CHECK_FLAG(pnc->flags, PIM_NEXTHOP_ANSWER_RECEIVED)) {
296 /* BSM from a new freshly registered BSR - do a synchronous
297 * zebra query since otherwise we'd drop the first packet,
298 * leading to additional delay in picking up BSM data
299 */
300
301 /* FIXME: this should really be moved into a generic NHT
302 * function that does "add and get immediate result" or maybe
303 * "check cache or get immediate result." But until that can
304 * be worked in, here's a copy of the code below :(
305 */
306 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
307 ifindex_t i;
308 struct interface *ifp = NULL;
309 int num_ifindex;
310
311 memset(nexthop_tab, 0, sizeof(nexthop_tab));
312 num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab,
313 MULTIPATH_NUM, bsr_addr,
314 PIM_NEXTHOP_LOOKUP_MAX);
315
316 if (num_ifindex <= 0)
317 return false;
318
319 for (i = 0; i < num_ifindex; i++) {
320 struct pim_zlookup_nexthop *znh = &nexthop_tab[i];
321
322 /* pim_zlookup_nexthop has no ->type */
323
324 /* 1:1 match code below with znh instead of nh */
325 ifp = if_lookup_by_index(znh->ifindex,
326 pim->vrf->vrf_id);
327
328 if (!ifp || !ifp->info)
329 continue;
330
331 if (if_is_loopback(ifp) && if_is_loopback(src_ifp))
332 return true;
333
334 nbr = pim_neighbor_find(ifp, znh->nexthop_addr);
335 if (!nbr)
336 continue;
337
338 return znh->ifindex == src_ifp->ifindex &&
339 (!pim_addr_cmp(znh->nexthop_addr, src_ip));
340 }
341 return false;
342 }
343
344 if (!CHECK_FLAG(pnc->flags, PIM_NEXTHOP_VALID))
345 return false;
346
347 /* if we accept BSMs from more than one ECMP nexthop, this will cause
348 * BSM message "multiplication" for each ECMP hop. i.e. if you have
349 * 4-way ECMP and 4 hops you end up with 256 copies of each BSM
350 * message.
351 *
352 * so... only accept the first (IPv4) valid nexthop as source.
353 */
354
355 for (nh = pnc->nexthop; nh; nh = nh->next) {
356 pim_addr nhaddr;
357
358 switch (nh->type) {
359 #if PIM_IPV == 4
360 case NEXTHOP_TYPE_IPV4:
361 if (nh->ifindex == IFINDEX_INTERNAL)
362 continue;
363
364 /* fallthru */
365 case NEXTHOP_TYPE_IPV4_IFINDEX:
366 nhaddr = nh->gate.ipv4;
367 break;
368 #else
369 case NEXTHOP_TYPE_IPV6:
370 if (nh->ifindex == IFINDEX_INTERNAL)
371 continue;
372
373 /* fallthru */
374 case NEXTHOP_TYPE_IPV6_IFINDEX:
375 nhaddr = nh->gate.ipv6;
376 break;
377 #endif
378 case NEXTHOP_TYPE_IFINDEX:
379 nhaddr = bsr_addr;
380 break;
381
382 default:
383 continue;
384 }
385
386 ifp = if_lookup_by_index(nh->ifindex, pim->vrf->vrf_id);
387 if (!ifp || !ifp->info)
388 continue;
389
390 if (if_is_loopback(ifp) && if_is_loopback(src_ifp))
391 return true;
392
393 /* MRIB (IGP) may be pointing at a router where PIM is down */
394 nbr = pim_neighbor_find(ifp, nhaddr);
395 if (!nbr)
396 continue;
397
398 return nh->ifindex == src_ifp->ifindex
399 && nhaddr.s_addr == src_ip.s_addr;
400 }
401 return false;
402 }
403 #endif /* PIM_IPV == 4 */
404
405 void pim_rp_nexthop_del(struct rp_info *rp_info)
406 {
407 rp_info->rp.source_nexthop.interface = NULL;
408 rp_info->rp.source_nexthop.mrib_nexthop_addr = PIMADDR_ANY;
409 rp_info->rp.source_nexthop.mrib_metric_preference =
410 router->infinite_assert_metric.metric_preference;
411 rp_info->rp.source_nexthop.mrib_route_metric =
412 router->infinite_assert_metric.route_metric;
413 }
414
415 /* Update RP nexthop info based on Nexthop update received from Zebra.*/
416 static void pim_update_rp_nh(struct pim_instance *pim,
417 struct pim_nexthop_cache *pnc)
418 {
419 struct listnode *node = NULL;
420 struct rp_info *rp_info = NULL;
421
422 /*Traverse RP list and update each RP Nexthop info */
423 for (ALL_LIST_ELEMENTS_RO(pnc->rp_list, node, rp_info)) {
424 if (pim_rpf_addr_is_inaddr_any(&rp_info->rp))
425 continue;
426
427 // Compute PIM RPF using cached nexthop
428 if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop,
429 &rp_info->rp.rpf_addr,
430 &rp_info->group, 1))
431 pim_rp_nexthop_del(rp_info);
432 }
433 }
434
435 /* Update Upstream nexthop info based on Nexthop update received from Zebra.*/
436 static int pim_update_upstream_nh_helper(struct hash_bucket *bucket, void *arg)
437 {
438 struct pim_instance *pim = (struct pim_instance *)arg;
439 struct pim_upstream *up = (struct pim_upstream *)bucket->data;
440
441 enum pim_rpf_result rpf_result;
442 struct pim_rpf old;
443
444 old.source_nexthop.interface = up->rpf.source_nexthop.interface;
445 rpf_result = pim_rpf_update(pim, up, &old, __func__);
446
447 /* update kernel multicast forwarding cache (MFC); if the
448 * RPF nbr is now unreachable the MFC has already been updated
449 * by pim_rpf_clear
450 */
451 if (rpf_result != PIM_RPF_FAILURE)
452 pim_upstream_mroute_iif_update(up->channel_oil, __func__);
453
454 if (rpf_result == PIM_RPF_CHANGED ||
455 (rpf_result == PIM_RPF_FAILURE && old.source_nexthop.interface))
456 pim_zebra_upstream_rpf_changed(pim, up, &old);
457
458
459 if (PIM_DEBUG_PIM_NHT) {
460 zlog_debug(
461 "%s: NHT upstream %s(%s) old ifp %s new ifp %s",
462 __func__, up->sg_str, pim->vrf->name,
463 old.source_nexthop.interface ? old.source_nexthop
464 .interface->name
465 : "Unknown",
466 up->rpf.source_nexthop.interface ? up->rpf.source_nexthop
467 .interface->name
468 : "Unknown");
469 }
470
471 return HASHWALK_CONTINUE;
472 }
473
474 static int pim_update_upstream_nh(struct pim_instance *pim,
475 struct pim_nexthop_cache *pnc)
476 {
477 hash_walk(pnc->upstream_hash, pim_update_upstream_nh_helper, pim);
478
479 pim_zebra_update_all_interfaces(pim);
480
481 return 0;
482 }
483
484 uint32_t pim_compute_ecmp_hash(struct prefix *src, struct prefix *grp)
485 {
486 uint32_t hash_val;
487
488 if (!src)
489 return 0;
490
491 hash_val = prefix_hash_key(src);
492 if (grp)
493 hash_val ^= prefix_hash_key(grp);
494 return hash_val;
495 }
496
497 static int pim_ecmp_nexthop_search(struct pim_instance *pim,
498 struct pim_nexthop_cache *pnc,
499 struct pim_nexthop *nexthop,
500 struct prefix *src, struct prefix *grp,
501 int neighbor_needed)
502 {
503 struct pim_neighbor *nbrs[MULTIPATH_NUM], *nbr = NULL;
504 struct interface *ifps[MULTIPATH_NUM];
505 struct nexthop *nh_node = NULL;
506 ifindex_t first_ifindex;
507 struct interface *ifp = NULL;
508 uint32_t hash_val = 0, mod_val = 0;
509 uint8_t nh_iter = 0, found = 0;
510 uint32_t i, num_nbrs = 0;
511 pim_addr nh_addr = nexthop->mrib_nexthop_addr;
512 pim_addr src_addr = pim_addr_from_prefix(src);
513 pim_addr grp_addr = pim_addr_from_prefix(grp);
514
515 if (!pnc || !pnc->nexthop_num || !nexthop)
516 return 0;
517
518 memset(&nbrs, 0, sizeof(nbrs));
519 memset(&ifps, 0, sizeof(ifps));
520
521
522 // Current Nexthop is VALID, check to stay on the current path.
523 if (nexthop->interface && nexthop->interface->info &&
524 (!pim_addr_is_any(nh_addr))) {
525 /* User configured knob to explicitly switch
526 to new path is disabled or current path
527 metric is less than nexthop update.
528 */
529
530 if (pim->ecmp_rebalance_enable == 0) {
531 uint8_t curr_route_valid = 0;
532 // Check if current nexthop is present in new updated
533 // Nexthop list.
534 // If the current nexthop is not valid, candidate to
535 // choose new Nexthop.
536 for (nh_node = pnc->nexthop; nh_node;
537 nh_node = nh_node->next) {
538 curr_route_valid = (nexthop->interface->ifindex
539 == nh_node->ifindex);
540 if (curr_route_valid)
541 break;
542 }
543
544 if (curr_route_valid &&
545 !pim_if_connected_to_source(nexthop->interface,
546 src_addr)) {
547 nbr = pim_neighbor_find(
548 nexthop->interface,
549 nexthop->mrib_nexthop_addr);
550 if (!nbr
551 && !if_is_loopback(nexthop->interface)) {
552 if (PIM_DEBUG_PIM_NHT)
553 zlog_debug(
554 "%s: current nexthop does not have nbr ",
555 __func__);
556 } else {
557 /* update metric even if the upstream
558 * neighbor stays unchanged
559 */
560 nexthop->mrib_metric_preference =
561 pnc->distance;
562 nexthop->mrib_route_metric =
563 pnc->metric;
564 if (PIM_DEBUG_PIM_NHT)
565 zlog_debug(
566 "%s: (%pPA,%pPA)(%s) current nexthop %s is valid, skipping new path selection",
567 __func__, &src_addr,
568 &grp_addr,
569 pim->vrf->name,
570 nexthop->interface->name);
571 return 1;
572 }
573 }
574 }
575 }
576
577 /*
578 * Look up all interfaces and neighbors,
579 * store for later usage
580 */
581 for (nh_node = pnc->nexthop, i = 0; nh_node;
582 nh_node = nh_node->next, i++) {
583 ifps[i] =
584 if_lookup_by_index(nh_node->ifindex, pim->vrf->vrf_id);
585 if (ifps[i]) {
586 #if PIM_IPV == 4
587 pim_addr nhaddr = nh_node->gate.ipv4;
588 #else
589 pim_addr nhaddr = nh_node->gate.ipv6;
590 #endif
591 nbrs[i] = pim_neighbor_find(ifps[i], nhaddr);
592 if (nbrs[i] ||
593 pim_if_connected_to_source(ifps[i], src_addr))
594 num_nbrs++;
595 }
596 }
597 if (pim->ecmp_enable) {
598 uint32_t consider = pnc->nexthop_num;
599
600 if (neighbor_needed && num_nbrs < consider)
601 consider = num_nbrs;
602
603 if (consider == 0)
604 return 0;
605
606 // PIM ECMP flag is enable then choose ECMP path.
607 hash_val = pim_compute_ecmp_hash(src, grp);
608 mod_val = hash_val % consider;
609 }
610
611 for (nh_node = pnc->nexthop; nh_node && (found == 0);
612 nh_node = nh_node->next) {
613 first_ifindex = nh_node->ifindex;
614 ifp = ifps[nh_iter];
615 if (!ifp) {
616 if (PIM_DEBUG_PIM_NHT)
617 zlog_debug(
618 "%s %s: could not find interface for ifindex %d (address %pPA(%s))",
619 __FILE__, __func__, first_ifindex,
620 &src_addr, pim->vrf->name);
621 if (nh_iter == mod_val)
622 mod_val++; // Select nexthpath
623 nh_iter++;
624 continue;
625 }
626 if (!ifp->info) {
627 if (PIM_DEBUG_PIM_NHT)
628 zlog_debug(
629 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %pPA)",
630 __func__, ifp->name, pim->vrf->name,
631 first_ifindex, &src_addr);
632 if (nh_iter == mod_val)
633 mod_val++; // Select nexthpath
634 nh_iter++;
635 continue;
636 }
637
638 if (neighbor_needed &&
639 !pim_if_connected_to_source(ifp, src_addr)) {
640 nbr = nbrs[nh_iter];
641 if (!nbr && !if_is_loopback(ifp)) {
642 if (PIM_DEBUG_PIM_NHT)
643 zlog_debug(
644 "%s: pim nbr not found on input interface %s(%s)",
645 __func__, ifp->name,
646 pim->vrf->name);
647 if (nh_iter == mod_val)
648 mod_val++; // Select nexthpath
649 nh_iter++;
650 continue;
651 }
652 }
653
654 if (nh_iter == mod_val) {
655 nexthop->interface = ifp;
656 #if PIM_IPV == 4
657 nexthop->mrib_nexthop_addr = nh_node->gate.ipv4;
658 #else
659 nexthop->mrib_nexthop_addr = nh_node->gate.ipv6;
660 #endif
661 nexthop->mrib_metric_preference = pnc->distance;
662 nexthop->mrib_route_metric = pnc->metric;
663 nexthop->last_lookup = src_addr;
664 nexthop->last_lookup_time = pim_time_monotonic_usec();
665 nexthop->nbr = nbr;
666 found = 1;
667 if (PIM_DEBUG_PIM_NHT)
668 zlog_debug(
669 "%s: (%pPA,%pPA)(%s) selected nhop interface %s addr %pPAs mod_val %u iter %d ecmp %d",
670 __func__, &src_addr, &grp_addr,
671 pim->vrf->name, ifp->name, &nh_addr,
672 mod_val, nh_iter, pim->ecmp_enable);
673 }
674 nh_iter++;
675 }
676
677 if (found)
678 return 1;
679 else
680 return 0;
681 }
682
683 /* This API is used to parse Registered address nexthop update coming from Zebra
684 */
685 int pim_parse_nexthop_update(ZAPI_CALLBACK_ARGS)
686 {
687 struct nexthop *nexthop;
688 struct nexthop *nhlist_head = NULL;
689 struct nexthop *nhlist_tail = NULL;
690 int i;
691 struct pim_rpf rpf;
692 struct pim_nexthop_cache *pnc = NULL;
693 struct interface *ifp = NULL;
694 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
695 struct pim_instance *pim;
696 struct zapi_route nhr;
697 struct prefix match;
698
699 if (!vrf)
700 return 0;
701 pim = vrf->info;
702
703 if (!zapi_nexthop_update_decode(zclient->ibuf, &match, &nhr)) {
704 zlog_err("%s: Decode of nexthop update from zebra failed",
705 __func__);
706 return 0;
707 }
708
709 if (cmd == ZEBRA_NEXTHOP_UPDATE) {
710 prefix_copy(&rpf.rpf_addr, &match);
711 pnc = pim_nexthop_cache_find(pim, &rpf);
712 if (!pnc) {
713 if (PIM_DEBUG_PIM_NHT)
714 zlog_debug(
715 "%s: Skipping NHT update, addr %pFX is not in local cached DB.",
716 __func__, &rpf.rpf_addr);
717 return 0;
718 }
719 } else {
720 /*
721 * We do not currently handle ZEBRA_IMPORT_CHECK_UPDATE
722 */
723 return 0;
724 }
725
726 pnc->last_update = pim_time_monotonic_usec();
727
728 if (nhr.nexthop_num) {
729 pnc->nexthop_num = 0; // Only increment for pim enabled rpf.
730
731 for (i = 0; i < nhr.nexthop_num; i++) {
732 nexthop = nexthop_from_zapi_nexthop(&nhr.nexthops[i]);
733 switch (nexthop->type) {
734 case NEXTHOP_TYPE_IFINDEX:
735 /*
736 * Connected route (i.e. no nexthop), use
737 * RPF address from nexthop cache (i.e.
738 * destination) as PIM nexthop.
739 */
740 #if PIM_IPV == 4
741 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
742 nexthop->gate.ipv4 =
743 pnc->rpf.rpf_addr.u.prefix4;
744 #else
745 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
746 nexthop->gate.ipv6 =
747 pnc->rpf.rpf_addr.u.prefix6;
748 #endif
749 break;
750 #if PIM_IPV == 4
751 /* RFC5549 IPv4-over-IPv6 nexthop handling:
752 * if we get an IPv6 nexthop in IPv4 PIM, hunt down a
753 * PIM neighbor and use that instead.
754 */
755 case NEXTHOP_TYPE_IPV6_IFINDEX: {
756 struct interface *ifp1 = NULL;
757 struct pim_neighbor *nbr = NULL;
758
759 ifp1 = if_lookup_by_index(nexthop->ifindex,
760 pim->vrf->vrf_id);
761
762 if (!ifp1)
763 nbr = NULL;
764 else
765 /* FIXME: should really use nbr's
766 * secondary address list here
767 */
768 nbr = pim_neighbor_find_if(ifp1);
769
770 /* Overwrite with Nbr address as NH addr */
771 if (nbr)
772 nexthop->gate.ipv4 = nbr->source_addr;
773 else
774 // Mark nexthop address to 0 until PIM
775 // Nbr is resolved.
776 nexthop->gate.ipv4 = PIMADDR_ANY;
777
778 break;
779 }
780 #else
781 case NEXTHOP_TYPE_IPV6_IFINDEX:
782 #endif
783 case NEXTHOP_TYPE_IPV6:
784 case NEXTHOP_TYPE_IPV4:
785 case NEXTHOP_TYPE_IPV4_IFINDEX:
786 case NEXTHOP_TYPE_BLACKHOLE:
787 /* nothing to do for the other nexthop types */
788 break;
789 }
790
791 ifp = if_lookup_by_index(nexthop->ifindex,
792 pim->vrf->vrf_id);
793 if (!ifp) {
794 if (PIM_DEBUG_PIM_NHT) {
795 char buf[NEXTHOP_STRLEN];
796 zlog_debug(
797 "%s: could not find interface for ifindex %d(%s) (addr %s)",
798 __func__, nexthop->ifindex,
799 pim->vrf->name,
800 nexthop2str(nexthop, buf,
801 sizeof(buf)));
802 }
803 nexthop_free(nexthop);
804 continue;
805 }
806
807 if (PIM_DEBUG_PIM_NHT) {
808 #if PIM_IPV == 4
809 pim_addr nhaddr = nexthop->gate.ipv4;
810 #else
811 pim_addr nhaddr = nexthop->gate.ipv6;
812 #endif
813 zlog_debug(
814 "%s: NHT addr %pFX(%s) %d-nhop via %pPA(%s) type %d distance:%u metric:%u ",
815 __func__, &match, pim->vrf->name, i + 1,
816 &nhaddr, ifp->name, nexthop->type,
817 nhr.distance, nhr.metric);
818 }
819
820 if (!ifp->info) {
821 /*
822 * Though Multicast is not enabled on this
823 * Interface store it in database otheriwse we
824 * may miss this update and this will not cause
825 * any issue, because while choosing the path we
826 * are ommitting the Interfaces which are not
827 * multicast enabled
828 */
829 if (PIM_DEBUG_PIM_NHT) {
830 char buf[NEXTHOP_STRLEN];
831
832 zlog_debug(
833 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, addr %s)",
834 __func__, ifp->name,
835 pim->vrf->name,
836 nexthop->ifindex,
837 nexthop2str(nexthop, buf,
838 sizeof(buf)));
839 }
840 }
841
842 if (nhlist_tail) {
843 nhlist_tail->next = nexthop;
844 nhlist_tail = nexthop;
845 } else {
846 nhlist_tail = nexthop;
847 nhlist_head = nexthop;
848 }
849 // Only keep track of nexthops which are PIM enabled.
850 pnc->nexthop_num++;
851 }
852 /* Reset existing pnc->nexthop before assigning new list */
853 nexthops_free(pnc->nexthop);
854 pnc->nexthop = nhlist_head;
855 if (pnc->nexthop_num) {
856 pnc->flags |= PIM_NEXTHOP_VALID;
857 pnc->distance = nhr.distance;
858 pnc->metric = nhr.metric;
859 }
860 } else {
861 pnc->flags &= ~PIM_NEXTHOP_VALID;
862 pnc->nexthop_num = nhr.nexthop_num;
863 nexthops_free(pnc->nexthop);
864 pnc->nexthop = NULL;
865 }
866 SET_FLAG(pnc->flags, PIM_NEXTHOP_ANSWER_RECEIVED);
867
868 if (PIM_DEBUG_PIM_NHT)
869 zlog_debug(
870 "%s: NHT Update for %pFX(%s) num_nh %d num_pim_nh %d vrf:%u up %ld rp %d",
871 __func__, &match, pim->vrf->name, nhr.nexthop_num,
872 pnc->nexthop_num, vrf_id, pnc->upstream_hash->count,
873 listcount(pnc->rp_list));
874
875 pim_rpf_set_refresh_time(pim);
876
877 if (listcount(pnc->rp_list))
878 pim_update_rp_nh(pim, pnc);
879 if (pnc->upstream_hash->count)
880 pim_update_upstream_nh(pim, pnc);
881
882 return 0;
883 }
884
885 int pim_ecmp_nexthop_lookup(struct pim_instance *pim,
886 struct pim_nexthop *nexthop, struct prefix *src,
887 struct prefix *grp, int neighbor_needed)
888 {
889 struct pim_nexthop_cache *pnc;
890 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
891 struct pim_neighbor *nbrs[MULTIPATH_NUM], *nbr = NULL;
892 struct pim_rpf rpf;
893 int num_ifindex;
894 struct interface *ifps[MULTIPATH_NUM], *ifp;
895 int first_ifindex;
896 int found = 0;
897 uint8_t i = 0;
898 uint32_t hash_val = 0, mod_val = 0;
899 uint32_t num_nbrs = 0;
900 pim_addr src_addr = pim_addr_from_prefix(src);
901
902 if (PIM_DEBUG_PIM_NHT_DETAIL)
903 zlog_debug("%s: Looking up: %pPA(%s), last lookup time: %lld",
904 __func__, &src_addr, pim->vrf->name,
905 nexthop->last_lookup_time);
906
907 rpf.rpf_addr = *src;
908
909 pnc = pim_nexthop_cache_find(pim, &rpf);
910 if (pnc) {
911 if (CHECK_FLAG(pnc->flags, PIM_NEXTHOP_ANSWER_RECEIVED))
912 return pim_ecmp_nexthop_search(pim, pnc, nexthop, src, grp,
913 neighbor_needed);
914 }
915
916 memset(nexthop_tab, 0,
917 sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM);
918 num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM,
919 src_addr, PIM_NEXTHOP_LOOKUP_MAX);
920 if (num_ifindex < 1) {
921 if (PIM_DEBUG_PIM_NHT)
922 zlog_warn(
923 "%s: could not find nexthop ifindex for address %pPA(%s)",
924 __func__, &src_addr, pim->vrf->name);
925 return 0;
926 }
927
928 memset(&nbrs, 0, sizeof(nbrs));
929 memset(&ifps, 0, sizeof(ifps));
930
931 /*
932 * Look up all interfaces and neighbors,
933 * store for later usage
934 */
935 for (i = 0; i < num_ifindex; i++) {
936 ifps[i] = if_lookup_by_index(nexthop_tab[i].ifindex,
937 pim->vrf->vrf_id);
938 if (ifps[i]) {
939 nbrs[i] = pim_neighbor_find(
940 ifps[i], nexthop_tab[i].nexthop_addr);
941 if (nbrs[i] ||
942 pim_if_connected_to_source(ifps[i], src_addr))
943 num_nbrs++;
944 }
945 }
946
947 // If PIM ECMP enable then choose ECMP path.
948 if (pim->ecmp_enable) {
949 uint32_t consider = num_ifindex;
950
951 if (neighbor_needed && num_nbrs < consider)
952 consider = num_nbrs;
953
954 if (consider == 0)
955 return 0;
956
957 hash_val = pim_compute_ecmp_hash(src, grp);
958 mod_val = hash_val % consider;
959 if (PIM_DEBUG_PIM_NHT_DETAIL)
960 zlog_debug("%s: hash_val %u mod_val %u", __func__,
961 hash_val, mod_val);
962 }
963
964 i = 0;
965 while (!found && (i < num_ifindex)) {
966 first_ifindex = nexthop_tab[i].ifindex;
967
968 ifp = ifps[i];
969 if (!ifp) {
970 if (PIM_DEBUG_PIM_NHT)
971 zlog_debug(
972 "%s %s: could not find interface for ifindex %d (address %pPA(%s))",
973 __FILE__, __func__, first_ifindex,
974 &src_addr, pim->vrf->name);
975 if (i == mod_val)
976 mod_val++;
977 i++;
978 continue;
979 }
980
981 if (!ifp->info) {
982 if (PIM_DEBUG_PIM_NHT)
983 zlog_debug(
984 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %pPA)",
985 __func__, ifp->name, pim->vrf->name,
986 first_ifindex, &src_addr);
987 if (i == mod_val)
988 mod_val++;
989 i++;
990 continue;
991 }
992 if (neighbor_needed &&
993 !pim_if_connected_to_source(ifp, src_addr)) {
994 nbr = nbrs[i];
995 if (PIM_DEBUG_PIM_NHT_DETAIL)
996 zlog_debug("ifp name: %s(%s), pim nbr: %p",
997 ifp->name, pim->vrf->name, nbr);
998 if (!nbr && !if_is_loopback(ifp)) {
999 if (i == mod_val)
1000 mod_val++;
1001 if (PIM_DEBUG_PIM_NHT)
1002 zlog_debug(
1003 "%s: NBR (%pPA) not found on input interface %s(%s) (RPF for source %pPA)",
1004 __func__,
1005 &nexthop_tab[i].nexthop_addr,
1006 ifp->name, pim->vrf->name,
1007 &src_addr);
1008 i++;
1009 continue;
1010 }
1011 }
1012
1013 if (i == mod_val) {
1014 if (PIM_DEBUG_PIM_NHT)
1015 zlog_debug(
1016 "%s: found nhop %pPA for addr %pPA interface %s(%s) metric %d dist %d",
1017 __func__, &nexthop_tab[i].nexthop_addr,
1018 &src_addr, ifp->name, pim->vrf->name,
1019 nexthop_tab[i].route_metric,
1020 nexthop_tab[i].protocol_distance);
1021 /* update nexthop data */
1022 nexthop->interface = ifp;
1023 nexthop->mrib_nexthop_addr =
1024 nexthop_tab[i].nexthop_addr;
1025 nexthop->mrib_metric_preference =
1026 nexthop_tab[i].protocol_distance;
1027 nexthop->mrib_route_metric =
1028 nexthop_tab[i].route_metric;
1029 nexthop->last_lookup = src_addr;
1030 nexthop->last_lookup_time = pim_time_monotonic_usec();
1031 nexthop->nbr = nbr;
1032 found = 1;
1033 }
1034 i++;
1035 }
1036
1037 if (found)
1038 return 1;
1039 else
1040 return 0;
1041 }
1042
1043 int pim_ecmp_fib_lookup_if_vif_index(struct pim_instance *pim,
1044 struct prefix *src, struct prefix *grp)
1045 {
1046 struct pim_nexthop nhop;
1047 int vif_index;
1048 ifindex_t ifindex;
1049 pim_addr src_addr;
1050
1051 if (PIM_DEBUG_PIM_NHT_DETAIL) {
1052 src_addr = pim_addr_from_prefix(src);
1053 }
1054
1055 memset(&nhop, 0, sizeof(nhop));
1056 if (!pim_ecmp_nexthop_lookup(pim, &nhop, src, grp, 1)) {
1057 if (PIM_DEBUG_PIM_NHT)
1058 zlog_debug(
1059 "%s: could not find nexthop ifindex for address %pPA(%s)",
1060 __func__, &src_addr, pim->vrf->name);
1061 return -1;
1062 }
1063
1064 ifindex = nhop.interface->ifindex;
1065 if (PIM_DEBUG_PIM_NHT)
1066 zlog_debug(
1067 "%s: found nexthop ifindex=%d (interface %s(%s)) for address %pPA",
1068 __func__, ifindex,
1069 ifindex2ifname(ifindex, pim->vrf->vrf_id),
1070 pim->vrf->name, &src_addr);
1071
1072 vif_index = pim_if_find_vifindex_by_ifindex(pim, ifindex);
1073
1074 if (vif_index < 0) {
1075 if (PIM_DEBUG_PIM_NHT) {
1076 zlog_debug(
1077 "%s: low vif_index=%d(%s) < 1 nexthop for address %pPA",
1078 __func__, vif_index, pim->vrf->name, &src_addr);
1079 }
1080 return -2;
1081 }
1082
1083 return vif_index;
1084 }