2 * BIRD Internet Routing Daemon -- Protocols
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #ifndef _BIRD_PROTOCOL_H_
10 #define _BIRD_PROTOCOL_H_
12 #include "lib/lists.h"
13 #include "lib/resource.h"
14 #include "lib/event.h"
15 #include "nest/route.h"
16 #include "conf/conf.h"
27 struct channel_config
;
43 char *template; /* Template for automatic generation of names */
44 int name_counter
; /* Counter for automatic name generation */
45 int attr_class
; /* Attribute class known to this protocol */
46 uint preference
; /* Default protocol preference */
47 uint channel_mask
; /* Mask of accepted channel types (NB_*) */
48 uint proto_size
; /* Size of protocol data structure */
49 uint config_size
; /* Size of protocol config data structure */
51 void (*preconfig
)(struct protocol
*, struct config
*); /* Just before configuring */
52 void (*postconfig
)(struct proto_config
*); /* After configuring each instance */
53 struct proto
* (*init
)(struct proto_config
*); /* Create new instance */
54 int (*reconfigure
)(struct proto
*, struct proto_config
*); /* Try to reconfigure instance, returns success */
55 void (*dump
)(struct proto
*); /* Debugging dump */
56 void (*dump_attrs
)(struct rte
*); /* Dump protocol-dependent attributes */
57 int (*start
)(struct proto
*); /* Start the instance */
58 int (*shutdown
)(struct proto
*); /* Stop the instance */
59 void (*cleanup
)(struct proto
*); /* Called after shutdown when protocol became hungry/down */
60 void (*get_status
)(struct proto
*, byte
*buf
); /* Get instance status (for `show protocols' command) */
61 void (*get_route_info
)(struct rte
*, byte
*buf
, struct ea_list
*attrs
); /* Get route information (for `show route' command) */
62 int (*get_attr
)(struct eattr
*, byte
*buf
, int buflen
); /* ASCIIfy dynamic attribute (returns GA_*) */
63 void (*show_proto_info
)(struct proto
*); /* Show protocol info (for `show protocols all' command) */
64 void (*copy_config
)(struct proto_config
*, struct proto_config
*); /* Copy config from given protocol instance */
67 void protos_build(void);
68 void proto_build(struct protocol
*);
69 void protos_preconfig(struct config
*);
70 void protos_commit(struct config
*new, struct config
*old
, int force_restart
, int type
);
71 void protos_dump_all(void);
73 #define GA_UNKNOWN 0 /* Attribute not recognized */
74 #define GA_NAME 1 /* Result = name */
75 #define GA_FULL 2 /* Result = both name and value */
81 extern struct protocol
82 proto_device
, proto_radv
, proto_rip
, proto_static
,
83 proto_ospf
, proto_pipe
, proto_bgp
, proto_bfd
, proto_babel
, proto_rpki
;
86 * Routing Protocol Instance
91 struct config
*global
; /* Global configuration data */
92 struct protocol
*protocol
; /* Protocol */
93 struct proto
*proto
; /* Instance we've created */
96 int class; /* SYM_PROTO or SYM_TEMPLATE */
97 u8 net_type
; /* Protocol network type (NET_*), 0 for undefined */
98 u8 disabled
; /* Protocol enabled/disabled by default */
99 u32 debug
, mrtdump
; /* Debugging bitfields, both use D_* constants */
100 u32 router_id
; /* Protocol specific router ID */
102 list channels
; /* List of channel configs (struct channel_config) */
103 struct iface
*vrf
; /* Related VRF instance, NULL if global */
105 /* Check proto_reconfigure() and proto_copy_config() after changing struct proto_config */
107 /* Protocol-specific data follow... */
110 /* Protocol statistics */
112 /* Import - from protocol to core */
113 u32 imp_routes
; /* Number of routes successfully imported to the (adjacent) routing table */
114 u32 filt_routes
; /* Number of routes rejected in import filter but kept in the routing table */
115 u32 imp_updates_received
; /* Number of route updates received */
116 u32 imp_updates_invalid
; /* Number of route updates rejected as invalid */
117 u32 imp_updates_filtered
; /* Number of route updates rejected by filters */
118 u32 imp_updates_ignored
; /* Number of route updates rejected as already in route table */
119 u32 imp_updates_accepted
; /* Number of route updates accepted and imported */
120 u32 imp_withdraws_received
; /* Number of route withdraws received */
121 u32 imp_withdraws_invalid
; /* Number of route withdraws rejected as invalid */
122 u32 imp_withdraws_ignored
; /* Number of route withdraws rejected as already not in route table */
123 u32 imp_withdraws_accepted
; /* Number of route withdraws accepted and processed */
125 /* Export - from core to protocol */
126 u32 exp_routes
; /* Number of routes successfully exported to the protocol */
127 u32 exp_updates_received
; /* Number of route updates received */
128 u32 exp_updates_rejected
; /* Number of route updates rejected by protocol */
129 u32 exp_updates_filtered
; /* Number of route updates rejected by filters */
130 u32 exp_updates_accepted
; /* Number of route updates accepted and exported */
131 u32 exp_withdraws_received
; /* Number of route withdraws received */
132 u32 exp_withdraws_accepted
; /* Number of route withdraws accepted and processed */
136 node n
; /* Node in global proto_list */
137 struct protocol
*proto
; /* Protocol */
138 struct proto_config
*cf
; /* Configuration data */
139 struct proto_config
*cf_new
; /* Configuration we want to switch to after shutdown (NULL=delete) */
140 pool
*pool
; /* Pool containing local objects */
141 event
*event
; /* Protocol event */
143 list channels
; /* List of channels to rtables (struct channel) */
144 struct channel
*main_channel
; /* Primary channel */
145 struct rte_src
*main_source
; /* Primary route source */
146 struct iface
*vrf
; /* Related VRF instance, NULL if global */
148 char *name
; /* Name of this instance (== cf->name) */
149 u32 debug
; /* Debugging flags */
150 u32 mrtdump
; /* MRTDump flags */
151 uint active_channels
; /* Number of active channels */
152 byte net_type
; /* Protocol network type (NET_*), 0 for undefined */
153 byte disabled
; /* Manually disabled */
154 byte proto_state
; /* Protocol state machine (PS_*, see below) */
155 byte active
; /* From PS_START to cleanup after PS_STOP */
156 byte do_start
; /* Start actions are scheduled */
157 byte do_stop
; /* Stop actions are scheduled */
158 byte reconfiguring
; /* We're shutting down due to reconfiguration */
159 byte gr_recovery
; /* Protocol should participate in graceful restart recovery */
160 byte down_sched
; /* Shutdown is scheduled for later (PDS_*) */
161 byte down_code
; /* Reason for shutdown (PDC_* codes) */
162 u32 hash_key
; /* Random key used for hashing of neighbors */
163 btime last_state_change
; /* Time of last state transition */
164 char *last_state_name_announced
; /* Last state name we've announced to the user */
165 char *message
; /* State-change message, allocated from proto_pool */
168 * General protocol hooks:
170 * if_notify Notify protocol about interface state changes.
171 * ifa_notify Notify protocol about interface address changes.
172 * rt_notify Notify protocol about routing table updates.
173 * neigh_notify Notify protocol about neighbor cache events.
174 * make_tmp_attrs Construct ea_list from private attrs stored in rte.
175 * store_tmp_attrs Store private attrs back to the rte.
176 * import_control Called as the first step of the route importing process.
177 * It can construct a new rte, add private attributes and
178 * decide whether the route shall be imported: 1=yes, -1=no,
179 * 0=process it through the import filter set by the user.
180 * reload_routes Request channel to reload all its routes to the core
181 * (using rte_update()). Returns: 0=reload cannot be done,
182 * 1= reload is scheduled and will happen (asynchronously).
183 * feed_begin Notify channel about beginning of route feeding.
184 * feed_end Notify channel about finish of route feeding.
187 void (*if_notify
)(struct proto
*, unsigned flags
, struct iface
*i
);
188 void (*ifa_notify
)(struct proto
*, unsigned flags
, struct ifa
*a
);
189 void (*rt_notify
)(struct proto
*, struct channel
*, struct network
*net
, struct rte
*new, struct rte
*old
, struct ea_list
*attrs
);
190 void (*neigh_notify
)(struct neighbor
*neigh
);
191 struct ea_list
*(*make_tmp_attrs
)(struct rte
*rt
, struct linpool
*pool
);
192 void (*store_tmp_attrs
)(struct rte
*rt
, struct ea_list
*attrs
);
193 int (*import_control
)(struct proto
*, struct rte
**rt
, struct ea_list
**attrs
, struct linpool
*pool
);
194 void (*reload_routes
)(struct channel
*);
195 void (*feed_begin
)(struct channel
*, int initial
);
196 void (*feed_end
)(struct channel
*);
199 * Routing entry hooks (called only for routes belonging to this protocol):
201 * rte_recalculate Called at the beginning of the best route selection
202 * rte_better Compare two rte's and decide which one is better (1=first, 0=second).
203 * rte_same Compare two rte's and decide whether they are identical (1=yes, 0=no).
204 * rte_mergable Compare two rte's and decide whether they could be merged (1=yes, 0=no).
205 * rte_insert Called whenever a rte is inserted to a routing table.
206 * rte_remove Called whenever a rte is removed from the routing table.
209 int (*rte_recalculate
)(struct rtable
*, struct network
*, struct rte
*, struct rte
*, struct rte
*);
210 int (*rte_better
)(struct rte
*, struct rte
*);
211 int (*rte_same
)(struct rte
*, struct rte
*);
212 int (*rte_mergable
)(struct rte
*, struct rte
*);
213 void (*rte_insert
)(struct network
*, struct rte
*);
214 void (*rte_remove
)(struct network
*, struct rte
*);
216 /* Hic sunt protocol-specific data */
225 #define PDS_DISABLE 1 /* Proto disable scheduled */
226 #define PDS_RESTART 2 /* Proto restart scheduled */
228 #define PDC_CF_REMOVE 0x01 /* Removed in new config */
229 #define PDC_CF_DISABLE 0x02 /* Disabled in new config */
230 #define PDC_CF_RESTART 0x03 /* Restart due to reconfiguration */
231 #define PDC_CMD_DISABLE 0x11 /* Result of disable command */
232 #define PDC_CMD_RESTART 0x12 /* Result of restart command */
233 #define PDC_CMD_SHUTDOWN 0x13 /* Result of global shutdown */
234 #define PDC_RX_LIMIT_HIT 0x21 /* Route receive limit reached */
235 #define PDC_IN_LIMIT_HIT 0x22 /* Route import limit reached */
236 #define PDC_OUT_LIMIT_HIT 0x23 /* Route export limit reached */
239 void *proto_new(struct proto_config
*);
240 void *proto_config_new(struct protocol
*, int class);
241 void proto_copy_config(struct proto_config
*dest
, struct proto_config
*src
);
242 void proto_set_message(struct proto
*p
, char *msg
, int len
);
244 void graceful_restart_recovery(void);
245 void graceful_restart_init(void);
246 void graceful_restart_show_status(void);
247 void channel_graceful_restart_lock(struct channel
*c
);
248 void channel_graceful_restart_unlock(struct channel
*c
);
250 #define DEFAULT_GR_WAIT 240
252 void channel_show_limit(struct channel_limit
*l
, const char *dsc
);
253 void channel_show_info(struct channel
*c
);
255 void proto_cmd_show(struct proto
*, uintptr_t, int);
256 void proto_cmd_disable(struct proto
*, uintptr_t, int);
257 void proto_cmd_enable(struct proto
*, uintptr_t, int);
258 void proto_cmd_restart(struct proto
*, uintptr_t, int);
259 void proto_cmd_reload(struct proto
*, uintptr_t, int);
260 void proto_cmd_debug(struct proto
*, uintptr_t, int);
261 void proto_cmd_mrtdump(struct proto
*, uintptr_t, int);
263 void proto_apply_cmd(struct proto_spec ps
, void (* cmd
)(struct proto
*, uintptr_t, int), int restricted
, uintptr_t arg
);
264 struct proto
*proto_get_named(struct symbol
*, struct protocol
*);
267 #define CMD_RELOAD_IN 1
268 #define CMD_RELOAD_OUT 2
271 proto_get_router_id(struct proto_config
*pc
)
273 return pc
->router_id
? pc
->router_id
: pc
->global
->router_id
;
276 static inline struct ea_list
*
277 rte_make_tmp_attrs(struct rte
*rt
, struct linpool
*pool
)
279 struct ea_list
*(*mta
)(struct rte
*rt
, struct linpool
*pool
);
280 mta
= rt
->attrs
->src
->proto
->make_tmp_attrs
;
281 return mta
? mta(rt
, pool
) : NULL
;
284 /* Moved from route.h to avoid dependency conflicts */
285 static inline void rte_update(struct proto
*p
, const net_addr
*n
, rte
*new) { rte_update2(p
->main_channel
, n
, new, p
->main_source
); }
287 extern pool
*proto_pool
;
288 extern list proto_list
;
291 * Each protocol instance runs two different state machines:
293 * [P] The protocol machine: (implemented inside protocol)
300 * States: DOWN Protocol is down and it's waiting for the core
301 * requesting protocol start.
302 * START Protocol is waiting for connection with the rest
303 * of the network and it's not willing to accept
304 * packets. When it connects, it goes to UP state.
305 * UP Protocol is up and running. When the network
306 * connection breaks down or the core requests
307 * protocol to be terminated, it goes to STOP state.
308 * STOP Protocol is disconnecting from the network.
309 * After it disconnects, it returns to DOWN state.
311 * In: start() Called in DOWN state to request protocol startup.
312 * Returns new state: either UP or START (in this
313 * case, the protocol will notify the core when it
315 * stop() Called in START, UP or STOP state to request
316 * protocol shutdown. Returns new state: either
317 * DOWN or STOP (in this case, the protocol will
318 * notify the core when it finally comes DOWN).
320 * Out: proto_notify_state() -- called by protocol instance when
321 * it does any state transition not covered by
322 * return values of start() and stop(). This includes
323 * START->UP (delayed protocol startup), UP->STOP
324 * (spontaneous shutdown) and STOP->DOWN (delayed
333 void proto_notify_state(struct proto
*p
, unsigned state
);
336 * [F] The feeder machine: (implemented in core routines)
338 * HUNGRY ----> FEEDING
341 * FLUSHING <---- HAPPY
343 * States: HUNGRY Protocol either administratively down (i.e.,
344 * disabled by the user) or temporarily down
345 * (i.e., [P] is not UP)
346 * FEEDING The protocol came up and we're feeding it
347 * initial routes. [P] is UP.
348 * HAPPY The protocol is up and it's receiving normal
349 * routing updates. [P] is UP.
350 * FLUSHING The protocol is down and we're removing its
351 * routes from the table. [P] is STOP or DOWN.
353 * Normal lifecycle of a protocol looks like:
355 * HUNGRY/DOWN --> HUNGRY/START --> HUNGRY/UP -->
356 * FEEDING/UP --> HAPPY/UP --> FLUSHING/STOP|DOWN -->
357 * HUNGRY/STOP|DOWN --> HUNGRY/DOWN
359 * Sometimes, protocol might switch from HAPPY/UP to FEEDING/UP
360 * if it wants to refeed the routes (for example BGP does so
361 * as a result of received ROUTE-REFRESH request).
370 #define D_STATES 1 /* [core] State transitions */
371 #define D_ROUTES 2 /* [core] Routes passed by the filters */
372 #define D_FILTERS 4 /* [core] Routes rejected by the filters */
373 #define D_IFACES 8 /* [core] Interface events */
374 #define D_EVENTS 16 /* Protocol events */
375 #define D_PACKETS 32 /* Packets sent/received */
378 #define TRACE(flags, msg, args...) \
379 do { if (p->p.debug & flags) log(L_TRACE "%s: " msg, p->p.name , ## args ); } while(0)
387 #define MD_STATES 1 /* Protocol state changes (BGP4MP_MESSAGE_AS4) */
388 #define MD_MESSAGES 2 /* Protocol packets (BGP4MP_MESSAGE_AS4) */
391 * Known unique protocol instances as referenced by config routines
394 extern struct proto_config
*cf_dev_proto
;
401 #define PLD_RX 0 /* Receive limit */
402 #define PLD_IN 1 /* Import limit */
403 #define PLD_OUT 2 /* Export limit */
406 #define PLA_NONE 0 /* No limit */
407 #define PLA_WARN 1 /* Issue log warning */
408 #define PLA_BLOCK 2 /* Block new routes */
409 #define PLA_RESTART 4 /* Force protocol restart */
410 #define PLA_DISABLE 5 /* Shutdown and disable protocol */
412 #define PLS_INITIAL 0 /* Initial limit state after protocol start */
413 #define PLS_ACTIVE 1 /* Limit was hit */
414 #define PLS_BLOCKED 2 /* Limit is active and blocking new routes */
416 struct channel_limit
{
417 u32 limit
; /* Maximum number of prefixes */
418 u8 action
; /* Action to take (PLA_*) */
419 u8 state
; /* State of limit (PLS_*) */
422 void channel_notify_limit(struct channel
*c
, struct channel_limit
*l
, int dir
, u32 rt_count
);
429 struct channel_class
{
430 uint channel_size
; /* Size of channel data structure */
431 uint config_size
; /* Size of channel config data structure */
433 void (*init
)(struct channel
*, struct channel_config
*); /* Create new instance */
434 int (*reconfigure
)(struct channel
*, struct channel_config
*); /* Try to reconfigure instance, returns success */
435 int (*start
)(struct channel
*); /* Start the instance */
436 void (*shutdown
)(struct channel
*); /* Stop the instance */
437 void (*cleanup
)(struct channel
*); /* Channel finished flush */
439 void (*copy_config
)(struct channel_config
*, struct channel_config
*); /* Copy config from given channel instance */
442 void (*preconfig
)(struct protocol
*, struct config
*); /* Just before configuring */
443 void (*postconfig
)(struct proto_config
*); /* After configuring each instance */
446 void (*dump
)(struct proto
*); /* Debugging dump */
447 void (*dump_attrs
)(struct rte
*); /* Dump protocol-dependent attributes */
449 void (*get_status
)(struct proto
*, byte
*buf
); /* Get instance status (for `show protocols' command) */
450 void (*get_route_info
)(struct rte
*, byte
*buf
, struct ea_list
*attrs
); /* Get route information (for `show route' command) */
451 int (*get_attr
)(struct eattr
*, byte
*buf
, int buflen
); /* ASCIIfy dynamic attribute (returns GA_*) */
452 void (*show_proto_info
)(struct proto
*); /* Show protocol info (for `show protocols all' command) */
457 extern struct channel_class channel_bgp
;
459 struct channel_config
{
462 const struct channel_class
*channel
;
464 struct rtable_config
*table
; /* Table we're attached to */
465 struct filter
*in_filter
, *out_filter
; /* Attached filters */
466 struct channel_limit rx_limit
; /* Limit for receiving routes from protocol
467 (relevant when in_keep_filtered is active) */
468 struct channel_limit in_limit
; /* Limit for importing routes from protocol */
469 struct channel_limit out_limit
; /* Limit for exporting routes to protocol */
471 u8 net_type
; /* Routing table network type (NET_*), 0 for undefined */
472 u8 ra_mode
; /* Mode of received route advertisements (RA_*) */
473 u16 preference
; /* Default route preference */
474 u8 merge_limit
; /* Maximal number of nexthops for RA_MERGED */
475 u8 in_keep_filtered
; /* Routes rejected in import filter are kept */
479 node n
; /* Node in proto->channels */
480 node table_node
; /* Node in table->channels */
482 const char *name
; /* Channel name (may be NULL) */
483 const struct channel_class
*channel
;
486 struct rtable
*table
;
487 struct filter
*in_filter
; /* Input filter */
488 struct filter
*out_filter
; /* Output filter */
489 struct channel_limit rx_limit
; /* Receive limit (for in_keep_filtered) */
490 struct channel_limit in_limit
; /* Input limit */
491 struct channel_limit out_limit
; /* Output limit */
493 struct event
*feed_event
; /* Event responsible for feeding */
494 struct fib_iterator feed_fit
; /* Routing table iterator used during feeding */
495 struct proto_stats stats
; /* Per-channel protocol statistics */
497 u8 net_type
; /* Routing table network type (NET_*), 0 for undefined */
498 u8 ra_mode
; /* Mode of received route advertisements (RA_*) */
499 u16 preference
; /* Default route preference */
500 u8 merge_limit
; /* Maximal number of nexthops for RA_MERGED */
501 u8 in_keep_filtered
; /* Routes rejected in import filter are kept */
503 u8 stale
; /* Used in reconfiguration */
506 u8 export_state
; /* Route export state (ES_*, see below) */
509 u8 refeeding
; /* We are refeeding (valid only if export_state == ES_FEEDING) */
510 u8 reloadable
; /* Hook reload_routes() is allowed on the channel */
511 u8 gr_lock
; /* Graceful restart mechanism should wait for this channel */
512 u8 gr_wait
; /* Route export to channel is postponed until graceful restart */
514 btime last_state_change
; /* Time of last state transition */
521 * CS_DOWN - The initial and the final state of a channel. There is no route
522 * exchange between the protocol and the table. Channel is not counted as
523 * active. Channel keeps a ptr to the table, but do not lock the table and is
524 * not linked in the table. Generally, new closed channels are created in
525 * protocols' init() hooks. The protocol is expected to explicitly activate its
526 * channels (by calling channel_init() or channel_open()).
528 * CS_START - The channel as a connection between the protocol and the table is
529 * initialized (counted as active by the protocol, linked in the table and keeps
530 * the table locked), but there is no current route exchange. There still may be
531 * routes associated with the channel in the routing table if the channel falls
532 * to CS_START from CS_UP. Generally, channels are initialized in protocols'
533 * start() hooks when going to PS_START.
535 * CS_UP - The channel is initialized and the route exchange is allowed. Note
536 * that even in CS_UP state, route export may still be down (ES_DOWN) by the
537 * core decision (e.g. waiting for table convergence after graceful restart).
538 * I.e., the protocol decides to open the channel but the core decides to start
539 * route export. Route import (caused by rte_update() from the protocol) is not
540 * restricted by that and is on volition of the protocol. Generally, channels
541 * are opened in protocols' start() hooks when going to PS_UP.
543 * CS_FLUSHING - The transitional state between initialized channel and closed
544 * channel. The channel is still initialized, but no route exchange is allowed.
545 * Instead, the associated table is running flush loop to remove routes imported
546 * through the channel. After that, the channel changes state to CS_DOWN and
547 * is detached from the table (the table is unlocked and the channel is unlinked
548 * from it). Unlike other states, the CS_FLUSHING state is not explicitly
549 * entered or left by the protocol. A protocol may request to close a channel
550 * (by calling channel_close()), which causes the channel to change state to
551 * CS_FLUSHING and later to CS_DOWN. Also note that channels are closed
552 * automatically by the core when the protocol is going down.
554 * Allowed transitions:
556 * CS_DOWN -> CS_START / CS_UP
557 * CS_START -> CS_UP / CS_FLUSHING
558 * CS_UP -> CS_START / CS_FLUSHING
559 * CS_FLUSHING -> CS_DOWN (automatic)
565 #define CS_FLUSHING 3
572 struct channel_config
*proto_cf_find_channel(struct proto_config
*p
, uint net_type
);
573 static inline struct channel_config
*proto_cf_main_channel(struct proto_config
*pc
)
574 { struct channel_config
*cc
= HEAD(pc
->channels
); return NODE_VALID(cc
) ? cc
: NULL
; }
576 struct channel
*proto_find_channel_by_table(struct proto
*p
, struct rtable
*t
);
577 struct channel
*proto_find_channel_by_name(struct proto
*p
, const char *n
);
578 struct channel
*proto_add_channel(struct proto
*p
, struct channel_config
*cf
);
579 int proto_configure_channel(struct proto
*p
, struct channel
**c
, struct channel_config
*cf
);
581 void channel_set_state(struct channel
*c
, uint state
);
583 static inline void channel_init(struct channel
*c
) { channel_set_state(c
, CS_START
); }
584 static inline void channel_open(struct channel
*c
) { channel_set_state(c
, CS_UP
); }
585 static inline void channel_close(struct channel
*c
) { channel_set_state(c
, CS_FLUSHING
); }
587 void channel_request_feeding(struct channel
*c
);
588 void *channel_config_new(const struct channel_class
*cc
, uint net_type
, struct proto_config
*proto
);
589 int channel_reconfigure(struct channel
*c
, struct channel_config
*cf
);