]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_router.c
lib: enforce vrf_name_to_id by returning default_vrf when name is null
[mirror_frr.git] / zebra / zebra_router.c
1 /* Zebra Router Code.
2 * Copyright (C) 2018 Cumulus Networks, Inc.
3 * Donald Sharp
4 *
5 * This file is part of FRR.
6 *
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * FRR 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
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22 #include "zebra.h"
23
24 #include "zebra_router.h"
25 #include "zebra_memory.h"
26 #include "zebra_pbr.h"
27 #include "zebra_vxlan.h"
28
29 struct zebra_router zrouter;
30
31 static inline int
32 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
33 const struct zebra_router_table *e2);
34
35 RB_GENERATE(zebra_router_table_head, zebra_router_table,
36 zebra_router_table_entry, zebra_router_table_entry_compare);
37
38
39 static inline int
40 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
41 const struct zebra_router_table *e2)
42 {
43 if (e1->tableid < e2->tableid)
44 return -1;
45 if (e1->tableid > e2->tableid)
46 return 1;
47 if (e1->ns_id < e2->ns_id)
48 return -1;
49 if (e1->ns_id > e2->ns_id)
50 return 1;
51 if (e1->afi < e2->afi)
52 return -1;
53 if (e1->afi > e2->afi)
54 return 1;
55 return (e1->safi - e2->safi);
56 }
57
58
59 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
60 uint32_t tableid, afi_t afi,
61 safi_t safi)
62 {
63 struct zebra_router_table finder;
64 struct zebra_router_table *zrt;
65
66 memset(&finder, 0, sizeof(finder));
67 finder.afi = afi;
68 finder.safi = safi;
69 finder.tableid = tableid;
70 finder.ns_id = zvrf->zns->ns_id;
71 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
72
73 if (zrt)
74 return zrt->table;
75 else
76 return NULL;
77 }
78
79 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
80 uint32_t tableid, afi_t afi,
81 safi_t safi)
82 {
83 struct zebra_router_table finder;
84 struct zebra_router_table *zrt;
85 rib_table_info_t *info;
86
87 memset(&finder, 0, sizeof(finder));
88 finder.afi = afi;
89 finder.safi = safi;
90 finder.tableid = tableid;
91 finder.ns_id = zvrf->zns->ns_id;
92 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
93
94 if (zrt)
95 return zrt->table;
96
97 zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt));
98 zrt->tableid = tableid;
99 zrt->afi = afi;
100 zrt->safi = safi;
101 zrt->ns_id = zvrf->zns->ns_id;
102 zrt->table =
103 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
104
105 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
106 info->zvrf = zvrf;
107 info->afi = afi;
108 info->safi = SAFI_UNICAST;
109 route_table_set_info(zrt->table, info);
110 zrt->table->cleanup = zebra_rtable_node_cleanup;
111
112 RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
113 return zrt->table;
114 }
115
116 unsigned long zebra_router_score_proto(uint8_t proto, unsigned short instance)
117 {
118 struct zebra_router_table *zrt;
119 unsigned long cnt = 0;
120
121 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
122 if (zrt->ns_id != NS_DEFAULT)
123 continue;
124 cnt += rib_score_proto_table(proto, instance, zrt->table);
125 }
126 return cnt;
127 }
128
129 void zebra_router_sweep_route(void)
130 {
131 struct zebra_router_table *zrt;
132
133 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
134 if (zrt->ns_id != NS_DEFAULT)
135 continue;
136 rib_sweep_table(zrt->table);
137 }
138 }
139
140 static void zebra_router_free_table(struct zebra_router_table *zrt)
141 {
142 void *table_info;
143
144 rib_close_table(zrt->table);
145
146 table_info = route_table_get_info(zrt->table);
147 route_table_finish(zrt->table);
148 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
149 XFREE(MTYPE_ZEBRA_NS, zrt);
150 }
151
152 void zebra_router_terminate(void)
153 {
154 struct zebra_router_table *zrt, *tmp;
155
156 RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp) {
157 RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
158 zebra_router_free_table(zrt);
159 }
160
161 zebra_vxlan_disable();
162 hash_clean(zrouter.rules_hash, zebra_pbr_rules_free);
163 hash_free(zrouter.rules_hash);
164
165 hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free),
166 hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free);
167 hash_free(zrouter.ipset_hash);
168 hash_free(zrouter.ipset_entry_hash);
169 hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free);
170 hash_free(zrouter.iptable_hash);
171 }
172
173 void zebra_router_init(void)
174 {
175 zebra_vxlan_init();
176 zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key,
177 zebra_pbr_rules_hash_equal,
178 "Rules Hash");
179
180 zrouter.ipset_hash =
181 hash_create_size(8, zebra_pbr_ipset_hash_key,
182 zebra_pbr_ipset_hash_equal, "IPset Hash");
183
184 zrouter.ipset_entry_hash = hash_create_size(
185 8, zebra_pbr_ipset_entry_hash_key,
186 zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry");
187
188 zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key,
189 zebra_pbr_iptable_hash_equal,
190 "IPtable Hash Entry");
191 }