]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/protocol.h
Implements pattern match for 'show protocols' command.
[thirdparty/bird.git] / nest / protocol.h
1 /*
2 * BIRD Internet Routing Daemon -- Protocols
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_PROTOCOL_H_
10 #define _BIRD_PROTOCOL_H_
11
12 #include "lib/lists.h"
13 #include "lib/resource.h"
14 #include "lib/timer.h"
15 #include "conf/conf.h"
16
17 struct iface;
18 struct ifa;
19 struct rtable;
20 struct rte;
21 struct neighbor;
22 struct rta;
23 struct network;
24 struct proto_config;
25 struct config;
26 struct proto;
27 struct event;
28 struct ea_list;
29 struct eattr;
30 struct symbol;
31
32 /*
33 * Routing Protocol
34 */
35
36 struct protocol {
37 node n;
38 char *name;
39 char *template; /* Template for automatic generation of names */
40 int name_counter; /* Counter for automatic name generation */
41 int attr_class; /* Attribute class known to this protocol */
42
43 void (*preconfig)(struct protocol *, struct config *); /* Just before configuring */
44 void (*postconfig)(struct proto_config *); /* After configuring each instance */
45 struct proto * (*init)(struct proto_config *); /* Create new instance */
46 int (*reconfigure)(struct proto *, struct proto_config *); /* Try to reconfigure instance, returns success */
47 void (*dump)(struct proto *); /* Debugging dump */
48 void (*dump_attrs)(struct rte *); /* Dump protocol-dependent attributes */
49 int (*start)(struct proto *); /* Start the instance */
50 int (*shutdown)(struct proto *); /* Stop the instance */
51 void (*get_status)(struct proto *, byte *buf); /* Get instance status (for `show protocols' command) */
52 void (*get_route_info)(struct rte *, byte *buf, struct ea_list *attrs); /* Get route information (for `show route' command) */
53 int (*get_attr)(struct eattr *, byte *buf, int buflen); /* ASCIIfy dynamic attribute (returns GA_*) */
54 };
55
56 void protos_build(void);
57 void proto_build(struct protocol *);
58 void protos_preconfig(struct config *);
59 void protos_postconfig(struct config *);
60 void protos_commit(struct config *new, struct config *old, int force_restart, int type);
61 void protos_dump_all(void);
62
63 #define GA_UNKNOWN 0 /* Attribute not recognized */
64 #define GA_NAME 1 /* Result = name */
65 #define GA_FULL 2 /* Result = both name and value */
66
67 /*
68 * Known protocols
69 */
70
71 extern struct protocol
72 proto_device, proto_rip, proto_static,
73 proto_ospf, proto_pipe, proto_bgp;
74
75 /*
76 * Routing Protocol Instance
77 */
78
79 struct proto_config {
80 node n;
81 struct config *global; /* Global configuration data */
82 struct protocol *protocol; /* Protocol */
83 struct proto *proto; /* Instance we've created */
84 char *name;
85 char *dsc;
86 u32 debug, mrtdump; /* Debugging bitfields, both use D_* constants */
87 unsigned preference, disabled; /* Generic parameters */
88 u32 router_id; /* Protocol specific router ID */
89 struct rtable_config *table; /* Table we're attached to */
90 struct filter *in_filter, *out_filter; /* Attached filters */
91
92 /* Protocol-specific data follow... */
93 };
94
95 /* Protocol statistics */
96 struct proto_stats {
97 /* Import - from protocol to core */
98 u32 imp_routes; /* Number of routes successfully imported to the (adjacent) routing table */
99 u32 pref_routes; /* Number of routes that are preferred, sum over all routing table */
100 u32 imp_updates_received; /* Number of route updates received */
101 u32 imp_updates_invalid; /* Number of route updates rejected as invalid */
102 u32 imp_updates_filtered; /* Number of route updates rejected by filters */
103 u32 imp_updates_ignored; /* Number of route updates rejected as already in route table */
104 u32 imp_updates_accepted; /* Number of route updates accepted and imported */
105 u32 imp_withdraws_received; /* Number of route withdraws received */
106 u32 imp_withdraws_invalid; /* Number of route withdraws rejected as invalid */
107 u32 imp_withdraws_ignored; /* Number of route withdraws rejected as already not in route table */
108 u32 imp_withdraws_accepted; /* Number of route withdraws accepted and processed */
109
110 /* Export - from core to protocol */
111 u32 exp_routes; /* Number of routes successfully exported to the protocol */
112 u32 exp_updates_received; /* Number of route updates received */
113 u32 exp_updates_rejected; /* Number of route updates rejected by protocol */
114 u32 exp_updates_filtered; /* Number of route updates rejected by filters */
115 u32 exp_updates_accepted; /* Number of route updates accepted and exported */
116 u32 exp_withdraws_received; /* Number of route withdraws received */
117 u32 exp_withdraws_accepted; /* Number of route withdraws accepted and processed */
118 };
119
120 struct proto {
121 node n; /* Node in *_proto_list */
122 node glob_node; /* Node in global proto_list */
123 struct protocol *proto; /* Protocol */
124 struct proto_config *cf; /* Configuration data */
125 struct proto_config *cf_new; /* Configuration we want to switch to after shutdown (NULL=delete) */
126 pool *pool; /* Pool containing local objects */
127 struct event *attn; /* "Pay attention" event */
128
129 char *name; /* Name of this instance (== cf->name) */
130 u32 debug; /* Debugging flags */
131 u32 mrtdump; /* MRTDump flags */
132 unsigned preference; /* Default route preference */
133 int min_scope; /* Minimal route scope accepted */
134 unsigned accept_ra_types; /* Which types of route announcements are accepted (RA_OPTIMAL or RA_ANY) */
135 unsigned disabled; /* Manually disabled */
136 unsigned proto_state; /* Protocol state machine (see below) */
137 unsigned core_state; /* Core state machine (see below) */
138 unsigned core_goal; /* State we want to reach (see below) */
139 unsigned reconfiguring; /* We're shutting down due to reconfiguration */
140 unsigned refeeding; /* We are refeeding (valid only if core_state == FS_FEEDING) */
141 u32 hash_key; /* Random key used for hashing of neighbors */
142 bird_clock_t last_state_change; /* Time of last state transition */
143 char *last_state_name_announced; /* Last state name we've announced to the user */
144 struct proto_stats stats; /* Current protocol statistics */
145
146 /*
147 * General protocol hooks:
148 *
149 * if_notify Notify protocol about interface state changes.
150 * ifa_notify Notify protocol about interface address changes.
151 * rt_notify Notify protocol about routing table updates.
152 * neigh_notify Notify protocol about neighbor cache events.
153 * make_tmp_attrs Construct ea_list from private attrs stored in rte.
154 * store_tmp_attrs Store private attrs back to the rte.
155 * import_control Called as the first step of the route importing process.
156 * It can construct a new rte, add private attributes and
157 * decide whether the route shall be imported: 1=yes, -1=no,
158 * 0=process it through the import filter set by the user.
159 * reload_routes Request protocol to reload all its routes to the core
160 * (using rte_update()). Returns: 0=reload cannot be done,
161 * 1= reload is scheduled and will happen (asynchronously).
162 */
163
164 void (*if_notify)(struct proto *, unsigned flags, struct iface *i);
165 void (*ifa_notify)(struct proto *, unsigned flags, struct ifa *a);
166 void (*rt_notify)(struct proto *, struct rtable *table, struct network *net, struct rte *new, struct rte *old, struct ea_list *attrs);
167 void (*neigh_notify)(struct neighbor *neigh);
168 struct ea_list *(*make_tmp_attrs)(struct rte *rt, struct linpool *pool);
169 void (*store_tmp_attrs)(struct rte *rt, struct ea_list *attrs);
170 int (*import_control)(struct proto *, struct rte **rt, struct ea_list **attrs, struct linpool *pool);
171 int (*reload_routes)(struct proto *);
172
173 /*
174 * Routing entry hooks (called only for rte's belonging to this protocol):
175 *
176 * rte_better Compare two rte's and decide which one is better (1=first, 0=second).
177 * rte_same Compare two rte's and decide whether they are identical (1=yes, 0=no).
178 * rte_insert Called whenever a rte is inserted to a routing table.
179 * rte_remove Called whenever a rte is removed from the routing table.
180 */
181
182 int (*rte_better)(struct rte *, struct rte *);
183 int (*rte_same)(struct rte *, struct rte *);
184 void (*rte_insert)(struct network *, struct rte *);
185 void (*rte_remove)(struct network *, struct rte *);
186
187 struct rtable *table; /* Our primary routing table */
188 struct filter *in_filter; /* Input filter */
189 struct filter *out_filter; /* Output filter */
190 struct announce_hook *ahooks; /* Announcement hooks for this protocol */
191
192 struct fib_iterator *feed_iterator; /* Routing table iterator used during protocol feeding */
193 struct announce_hook *feed_ahook; /* Announce hook we currently feed */
194
195 /* Hic sunt protocol-specific data */
196 };
197
198 struct proto_spec {
199 void *ptr;
200 int patt;
201 };
202
203
204 void *proto_new(struct proto_config *, unsigned size);
205 void *proto_config_new(struct protocol *, unsigned size);
206 void proto_request_feeding(struct proto *p);
207
208 void proto_cmd_show(struct proto *, unsigned int, int);
209 void proto_cmd_disable(struct proto *, unsigned int, int);
210 void proto_cmd_enable(struct proto *, unsigned int, int);
211 void proto_cmd_restart(struct proto *, unsigned int, int);
212 void proto_cmd_reload(struct proto *, unsigned int, int);
213 void proto_cmd_debug(struct proto *, unsigned int, int);
214 void proto_cmd_mrtdump(struct proto *, unsigned int, int);
215
216 void proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, unsigned int, int), unsigned int arg);
217 struct proto *proto_get_named(struct symbol *, struct protocol *);
218
219 #define CMD_RELOAD 0
220 #define CMD_RELOAD_IN 1
221 #define CMD_RELOAD_OUT 2
222
223 static inline u32
224 proto_get_router_id(struct proto_config *pc)
225 {
226 return pc->router_id ? pc->router_id : pc->global->router_id;
227 }
228
229 extern list active_proto_list;
230
231 /*
232 * Each protocol instance runs two different state machines:
233 *
234 * [P] The protocol machine: (implemented inside protocol)
235 *
236 * DOWN ----> START
237 * ^ |
238 * | V
239 * STOP <---- UP
240 *
241 * States: DOWN Protocol is down and it's waiting for the core
242 * requesting protocol start.
243 * START Protocol is waiting for connection with the rest
244 * of the network and it's not willing to accept
245 * packets. When it connects, it goes to UP state.
246 * UP Protocol is up and running. When the network
247 * connection breaks down or the core requests
248 * protocol to be terminated, it goes to STOP state.
249 * STOP Protocol is disconnecting from the network.
250 * After it disconnects, it returns to DOWN state.
251 *
252 * In: start() Called in DOWN state to request protocol startup.
253 * Returns new state: either UP or START (in this
254 * case, the protocol will notify the core when it
255 * finally comes UP).
256 * stop() Called in START, UP or STOP state to request
257 * protocol shutdown. Returns new state: either
258 * DOWN or STOP (in this case, the protocol will
259 * notify the core when it finally comes DOWN).
260 *
261 * Out: proto_notify_state() -- called by protocol instance when
262 * it does any state transition not covered by
263 * return values of start() and stop(). This includes
264 * START->UP (delayed protocol startup), UP->STOP
265 * (spontaneous shutdown) and STOP->DOWN (delayed
266 * shutdown).
267 */
268
269 #define PS_DOWN 0
270 #define PS_START 1
271 #define PS_UP 2
272 #define PS_STOP 3
273
274 void proto_notify_state(struct proto *p, unsigned state);
275
276 /*
277 * [F] The feeder machine: (implemented in core routines)
278 *
279 * HUNGRY ----> FEEDING
280 * ^ |
281 * | V
282 * FLUSHING <---- HAPPY
283 *
284 * States: HUNGRY Protocol either administratively down (i.e.,
285 * disabled by the user) or temporarily down
286 * (i.e., [P] is not UP)
287 * FEEDING The protocol came up and we're feeding it
288 * initial routes. [P] is UP.
289 * HAPPY The protocol is up and it's receiving normal
290 * routing updates. [P] is UP.
291 * FLUSHING The protocol is down and we're removing its
292 * routes from the table. [P] is STOP or DOWN.
293 *
294 * Normal lifecycle of a protocol looks like:
295 *
296 * HUNGRY/DOWN --> HUNGRY/START --> HUNGRY/UP -->
297 * FEEDING/UP --> HAPPY/UP --> FLUSHING/STOP|DOWN -->
298 * HUNGRY/STOP|DOWN --> HUNGRY/DOWN
299 *
300 * Sometimes, protocol might switch from HAPPY/UP to FEEDING/UP
301 * if it wants to refeed the routes (for example BGP does so
302 * as a result of received ROUTE-REFRESH request).
303 */
304
305 #define FS_HUNGRY 0
306 #define FS_FEEDING 1
307 #define FS_HAPPY 2
308 #define FS_FLUSHING 3
309
310 /*
311 * Debugging flags
312 */
313
314 #define D_STATES 1 /* [core] State transitions */
315 #define D_ROUTES 2 /* [core] Routes passed by the filters */
316 #define D_FILTERS 4 /* [core] Routes rejected by the filters */
317 #define D_IFACES 8 /* [core] Interface events */
318 #define D_EVENTS 16 /* Protocol events */
319 #define D_PACKETS 32 /* Packets sent/received */
320
321 /*
322 * MRTDump flags
323 */
324
325 #define MD_STATES 1 /* Protocol state changes (BGP4MP_MESSAGE_AS4) */
326 #define MD_MESSAGES 2 /* Protocol packets (BGP4MP_MESSAGE_AS4) */
327
328 /*
329 * Known unique protocol instances as referenced by config routines
330 */
331
332 extern struct proto_config *cf_dev_proto;
333
334 /*
335 * Route Announcement Hook
336 */
337
338 struct announce_hook {
339 node n;
340 struct rtable *table;
341 struct proto *proto;
342 struct announce_hook *next; /* Next hook for the same protocol */
343 };
344
345 struct announce_hook *proto_add_announce_hook(struct proto *, struct rtable *);
346
347 /*
348 * Some pipe-specific nest hacks
349 */
350
351 #ifdef CONFIG_PIPE
352 #include "proto/pipe/pipe.h"
353 #endif
354
355
356 #endif