]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_rpf.c
pimd: Join/Prune Aggregation
[mirror_frr.git] / pimd / pim_rpf.c
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.
14
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
19 */
20
21 #include <zebra.h>
22
23 #include "if.h"
24
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"
36 #include "pim_time.h"
37
38 static long long last_route_change_time = -1;
39 long long nexthop_lookups_avoided = 0;
40
41 static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up);
42
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
52 int pim_nexthop_lookup(struct pim_nexthop *nexthop, struct in_addr addr, int neighbor_needed)
53 {
54 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
55 struct pim_neighbor *nbr = NULL;
56 int num_ifindex;
57 struct interface *ifp = NULL;
58 ifindex_t first_ifindex = 0;
59 int found = 0;
60 int i = 0;
61
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));
69 zlog_debug ("%s: Using last lookup for %s at %lld, %lld",
70 __PRETTY_FUNCTION__,
71 addr_str,
72 nexthop->last_lookup_time,
73 last_route_change_time);
74 }
75 nexthop_lookups_avoided++;
76 return 0;
77 }
78 else
79 {
80 if (PIM_DEBUG_TRACE)
81 {
82 char addr_str[INET_ADDRSTRLEN];
83 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
84 zlog_debug ("%s: Looking up: %s, last lookup time: %lld, %lld",
85 __PRETTY_FUNCTION__,
86 addr_str,
87 nexthop->last_lookup_time,
88 last_route_change_time);
89 }
90 }
91
92 memset (nexthop_tab, 0, sizeof (struct pim_zlookup_nexthop) * MULTIPATH_NUM);
93 num_ifindex = zclient_lookup_nexthop(nexthop_tab,
94 MULTIPATH_NUM,
95 addr, PIM_NEXTHOP_LOOKUP_MAX);
96 if (num_ifindex < 1) {
97 char addr_str[INET_ADDRSTRLEN];
98 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
99 zlog_warn("%s %s: could not find nexthop ifindex for address %s",
100 __FILE__, __PRETTY_FUNCTION__,
101 addr_str);
102 return -1;
103 }
104
105 while (!found && (i < num_ifindex))
106 {
107 first_ifindex = nexthop_tab[i].ifindex;
108
109 ifp = if_lookup_by_index(first_ifindex);
110 if (!ifp)
111 {
112 if (PIM_DEBUG_ZEBRA)
113 {
114 char addr_str[INET_ADDRSTRLEN];
115 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
116 zlog_debug("%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 {
126 if (PIM_DEBUG_ZEBRA)
127 {
128 char addr_str[INET_ADDRSTRLEN];
129 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
130 zlog_debug("%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)",
131 __PRETTY_FUNCTION__,
132 ifp->name, first_ifindex, addr_str);
133 }
134 i++;
135 }
136 else if (neighbor_needed && !pim_if_connected_to_source (ifp, addr))
137 {
138 nbr = pim_neighbor_find (ifp, nexthop_tab[i].nexthop_addr.u.prefix4);
139 if (PIM_DEBUG_PIM_TRACE_DETAIL)
140 zlog_debug ("ifp name: %s, pim nbr: %p", ifp->name, nbr);
141 if (!nbr && !if_is_loopback (ifp))
142 i++;
143 else
144 found = 1;
145 }
146 else
147 found = 1;
148 }
149
150 if (found)
151 {
152 if (PIM_DEBUG_ZEBRA) {
153 char nexthop_str[PREFIX_STRLEN];
154 char addr_str[INET_ADDRSTRLEN];
155 pim_addr_dump("<nexthop?>", &nexthop_tab[i].nexthop_addr, nexthop_str, sizeof(nexthop_str));
156 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
157 zlog_debug("%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d",
158 __FILE__, __PRETTY_FUNCTION__,
159 nexthop_str, addr_str,
160 ifp->name, first_ifindex,
161 nexthop_tab[i].route_metric,
162 nexthop_tab[i].protocol_distance);
163 }
164 /* update nextop data */
165 nexthop->interface = ifp;
166 nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr;
167 nexthop->mrib_metric_preference = nexthop_tab[i].protocol_distance;
168 nexthop->mrib_route_metric = nexthop_tab[i].route_metric;
169 nexthop->last_lookup = addr;
170 nexthop->last_lookup_time = pim_time_monotonic_usec();
171 nexthop->nbr = nbr;
172 return 0;
173 }
174 else
175 return -1;
176 }
177
178 static int nexthop_mismatch(const struct pim_nexthop *nh1,
179 const struct pim_nexthop *nh2)
180 {
181 return (nh1->interface != nh2->interface) ||
182 (nh1->mrib_nexthop_addr.u.prefix4.s_addr != nh2->mrib_nexthop_addr.u.prefix4.s_addr) ||
183 (nh1->mrib_metric_preference != nh2->mrib_metric_preference) ||
184 (nh1->mrib_route_metric != nh2->mrib_route_metric);
185 }
186
187 enum pim_rpf_result pim_rpf_update(struct pim_upstream *up, struct pim_rpf *old)
188 {
189 struct pim_rpf *rpf = &up->rpf;
190 struct pim_rpf saved;
191
192 saved.source_nexthop = rpf->source_nexthop;
193 saved.rpf_addr = rpf->rpf_addr;
194
195 if (pim_nexthop_lookup(&rpf->source_nexthop,
196 up->upstream_addr,
197 !PIM_UPSTREAM_FLAG_TEST_FHR (up->flags) &&
198 !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP (up->flags))) {
199 return PIM_RPF_FAILURE;
200 }
201
202 rpf->rpf_addr.family = AF_INET;
203 rpf->rpf_addr.u.prefix4 = pim_rpf_find_rpf_addr(up);
204 if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) {
205 /* RPF'(S,G) not found */
206 zlog_debug("%s %s: RPF'%s not found: won't send join upstream",
207 __FILE__, __PRETTY_FUNCTION__,
208 up->sg_str);
209 /* warning only */
210 }
211
212 /* detect change in pim_nexthop */
213 if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) {
214
215 if (PIM_DEBUG_ZEBRA) {
216 char nhaddr_str[PREFIX_STRLEN];
217 pim_addr_dump("<addr?>", &rpf->source_nexthop.mrib_nexthop_addr, nhaddr_str, sizeof(nhaddr_str));
218 zlog_debug("%s %s: (S,G)=%s source nexthop now is: interface=%s address=%s pref=%d metric=%d",
219 __FILE__, __PRETTY_FUNCTION__,
220 up->sg_str,
221 rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>",
222 nhaddr_str,
223 rpf->source_nexthop.mrib_metric_preference,
224 rpf->source_nexthop.mrib_route_metric);
225 }
226
227 pim_upstream_update_join_desired(up);
228 pim_upstream_update_could_assert(up);
229 pim_upstream_update_my_assert_metric(up);
230 }
231
232 /* detect change in RPF_interface(S) */
233 if (saved.source_nexthop.interface != rpf->source_nexthop.interface) {
234
235 if (PIM_DEBUG_ZEBRA) {
236 zlog_debug("%s %s: (S,G)=%s RPF_interface(S) changed from %s to %s",
237 __FILE__, __PRETTY_FUNCTION__,
238 up->sg_str,
239 saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>",
240 rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>");
241 /* warning only */
242 }
243
244 pim_upstream_rpf_interface_changed(up, saved.source_nexthop.interface);
245 }
246
247 /* detect change in RPF'(S,G) */
248 if (saved.rpf_addr.u.prefix4.s_addr != rpf->rpf_addr.u.prefix4.s_addr) {
249
250 /* return old rpf to caller ? */
251 if (old)
252 {
253 old->source_nexthop = saved.source_nexthop;
254 old->rpf_addr = saved.rpf_addr;
255 }
256 return PIM_RPF_CHANGED;
257 }
258
259 return PIM_RPF_OK;
260 }
261
262 /*
263 RFC 4601: 4.1.6. State Summarization Macros
264
265 neighbor RPF'(S,G) {
266 if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) {
267 return AssertWinner(S, G, RPF_interface(S) )
268 } else {
269 return NBR( RPF_interface(S), MRIB.next_hop( S ) )
270 }
271 }
272
273 RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data
274 packets should be coming and to which joins should be sent on the RP
275 tree and SPT, respectively.
276 */
277 static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up)
278 {
279 struct pim_ifchannel *rpf_ch;
280 struct pim_neighbor *neigh;
281 struct in_addr rpf_addr;
282
283 if (!up->rpf.source_nexthop.interface) {
284 zlog_warn("%s: missing RPF interface for upstream (S,G)=%s",
285 __PRETTY_FUNCTION__,
286 up->sg_str);
287
288 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
289 return rpf_addr;
290 }
291
292 rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface,
293 &up->sg);
294 if (rpf_ch) {
295 if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) {
296 return rpf_ch->ifassert_winner;
297 }
298 }
299
300 /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */
301
302 neigh = pim_if_find_neighbor(up->rpf.source_nexthop.interface,
303 up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4);
304 if (neigh)
305 rpf_addr = neigh->source_addr;
306 else
307 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
308
309 return rpf_addr;
310 }
311
312 int
313 pim_rpf_addr_is_inaddr_none (struct pim_rpf *rpf)
314 {
315 switch (rpf->rpf_addr.family)
316 {
317 case AF_INET:
318 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE;
319 break;
320 case AF_INET6:
321 zlog_warn ("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__);
322 return 1;
323 break;
324 default:
325 return 0;
326 break;
327 }
328
329 return 0;
330 }
331
332 int
333 pim_rpf_addr_is_inaddr_any (struct pim_rpf *rpf)
334 {
335 switch (rpf->rpf_addr.family)
336 {
337 case AF_INET:
338 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY;
339 break;
340 case AF_INET6:
341 zlog_warn ("%s: v6 Unimplmented", __PRETTY_FUNCTION__);
342 return 1;
343 break;
344 default:
345 return 0;
346 break;
347 }
348
349 return 0;
350 }
351
352 int
353 pim_rpf_is_same (struct pim_rpf *rpf1, struct pim_rpf *rpf2)
354 {
355 if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface)
356 return 1;
357
358 return 0;
359 }