Statistics
| Branch: | Revision:

iof-bird-daemon / nest / route.h @ ce1da96e

History | View | Annotate | Download (12.6 KB)

1
/*
2
 *        BIRD Internet Routing Daemon -- Routing Table
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_ROUTE_H_
10
#define _BIRD_ROUTE_H_
11

    
12
#include "lib/lists.h"
13
#include "lib/resource.h"
14
#include "lib/timer.h"
15

    
16
struct protocol;
17
struct proto;
18
struct symbol;
19
struct filter;
20
struct cli;
21

    
22
/*
23
 *        Generic data structure for storing network prefixes. Also used
24
 *        for the master routing table. Currently implemented as a hash
25
 *        table.
26
 *
27
 *        Available operations:
28
 *                - insertion of new entry
29
 *                - deletion of entry
30
 *                - searching for entry by network prefix
31
 *                - asynchronous retrieval of fib contents
32
 */
33

    
34
struct fib_node {
35
  struct fib_node *next;                /* Next in hash chain */
36
  struct fib_iterator *readers;                /* List of readers of this node */
37
  byte pxlen;
38
  byte flags;                                /* User-defined */
39
  byte x0, x1;                                /* User-defined */
40
  ip_addr prefix;                        /* In host order */
41
};
42

    
43
struct fib_iterator {                        /* See lib/slists.h for an explanation */
44
  struct fib_iterator *prev, *next;        /* Must be synced with struct fib_node! */
45
  struct fib_node *node;                /* Or NULL if freshly merged */
46
  byte efef;                                /* 0xff to distinguish between iterator and node */
47
  byte pad[3];
48
  unsigned int hash;
49
};
50

    
51
struct fib {
52
  pool *fib_pool;                        /* Pool holding all our data */
53
  slab *fib_slab;                        /* Slab holding all fib nodes */
54
  struct fib_node **hash_table;                /* Node hash table */
55
  unsigned int hash_size;                /* Number of hash table entries (a power of two) */
56
  unsigned int hash_order;                /* Binary logarithm of hash_size */
57
  unsigned int hash_shift;                /* 16 - hash_log */
58
  unsigned int entries;                        /* Number of entries */
59
  unsigned int entries_min, entries_max;/* Entry count limits (else start rehashing) */
60
  void (*init)(struct fib_node *);        /* Constructor */
61
};
62

    
63
void fib_init(struct fib *, pool *, unsigned node_size, unsigned hash_order, void (*init)(struct fib_node *));
64
void *fib_find(struct fib *, ip_addr *, int);        /* Find or return NULL if doesn't exist */
65
void *fib_get(struct fib *, ip_addr *, int);         /* Find or create new if nonexistent */
66
void fib_delete(struct fib *, void *);        /* Remove fib entry */
67
void fib_free(struct fib *);                /* Destroy the fib */
68
void fib_check(struct fib *);                /* Consistency check for debugging */
69

    
70
void fit_init(struct fib_iterator *, struct fib *); /* Internal functions, don't call */
71
struct fib_node *fit_get(struct fib *, struct fib_iterator *);
72
void fit_put(struct fib_iterator *, struct fib_node *);
73

    
74
#define FIB_WALK(fib, z) do {                                        \
75
        struct fib_node *z, **ff = (fib)->hash_table;                \
76
        unsigned int count = (fib)->hash_size;                        \
77
        while (count--)                                                \
78
          for(z = *ff++; z; z=z->next)
79

    
80
#define FIB_WALK_END } while (0)
81

    
82
#define FIB_ITERATE_INIT(it, fib) fit_init(it, fib)
83

    
84
#define FIB_ITERATE_START(fib, it, z) do {                        \
85
        struct fib_node *z = fit_get(fib, it);                        \
86
        unsigned int count = (fib)->hash_size;                        \
87
        unsigned int hpos = (it)->hash;                                \
88
        for(;;) {                                                \
89
        fis_again:        if (!z) {                                \
90
                        if (++hpos >= count)                        \
91
                                break;                                \
92
                        z = (fib)->hash_table[hpos];                \
93
                        goto fis_again;                                \
94
                }
95

    
96
#define FIB_ITERATE_END(z) z = z->next; } } while(0)
97

    
98
#define FIB_ITERATE_PUT(it, z) fit_put(it, z)
99

    
100
/*
101
 *        Master Routing Tables. Generally speaking, each of them contains a FIB
102
 *        with each entry pointing to a list of route entries representing routes
103
 *        to given network (with the selected one at the head).
104
 *
105
 *        Each of the RTE's contains variable data (the preference and protocol-dependent
106
 *        metrics) and a pointer to a route attribute block common for many routes).
107
 *
108
 *        It's guaranteed that there is at most one RTE for every (prefix,proto) pair.
109
 */
110

    
111
struct rtable_config {
112
  node n;
113
  char *name;
114
  struct rtable *table;
115
  struct proto_config *krt_attached;        /* Kernel syncer attached to this table */
116
  int gc_max_ops;                        /* Maximum number of operations before GC is run */
117
  int gc_min_time;                        /* Minimum time between two consecutive GC runs */
118
};
119

    
120
typedef struct rtable {
121
  node n;                                /* Node in list of all tables */
122
  struct fib fib;
123
  char *name;                                /* Name of this table */
124
  list hooks;                                /* List of announcement hooks */
125
  int pipe_busy;                        /* Pipe loop detection */
126
  int use_count;                        /* Number of protocols using this table */
127
  struct rtable_config *config;                /* Configuration of this table */
128
  struct config *deleted;                /* Table doesn't exist in current configuration,
129
                                         * delete as soon as use_count becomes 0 and remove
130
                                         * obstacle from this routing table.
131
                                         */
132
  struct event *gc_event;                /* Garbage collector event */
133
  int gc_counter;                        /* Number of operations since last GC */
134
  bird_clock_t gc_time;                        /* Time of last GC */
135
} rtable;
136

    
137
typedef struct network {
138
  struct fib_node n;                        /* FIB flags reserved for kernel syncer */
139
  struct rte *routes;                        /* Available routes for this network */
140
} net;
141

    
142
typedef struct rte {
143
  struct rte *next;
144
  net *net;                                /* Network this RTE belongs to */
145
  struct rta *attrs;                        /* Attributes of this route */
146
  byte flags;                                /* Flags (REF_...) */
147
  byte pflags;                                /* Protocol-specific flags */
148
  word pref;                                /* Route preference */
149
  bird_clock_t lastmod;                        /* Last modified */
150
  union {                                /* Protocol-dependent data (metrics etc.) */
151
#ifdef CONFIG_RIP
152
    struct {
153
      node garbage;                        /* List for garbage collection */
154
      byte metric;                        /* RIP metric */
155
      u16 tag;                                /* External route tag */
156
      bird_clock_t lastmodX;                /* Strange kind of last modification time */
157
    } rip;
158
#endif
159
#ifdef CONFIG_OSPF
160
    struct {
161
      u32 metric1, metric2;                /* OSPF Type 1 and Type 2 metrics */
162
      u32 tag;                                /* External route tag */
163
    } ospf;
164
#endif
165
    struct {                                /* Routes generated by krt sync (both temporary and inherited ones) */
166
      s8 src;                                /* Alleged route source (see krt.h) */
167
      u8 proto;                                /* Kernel source protocol ID */
168
      u8 type;                                /* Kernel route type */
169
      u8 seen;                                /* Seen during last scan */
170
      u32 metric;                        /* Kernel metric */
171
    } krt;
172
  } u;
173
} rte;
174

    
175
#define REF_COW 1                        /* Copy this rte on write */
176

    
177
struct config;
178

    
179
void rt_init(void);
180
void rt_preconfig(struct config *);
181
void rt_commit(struct config *new, struct config *old);
182
void rt_lock_table(rtable *);
183
void rt_unlock_table(rtable *);
184
void rt_setup(pool *, rtable *, char *, struct rtable_config *);
185
static inline net *net_find(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_find(&tab->fib, &addr, len); }
186
static inline net *net_get(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_get(&tab->fib, &addr, len); }
187
rte *rte_find(net *net, struct proto *p);
188
rte *rte_get_temp(struct rta *);
189
void rte_update(rtable *tab, net *net, struct proto *p, rte *new);
190
void rte_discard(rtable *tab, rte *old);
191
void rte_dump(rte *);
192
void rte_free(rte *);
193
rte *rte_do_cow(rte *);
194
static inline rte * rte_cow(rte *r) { return (r->flags & REF_COW) ? rte_do_cow(r) : r; }
195
void rt_dump(rtable *);
196
void rt_dump_all(void);
197
void rt_feed_baby(struct proto *p);
198
void rt_prune(rtable *tab);
199
void rt_prune_all(void);
200
struct rtable_config *rt_new_table(struct symbol *s);
201

    
202
struct rt_show_data {
203
  ip_addr prefix;
204
  unsigned pxlen;
205
  rtable *table;
206
  struct filter *filter;
207
  int verbose;
208
  struct fib_iterator fit;
209
  struct proto *import_protocol;
210
  int import_mode, primary_only;
211
  struct config *running_on_config;
212
};
213
void rt_show(struct rt_show_data *);
214

    
215
/*
216
 *        Route Attributes
217
 *
218
 *        Beware: All standard BGP attributes must be represented here instead
219
 *        of making them local to the route. This is needed to ensure proper
220
 *        construction of BGP route attribute lists.
221
 */
222

    
223
typedef struct rta {
224
  struct rta *next, **pprev;                /* Hash chain */
225
  struct proto *proto;                        /* Protocol instance */
226
  unsigned uc;                                /* Use count */
227
  byte source;                                /* Route source (RTS_...) */
228
  byte scope;                                /* Route scope (SCOPE_... -- see ip.h) */
229
  byte cast;                                /* Casting type (RTC_...) */
230
  byte dest;                                /* Route destination type (RTD_...) */
231
  byte flags;                                /* Route flags (RTF_...), now unused */
232
  byte aflags;                                /* Attribute cache flags (RTAF_...) */
233
  u16 hash_key;                                /* Hash over important fields */
234
  ip_addr gw;                                /* Next hop */
235
  ip_addr from;                                /* Advertising router */
236
  struct iface *iface;                        /* Outgoing interface */
237
  struct ea_list *eattrs;                /* Extended Attribute chain */
238
} rta;
239

    
240
#define RTS_DUMMY 0                        /* Dummy route to be removed soon */
241
#define RTS_STATIC 1                        /* Normal static route */
242
#define RTS_INHERIT 2                        /* Route inherited from kernel */
243
#define RTS_DEVICE 3                        /* Device route */
244
#define RTS_STATIC_DEVICE 4                /* Static device route */
245
#define RTS_REDIRECT 5                        /* Learned via redirect */
246
#define RTS_RIP 6                        /* RIP route */
247
#define RTS_OSPF 7                        /* OSPF route */
248
#define RTS_OSPF_EXT 8                        /* OSPF external route */
249
#define RTS_OSPF_IA 9                        /* OSPF inter-area route */
250
#define RTS_OSPF_BOUNDARY 10                /* OSPF route to boundary router */
251
#define RTS_BGP 11                        /* BGP route */
252
#define RTS_PIPE 12                        /* Inter-table wormhole */
253

    
254
#define RTC_UNICAST 0
255
#define RTC_BROADCAST 1
256
#define RTC_MULTICAST 2
257
#define RTC_ANYCAST 3                        /* IPv6 Anycast */
258

    
259
#define RTD_ROUTER 0                        /* Next hop is neighbor router */
260
#define RTD_DEVICE 1                        /* Points to device */
261
#define RTD_BLACKHOLE 2                        /* Silently drop packets */
262
#define RTD_UNREACHABLE 3                /* Reject as unreachable */
263
#define RTD_PROHIBIT 4                        /* Administratively prohibited */
264

    
265
#define RTAF_CACHED 1                        /* This is a cached rta */
266

    
267
/*
268
 *        Extended Route Attributes
269
 */
270

    
271
typedef struct eattr {
272
  word id;                                /* EA_CODE(EAP_..., protocol-dependent ID) */
273
  byte flags;                                /* Protocol-dependent flags */
274
  byte type;                                /* Attribute type and several flags (EAF_...) */
275
  union {
276
    u32 data;
277
    struct adata *ptr;                        /* Attribute data elsewhere */
278
  } u;
279
} eattr;
280

    
281
#define EAP_GENERIC 0                        /* Generic attributes */
282
#define EAP_BGP 1                        /* BGP attributes */
283
#define EAP_RIP 2                        /* RIP */
284
#define EAP_MAX 3
285

    
286
#define EA_CODE(proto,id) (((proto) << 8) | (id))
287
#define EA_PROTO(ea) ((ea) >> 8)
288
#define EA_ID(ea) ((ea) & 0xff)
289

    
290
#define EA_CODE_MASK 0xffff
291
#define EA_ALLOW_UNDEF 0x10000                /* ea_find: allow EAF_TYPE_UNDEF */
292

    
293
#define EAF_TYPE_MASK 0x0f                /* Mask with this to get type */
294
#define EAF_TYPE_INT 0x01                /* 32-bit signed integer number */
295
#define EAF_TYPE_OPAQUE 0x02                /* Opaque byte string (not filterable) */
296
#define EAF_TYPE_IP_ADDRESS 0x04        /* IP address */
297
#define EAF_TYPE_ROUTER_ID 0x05                /* Router ID (IPv4 address) */
298
#define EAF_TYPE_AS_PATH 0x06                /* BGP AS path (encoding per RFC 1771:4.3) */
299
#define EAF_TYPE_INT_SET 0x0a                /* Set of u32's (e.g., a community list) */
300
#define EAF_TYPE_UNDEF 0x0f                /* `force undefined' entry */
301
#define EAF_EMBEDDED 0x01                /* Data stored in eattr.u.data (part of type spec) */
302
#define EAF_VAR_LENGTH 0x02                /* Attribute length is variable (part of type spec) */
303
#define EAF_ORIGINATED 0x40                /* The attribute has originated locally */
304
#define EAF_TEMP 0x80                        /* A temporary attribute (the one stored in the tmp attr list) */
305

    
306
struct adata {
307
  unsigned int length;                        /* Length of data */
308
  byte data[0];
309
};
310

    
311
typedef struct ea_list {
312
  struct ea_list *next;                        /* In case we have an override list */
313
  byte flags;                                /* Flags: EALF_... */
314
  byte rfu;
315
  word count;                                /* Number of attributes */
316
  eattr attrs[0];                        /* Attribute definitions themselves */
317
} ea_list;
318

    
319
#define EALF_SORTED 1                        /* Attributes are sorted by code */
320
#define EALF_BISECT 2                        /* Use interval bisection for searching */
321
#define EALF_CACHED 4                        /* Attributes belonging to cached rta */
322

    
323
eattr *ea_find(ea_list *, unsigned ea);
324
void ea_dump(ea_list *);
325
void ea_sort(ea_list *);                /* Sort entries in all sub-lists */
326
unsigned ea_scan(ea_list *);                /* How many bytes do we need for merged ea_list */
327
void ea_merge(ea_list *from, ea_list *to); /* Merge sub-lists to allocated buffer */
328
int ea_same(ea_list *x, ea_list *y);        /* Test whether two ea_lists are identical */
329
unsigned int ea_hash(ea_list *e);        /* Calculate 16-bit hash value */
330
void ea_format(eattr *e, byte *buf);
331
#define EA_FORMAT_BUF_SIZE 256
332
ea_list *ea_append(ea_list *to, ea_list *what);
333

    
334
void rta_init(void);
335
rta *rta_lookup(rta *);                        /* Get rta equivalent to this one, uc++ */
336
static inline rta *rta_clone(rta *r) { r->uc++; return r; }
337
void rta__free(rta *r);
338
static inline void rta_free(rta *r) { if (r && !--r->uc) rta__free(r); }
339
void rta_dump(rta *);
340
void rta_dump_all(void);
341
static inline eattr * rta_find(rta *a, unsigned ea) { return ea_find(a->eattrs, ea); }
342
void rta_show(struct cli *, rta *, ea_list *);
343

    
344
extern struct protocol *attr_class_to_protocol[EAP_MAX];
345

    
346
/*
347
 *        Default protocol preferences
348
 */
349

    
350
#define DEF_PREF_DIRECT                    240        /* Directly connected */
351
#define DEF_PREF_STATIC                200        /* Static route */
352
#define DEF_PREF_OSPF_INTERNAL        150        /* OSPF intra-area, inter-area and type 1 external routes */
353
#define DEF_PREF_RIP                120        /* RIP */
354
#define DEF_PREF_BGP                100        /* BGP */
355
#define DEF_PREF_OSPF_EXTERNAL        80        /* OSPF external routes */
356
#define DEF_PREF_PIPE                70        /* Routes piped from other tables */
357

    
358
#endif