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