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