]>
Commit | Line | Data |
---|---|---|
8ef0791c | 1 | /* |
165b5fff JB |
2 | * BGP Multipath |
3 | * Copyright (C) 2010 Google Inc. | |
4 | * | |
5 | * This file is part of Quagga | |
6 | * | |
7 | * Quagga is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * Quagga is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
165b5fff JB |
20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "command.h" | |
96450faf JB |
25 | #include "prefix.h" |
26 | #include "linklist.h" | |
27 | #include "sockunion.h" | |
de8d5dff | 28 | #include "memory.h" |
3f9c7369 | 29 | #include "queue.h" |
039f3a34 | 30 | #include "filter.h" |
165b5fff JB |
31 | |
32 | #include "bgpd/bgpd.h" | |
96450faf JB |
33 | #include "bgpd/bgp_table.h" |
34 | #include "bgpd/bgp_route.h" | |
35 | #include "bgpd/bgp_attr.h" | |
de8d5dff | 36 | #include "bgpd/bgp_debug.h" |
0b597ef0 JB |
37 | #include "bgpd/bgp_aspath.h" |
38 | #include "bgpd/bgp_community.h" | |
39 | #include "bgpd/bgp_ecommunity.h" | |
57d187bc | 40 | #include "bgpd/bgp_lcommunity.h" |
165b5fff JB |
41 | #include "bgpd/bgp_mpath.h" |
42 | ||
43 | /* | |
44 | * bgp_maximum_paths_set | |
45 | * | |
46 | * Record maximum-paths configuration for BGP instance | |
47 | */ | |
d62a17ae | 48 | int bgp_maximum_paths_set(struct bgp *bgp, afi_t afi, safi_t safi, int peertype, |
d7c0a89a | 49 | uint16_t maxpaths, uint16_t options) |
165b5fff | 50 | { |
d62a17ae | 51 | if (!bgp || (afi >= AFI_MAX) || (safi >= SAFI_MAX)) |
52 | return -1; | |
53 | ||
54 | switch (peertype) { | |
55 | case BGP_PEER_IBGP: | |
56 | bgp->maxpaths[afi][safi].maxpaths_ibgp = maxpaths; | |
57 | bgp->maxpaths[afi][safi].ibgp_flags |= options; | |
58 | break; | |
59 | case BGP_PEER_EBGP: | |
60 | bgp->maxpaths[afi][safi].maxpaths_ebgp = maxpaths; | |
61 | break; | |
62 | default: | |
63 | return -1; | |
64 | } | |
65 | ||
66 | return 0; | |
165b5fff JB |
67 | } |
68 | ||
69 | /* | |
70 | * bgp_maximum_paths_unset | |
71 | * | |
72 | * Remove maximum-paths configuration from BGP instance | |
73 | */ | |
d62a17ae | 74 | int bgp_maximum_paths_unset(struct bgp *bgp, afi_t afi, safi_t safi, |
75 | int peertype) | |
165b5fff | 76 | { |
d62a17ae | 77 | if (!bgp || (afi >= AFI_MAX) || (safi >= SAFI_MAX)) |
78 | return -1; | |
79 | ||
80 | switch (peertype) { | |
81 | case BGP_PEER_IBGP: | |
82 | bgp->maxpaths[afi][safi].maxpaths_ibgp = multipath_num; | |
83 | bgp->maxpaths[afi][safi].ibgp_flags = 0; | |
84 | break; | |
85 | case BGP_PEER_EBGP: | |
86 | bgp->maxpaths[afi][safi].maxpaths_ebgp = multipath_num; | |
87 | break; | |
88 | default: | |
89 | return -1; | |
90 | } | |
91 | ||
92 | return 0; | |
165b5fff | 93 | } |
96450faf | 94 | |
194a4f2c DW |
95 | /* |
96 | * bgp_interface_same | |
97 | * | |
98 | * Return true if ifindex for ifp1 and ifp2 are the same, else return false. | |
99 | */ | |
100 | static int bgp_interface_same(struct interface *ifp1, struct interface *ifp2) | |
101 | { | |
102 | if (!ifp1 && !ifp2) | |
103 | return 1; | |
104 | ||
105 | if (!ifp1 && ifp2) | |
106 | return 0; | |
107 | ||
108 | if (ifp1 && !ifp2) | |
109 | return 0; | |
110 | ||
111 | return (ifp1->ifindex == ifp2->ifindex); | |
112 | } | |
113 | ||
114 | ||
96450faf | 115 | /* |
18ee8310 | 116 | * bgp_path_info_nexthop_cmp |
96450faf JB |
117 | * |
118 | * Compare the nexthops of two paths. Return value is less than, equal to, | |
40381db7 DS |
119 | * or greater than zero if bpi1 is respectively less than, equal to, |
120 | * or greater than bpi2. | |
96450faf | 121 | */ |
40381db7 DS |
122 | int bgp_path_info_nexthop_cmp(struct bgp_path_info *bpi1, |
123 | struct bgp_path_info *bpi2) | |
96450faf | 124 | { |
d62a17ae | 125 | int compare; |
05a38c0c | 126 | struct in6_addr addr1, addr2; |
d62a17ae | 127 | |
40381db7 | 128 | compare = IPV4_ADDR_CMP(&bpi1->attr->nexthop, &bpi2->attr->nexthop); |
d62a17ae | 129 | if (!compare) { |
40381db7 DS |
130 | if (bpi1->attr->mp_nexthop_len == bpi2->attr->mp_nexthop_len) { |
131 | switch (bpi1->attr->mp_nexthop_len) { | |
d62a17ae | 132 | case BGP_ATTR_NHLEN_IPV4: |
133 | case BGP_ATTR_NHLEN_VPNV4: | |
134 | compare = IPV4_ADDR_CMP( | |
40381db7 DS |
135 | &bpi1->attr->mp_nexthop_global_in, |
136 | &bpi2->attr->mp_nexthop_global_in); | |
d62a17ae | 137 | break; |
138 | case BGP_ATTR_NHLEN_IPV6_GLOBAL: | |
139 | case BGP_ATTR_NHLEN_VPNV6_GLOBAL: | |
140 | compare = IPV6_ADDR_CMP( | |
40381db7 DS |
141 | &bpi1->attr->mp_nexthop_global, |
142 | &bpi2->attr->mp_nexthop_global); | |
d62a17ae | 143 | break; |
144 | case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL: | |
40381db7 DS |
145 | addr1 = (bpi1->attr->mp_nexthop_prefer_global) |
146 | ? bpi1->attr->mp_nexthop_global | |
147 | : bpi1->attr->mp_nexthop_local; | |
148 | addr2 = (bpi2->attr->mp_nexthop_prefer_global) | |
149 | ? bpi2->attr->mp_nexthop_global | |
150 | : bpi2->attr->mp_nexthop_local; | |
151 | ||
152 | if (!bpi1->attr->mp_nexthop_prefer_global | |
153 | && !bpi2->attr->mp_nexthop_prefer_global) | |
996c9314 | 154 | compare = !bgp_interface_same( |
40381db7 DS |
155 | bpi1->peer->ifp, |
156 | bpi2->peer->ifp); | |
194a4f2c | 157 | |
d62a17ae | 158 | if (!compare) |
05a38c0c | 159 | compare = IPV6_ADDR_CMP(&addr1, &addr2); |
d62a17ae | 160 | break; |
161 | } | |
162 | } | |
163 | ||
164 | /* This can happen if one IPv6 peer sends you global and | |
165 | * link-local | |
166 | * nexthops but another IPv6 peer only sends you global | |
167 | */ | |
40381db7 DS |
168 | else if (bpi1->attr->mp_nexthop_len |
169 | == BGP_ATTR_NHLEN_IPV6_GLOBAL | |
170 | || bpi1->attr->mp_nexthop_len | |
d62a17ae | 171 | == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) { |
40381db7 DS |
172 | compare = IPV6_ADDR_CMP(&bpi1->attr->mp_nexthop_global, |
173 | &bpi2->attr->mp_nexthop_global); | |
d62a17ae | 174 | if (!compare) { |
40381db7 DS |
175 | if (bpi1->attr->mp_nexthop_len |
176 | < bpi2->attr->mp_nexthop_len) | |
d62a17ae | 177 | compare = -1; |
178 | else | |
179 | compare = 1; | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
da0c0ef7 KM |
184 | /* |
185 | * If both nexthops are same then check | |
186 | * if they belong to same VRF | |
187 | */ | |
188 | if (!compare && bpi1->attr->nh_type != NEXTHOP_TYPE_BLACKHOLE) { | |
189 | if (bpi1->extra && bpi1->extra->bgp_orig && bpi2->extra | |
190 | && bpi2->extra->bgp_orig) { | |
191 | if (bpi1->extra->bgp_orig->vrf_id | |
192 | != bpi2->extra->bgp_orig->vrf_id) { | |
193 | compare = 1; | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
d62a17ae | 198 | return compare; |
96450faf JB |
199 | } |
200 | ||
201 | /* | |
18ee8310 | 202 | * bgp_path_info_mpath_cmp |
96450faf JB |
203 | * |
204 | * This function determines our multipath list ordering. By ordering | |
205 | * the list we can deterministically select which paths are included | |
206 | * in the multipath set. The ordering also helps in detecting changes | |
207 | * in the multipath selection so we can detect whether to send an | |
208 | * update to zebra. | |
209 | * | |
210 | * The order of paths is determined first by received nexthop, and then | |
211 | * by peer address if the nexthops are the same. | |
212 | */ | |
18ee8310 | 213 | static int bgp_path_info_mpath_cmp(void *val1, void *val2) |
96450faf | 214 | { |
40381db7 | 215 | struct bgp_path_info *bpi1, *bpi2; |
d62a17ae | 216 | int compare; |
217 | ||
40381db7 DS |
218 | bpi1 = val1; |
219 | bpi2 = val2; | |
d62a17ae | 220 | |
40381db7 | 221 | compare = bgp_path_info_nexthop_cmp(bpi1, bpi2); |
d62a17ae | 222 | |
223 | if (!compare) { | |
40381db7 | 224 | if (!bpi1->peer->su_remote && !bpi2->peer->su_remote) |
d62a17ae | 225 | compare = 0; |
40381db7 | 226 | else if (!bpi1->peer->su_remote) |
d62a17ae | 227 | compare = 1; |
40381db7 | 228 | else if (!bpi2->peer->su_remote) |
d62a17ae | 229 | compare = -1; |
230 | else | |
40381db7 DS |
231 | compare = sockunion_cmp(bpi1->peer->su_remote, |
232 | bpi2->peer->su_remote); | |
d62a17ae | 233 | } |
234 | ||
235 | return compare; | |
96450faf JB |
236 | } |
237 | ||
238 | /* | |
239 | * bgp_mp_list_init | |
240 | * | |
241 | * Initialize the mp_list, which holds the list of multipaths | |
242 | * selected by bgp_best_selection | |
243 | */ | |
d62a17ae | 244 | void bgp_mp_list_init(struct list *mp_list) |
96450faf | 245 | { |
d62a17ae | 246 | assert(mp_list); |
247 | memset(mp_list, 0, sizeof(struct list)); | |
18ee8310 | 248 | mp_list->cmp = bgp_path_info_mpath_cmp; |
96450faf JB |
249 | } |
250 | ||
251 | /* | |
252 | * bgp_mp_list_clear | |
253 | * | |
254 | * Clears all entries out of the mp_list | |
255 | */ | |
d62a17ae | 256 | void bgp_mp_list_clear(struct list *mp_list) |
96450faf | 257 | { |
d62a17ae | 258 | assert(mp_list); |
259 | list_delete_all_node(mp_list); | |
96450faf JB |
260 | } |
261 | ||
262 | /* | |
263 | * bgp_mp_list_add | |
264 | * | |
265 | * Adds a multipath entry to the mp_list | |
266 | */ | |
4b7e6066 | 267 | void bgp_mp_list_add(struct list *mp_list, struct bgp_path_info *mpinfo) |
96450faf | 268 | { |
d62a17ae | 269 | assert(mp_list && mpinfo); |
270 | listnode_add_sort(mp_list, mpinfo); | |
96450faf | 271 | } |
de8d5dff JB |
272 | |
273 | /* | |
18ee8310 | 274 | * bgp_path_info_mpath_new |
de8d5dff | 275 | * |
18ee8310 | 276 | * Allocate and zero memory for a new bgp_path_info_mpath element |
de8d5dff | 277 | */ |
18ee8310 | 278 | static struct bgp_path_info_mpath *bgp_path_info_mpath_new(void) |
de8d5dff | 279 | { |
4b7e6066 DS |
280 | struct bgp_path_info_mpath *new_mpath; |
281 | new_mpath = XCALLOC(MTYPE_BGP_MPATH_INFO, | |
282 | sizeof(struct bgp_path_info_mpath)); | |
d62a17ae | 283 | return new_mpath; |
de8d5dff JB |
284 | } |
285 | ||
286 | /* | |
18ee8310 | 287 | * bgp_path_info_mpath_free |
de8d5dff | 288 | * |
18ee8310 | 289 | * Release resources for a bgp_path_info_mpath element and zero out pointer |
de8d5dff | 290 | */ |
18ee8310 | 291 | void bgp_path_info_mpath_free(struct bgp_path_info_mpath **mpath) |
de8d5dff | 292 | { |
d62a17ae | 293 | if (mpath && *mpath) { |
294 | if ((*mpath)->mp_attr) | |
295 | bgp_attr_unintern(&(*mpath)->mp_attr); | |
296 | XFREE(MTYPE_BGP_MPATH_INFO, *mpath); | |
d62a17ae | 297 | } |
de8d5dff JB |
298 | } |
299 | ||
300 | /* | |
18ee8310 | 301 | * bgp_path_info_mpath_get |
de8d5dff | 302 | * |
18ee8310 | 303 | * Fetch the mpath element for the given bgp_path_info. Used for |
de8d5dff JB |
304 | * doing lazy allocation. |
305 | */ | |
4b7e6066 | 306 | static struct bgp_path_info_mpath * |
9b6d8fcf | 307 | bgp_path_info_mpath_get(struct bgp_path_info *path) |
de8d5dff | 308 | { |
4b7e6066 | 309 | struct bgp_path_info_mpath *mpath; |
fa3bf3a2 MS |
310 | |
311 | if (!path) | |
312 | return NULL; | |
313 | ||
9b6d8fcf | 314 | if (!path->mpath) { |
18ee8310 | 315 | mpath = bgp_path_info_mpath_new(); |
d62a17ae | 316 | if (!mpath) |
317 | return NULL; | |
9b6d8fcf DS |
318 | path->mpath = mpath; |
319 | mpath->mp_info = path; | |
d62a17ae | 320 | } |
9b6d8fcf | 321 | return path->mpath; |
de8d5dff JB |
322 | } |
323 | ||
324 | /* | |
18ee8310 | 325 | * bgp_path_info_mpath_enqueue |
de8d5dff JB |
326 | * |
327 | * Enqueue a path onto the multipath list given the previous multipath | |
328 | * list entry | |
329 | */ | |
18ee8310 | 330 | static void bgp_path_info_mpath_enqueue(struct bgp_path_info *prev_info, |
9b6d8fcf | 331 | struct bgp_path_info *path) |
de8d5dff | 332 | { |
4b7e6066 | 333 | struct bgp_path_info_mpath *prev, *mpath; |
de8d5dff | 334 | |
18ee8310 | 335 | prev = bgp_path_info_mpath_get(prev_info); |
9b6d8fcf | 336 | mpath = bgp_path_info_mpath_get(path); |
d62a17ae | 337 | if (!prev || !mpath) |
338 | return; | |
de8d5dff | 339 | |
d62a17ae | 340 | mpath->mp_next = prev->mp_next; |
341 | mpath->mp_prev = prev; | |
342 | if (prev->mp_next) | |
343 | prev->mp_next->mp_prev = mpath; | |
344 | prev->mp_next = mpath; | |
de8d5dff | 345 | |
9b6d8fcf | 346 | SET_FLAG(path->flags, BGP_PATH_MULTIPATH); |
de8d5dff JB |
347 | } |
348 | ||
349 | /* | |
18ee8310 | 350 | * bgp_path_info_mpath_dequeue |
de8d5dff JB |
351 | * |
352 | * Remove a path from the multipath list | |
353 | */ | |
9b6d8fcf | 354 | void bgp_path_info_mpath_dequeue(struct bgp_path_info *path) |
de8d5dff | 355 | { |
9b6d8fcf | 356 | struct bgp_path_info_mpath *mpath = path->mpath; |
d62a17ae | 357 | if (!mpath) |
358 | return; | |
359 | if (mpath->mp_prev) | |
360 | mpath->mp_prev->mp_next = mpath->mp_next; | |
361 | if (mpath->mp_next) | |
362 | mpath->mp_next->mp_prev = mpath->mp_prev; | |
363 | mpath->mp_next = mpath->mp_prev = NULL; | |
9b6d8fcf | 364 | UNSET_FLAG(path->flags, BGP_PATH_MULTIPATH); |
de8d5dff JB |
365 | } |
366 | ||
367 | /* | |
18ee8310 | 368 | * bgp_path_info_mpath_next |
de8d5dff | 369 | * |
18ee8310 | 370 | * Given a bgp_path_info, return the next multipath entry |
de8d5dff | 371 | */ |
9b6d8fcf | 372 | struct bgp_path_info *bgp_path_info_mpath_next(struct bgp_path_info *path) |
de8d5dff | 373 | { |
9b6d8fcf | 374 | if (!path->mpath || !path->mpath->mp_next) |
d62a17ae | 375 | return NULL; |
9b6d8fcf | 376 | return path->mpath->mp_next->mp_info; |
de8d5dff JB |
377 | } |
378 | ||
379 | /* | |
18ee8310 | 380 | * bgp_path_info_mpath_first |
de8d5dff | 381 | * |
18ee8310 | 382 | * Given bestpath bgp_path_info, return the first multipath entry. |
de8d5dff | 383 | */ |
9b6d8fcf | 384 | struct bgp_path_info *bgp_path_info_mpath_first(struct bgp_path_info *path) |
de8d5dff | 385 | { |
9b6d8fcf | 386 | return bgp_path_info_mpath_next(path); |
de8d5dff JB |
387 | } |
388 | ||
389 | /* | |
18ee8310 | 390 | * bgp_path_info_mpath_count |
de8d5dff | 391 | * |
18ee8310 | 392 | * Given the bestpath bgp_path_info, return the number of multipath entries |
de8d5dff | 393 | */ |
9b6d8fcf | 394 | uint32_t bgp_path_info_mpath_count(struct bgp_path_info *path) |
de8d5dff | 395 | { |
9b6d8fcf | 396 | if (!path->mpath) |
d62a17ae | 397 | return 0; |
9b6d8fcf | 398 | return path->mpath->mp_count; |
de8d5dff JB |
399 | } |
400 | ||
401 | /* | |
18ee8310 | 402 | * bgp_path_info_mpath_count_set |
de8d5dff JB |
403 | * |
404 | * Sets the count of multipaths into bestpath's mpath element | |
405 | */ | |
9b6d8fcf | 406 | static void bgp_path_info_mpath_count_set(struct bgp_path_info *path, |
50346a98 | 407 | uint16_t count) |
de8d5dff | 408 | { |
4b7e6066 | 409 | struct bgp_path_info_mpath *mpath; |
9b6d8fcf | 410 | if (!count && !path->mpath) |
d62a17ae | 411 | return; |
9b6d8fcf | 412 | mpath = bgp_path_info_mpath_get(path); |
d62a17ae | 413 | if (!mpath) |
414 | return; | |
415 | mpath->mp_count = count; | |
de8d5dff JB |
416 | } |
417 | ||
42d436cf | 418 | /* |
419 | * bgp_path_info_mpath_lb_update | |
420 | * | |
421 | * Update cumulative info related to link-bandwidth | |
422 | */ | |
423 | static void bgp_path_info_mpath_lb_update(struct bgp_path_info *path, bool set, | |
424 | bool all_paths_lb, uint64_t cum_bw) | |
425 | { | |
426 | struct bgp_path_info_mpath *mpath; | |
427 | ||
4953391b DA |
428 | mpath = path->mpath; |
429 | if (mpath == NULL) { | |
1ba8b2a8 | 430 | if (!set || (cum_bw == 0 && !all_paths_lb)) |
42d436cf | 431 | return; |
1ba8b2a8 | 432 | |
42d436cf | 433 | mpath = bgp_path_info_mpath_get(path); |
434 | if (!mpath) | |
435 | return; | |
436 | } | |
437 | if (set) { | |
f7e1c681 | 438 | if (cum_bw) |
439 | SET_FLAG(mpath->mp_flags, BGP_MP_LB_PRESENT); | |
440 | else | |
441 | UNSET_FLAG(mpath->mp_flags, BGP_MP_LB_PRESENT); | |
42d436cf | 442 | if (all_paths_lb) |
443 | SET_FLAG(mpath->mp_flags, BGP_MP_LB_ALL); | |
444 | else | |
445 | UNSET_FLAG(mpath->mp_flags, BGP_MP_LB_ALL); | |
446 | mpath->cum_bw = cum_bw; | |
447 | } else { | |
448 | mpath->mp_flags = 0; | |
449 | mpath->cum_bw = 0; | |
450 | } | |
451 | } | |
452 | ||
0b597ef0 | 453 | /* |
18ee8310 | 454 | * bgp_path_info_mpath_attr |
0b597ef0 | 455 | * |
18ee8310 | 456 | * Given bestpath bgp_path_info, return aggregated attribute set used |
0b597ef0 JB |
457 | * for advertising the multipath route |
458 | */ | |
9b6d8fcf | 459 | struct attr *bgp_path_info_mpath_attr(struct bgp_path_info *path) |
0b597ef0 | 460 | { |
9b6d8fcf | 461 | if (!path->mpath) |
d62a17ae | 462 | return NULL; |
9b6d8fcf | 463 | return path->mpath->mp_attr; |
0b597ef0 JB |
464 | } |
465 | ||
4e30bc2b | 466 | /* |
467 | * bgp_path_info_chkwtd | |
468 | * | |
f7e1c681 | 469 | * Return if we should attempt to do weighted ECMP or not |
470 | * The path passed in is the bestpath. | |
4e30bc2b | 471 | */ |
f7e1c681 | 472 | bool bgp_path_info_mpath_chkwtd(struct bgp *bgp, struct bgp_path_info *path) |
4e30bc2b | 473 | { |
f7e1c681 | 474 | /* Check if told to ignore weights or not multipath */ |
475 | if (bgp->lb_handling == BGP_LINK_BW_IGNORE_BW || !path->mpath) | |
4e30bc2b | 476 | return false; |
f7e1c681 | 477 | |
478 | /* All paths in multipath should have associated weight (bandwidth) | |
479 | * unless told explicitly otherwise. | |
480 | */ | |
481 | if (bgp->lb_handling != BGP_LINK_BW_SKIP_MISSING && | |
482 | bgp->lb_handling != BGP_LINK_BW_DEFWT_4_MISSING) | |
483 | return (path->mpath->mp_flags & BGP_MP_LB_ALL); | |
484 | ||
485 | /* At least one path should have bandwidth. */ | |
486 | return (path->mpath->mp_flags & BGP_MP_LB_PRESENT); | |
4e30bc2b | 487 | } |
488 | ||
489 | /* | |
490 | * bgp_path_info_mpath_attr | |
491 | * | |
492 | * Given bestpath bgp_path_info, return cumulative bandwidth | |
493 | * computed for all multipaths with bandwidth info | |
494 | */ | |
495 | uint64_t bgp_path_info_mpath_cumbw(struct bgp_path_info *path) | |
496 | { | |
497 | if (!path->mpath) | |
498 | return 0; | |
499 | return path->mpath->cum_bw; | |
500 | } | |
501 | ||
0b597ef0 | 502 | /* |
18ee8310 | 503 | * bgp_path_info_mpath_attr_set |
0b597ef0 JB |
504 | * |
505 | * Sets the aggregated attribute into bestpath's mpath element | |
506 | */ | |
9b6d8fcf | 507 | static void bgp_path_info_mpath_attr_set(struct bgp_path_info *path, |
18ee8310 | 508 | struct attr *attr) |
0b597ef0 | 509 | { |
4b7e6066 | 510 | struct bgp_path_info_mpath *mpath; |
9b6d8fcf | 511 | if (!attr && !path->mpath) |
d62a17ae | 512 | return; |
9b6d8fcf | 513 | mpath = bgp_path_info_mpath_get(path); |
d62a17ae | 514 | if (!mpath) |
515 | return; | |
516 | mpath->mp_attr = attr; | |
0b597ef0 JB |
517 | } |
518 | ||
de8d5dff | 519 | /* |
18ee8310 | 520 | * bgp_path_info_mpath_update |
de8d5dff JB |
521 | * |
522 | * Compare and sync up the multipath list with the mp_list generated by | |
523 | * bgp_best_selection | |
524 | */ | |
4378495a | 525 | void bgp_path_info_mpath_update(struct bgp *bgp, struct bgp_dest *dest, |
18ee8310 DS |
526 | struct bgp_path_info *new_best, |
527 | struct bgp_path_info *old_best, | |
528 | struct list *mp_list, | |
529 | struct bgp_maxpaths_cfg *mpath_cfg) | |
de8d5dff | 530 | { |
d7c0a89a | 531 | uint16_t maxpaths, mpath_count, old_mpath_count; |
42d436cf | 532 | uint32_t bwval; |
b1875e65 | 533 | uint64_t cum_bw, old_cum_bw; |
d62a17ae | 534 | struct listnode *mp_node, *mp_next_node; |
4b7e6066 | 535 | struct bgp_path_info *cur_mpath, *new_mpath, *next_mpath, *prev_mpath; |
d62a17ae | 536 | int mpath_changed, debug; |
42d436cf | 537 | bool all_paths_lb; |
d62a17ae | 538 | char path_buf[PATH_ADDPATH_STR_BUFFER]; |
539 | ||
540 | mpath_changed = 0; | |
541 | maxpaths = multipath_num; | |
542 | mpath_count = 0; | |
543 | cur_mpath = NULL; | |
544 | old_mpath_count = 0; | |
b1875e65 | 545 | old_cum_bw = cum_bw = 0; |
d62a17ae | 546 | prev_mpath = new_best; |
547 | mp_node = listhead(mp_list); | |
9bcb3eef | 548 | debug = bgp_debug_bestpath(dest); |
d62a17ae | 549 | |
d62a17ae | 550 | if (new_best) { |
551 | mpath_count++; | |
552 | if (new_best != old_best) | |
18ee8310 | 553 | bgp_path_info_mpath_dequeue(new_best); |
d62a17ae | 554 | maxpaths = (new_best->peer->sort == BGP_PEER_IBGP) |
555 | ? mpath_cfg->maxpaths_ibgp | |
556 | : mpath_cfg->maxpaths_ebgp; | |
557 | } | |
558 | ||
559 | if (old_best) { | |
18ee8310 DS |
560 | cur_mpath = bgp_path_info_mpath_first(old_best); |
561 | old_mpath_count = bgp_path_info_mpath_count(old_best); | |
b1875e65 | 562 | old_cum_bw = bgp_path_info_mpath_cumbw(old_best); |
18ee8310 | 563 | bgp_path_info_mpath_count_set(old_best, 0); |
42d436cf | 564 | bgp_path_info_mpath_lb_update(old_best, false, false, 0); |
18ee8310 | 565 | bgp_path_info_mpath_dequeue(old_best); |
d62a17ae | 566 | } |
567 | ||
568 | if (debug) | |
569 | zlog_debug( | |
4378495a DS |
570 | "%pRN(%s): starting mpath update, newbest %s num candidates %d old-mpath-count %d old-cum-bw u%" PRIu64, |
571 | bgp_dest_to_rnode(dest), bgp->name_pretty, | |
9bcb3eef | 572 | new_best ? new_best->peer->host : "NONE", |
4378495a DS |
573 | mp_list ? listcount(mp_list) : 0, old_mpath_count, |
574 | old_cum_bw); | |
d62a17ae | 575 | |
576 | /* | |
577 | * We perform an ordered walk through both lists in parallel. | |
578 | * The reason for the ordered walk is that if there are paths | |
579 | * that were previously multipaths and are still multipaths, the walk | |
580 | * should encounter them in both lists at the same time. Otherwise | |
581 | * there will be paths that are in one list or another, and we | |
582 | * will deal with these separately. | |
583 | * | |
584 | * Note that new_best might be somewhere in the mp_list, so we need | |
585 | * to skip over it | |
586 | */ | |
42d436cf | 587 | all_paths_lb = true; /* We'll reset if any path doesn't have LB. */ |
d62a17ae | 588 | while (mp_node || cur_mpath) { |
4b7e6066 | 589 | struct bgp_path_info *tmp_info; |
d62a17ae | 590 | |
591 | /* | |
592 | * We can bail out of this loop if all existing paths on the | |
593 | * multipath list have been visited (for cleanup purposes) and | |
594 | * the maxpath requirement is fulfulled | |
595 | */ | |
596 | if (!cur_mpath && (mpath_count >= maxpaths)) | |
597 | break; | |
598 | ||
599 | mp_next_node = mp_node ? listnextnode(mp_node) : NULL; | |
18ee8310 DS |
600 | next_mpath = |
601 | cur_mpath ? bgp_path_info_mpath_next(cur_mpath) : NULL; | |
d62a17ae | 602 | tmp_info = mp_node ? listgetdata(mp_node) : NULL; |
603 | ||
604 | if (debug) | |
605 | zlog_debug( | |
4378495a DS |
606 | "%pRN(%s): comparing candidate %s with existing mpath %s", |
607 | bgp_dest_to_rnode(dest), bgp->name_pretty, | |
d62a17ae | 608 | tmp_info ? tmp_info->peer->host : "NONE", |
609 | cur_mpath ? cur_mpath->peer->host : "NONE"); | |
610 | ||
611 | /* | |
612 | * If equal, the path was a multipath and is still a multipath. | |
613 | * Insert onto new multipath list if maxpaths allows. | |
614 | */ | |
615 | if (mp_node && (listgetdata(mp_node) == cur_mpath)) { | |
616 | list_delete_node(mp_list, mp_node); | |
18ee8310 | 617 | bgp_path_info_mpath_dequeue(cur_mpath); |
d62a17ae | 618 | if ((mpath_count < maxpaths) |
fa3bf3a2 | 619 | && prev_mpath |
18ee8310 DS |
620 | && bgp_path_info_nexthop_cmp(prev_mpath, |
621 | cur_mpath)) { | |
622 | bgp_path_info_mpath_enqueue(prev_mpath, | |
623 | cur_mpath); | |
d62a17ae | 624 | prev_mpath = cur_mpath; |
625 | mpath_count++; | |
42d436cf | 626 | if (ecommunity_linkbw_present( |
b53e67a3 DA |
627 | bgp_attr_get_ecommunity( |
628 | cur_mpath->attr), | |
27aa23a4 | 629 | &bwval)) |
42d436cf | 630 | cum_bw += bwval; |
631 | else | |
632 | all_paths_lb = false; | |
d62a17ae | 633 | if (debug) { |
18ee8310 | 634 | bgp_path_info_path_with_addpath_rx_str( |
7533cad7 QY |
635 | cur_mpath, path_buf, |
636 | sizeof(path_buf)); | |
d62a17ae | 637 | zlog_debug( |
b54892e0 | 638 | "%pRN: %s is still multipath, cur count %d", |
9bcb3eef DS |
639 | bgp_dest_to_rnode(dest), |
640 | path_buf, mpath_count); | |
d62a17ae | 641 | } |
642 | } else { | |
643 | mpath_changed = 1; | |
644 | if (debug) { | |
18ee8310 | 645 | bgp_path_info_path_with_addpath_rx_str( |
7533cad7 QY |
646 | cur_mpath, path_buf, |
647 | sizeof(path_buf)); | |
d62a17ae | 648 | zlog_debug( |
c0d72166 | 649 | "%pRN: remove mpath %s nexthop %pI4, cur count %d", |
9bcb3eef DS |
650 | bgp_dest_to_rnode(dest), |
651 | path_buf, | |
c0d72166 | 652 | &cur_mpath->attr->nexthop, |
d62a17ae | 653 | mpath_count); |
654 | } | |
655 | } | |
656 | mp_node = mp_next_node; | |
657 | cur_mpath = next_mpath; | |
658 | continue; | |
659 | } | |
660 | ||
661 | if (cur_mpath | |
662 | && (!mp_node | |
18ee8310 DS |
663 | || (bgp_path_info_mpath_cmp(cur_mpath, |
664 | listgetdata(mp_node)) | |
d62a17ae | 665 | < 0))) { |
666 | /* | |
667 | * If here, we have an old multipath and either the | |
668 | * mp_list | |
669 | * is finished or the next mp_node points to a later | |
670 | * multipath, so we need to purge this path from the | |
671 | * multipath list | |
672 | */ | |
18ee8310 | 673 | bgp_path_info_mpath_dequeue(cur_mpath); |
d62a17ae | 674 | mpath_changed = 1; |
675 | if (debug) { | |
18ee8310 | 676 | bgp_path_info_path_with_addpath_rx_str( |
7533cad7 | 677 | cur_mpath, path_buf, sizeof(path_buf)); |
d62a17ae | 678 | zlog_debug( |
c0d72166 | 679 | "%pRN: remove mpath %s nexthop %pI4, cur count %d", |
9bcb3eef | 680 | bgp_dest_to_rnode(dest), path_buf, |
c0d72166 | 681 | &cur_mpath->attr->nexthop, mpath_count); |
d62a17ae | 682 | } |
683 | cur_mpath = next_mpath; | |
684 | } else { | |
685 | /* | |
686 | * If here, we have a path on the mp_list that was not | |
687 | * previously | |
688 | * a multipath (due to non-equivalance or maxpaths | |
689 | * exceeded), | |
690 | * or the matching multipath is sorted later in the | |
691 | * multipath | |
692 | * list. Before we enqueue the path on the new multipath | |
693 | * list, | |
694 | * make sure its not on the old_best multipath list or | |
695 | * referenced | |
696 | * via next_mpath: | |
697 | * - If next_mpath points to this new path, update | |
698 | * next_mpath to | |
699 | * point to the multipath after this one | |
700 | * - Dequeue the path from the multipath list just to | |
701 | * make sure | |
702 | */ | |
703 | new_mpath = listgetdata(mp_node); | |
704 | list_delete_node(mp_list, mp_node); | |
2165e2ea QY |
705 | assert(new_mpath); |
706 | assert(prev_mpath); | |
d62a17ae | 707 | if ((mpath_count < maxpaths) && (new_mpath != new_best) |
18ee8310 DS |
708 | && bgp_path_info_nexthop_cmp(prev_mpath, |
709 | new_mpath)) { | |
18ee8310 | 710 | bgp_path_info_mpath_dequeue(new_mpath); |
d62a17ae | 711 | |
18ee8310 DS |
712 | bgp_path_info_mpath_enqueue(prev_mpath, |
713 | new_mpath); | |
d62a17ae | 714 | prev_mpath = new_mpath; |
715 | mpath_changed = 1; | |
716 | mpath_count++; | |
42d436cf | 717 | if (ecommunity_linkbw_present( |
b53e67a3 DA |
718 | bgp_attr_get_ecommunity( |
719 | new_mpath->attr), | |
27aa23a4 | 720 | &bwval)) |
42d436cf | 721 | cum_bw += bwval; |
722 | else | |
723 | all_paths_lb = false; | |
d62a17ae | 724 | if (debug) { |
18ee8310 | 725 | bgp_path_info_path_with_addpath_rx_str( |
7533cad7 QY |
726 | new_mpath, path_buf, |
727 | sizeof(path_buf)); | |
d62a17ae | 728 | zlog_debug( |
c0d72166 | 729 | "%pRN: add mpath %s nexthop %pI4, cur count %d", |
9bcb3eef DS |
730 | bgp_dest_to_rnode(dest), |
731 | path_buf, | |
c0d72166 | 732 | &new_mpath->attr->nexthop, |
d62a17ae | 733 | mpath_count); |
734 | } | |
735 | } | |
736 | mp_node = mp_next_node; | |
737 | } | |
738 | } | |
739 | ||
740 | if (new_best) { | |
42d436cf | 741 | bgp_path_info_mpath_count_set(new_best, mpath_count - 1); |
b53e67a3 DA |
742 | if (mpath_count <= 1 || |
743 | !ecommunity_linkbw_present( | |
744 | bgp_attr_get_ecommunity(new_best->attr), &bwval)) | |
42d436cf | 745 | all_paths_lb = false; |
746 | else | |
747 | cum_bw += bwval; | |
748 | bgp_path_info_mpath_lb_update(new_best, true, | |
749 | all_paths_lb, cum_bw); | |
750 | ||
d62a17ae | 751 | if (debug) |
752 | zlog_debug( | |
4378495a DS |
753 | "%pRN(%s): New mpath count (incl newbest) %d mpath-change %s all_paths_lb %d cum_bw u%" PRIu64, |
754 | bgp_dest_to_rnode(dest), bgp->name_pretty, | |
755 | mpath_count, mpath_changed ? "YES" : "NO", | |
42d436cf | 756 | all_paths_lb, cum_bw); |
d62a17ae | 757 | |
d62a17ae | 758 | if (mpath_changed |
18ee8310 | 759 | || (bgp_path_info_mpath_count(new_best) != old_mpath_count)) |
1defdda8 | 760 | SET_FLAG(new_best->flags, BGP_PATH_MULTIPATH_CHG); |
b1875e65 | 761 | if ((mpath_count - 1) != old_mpath_count || |
762 | old_cum_bw != cum_bw) | |
763 | SET_FLAG(new_best->flags, BGP_PATH_LINK_BW_CHG); | |
d62a17ae | 764 | } |
de8d5dff | 765 | } |
6918e74b JB |
766 | |
767 | /* | |
768 | * bgp_mp_dmed_deselect | |
769 | * | |
1defdda8 | 770 | * Clean up multipath information for BGP_PATH_DMED_SELECTED path that |
6918e74b JB |
771 | * is not selected as best path |
772 | */ | |
4b7e6066 | 773 | void bgp_mp_dmed_deselect(struct bgp_path_info *dmed_best) |
6918e74b | 774 | { |
4b7e6066 | 775 | struct bgp_path_info *mpinfo, *mpnext; |
6918e74b | 776 | |
d62a17ae | 777 | if (!dmed_best) |
778 | return; | |
6918e74b | 779 | |
18ee8310 | 780 | for (mpinfo = bgp_path_info_mpath_first(dmed_best); mpinfo; |
d62a17ae | 781 | mpinfo = mpnext) { |
18ee8310 DS |
782 | mpnext = bgp_path_info_mpath_next(mpinfo); |
783 | bgp_path_info_mpath_dequeue(mpinfo); | |
d62a17ae | 784 | } |
6918e74b | 785 | |
18ee8310 | 786 | bgp_path_info_mpath_count_set(dmed_best, 0); |
1defdda8 | 787 | UNSET_FLAG(dmed_best->flags, BGP_PATH_MULTIPATH_CHG); |
b1875e65 | 788 | UNSET_FLAG(dmed_best->flags, BGP_PATH_LINK_BW_CHG); |
831600c3 | 789 | assert(bgp_path_info_mpath_first(dmed_best) == NULL); |
6918e74b | 790 | } |
0b597ef0 JB |
791 | |
792 | /* | |
18ee8310 | 793 | * bgp_path_info_mpath_aggregate_update |
0b597ef0 JB |
794 | * |
795 | * Set the multipath aggregate attribute. We need to see if the | |
796 | * aggregate has changed and then set the ATTR_CHANGED flag on the | |
797 | * bestpath info so that a peer update will be generated. The | |
798 | * change is detected by generating the current attribute, | |
799 | * interning it, and then comparing the interned pointer with the | |
800 | * current value. We can skip this generate/compare step if there | |
801 | * is no change in multipath selection and no attribute change in | |
802 | * any multipath. | |
803 | */ | |
18ee8310 DS |
804 | void bgp_path_info_mpath_aggregate_update(struct bgp_path_info *new_best, |
805 | struct bgp_path_info *old_best) | |
0b597ef0 | 806 | { |
4b7e6066 | 807 | struct bgp_path_info *mpinfo; |
d62a17ae | 808 | struct aspath *aspath; |
809 | struct aspath *asmerge; | |
810 | struct attr *new_attr, *old_attr; | |
d7c0a89a | 811 | uint8_t origin; |
d62a17ae | 812 | struct community *community, *commerge; |
813 | struct ecommunity *ecomm, *ecommerge; | |
814 | struct lcommunity *lcomm, *lcommerge; | |
815 | struct attr attr = {0}; | |
816 | ||
817 | if (old_best && (old_best != new_best) | |
18ee8310 | 818 | && (old_attr = bgp_path_info_mpath_attr(old_best))) { |
d62a17ae | 819 | bgp_attr_unintern(&old_attr); |
18ee8310 | 820 | bgp_path_info_mpath_attr_set(old_best, NULL); |
d62a17ae | 821 | } |
822 | ||
823 | if (!new_best) | |
824 | return; | |
825 | ||
18ee8310 DS |
826 | if (!bgp_path_info_mpath_count(new_best)) { |
827 | if ((new_attr = bgp_path_info_mpath_attr(new_best))) { | |
d62a17ae | 828 | bgp_attr_unintern(&new_attr); |
18ee8310 | 829 | bgp_path_info_mpath_attr_set(new_best, NULL); |
1defdda8 | 830 | SET_FLAG(new_best->flags, BGP_PATH_ATTR_CHANGED); |
d62a17ae | 831 | } |
832 | return; | |
833 | } | |
834 | ||
6f4f49b2 | 835 | attr = *new_best->attr; |
d62a17ae | 836 | |
892fedb6 DA |
837 | if (new_best->peer |
838 | && CHECK_FLAG(new_best->peer->bgp->flags, | |
839 | BGP_FLAG_MULTIPATH_RELAX_AS_SET)) { | |
d62a17ae | 840 | |
841 | /* aggregate attribute from multipath constituents */ | |
842 | aspath = aspath_dup(attr.aspath); | |
843 | origin = attr.origin; | |
844 | community = | |
845 | attr.community ? community_dup(attr.community) : NULL; | |
b53e67a3 DA |
846 | ecomm = (bgp_attr_get_ecommunity(&attr)) |
847 | ? ecommunity_dup(bgp_attr_get_ecommunity(&attr)) | |
848 | : NULL; | |
1bcf3a96 DA |
849 | lcomm = (bgp_attr_get_lcommunity(&attr)) |
850 | ? lcommunity_dup(bgp_attr_get_lcommunity(&attr)) | |
851 | : NULL; | |
d62a17ae | 852 | |
18ee8310 DS |
853 | for (mpinfo = bgp_path_info_mpath_first(new_best); mpinfo; |
854 | mpinfo = bgp_path_info_mpath_next(mpinfo)) { | |
d62a17ae | 855 | asmerge = |
856 | aspath_aggregate(aspath, mpinfo->attr->aspath); | |
857 | aspath_free(aspath); | |
858 | aspath = asmerge; | |
859 | ||
860 | if (origin < mpinfo->attr->origin) | |
861 | origin = mpinfo->attr->origin; | |
862 | ||
863 | if (mpinfo->attr->community) { | |
864 | if (community) { | |
865 | commerge = community_merge( | |
866 | community, | |
867 | mpinfo->attr->community); | |
868 | community = | |
869 | community_uniq_sort(commerge); | |
3c1f53de | 870 | community_free(&commerge); |
d62a17ae | 871 | } else |
872 | community = community_dup( | |
873 | mpinfo->attr->community); | |
874 | } | |
875 | ||
b53e67a3 | 876 | if (bgp_attr_get_ecommunity(mpinfo->attr)) { |
d62a17ae | 877 | if (ecomm) { |
878 | ecommerge = ecommunity_merge( | |
b53e67a3 DA |
879 | ecomm, bgp_attr_get_ecommunity( |
880 | mpinfo->attr)); | |
d62a17ae | 881 | ecomm = ecommunity_uniq_sort(ecommerge); |
882 | ecommunity_free(&ecommerge); | |
883 | } else | |
884 | ecomm = ecommunity_dup( | |
b53e67a3 DA |
885 | bgp_attr_get_ecommunity( |
886 | mpinfo->attr)); | |
d62a17ae | 887 | } |
1bcf3a96 | 888 | if (bgp_attr_get_lcommunity(mpinfo->attr)) { |
d62a17ae | 889 | if (lcomm) { |
890 | lcommerge = lcommunity_merge( | |
1bcf3a96 DA |
891 | lcomm, bgp_attr_get_lcommunity( |
892 | mpinfo->attr)); | |
d62a17ae | 893 | lcomm = lcommunity_uniq_sort(lcommerge); |
894 | lcommunity_free(&lcommerge); | |
895 | } else | |
896 | lcomm = lcommunity_dup( | |
1bcf3a96 DA |
897 | bgp_attr_get_lcommunity( |
898 | mpinfo->attr)); | |
d62a17ae | 899 | } |
900 | } | |
901 | ||
902 | attr.aspath = aspath; | |
903 | attr.origin = origin; | |
904 | if (community) { | |
905 | attr.community = community; | |
906 | attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES); | |
907 | } | |
908 | if (ecomm) { | |
b53e67a3 | 909 | bgp_attr_set_ecommunity(&attr, ecomm); |
d62a17ae | 910 | attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES); |
911 | } | |
44f12f20 | 912 | if (lcomm) { |
1bcf3a96 | 913 | bgp_attr_set_lcommunity(&attr, lcomm); |
44f12f20 RW |
914 | attr.flag |= ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES); |
915 | } | |
d62a17ae | 916 | |
917 | /* Zap multipath attr nexthop so we set nexthop to self */ | |
975a328e | 918 | attr.nexthop.s_addr = INADDR_ANY; |
d62a17ae | 919 | memset(&attr.mp_nexthop_global, 0, sizeof(struct in6_addr)); |
920 | ||
921 | /* TODO: should we set ATOMIC_AGGREGATE and AGGREGATOR? */ | |
922 | } | |
923 | ||
924 | new_attr = bgp_attr_intern(&attr); | |
925 | ||
18ee8310 DS |
926 | if (new_attr != bgp_path_info_mpath_attr(new_best)) { |
927 | if ((old_attr = bgp_path_info_mpath_attr(new_best))) | |
d62a17ae | 928 | bgp_attr_unintern(&old_attr); |
18ee8310 | 929 | bgp_path_info_mpath_attr_set(new_best, new_attr); |
1defdda8 | 930 | SET_FLAG(new_best->flags, BGP_PATH_ATTR_CHANGED); |
d62a17ae | 931 | } else |
932 | bgp_attr_unintern(&new_attr); | |
0b597ef0 | 933 | } |