]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_router.c
Merge pull request #3567 from donaldsharp/cleanup_route_table_creation
[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 #include "zebra_mlag.h"
29
30 struct zebra_router zrouter;
31
32 static inline int
33 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
34 const struct zebra_router_table *e2);
35
36 RB_GENERATE(zebra_router_table_head, zebra_router_table,
37 zebra_router_table_entry, zebra_router_table_entry_compare);
38
39
40 static inline int
41 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
42 const struct zebra_router_table *e2)
43 {
44 if (e1->tableid < e2->tableid)
45 return -1;
46 if (e1->tableid > e2->tableid)
47 return 1;
48 if (e1->ns_id < e2->ns_id)
49 return -1;
50 if (e1->ns_id > e2->ns_id)
51 return 1;
52 if (e1->afi < e2->afi)
53 return -1;
54 if (e1->afi > e2->afi)
55 return 1;
56 return (e1->safi - e2->safi);
57 }
58
59
60 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
61 uint32_t tableid, afi_t afi,
62 safi_t safi)
63 {
64 struct zebra_router_table finder;
65 struct zebra_router_table *zrt;
66
67 memset(&finder, 0, sizeof(finder));
68 finder.afi = afi;
69 finder.safi = safi;
70 finder.tableid = tableid;
71 finder.ns_id = zvrf->zns->ns_id;
72 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
73
74 if (zrt)
75 return zrt->table;
76 else
77 return NULL;
78 }
79
80 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
81 uint32_t tableid, afi_t afi,
82 safi_t safi)
83 {
84 struct zebra_router_table finder;
85 struct zebra_router_table *zrt;
86 rib_table_info_t *info;
87
88 memset(&finder, 0, sizeof(finder));
89 finder.afi = afi;
90 finder.safi = safi;
91 finder.tableid = tableid;
92 finder.ns_id = zvrf->zns->ns_id;
93 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
94
95 if (zrt)
96 return zrt->table;
97
98 zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt));
99 zrt->tableid = tableid;
100 zrt->afi = afi;
101 zrt->safi = safi;
102 zrt->ns_id = zvrf->zns->ns_id;
103 zrt->table =
104 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
105
106 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
107 info->zvrf = zvrf;
108 info->afi = afi;
109 info->safi = safi;
110 route_table_set_info(zrt->table, info);
111 zrt->table->cleanup = zebra_rtable_node_cleanup;
112
113 RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
114 return zrt->table;
115 }
116
117 unsigned long zebra_router_score_proto(uint8_t proto, unsigned short instance)
118 {
119 struct zebra_router_table *zrt;
120 unsigned long cnt = 0;
121
122 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
123 if (zrt->ns_id != NS_DEFAULT)
124 continue;
125 cnt += rib_score_proto_table(proto, instance, zrt->table);
126 }
127 return cnt;
128 }
129
130 void zebra_router_show_table_summary(struct vty *vty)
131 {
132 struct zebra_router_table *zrt;
133
134 vty_out(vty,
135 "VRF NS ID VRF ID AFI SAFI Table Count\n");
136 vty_out(vty,
137 "---------------------------------------------------------------------------\n");
138 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
139 rib_table_info_t *info = route_table_get_info(zrt->table);
140
141 vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name,
142 zrt->ns_id, info->zvrf->vrf->vrf_id,
143 afi2str(zrt->afi), safi2str(zrt->safi),
144 zrt->tableid,
145 zrt->table->count);
146 }
147 }
148
149 void zebra_router_sweep_route(void)
150 {
151 struct zebra_router_table *zrt;
152
153 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
154 if (zrt->ns_id != NS_DEFAULT)
155 continue;
156 rib_sweep_table(zrt->table);
157 }
158 }
159
160 static void zebra_router_free_table(struct zebra_router_table *zrt)
161 {
162 void *table_info;
163
164 rib_close_table(zrt->table);
165
166 table_info = route_table_get_info(zrt->table);
167 route_table_finish(zrt->table);
168 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
169 XFREE(MTYPE_ZEBRA_NS, zrt);
170 }
171
172 void zebra_router_terminate(void)
173 {
174 struct zebra_router_table *zrt, *tmp;
175
176 RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp) {
177 RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
178 zebra_router_free_table(zrt);
179 }
180
181 zebra_vxlan_disable();
182 zebra_mlag_terminate();
183
184 hash_clean(zrouter.rules_hash, zebra_pbr_rules_free);
185 hash_free(zrouter.rules_hash);
186
187 hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free),
188 hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free);
189 hash_free(zrouter.ipset_hash);
190 hash_free(zrouter.ipset_entry_hash);
191 hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free);
192 hash_free(zrouter.iptable_hash);
193 }
194
195 void zebra_router_init(void)
196 {
197 zebra_vxlan_init();
198 zebra_mlag_init();
199
200 zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key,
201 zebra_pbr_rules_hash_equal,
202 "Rules Hash");
203
204 zrouter.ipset_hash =
205 hash_create_size(8, zebra_pbr_ipset_hash_key,
206 zebra_pbr_ipset_hash_equal, "IPset Hash");
207
208 zrouter.ipset_entry_hash = hash_create_size(
209 8, zebra_pbr_ipset_entry_hash_key,
210 zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry");
211
212 zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key,
213 zebra_pbr_iptable_hash_equal,
214 "IPtable Hash Entry");
215 }