2 * Copyright (C) 2018 Cumulus Networks, Inc.
5 * This file is part of FRR.
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
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.
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
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"
30 struct zebra_router zrouter
;
33 zebra_router_table_entry_compare(const struct zebra_router_table
*e1
,
34 const struct zebra_router_table
*e2
);
36 RB_GENERATE(zebra_router_table_head
, zebra_router_table
,
37 zebra_router_table_entry
, zebra_router_table_entry_compare
);
41 zebra_router_table_entry_compare(const struct zebra_router_table
*e1
,
42 const struct zebra_router_table
*e2
)
44 if (e1
->tableid
< e2
->tableid
)
46 if (e1
->tableid
> e2
->tableid
)
48 if (e1
->ns_id
< e2
->ns_id
)
50 if (e1
->ns_id
> e2
->ns_id
)
52 if (e1
->afi
< e2
->afi
)
54 if (e1
->afi
> e2
->afi
)
56 return (e1
->safi
- e2
->safi
);
60 struct route_table
*zebra_router_find_table(struct zebra_vrf
*zvrf
,
61 uint32_t tableid
, afi_t afi
,
64 struct zebra_router_table finder
;
65 struct zebra_router_table
*zrt
;
67 memset(&finder
, 0, sizeof(finder
));
70 finder
.tableid
= tableid
;
71 finder
.ns_id
= zvrf
->zns
->ns_id
;
72 zrt
= RB_FIND(zebra_router_table_head
, &zrouter
.tables
, &finder
);
80 struct route_table
*zebra_router_get_table(struct zebra_vrf
*zvrf
,
81 uint32_t tableid
, afi_t afi
,
84 struct zebra_router_table finder
;
85 struct zebra_router_table
*zrt
;
86 rib_table_info_t
*info
;
88 memset(&finder
, 0, sizeof(finder
));
91 finder
.tableid
= tableid
;
92 finder
.ns_id
= zvrf
->zns
->ns_id
;
93 zrt
= RB_FIND(zebra_router_table_head
, &zrouter
.tables
, &finder
);
98 zrt
= XCALLOC(MTYPE_ZEBRA_NS
, sizeof(*zrt
));
99 zrt
->tableid
= tableid
;
102 zrt
->ns_id
= zvrf
->zns
->ns_id
;
104 (afi
== AFI_IP6
) ? srcdest_table_init() : route_table_init();
106 info
= XCALLOC(MTYPE_RIB_TABLE_INFO
, sizeof(*info
));
109 info
->safi
= SAFI_UNICAST
;
110 route_table_set_info(zrt
->table
, info
);
111 zrt
->table
->cleanup
= zebra_rtable_node_cleanup
;
113 RB_INSERT(zebra_router_table_head
, &zrouter
.tables
, zrt
);
117 unsigned long zebra_router_score_proto(uint8_t proto
, unsigned short instance
)
119 struct zebra_router_table
*zrt
;
120 unsigned long cnt
= 0;
122 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
123 if (zrt
->ns_id
!= NS_DEFAULT
)
125 cnt
+= rib_score_proto_table(proto
, instance
, zrt
->table
);
130 void zebra_router_sweep_route(void)
132 struct zebra_router_table
*zrt
;
134 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
135 if (zrt
->ns_id
!= NS_DEFAULT
)
137 rib_sweep_table(zrt
->table
);
141 static void zebra_router_free_table(struct zebra_router_table
*zrt
)
145 rib_close_table(zrt
->table
);
147 table_info
= route_table_get_info(zrt
->table
);
148 route_table_finish(zrt
->table
);
149 XFREE(MTYPE_RIB_TABLE_INFO
, table_info
);
150 XFREE(MTYPE_ZEBRA_NS
, zrt
);
153 void zebra_router_terminate(void)
155 struct zebra_router_table
*zrt
, *tmp
;
157 RB_FOREACH_SAFE (zrt
, zebra_router_table_head
, &zrouter
.tables
, tmp
) {
158 RB_REMOVE(zebra_router_table_head
, &zrouter
.tables
, zrt
);
159 zebra_router_free_table(zrt
);
162 zebra_vxlan_disable();
163 zebra_mlag_terminate();
165 hash_clean(zrouter
.rules_hash
, zebra_pbr_rules_free
);
166 hash_free(zrouter
.rules_hash
);
168 hash_clean(zrouter
.ipset_entry_hash
, zebra_pbr_ipset_entry_free
),
169 hash_clean(zrouter
.ipset_hash
, zebra_pbr_ipset_free
);
170 hash_free(zrouter
.ipset_hash
);
171 hash_free(zrouter
.ipset_entry_hash
);
172 hash_clean(zrouter
.iptable_hash
, zebra_pbr_iptable_free
);
173 hash_free(zrouter
.iptable_hash
);
176 void zebra_router_init(void)
181 zrouter
.rules_hash
= hash_create_size(8, zebra_pbr_rules_hash_key
,
182 zebra_pbr_rules_hash_equal
,
186 hash_create_size(8, zebra_pbr_ipset_hash_key
,
187 zebra_pbr_ipset_hash_equal
, "IPset Hash");
189 zrouter
.ipset_entry_hash
= hash_create_size(
190 8, zebra_pbr_ipset_entry_hash_key
,
191 zebra_pbr_ipset_entry_hash_equal
, "IPset Hash Entry");
193 zrouter
.iptable_hash
= hash_create_size(8, zebra_pbr_iptable_hash_key
,
194 zebra_pbr_iptable_hash_equal
,
195 "IPtable Hash Entry");