1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* Nexthop Group structure definition.
* Copyright (C) 2018 Cumulus Networks, Inc.
* Donald Sharp
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include <vrf.h>
#include <nexthop.h>
#include <nexthop_group.h>
#include <vty.h>
#include <command.h>
#ifndef VTYSH_EXTRACT_PL
#include "lib/nexthop_group_clippy.c"
#endif
DEFINE_MTYPE_STATIC(LIB, NEXTHOP_GROUP, "Nexthop Group")
struct nexthop_group_hooks {
void (*new)(const char *name);
void (*add_nexthop)(const struct nexthop_group_cmd *nhg,
const struct nexthop *nhop);
void (*del_nexthop)(const struct nexthop_group_cmd *nhg,
const struct nexthop *nhop);
void (*delete)(const char *name);
};
static struct nexthop_group_hooks nhg_hooks;
static inline int
nexthop_group_cmd_compare(const struct nexthop_group_cmd *nhgc1,
const struct nexthop_group_cmd *nhgc2);
RB_GENERATE(nhgc_entry_head, nexthop_group_cmd, nhgc_entry,
nexthop_group_cmd_compare)
struct nhgc_entry_head nhgc_entries;
static inline int
nexthop_group_cmd_compare(const struct nexthop_group_cmd *nhgc1,
const struct nexthop_group_cmd *nhgc2)
{
return strcmp(nhgc1->name, nhgc2->name);
}
struct nexthop *nexthop_exists(struct nexthop_group *nhg, struct nexthop *nh)
{
struct nexthop *nexthop;
for (nexthop = nhg->nexthop; nexthop; nexthop = nexthop->next) {
if (nexthop_same(nh, nexthop))
return nexthop;
}
return NULL;
}
struct nexthop_group *nexthop_group_new(void)
{
return XCALLOC(MTYPE_NEXTHOP_GROUP, sizeof(struct nexthop_group));
}
void nexthop_group_delete(struct nexthop_group **nhg)
{
XFREE(MTYPE_NEXTHOP_GROUP, *nhg);
}
/* Add nexthop to the end of a nexthop list. */
void nexthop_add(struct nexthop **target, struct nexthop *nexthop)
{
struct nexthop *last;
for (last = *target; last && last->next; last = last->next)
;
if (last)
last->next = nexthop;
else
*target = nexthop;
nexthop->prev = last;
}
/* Delete nexthop from a nexthop list. */
void nexthop_del(struct nexthop_group *nhg, struct nexthop *nh)
{
struct nexthop *nexthop;
for (nexthop = nhg->nexthop; nexthop; nexthop = nexthop->next) {
if (nexthop_same(nh, nexthop))
break;
}
assert(nexthop);
if (nexthop->prev)
nexthop->prev->next = nexthop->next;
else
nhg->nexthop = nexthop->next;
if (nexthop->next)
nexthop->next->prev = nexthop->prev;
}
void copy_nexthops(struct nexthop **tnh, struct nexthop *nh,
struct nexthop *rparent)
{
struct nexthop *nexthop;
struct nexthop *nh1;
for (nh1 = nh; nh1; nh1 = nh1->next) {
nexthop = nexthop_new();
nexthop->vrf_id = nh1->vrf_id;
nexthop->ifindex = nh1->ifindex;
nexthop->type = nh1->type;
nexthop->flags = nh1->flags;
memcpy(&nexthop->gate, &nh1->gate, sizeof(nh1->gate));
memcpy(&nexthop->src, &nh1->src, sizeof(nh1->src));
memcpy(&nexthop->rmap_src, &nh1->rmap_src,
sizeof(nh1->rmap_src));
nexthop->rparent = rparent;
if (nh1->nh_label)
nexthop_add_labels(nexthop, nh1->nh_label_type,
nh1->nh_label->num_labels,
&nh1->nh_label->label[0]);
nexthop_add(tnh, nexthop);
if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))
copy_nexthops(&nexthop->resolved, nh1->resolved,
nexthop);
}
}
static void nhgc_delete_nexthops(struct nexthop_group_cmd *nhgc)
{
struct nexthop *nexthop;
nexthop = nhgc->nhg.nexthop;
while (nexthop) {
struct nexthop *next = nexthop_next(nexthop);
if (nhg_hooks.del_nexthop)
nhg_hooks.del_nexthop(nhgc, nexthop);
nexthop_free(nexthop);
nexthop = next;
}
}
struct nexthop_group_cmd *nhgc_find(const char *name)
{
struct nexthop_group_cmd find;
strlcpy(find.name, name, sizeof(find.name));
return RB_FIND(nhgc_entry_head, &nhgc_entries, &find);
}
static struct nexthop_group_cmd *nhgc_get(const char *name)
{
struct nexthop_group_cmd *nhgc;
nhgc = nhgc_find(name);
if (!nhgc) {
nhgc = XCALLOC(MTYPE_TMP, sizeof(*nhgc));
strlcpy(nhgc->name, name, sizeof(nhgc->name));
QOBJ_REG(nhgc, nexthop_group_cmd);
RB_INSERT(nhgc_entry_head, &nhgc_entries, nhgc);
if (nhg_hooks.new)
nhg_hooks.new(name);
}
return nhgc;
}
static void nhgc_delete(struct nexthop_group_cmd *nhgc)
{
nhgc_delete_nexthops(nhgc);
if (nhg_hooks.delete)
nhg_hooks.delete(nhgc->name);
RB_REMOVE(nhgc_entry_head, &nhgc_entries, nhgc);
}
DEFINE_QOBJ_TYPE(nexthop_group_cmd)
DEFUN_NOSH(nexthop_group, nexthop_group_cmd, "nexthop-group NAME",
"Enter into the nexthop-group submode\n"
"Specify the NAME of the nexthop-group\n")
{
const char *nhg_name = argv[1]->arg;
struct nexthop_group_cmd *nhgc = NULL;
nhgc = nhgc_get(nhg_name);
VTY_PUSH_CONTEXT(NH_GROUP_NODE, nhgc);
return CMD_SUCCESS;
}
DEFUN_NOSH(no_nexthop_group, no_nexthop_group_cmd, "no nexthop-group NAME",
NO_STR
"Delete the nexthop-group\n"
"Specify the NAME of the nexthop-group\n")
{
const char *nhg_name = argv[2]->arg;
struct nexthop_group_cmd *nhgc = NULL;
nhgc = nhgc_find(nhg_name);
if (nhgc)
nhgc_delete(nhgc);
return CMD_SUCCESS;
}
DEFPY(ecmp_nexthops, ecmp_nexthops_cmd,
"[no] nexthop <A.B.C.D|X:X::X:X>$addr [INTERFACE]$intf [nexthop-vrf NAME$name]",
NO_STR
"Specify one of the nexthops in this ECMP group\n"
"v4 Address\n"
"v6 Address\n"
"Interface to use\n"
"If the nexthop is in a different vrf tell us\n"
"The nexthop-vrf Name\n")
{
VTY_DECLVAR_CONTEXT(nexthop_group_cmd, nhgc);
struct vrf *vrf;
struct nexthop nhop;
struct nexthop *nh;
if (name)
vrf = vrf_lookup_by_name(name);
else
vrf = vrf_lookup_by_id(VRF_DEFAULT);
if (!vrf) {
vty_out(vty, "Specified: %s is non-existent\n", name);
return CMD_WARNING;
}
memset(&nhop, 0, sizeof(nhop));
nhop.vrf_id = vrf->vrf_id;
if (addr->sa.sa_family == AF_INET) {
nhop.gate.ipv4.s_addr = addr->sin.sin_addr.s_addr;
if (intf) {
nhop.type = NEXTHOP_TYPE_IPV4_IFINDEX;
nhop.ifindex = ifname2ifindex(intf, vrf->vrf_id);
if (nhop.ifindex == IFINDEX_INTERNAL) {
vty_out(vty,
"Specified Intf %s does not exist in vrf: %s\n",
intf, vrf->name);
return CMD_WARNING;
}
} else
nhop.type = NEXTHOP_TYPE_IPV4;
} else {
memcpy(&nhop.gate.ipv6, &addr->sin6.sin6_addr, 16);
if (intf) {
nhop.type = NEXTHOP_TYPE_IPV6_IFINDEX;
nhop.ifindex = ifname2ifindex(intf, vrf->vrf_id);
if (nhop.ifindex == IFINDEX_INTERNAL) {
vty_out(vty,
"Specified Intf %s does not exist in vrf: %s\n",
intf, vrf->name);
return CMD_WARNING;
}
} else
nhop.type = NEXTHOP_TYPE_IPV6;
}
nh = nexthop_exists(&nhgc->nhg, &nhop);
if (no) {
if (nh) {
nexthop_del(&nhgc->nhg, nh);
if (nhg_hooks.del_nexthop)
nhg_hooks.del_nexthop(nhgc, nh);
nexthop_free(nh);
}
} else if (!nh) {
/* must be adding new nexthop since !no and !nexthop_exists */
nh = nexthop_new();
memcpy(nh, &nhop, sizeof(nhop));
nexthop_add(&nhgc->nhg.nexthop, nh);
if (nhg_hooks.add_nexthop)
nhg_hooks.add_nexthop(nhgc, nh);
}
return CMD_SUCCESS;
}
struct cmd_node nexthop_group_node = {
NH_GROUP_NODE,
"%s(config-nh-group)# ",
1
};
void nexthop_group_write_nexthop(struct vty *vty, struct nexthop *nh)
{
char buf[100];
struct vrf *vrf;
vty_out(vty, " nexthop ");
switch (nh->type) {
case NEXTHOP_TYPE_IFINDEX:
vty_out(vty, "%s", ifindex2ifname(nh->ifindex, nh->vrf_id));
break;
case NEXTHOP_TYPE_IPV4:
vty_out(vty, "%s", inet_ntoa(nh->gate.ipv4));
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
vty_out(vty, "%s %s", inet_ntoa(nh->gate.ipv4),
ifindex2ifname(nh->ifindex, nh->vrf_id));
break;
case NEXTHOP_TYPE_IPV6:
vty_out(vty, "%s",
inet_ntop(AF_INET6, &nh->gate.ipv6, buf, sizeof(buf)));
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
vty_out(vty, "%s %s",
inet_ntop(AF_INET6, &nh->gate.ipv6, buf, sizeof(buf)),
ifindex2ifname(nh->ifindex, nh->vrf_id));
break;
case NEXTHOP_TYPE_BLACKHOLE:
break;
}
if (nh->vrf_id != VRF_DEFAULT) {
vrf = vrf_lookup_by_id(nh->vrf_id);
vty_out(vty, " nexthop-vrf %s", vrf->name);
}
vty_out(vty, "\n");
}
static int nexthop_group_write(struct vty *vty)
{
struct nexthop_group_cmd *nhgc;
struct nexthop *nh;
RB_FOREACH (nhgc, nhgc_entry_head, &nhgc_entries) {
vty_out(vty, "nexthop-group %s\n", nhgc->name);
for (nh = nhgc->nhg.nexthop; nh; nh = nh->next)
nexthop_group_write_nexthop(vty, nh);
vty_out(vty, "!\n");
}
return 1;
}
void nexthop_group_init(void (*new)(const char *name),
void (*add_nexthop)(const struct nexthop_group_cmd *nhg,
const struct nexthop *nhop),
void (*del_nexthop)(const struct nexthop_group_cmd *nhg,
const struct nexthop *nhop),
void (*delete)(const char *name))
{
RB_INIT(nhgc_entry_head, &nhgc_entries);
install_node(&nexthop_group_node, nexthop_group_write);
install_element(CONFIG_NODE, &nexthop_group_cmd);
install_element(CONFIG_NODE, &no_nexthop_group_cmd);
install_default(NH_GROUP_NODE);
install_element(NH_GROUP_NODE, &ecmp_nexthops_cmd);
memset(&nhg_hooks, 0, sizeof(nhg_hooks));
if (new)
nhg_hooks.new = new;
if (add_nexthop)
nhg_hooks.add_nexthop = add_nexthop;
if (del_nexthop)
nhg_hooks.del_nexthop = del_nexthop;
if (delete)
nhg_hooks.delete = delete;
}
|