]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_nht.c
Merge pull request #870 from chiragshah6/mdev
[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 "pimd.h"
30 #include "pimd/pim_nht.h"
31 #include "log.h"
32 #include "pim_time.h"
33 #include "pim_oil.h"
34 #include "pim_ifchannel.h"
35 #include "pim_mroute.h"
36 #include "pim_zebra.h"
37 #include "pim_upstream.h"
38 #include "pim_join.h"
39 #include "pim_jp_agg.h"
40 #include "pim_zebra.h"
41 #include "pim_zlookup.h"
42
43 /**
44 * pim_sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
45 * command to Zebra.
46 */
47 void pim_sendmsg_zebra_rnh(struct pim_instance *pim, struct zclient *zclient,
48 struct pim_nexthop_cache *pnc, int command)
49 {
50 struct stream *s;
51 struct prefix *p;
52 int ret;
53
54 /* Check socket. */
55 if (!zclient || zclient->sock < 0)
56 return;
57
58 p = &(pnc->rpf.rpf_addr);
59 s = zclient->obuf;
60 stream_reset(s);
61 zclient_create_header(s, command, pim->vrf_id);
62 /* get update for all routes for a prefix */
63 stream_putc(s, 0);
64
65 stream_putw(s, PREFIX_FAMILY(p));
66 stream_putc(s, p->prefixlen);
67 switch (PREFIX_FAMILY(p)) {
68 case AF_INET:
69 stream_put_in_addr(s, &p->u.prefix4);
70 break;
71 case AF_INET6:
72 stream_put(s, &(p->u.prefix6), 16);
73 break;
74 default:
75 break;
76 }
77 stream_putw_at(s, 0, stream_get_endp(s));
78
79 ret = zclient_send_message(zclient);
80 if (ret < 0)
81 zlog_warn("sendmsg_nexthop: zclient_send_message() failed");
82
83
84 if (PIM_DEBUG_PIM_NHT) {
85 char buf[PREFIX2STR_BUFFER];
86 prefix2str(p, buf, sizeof(buf));
87 zlog_debug(
88 "%s: NHT %sregistered addr %s(%s) with Zebra ret:%d ",
89 __PRETTY_FUNCTION__,
90 (command == ZEBRA_NEXTHOP_REGISTER) ? " " : "de", buf,
91 pim->vrf->name, ret);
92 }
93
94 return;
95 }
96
97 struct pim_nexthop_cache *pim_nexthop_cache_find(struct pim_instance *pim,
98 struct pim_rpf *rpf)
99 {
100 struct pim_nexthop_cache *pnc = NULL;
101 struct pim_nexthop_cache lookup;
102
103 lookup.rpf.rpf_addr.family = rpf->rpf_addr.family;
104 lookup.rpf.rpf_addr.prefixlen = rpf->rpf_addr.prefixlen;
105 lookup.rpf.rpf_addr.u.prefix4.s_addr = rpf->rpf_addr.u.prefix4.s_addr;
106
107 pnc = hash_lookup(pim->rpf_hash, &lookup);
108
109 return pnc;
110 }
111
112 static struct pim_nexthop_cache *pim_nexthop_cache_add(struct pim_instance *pim,
113 struct pim_rpf *rpf_addr)
114 {
115 struct pim_nexthop_cache *pnc;
116 char hash_name[64];
117 char buf1[64];
118
119 pnc = XCALLOC(MTYPE_PIM_NEXTHOP_CACHE,
120 sizeof(struct pim_nexthop_cache));
121 if (!pnc) {
122 zlog_err("%s: NHT PIM XCALLOC failure ", __PRETTY_FUNCTION__);
123 return NULL;
124 }
125 pnc->rpf.rpf_addr.family = rpf_addr->rpf_addr.family;
126 pnc->rpf.rpf_addr.prefixlen = rpf_addr->rpf_addr.prefixlen;
127 pnc->rpf.rpf_addr.u.prefix4.s_addr =
128 rpf_addr->rpf_addr.u.prefix4.s_addr;
129
130 pnc = hash_get(pim->rpf_hash, pnc, hash_alloc_intern);
131
132 pnc->rp_list = list_new();
133 pnc->rp_list->cmp = pim_rp_list_cmp;
134
135 snprintf(hash_name, 64, "PNC %s(%s) Upstream Hash",
136 prefix2str(&pnc->rpf.rpf_addr, buf1, 64),
137 pim->vrf->name);
138 pnc->upstream_hash = hash_create_size(8192, pim_upstream_hash_key,
139 pim_upstream_equal,
140 hash_name);
141
142 return pnc;
143 }
144
145 /*
146 * pim_find_or_track_nexthop
147 *
148 * This API is used to Register an address with Zebra
149 *
150 * 1 -> Success
151 * 0 -> Failure
152 */
153 int pim_find_or_track_nexthop(struct pim_instance *pim, struct prefix *addr,
154 struct pim_upstream *up, struct rp_info *rp,
155 struct pim_nexthop_cache *out_pnc)
156 {
157 struct pim_nexthop_cache *pnc = NULL;
158 struct pim_rpf rpf;
159 struct listnode *ch_node = NULL;
160 struct zclient *zclient = NULL;
161
162 zclient = pim_zebra_zclient_get();
163 memset(&rpf, 0, sizeof(struct pim_rpf));
164 rpf.rpf_addr.family = addr->family;
165 rpf.rpf_addr.prefixlen = addr->prefixlen;
166 rpf.rpf_addr.u.prefix4 = addr->u.prefix4;
167
168 pnc = pim_nexthop_cache_find(pim, &rpf);
169 if (!pnc) {
170 pnc = pim_nexthop_cache_add(pim, &rpf);
171 if (!pnc) {
172 char rpf_str[PREFIX_STRLEN];
173 pim_addr_dump("<nht-pnc?>", addr, rpf_str,
174 sizeof(rpf_str));
175 zlog_warn("%s: pnc node allocation failed. addr %s ",
176 __PRETTY_FUNCTION__, rpf_str);
177 return 0;
178 }
179 pim_sendmsg_zebra_rnh(pim, zclient, pnc,
180 ZEBRA_NEXTHOP_REGISTER);
181 if (PIM_DEBUG_PIM_NHT) {
182 char buf[PREFIX2STR_BUFFER];
183 prefix2str(addr, buf, sizeof(buf));
184 zlog_debug(
185 "%s: NHT cache and zebra notification added for %s(%s)",
186 __PRETTY_FUNCTION__, buf, pim->vrf->name);
187 }
188 }
189
190 if (rp != NULL) {
191 ch_node = listnode_lookup(pnc->rp_list, rp);
192 if (ch_node == NULL)
193 listnode_add_sort(pnc->rp_list, rp);
194 }
195
196 if (up != NULL)
197 up = hash_get(pnc->upstream_hash, up, hash_alloc_intern);
198
199 if (pnc && CHECK_FLAG(pnc->flags, PIM_NEXTHOP_VALID)) {
200 memcpy(out_pnc, pnc, sizeof(struct pim_nexthop_cache));
201 return 1;
202 }
203
204 return 0;
205 }
206
207 void pim_delete_tracked_nexthop(struct pim_instance *pim, struct prefix *addr,
208 struct pim_upstream *up, struct rp_info *rp)
209 {
210 struct pim_nexthop_cache *pnc = NULL;
211 struct pim_nexthop_cache lookup;
212 struct zclient *zclient = NULL;
213
214 zclient = pim_zebra_zclient_get();
215
216 /* Remove from RPF hash if it is the last entry */
217 lookup.rpf.rpf_addr = *addr;
218 pnc = hash_lookup(pim->rpf_hash, &lookup);
219 if (pnc) {
220 if (rp)
221 listnode_delete(pnc->rp_list, rp);
222 if (up)
223 hash_release(pnc->upstream_hash, up);
224
225 if (PIM_DEBUG_PIM_NHT) {
226 char buf[PREFIX_STRLEN];
227 prefix2str(addr, buf, sizeof buf);
228 zlog_debug(
229 "%s: NHT %s(%s) rp_list count:%d upstream count:%ld",
230 __PRETTY_FUNCTION__, buf, pim->vrf->name,
231 pnc->rp_list->count, pnc->upstream_hash->count);
232 }
233
234 if (pnc->rp_list->count == 0
235 && pnc->upstream_hash->count == 0) {
236 pim_sendmsg_zebra_rnh(pim, zclient, pnc,
237 ZEBRA_NEXTHOP_UNREGISTER);
238
239 list_delete(pnc->rp_list);
240 hash_free(pnc->upstream_hash);
241
242 hash_release(pim->rpf_hash, pnc);
243 if (pnc->nexthop)
244 nexthops_free(pnc->nexthop);
245 XFREE(MTYPE_PIM_NEXTHOP_CACHE, pnc);
246 }
247 }
248 }
249
250 /* Update RP nexthop info based on Nexthop update received from Zebra.*/
251 static int pim_update_rp_nh(struct pim_instance *pim,
252 struct pim_nexthop_cache *pnc)
253 {
254 struct listnode *node = NULL;
255 struct rp_info *rp_info = NULL;
256 int ret = 0;
257
258 /*Traverse RP list and update each RP Nexthop info */
259 for (ALL_LIST_ELEMENTS_RO(pnc->rp_list, node, rp_info)) {
260 if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE)
261 continue;
262
263 // Compute PIM RPF using cached nexthop
264 ret = pim_ecmp_nexthop_search(
265 pim, pnc, &rp_info->rp.source_nexthop,
266 &rp_info->rp.rpf_addr, &rp_info->group, 1);
267 }
268
269 return !ret;
270 }
271
272 /* This API is used to traverse nexthop cache of RPF addr
273 of upstream entry whose IPv4 nexthop address is in
274 unresolved state and due to event like pim neighbor
275 UP event if it can be resolved.
276 */
277 void pim_resolve_upstream_nh(struct pim_instance *pim, struct prefix *nht_p)
278 {
279 struct nexthop *nh_node = NULL;
280 struct pim_nexthop_cache pnc;
281 struct pim_neighbor *nbr = NULL;
282
283 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
284 if (!pim_find_or_track_nexthop(pim, nht_p, NULL, NULL, &pnc))
285 return;
286
287 for (nh_node = pnc.nexthop; nh_node; nh_node = nh_node->next) {
288 if (nh_node->gate.ipv4.s_addr != 0)
289 continue;
290
291 struct interface *ifp1 =
292 if_lookup_by_index(nh_node->ifindex, pim->vrf_id);
293 nbr = pim_neighbor_find_if(ifp1);
294 if (!nbr)
295 continue;
296
297 nh_node->gate.ipv4 = nbr->source_addr;
298 if (PIM_DEBUG_PIM_NHT) {
299 char str[PREFIX_STRLEN];
300 char str1[INET_ADDRSTRLEN];
301 pim_inet4_dump("<nht_nbr?>", nbr->source_addr, str1,
302 sizeof(str1));
303 pim_addr_dump("<nht_addr?>", nht_p, str, sizeof(str));
304 zlog_debug(
305 "%s: addr %s new nexthop addr %s interface %s",
306 __PRETTY_FUNCTION__, str, str1, ifp1->name);
307 }
308 }
309 }
310
311 /* Update Upstream nexthop info based on Nexthop update received from Zebra.*/
312 static int pim_update_upstream_nh_helper(struct hash_backet *backet, void *arg)
313 {
314 struct pim_instance *pim = (struct pim_instance *)arg;
315 struct pim_upstream *up = (struct pim_upstream *)backet->data;
316 int vif_index = 0;
317
318 enum pim_rpf_result rpf_result;
319 struct pim_rpf old;
320
321 old.source_nexthop.interface = up->rpf.source_nexthop.interface;
322 rpf_result = pim_rpf_update(pim, up, &old, 0);
323 if (rpf_result == PIM_RPF_FAILURE)
324 return HASHWALK_CONTINUE;
325
326 /* update kernel multicast forwarding cache (MFC) */
327 if (up->channel_oil) {
328 ifindex_t ifindex = up->rpf.source_nexthop.interface->ifindex;
329
330 vif_index = pim_if_find_vifindex_by_ifindex(pim, ifindex);
331 /* Pass Current selected NH vif index to mroute download
332 */
333 if (vif_index)
334 pim_scan_individual_oil(up->channel_oil, vif_index);
335 else {
336 if (PIM_DEBUG_PIM_NHT)
337 zlog_debug(
338 "%s: NHT upstream %s channel_oil IIF %s vif_index is not valid",
339 __PRETTY_FUNCTION__, up->sg_str,
340 up->rpf.source_nexthop.interface->name);
341 }
342 }
343
344 if (rpf_result == PIM_RPF_CHANGED) {
345 struct pim_neighbor *nbr;
346
347 nbr = pim_neighbor_find(old.source_nexthop.interface,
348 old.rpf_addr.u.prefix4);
349 if (nbr)
350 pim_jp_agg_remove_group(nbr->upstream_jp_agg, up);
351
352 /*
353 * We have detected a case where we might need to rescan
354 * the inherited o_list so do it.
355 */
356 if (up->channel_oil
357 && up->channel_oil->oil_inherited_rescan) {
358 pim_upstream_inherited_olist_decide(pim, up);
359 up->channel_oil->oil_inherited_rescan = 0;
360 }
361
362 if (up->join_state == PIM_UPSTREAM_JOINED) {
363 /*
364 * If we come up real fast we can be here
365 * where the mroute has not been installed
366 * so install it.
367 */
368 if (up->channel_oil
369 && !up->channel_oil->installed)
370 pim_mroute_add(up->channel_oil,
371 __PRETTY_FUNCTION__);
372
373 /*
374 * RFC 4601: 4.5.7. Sending (S,G) Join/Prune Messages
375 *
376 * Transitions from Joined State
377 *
378 * RPF'(S,G) changes not due to an Assert
379 *
380 * The upstream (S,G) state machine remains in Joined
381 * state. Send Join(S,G) to the new upstream
382 * neighbor, which is the new value of RPF'(S,G).
383 * Send Prune(S,G) to the old upstream neighbor, which
384 * is the old value of RPF'(S,G). Set the Join
385 * Timer (JT) to expire after t_periodic seconds.
386 */
387 pim_jp_agg_switch_interface(&old, &up->rpf, up);
388
389 pim_upstream_join_timer_restart(up, &old);
390 } /* up->join_state == PIM_UPSTREAM_JOINED */
391
392 /*
393 * FIXME can join_desired actually be changed by
394 * pim_rpf_update() returning PIM_RPF_CHANGED ?
395 */
396 pim_upstream_update_join_desired(pim, up);
397
398 } /* PIM_RPF_CHANGED */
399
400 if (PIM_DEBUG_PIM_NHT) {
401 zlog_debug("%s: NHT upstream %s(%s) old ifp %s new ifp %s",
402 __PRETTY_FUNCTION__, up->sg_str, pim->vrf->name,
403 old.source_nexthop.interface->name,
404 up->rpf.source_nexthop.interface->name);
405 }
406
407 return HASHWALK_CONTINUE;
408 }
409
410 static int pim_update_upstream_nh(struct pim_instance *pim,
411 struct pim_nexthop_cache *pnc)
412 {
413 struct listnode *node, *ifnode;
414 struct interface *ifp;
415
416 hash_walk(pnc->upstream_hash, pim_update_upstream_nh_helper, pim);
417
418 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(pim->vrf_id), ifnode, ifp))
419 if (ifp->info) {
420 struct pim_interface *pim_ifp = ifp->info;
421 struct pim_iface_upstream_switch *us;
422
423 for (ALL_LIST_ELEMENTS_RO(pim_ifp->upstream_switch_list,
424 node, us)) {
425 struct pim_rpf rpf;
426 rpf.source_nexthop.interface = ifp;
427 rpf.rpf_addr.u.prefix4 = us->address;
428 pim_joinprune_send(&rpf, us->us);
429 pim_jp_agg_clear_group(us->us);
430 }
431 }
432
433 return 0;
434 }
435
436 uint32_t pim_compute_ecmp_hash(struct prefix *src, struct prefix *grp)
437 {
438 uint32_t hash_val;
439 uint32_t s = 0, g = 0;
440
441 if ((!src))
442 return 0;
443
444 switch (src->family) {
445 case AF_INET: {
446 s = src->u.prefix4.s_addr;
447 s = s == 0 ? 1 : s;
448 if (grp)
449 g = grp->u.prefix4.s_addr;
450 } break;
451 default:
452 break;
453 }
454
455 hash_val = jhash_2words(g, s, 101);
456 return hash_val;
457 }
458
459 int pim_ecmp_nexthop_search(struct pim_instance *pim,
460 struct pim_nexthop_cache *pnc,
461 struct pim_nexthop *nexthop, struct prefix *src,
462 struct prefix *grp, int neighbor_needed)
463 {
464 struct pim_neighbor *nbr = NULL;
465 struct nexthop *nh_node = NULL;
466 ifindex_t first_ifindex;
467 struct interface *ifp = NULL;
468 uint32_t hash_val = 0, mod_val = 0;
469 uint8_t nh_iter = 0, found = 0;
470
471 if (!pnc || !pnc->nexthop_num || !nexthop)
472 return 0;
473
474 // Current Nexthop is VALID, check to stay on the current path.
475 if (nexthop->interface && nexthop->interface->info
476 && nexthop->mrib_nexthop_addr.u.prefix4.s_addr
477 != PIM_NET_INADDR_ANY) {
478 /* User configured knob to explicitly switch
479 to new path is disabled or current path
480 metric is less than nexthop update.
481 */
482
483 if (qpim_ecmp_rebalance_enable == 0) {
484 uint8_t curr_route_valid = 0;
485 // Check if current nexthop is present in new updated
486 // Nexthop list.
487 // If the current nexthop is not valid, candidate to
488 // choose new Nexthop.
489 for (nh_node = pnc->nexthop; nh_node;
490 nh_node = nh_node->next) {
491 curr_route_valid = (nexthop->interface->ifindex
492 == nh_node->ifindex);
493 if (curr_route_valid)
494 break;
495 }
496
497 if (curr_route_valid
498 && !pim_if_connected_to_source(nexthop->interface,
499 src->u.prefix4)) {
500 nbr = pim_neighbor_find(
501 nexthop->interface,
502 nexthop->mrib_nexthop_addr.u.prefix4);
503 if (!nbr
504 && !if_is_loopback(nexthop->interface)) {
505 if (PIM_DEBUG_PIM_NHT)
506 zlog_debug(
507 "%s: current nexthop does not have nbr ",
508 __PRETTY_FUNCTION__);
509 } else {
510 if (PIM_DEBUG_PIM_NHT) {
511 char src_str[INET_ADDRSTRLEN];
512 pim_inet4_dump("<addr?>",
513 src->u.prefix4,
514 src_str,
515 sizeof(src_str));
516 char grp_str[INET_ADDRSTRLEN];
517 pim_inet4_dump("<addr?>",
518 grp->u.prefix4,
519 grp_str,
520 sizeof(grp_str));
521 zlog_debug(
522 "%s: (%s,%s)(%s) current nexthop %s is valid, skipping new path selection",
523 __PRETTY_FUNCTION__,
524 src_str, grp_str,
525 pim->vrf->name,
526 nexthop->interface->name);
527 }
528 return 0;
529 }
530 }
531 }
532 }
533 if (qpim_ecmp_enable) {
534 // PIM ECMP flag is enable then choose ECMP path.
535 hash_val = pim_compute_ecmp_hash(src, grp);
536 mod_val = hash_val % pnc->nexthop_num;
537 }
538
539 for (nh_node = pnc->nexthop; nh_node && (found == 0);
540 nh_node = nh_node->next) {
541 first_ifindex = nh_node->ifindex;
542 ifp = if_lookup_by_index(first_ifindex, pim->vrf_id);
543 if (!ifp) {
544 if (PIM_DEBUG_PIM_NHT) {
545 char addr_str[INET_ADDRSTRLEN];
546 pim_inet4_dump("<addr?>", src->u.prefix4,
547 addr_str, sizeof(addr_str));
548 zlog_debug(
549 "%s %s: could not find interface for ifindex %d (address %s(%s))",
550 __FILE__, __PRETTY_FUNCTION__,
551 first_ifindex, addr_str,
552 pim->vrf->name);
553 }
554 if (nh_iter == mod_val)
555 mod_val++; // Select nexthpath
556 nh_iter++;
557 continue;
558 }
559 if (!ifp->info) {
560 if (PIM_DEBUG_PIM_NHT) {
561 char addr_str[INET_ADDRSTRLEN];
562 pim_inet4_dump("<addr?>", src->u.prefix4,
563 addr_str, sizeof(addr_str));
564 zlog_debug(
565 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
566 __PRETTY_FUNCTION__, ifp->name,
567 pim->vrf->name, first_ifindex,
568 addr_str);
569 }
570 if (nh_iter == mod_val)
571 mod_val++; // Select nexthpath
572 nh_iter++;
573 continue;
574 }
575
576 if (neighbor_needed
577 && !pim_if_connected_to_source(ifp, src->u.prefix4)) {
578 nbr = pim_neighbor_find(ifp, nh_node->gate.ipv4);
579 if (!nbr && !if_is_loopback(ifp)) {
580 if (PIM_DEBUG_PIM_NHT)
581 zlog_debug(
582 "%s: pim nbr not found on input interface %s(%s)",
583 __PRETTY_FUNCTION__, ifp->name,
584 pim->vrf->name);
585 if (nh_iter == mod_val)
586 mod_val++; // Select nexthpath
587 nh_iter++;
588 continue;
589 }
590 }
591
592 if (nh_iter == mod_val) {
593 nexthop->interface = ifp;
594 nexthop->mrib_nexthop_addr.family = AF_INET;
595 nexthop->mrib_nexthop_addr.prefixlen = IPV4_MAX_BITLEN;
596 nexthop->mrib_nexthop_addr.u.prefix4 =
597 nh_node->gate.ipv4;
598 nexthop->mrib_metric_preference = pnc->distance;
599 nexthop->mrib_route_metric = pnc->metric;
600 nexthop->last_lookup = src->u.prefix4;
601 nexthop->last_lookup_time = pim_time_monotonic_usec();
602 nexthop->nbr = nbr;
603 found = 1;
604 if (PIM_DEBUG_PIM_NHT) {
605 char buf[INET_ADDRSTRLEN];
606 char buf2[INET_ADDRSTRLEN];
607 char buf3[INET_ADDRSTRLEN];
608 pim_inet4_dump("<src?>", src->u.prefix4, buf2,
609 sizeof(buf2));
610 pim_inet4_dump("<grp?>", grp->u.prefix4, buf3,
611 sizeof(buf3));
612 pim_inet4_dump(
613 "<rpf?>",
614 nexthop->mrib_nexthop_addr.u.prefix4,
615 buf, sizeof(buf));
616 zlog_debug(
617 "%s: (%s,%s)(%s) selected nhop interface %s addr %s mod_val %u iter %d ecmp %d",
618 __PRETTY_FUNCTION__, buf2, buf3,
619 pim->vrf->name, ifp->name, buf, mod_val,
620 nh_iter, qpim_ecmp_enable);
621 }
622 }
623 nh_iter++;
624 }
625
626 if (found)
627 return 1;
628 else
629 return 0;
630 }
631
632 /* This API is used to parse Registered address nexthop update coming from Zebra
633 */
634 int pim_parse_nexthop_update(int command, struct zclient *zclient,
635 zebra_size_t length, vrf_id_t vrf_id)
636 {
637 struct stream *s;
638 struct prefix p;
639 struct nexthop *nexthop;
640 struct nexthop *nhlist_head = NULL;
641 struct nexthop *nhlist_tail = NULL;
642 uint32_t metric, distance;
643 u_char nexthop_num = 0;
644 int i;
645 struct pim_rpf rpf;
646 struct pim_nexthop_cache *pnc = NULL;
647 struct pim_neighbor *nbr = NULL;
648 struct interface *ifp = NULL;
649 struct interface *ifp1 = NULL;
650 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
651 struct pim_instance *pim = vrf->info;
652
653 s = zclient->ibuf;
654 memset(&p, 0, sizeof(struct prefix));
655 p.family = stream_getw(s);
656 p.prefixlen = stream_getc(s);
657 switch (p.family) {
658 case AF_INET:
659 p.u.prefix4.s_addr = stream_get_ipv4(s);
660 break;
661 case AF_INET6:
662 stream_get(&p.u.prefix6, s, 16);
663 break;
664 default:
665 break;
666 }
667
668 if (command == ZEBRA_NEXTHOP_UPDATE) {
669 rpf.rpf_addr.family = p.family;
670 rpf.rpf_addr.prefixlen = p.prefixlen;
671 rpf.rpf_addr.u.prefix4.s_addr = p.u.prefix4.s_addr;
672 pnc = pim_nexthop_cache_find(pim, &rpf);
673 if (!pnc) {
674 if (PIM_DEBUG_PIM_NHT) {
675 char buf[PREFIX2STR_BUFFER];
676 prefix2str(&rpf.rpf_addr, buf, sizeof(buf));
677 zlog_debug(
678 "%s: Skipping NHT update, addr %s is not in local cached DB.",
679 __PRETTY_FUNCTION__, buf);
680 }
681 return 0;
682 }
683 } else {
684 /*
685 * We do not currently handle ZEBRA_IMPORT_CHECK_UPDATE
686 */
687 return 0;
688 }
689
690 pnc->last_update = pim_time_monotonic_usec();
691 distance = stream_getc(s);
692 metric = stream_getl(s);
693 nexthop_num = stream_getc(s);
694
695 if (nexthop_num) {
696 pnc->nexthop_num = 0; // Only increment for pim enabled rpf.
697
698 for (i = 0; i < nexthop_num; i++) {
699 nexthop = nexthop_new();
700 nexthop->type = stream_getc(s);
701 switch (nexthop->type) {
702 case NEXTHOP_TYPE_IPV4:
703 nexthop->gate.ipv4.s_addr = stream_get_ipv4(s);
704 nexthop->ifindex = stream_getl(s);
705 break;
706 case NEXTHOP_TYPE_IFINDEX:
707 nexthop->ifindex = stream_getl(s);
708 break;
709 case NEXTHOP_TYPE_IPV4_IFINDEX:
710 nexthop->gate.ipv4.s_addr = stream_get_ipv4(s);
711 nexthop->ifindex = stream_getl(s);
712 break;
713 case NEXTHOP_TYPE_IPV6:
714 stream_get(&nexthop->gate.ipv6, s, 16);
715 break;
716 case NEXTHOP_TYPE_IPV6_IFINDEX:
717 stream_get(&nexthop->gate.ipv6, s, 16);
718 nexthop->ifindex = stream_getl(s);
719 ifp1 = if_lookup_by_index(nexthop->ifindex,
720 pim->vrf_id);
721 nbr = pim_neighbor_find_if(ifp1);
722 /* Overwrite with Nbr address as NH addr */
723 if (nbr)
724 nexthop->gate.ipv4 = nbr->source_addr;
725 else {
726 // Mark nexthop address to 0 until PIM
727 // Nbr is resolved.
728 nexthop->gate.ipv4.s_addr =
729 PIM_NET_INADDR_ANY;
730 }
731
732 break;
733 default:
734 /* do nothing */
735 break;
736 }
737
738 ifp = if_lookup_by_index(nexthop->ifindex, pim->vrf_id);
739 if (!ifp) {
740 if (PIM_DEBUG_PIM_NHT) {
741 char buf[NEXTHOP_STRLEN];
742 zlog_debug(
743 "%s: could not find interface for ifindex %d(%s) (addr %s)",
744 __PRETTY_FUNCTION__,
745 nexthop->ifindex,
746 pim->vrf->name,
747 nexthop2str(nexthop, buf,
748 sizeof(buf)));
749 }
750 nexthop_free(nexthop);
751 continue;
752 }
753
754 if (PIM_DEBUG_PIM_NHT) {
755 char p_str[PREFIX2STR_BUFFER];
756 prefix2str(&p, p_str, sizeof(p_str));
757 zlog_debug(
758 "%s: NHT addr %s(%s) %d-nhop via %s(%s) type %d distance:%u metric:%u ",
759 __PRETTY_FUNCTION__, p_str,
760 pim->vrf->name, i + 1,
761 inet_ntoa(nexthop->gate.ipv4),
762 ifp->name, nexthop->type, distance,
763 metric);
764 }
765
766 if (!ifp->info) {
767 if (PIM_DEBUG_PIM_NHT) {
768 char buf[NEXTHOP_STRLEN];
769 zlog_debug(
770 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, addr %s)",
771 __PRETTY_FUNCTION__, ifp->name,
772 pim->vrf->name,
773 nexthop->ifindex,
774 nexthop2str(nexthop, buf,
775 sizeof(buf)));
776 }
777 nexthop_free(nexthop);
778 continue;
779 }
780
781 if (nhlist_tail) {
782 nhlist_tail->next = nexthop;
783 nhlist_tail = nexthop;
784 } else {
785 nhlist_tail = nexthop;
786 nhlist_head = nexthop;
787 }
788 // Only keep track of nexthops which are PIM enabled.
789 pnc->nexthop_num++;
790 }
791 /* Reset existing pnc->nexthop before assigning new list */
792 nexthops_free(pnc->nexthop);
793 pnc->nexthop = nhlist_head;
794 if (pnc->nexthop_num) {
795 pnc->flags |= PIM_NEXTHOP_VALID;
796 pnc->distance = distance;
797 pnc->metric = metric;
798 }
799 } else {
800 pnc->flags &= ~PIM_NEXTHOP_VALID;
801 pnc->nexthop_num = nexthop_num;
802 nexthops_free(pnc->nexthop);
803 pnc->nexthop = NULL;
804 }
805
806 if (PIM_DEBUG_PIM_NHT) {
807 char buf[PREFIX2STR_BUFFER];
808 prefix2str(&p, buf, sizeof(buf));
809 zlog_debug(
810 "%s: NHT Update for %s(%s) num_nh %d num_pim_nh %d vrf:%d up %ld rp %d",
811 __PRETTY_FUNCTION__, buf, pim->vrf->name, nexthop_num,
812 pnc->nexthop_num, vrf_id, pnc->upstream_hash->count,
813 listcount(pnc->rp_list));
814 }
815
816 pim_rpf_set_refresh_time();
817
818 if (listcount(pnc->rp_list))
819 pim_update_rp_nh(pim, pnc);
820 if (pnc->upstream_hash->count)
821 pim_update_upstream_nh(pim, pnc);
822
823 return 0;
824 }
825
826 int pim_ecmp_nexthop_lookup(struct pim_instance *pim,
827 struct pim_nexthop *nexthop, struct in_addr addr,
828 struct prefix *src, struct prefix *grp,
829 int neighbor_needed)
830 {
831 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
832 struct pim_neighbor *nbr = NULL;
833 int num_ifindex;
834 struct interface *ifp;
835 int first_ifindex;
836 int found = 0;
837 uint8_t i = 0;
838 uint32_t hash_val = 0, mod_val = 0;
839
840 if (PIM_DEBUG_PIM_NHT) {
841 char addr_str[INET_ADDRSTRLEN];
842 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
843 zlog_debug("%s: Looking up: %s(%s), last lookup time: %lld",
844 __PRETTY_FUNCTION__, addr_str, pim->vrf->name,
845 nexthop->last_lookup_time);
846 }
847
848 memset(nexthop_tab, 0,
849 sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM);
850 num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM,
851 addr, PIM_NEXTHOP_LOOKUP_MAX);
852 if (num_ifindex < 1) {
853 if (PIM_DEBUG_PIM_NHT) {
854 char addr_str[INET_ADDRSTRLEN];
855 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
856 zlog_warn(
857 "%s: could not find nexthop ifindex for address %s(%s)",
858 __PRETTY_FUNCTION__, addr_str,
859 pim->vrf->name);
860 }
861 return 0;
862 }
863
864 // If PIM ECMP enable then choose ECMP path.
865 if (qpim_ecmp_enable) {
866 hash_val = pim_compute_ecmp_hash(src, grp);
867 mod_val = hash_val % num_ifindex;
868 if (PIM_DEBUG_PIM_NHT_DETAIL)
869 zlog_debug("%s: hash_val %u mod_val %u",
870 __PRETTY_FUNCTION__, hash_val, mod_val);
871 }
872
873 while (!found && (i < num_ifindex)) {
874 first_ifindex = nexthop_tab[i].ifindex;
875
876 ifp = if_lookup_by_index(first_ifindex, pim->vrf_id);
877 if (!ifp) {
878 if (PIM_DEBUG_PIM_NHT) {
879 char addr_str[INET_ADDRSTRLEN];
880 pim_inet4_dump("<addr?>", addr, addr_str,
881 sizeof(addr_str));
882 zlog_debug(
883 "%s %s: could not find interface for ifindex %d (address %s(%s))",
884 __FILE__, __PRETTY_FUNCTION__,
885 first_ifindex, addr_str,
886 pim->vrf->name);
887 }
888 if (i == mod_val)
889 mod_val++;
890 i++;
891 continue;
892 }
893
894 if (!ifp->info) {
895 if (PIM_DEBUG_PIM_NHT) {
896 char addr_str[INET_ADDRSTRLEN];
897 pim_inet4_dump("<addr?>", addr, addr_str,
898 sizeof(addr_str));
899 zlog_debug(
900 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
901 __PRETTY_FUNCTION__, ifp->name,
902 pim->vrf->name, first_ifindex,
903 addr_str);
904 }
905 if (i == mod_val)
906 mod_val++;
907 i++;
908 continue;
909 }
910 if (neighbor_needed && !pim_if_connected_to_source(ifp, addr)) {
911 nbr = pim_neighbor_find(
912 ifp, nexthop_tab[i].nexthop_addr.u.prefix4);
913 if (PIM_DEBUG_PIM_NHT_DETAIL)
914 zlog_debug("ifp name: %s(%s), pim nbr: %p",
915 ifp->name, pim->vrf->name, nbr);
916 if (!nbr && !if_is_loopback(ifp)) {
917 if (i == mod_val)
918 mod_val++;
919 i++;
920 if (PIM_DEBUG_PIM_NHT) {
921 char addr_str[INET_ADDRSTRLEN];
922 pim_inet4_dump("<addr?>", addr,
923 addr_str,
924 sizeof(addr_str));
925 zlog_debug(
926 "%s: NBR not found on input interface %s(%s) (RPF for source %s)",
927 __PRETTY_FUNCTION__, ifp->name,
928 pim->vrf->name, addr_str);
929 }
930 continue;
931 }
932 }
933
934 if (i == mod_val) {
935 if (PIM_DEBUG_PIM_NHT) {
936 char nexthop_str[PREFIX_STRLEN];
937 char addr_str[INET_ADDRSTRLEN];
938 pim_addr_dump("<nexthop?>",
939 &nexthop_tab[i].nexthop_addr,
940 nexthop_str, sizeof(nexthop_str));
941 pim_inet4_dump("<addr?>", addr, addr_str,
942 sizeof(addr_str));
943 zlog_debug(
944 "%s: found nhop %s for addr %s interface %s(%s) metric %d dist %d",
945 __PRETTY_FUNCTION__, nexthop_str,
946 addr_str, ifp->name, pim->vrf->name,
947 nexthop_tab[i].route_metric,
948 nexthop_tab[i].protocol_distance);
949 }
950 /* update nextop data */
951 nexthop->interface = ifp;
952 nexthop->mrib_nexthop_addr =
953 nexthop_tab[i].nexthop_addr;
954 nexthop->mrib_metric_preference =
955 nexthop_tab[i].protocol_distance;
956 nexthop->mrib_route_metric =
957 nexthop_tab[i].route_metric;
958 nexthop->last_lookup = addr;
959 nexthop->last_lookup_time = pim_time_monotonic_usec();
960 nexthop->nbr = nbr;
961 found = 1;
962 }
963 i++;
964 }
965
966 if (found)
967 return 1;
968 else
969 return 0;
970 }
971
972 int pim_ecmp_fib_lookup_if_vif_index(struct pim_instance *pim,
973 struct in_addr addr, struct prefix *src,
974 struct prefix *grp)
975 {
976 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
977 int num_ifindex;
978 int vif_index;
979 ifindex_t first_ifindex;
980 uint32_t hash_val = 0, mod_val = 0;
981
982 memset(nexthop_tab, 0,
983 sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM);
984 num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM,
985 addr, PIM_NEXTHOP_LOOKUP_MAX);
986 if (num_ifindex < 1) {
987 if (PIM_DEBUG_PIM_NHT) {
988 char addr_str[INET_ADDRSTRLEN];
989 pim_inet4_dump("<addr?>", addr, addr_str,
990 sizeof(addr_str));
991 zlog_debug(
992 "%s: could not find nexthop ifindex for address %s(%s)",
993 __PRETTY_FUNCTION__, addr_str, pim->vrf->name);
994 }
995 return -1;
996 }
997
998 // If PIM ECMP enable then choose ECMP path.
999 if (qpim_ecmp_enable) {
1000 hash_val = pim_compute_ecmp_hash(src, grp);
1001 mod_val = hash_val % num_ifindex;
1002 if (PIM_DEBUG_PIM_NHT_DETAIL)
1003 zlog_debug("%s: hash_val %u mod_val %u",
1004 __PRETTY_FUNCTION__, hash_val, mod_val);
1005 }
1006
1007 first_ifindex = nexthop_tab[mod_val].ifindex;
1008
1009 if (PIM_DEBUG_PIM_NHT) {
1010 char addr_str[INET_ADDRSTRLEN];
1011 pim_inet4_dump("<ifaddr?>", addr, addr_str, sizeof(addr_str));
1012 zlog_debug(
1013 "%s: found nexthop ifindex=%d (interface %s(%s)) for address %s",
1014 __PRETTY_FUNCTION__, first_ifindex,
1015 ifindex2ifname(first_ifindex, pim->vrf_id),
1016 pim->vrf->name, addr_str);
1017 }
1018
1019 vif_index = pim_if_find_vifindex_by_ifindex(pim, first_ifindex);
1020
1021 if (vif_index < 0) {
1022 if (PIM_DEBUG_PIM_NHT) {
1023 char addr_str[INET_ADDRSTRLEN];
1024 pim_inet4_dump("<addr?>", addr, addr_str,
1025 sizeof(addr_str));
1026 zlog_debug(
1027 "%s: low vif_index=%d(%s) < 1 nexthop for address %s",
1028 __PRETTY_FUNCTION__, vif_index, pim->vrf->name,
1029 addr_str);
1030 }
1031 return -2;
1032 }
1033
1034 return vif_index;
1035 }