]>
Commit | Line | Data |
---|---|---|
12e41d03 | 1 | /* |
896014f4 DL |
2 | * PIM for Quagga |
3 | * Copyright (C) 2008 Everton da Silva Marques | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
12e41d03 DL |
19 | |
20 | #include <zebra.h> | |
21 | ||
744d91b3 DS |
22 | #include "if.h" |
23 | ||
12e41d03 DL |
24 | #include "log.h" |
25 | #include "prefix.h" | |
26 | #include "memory.h" | |
27 | ||
28 | #include "pimd.h" | |
29 | #include "pim_rpf.h" | |
30 | #include "pim_pim.h" | |
31 | #include "pim_str.h" | |
32 | #include "pim_iface.h" | |
33 | #include "pim_zlookup.h" | |
34 | #include "pim_ifchannel.h" | |
e71bf8f7 | 35 | #include "pim_time.h" |
1bc98276 | 36 | #include "pim_nht.h" |
e71bf8f7 DS |
37 | |
38 | static long long last_route_change_time = -1; | |
39 | long long nexthop_lookups_avoided = 0; | |
12e41d03 | 40 | |
1bc98276 | 41 | static struct in_addr pim_rpf_find_rpf_addr (struct pim_upstream *up); |
12e41d03 | 42 | |
e71bf8f7 DS |
43 | void |
44 | pim_rpf_set_refresh_time (void) | |
45 | { | |
46 | last_route_change_time = pim_time_monotonic_usec(); | |
47 | if (PIM_DEBUG_TRACE) | |
48 | zlog_debug ("%s: New last route change time: %lld", | |
49 | __PRETTY_FUNCTION__, last_route_change_time); | |
50 | } | |
51 | ||
63b8f7a3 | 52 | int pim_nexthop_lookup(struct pim_nexthop *nexthop, struct in_addr addr, int neighbor_needed) |
12e41d03 | 53 | { |
7b2c4d16 | 54 | struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM]; |
982bff89 | 55 | struct pim_neighbor *nbr = NULL; |
12e41d03 | 56 | int num_ifindex; |
7a6e6567 DS |
57 | struct interface *ifp = NULL; |
58 | ifindex_t first_ifindex = 0; | |
63b8f7a3 DS |
59 | int found = 0; |
60 | int i = 0; | |
12e41d03 | 61 | |
e71bf8f7 DS |
62 | if ((nexthop->last_lookup.s_addr == addr.s_addr) && |
63 | (nexthop->last_lookup_time > last_route_change_time)) | |
64 | { | |
65 | if (PIM_DEBUG_TRACE) | |
66 | { | |
67 | char addr_str[INET_ADDRSTRLEN]; | |
68 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
633988a7 CS |
69 | char nexthop_str[PREFIX_STRLEN]; |
70 | pim_addr_dump("<nexthop?>", &nexthop->mrib_nexthop_addr, | |
71 | nexthop_str, sizeof(nexthop_str)); | |
72 | zlog_debug ("%s: Using last lookup for %s at %lld, %lld addr%s", | |
e71bf8f7 DS |
73 | __PRETTY_FUNCTION__, |
74 | addr_str, | |
75 | nexthop->last_lookup_time, | |
633988a7 | 76 | last_route_change_time, nexthop_str); |
e71bf8f7 DS |
77 | } |
78 | nexthop_lookups_avoided++; | |
79 | return 0; | |
80 | } | |
81 | else | |
82 | { | |
83 | if (PIM_DEBUG_TRACE) | |
84 | { | |
85 | char addr_str[INET_ADDRSTRLEN]; | |
86 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
87 | zlog_debug ("%s: Looking up: %s, last lookup time: %lld, %lld", | |
88 | __PRETTY_FUNCTION__, | |
89 | addr_str, | |
90 | nexthop->last_lookup_time, | |
91 | last_route_change_time); | |
92 | } | |
93 | } | |
94 | ||
7b2c4d16 | 95 | memset (nexthop_tab, 0, sizeof (struct pim_zlookup_nexthop) * MULTIPATH_NUM); |
f24405b1 | 96 | num_ifindex = zclient_lookup_nexthop(nexthop_tab, |
7b2c4d16 | 97 | MULTIPATH_NUM, |
f24405b1 DS |
98 | addr, PIM_NEXTHOP_LOOKUP_MAX); |
99 | if (num_ifindex < 1) { | |
eaa54bdb | 100 | char addr_str[INET_ADDRSTRLEN]; |
f24405b1 DS |
101 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
102 | zlog_warn("%s %s: could not find nexthop ifindex for address %s", | |
103 | __FILE__, __PRETTY_FUNCTION__, | |
104 | addr_str); | |
105 | return -1; | |
106 | } | |
107 | ||
518e5727 | 108 | while (!found && (i < num_ifindex)) |
63b8f7a3 DS |
109 | { |
110 | first_ifindex = nexthop_tab[i].ifindex; | |
111 | ||
7e2b7603 | 112 | ifp = if_lookup_by_index(first_ifindex, VRF_DEFAULT); |
63b8f7a3 DS |
113 | if (!ifp) |
114 | { | |
115 | if (PIM_DEBUG_ZEBRA) | |
116 | { | |
eaa54bdb | 117 | char addr_str[INET_ADDRSTRLEN]; |
63b8f7a3 DS |
118 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
119 | zlog_debug("%s %s: could not find interface for ifindex %d (address %s)", | |
120 | __FILE__, __PRETTY_FUNCTION__, | |
121 | first_ifindex, addr_str); | |
122 | } | |
28185336 DS |
123 | i++; |
124 | continue; | |
63b8f7a3 DS |
125 | } |
126 | ||
518e5727 | 127 | if (!ifp->info) |
63b8f7a3 | 128 | { |
518e5727 DS |
129 | if (PIM_DEBUG_ZEBRA) |
130 | { | |
131 | char addr_str[INET_ADDRSTRLEN]; | |
132 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
133 | zlog_debug("%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)", | |
134 | __PRETTY_FUNCTION__, | |
135 | ifp->name, first_ifindex, addr_str); | |
136 | } | |
137 | i++; | |
63b8f7a3 | 138 | } |
518e5727 | 139 | else if (neighbor_needed && !pim_if_connected_to_source (ifp, addr)) |
63b8f7a3 | 140 | { |
63b8f7a3 | 141 | nbr = pim_neighbor_find (ifp, nexthop_tab[i].nexthop_addr.u.prefix4); |
802b226c | 142 | if (PIM_DEBUG_PIM_TRACE_DETAIL) |
63b8f7a3 DS |
143 | zlog_debug ("ifp name: %s, pim nbr: %p", ifp->name, nbr); |
144 | if (!nbr && !if_is_loopback (ifp)) | |
145 | i++; | |
146 | else | |
147 | found = 1; | |
148 | } | |
149 | else | |
150 | found = 1; | |
151 | } | |
152 | ||
153 | if (found) | |
154 | { | |
155 | if (PIM_DEBUG_ZEBRA) { | |
eaa54bdb DW |
156 | char nexthop_str[PREFIX_STRLEN]; |
157 | char addr_str[INET_ADDRSTRLEN]; | |
58c51da2 | 158 | pim_addr_dump("<nexthop?>", &nexthop_tab[i].nexthop_addr, nexthop_str, sizeof(nexthop_str)); |
63b8f7a3 DS |
159 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
160 | zlog_debug("%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d", | |
161 | __FILE__, __PRETTY_FUNCTION__, | |
162 | nexthop_str, addr_str, | |
163 | ifp->name, first_ifindex, | |
58c51da2 | 164 | nexthop_tab[i].route_metric, |
165 | nexthop_tab[i].protocol_distance); | |
63b8f7a3 DS |
166 | } |
167 | /* update nextop data */ | |
168 | nexthop->interface = ifp; | |
58c51da2 | 169 | nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr; |
170 | nexthop->mrib_metric_preference = nexthop_tab[i].protocol_distance; | |
171 | nexthop->mrib_route_metric = nexthop_tab[i].route_metric; | |
e71bf8f7 DS |
172 | nexthop->last_lookup = addr; |
173 | nexthop->last_lookup_time = pim_time_monotonic_usec(); | |
982bff89 | 174 | nexthop->nbr = nbr; |
63b8f7a3 DS |
175 | return 0; |
176 | } | |
177 | else | |
7f748d95 | 178 | return -1; |
12e41d03 DL |
179 | } |
180 | ||
181 | static int nexthop_mismatch(const struct pim_nexthop *nh1, | |
182 | const struct pim_nexthop *nh2) | |
183 | { | |
63c59d0c DS |
184 | return (nh1->interface != nh2->interface) || |
185 | (nh1->mrib_nexthop_addr.u.prefix4.s_addr != nh2->mrib_nexthop_addr.u.prefix4.s_addr) || | |
186 | (nh1->mrib_metric_preference != nh2->mrib_metric_preference) || | |
12e41d03 DL |
187 | (nh1->mrib_route_metric != nh2->mrib_route_metric); |
188 | } | |
189 | ||
1bc98276 | 190 | enum pim_rpf_result pim_rpf_update(struct pim_upstream *up, struct pim_rpf *old, uint8_t is_new) |
12e41d03 | 191 | { |
12e41d03 | 192 | struct pim_rpf *rpf = &up->rpf; |
42a0111b | 193 | struct pim_rpf saved; |
1bc98276 | 194 | struct prefix nht_p; |
633988a7 CS |
195 | struct pim_nexthop_cache pnc; |
196 | int ret = 0; | |
197 | struct prefix src, grp; | |
12e41d03 | 198 | |
42a0111b DS |
199 | saved.source_nexthop = rpf->source_nexthop; |
200 | saved.rpf_addr = rpf->rpf_addr; | |
12e41d03 | 201 | |
633988a7 CS |
202 | if (is_new && PIM_DEBUG_ZEBRA) |
203 | { | |
204 | char source_str[INET_ADDRSTRLEN]; | |
205 | pim_inet4_dump ("<source?>", up->upstream_addr, source_str, | |
206 | sizeof (source_str)); | |
207 | zlog_debug ("%s: NHT Register upstream %s addr %s with Zebra.", | |
208 | __PRETTY_FUNCTION__, up->sg_str, source_str); | |
209 | } | |
210 | /* Register addr with Zebra NHT */ | |
211 | nht_p.family = AF_INET; | |
212 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
213 | nht_p.u.prefix4.s_addr = up->upstream_addr.s_addr; | |
214 | ||
215 | src.family = AF_INET; | |
216 | src.prefixlen = IPV4_MAX_BITLEN; | |
217 | src.u.prefix4 = up->upstream_addr; //RP or Src address | |
218 | grp.family = AF_INET; | |
219 | grp.prefixlen = IPV4_MAX_BITLEN; | |
220 | grp.u.prefix4 = up->sg.grp; | |
221 | memset (&pnc, 0, sizeof (struct pim_nexthop_cache)); | |
222 | if ((ret = pim_find_or_track_nexthop (&nht_p, up, NULL, &pnc)) == 1) | |
1bc98276 | 223 | { |
633988a7 | 224 | if (pnc.nexthop_num) |
1bc98276 | 225 | { |
633988a7 | 226 | //Compute PIM RPF using Cached nexthop |
1131c2eb CS |
227 | if (pim_ecmp_nexthop_search (&pnc, &up->rpf.source_nexthop, |
228 | &src, &grp, | |
229 | !PIM_UPSTREAM_FLAG_TEST_FHR (up->flags) && | |
230 | !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP (up->flags))) | |
231 | ||
232 | { | |
233 | return PIM_RPF_FAILURE; | |
234 | } | |
1bc98276 | 235 | } |
1bc98276 | 236 | } |
633988a7 CS |
237 | else |
238 | { | |
239 | if (pim_ecmp_nexthop_lookup (&rpf->source_nexthop, | |
240 | up->upstream_addr, &src, &grp, | |
241 | !PIM_UPSTREAM_FLAG_TEST_FHR (up->flags) && | |
4ba87bb9 | 242 | !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP (up->flags))) |
633988a7 CS |
243 | { |
244 | return PIM_RPF_FAILURE; | |
245 | } | |
246 | } | |
12e41d03 | 247 | |
63c59d0c DS |
248 | rpf->rpf_addr.family = AF_INET; |
249 | rpf->rpf_addr.u.prefix4 = pim_rpf_find_rpf_addr(up); | |
250 | if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) { | |
12e41d03 | 251 | /* RPF'(S,G) not found */ |
05e451f8 | 252 | zlog_debug("%s %s: RPF'%s not found: won't send join upstream", |
7adf0260 | 253 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 254 | up->sg_str); |
12e41d03 DL |
255 | /* warning only */ |
256 | } | |
257 | ||
258 | /* detect change in pim_nexthop */ | |
42a0111b | 259 | if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) { |
12e41d03 | 260 | |
ff67a923 | 261 | if (PIM_DEBUG_ZEBRA) { |
eaa54bdb | 262 | char nhaddr_str[PREFIX_STRLEN]; |
63c59d0c | 263 | pim_addr_dump("<addr?>", &rpf->source_nexthop.mrib_nexthop_addr, nhaddr_str, sizeof(nhaddr_str)); |
05e451f8 | 264 | zlog_debug("%s %s: (S,G)=%s source nexthop now is: interface=%s address=%s pref=%d metric=%d", |
12e41d03 | 265 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 266 | up->sg_str, |
12e41d03 DL |
267 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>", |
268 | nhaddr_str, | |
269 | rpf->source_nexthop.mrib_metric_preference, | |
270 | rpf->source_nexthop.mrib_route_metric); | |
12e41d03 DL |
271 | } |
272 | ||
273 | pim_upstream_update_join_desired(up); | |
274 | pim_upstream_update_could_assert(up); | |
275 | pim_upstream_update_my_assert_metric(up); | |
276 | } | |
277 | ||
278 | /* detect change in RPF_interface(S) */ | |
42a0111b | 279 | if (saved.source_nexthop.interface != rpf->source_nexthop.interface) { |
12e41d03 | 280 | |
ff67a923 | 281 | if (PIM_DEBUG_ZEBRA) { |
05e451f8 | 282 | zlog_debug("%s %s: (S,G)=%s RPF_interface(S) changed from %s to %s", |
12e41d03 | 283 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 284 | up->sg_str, |
42a0111b | 285 | saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>", |
12e41d03 DL |
286 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>"); |
287 | /* warning only */ | |
288 | } | |
289 | ||
42a0111b | 290 | pim_upstream_rpf_interface_changed(up, saved.source_nexthop.interface); |
12e41d03 DL |
291 | } |
292 | ||
293 | /* detect change in RPF'(S,G) */ | |
ce2b6ce0 DS |
294 | if (saved.rpf_addr.u.prefix4.s_addr != rpf->rpf_addr.u.prefix4.s_addr || |
295 | saved.source_nexthop.interface != rpf->source_nexthop.interface) | |
296 | { | |
12e41d03 DL |
297 | |
298 | /* return old rpf to caller ? */ | |
42a0111b DS |
299 | if (old) |
300 | { | |
301 | old->source_nexthop = saved.source_nexthop; | |
302 | old->rpf_addr = saved.rpf_addr; | |
303 | } | |
12e41d03 DL |
304 | return PIM_RPF_CHANGED; |
305 | } | |
306 | ||
307 | return PIM_RPF_OK; | |
308 | } | |
309 | ||
310 | /* | |
311 | RFC 4601: 4.1.6. State Summarization Macros | |
312 | ||
313 | neighbor RPF'(S,G) { | |
314 | if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) { | |
315 | return AssertWinner(S, G, RPF_interface(S) ) | |
316 | } else { | |
317 | return NBR( RPF_interface(S), MRIB.next_hop( S ) ) | |
318 | } | |
319 | } | |
320 | ||
321 | RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data | |
322 | packets should be coming and to which joins should be sent on the RP | |
323 | tree and SPT, respectively. | |
324 | */ | |
325 | static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up) | |
326 | { | |
327 | struct pim_ifchannel *rpf_ch; | |
328 | struct pim_neighbor *neigh; | |
329 | struct in_addr rpf_addr; | |
330 | ||
331 | if (!up->rpf.source_nexthop.interface) { | |
05e451f8 | 332 | zlog_warn("%s: missing RPF interface for upstream (S,G)=%s", |
12e41d03 | 333 | __PRETTY_FUNCTION__, |
8bfb8b67 | 334 | up->sg_str); |
12e41d03 DL |
335 | |
336 | rpf_addr.s_addr = PIM_NET_INADDR_ANY; | |
337 | return rpf_addr; | |
338 | } | |
339 | ||
340 | rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface, | |
5074a423 | 341 | &up->sg); |
12e41d03 DL |
342 | if (rpf_ch) { |
343 | if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) { | |
344 | return rpf_ch->ifassert_winner; | |
345 | } | |
346 | } | |
347 | ||
348 | /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */ | |
349 | ||
350 | neigh = pim_if_find_neighbor(up->rpf.source_nexthop.interface, | |
63c59d0c | 351 | up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4); |
12e41d03 DL |
352 | if (neigh) |
353 | rpf_addr = neigh->source_addr; | |
354 | else | |
355 | rpf_addr.s_addr = PIM_NET_INADDR_ANY; | |
356 | ||
357 | return rpf_addr; | |
358 | } | |
63c59d0c DS |
359 | |
360 | int | |
361 | pim_rpf_addr_is_inaddr_none (struct pim_rpf *rpf) | |
362 | { | |
363 | switch (rpf->rpf_addr.family) | |
364 | { | |
fa8da98c | 365 | case AF_INET: |
63c59d0c DS |
366 | return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE; |
367 | break; | |
fa8da98c | 368 | case AF_INET6: |
63c59d0c DS |
369 | zlog_warn ("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__); |
370 | return 1; | |
371 | break; | |
372 | default: | |
373 | return 0; | |
374 | break; | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | int | |
381 | pim_rpf_addr_is_inaddr_any (struct pim_rpf *rpf) | |
382 | { | |
383 | switch (rpf->rpf_addr.family) | |
384 | { | |
fa8da98c | 385 | case AF_INET: |
63c59d0c DS |
386 | return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY; |
387 | break; | |
fa8da98c | 388 | case AF_INET6: |
63c59d0c DS |
389 | zlog_warn ("%s: v6 Unimplmented", __PRETTY_FUNCTION__); |
390 | return 1; | |
391 | break; | |
392 | default: | |
393 | return 0; | |
394 | break; | |
395 | } | |
396 | ||
397 | return 0; | |
398 | } | |
66cc32fa DS |
399 | |
400 | int | |
401 | pim_rpf_is_same (struct pim_rpf *rpf1, struct pim_rpf *rpf2) | |
402 | { | |
403 | if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface) | |
404 | return 1; | |
405 | ||
406 | return 0; | |
407 | } |