]> git.proxmox.com Git - mirror_frr.git/blob - lib/vrf.c
lib: terminate capabilities only if initialized
[mirror_frr.git] / lib / vrf.c
1 /*
2 * VRF functions.
3 * Copyright (C) 2014 6WIND S.A.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra 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 *
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
20 */
21
22 #include <zebra.h>
23
24 #include "if.h"
25 #include "vrf.h"
26 #include "vrf_int.h"
27 #include "prefix.h"
28 #include "table.h"
29 #include "log.h"
30 #include "memory.h"
31 #include "command.h"
32
33 DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
34 DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
35
36 DEFINE_QOBJ_TYPE(vrf)
37
38 static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
39 static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
40
41 RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
42 RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
43
44 struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
45 struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
46
47 /*
48 * Turn on/off debug code
49 * for vrf.
50 */
51 int debug_vrf = 0;
52
53 /* Holding VRF hooks */
54 struct vrf_master {
55 int (*vrf_new_hook)(struct vrf *);
56 int (*vrf_delete_hook)(struct vrf *);
57 int (*vrf_enable_hook)(struct vrf *);
58 int (*vrf_disable_hook)(struct vrf *);
59 } vrf_master = {
60 0,
61 };
62
63 static int vrf_is_enabled(struct vrf *vrf);
64 static void vrf_disable(struct vrf *vrf);
65
66 /* VRF list existance check by name. */
67 struct vrf *vrf_lookup_by_name(const char *name)
68 {
69 struct vrf vrf;
70 strlcpy(vrf.name, name, sizeof(vrf.name));
71 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
72 }
73
74 static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
75 {
76 return (a->vrf_id - b->vrf_id);
77 }
78
79 static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
80 {
81 return strcmp(a->name, b->name);
82 }
83
84 /* Get a VRF. If not found, create one.
85 * Arg:
86 * name - The name of the vrf. May be NULL if unknown.
87 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
88 * Description: Please note that this routine can be called with just the name
89 * and 0 vrf-id
90 */
91 struct vrf *vrf_get(vrf_id_t vrf_id, const char *name)
92 {
93 struct vrf *vrf = NULL;
94 int new = 0;
95
96 if (debug_vrf)
97 zlog_debug("VRF_GET: %s(%d)", name, vrf_id);
98
99 /* Nothing to see, move along here */
100 if (!name && vrf_id == VRF_UNKNOWN)
101 return NULL;
102
103 /* Try to find VRF both by ID and name */
104 if (vrf_id != VRF_UNKNOWN)
105 vrf = vrf_lookup_by_id(vrf_id);
106 if (!vrf && name)
107 vrf = vrf_lookup_by_name(name);
108
109 if (vrf == NULL) {
110 vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf));
111 vrf->vrf_id = VRF_UNKNOWN;
112 if_init(&vrf->iflist);
113 QOBJ_REG(vrf, vrf);
114 new = 1;
115
116 if (debug_vrf)
117 zlog_debug("VRF(%u) %s is created.", vrf_id,
118 (name) ? name : "(NULL)");
119 }
120
121 /* Set identifier */
122 if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) {
123 vrf->vrf_id = vrf_id;
124 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
125 }
126
127 /* Set name */
128 if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) {
129 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
130 strlcpy(vrf->name, name, sizeof(vrf->name));
131 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
132 } else if (name && vrf->name[0] == '\0') {
133 strlcpy(vrf->name, name, sizeof(vrf->name));
134 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
135 }
136
137 if (new &&vrf_master.vrf_new_hook)
138 (*vrf_master.vrf_new_hook)(vrf);
139
140 return vrf;
141 }
142
143 /* Delete a VRF. This is called in vrf_terminate(). */
144 void vrf_delete(struct vrf *vrf)
145 {
146 if (debug_vrf)
147 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
148
149 if (vrf_is_enabled(vrf))
150 vrf_disable(vrf);
151
152 if (vrf_master.vrf_delete_hook)
153 (*vrf_master.vrf_delete_hook)(vrf);
154
155 QOBJ_UNREG(vrf);
156 if_terminate(&vrf->iflist);
157
158 if (vrf->vrf_id != VRF_UNKNOWN)
159 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
160 if (vrf->name[0] != '\0')
161 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
162
163 XFREE(MTYPE_VRF, vrf);
164 }
165
166 /* Look up a VRF by identifier. */
167 struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
168 {
169 struct vrf vrf;
170 vrf.vrf_id = vrf_id;
171 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
172 }
173
174 /*
175 * Check whether the VRF is enabled.
176 */
177 static int vrf_is_enabled(struct vrf *vrf)
178 {
179 return vrf && CHECK_FLAG(vrf->status, VRF_ACTIVE);
180 }
181
182 /*
183 * Enable a VRF - that is, let the VRF be ready to use.
184 * The VRF_ENABLE_HOOK callback will be called to inform
185 * that they can allocate resources in this VRF.
186 *
187 * RETURN: 1 - enabled successfully; otherwise, 0.
188 */
189 int vrf_enable(struct vrf *vrf)
190 {
191 if (vrf_is_enabled(vrf))
192 return 1;
193
194 if (debug_vrf)
195 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
196
197 SET_FLAG(vrf->status, VRF_ACTIVE);
198
199 if (vrf_master.vrf_enable_hook)
200 (*vrf_master.vrf_enable_hook)(vrf);
201
202 return 1;
203 }
204
205 /*
206 * Disable a VRF - that is, let the VRF be unusable.
207 * The VRF_DELETE_HOOK callback will be called to inform
208 * that they must release the resources in the VRF.
209 */
210 static void vrf_disable(struct vrf *vrf)
211 {
212 if (!vrf_is_enabled(vrf))
213 return;
214
215 UNSET_FLAG(vrf->status, VRF_ACTIVE);
216
217 if (debug_vrf)
218 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
219
220 /* Till now, nothing to be done for the default VRF. */
221 // Pending: see why this statement.
222
223 if (vrf_master.vrf_disable_hook)
224 (*vrf_master.vrf_disable_hook)(vrf);
225 }
226
227 vrf_id_t vrf_name_to_id(const char *name)
228 {
229 struct vrf *vrf;
230 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
231 // id/ routine not used.
232
233 vrf = vrf_lookup_by_name(name);
234 if (vrf)
235 vrf_id = vrf->vrf_id;
236
237 return vrf_id;
238 }
239
240 /* Get the data pointer of the specified VRF. If not found, create one. */
241 void *vrf_info_get(vrf_id_t vrf_id)
242 {
243 struct vrf *vrf = vrf_get(vrf_id, NULL);
244 return vrf->info;
245 }
246
247 /* Look up the data pointer of the specified VRF. */
248 void *vrf_info_lookup(vrf_id_t vrf_id)
249 {
250 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
251 return vrf ? vrf->info : NULL;
252 }
253
254 /* Look up the interface list in a VRF. */
255 struct list *vrf_iflist(vrf_id_t vrf_id)
256 {
257 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
258 return vrf ? vrf->iflist : NULL;
259 }
260
261 /* Get the interface list of the specified VRF. Create one if not find. */
262 struct list *vrf_iflist_get(vrf_id_t vrf_id)
263 {
264 struct vrf *vrf = vrf_get(vrf_id, NULL);
265 return vrf->iflist;
266 }
267
268 /*
269 * VRF bit-map
270 */
271
272 #define VRF_BITMAP_NUM_OF_GROUPS 8
273 #define VRF_BITMAP_NUM_OF_BITS_IN_GROUP (UINT16_MAX / VRF_BITMAP_NUM_OF_GROUPS)
274 #define VRF_BITMAP_NUM_OF_BYTES_IN_GROUP \
275 (VRF_BITMAP_NUM_OF_BITS_IN_GROUP / CHAR_BIT + 1) /* +1 for ensure */
276
277 #define VRF_BITMAP_GROUP(_id) ((_id) / VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
278 #define VRF_BITMAP_BIT_OFFSET(_id) ((_id) % VRF_BITMAP_NUM_OF_BITS_IN_GROUP)
279
280 #define VRF_BITMAP_INDEX_IN_GROUP(_bit_offset) ((_bit_offset) / CHAR_BIT)
281 #define VRF_BITMAP_FLAG(_bit_offset) (((u_char)1) << ((_bit_offset) % CHAR_BIT))
282
283 struct vrf_bitmap {
284 u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
285 };
286
287 vrf_bitmap_t vrf_bitmap_init(void)
288 {
289 return (vrf_bitmap_t)XCALLOC(MTYPE_VRF_BITMAP,
290 sizeof(struct vrf_bitmap));
291 }
292
293 void vrf_bitmap_free(vrf_bitmap_t bmap)
294 {
295 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
296 int i;
297
298 if (bmap == VRF_BITMAP_NULL)
299 return;
300
301 for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
302 if (bm->groups[i])
303 XFREE(MTYPE_VRF_BITMAP, bm->groups[i]);
304
305 XFREE(MTYPE_VRF_BITMAP, bm);
306 }
307
308 void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
309 {
310 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
311 u_char group = VRF_BITMAP_GROUP(vrf_id);
312 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
313
314 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN)
315 return;
316
317 if (bm->groups[group] == NULL)
318 bm->groups[group] = XCALLOC(MTYPE_VRF_BITMAP,
319 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
320
321 SET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
322 VRF_BITMAP_FLAG(offset));
323 }
324
325 void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
326 {
327 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
328 u_char group = VRF_BITMAP_GROUP(vrf_id);
329 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
330
331 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
332 || bm->groups[group] == NULL)
333 return;
334
335 UNSET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
336 VRF_BITMAP_FLAG(offset));
337 }
338
339 int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
340 {
341 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
342 u_char group = VRF_BITMAP_GROUP(vrf_id);
343 u_char offset = VRF_BITMAP_BIT_OFFSET(vrf_id);
344
345 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
346 || bm->groups[group] == NULL)
347 return 0;
348
349 return CHECK_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
350 VRF_BITMAP_FLAG(offset))
351 ? 1
352 : 0;
353 }
354
355 static void vrf_autocomplete(vector comps, struct cmd_token *token)
356 {
357 struct vrf *vrf = NULL;
358
359 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name)
360 {
361 if (vrf->vrf_id != 0)
362 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
363 }
364 }
365
366 static const struct cmd_variable_handler vrf_var_handlers[] = {
367 {
368 .varname = "vrf",
369 .completions = vrf_autocomplete,
370 },
371 {.completions = NULL},
372 };
373
374 /* Initialize VRF module. */
375 void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
376 int (*disable)(struct vrf *), int (*delete)(struct vrf *))
377 {
378 struct vrf *default_vrf;
379
380 if (debug_vrf)
381 zlog_debug("%s: Initializing VRF subsystem",
382 __PRETTY_FUNCTION__);
383
384 vrf_master.vrf_new_hook = create;
385 vrf_master.vrf_enable_hook = enable;
386 vrf_master.vrf_disable_hook = disable;
387 vrf_master.vrf_delete_hook = delete;
388
389 /* The default VRF always exists. */
390 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
391 if (!default_vrf) {
392 zlog_err("vrf_init: failed to create the default VRF!");
393 exit(1);
394 }
395
396 /* Enable the default VRF. */
397 if (!vrf_enable(default_vrf)) {
398 zlog_err("vrf_init: failed to enable the default VRF!");
399 exit(1);
400 }
401
402 cmd_variable_handler_register(vrf_var_handlers);
403 }
404
405 /* Terminate VRF module. */
406 void vrf_terminate(void)
407 {
408 struct vrf *vrf;
409
410 if (debug_vrf)
411 zlog_debug("%s: Shutting down vrf subsystem",
412 __PRETTY_FUNCTION__);
413
414 while ((vrf = RB_ROOT(vrf_id_head, &vrfs_by_id)) != NULL)
415 vrf_delete(vrf);
416 while ((vrf = RB_ROOT(vrf_name_head, &vrfs_by_name)) != NULL)
417 vrf_delete(vrf);
418 }
419
420 /* Create a socket for the VRF. */
421 int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id)
422 {
423 int ret = -1;
424
425 ret = socket(domain, type, protocol);
426
427 return ret;
428 }
429
430 /* vrf CLI commands */
431 DEFUN_NOSH (vrf,
432 vrf_cmd,
433 "vrf NAME",
434 "Select a VRF to configure\n"
435 "VRF's name\n")
436 {
437 int idx_name = 1;
438 const char *vrfname = argv[idx_name]->arg;
439 struct vrf *vrfp;
440
441 if (strlen(vrfname) > VRF_NAMSIZ) {
442 vty_out(vty,
443 "%% VRF name %s is invalid: length exceeds "
444 "%d characters\n",
445 vrfname, VRF_NAMSIZ);
446 return CMD_WARNING_CONFIG_FAILED;
447 }
448
449 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
450
451 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
452
453 return CMD_SUCCESS;
454 }
455
456 DEFUN_NOSH (no_vrf,
457 no_vrf_cmd,
458 "no vrf NAME",
459 NO_STR
460 "Delete a pseudo VRF's configuration\n"
461 "VRF's name\n")
462 {
463 const char *vrfname = argv[2]->arg;
464
465 struct vrf *vrfp;
466
467 vrfp = vrf_lookup_by_name(vrfname);
468
469 if (vrfp == NULL) {
470 vty_out(vty, "%% VRF %s does not exist\n", vrfname);
471 return CMD_WARNING_CONFIG_FAILED;
472 }
473
474 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
475 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
476 return CMD_WARNING_CONFIG_FAILED;
477 }
478
479 vrf_delete(vrfp);
480
481 return CMD_SUCCESS;
482 }
483
484
485 struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
486
487 /*
488 * Debug CLI for vrf's
489 */
490 DEFUN (vrf_debug,
491 vrf_debug_cmd,
492 "debug vrf",
493 DEBUG_STR
494 "VRF Debugging\n")
495 {
496 debug_vrf = 1;
497
498 return CMD_SUCCESS;
499 }
500
501 DEFUN (no_vrf_debug,
502 no_vrf_debug_cmd,
503 "no debug vrf",
504 NO_STR
505 DEBUG_STR
506 "VRF Debugging\n")
507 {
508 debug_vrf = 0;
509
510 return CMD_SUCCESS;
511 }
512
513 static int vrf_write_host(struct vty *vty)
514 {
515 if (debug_vrf)
516 vty_out(vty, "debug vrf\n");
517
518 return 1;
519 }
520
521 static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
522
523 void vrf_install_commands(void)
524 {
525 install_node(&vrf_debug_node, vrf_write_host);
526
527 install_element(CONFIG_NODE, &vrf_debug_cmd);
528 install_element(ENABLE_NODE, &vrf_debug_cmd);
529 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
530 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
531 }
532
533 void vrf_cmd_init(int (*writefunc)(struct vty *vty))
534 {
535 install_element(CONFIG_NODE, &vrf_cmd);
536 install_element(CONFIG_NODE, &no_vrf_cmd);
537 install_node(&vrf_node, writefunc);
538 install_default(VRF_NODE);
539 }