1 /* Zebra Router header.
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 #ifndef __ZEBRA_ROUTER_H__
23 #define __ZEBRA_ROUTER_H__
24 
25 #include "lib/mlag.h"
26 
27 #include "zebra/zebra_ns.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /*
34  * This header file contains the idea of a router and as such
35  * owns data that is associated with a router from zebra's
36  * perspective.
37  */
38 
39 struct zebra_router_table {
40 	RB_ENTRY(zebra_router_table) zebra_router_table_entry;
41 
42 	uint32_t tableid;
43 	afi_t afi;
44 	safi_t safi;
45 	ns_id_t ns_id;
46 
47 	struct route_table *table;
48 };
49 RB_HEAD(zebra_router_table_head, zebra_router_table);
50 RB_PROTOTYPE(zebra_router_table_head, zebra_router_table,
51 	     zebra_router_table_entry, zebra_router_table_entry_compare)
52 
53 /* RPF lookup behaviour */
54 enum multicast_mode {
55 	MCAST_NO_CONFIG = 0,  /* MIX_MRIB_FIRST, but no show in config write */
56 	MCAST_MRIB_ONLY,      /* MRIB only */
57 	MCAST_URIB_ONLY,      /* URIB only */
58 	MCAST_MIX_MRIB_FIRST, /* MRIB, if nothing at all then URIB */
59 	MCAST_MIX_DISTANCE,   /* MRIB & URIB, lower distance wins */
60 	MCAST_MIX_PFXLEN,     /* MRIB & URIB, longer prefix wins */
61 			      /* on equal value, MRIB wins for last 2 */
62 };
63 
64 struct zebra_mlag_info {
65 	/* Role this zebra router is playing */
66 	enum mlag_role role;
67 
68 	/* The peerlink being used for mlag */
69 	char *peerlink;
70 	ifindex_t peerlink_ifindex;
71 
72 	/* The system mac being used */
73 	struct ethaddr mac;
74 	/*
75 	 * Zebra will open the communication channel with MLAGD only if any
76 	 * clients are interested and it is controlled dynamically based on
77 	 * client registers & un-registers.
78 	 */
79 	uint32_t clients_interested_cnt;
80 
81 	/* coomunication channel with MLAGD is established */
82 	bool connected;
83 
84 	/* connection retry timer is running */
85 	bool timer_running;
86 
87 	/* Holds the client data(unencoded) that need to be pushed to MCLAGD*/
88 	struct stream_fifo *mlag_fifo;
89 
90 	/*
91 	 * A new Kernel thread will be created to post the data to MCLAGD.
92 	 * where as, read will be performed from the zebra main thread, because
93 	 * read involves accessing client registartion data structures.
94 	 */
95 	struct frr_pthread *zebra_pth_mlag;
96 
97 	/* MLAG Thread context 'master' */
98 	struct thread_master *th_master;
99 
100 	/*
101 	 * Event for Initial MLAG Connection setup & Data Read
102 	 * Read can be performed only after successful connection establishment,
103 	 * so no issues.
104 	 *
105 	 */
106 	struct thread *t_read;
107 	/* Event for MLAG write */
108 	struct thread *t_write;
109 };
110 
111 struct zebra_router {
112 	atomic_bool in_shutdown;
113 
114 	/* Thread master */
115 	struct thread_master *master;
116 
117 	/* Lists of clients who have connected to us */
118 	struct list *client_list;
119 
120 	/* List of clients in GR */
121 	struct list *stale_client_list;
122 
123 	struct zebra_router_table_head tables;
124 
125 	/* L3-VNI hash table (for EVPN). Only in default instance */
126 	struct hash *l3vni_table;
127 
128 	/* Tables and other global info maintained for EVPN multihoming */
129 	struct zebra_evpn_mh_info *mh_info;
130 
131 	/* EVPN MH broadcast domains indexed by the VID */
132 	struct hash *evpn_vlan_table;
133 
134 	struct hash *rules_hash;
135 
136 	struct hash *ipset_hash;
137 
138 	struct hash *ipset_entry_hash;
139 
140 	struct hash *iptable_hash;
141 
142 	/* used if vrf backend is not network namespace */
143 	int rtadv_sock;
144 
145 	/* A sequence number used for tracking routes */
146 	_Atomic uint32_t sequence_num;
147 
148 	/* rib work queue */
149 #define ZEBRA_RIB_PROCESS_HOLD_TIME 10
150 #define ZEBRA_RIB_PROCESS_RETRY_TIME 1
151 	struct work_queue *ribq;
152 
153 	/* Meta Queue Information */
154 	struct meta_queue *mq;
155 
156 	/* LSP work queue */
157 	struct work_queue *lsp_process_q;
158 
159 #define ZEBRA_ZAPI_PACKETS_TO_PROCESS 1000
160 	_Atomic uint32_t packets_to_process;
161 
162 	/* Mlag information for the router */
163 	struct zebra_mlag_info mlag_info;
164 
165 	/*
166 	 * The EVPN instance, if any
167 	 */
168 	struct zebra_vrf *evpn_vrf;
169 
170 	uint32_t multipath_num;
171 
172 	/* RPF Lookup behavior */
173 	enum multicast_mode ipv4_multicast_mode;
174 
175 	/*
176 	 * Time for when we sweep the rib from old routes
177 	 */
178 	time_t startup_time;
179 
180 	/*
181 	 * The hash of nexthop groups associated with this router
182 	 */
183 	struct hash *nhgs;
184 	struct hash *nhgs_id;
185 };
186 
187 #define GRACEFUL_RESTART_TIME 60
188 
189 extern struct zebra_router zrouter;
190 
191 extern void zebra_router_init(void);
192 extern void zebra_router_cleanup(void);
193 extern void zebra_router_terminate(void);
194 
195 extern struct zebra_router_table *zebra_router_find_zrt(struct zebra_vrf *zvrf,
196 							uint32_t tableid,
197 							afi_t afi, safi_t safi);
198 extern struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
199 						   uint32_t tableid, afi_t afi,
200 						   safi_t safi);
201 extern struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
202 						  uint32_t tableid, afi_t afi,
203 						  safi_t safi);
204 extern void zebra_router_release_table(struct zebra_vrf *zvrf, uint32_t tableid,
205 				       afi_t afi, safi_t safi);
206 
207 extern int zebra_router_config_write(struct vty *vty);
208 
209 extern void zebra_router_sweep_route(void);
210 extern void zebra_router_sweep_nhgs(void);
211 
212 extern void zebra_router_show_table_summary(struct vty *vty);
213 
214 extern uint32_t zebra_router_get_next_sequence(void);
215 
zebra_vrf_get_evpn_id(void)216 static inline vrf_id_t zebra_vrf_get_evpn_id(void)
217 {
218 	return zrouter.evpn_vrf ? zvrf_id(zrouter.evpn_vrf) : VRF_DEFAULT;
219 }
zebra_vrf_get_evpn(void)220 static inline struct zebra_vrf *zebra_vrf_get_evpn(void)
221 {
222 	return zrouter.evpn_vrf ? zrouter.evpn_vrf
223 			        : zebra_vrf_lookup_by_id(VRF_DEFAULT);
224 }
225 
226 extern void multicast_mode_ipv4_set(enum multicast_mode mode);
227 
228 extern enum multicast_mode multicast_mode_ipv4_get(void);
229 
230 /* zebra_northbound.c */
231 extern const struct frr_yang_module_info frr_zebra_info;
232 
233 #ifdef __cplusplus
234 }
235 #endif
236 
237 #endif
238