]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_router.c
zebra: Start breakup of zns into zrouter and zns
[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
27 struct zebra_router zrouter;
28
29 static inline int
30 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
31 const struct zebra_router_table *e2);
32
33 RB_GENERATE(zebra_router_table_head, zebra_router_table,
34 zebra_router_table_entry, zebra_router_table_entry_compare);
35
36
37 static inline int
38 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
39 const struct zebra_router_table *e2)
40 {
41 if (e1->tableid < e2->tableid)
42 return -1;
43 if (e1->tableid > e2->tableid)
44 return 1;
45 if (e1->ns_id < e2->ns_id)
46 return -1;
47 if (e1->ns_id > e2->ns_id)
48 return 1;
49 if (e1->afi < e2->afi)
50 return -1;
51 if (e1->afi > e2->afi)
52 return 1;
53 return (e1->safi - e2->safi);
54 }
55
56
57 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
58 uint32_t tableid, afi_t afi,
59 safi_t safi)
60 {
61 struct zebra_router_table finder;
62 struct zebra_router_table *zrt;
63
64 memset(&finder, 0, sizeof(finder));
65 finder.afi = afi;
66 finder.safi = safi;
67 finder.tableid = tableid;
68 finder.ns_id = zvrf->zns->ns_id;
69 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
70
71 if (zrt)
72 return zrt->table;
73 else
74 return NULL;
75 }
76
77 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
78 uint32_t tableid, afi_t afi,
79 safi_t safi)
80 {
81 struct zebra_router_table finder;
82 struct zebra_router_table *zrt;
83 rib_table_info_t *info;
84
85 memset(&finder, 0, sizeof(finder));
86 finder.afi = afi;
87 finder.safi = safi;
88 finder.tableid = tableid;
89 finder.ns_id = zvrf->zns->ns_id;
90 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
91
92 if (zrt)
93 return zrt->table;
94
95 zrt = XCALLOC(MTYPE_ZEBRA_NS, sizeof(*zrt));
96 zrt->tableid = tableid;
97 zrt->afi = afi;
98 zrt->ns_id = zvrf->zns->ns_id;
99 zrt->table =
100 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
101
102 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
103 info->zvrf = zvrf;
104 info->afi = afi;
105 info->safi = SAFI_UNICAST;
106 route_table_set_info(zrt->table, info);
107 zrt->table->cleanup = zebra_rtable_node_cleanup;
108
109 RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
110 return zrt->table;
111 }
112
113 unsigned long zebra_router_score_proto(uint8_t proto, unsigned short instance)
114 {
115 struct zebra_router_table *zrt;
116 unsigned long cnt = 0;
117
118 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
119 if (zrt->ns_id != NS_DEFAULT)
120 continue;
121 cnt += rib_score_proto_table(proto, instance, zrt->table);
122 }
123 return cnt;
124 }
125
126 void zebra_router_sweep_route(void)
127 {
128 struct zebra_router_table *zrt;
129
130 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
131 if (zrt->ns_id != NS_DEFAULT)
132 continue;
133 rib_sweep_table(zrt->table);
134 }
135 }
136
137 static void zebra_router_free_table(struct zebra_router_table *zrt)
138 {
139 void *table_info;
140
141 rib_close_table(zrt->table);
142
143 table_info = route_table_get_info(zrt->table);
144 route_table_finish(zrt->table);
145 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
146 XFREE(MTYPE_ZEBRA_NS, zrt);
147 }
148
149 void zebra_router_terminate(void)
150 {
151 struct zebra_router_table *zrt, *tmp;
152
153 RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp) {
154 RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
155 zebra_router_free_table(zrt);
156 }
157 }
158
159 void zebra_router_init(void)
160 {
161 zrouter.l3vni_table = NULL;
162 }