]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_vrf.c
Merge pull request #7378 from donaldsharp/pbr_ifp_leak
[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 #include "pbr_nht.h"
29 #include "pbr_zebra.h"
30
31 DEFINE_MTYPE_STATIC(PBRD, PBR_MAP_VRF, "PBR Map VRF")
32
33 static struct pbr_vrf *pbr_vrf_alloc(void)
34 {
35 struct pbr_vrf *pbr_vrf;
36
37 pbr_vrf = XCALLOC(MTYPE_PBR_MAP_VRF, sizeof(struct pbr_vrf));
38
39 return pbr_vrf;
40 }
41
42 static void pbr_vrf_free(struct pbr_vrf *pbr_vrf)
43 {
44 XFREE(MTYPE_PBR_MAP_VRF, pbr_vrf);
45 }
46
47 static int pbr_vrf_new(struct vrf *vrf)
48 {
49 struct pbr_vrf *pbr_vrf;
50
51 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
52
53 pbr_vrf = pbr_vrf_alloc();
54 vrf->info = pbr_vrf;
55 pbr_vrf->vrf = vrf;
56
57 return 0;
58 }
59
60 static int pbr_vrf_enable(struct vrf *vrf)
61 {
62 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
63
64 pbr_nht_vrf_update(vrf->info);
65 pbr_map_vrf_update(vrf->info);
66
67 return 0;
68 }
69
70 static int pbr_vrf_disable(struct vrf *vrf)
71 {
72 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
73
74 pbr_map_vrf_update(vrf->info);
75
76 return 0;
77 }
78
79 static int pbr_vrf_delete(struct vrf *vrf)
80 {
81 DEBUGD(&pbr_dbg_event, "%s: %u (%s)", __func__, vrf->vrf_id, vrf->name);
82
83 /*
84 * Make sure vrf is always marked disabled first so we handle
85 * pbr rules using it.
86 */
87 assert(!vrf_is_enabled(vrf));
88
89 pbr_vrf_free(vrf->info);
90 vrf->info = NULL;
91
92 return 0;
93 }
94
95 struct pbr_vrf *pbr_vrf_lookup_by_id(vrf_id_t vrf_id)
96 {
97 struct vrf *vrf;
98
99 vrf = vrf_lookup_by_id(vrf_id);
100 if (vrf)
101 return ((struct pbr_vrf *)vrf->info);
102
103 return NULL;
104 }
105
106 struct pbr_vrf *pbr_vrf_lookup_by_name(const char *name)
107 {
108 struct vrf *vrf;
109
110 if (!name)
111 name = VRF_DEFAULT_NAME;
112
113 vrf = vrf_lookup_by_name(name);
114 if (vrf)
115 return ((struct pbr_vrf *)vrf->info);
116
117 return NULL;
118 }
119
120 bool pbr_vrf_is_enabled(const struct pbr_vrf *pbr_vrf)
121 {
122 return vrf_is_enabled(pbr_vrf->vrf) ? true : false;
123 }
124
125 bool pbr_vrf_is_valid(const struct pbr_vrf *pbr_vrf)
126 {
127 if (vrf_is_backend_netns())
128 return false;
129
130 if (!pbr_vrf->vrf)
131 return false;
132
133 return pbr_vrf_is_enabled(pbr_vrf);
134 }
135
136 void pbr_vrf_init(void)
137 {
138 vrf_init(pbr_vrf_new, pbr_vrf_enable, pbr_vrf_disable, pbr_vrf_delete,
139 NULL);
140 }
141
142 void pbr_vrf_terminate(void)
143 {
144 struct vrf *vrf;
145 struct interface *ifp;
146
147 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
148 FOR_ALL_INTERFACES (vrf, ifp)
149 pbr_if_del(ifp);
150 }
151 }