]>
Commit | Line | Data |
---|---|---|
1bc98276 CS |
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 | * | |
896014f4 DL |
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 | |
1bc98276 CS |
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" | |
633988a7 | 41 | #include "pim_zlookup.h" |
640b8d93 | 42 | #include "pim_rp.h" |
1bc98276 CS |
43 | |
44 | /** | |
45 | * pim_sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister | |
46 | * command to Zebra. | |
47 | */ | |
64c86530 | 48 | void pim_sendmsg_zebra_rnh(struct pim_instance *pim, struct zclient *zclient, |
d62a17ae | 49 | struct pim_nexthop_cache *pnc, int command) |
1bc98276 | 50 | { |
d62a17ae | 51 | struct prefix *p; |
52 | int ret; | |
53 | ||
d62a17ae | 54 | p = &(pnc->rpf.rpf_addr); |
3c192540 | 55 | ret = zclient_send_rnh(zclient, command, p, false, pim->vrf_id); |
d62a17ae | 56 | if (ret < 0) |
57 | zlog_warn("sendmsg_nexthop: zclient_send_message() failed"); | |
58 | ||
5cef40fc | 59 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 60 | char buf[PREFIX2STR_BUFFER]; |
61 | prefix2str(p, buf, sizeof(buf)); | |
5cef40fc DS |
62 | zlog_debug( |
63 | "%s: NHT %sregistered addr %s(%s) with Zebra ret:%d ", | |
15569c58 | 64 | __func__, |
5cef40fc DS |
65 | (command == ZEBRA_NEXTHOP_REGISTER) ? " " : "de", buf, |
66 | pim->vrf->name, ret); | |
d62a17ae | 67 | } |
68 | ||
69 | return; | |
1bc98276 CS |
70 | } |
71 | ||
d0a4f55d DS |
72 | struct pim_nexthop_cache *pim_nexthop_cache_find(struct pim_instance *pim, |
73 | struct pim_rpf *rpf) | |
1bc98276 | 74 | { |
d62a17ae | 75 | struct pim_nexthop_cache *pnc = NULL; |
76 | struct pim_nexthop_cache lookup; | |
1bc98276 | 77 | |
d62a17ae | 78 | lookup.rpf.rpf_addr.family = rpf->rpf_addr.family; |
79 | lookup.rpf.rpf_addr.prefixlen = rpf->rpf_addr.prefixlen; | |
80 | lookup.rpf.rpf_addr.u.prefix4.s_addr = rpf->rpf_addr.u.prefix4.s_addr; | |
1bc98276 | 81 | |
d0a4f55d | 82 | pnc = hash_lookup(pim->rpf_hash, &lookup); |
1bc98276 | 83 | |
d62a17ae | 84 | return pnc; |
1bc98276 CS |
85 | } |
86 | ||
5cef40fc DS |
87 | static struct pim_nexthop_cache *pim_nexthop_cache_add(struct pim_instance *pim, |
88 | struct pim_rpf *rpf_addr) | |
1bc98276 | 89 | { |
d62a17ae | 90 | struct pim_nexthop_cache *pnc; |
9fb302f4 DS |
91 | char hash_name[64]; |
92 | char buf1[64]; | |
d62a17ae | 93 | |
94 | pnc = XCALLOC(MTYPE_PIM_NEXTHOP_CACHE, | |
95 | sizeof(struct pim_nexthop_cache)); | |
d62a17ae | 96 | pnc->rpf.rpf_addr.family = rpf_addr->rpf_addr.family; |
97 | pnc->rpf.rpf_addr.prefixlen = rpf_addr->rpf_addr.prefixlen; | |
98 | pnc->rpf.rpf_addr.u.prefix4.s_addr = | |
99 | rpf_addr->rpf_addr.u.prefix4.s_addr; | |
100 | ||
d0a4f55d | 101 | pnc = hash_get(pim->rpf_hash, pnc, hash_alloc_intern); |
d62a17ae | 102 | |
103 | pnc->rp_list = list_new(); | |
104 | pnc->rp_list->cmp = pim_rp_list_cmp; | |
105 | ||
772270f3 | 106 | snprintf(hash_name, sizeof(hash_name), "PNC %s(%s) Upstream Hash", |
996c9314 | 107 | prefix2str(&pnc->rpf.rpf_addr, buf1, 64), pim->vrf->name); |
7c591950 | 108 | pnc->upstream_hash = hash_create_size(8192, pim_upstream_hash_key, |
996c9314 | 109 | pim_upstream_equal, hash_name); |
d62a17ae | 110 | |
d62a17ae | 111 | return pnc; |
1bc98276 CS |
112 | } |
113 | ||
cb9c7c50 DS |
114 | /* |
115 | * pim_find_or_track_nexthop | |
116 | * | |
117 | * This API is used to Register an address with Zebra | |
118 | * | |
119 | * 1 -> Success | |
120 | * 0 -> Failure | |
121 | */ | |
25bdac42 DS |
122 | int pim_find_or_track_nexthop(struct pim_instance *pim, struct prefix *addr, |
123 | struct pim_upstream *up, struct rp_info *rp, | |
4533b847 | 124 | bool bsr_track_needed, |
d62a17ae | 125 | struct pim_nexthop_cache *out_pnc) |
1bc98276 | 126 | { |
d62a17ae | 127 | struct pim_nexthop_cache *pnc = NULL; |
128 | struct pim_rpf rpf; | |
129 | struct listnode *ch_node = NULL; | |
130 | struct zclient *zclient = NULL; | |
131 | ||
132 | zclient = pim_zebra_zclient_get(); | |
133 | memset(&rpf, 0, sizeof(struct pim_rpf)); | |
134 | rpf.rpf_addr.family = addr->family; | |
135 | rpf.rpf_addr.prefixlen = addr->prefixlen; | |
136 | rpf.rpf_addr.u.prefix4 = addr->u.prefix4; | |
137 | ||
25bdac42 | 138 | pnc = pim_nexthop_cache_find(pim, &rpf); |
d62a17ae | 139 | if (!pnc) { |
25bdac42 | 140 | pnc = pim_nexthop_cache_add(pim, &rpf); |
5cef40fc DS |
141 | pim_sendmsg_zebra_rnh(pim, zclient, pnc, |
142 | ZEBRA_NEXTHOP_REGISTER); | |
143 | if (PIM_DEBUG_PIM_NHT) { | |
144 | char buf[PREFIX2STR_BUFFER]; | |
145 | prefix2str(addr, buf, sizeof(buf)); | |
146 | zlog_debug( | |
147 | "%s: NHT cache and zebra notification added for %s(%s)", | |
15569c58 | 148 | __func__, buf, pim->vrf->name); |
5cef40fc | 149 | } |
d62a17ae | 150 | } |
151 | ||
152 | if (rp != NULL) { | |
153 | ch_node = listnode_lookup(pnc->rp_list, rp); | |
5cef40fc | 154 | if (ch_node == NULL) |
d62a17ae | 155 | listnode_add_sort(pnc->rp_list, rp); |
d62a17ae | 156 | } |
157 | ||
7c591950 | 158 | if (up != NULL) |
c31a793b | 159 | hash_get(pnc->upstream_hash, up, hash_alloc_intern); |
d62a17ae | 160 | |
4533b847 | 161 | if (bsr_track_needed) |
162 | pnc->bsr_tracking = true; | |
163 | ||
2b57b948 | 164 | if (CHECK_FLAG(pnc->flags, PIM_NEXTHOP_VALID)) { |
43763b11 DS |
165 | if (out_pnc) |
166 | memcpy(out_pnc, pnc, sizeof(struct pim_nexthop_cache)); | |
d62a17ae | 167 | return 1; |
168 | } | |
169 | ||
170 | return 0; | |
1bc98276 CS |
171 | } |
172 | ||
d0a4f55d | 173 | void pim_delete_tracked_nexthop(struct pim_instance *pim, struct prefix *addr, |
4533b847 | 174 | struct pim_upstream *up, struct rp_info *rp, |
175 | bool del_bsr_tracking) | |
1bc98276 | 176 | { |
d62a17ae | 177 | struct pim_nexthop_cache *pnc = NULL; |
178 | struct pim_nexthop_cache lookup; | |
179 | struct zclient *zclient = NULL; | |
246445a3 | 180 | struct pim_upstream *upstream = NULL; |
d62a17ae | 181 | |
182 | zclient = pim_zebra_zclient_get(); | |
183 | ||
184 | /* Remove from RPF hash if it is the last entry */ | |
185 | lookup.rpf.rpf_addr = *addr; | |
d0a4f55d | 186 | pnc = hash_lookup(pim->rpf_hash, &lookup); |
d62a17ae | 187 | if (pnc) { |
246445a3 SP |
188 | if (rp) { |
189 | /* Release the (*, G)upstream from pnc->upstream_hash, | |
190 | * whose Group belongs to the RP getting deleted | |
191 | */ | |
dd3364cb DS |
192 | frr_each (rb_pim_upstream, &pim->upstream_head, |
193 | upstream) { | |
246445a3 SP |
194 | struct prefix grp; |
195 | struct rp_info *trp_info; | |
196 | ||
197 | if (upstream->sg.src.s_addr != INADDR_ANY) | |
198 | continue; | |
199 | ||
200 | grp.family = AF_INET; | |
201 | grp.prefixlen = IPV4_MAX_BITLEN; | |
202 | grp.u.prefix4 = upstream->sg.grp; | |
203 | ||
204 | trp_info = pim_rp_find_match_group(pim, &grp); | |
205 | if (trp_info == rp) | |
206 | hash_release(pnc->upstream_hash, | |
207 | upstream); | |
208 | } | |
d62a17ae | 209 | listnode_delete(pnc->rp_list, rp); |
246445a3 SP |
210 | } |
211 | ||
d62a17ae | 212 | if (up) |
7c591950 | 213 | hash_release(pnc->upstream_hash, up); |
d62a17ae | 214 | |
4533b847 | 215 | if (del_bsr_tracking) |
216 | pnc->bsr_tracking = false; | |
217 | ||
5cef40fc DS |
218 | if (PIM_DEBUG_PIM_NHT) { |
219 | char buf[PREFIX_STRLEN]; | |
0d6f7fd6 | 220 | prefix2str(addr, buf, sizeof(buf)); |
d62a17ae | 221 | zlog_debug( |
7c591950 | 222 | "%s: NHT %s(%s) rp_list count:%d upstream count:%ld", |
15569c58 | 223 | __func__, buf, pim->vrf->name, |
7c591950 | 224 | pnc->rp_list->count, pnc->upstream_hash->count); |
5cef40fc | 225 | } |
d62a17ae | 226 | |
227 | if (pnc->rp_list->count == 0 | |
4533b847 | 228 | && pnc->upstream_hash->count == 0 |
229 | && pnc->bsr_tracking == false) { | |
64c86530 | 230 | pim_sendmsg_zebra_rnh(pim, zclient, pnc, |
d62a17ae | 231 | ZEBRA_NEXTHOP_UNREGISTER); |
232 | ||
6a154c88 | 233 | list_delete(&pnc->rp_list); |
7c591950 | 234 | hash_free(pnc->upstream_hash); |
d62a17ae | 235 | |
d0a4f55d | 236 | hash_release(pim->rpf_hash, pnc); |
d62a17ae | 237 | if (pnc->nexthop) |
238 | nexthops_free(pnc->nexthop); | |
239 | XFREE(MTYPE_PIM_NEXTHOP_CACHE, pnc); | |
240 | } | |
241 | } | |
1bc98276 CS |
242 | } |
243 | ||
4b936634 | 244 | /* Given a source address and a neighbor address, check if the neighbor is one |
245 | * of the next hop to reach the source. search from zebra route database | |
246 | */ | |
247 | bool pim_nexthop_match(struct pim_instance *pim, struct in_addr addr, | |
248 | struct in_addr ip_src) | |
249 | { | |
250 | struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM]; | |
251 | int i = 0; | |
252 | ifindex_t first_ifindex = 0; | |
253 | struct interface *ifp = NULL; | |
254 | struct pim_neighbor *nbr = NULL; | |
255 | int num_ifindex; | |
256 | ||
257 | if (addr.s_addr == INADDR_NONE) | |
f06c4576 | 258 | return false; |
4b936634 | 259 | |
260 | memset(nexthop_tab, 0, | |
261 | sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM); | |
262 | num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM, | |
263 | addr, PIM_NEXTHOP_LOOKUP_MAX); | |
264 | if (num_ifindex < 1) { | |
265 | char addr_str[INET_ADDRSTRLEN]; | |
266 | ||
267 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
268 | zlog_warn( | |
269 | "%s %s: could not find nexthop ifindex for address %s", | |
15569c58 | 270 | __FILE__, __func__, addr_str); |
f06c4576 | 271 | return false; |
4b936634 | 272 | } |
273 | ||
274 | while (i < num_ifindex) { | |
275 | first_ifindex = nexthop_tab[i].ifindex; | |
276 | ||
277 | ifp = if_lookup_by_index(first_ifindex, pim->vrf_id); | |
278 | if (!ifp) { | |
279 | if (PIM_DEBUG_ZEBRA) { | |
280 | char addr_str[INET_ADDRSTRLEN]; | |
281 | ||
282 | pim_inet4_dump("<addr?>", addr, addr_str, | |
283 | sizeof(addr_str)); | |
284 | zlog_debug( | |
285 | "%s %s: could not find interface for ifindex %d (address %s)", | |
15569c58 DA |
286 | __FILE__, __func__, first_ifindex, |
287 | addr_str); | |
4b936634 | 288 | } |
289 | i++; | |
290 | continue; | |
291 | } | |
292 | ||
293 | if (!ifp->info) { | |
294 | if (PIM_DEBUG_ZEBRA) { | |
295 | char addr_str[INET_ADDRSTRLEN]; | |
296 | ||
297 | pim_inet4_dump("<addr?>", addr, addr_str, | |
298 | sizeof(addr_str)); | |
299 | zlog_debug( | |
300 | "%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)", | |
15569c58 DA |
301 | __func__, ifp->name, first_ifindex, |
302 | addr_str); | |
4b936634 | 303 | } |
304 | i++; | |
305 | continue; | |
306 | } | |
307 | ||
308 | if (!pim_if_connected_to_source(ifp, addr)) { | |
309 | nbr = pim_neighbor_find( | |
310 | ifp, nexthop_tab[i].nexthop_addr.u.prefix4); | |
311 | if (PIM_DEBUG_PIM_TRACE_DETAIL) | |
312 | zlog_debug("ifp name: %s, pim nbr: %p", | |
313 | ifp->name, nbr); | |
314 | if (!nbr && !if_is_loopback(ifp)) { | |
315 | i++; | |
316 | continue; | |
317 | } | |
318 | } | |
319 | ||
320 | if (nexthop_tab[i].nexthop_addr.u.prefix4.s_addr | |
321 | == ip_src.s_addr) | |
f06c4576 | 322 | return true; |
4b936634 | 323 | |
324 | i++; | |
325 | } | |
326 | ||
f06c4576 | 327 | return false; |
4b936634 | 328 | } |
329 | ||
330 | /* Given a source address and a neighbor address, check if the neighbor is one | |
331 | * of the next hop to reach the source. search from pim next hop cache | |
332 | */ | |
333 | bool pim_nexthop_match_nht_cache(struct pim_instance *pim, struct in_addr addr, | |
334 | struct in_addr ip_src) | |
335 | { | |
336 | struct pim_rpf rpf; | |
337 | ifindex_t first_ifindex; | |
338 | struct interface *ifp = NULL; | |
339 | uint8_t nh_iter = 0; | |
340 | struct pim_neighbor *nbr = NULL; | |
341 | struct nexthop *nh_node = NULL; | |
342 | struct pim_nexthop_cache *pnc = NULL; | |
343 | ||
344 | memset(&rpf, 0, sizeof(struct pim_rpf)); | |
345 | rpf.rpf_addr.family = AF_INET; | |
346 | rpf.rpf_addr.prefixlen = IPV4_MAX_BITLEN; | |
347 | rpf.rpf_addr.u.prefix4 = addr; | |
348 | ||
349 | pnc = pim_nexthop_cache_find(pim, &rpf); | |
350 | if (!pnc || !pnc->nexthop_num) | |
f06c4576 | 351 | return false; |
4b936634 | 352 | |
353 | for (nh_node = pnc->nexthop; nh_node; nh_node = nh_node->next) { | |
354 | first_ifindex = nh_node->ifindex; | |
355 | ifp = if_lookup_by_index(first_ifindex, pim->vrf_id); | |
356 | if (!ifp) { | |
357 | if (PIM_DEBUG_PIM_NHT) { | |
358 | char addr_str[INET_ADDRSTRLEN]; | |
359 | ||
360 | pim_inet4_dump("<addr?>", addr, addr_str, | |
361 | sizeof(addr_str)); | |
362 | zlog_debug( | |
363 | "%s %s: could not find interface for ifindex %d (address %s(%s))", | |
15569c58 DA |
364 | __FILE__, __func__, first_ifindex, |
365 | addr_str, pim->vrf->name); | |
4b936634 | 366 | } |
367 | nh_iter++; | |
368 | continue; | |
369 | } | |
370 | if (!ifp->info) { | |
371 | if (PIM_DEBUG_PIM_NHT) { | |
372 | char addr_str[INET_ADDRSTRLEN]; | |
373 | ||
374 | pim_inet4_dump("<addr?>", addr, addr_str, | |
375 | sizeof(addr_str)); | |
376 | zlog_debug( | |
377 | "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)", | |
15569c58 DA |
378 | __func__, ifp->name, pim->vrf->name, |
379 | first_ifindex, addr_str); | |
4b936634 | 380 | } |
381 | nh_iter++; | |
382 | continue; | |
383 | } | |
384 | ||
385 | if (!pim_if_connected_to_source(ifp, addr)) { | |
386 | nbr = pim_neighbor_find(ifp, nh_node->gate.ipv4); | |
387 | if (!nbr && !if_is_loopback(ifp)) { | |
388 | if (PIM_DEBUG_PIM_NHT) | |
389 | zlog_debug( | |
390 | "%s: pim nbr not found on input interface %s(%s)", | |
15569c58 | 391 | __func__, ifp->name, |
4b936634 | 392 | pim->vrf->name); |
393 | nh_iter++; | |
394 | continue; | |
395 | } | |
396 | } | |
397 | ||
398 | if (nh_node->gate.ipv4.s_addr == ip_src.s_addr) | |
f06c4576 | 399 | return true; |
4b936634 | 400 | } |
401 | ||
f06c4576 | 402 | return false; |
4b936634 | 403 | } |
404 | ||
640b8d93 SP |
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.u.prefix4.s_addr = | |
409 | PIM_NET_INADDR_ANY; | |
410 | rp_info->rp.source_nexthop.mrib_metric_preference = | |
411 | router->infinite_assert_metric.metric_preference; | |
412 | rp_info->rp.source_nexthop.mrib_route_metric = | |
413 | router->infinite_assert_metric.route_metric; | |
414 | } | |
415 | ||
1bc98276 | 416 | /* Update RP nexthop info based on Nexthop update received from Zebra.*/ |
a7f95c76 DS |
417 | static void pim_update_rp_nh(struct pim_instance *pim, |
418 | struct pim_nexthop_cache *pnc) | |
1bc98276 | 419 | { |
d62a17ae | 420 | struct listnode *node = NULL; |
421 | struct rp_info *rp_info = NULL; | |
d62a17ae | 422 | |
423 | /*Traverse RP list and update each RP Nexthop info */ | |
424 | for (ALL_LIST_ELEMENTS_RO(pnc->rp_list, node, rp_info)) { | |
425 | if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE) | |
426 | continue; | |
427 | ||
428 | // Compute PIM RPF using cached nexthop | |
43763b11 DS |
429 | if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, |
430 | &rp_info->rp.rpf_addr, | |
431 | &rp_info->group, 1)) | |
640b8d93 | 432 | pim_rp_nexthop_del(rp_info); |
d62a17ae | 433 | } |
1bc98276 CS |
434 | } |
435 | ||
436 | /* Update Upstream nexthop info based on Nexthop update received from Zebra.*/ | |
e3b78da8 | 437 | static int pim_update_upstream_nh_helper(struct hash_bucket *bucket, void *arg) |
1bc98276 | 438 | { |
7c591950 | 439 | struct pim_instance *pim = (struct pim_instance *)arg; |
e3b78da8 | 440 | struct pim_upstream *up = (struct pim_upstream *)bucket->data; |
d62a17ae | 441 | |
7c591950 DS |
442 | enum pim_rpf_result rpf_result; |
443 | struct pim_rpf old; | |
d62a17ae | 444 | |
7c591950 | 445 | old.source_nexthop.interface = up->rpf.source_nexthop.interface; |
8c55c132 | 446 | rpf_result = pim_rpf_update(pim, up, &old, __func__); |
d62a17ae | 447 | |
b36576e4 AK |
448 | /* update kernel multicast forwarding cache (MFC); if the |
449 | * RPF nbr is now unreachable the MFC has already been updated | |
450 | * by pim_rpf_clear | |
451 | */ | |
452 | if (rpf_result != PIM_RPF_FAILURE) | |
453 | pim_upstream_mroute_iif_update(up->channel_oil, __func__); | |
7c591950 | 454 | |
b36576e4 AK |
455 | if (rpf_result == PIM_RPF_CHANGED || |
456 | (rpf_result == PIM_RPF_FAILURE && old.source_nexthop.interface)) | |
cc67ccf9 | 457 | pim_zebra_upstream_rpf_changed(pim, up, &old); |
d62a17ae | 458 | |
7c591950 DS |
459 | |
460 | if (PIM_DEBUG_PIM_NHT) { | |
15569c58 DA |
461 | zlog_debug( |
462 | "%s: NHT upstream %s(%s) old ifp %s new ifp %s", | |
463 | __func__, up->sg_str, pim->vrf->name, | |
464 | old.source_nexthop.interface ? old.source_nexthop | |
465 | .interface->name | |
466 | : "Unknown", | |
467 | up->rpf.source_nexthop.interface ? up->rpf.source_nexthop | |
468 | .interface->name | |
469 | : "Unknown"); | |
7c591950 DS |
470 | } |
471 | ||
472 | return HASHWALK_CONTINUE; | |
473 | } | |
474 | ||
475 | static int pim_update_upstream_nh(struct pim_instance *pim, | |
476 | struct pim_nexthop_cache *pnc) | |
477 | { | |
7c591950 | 478 | hash_walk(pnc->upstream_hash, pim_update_upstream_nh_helper, pim); |
d62a17ae | 479 | |
74389231 | 480 | pim_zebra_update_all_interfaces(pim); |
d62a17ae | 481 | |
482 | return 0; | |
1bc98276 CS |
483 | } |
484 | ||
d62a17ae | 485 | uint32_t pim_compute_ecmp_hash(struct prefix *src, struct prefix *grp) |
633988a7 | 486 | { |
d62a17ae | 487 | uint32_t hash_val; |
488 | uint32_t s = 0, g = 0; | |
489 | ||
490 | if ((!src)) | |
491 | return 0; | |
492 | ||
493 | switch (src->family) { | |
494 | case AF_INET: { | |
495 | s = src->u.prefix4.s_addr; | |
496 | s = s == 0 ? 1 : s; | |
497 | if (grp) | |
498 | g = grp->u.prefix4.s_addr; | |
499 | } break; | |
500 | default: | |
501 | break; | |
502 | } | |
503 | ||
504 | hash_val = jhash_2words(g, s, 101); | |
d62a17ae | 505 | return hash_val; |
633988a7 CS |
506 | } |
507 | ||
43763b11 DS |
508 | static int pim_ecmp_nexthop_search(struct pim_instance *pim, |
509 | struct pim_nexthop_cache *pnc, | |
510 | struct pim_nexthop *nexthop, | |
511 | struct prefix *src, struct prefix *grp, | |
512 | int neighbor_needed) | |
633988a7 | 513 | { |
c517178d | 514 | struct pim_neighbor *nbrs[MULTIPATH_NUM], *nbr = NULL; |
19967127 | 515 | struct interface *ifps[MULTIPATH_NUM]; |
d62a17ae | 516 | struct nexthop *nh_node = NULL; |
517 | ifindex_t first_ifindex; | |
518 | struct interface *ifp = NULL; | |
519 | uint32_t hash_val = 0, mod_val = 0; | |
520 | uint8_t nh_iter = 0, found = 0; | |
19967127 | 521 | uint32_t i, num_nbrs = 0; |
d62a17ae | 522 | |
523 | if (!pnc || !pnc->nexthop_num || !nexthop) | |
cb9c7c50 | 524 | return 0; |
d62a17ae | 525 | |
431f21d3 A |
526 | memset(&nbrs, 0, sizeof(nbrs)); |
527 | memset(&ifps, 0, sizeof(ifps)); | |
528 | ||
d62a17ae | 529 | // Current Nexthop is VALID, check to stay on the current path. |
530 | if (nexthop->interface && nexthop->interface->info | |
531 | && nexthop->mrib_nexthop_addr.u.prefix4.s_addr | |
532 | != PIM_NET_INADDR_ANY) { | |
533 | /* User configured knob to explicitly switch | |
534 | to new path is disabled or current path | |
535 | metric is less than nexthop update. | |
536 | */ | |
537 | ||
4795fff7 | 538 | if (pim->ecmp_rebalance_enable == 0) { |
d62a17ae | 539 | uint8_t curr_route_valid = 0; |
540 | // Check if current nexthop is present in new updated | |
541 | // Nexthop list. | |
542 | // If the current nexthop is not valid, candidate to | |
543 | // choose new Nexthop. | |
544 | for (nh_node = pnc->nexthop; nh_node; | |
1b5f8a1a | 545 | nh_node = nh_node->next) { |
d62a17ae | 546 | curr_route_valid = (nexthop->interface->ifindex |
547 | == nh_node->ifindex); | |
1b5f8a1a DS |
548 | if (curr_route_valid) |
549 | break; | |
550 | } | |
d62a17ae | 551 | |
552 | if (curr_route_valid | |
553 | && !pim_if_connected_to_source(nexthop->interface, | |
554 | src->u.prefix4)) { | |
555 | nbr = pim_neighbor_find( | |
556 | nexthop->interface, | |
557 | nexthop->mrib_nexthop_addr.u.prefix4); | |
558 | if (!nbr | |
559 | && !if_is_loopback(nexthop->interface)) { | |
5cef40fc | 560 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 561 | zlog_debug( |
562 | "%s: current nexthop does not have nbr ", | |
15569c58 | 563 | __func__); |
d62a17ae | 564 | } else { |
20fcf61c AK |
565 | /* update metric even if the upstream |
566 | * neighbor stays unchanged | |
567 | */ | |
568 | nexthop->mrib_metric_preference = | |
569 | pnc->distance; | |
570 | nexthop->mrib_route_metric = | |
571 | pnc->metric; | |
5cef40fc | 572 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 573 | char src_str[INET_ADDRSTRLEN]; |
574 | pim_inet4_dump("<addr?>", | |
575 | src->u.prefix4, | |
576 | src_str, | |
577 | sizeof(src_str)); | |
578 | char grp_str[INET_ADDRSTRLEN]; | |
579 | pim_inet4_dump("<addr?>", | |
580 | grp->u.prefix4, | |
581 | grp_str, | |
582 | sizeof(grp_str)); | |
583 | zlog_debug( | |
5cef40fc | 584 | "%s: (%s,%s)(%s) current nexthop %s is valid, skipping new path selection", |
15569c58 DA |
585 | __func__, src_str, |
586 | grp_str, pim->vrf->name, | |
d62a17ae | 587 | nexthop->interface->name); |
588 | } | |
a7f95c76 | 589 | return 1; |
d62a17ae | 590 | } |
591 | } | |
592 | } | |
593 | } | |
19967127 DS |
594 | |
595 | /* | |
596 | * Look up all interfaces and neighbors, | |
597 | * store for later usage | |
598 | */ | |
599 | for (nh_node = pnc->nexthop, i = 0; nh_node; | |
600 | nh_node = nh_node->next, i++) { | |
601 | ifps[i] = if_lookup_by_index(nh_node->ifindex, pim->vrf_id); | |
602 | if (ifps[i]) { | |
603 | nbrs[i] = pim_neighbor_find(ifps[i], | |
604 | nh_node->gate.ipv4); | |
605 | if (nbrs[i] || pim_if_connected_to_source(ifps[i], | |
ff5d90c2 | 606 | |
19967127 DS |
607 | src->u.prefix4)) |
608 | num_nbrs++; | |
609 | } | |
610 | } | |
4795fff7 | 611 | if (pim->ecmp_enable) { |
19967127 DS |
612 | uint32_t consider = pnc->nexthop_num; |
613 | ||
614 | if (neighbor_needed && num_nbrs < consider) | |
615 | consider = num_nbrs; | |
616 | ||
617 | if (consider == 0) | |
618 | return 0; | |
619 | ||
d62a17ae | 620 | // PIM ECMP flag is enable then choose ECMP path. |
621 | hash_val = pim_compute_ecmp_hash(src, grp); | |
19967127 | 622 | mod_val = hash_val % consider; |
d62a17ae | 623 | } |
624 | ||
625 | for (nh_node = pnc->nexthop; nh_node && (found == 0); | |
626 | nh_node = nh_node->next) { | |
627 | first_ifindex = nh_node->ifindex; | |
19967127 | 628 | ifp = ifps[nh_iter]; |
d62a17ae | 629 | if (!ifp) { |
5cef40fc | 630 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 631 | char addr_str[INET_ADDRSTRLEN]; |
632 | pim_inet4_dump("<addr?>", src->u.prefix4, | |
633 | addr_str, sizeof(addr_str)); | |
634 | zlog_debug( | |
5cef40fc | 635 | "%s %s: could not find interface for ifindex %d (address %s(%s))", |
15569c58 DA |
636 | __FILE__, __func__, first_ifindex, |
637 | addr_str, pim->vrf->name); | |
d62a17ae | 638 | } |
639 | if (nh_iter == mod_val) | |
640 | mod_val++; // Select nexthpath | |
641 | nh_iter++; | |
642 | continue; | |
643 | } | |
644 | if (!ifp->info) { | |
5cef40fc | 645 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 646 | char addr_str[INET_ADDRSTRLEN]; |
647 | pim_inet4_dump("<addr?>", src->u.prefix4, | |
648 | addr_str, sizeof(addr_str)); | |
649 | zlog_debug( | |
5cef40fc | 650 | "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)", |
15569c58 DA |
651 | __func__, ifp->name, pim->vrf->name, |
652 | first_ifindex, addr_str); | |
d62a17ae | 653 | } |
654 | if (nh_iter == mod_val) | |
655 | mod_val++; // Select nexthpath | |
656 | nh_iter++; | |
657 | continue; | |
658 | } | |
659 | ||
660 | if (neighbor_needed | |
661 | && !pim_if_connected_to_source(ifp, src->u.prefix4)) { | |
19967127 | 662 | nbr = nbrs[nh_iter]; |
d62a17ae | 663 | if (!nbr && !if_is_loopback(ifp)) { |
5cef40fc | 664 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 665 | zlog_debug( |
5cef40fc | 666 | "%s: pim nbr not found on input interface %s(%s)", |
15569c58 | 667 | __func__, ifp->name, |
5cef40fc | 668 | pim->vrf->name); |
d62a17ae | 669 | if (nh_iter == mod_val) |
670 | mod_val++; // Select nexthpath | |
671 | nh_iter++; | |
672 | continue; | |
673 | } | |
674 | } | |
675 | ||
676 | if (nh_iter == mod_val) { | |
677 | nexthop->interface = ifp; | |
678 | nexthop->mrib_nexthop_addr.family = AF_INET; | |
679 | nexthop->mrib_nexthop_addr.prefixlen = IPV4_MAX_BITLEN; | |
680 | nexthop->mrib_nexthop_addr.u.prefix4 = | |
681 | nh_node->gate.ipv4; | |
682 | nexthop->mrib_metric_preference = pnc->distance; | |
683 | nexthop->mrib_route_metric = pnc->metric; | |
684 | nexthop->last_lookup = src->u.prefix4; | |
685 | nexthop->last_lookup_time = pim_time_monotonic_usec(); | |
686 | nexthop->nbr = nbr; | |
687 | found = 1; | |
5cef40fc | 688 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 689 | char buf[INET_ADDRSTRLEN]; |
690 | char buf2[INET_ADDRSTRLEN]; | |
691 | char buf3[INET_ADDRSTRLEN]; | |
692 | pim_inet4_dump("<src?>", src->u.prefix4, buf2, | |
693 | sizeof(buf2)); | |
694 | pim_inet4_dump("<grp?>", grp->u.prefix4, buf3, | |
695 | sizeof(buf3)); | |
696 | pim_inet4_dump( | |
697 | "<rpf?>", | |
698 | nexthop->mrib_nexthop_addr.u.prefix4, | |
699 | buf, sizeof(buf)); | |
700 | zlog_debug( | |
5cef40fc | 701 | "%s: (%s,%s)(%s) selected nhop interface %s addr %s mod_val %u iter %d ecmp %d", |
15569c58 DA |
702 | __func__, buf2, buf3, pim->vrf->name, |
703 | ifp->name, buf, mod_val, nh_iter, | |
704 | pim->ecmp_enable); | |
d62a17ae | 705 | } |
706 | } | |
707 | nh_iter++; | |
708 | } | |
709 | ||
710 | if (found) | |
cb9c7c50 | 711 | return 1; |
d62a17ae | 712 | else |
cb9c7c50 | 713 | return 0; |
633988a7 CS |
714 | } |
715 | ||
d62a17ae | 716 | /* This API is used to parse Registered address nexthop update coming from Zebra |
717 | */ | |
121f9dee | 718 | int pim_parse_nexthop_update(ZAPI_CALLBACK_ARGS) |
1bc98276 | 719 | { |
d62a17ae | 720 | struct nexthop *nexthop; |
721 | struct nexthop *nhlist_head = NULL; | |
722 | struct nexthop *nhlist_tail = NULL; | |
d62a17ae | 723 | int i; |
724 | struct pim_rpf rpf; | |
725 | struct pim_nexthop_cache *pnc = NULL; | |
726 | struct pim_neighbor *nbr = NULL; | |
727 | struct interface *ifp = NULL; | |
728 | struct interface *ifp1 = NULL; | |
cf663ceb | 729 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
819f099b | 730 | struct pim_instance *pim; |
4a749e2c | 731 | struct zapi_route nhr; |
819f099b DS |
732 | |
733 | if (!vrf) | |
734 | return 0; | |
735 | pim = vrf->info; | |
d62a17ae | 736 | |
7d30a959 DS |
737 | if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) { |
738 | if (PIM_DEBUG_PIM_NHT) | |
996c9314 LB |
739 | zlog_debug( |
740 | "%s: Decode of nexthop update from zebra failed", | |
15569c58 | 741 | __func__); |
7d30a959 DS |
742 | return 0; |
743 | } | |
d62a17ae | 744 | |
121f9dee | 745 | if (cmd == ZEBRA_NEXTHOP_UPDATE) { |
4a749e2c | 746 | prefix_copy(&rpf.rpf_addr, &nhr.prefix); |
25b787a2 | 747 | pnc = pim_nexthop_cache_find(pim, &rpf); |
d62a17ae | 748 | if (!pnc) { |
5cef40fc | 749 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 750 | char buf[PREFIX2STR_BUFFER]; |
751 | prefix2str(&rpf.rpf_addr, buf, sizeof(buf)); | |
752 | zlog_debug( | |
753 | "%s: Skipping NHT update, addr %s is not in local cached DB.", | |
15569c58 | 754 | __func__, buf); |
d62a17ae | 755 | } |
756 | return 0; | |
757 | } | |
758 | } else { | |
759 | /* | |
760 | * We do not currently handle ZEBRA_IMPORT_CHECK_UPDATE | |
761 | */ | |
762 | return 0; | |
763 | } | |
764 | ||
765 | pnc->last_update = pim_time_monotonic_usec(); | |
d62a17ae | 766 | |
4a749e2c | 767 | if (nhr.nexthop_num) { |
d62a17ae | 768 | pnc->nexthop_num = 0; // Only increment for pim enabled rpf. |
769 | ||
4a749e2c DS |
770 | for (i = 0; i < nhr.nexthop_num; i++) { |
771 | nexthop = nexthop_from_zapi_nexthop(&nhr.nexthops[i]); | |
d62a17ae | 772 | switch (nexthop->type) { |
773 | case NEXTHOP_TYPE_IPV4: | |
d62a17ae | 774 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
d62a17ae | 775 | case NEXTHOP_TYPE_IPV6: |
4a749e2c | 776 | case NEXTHOP_TYPE_BLACKHOLE: |
d62a17ae | 777 | break; |
66f5152f MB |
778 | case NEXTHOP_TYPE_IFINDEX: |
779 | /* | |
780 | * Connected route (i.e. no nexthop), use | |
781 | * RPF address from nexthop cache (i.e. | |
782 | * destination) as PIM nexthop. | |
783 | */ | |
56c1568b | 784 | nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
66f5152f MB |
785 | nexthop->gate.ipv4 = |
786 | pnc->rpf.rpf_addr.u.prefix4; | |
787 | break; | |
d62a17ae | 788 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
d62a17ae | 789 | ifp1 = if_lookup_by_index(nexthop->ifindex, |
25b787a2 | 790 | pim->vrf_id); |
85948e7b DS |
791 | |
792 | if (!ifp1) | |
793 | nbr = NULL; | |
794 | else | |
795 | nbr = pim_neighbor_find_if(ifp1); | |
d62a17ae | 796 | /* Overwrite with Nbr address as NH addr */ |
5cef40fc | 797 | if (nbr) |
d62a17ae | 798 | nexthop->gate.ipv4 = nbr->source_addr; |
5cef40fc | 799 | else { |
d62a17ae | 800 | // Mark nexthop address to 0 until PIM |
801 | // Nbr is resolved. | |
802 | nexthop->gate.ipv4.s_addr = | |
803 | PIM_NET_INADDR_ANY; | |
804 | } | |
805 | ||
d62a17ae | 806 | break; |
807 | } | |
808 | ||
25b787a2 | 809 | ifp = if_lookup_by_index(nexthop->ifindex, pim->vrf_id); |
d62a17ae | 810 | if (!ifp) { |
5cef40fc | 811 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 812 | char buf[NEXTHOP_STRLEN]; |
813 | zlog_debug( | |
5cef40fc | 814 | "%s: could not find interface for ifindex %d(%s) (addr %s)", |
15569c58 | 815 | __func__, nexthop->ifindex, |
5cef40fc | 816 | pim->vrf->name, |
d62a17ae | 817 | nexthop2str(nexthop, buf, |
818 | sizeof(buf))); | |
819 | } | |
820 | nexthop_free(nexthop); | |
821 | continue; | |
822 | } | |
823 | ||
1b5f8a1a DS |
824 | if (PIM_DEBUG_PIM_NHT) { |
825 | char p_str[PREFIX2STR_BUFFER]; | |
4a749e2c DS |
826 | |
827 | prefix2str(&nhr.prefix, p_str, sizeof(p_str)); | |
1b5f8a1a DS |
828 | zlog_debug( |
829 | "%s: NHT addr %s(%s) %d-nhop via %s(%s) type %d distance:%u metric:%u ", | |
15569c58 | 830 | __func__, p_str, pim->vrf->name, i + 1, |
1b5f8a1a | 831 | inet_ntoa(nexthop->gate.ipv4), |
4a749e2c DS |
832 | ifp->name, nexthop->type, nhr.distance, |
833 | nhr.metric); | |
1b5f8a1a DS |
834 | } |
835 | ||
d62a17ae | 836 | if (!ifp->info) { |
59711f10 SK |
837 | /* |
838 | * Though Multicast is not enabled on this | |
839 | * Interface store it in database otheriwse we | |
840 | * may miss this update and this will not cause | |
841 | * any issue, because while choosing the path we | |
842 | * are ommitting the Interfaces which are not | |
843 | * multicast enabled | |
844 | */ | |
5cef40fc | 845 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 846 | char buf[NEXTHOP_STRLEN]; |
4a749e2c | 847 | |
d62a17ae | 848 | zlog_debug( |
5cef40fc | 849 | "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, addr %s)", |
15569c58 | 850 | __func__, ifp->name, |
5cef40fc | 851 | pim->vrf->name, |
d62a17ae | 852 | nexthop->ifindex, |
853 | nexthop2str(nexthop, buf, | |
854 | sizeof(buf))); | |
855 | } | |
d62a17ae | 856 | } |
857 | ||
858 | if (nhlist_tail) { | |
859 | nhlist_tail->next = nexthop; | |
860 | nhlist_tail = nexthop; | |
861 | } else { | |
862 | nhlist_tail = nexthop; | |
863 | nhlist_head = nexthop; | |
864 | } | |
865 | // Only keep track of nexthops which are PIM enabled. | |
866 | pnc->nexthop_num++; | |
867 | } | |
868 | /* Reset existing pnc->nexthop before assigning new list */ | |
869 | nexthops_free(pnc->nexthop); | |
870 | pnc->nexthop = nhlist_head; | |
871 | if (pnc->nexthop_num) { | |
872 | pnc->flags |= PIM_NEXTHOP_VALID; | |
4a749e2c DS |
873 | pnc->distance = nhr.distance; |
874 | pnc->metric = nhr.metric; | |
d62a17ae | 875 | } |
876 | } else { | |
877 | pnc->flags &= ~PIM_NEXTHOP_VALID; | |
4a749e2c | 878 | pnc->nexthop_num = nhr.nexthop_num; |
d62a17ae | 879 | nexthops_free(pnc->nexthop); |
880 | pnc->nexthop = NULL; | |
881 | } | |
2cb7234f | 882 | SET_FLAG(pnc->flags, PIM_NEXTHOP_ANSWER_RECEIVED); |
d62a17ae | 883 | |
5cef40fc | 884 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 885 | char buf[PREFIX2STR_BUFFER]; |
4a749e2c | 886 | prefix2str(&nhr.prefix, buf, sizeof(buf)); |
d62a17ae | 887 | zlog_debug( |
87ad28f4 | 888 | "%s: NHT Update for %s(%s) num_nh %d num_pim_nh %d vrf:%u up %ld rp %d", |
15569c58 DA |
889 | __func__, buf, pim->vrf->name, nhr.nexthop_num, |
890 | pnc->nexthop_num, vrf_id, pnc->upstream_hash->count, | |
891 | listcount(pnc->rp_list)); | |
d62a17ae | 892 | } |
893 | ||
bfc92019 | 894 | pim_rpf_set_refresh_time(pim); |
d62a17ae | 895 | |
896 | if (listcount(pnc->rp_list)) | |
25b787a2 | 897 | pim_update_rp_nh(pim, pnc); |
7c591950 | 898 | if (pnc->upstream_hash->count) |
cf663ceb | 899 | pim_update_upstream_nh(pim, pnc); |
d62a17ae | 900 | |
901 | return 0; | |
1bc98276 | 902 | } |
633988a7 | 903 | |
25b787a2 | 904 | int pim_ecmp_nexthop_lookup(struct pim_instance *pim, |
b938537b DS |
905 | struct pim_nexthop *nexthop, struct prefix *src, |
906 | struct prefix *grp, int neighbor_needed) | |
633988a7 | 907 | { |
43763b11 | 908 | struct pim_nexthop_cache *pnc; |
d62a17ae | 909 | struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM]; |
19967127 | 910 | struct pim_neighbor *nbrs[MULTIPATH_NUM], *nbr = NULL; |
43763b11 | 911 | struct pim_rpf rpf; |
d62a17ae | 912 | int num_ifindex; |
19967127 | 913 | struct interface *ifps[MULTIPATH_NUM], *ifp; |
d62a17ae | 914 | int first_ifindex; |
915 | int found = 0; | |
916 | uint8_t i = 0; | |
917 | uint32_t hash_val = 0, mod_val = 0; | |
19967127 | 918 | uint32_t num_nbrs = 0; |
b938537b | 919 | char addr_str[PREFIX_STRLEN]; |
d62a17ae | 920 | |
5cef40fc | 921 | if (PIM_DEBUG_PIM_NHT) { |
b938537b DS |
922 | pim_inet4_dump("<addr?>", src->u.prefix4, addr_str, |
923 | sizeof(addr_str)); | |
5cef40fc | 924 | zlog_debug("%s: Looking up: %s(%s), last lookup time: %lld", |
15569c58 | 925 | __func__, addr_str, pim->vrf->name, |
d62a17ae | 926 | nexthop->last_lookup_time); |
927 | } | |
928 | ||
43763b11 DS |
929 | memset(&rpf, 0, sizeof(struct pim_rpf)); |
930 | rpf.rpf_addr.family = AF_INET; | |
931 | rpf.rpf_addr.prefixlen = IPV4_MAX_BITLEN; | |
932 | rpf.rpf_addr.u.prefix4 = src->u.prefix4; | |
933 | ||
934 | pnc = pim_nexthop_cache_find(pim, &rpf); | |
2cb7234f DS |
935 | if (pnc) { |
936 | if (CHECK_FLAG(pnc->flags, PIM_NEXTHOP_ANSWER_RECEIVED)) | |
937 | return pim_ecmp_nexthop_search(pim, pnc, nexthop, src, grp, | |
938 | neighbor_needed); | |
939 | } | |
43763b11 | 940 | |
d62a17ae | 941 | memset(nexthop_tab, 0, |
942 | sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM); | |
b938537b DS |
943 | num_ifindex = |
944 | zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM, | |
945 | src->u.prefix4, PIM_NEXTHOP_LOOKUP_MAX); | |
d62a17ae | 946 | if (num_ifindex < 1) { |
b938537b | 947 | if (PIM_DEBUG_PIM_NHT) |
209a5679 DS |
948 | zlog_warn( |
949 | "%s: could not find nexthop ifindex for address %s(%s)", | |
15569c58 | 950 | __func__, addr_str, pim->vrf->name); |
cb9c7c50 | 951 | return 0; |
d62a17ae | 952 | } |
953 | ||
431f21d3 A |
954 | memset(&nbrs, 0, sizeof(nbrs)); |
955 | memset(&ifps, 0, sizeof(ifps)); | |
956 | ||
19967127 DS |
957 | /* |
958 | * Look up all interfaces and neighbors, | |
959 | * store for later usage | |
960 | */ | |
961 | for (i = 0; i < num_ifindex; i++) { | |
962 | ifps[i] = if_lookup_by_index(nexthop_tab[i].ifindex, | |
963 | pim->vrf_id); | |
964 | if (ifps[i]) { | |
965 | nbrs[i] = pim_neighbor_find( | |
966 | ifps[i], nexthop_tab[i].nexthop_addr.u.prefix4); | |
b938537b DS |
967 | if (nbrs[i] |
968 | || pim_if_connected_to_source(ifps[i], | |
969 | src->u.prefix4)) | |
19967127 DS |
970 | num_nbrs++; |
971 | } | |
972 | } | |
973 | ||
d62a17ae | 974 | // If PIM ECMP enable then choose ECMP path. |
4795fff7 | 975 | if (pim->ecmp_enable) { |
19967127 DS |
976 | uint32_t consider = num_ifindex; |
977 | ||
978 | if (neighbor_needed && num_nbrs < consider) | |
979 | consider = num_nbrs; | |
980 | ||
981 | if (consider == 0) | |
982 | return 0; | |
983 | ||
d62a17ae | 984 | hash_val = pim_compute_ecmp_hash(src, grp); |
19967127 | 985 | mod_val = hash_val % consider; |
5cef40fc | 986 | if (PIM_DEBUG_PIM_NHT_DETAIL) |
15569c58 DA |
987 | zlog_debug("%s: hash_val %u mod_val %u", __func__, |
988 | hash_val, mod_val); | |
d62a17ae | 989 | } |
990 | ||
19967127 | 991 | i = 0; |
d62a17ae | 992 | while (!found && (i < num_ifindex)) { |
993 | first_ifindex = nexthop_tab[i].ifindex; | |
994 | ||
19967127 | 995 | ifp = ifps[i]; |
d62a17ae | 996 | if (!ifp) { |
b938537b | 997 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 998 | zlog_debug( |
5cef40fc | 999 | "%s %s: could not find interface for ifindex %d (address %s(%s))", |
15569c58 DA |
1000 | __FILE__, __func__, first_ifindex, |
1001 | addr_str, pim->vrf->name); | |
d62a17ae | 1002 | if (i == mod_val) |
1003 | mod_val++; | |
1004 | i++; | |
1005 | continue; | |
1006 | } | |
1007 | ||
1008 | if (!ifp->info) { | |
b938537b | 1009 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 1010 | zlog_debug( |
5cef40fc | 1011 | "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)", |
15569c58 DA |
1012 | __func__, ifp->name, pim->vrf->name, |
1013 | first_ifindex, addr_str); | |
d62a17ae | 1014 | if (i == mod_val) |
1015 | mod_val++; | |
1016 | i++; | |
1017 | continue; | |
1018 | } | |
b938537b DS |
1019 | if (neighbor_needed |
1020 | && !pim_if_connected_to_source(ifp, src->u.prefix4)) { | |
19967127 | 1021 | nbr = nbrs[i]; |
5cef40fc DS |
1022 | if (PIM_DEBUG_PIM_NHT_DETAIL) |
1023 | zlog_debug("ifp name: %s(%s), pim nbr: %p", | |
1024 | ifp->name, pim->vrf->name, nbr); | |
d62a17ae | 1025 | if (!nbr && !if_is_loopback(ifp)) { |
1026 | if (i == mod_val) | |
1027 | mod_val++; | |
1028 | i++; | |
b938537b | 1029 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 1030 | zlog_debug( |
5cef40fc | 1031 | "%s: NBR not found on input interface %s(%s) (RPF for source %s)", |
15569c58 | 1032 | __func__, ifp->name, |
5cef40fc | 1033 | pim->vrf->name, addr_str); |
d62a17ae | 1034 | continue; |
1035 | } | |
1036 | } | |
1037 | ||
1038 | if (i == mod_val) { | |
5cef40fc | 1039 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 1040 | char nexthop_str[PREFIX_STRLEN]; |
b938537b | 1041 | |
d62a17ae | 1042 | pim_addr_dump("<nexthop?>", |
1043 | &nexthop_tab[i].nexthop_addr, | |
1044 | nexthop_str, sizeof(nexthop_str)); | |
d62a17ae | 1045 | zlog_debug( |
5cef40fc | 1046 | "%s: found nhop %s for addr %s interface %s(%s) metric %d dist %d", |
15569c58 DA |
1047 | __func__, nexthop_str, addr_str, |
1048 | ifp->name, pim->vrf->name, | |
d62a17ae | 1049 | nexthop_tab[i].route_metric, |
1050 | nexthop_tab[i].protocol_distance); | |
1051 | } | |
1a81b790 | 1052 | /* update nexthop data */ |
d62a17ae | 1053 | nexthop->interface = ifp; |
1054 | nexthop->mrib_nexthop_addr = | |
1055 | nexthop_tab[i].nexthop_addr; | |
1056 | nexthop->mrib_metric_preference = | |
1057 | nexthop_tab[i].protocol_distance; | |
1058 | nexthop->mrib_route_metric = | |
1059 | nexthop_tab[i].route_metric; | |
b938537b | 1060 | nexthop->last_lookup = src->u.prefix4; |
d62a17ae | 1061 | nexthop->last_lookup_time = pim_time_monotonic_usec(); |
1062 | nexthop->nbr = nbr; | |
1063 | found = 1; | |
1064 | } | |
1065 | i++; | |
1066 | } | |
cb9c7c50 | 1067 | |
d62a17ae | 1068 | if (found) |
cb9c7c50 | 1069 | return 1; |
d62a17ae | 1070 | else |
cb9c7c50 | 1071 | return 0; |
633988a7 | 1072 | } |
815c33c9 | 1073 | |
25b787a2 | 1074 | int pim_ecmp_fib_lookup_if_vif_index(struct pim_instance *pim, |
b938537b | 1075 | struct prefix *src, struct prefix *grp) |
815c33c9 | 1076 | { |
8a5134aa | 1077 | struct pim_nexthop nhop; |
d62a17ae | 1078 | int vif_index; |
8a5134aa | 1079 | ifindex_t ifindex; |
b938537b | 1080 | char addr_str[PREFIX_STRLEN]; |
d62a17ae | 1081 | |
b938537b DS |
1082 | if (PIM_DEBUG_PIM_NHT) |
1083 | pim_inet4_dump("<addr?>", src->u.prefix4, addr_str, | |
1084 | sizeof(addr_str)); | |
2ccc414b DS |
1085 | |
1086 | memset(&nhop, 0, sizeof(nhop)); | |
3d68661d | 1087 | if (!pim_ecmp_nexthop_lookup(pim, &nhop, src, grp, 1)) { |
b938537b | 1088 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 1089 | zlog_debug( |
5cef40fc | 1090 | "%s: could not find nexthop ifindex for address %s(%s)", |
15569c58 | 1091 | __func__, addr_str, pim->vrf->name); |
d62a17ae | 1092 | return -1; |
1093 | } | |
1094 | ||
8a5134aa | 1095 | ifindex = nhop.interface->ifindex; |
b938537b | 1096 | if (PIM_DEBUG_PIM_NHT) |
d62a17ae | 1097 | zlog_debug( |
5cef40fc | 1098 | "%s: found nexthop ifindex=%d (interface %s(%s)) for address %s", |
15569c58 | 1099 | __func__, ifindex, ifindex2ifname(ifindex, pim->vrf_id), |
5cef40fc | 1100 | pim->vrf->name, addr_str); |
d62a17ae | 1101 | |
8a5134aa | 1102 | vif_index = pim_if_find_vifindex_by_ifindex(pim, ifindex); |
d62a17ae | 1103 | |
1104 | if (vif_index < 0) { | |
5cef40fc | 1105 | if (PIM_DEBUG_PIM_NHT) { |
d62a17ae | 1106 | zlog_debug( |
5cef40fc | 1107 | "%s: low vif_index=%d(%s) < 1 nexthop for address %s", |
15569c58 | 1108 | __func__, vif_index, pim->vrf->name, addr_str); |
d62a17ae | 1109 | } |
1110 | return -2; | |
1111 | } | |
1112 | ||
1113 | return vif_index; | |
815c33c9 | 1114 | } |