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