]> git.proxmox.com Git - mirror_frr.git/blame - lib/vrf.c
lib: register 'if_var_handlers' only once
[mirror_frr.git] / lib / vrf.c
CommitLineData
b72ede27
FL
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 *
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
b72ede27
FL
20 */
21
22#include <zebra.h>
23
6a69b354 24#include "if.h"
b72ede27 25#include "vrf.h"
7922fc65 26#include "vrf_int.h"
b72ede27
FL
27#include "prefix.h"
28#include "table.h"
29#include "log.h"
30#include "memory.h"
19dc275e
DS
31#include "command.h"
32
d62a17ae 33DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
4a1ab8e4
DL
34DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
35
e80e7cce
DL
36DEFINE_QOBJ_TYPE(vrf)
37
d62a17ae 38static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
39static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
1a1a7065 40
d62a17ae 41RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
42RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
1a1a7065 43
d62a17ae 44struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
45struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
1a1a7065 46
19dc275e
DS
47/*
48 * Turn on/off debug code
49 * for vrf.
50 */
51int debug_vrf = 0;
b72ede27 52
b72ede27 53/* Holding VRF hooks */
d62a17ae 54struct 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};
b72ede27 62
d62a17ae 63static int vrf_is_enabled(struct vrf *vrf);
64static void vrf_disable(struct vrf *vrf);
e5bf3e1e 65
216b18ef 66/* VRF list existance check by name. */
d62a17ae 67struct vrf *vrf_lookup_by_name(const char *name)
216b18ef 68{
d62a17ae 69 struct vrf vrf;
70 strlcpy(vrf.name, name, sizeof(vrf.name));
71 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
216b18ef 72}
216b18ef 73
d62a17ae 74static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
b72ede27 75{
d62a17ae 76 return (a->vrf_id - b->vrf_id);
216b18ef
DS
77}
78
d62a17ae 79static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
b72ede27 80{
d62a17ae 81 return strcmp(a->name, b->name);
b72ede27
FL
82}
83
216b18ef 84/* Get a VRF. If not found, create one.
34f8e6af
DS
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
216b18ef 88 * Description: Please note that this routine can be called with just the name
34f8e6af
DS
89 * and 0 vrf-id
90 */
d62a17ae 91struct 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;
b72ede27
FL
141}
142
143/* Delete a VRF. This is called in vrf_terminate(). */
d62a17ae 144void vrf_delete(struct vrf *vrf)
b72ede27 145{
d62a17ae 146 if (debug_vrf)
147 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
b72ede27 148
d62a17ae 149 if (vrf_is_enabled(vrf))
150 vrf_disable(vrf);
e5bf3e1e 151
d62a17ae 152 if (vrf_master.vrf_delete_hook)
153 (*vrf_master.vrf_delete_hook)(vrf);
216b18ef 154
d62a17ae 155 QOBJ_UNREG(vrf);
156 if_terminate(&vrf->iflist);
b72ede27 157
d62a17ae 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);
b72ede27 162
d62a17ae 163 XFREE(MTYPE_VRF, vrf);
b72ede27
FL
164}
165
166/* Look up a VRF by identifier. */
d62a17ae 167struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
b72ede27 168{
d62a17ae 169 struct vrf vrf;
170 vrf.vrf_id = vrf_id;
171 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
b72ede27
FL
172}
173
e5bf3e1e 174/*
05e8e11e 175 * Check whether the VRF is enabled.
e5bf3e1e 176 */
d62a17ae 177static int vrf_is_enabled(struct vrf *vrf)
e5bf3e1e 178{
d62a17ae 179 return vrf && CHECK_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e
FL
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 */
d62a17ae 189int vrf_enable(struct vrf *vrf)
e5bf3e1e 190{
d62a17ae 191 if (vrf_is_enabled(vrf))
192 return 1;
05e8e11e 193
d62a17ae 194 if (debug_vrf)
195 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
e5bf3e1e 196
d62a17ae 197 SET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 198
d62a17ae 199 if (vrf_master.vrf_enable_hook)
200 (*vrf_master.vrf_enable_hook)(vrf);
e5bf3e1e 201
d62a17ae 202 return 1;
e5bf3e1e
FL
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 */
d62a17ae 210static void vrf_disable(struct vrf *vrf)
e5bf3e1e 211{
d62a17ae 212 if (!vrf_is_enabled(vrf))
213 return;
a647bfa8 214
d62a17ae 215 UNSET_FLAG(vrf->status, VRF_ACTIVE);
e5bf3e1e 216
d62a17ae 217 if (debug_vrf)
218 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
e5bf3e1e 219
d62a17ae 220 /* Till now, nothing to be done for the default VRF. */
221 // Pending: see why this statement.
e74f14fc 222
d62a17ae 223 if (vrf_master.vrf_disable_hook)
224 (*vrf_master.vrf_disable_hook)(vrf);
e5bf3e1e
FL
225}
226
d62a17ae 227vrf_id_t vrf_name_to_id(const char *name)
216b18ef 228{
d62a17ae 229 struct vrf *vrf;
230 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
231 // id/ routine not used.
216b18ef 232
d62a17ae 233 vrf = vrf_lookup_by_name(name);
234 if (vrf)
235 vrf_id = vrf->vrf_id;
216b18ef 236
d62a17ae 237 return vrf_id;
216b18ef
DS
238}
239
b72ede27 240/* Get the data pointer of the specified VRF. If not found, create one. */
d62a17ae 241void *vrf_info_get(vrf_id_t vrf_id)
b72ede27 242{
d62a17ae 243 struct vrf *vrf = vrf_get(vrf_id, NULL);
244 return vrf->info;
b72ede27
FL
245}
246
247/* Look up the data pointer of the specified VRF. */
d62a17ae 248void *vrf_info_lookup(vrf_id_t vrf_id)
b72ede27 249{
d62a17ae 250 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
251 return vrf ? vrf->info : NULL;
b72ede27
FL
252}
253
8736158a 254/* Look up the interface list in a VRF. */
d62a17ae 255struct list *vrf_iflist(vrf_id_t vrf_id)
8736158a 256{
d62a17ae 257 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
258 return vrf ? vrf->iflist : NULL;
8736158a
FL
259}
260
261/* Get the interface list of the specified VRF. Create one if not find. */
d62a17ae 262struct list *vrf_iflist_get(vrf_id_t vrf_id)
8736158a 263{
d62a17ae 264 struct vrf *vrf = vrf_get(vrf_id, NULL);
265 return vrf->iflist;
8736158a
FL
266}
267
7076bb2f
FL
268/*
269 * VRF bit-map
270 */
271
272#define VRF_BITMAP_NUM_OF_GROUPS 8
d62a17ae 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
283struct vrf_bitmap {
284 u_char *groups[VRF_BITMAP_NUM_OF_GROUPS];
7076bb2f
FL
285};
286
d62a17ae 287vrf_bitmap_t vrf_bitmap_init(void)
7076bb2f 288{
d62a17ae 289 return (vrf_bitmap_t)XCALLOC(MTYPE_VRF_BITMAP,
290 sizeof(struct vrf_bitmap));
7076bb2f
FL
291}
292
d62a17ae 293void vrf_bitmap_free(vrf_bitmap_t bmap)
7076bb2f 294{
d62a17ae 295 struct vrf_bitmap *bm = (struct vrf_bitmap *)bmap;
296 int i;
7076bb2f 297
d62a17ae 298 if (bmap == VRF_BITMAP_NULL)
299 return;
7076bb2f 300
d62a17ae 301 for (i = 0; i < VRF_BITMAP_NUM_OF_GROUPS; i++)
302 if (bm->groups[i])
303 XFREE(MTYPE_VRF_BITMAP, bm->groups[i]);
7076bb2f 304
d62a17ae 305 XFREE(MTYPE_VRF_BITMAP, bm);
7076bb2f
FL
306}
307
d62a17ae 308void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 309{
d62a17ae 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);
7076bb2f 313
d62a17ae 314 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN)
315 return;
7076bb2f 316
d62a17ae 317 if (bm->groups[group] == NULL)
318 bm->groups[group] = XCALLOC(MTYPE_VRF_BITMAP,
319 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP);
7076bb2f 320
d62a17ae 321 SET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
322 VRF_BITMAP_FLAG(offset));
7076bb2f
FL
323}
324
d62a17ae 325void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 326{
d62a17ae 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);
7076bb2f 330
d62a17ae 331 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
332 || bm->groups[group] == NULL)
333 return;
7076bb2f 334
d62a17ae 335 UNSET_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
336 VRF_BITMAP_FLAG(offset));
7076bb2f
FL
337}
338
d62a17ae 339int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id)
7076bb2f 340{
d62a17ae 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);
7076bb2f 344
d62a17ae 345 if (bmap == VRF_BITMAP_NULL || vrf_id == VRF_UNKNOWN
346 || bm->groups[group] == NULL)
347 return 0;
7076bb2f 348
d62a17ae 349 return CHECK_FLAG(bm->groups[group][VRF_BITMAP_INDEX_IN_GROUP(offset)],
350 VRF_BITMAP_FLAG(offset))
351 ? 1
352 : 0;
7076bb2f
FL
353}
354
d62a17ae 355static void vrf_autocomplete(vector comps, struct cmd_token *token)
d617d5fe 356{
d62a17ae 357 struct vrf *vrf = NULL;
d617d5fe 358
a2addae8 359 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 360 if (vrf->vrf_id != 0)
361 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
362 }
d617d5fe
DS
363}
364
365static const struct cmd_variable_handler vrf_var_handlers[] = {
d62a17ae 366 {
367 .varname = "vrf",
368 .completions = vrf_autocomplete,
369 },
370 {.completions = NULL},
d617d5fe
DS
371};
372
b72ede27 373/* Initialize VRF module. */
d62a17ae 374void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
375 int (*disable)(struct vrf *), int (*delete)(struct vrf *))
376{
377 struct vrf *default_vrf;
378
379 if (debug_vrf)
380 zlog_debug("%s: Initializing VRF subsystem",
381 __PRETTY_FUNCTION__);
382
383 vrf_master.vrf_new_hook = create;
384 vrf_master.vrf_enable_hook = enable;
385 vrf_master.vrf_disable_hook = disable;
386 vrf_master.vrf_delete_hook = delete;
387
388 /* The default VRF always exists. */
389 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
390 if (!default_vrf) {
391 zlog_err("vrf_init: failed to create the default VRF!");
392 exit(1);
393 }
394
395 /* Enable the default VRF. */
396 if (!vrf_enable(default_vrf)) {
397 zlog_err("vrf_init: failed to enable the default VRF!");
398 exit(1);
399 }
400
401 cmd_variable_handler_register(vrf_var_handlers);
b72ede27
FL
402}
403
404/* Terminate VRF module. */
d62a17ae 405void vrf_terminate(void)
b72ede27 406{
d62a17ae 407 struct vrf *vrf;
b72ede27 408
d62a17ae 409 if (debug_vrf)
410 zlog_debug("%s: Shutting down vrf subsystem",
411 __PRETTY_FUNCTION__);
19dc275e 412
d62a17ae 413 while ((vrf = RB_ROOT(vrf_id_head, &vrfs_by_id)) != NULL)
414 vrf_delete(vrf);
415 while ((vrf = RB_ROOT(vrf_name_head, &vrfs_by_name)) != NULL)
416 vrf_delete(vrf);
b72ede27
FL
417}
418
e5bf3e1e 419/* Create a socket for the VRF. */
d62a17ae 420int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id)
e5bf3e1e 421{
d62a17ae 422 int ret = -1;
e5bf3e1e 423
d62a17ae 424 ret = socket(domain, type, protocol);
e5bf3e1e 425
d62a17ae 426 return ret;
e5bf3e1e
FL
427}
428
f30c50b9 429/* vrf CLI commands */
505e5056 430DEFUN_NOSH (vrf,
f30c50b9 431 vrf_cmd,
d7a75a6c 432 "vrf NAME",
f30c50b9
RW
433 "Select a VRF to configure\n"
434 "VRF's name\n")
435{
d62a17ae 436 int idx_name = 1;
437 const char *vrfname = argv[idx_name]->arg;
438 struct vrf *vrfp;
f30c50b9 439
d62a17ae 440 if (strlen(vrfname) > VRF_NAMSIZ) {
441 vty_out(vty,
442 "%% VRF name %s is invalid: length exceeds "
443 "%d characters\n",
444 vrfname, VRF_NAMSIZ);
445 return CMD_WARNING_CONFIG_FAILED;
446 }
f30c50b9 447
d62a17ae 448 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
f30c50b9 449
d62a17ae 450 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
f30c50b9 451
d62a17ae 452 return CMD_SUCCESS;
f30c50b9
RW
453}
454
455DEFUN_NOSH (no_vrf,
456 no_vrf_cmd,
d7a75a6c 457 "no vrf NAME",
f30c50b9
RW
458 NO_STR
459 "Delete a pseudo VRF's configuration\n"
460 "VRF's name\n")
461{
d62a17ae 462 const char *vrfname = argv[2]->arg;
53dc2b05 463
d62a17ae 464 struct vrf *vrfp;
f30c50b9 465
d62a17ae 466 vrfp = vrf_lookup_by_name(vrfname);
f30c50b9 467
d62a17ae 468 if (vrfp == NULL) {
469 vty_out(vty, "%% VRF %s does not exist\n", vrfname);
470 return CMD_WARNING_CONFIG_FAILED;
471 }
f30c50b9 472
d62a17ae 473 if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
474 vty_out(vty, "%% Only inactive VRFs can be deleted\n");
475 return CMD_WARNING_CONFIG_FAILED;
476 }
f30c50b9 477
d62a17ae 478 vrf_delete(vrfp);
f30c50b9 479
d62a17ae 480 return CMD_SUCCESS;
f30c50b9
RW
481}
482
53dc2b05 483
d62a17ae 484struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
7ddcfca4 485
19dc275e
DS
486/*
487 * Debug CLI for vrf's
488 */
489DEFUN (vrf_debug,
490 vrf_debug_cmd,
491 "debug vrf",
492 DEBUG_STR
493 "VRF Debugging\n")
494{
d62a17ae 495 debug_vrf = 1;
19dc275e 496
d62a17ae 497 return CMD_SUCCESS;
19dc275e
DS
498}
499
500DEFUN (no_vrf_debug,
501 no_vrf_debug_cmd,
502 "no debug vrf",
503 NO_STR
504 DEBUG_STR
505 "VRF Debugging\n")
506{
d62a17ae 507 debug_vrf = 0;
19dc275e 508
d62a17ae 509 return CMD_SUCCESS;
19dc275e
DS
510}
511
d62a17ae 512static int vrf_write_host(struct vty *vty)
19dc275e 513{
d62a17ae 514 if (debug_vrf)
515 vty_out(vty, "debug vrf\n");
19dc275e 516
d62a17ae 517 return 1;
19dc275e
DS
518}
519
d62a17ae 520static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
19dc275e 521
d62a17ae 522void vrf_install_commands(void)
19dc275e 523{
d62a17ae 524 install_node(&vrf_debug_node, vrf_write_host);
19dc275e 525
d62a17ae 526 install_element(CONFIG_NODE, &vrf_debug_cmd);
527 install_element(ENABLE_NODE, &vrf_debug_cmd);
528 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
529 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
19dc275e 530}
53dc2b05 531
d62a17ae 532void vrf_cmd_init(int (*writefunc)(struct vty *vty))
7ddcfca4 533{
d62a17ae 534 install_element(CONFIG_NODE, &vrf_cmd);
535 install_element(CONFIG_NODE, &no_vrf_cmd);
536 install_node(&vrf_node, writefunc);
537 install_default(VRF_NODE);
19dc275e 538}