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