]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_router.c
Merge pull request #4447 from donaldsharp/pim_unused_functions
[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 <pthread.h>
25 #include "lib/frratomic.h"
26
27 #include "zebra_router.h"
28 #include "zebra_memory.h"
29 #include "zebra_pbr.h"
30 #include "zebra_vxlan.h"
31 #include "zebra_mlag.h"
32 #include "zebra_nhg.h"
33 #include "debug.h"
34
35 struct zebra_router zrouter = {
36 .multipath_num = MULTIPATH_NUM,
37 .ipv4_multicast_mode = MCAST_NO_CONFIG,
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 RB_GENERATE(zebra_router_table_head, zebra_router_table,
45 zebra_router_table_entry, zebra_router_table_entry_compare);
46
47
48 static inline int
49 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
50 const struct zebra_router_table *e2)
51 {
52 if (e1->tableid < e2->tableid)
53 return -1;
54 if (e1->tableid > e2->tableid)
55 return 1;
56 if (e1->ns_id < e2->ns_id)
57 return -1;
58 if (e1->ns_id > e2->ns_id)
59 return 1;
60 if (e1->afi < e2->afi)
61 return -1;
62 if (e1->afi > e2->afi)
63 return 1;
64 return (e1->safi - e2->safi);
65 }
66
67
68 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
69 uint32_t tableid, afi_t afi,
70 safi_t safi)
71 {
72 struct zebra_router_table finder;
73 struct zebra_router_table *zrt;
74
75 memset(&finder, 0, sizeof(finder));
76 finder.afi = afi;
77 finder.safi = safi;
78 finder.tableid = tableid;
79 finder.ns_id = zvrf->zns->ns_id;
80 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
81
82 if (zrt)
83 return zrt->table;
84 else
85 return NULL;
86 }
87
88 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
89 uint32_t tableid, afi_t afi,
90 safi_t safi)
91 {
92 struct zebra_router_table finder;
93 struct zebra_router_table *zrt;
94 rib_table_info_t *info;
95
96 memset(&finder, 0, sizeof(finder));
97 finder.afi = afi;
98 finder.safi = safi;
99 finder.tableid = tableid;
100 finder.ns_id = zvrf->zns->ns_id;
101 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
102
103 if (zrt)
104 return zrt->table;
105
106 zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt));
107 zrt->tableid = tableid;
108 zrt->afi = afi;
109 zrt->safi = safi;
110 zrt->ns_id = zvrf->zns->ns_id;
111 zrt->table =
112 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
113
114 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
115 info->zvrf = zvrf;
116 info->afi = afi;
117 info->safi = safi;
118 route_table_set_info(zrt->table, info);
119 zrt->table->cleanup = zebra_rtable_node_cleanup;
120
121 RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
122 return zrt->table;
123 }
124
125 void zebra_router_show_table_summary(struct vty *vty)
126 {
127 struct zebra_router_table *zrt;
128
129 vty_out(vty,
130 "VRF NS ID VRF ID AFI SAFI Table Count\n");
131 vty_out(vty,
132 "---------------------------------------------------------------------------\n");
133 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
134 rib_table_info_t *info = route_table_get_info(zrt->table);
135
136 vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name,
137 zrt->ns_id, info->zvrf->vrf->vrf_id,
138 afi2str(zrt->afi), safi2str(zrt->safi),
139 zrt->tableid,
140 zrt->table->count);
141 }
142 }
143
144 void zebra_router_sweep_route(void)
145 {
146 struct zebra_router_table *zrt;
147
148 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
149 if (zrt->ns_id != NS_DEFAULT)
150 continue;
151 rib_sweep_table(zrt->table);
152 }
153 }
154
155 static void zebra_router_free_table(struct zebra_router_table *zrt)
156 {
157 void *table_info;
158
159 table_info = route_table_get_info(zrt->table);
160 route_table_finish(zrt->table);
161 RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
162
163 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
164 XFREE(MTYPE_ZEBRA_NS, zrt);
165 }
166
167 void zebra_router_release_table(struct zebra_vrf *zvrf, uint32_t tableid,
168 afi_t afi, safi_t safi)
169 {
170 struct zebra_router_table finder;
171 struct zebra_router_table *zrt;
172
173 memset(&finder, 0, sizeof(finder));
174 finder.afi = afi;
175 finder.safi = safi;
176 finder.tableid = tableid;
177 finder.ns_id = zvrf->zns->ns_id;
178 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
179
180 if (!zrt)
181 return;
182
183 zebra_router_free_table(zrt);
184 }
185
186 uint32_t zebra_router_get_next_sequence(void)
187 {
188 return 1
189 + atomic_fetch_add_explicit(&zrouter.sequence_num, 1,
190 memory_order_relaxed);
191 }
192
193 void multicast_mode_ipv4_set(enum multicast_mode mode)
194 {
195 if (IS_ZEBRA_DEBUG_RIB)
196 zlog_debug("%s: multicast lookup mode set (%d)", __func__,
197 mode);
198 zrouter.ipv4_multicast_mode = mode;
199 }
200
201 enum multicast_mode multicast_mode_ipv4_get(void)
202 {
203 return zrouter.ipv4_multicast_mode;
204 }
205
206 void zebra_router_terminate(void)
207 {
208 struct zebra_router_table *zrt, *tmp;
209
210 RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp)
211 zebra_router_free_table(zrt);
212
213 work_queue_free_and_null(&zrouter.ribq);
214 meta_queue_free(zrouter.mq);
215
216 zebra_vxlan_disable();
217 zebra_mlag_terminate();
218
219 hash_clean(zrouter.rules_hash, zebra_pbr_rules_free);
220 hash_free(zrouter.rules_hash);
221
222 hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free),
223 hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free);
224 hash_free(zrouter.ipset_hash);
225 hash_free(zrouter.ipset_entry_hash);
226 hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free);
227 hash_free(zrouter.iptable_hash);
228 }
229
230 void zebra_router_init(void)
231 {
232 zrouter.sequence_num = 0;
233
234 zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS;
235
236 zebra_vxlan_init();
237 zebra_mlag_init();
238
239 zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key,
240 zebra_pbr_rules_hash_equal,
241 "Rules Hash");
242
243 zrouter.ipset_hash =
244 hash_create_size(8, zebra_pbr_ipset_hash_key,
245 zebra_pbr_ipset_hash_equal, "IPset Hash");
246
247 zrouter.ipset_entry_hash = hash_create_size(
248 8, zebra_pbr_ipset_entry_hash_key,
249 zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry");
250
251 zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key,
252 zebra_pbr_iptable_hash_equal,
253 "IPtable Hash Entry");
254 }