]>
Commit | Line | Data |
---|---|---|
7ee30f28 DS |
1 | /* |
2 | * Nexthop Group structure definition. | |
3 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
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 | |
19 | */ | |
20 | #include <zebra.h> | |
21 | ||
31919191 | 22 | #include <vrf.h> |
c57bd6bb | 23 | #include <sockunion.h> |
7ee30f28 DS |
24 | #include <nexthop.h> |
25 | #include <nexthop_group.h> | |
dba32923 DS |
26 | #include <vty.h> |
27 | #include <command.h> | |
28 | ||
29 | #ifndef VTYSH_EXTRACT_PL | |
30 | #include "lib/nexthop_group_clippy.c" | |
31 | #endif | |
7ee30f28 | 32 | |
31919191 DS |
33 | DEFINE_MTYPE_STATIC(LIB, NEXTHOP_GROUP, "Nexthop Group") |
34 | ||
35 | struct nexthop_group_hooks { | |
36 | void (*new)(const char *name); | |
37 | void (*add_nexthop)(const struct nexthop_group_cmd *nhg, | |
38 | const struct nexthop *nhop); | |
39 | void (*del_nexthop)(const struct nexthop_group_cmd *nhg, | |
40 | const struct nexthop *nhop); | |
41 | void (*delete)(const char *name); | |
42 | }; | |
43 | ||
44 | static struct nexthop_group_hooks nhg_hooks; | |
45 | ||
46 | static inline int | |
47 | nexthop_group_cmd_compare(const struct nexthop_group_cmd *nhgc1, | |
48 | const struct nexthop_group_cmd *nhgc2); | |
49 | RB_GENERATE(nhgc_entry_head, nexthop_group_cmd, nhgc_entry, | |
50 | nexthop_group_cmd_compare) | |
51 | ||
52 | struct nhgc_entry_head nhgc_entries; | |
53 | ||
54 | static inline int | |
55 | nexthop_group_cmd_compare(const struct nexthop_group_cmd *nhgc1, | |
56 | const struct nexthop_group_cmd *nhgc2) | |
57 | { | |
58 | return strcmp(nhgc1->name, nhgc2->name); | |
59 | } | |
60 | ||
61 | struct nexthop *nexthop_exists(struct nexthop_group *nhg, struct nexthop *nh) | |
62 | { | |
63 | struct nexthop *nexthop; | |
64 | ||
65 | for (nexthop = nhg->nexthop; nexthop; nexthop = nexthop->next) { | |
66 | if (nexthop_same(nh, nexthop)) | |
67 | return nexthop; | |
68 | } | |
69 | ||
70 | return NULL; | |
71 | } | |
72 | ||
73 | struct nexthop_group *nexthop_group_new(void) | |
74 | { | |
75 | return XCALLOC(MTYPE_NEXTHOP_GROUP, sizeof(struct nexthop_group)); | |
76 | } | |
77 | ||
78 | void nexthop_group_delete(struct nexthop_group **nhg) | |
79 | { | |
80 | XFREE(MTYPE_NEXTHOP_GROUP, *nhg); | |
81 | } | |
82 | ||
7ee30f28 DS |
83 | /* Add nexthop to the end of a nexthop list. */ |
84 | void nexthop_add(struct nexthop **target, struct nexthop *nexthop) | |
85 | { | |
86 | struct nexthop *last; | |
87 | ||
88 | for (last = *target; last && last->next; last = last->next) | |
89 | ; | |
90 | if (last) | |
91 | last->next = nexthop; | |
92 | else | |
93 | *target = nexthop; | |
94 | nexthop->prev = last; | |
95 | } | |
96 | ||
31919191 DS |
97 | /* Delete nexthop from a nexthop list. */ |
98 | void nexthop_del(struct nexthop_group *nhg, struct nexthop *nh) | |
99 | { | |
100 | struct nexthop *nexthop; | |
101 | ||
102 | for (nexthop = nhg->nexthop; nexthop; nexthop = nexthop->next) { | |
103 | if (nexthop_same(nh, nexthop)) | |
104 | break; | |
105 | } | |
106 | ||
107 | assert(nexthop); | |
108 | ||
109 | if (nexthop->prev) | |
110 | nexthop->prev->next = nexthop->next; | |
111 | else | |
112 | nhg->nexthop = nexthop->next; | |
113 | ||
114 | if (nexthop->next) | |
115 | nexthop->next->prev = nexthop->prev; | |
ebee2bc4 DS |
116 | |
117 | nh->prev = NULL; | |
118 | nh->next = NULL; | |
31919191 DS |
119 | } |
120 | ||
7ee30f28 DS |
121 | void copy_nexthops(struct nexthop **tnh, struct nexthop *nh, |
122 | struct nexthop *rparent) | |
123 | { | |
124 | struct nexthop *nexthop; | |
125 | struct nexthop *nh1; | |
126 | ||
127 | for (nh1 = nh; nh1; nh1 = nh1->next) { | |
128 | nexthop = nexthop_new(); | |
129 | nexthop->vrf_id = nh1->vrf_id; | |
130 | nexthop->ifindex = nh1->ifindex; | |
131 | nexthop->type = nh1->type; | |
132 | nexthop->flags = nh1->flags; | |
133 | memcpy(&nexthop->gate, &nh1->gate, sizeof(nh1->gate)); | |
134 | memcpy(&nexthop->src, &nh1->src, sizeof(nh1->src)); | |
135 | memcpy(&nexthop->rmap_src, &nh1->rmap_src, | |
136 | sizeof(nh1->rmap_src)); | |
137 | nexthop->rparent = rparent; | |
138 | if (nh1->nh_label) | |
139 | nexthop_add_labels(nexthop, nh1->nh_label_type, | |
140 | nh1->nh_label->num_labels, | |
141 | &nh1->nh_label->label[0]); | |
142 | nexthop_add(tnh, nexthop); | |
143 | ||
144 | if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE)) | |
145 | copy_nexthops(&nexthop->resolved, nh1->resolved, | |
146 | nexthop); | |
147 | } | |
148 | } | |
dba32923 | 149 | |
31919191 DS |
150 | static void nhgc_delete_nexthops(struct nexthop_group_cmd *nhgc) |
151 | { | |
152 | struct nexthop *nexthop; | |
153 | ||
154 | nexthop = nhgc->nhg.nexthop; | |
155 | while (nexthop) { | |
156 | struct nexthop *next = nexthop_next(nexthop); | |
157 | ||
ebee2bc4 | 158 | nexthop_del(&nhgc->nhg, nexthop); |
31919191 DS |
159 | if (nhg_hooks.del_nexthop) |
160 | nhg_hooks.del_nexthop(nhgc, nexthop); | |
161 | ||
162 | nexthop_free(nexthop); | |
163 | ||
164 | nexthop = next; | |
165 | } | |
166 | } | |
167 | ||
d604266c | 168 | struct nexthop_group_cmd *nhgc_find(const char *name) |
31919191 DS |
169 | { |
170 | struct nexthop_group_cmd find; | |
171 | ||
172 | strlcpy(find.name, name, sizeof(find.name)); | |
173 | ||
174 | return RB_FIND(nhgc_entry_head, &nhgc_entries, &find); | |
175 | } | |
176 | ||
c57bd6bb DS |
177 | static int nhgc_cmp_helper(const char *a, const char *b) |
178 | { | |
179 | if (!a && !b) | |
180 | return 0; | |
181 | ||
182 | if (a && !b) | |
183 | return -1; | |
184 | ||
185 | if (!a && b) | |
186 | return 1; | |
187 | ||
188 | return strcmp(a, b); | |
189 | } | |
190 | ||
191 | static int nhgl_cmp(struct nexthop_hold *nh1, struct nexthop_hold *nh2) | |
192 | { | |
193 | int ret; | |
194 | ||
195 | ret = sockunion_cmp(&nh1->addr, &nh2->addr); | |
196 | if (ret) | |
197 | return ret; | |
198 | ||
199 | ret = nhgc_cmp_helper(nh1->intf, nh2->intf); | |
200 | if (ret) | |
201 | return ret; | |
202 | ||
203 | return nhgc_cmp_helper(nh1->nhvrf_name, nh2->nhvrf_name); | |
204 | } | |
205 | ||
206 | static void nhgl_delete(struct nexthop_hold *nh) | |
207 | { | |
208 | if (nh->intf) | |
209 | XFREE(MTYPE_TMP, nh->intf); | |
210 | ||
211 | if (nh->nhvrf_name) | |
212 | XFREE(MTYPE_TMP, nh->nhvrf_name); | |
213 | ||
214 | XFREE(MTYPE_TMP, nh); | |
215 | } | |
216 | ||
31919191 DS |
217 | static struct nexthop_group_cmd *nhgc_get(const char *name) |
218 | { | |
219 | struct nexthop_group_cmd *nhgc; | |
220 | ||
221 | nhgc = nhgc_find(name); | |
222 | if (!nhgc) { | |
223 | nhgc = XCALLOC(MTYPE_TMP, sizeof(*nhgc)); | |
224 | strlcpy(nhgc->name, name, sizeof(nhgc->name)); | |
225 | ||
226 | QOBJ_REG(nhgc, nexthop_group_cmd); | |
227 | RB_INSERT(nhgc_entry_head, &nhgc_entries, nhgc); | |
228 | ||
c57bd6bb DS |
229 | nhgc->nhg_list = list_new(); |
230 | nhgc->nhg_list->cmp = (int (*)(void *, void *))nhgl_cmp; | |
231 | nhgc->nhg_list->del = (void (*)(void *))nhgl_delete; | |
232 | ||
31919191 DS |
233 | if (nhg_hooks.new) |
234 | nhg_hooks.new(name); | |
235 | } | |
236 | ||
237 | return nhgc; | |
238 | } | |
239 | ||
240 | static void nhgc_delete(struct nexthop_group_cmd *nhgc) | |
dba32923 | 241 | { |
31919191 DS |
242 | nhgc_delete_nexthops(nhgc); |
243 | ||
244 | if (nhg_hooks.delete) | |
245 | nhg_hooks.delete(nhgc->name); | |
246 | ||
247 | RB_REMOVE(nhgc_entry_head, &nhgc_entries, nhgc); | |
c57bd6bb DS |
248 | |
249 | list_delete_and_null(&nhgc->nhg_list); | |
250 | ||
251 | XFREE(MTYPE_TMP, nhgc); | |
31919191 DS |
252 | } |
253 | ||
254 | DEFINE_QOBJ_TYPE(nexthop_group_cmd) | |
255 | ||
256 | DEFUN_NOSH(nexthop_group, nexthop_group_cmd, "nexthop-group NAME", | |
257 | "Enter into the nexthop-group submode\n" | |
258 | "Specify the NAME of the nexthop-group\n") | |
259 | { | |
260 | const char *nhg_name = argv[1]->arg; | |
261 | struct nexthop_group_cmd *nhgc = NULL; | |
262 | ||
263 | nhgc = nhgc_get(nhg_name); | |
264 | VTY_PUSH_CONTEXT(NH_GROUP_NODE, nhgc); | |
265 | ||
266 | return CMD_SUCCESS; | |
267 | } | |
268 | ||
269 | DEFUN_NOSH(no_nexthop_group, no_nexthop_group_cmd, "no nexthop-group NAME", | |
270 | NO_STR | |
271 | "Delete the nexthop-group\n" | |
272 | "Specify the NAME of the nexthop-group\n") | |
273 | { | |
274 | const char *nhg_name = argv[2]->arg; | |
275 | struct nexthop_group_cmd *nhgc = NULL; | |
276 | ||
277 | nhgc = nhgc_find(nhg_name); | |
278 | if (nhgc) | |
279 | nhgc_delete(nhgc); | |
280 | ||
281 | return CMD_SUCCESS; | |
282 | } | |
283 | ||
c57bd6bb DS |
284 | static void nexthop_group_save_nhop(struct nexthop_group_cmd *nhgc, |
285 | const char *nhvrf_name, | |
286 | const union sockunion *addr, | |
287 | const char *intf) | |
288 | { | |
289 | struct nexthop_hold *nh; | |
290 | ||
291 | nh = XCALLOC(MTYPE_TMP, sizeof(*nh)); | |
292 | ||
293 | if (nhvrf_name) | |
294 | nh->nhvrf_name = XSTRDUP(MTYPE_TMP, nhvrf_name); | |
295 | if (intf) | |
296 | nh->intf = XSTRDUP(MTYPE_TMP, intf); | |
297 | ||
298 | nh->addr = *addr; | |
299 | ||
300 | listnode_add_sort(nhgc->nhg_list, nh); | |
301 | } | |
302 | ||
303 | static void nexthop_group_unsave_nhop(struct nexthop_group_cmd *nhgc, | |
304 | const char *nhvrf_name, | |
305 | const union sockunion *addr, | |
306 | const char *intf) | |
307 | { | |
308 | struct nexthop_hold *nh; | |
309 | struct listnode *node; | |
310 | ||
311 | for (ALL_LIST_ELEMENTS_RO(nhgc->nhg_list, node, nh)) { | |
312 | if (nhgc_cmp_helper(nhvrf_name, nh->nhvrf_name) == 0 && | |
313 | sockunion_cmp(addr, &nh->addr) == 0 && | |
314 | nhgc_cmp_helper(intf, nh->intf) == 0) | |
315 | break; | |
316 | } | |
317 | ||
318 | /* | |
319 | * Something has gone seriously wrong, fail gracefully | |
320 | */ | |
321 | if (!nh) | |
322 | return; | |
323 | ||
324 | list_delete_node(nhgc->nhg_list, node); | |
325 | ||
326 | if (nh->nhvrf_name) | |
327 | XFREE(MTYPE_TMP, nh->nhvrf_name); | |
328 | if (nh->intf) | |
329 | XFREE(MTYPE_TMP, nh->intf); | |
330 | ||
331 | XFREE(MTYPE_TMP, nh); | |
332 | } | |
333 | ||
98cbbaea DS |
334 | static bool nexthop_group_parse_nexthop(struct nexthop *nhop, |
335 | const union sockunion *addr, | |
336 | const char *intf, const char *name) | |
31919191 | 337 | { |
31919191 | 338 | struct vrf *vrf; |
98cbbaea DS |
339 | |
340 | memset(nhop, 0, sizeof(*nhop)); | |
31919191 DS |
341 | |
342 | if (name) | |
343 | vrf = vrf_lookup_by_name(name); | |
344 | else | |
345 | vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
346 | ||
98cbbaea DS |
347 | if (!vrf) |
348 | return false; | |
31919191 | 349 | |
98cbbaea | 350 | nhop->vrf_id = vrf->vrf_id; |
31919191 DS |
351 | |
352 | if (addr->sa.sa_family == AF_INET) { | |
98cbbaea | 353 | nhop->gate.ipv4.s_addr = addr->sin.sin_addr.s_addr; |
31919191 | 354 | if (intf) { |
98cbbaea DS |
355 | nhop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
356 | nhop->ifindex = ifname2ifindex(intf, vrf->vrf_id); | |
357 | if (nhop->ifindex == IFINDEX_INTERNAL) | |
358 | return false; | |
31919191 | 359 | } else |
98cbbaea | 360 | nhop->type = NEXTHOP_TYPE_IPV4; |
31919191 | 361 | } else { |
98cbbaea | 362 | memcpy(&nhop->gate.ipv6, &addr->sin6.sin6_addr, 16); |
31919191 | 363 | if (intf) { |
98cbbaea DS |
364 | nhop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
365 | nhop->ifindex = ifname2ifindex(intf, vrf->vrf_id); | |
366 | if (nhop->ifindex == IFINDEX_INTERNAL) | |
367 | return false; | |
368 | } else | |
369 | nhop->type = NEXTHOP_TYPE_IPV6; | |
370 | } | |
371 | ||
372 | return true; | |
373 | } | |
374 | ||
375 | DEFPY(ecmp_nexthops, ecmp_nexthops_cmd, | |
376 | "[no] nexthop <A.B.C.D|X:X::X:X>$addr [INTERFACE]$intf [nexthop-vrf NAME$name]", | |
377 | NO_STR | |
378 | "Specify one of the nexthops in this ECMP group\n" | |
379 | "v4 Address\n" | |
380 | "v6 Address\n" | |
381 | "Interface to use\n" | |
382 | "If the nexthop is in a different vrf tell us\n" | |
383 | "The nexthop-vrf Name\n") | |
384 | { | |
385 | VTY_DECLVAR_CONTEXT(nexthop_group_cmd, nhgc); | |
386 | struct nexthop nhop; | |
387 | struct nexthop *nh; | |
388 | bool legal; | |
389 | ||
390 | legal = nexthop_group_parse_nexthop(&nhop, addr, intf, name); | |
391 | ||
392 | if (nhop.type == NEXTHOP_TYPE_IPV6 | |
393 | && IN6_IS_ADDR_LINKLOCAL(&nhop.gate.ipv6)) { | |
394 | vty_out(vty, | |
395 | "Specified a v6 LL with no interface, rejecting\n"); | |
396 | return CMD_WARNING_CONFIG_FAILED; | |
31919191 DS |
397 | } |
398 | ||
399 | nh = nexthop_exists(&nhgc->nhg, &nhop); | |
400 | ||
401 | if (no) { | |
98cbbaea | 402 | nexthop_group_unsave_nhop(nhgc, name, addr, intf); |
31919191 DS |
403 | if (nh) { |
404 | nexthop_del(&nhgc->nhg, nh); | |
405 | ||
406 | if (nhg_hooks.del_nexthop) | |
407 | nhg_hooks.del_nexthop(nhgc, nh); | |
408 | ||
409 | nexthop_free(nh); | |
410 | } | |
411 | } else if (!nh) { | |
412 | /* must be adding new nexthop since !no and !nexthop_exists */ | |
98cbbaea DS |
413 | if (legal) { |
414 | nh = nexthop_new(); | |
31919191 | 415 | |
98cbbaea DS |
416 | memcpy(nh, &nhop, sizeof(nhop)); |
417 | nexthop_add(&nhgc->nhg.nexthop, nh); | |
418 | } | |
31919191 | 419 | |
c57bd6bb DS |
420 | nexthop_group_save_nhop(nhgc, name, addr, intf); |
421 | ||
98cbbaea | 422 | if (legal && nhg_hooks.add_nexthop) |
31919191 DS |
423 | nhg_hooks.add_nexthop(nhgc, nh); |
424 | } | |
425 | ||
dba32923 DS |
426 | return CMD_SUCCESS; |
427 | } | |
428 | ||
429 | struct cmd_node nexthop_group_node = { | |
430 | NH_GROUP_NODE, | |
431 | "%s(config-nh-group)# ", | |
432 | 1 | |
433 | }; | |
434 | ||
1b7bce04 DS |
435 | void nexthop_group_write_nexthop(struct vty *vty, struct nexthop *nh) |
436 | { | |
437 | char buf[100]; | |
438 | struct vrf *vrf; | |
439 | ||
57cdafc4 | 440 | vty_out(vty, "nexthop "); |
1b7bce04 DS |
441 | |
442 | switch (nh->type) { | |
443 | case NEXTHOP_TYPE_IFINDEX: | |
444 | vty_out(vty, "%s", ifindex2ifname(nh->ifindex, nh->vrf_id)); | |
445 | break; | |
446 | case NEXTHOP_TYPE_IPV4: | |
447 | vty_out(vty, "%s", inet_ntoa(nh->gate.ipv4)); | |
448 | break; | |
449 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
450 | vty_out(vty, "%s %s", inet_ntoa(nh->gate.ipv4), | |
451 | ifindex2ifname(nh->ifindex, nh->vrf_id)); | |
452 | break; | |
453 | case NEXTHOP_TYPE_IPV6: | |
454 | vty_out(vty, "%s", | |
455 | inet_ntop(AF_INET6, &nh->gate.ipv6, buf, sizeof(buf))); | |
456 | break; | |
457 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
458 | vty_out(vty, "%s %s", | |
459 | inet_ntop(AF_INET6, &nh->gate.ipv6, buf, sizeof(buf)), | |
460 | ifindex2ifname(nh->ifindex, nh->vrf_id)); | |
461 | break; | |
462 | case NEXTHOP_TYPE_BLACKHOLE: | |
463 | break; | |
464 | } | |
465 | ||
466 | if (nh->vrf_id != VRF_DEFAULT) { | |
467 | vrf = vrf_lookup_by_id(nh->vrf_id); | |
468 | vty_out(vty, " nexthop-vrf %s", vrf->name); | |
469 | } | |
470 | vty_out(vty, "\n"); | |
471 | } | |
472 | ||
c57bd6bb DS |
473 | static void nexthop_group_write_nexthop_internal(struct vty *vty, |
474 | struct nexthop_hold *nh) | |
475 | { | |
476 | char buf[100]; | |
477 | ||
478 | vty_out(vty, "nexthop "); | |
479 | ||
480 | vty_out(vty, "%s", sockunion2str(&nh->addr, buf, sizeof(buf))); | |
481 | ||
482 | if (nh->intf) | |
483 | vty_out(vty, " %s", nh->intf); | |
484 | ||
485 | if (nh->nhvrf_name) | |
486 | vty_out(vty, " nexthop-vrf %s", nh->nhvrf_name); | |
487 | ||
488 | vty_out(vty, "\n"); | |
489 | } | |
490 | ||
dba32923 DS |
491 | static int nexthop_group_write(struct vty *vty) |
492 | { | |
31919191 | 493 | struct nexthop_group_cmd *nhgc; |
c57bd6bb | 494 | struct nexthop_hold *nh; |
31919191 DS |
495 | |
496 | RB_FOREACH (nhgc, nhgc_entry_head, &nhgc_entries) { | |
c57bd6bb DS |
497 | struct listnode *node; |
498 | ||
31919191 DS |
499 | vty_out(vty, "nexthop-group %s\n", nhgc->name); |
500 | ||
c57bd6bb | 501 | for (ALL_LIST_ELEMENTS_RO(nhgc->nhg_list, node, nh)) { |
811f859f | 502 | vty_out(vty, " "); |
c57bd6bb | 503 | nexthop_group_write_nexthop_internal(vty, nh); |
811f859f | 504 | } |
31919191 | 505 | |
31919191 DS |
506 | vty_out(vty, "!\n"); |
507 | } | |
dba32923 DS |
508 | |
509 | return 1; | |
510 | } | |
511 | ||
98cbbaea DS |
512 | void nexthop_group_enable_vrf(struct vrf *vrf) |
513 | { | |
514 | struct nexthop_group_cmd *nhgc; | |
515 | struct nexthop_hold *nhh; | |
516 | ||
517 | RB_FOREACH (nhgc, nhgc_entry_head, &nhgc_entries) { | |
518 | struct listnode *node; | |
519 | ||
520 | for (ALL_LIST_ELEMENTS_RO(nhgc->nhg_list, node, nhh)) { | |
521 | struct nexthop nhop; | |
522 | struct nexthop *nh; | |
523 | ||
524 | if (!nexthop_group_parse_nexthop(&nhop, &nhh->addr, | |
525 | nhh->intf, | |
526 | nhh->nhvrf_name)) | |
527 | continue; | |
528 | ||
529 | nh = nexthop_exists(&nhgc->nhg, &nhop); | |
530 | ||
531 | if (nh) | |
532 | continue; | |
533 | ||
534 | if (nhop.vrf_id != vrf->vrf_id) | |
535 | continue; | |
536 | ||
537 | nh = nexthop_new(); | |
538 | ||
539 | memcpy(nh, &nhop, sizeof(nhop)); | |
540 | nexthop_add(&nhgc->nhg.nexthop, nh); | |
541 | ||
542 | if (nhg_hooks.add_nexthop) | |
543 | nhg_hooks.add_nexthop(nhgc, nh); | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | void nexthop_group_disable_vrf(struct vrf *vrf) | |
549 | { | |
550 | struct nexthop_group_cmd *nhgc; | |
551 | struct nexthop_hold *nhh; | |
552 | ||
553 | RB_FOREACH (nhgc, nhgc_entry_head, &nhgc_entries) { | |
554 | struct listnode *node; | |
555 | ||
556 | for (ALL_LIST_ELEMENTS_RO(nhgc->nhg_list, node, nhh)) { | |
557 | struct nexthop nhop; | |
558 | struct nexthop *nh; | |
559 | ||
560 | if (!nexthop_group_parse_nexthop(&nhop, &nhh->addr, | |
561 | nhh->intf, | |
562 | nhh->nhvrf_name)) | |
563 | continue; | |
564 | ||
565 | nh = nexthop_exists(&nhgc->nhg, &nhop); | |
566 | ||
567 | if (!nh) | |
568 | continue; | |
569 | ||
570 | if (nh->vrf_id != vrf->vrf_id) | |
571 | continue; | |
572 | ||
573 | nexthop_del(&nhgc->nhg, nh); | |
574 | ||
575 | if (nhg_hooks.del_nexthop) | |
576 | nhg_hooks.del_nexthop(nhgc, nh); | |
577 | ||
578 | nexthop_free(nh); | |
579 | } | |
580 | } | |
581 | } | |
582 | ||
583 | void nexthop_group_interface_state_change(struct interface *ifp, | |
584 | ifindex_t oldifindex) | |
585 | { | |
586 | struct nexthop_group_cmd *nhgc; | |
587 | struct nexthop_hold *nhh; | |
588 | ||
589 | RB_FOREACH (nhgc, nhgc_entry_head, &nhgc_entries) { | |
590 | struct listnode *node; | |
591 | struct nexthop *nh; | |
592 | ||
593 | if (if_is_up(ifp)) { | |
594 | for (ALL_LIST_ELEMENTS_RO(nhgc->nhg_list, node, nhh)) { | |
595 | struct nexthop nhop; | |
596 | ||
597 | if (!nexthop_group_parse_nexthop( | |
598 | &nhop, &nhh->addr, nhh->intf, | |
599 | nhh->nhvrf_name)) | |
600 | continue; | |
601 | ||
602 | switch (nhop.type) { | |
603 | case NEXTHOP_TYPE_IPV4: | |
604 | case NEXTHOP_TYPE_IPV6: | |
605 | case NEXTHOP_TYPE_BLACKHOLE: | |
606 | continue; | |
607 | case NEXTHOP_TYPE_IFINDEX: | |
608 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
609 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
610 | break; | |
611 | } | |
612 | nh = nexthop_exists(&nhgc->nhg, &nhop); | |
613 | ||
614 | if (nh) | |
615 | continue; | |
616 | ||
617 | if (ifp->ifindex != nhop.ifindex) | |
618 | continue; | |
619 | ||
620 | nh = nexthop_new(); | |
621 | ||
622 | memcpy(nh, &nhop, sizeof(nhop)); | |
623 | nexthop_add(&nhgc->nhg.nexthop, nh); | |
624 | ||
625 | if (nhg_hooks.add_nexthop) | |
626 | nhg_hooks.add_nexthop(nhgc, nh); | |
627 | } | |
628 | } else { | |
629 | struct nexthop *next_nh; | |
630 | ||
631 | for (nh = nhgc->nhg.nexthop; nh; nh = next_nh) { | |
632 | next_nh = nh->next; | |
633 | switch (nh->type) { | |
634 | case NEXTHOP_TYPE_IPV4: | |
635 | case NEXTHOP_TYPE_IPV6: | |
636 | case NEXTHOP_TYPE_BLACKHOLE: | |
637 | continue; | |
638 | case NEXTHOP_TYPE_IFINDEX: | |
639 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
640 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
641 | break; | |
642 | } | |
643 | ||
644 | if (oldifindex != nh->ifindex) | |
645 | continue; | |
646 | ||
647 | nexthop_del(&nhgc->nhg, nh); | |
648 | ||
649 | if (nhg_hooks.del_nexthop) | |
650 | nhg_hooks.del_nexthop(nhgc, nh); | |
651 | ||
652 | nexthop_free(nh); | |
653 | } | |
654 | } | |
655 | } | |
656 | } | |
657 | ||
31919191 DS |
658 | void nexthop_group_init(void (*new)(const char *name), |
659 | void (*add_nexthop)(const struct nexthop_group_cmd *nhg, | |
660 | const struct nexthop *nhop), | |
661 | void (*del_nexthop)(const struct nexthop_group_cmd *nhg, | |
662 | const struct nexthop *nhop), | |
663 | void (*delete)(const char *name)) | |
dba32923 | 664 | { |
31919191 DS |
665 | RB_INIT(nhgc_entry_head, &nhgc_entries); |
666 | ||
dba32923 DS |
667 | install_node(&nexthop_group_node, nexthop_group_write); |
668 | install_element(CONFIG_NODE, &nexthop_group_cmd); | |
31919191 DS |
669 | install_element(CONFIG_NODE, &no_nexthop_group_cmd); |
670 | ||
671 | install_default(NH_GROUP_NODE); | |
672 | install_element(NH_GROUP_NODE, &ecmp_nexthops_cmd); | |
673 | ||
674 | memset(&nhg_hooks, 0, sizeof(nhg_hooks)); | |
675 | ||
676 | if (new) | |
677 | nhg_hooks.new = new; | |
678 | if (add_nexthop) | |
679 | nhg_hooks.add_nexthop = add_nexthop; | |
680 | if (del_nexthop) | |
681 | nhg_hooks.del_nexthop = del_nexthop; | |
682 | if (delete) | |
683 | nhg_hooks.delete = delete; | |
dba32923 | 684 | } |