]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_vrf.c
Merge pull request #4765 from opensourcerouting/defaults-v2
[mirror_frr.git] / pbrd / pbr_vrf.c
1 /*
2 * PBR - vrf code
3 * Copyright (C) 2019 Cumulus Networks, Inc.
4 * Stephen Worley
5 *
6 * FRR 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
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for 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
22 #include "vrf.h"
23
24 #include "pbr_vrf.h"
25 #include "pbr_memory.h"
26 #include "pbr_map.h"
27 #include "pbr_debug.h"
28
29 DEFINE_MTYPE_STATIC(PBRD, PBR_MAP_VRF, "PBR Map VRF")
30
31 static struct pbr_vrf *pbr_vrf_alloc(void)
32 {
33 struct pbr_vrf *pbr_vrf;
34
35 pbr_vrf = XCALLOC(MTYPE_PBR_MAP_VRF, sizeof(struct pbr_vrf));
36
37 return pbr_vrf;
38 }
39
40 static void pbr_vrf_free(struct pbr_vrf *pbr_vrf)
41 {
42 XFREE(MTYPE_PBR_MAP_VRF, pbr_vrf);
43 }
44
45 static int pbr_vrf_new(struct vrf *vrf)
46 {
47 struct pbr_vrf *pbr_vrf;
48
49 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
50
51 pbr_vrf = pbr_vrf_alloc();
52 vrf->info = pbr_vrf;
53 pbr_vrf->vrf = vrf;
54
55 return 0;
56 }
57
58 static int pbr_vrf_enable(struct vrf *vrf)
59 {
60 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
61
62 pbr_map_vrf_update(vrf->info);
63
64 return 0;
65 }
66
67 static int pbr_vrf_disable(struct vrf *vrf)
68 {
69 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
70
71 pbr_map_vrf_update(vrf->info);
72
73 return 0;
74 }
75
76 static int pbr_vrf_delete(struct vrf *vrf)
77 {
78 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
79
80 /*
81 * Make sure vrf is always marked disabled first so we handle
82 * pbr rules using it.
83 */
84 assert(!vrf_is_enabled(vrf));
85
86 pbr_vrf_free(vrf->info);
87 vrf->info = NULL;
88
89 return 0;
90 }
91
92 struct pbr_vrf *pbr_vrf_lookup_by_id(vrf_id_t vrf_id)
93 {
94 struct vrf *vrf;
95
96 vrf = vrf_lookup_by_id(vrf_id);
97 if (vrf)
98 return ((struct pbr_vrf *)vrf->info);
99
100 return NULL;
101 }
102
103 struct pbr_vrf *pbr_vrf_lookup_by_name(const char *name)
104 {
105 struct vrf *vrf;
106
107 if (!name)
108 name = VRF_DEFAULT_NAME;
109
110 vrf = vrf_lookup_by_name(name);
111 if (vrf)
112 return ((struct pbr_vrf *)vrf->info);
113
114 return NULL;
115 }
116
117 bool pbr_vrf_is_enabled(const struct pbr_vrf *pbr_vrf)
118 {
119 return vrf_is_enabled(pbr_vrf->vrf) ? true : false;
120 }
121
122 bool pbr_vrf_is_valid(const struct pbr_vrf *pbr_vrf)
123 {
124 if (vrf_is_backend_netns())
125 return false;
126
127 if (!pbr_vrf->vrf)
128 return false;
129
130 return pbr_vrf_is_enabled(pbr_vrf);
131 }
132
133 void pbr_vrf_init(void)
134 {
135 vrf_init(pbr_vrf_new, pbr_vrf_enable, pbr_vrf_disable, pbr_vrf_delete,
136 NULL);
137 }