Statistics
| Branch: | Revision:

iof-bird-daemon / nest / route.h @ 9c11ec9e

History | View | Annotate | Download (11.3 KB)

1
/*
2
 *        BIRD Internet Routing Daemon -- Routing Table
3
 *
4
 *        (c) 1998--1999 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

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

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

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

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

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

    
67
void fit_init(struct fib_iterator *, struct fib *); /* Internal functions, don't call */
68
struct fib_node *fit_get(struct fib *, struct fib_iterator *);
69
void fit_put(struct fib_iterator *, struct fib_node *);
70

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

    
77
#define FIB_WALK_END } while (0)
78

    
79
#define FIB_ITERATE_INIT(it, fib) fit_init(it, fib)
80

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

    
93
#define FIB_ITERATE_END(z) z = z->next; } } while(0)
94

    
95
#define FIB_ITERATE_PUT(it, z) fit_put(it, z)
96

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

    
108
struct rtable_config {
109
  node n;
110
  char *name;
111
  struct rtable *table;
112
  struct proto_config *krt_attached;        /* Kernel syncer attached to this table */
113
};
114

    
115
typedef struct rtable {
116
  node n;                                /* Node in list of all tables */
117
  struct fib fib;
118
  char *name;                                /* Name of this table */
119
  list hooks;                                /* List of announcement hooks */
120
  int pipe_busy;                        /* Pipe loop detection */
121
} rtable;
122

    
123
typedef struct network {
124
  struct fib_node n;                        /* FIB flags reserved for kernel syncer */
125
  struct rte *routes;                        /* Available routes for this network */
126
} net;
127

    
128
typedef struct rte {
129
  struct rte *next;
130
  net *net;                                /* Network this RTE belongs to */
131
  struct rta *attrs;                        /* Attributes of this route */
132
  byte flags;                                /* Flags (REF_...) */
133
  byte pflags;                                /* Protocol-specific flags */
134
  word pref;                                /* Route preference */
135
  bird_clock_t lastmod;                        /* Last modified */
136
  union {                                /* Protocol-dependent data (metrics etc.) */
137
#ifdef CONFIG_RIP
138
    struct {
139
      node garbage;                        /* List for garbage collection */
140
      byte metric;                        /* RIP metric */
141
      u16 tag;                                /* External route tag */
142
    } rip;
143
#endif
144
#ifdef CONFIG_OSPF
145
    struct {
146
      u32 metric1, metric2;                /* OSPF Type 1 and Type 2 metrics */
147
      u32 tag;                                /* External route tag */
148
    } ospf;
149
#endif
150
#ifdef CONFIG_BGP
151
    struct {
152
    } bgp;
153
#endif
154
    struct {                                /* Routes generated by krt sync (both temporary and inherited ones) */
155
      s8 src;                                /* Alleged route source (see krt.h) */
156
      u8 proto;                                /* Kernel source protocol ID */
157
      u8 type;                                /* Kernel route type */
158
      u8 seen;                                /* Seen during last scan */
159
      u32 metric;                        /* Kernel metric */
160
    } krt;
161
  } u;
162
} rte;
163

    
164
#define REF_COW 1                        /* Copy this rte on write */
165

    
166
struct config;
167

    
168
void rt_init(void);
169
void rt_preconfig(struct config *);
170
void rt_commit(struct config *);
171
void rt_setup(pool *, rtable *, char *);
172
static inline net *net_find(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_find(&tab->fib, &addr, len); }
173
static inline net *net_get(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_get(&tab->fib, &addr, len); }
174
rte *rte_find(net *net, struct proto *p);
175
rte *rte_get_temp(struct rta *);
176
void rte_update(rtable *tab, net *net, struct proto *p, rte *new);
177
void rte_discard(rtable *tab, rte *old);
178
void rte_dump(rte *);
179
void rte_free(rte *);
180
rte *rte_do_cow(rte *);
181
static inline rte * rte_cow(rte *r) { return (r->flags & REF_COW) ? rte_do_cow(r) : r; }
182
void rt_dump(rtable *);
183
void rt_dump_all(void);
184
void rt_feed_baby(struct proto *p);
185
void rt_prune(rtable *tab);
186
void rt_prune_all(void);
187

    
188
/*
189
 *        Route Attributes
190
 *
191
 *        Beware: All standard BGP attributes must be represented here instead
192
 *        of making them local to the route. This is needed to ensure proper
193
 *        construction of BGP route attribute lists.
194
 */
195

    
196
typedef struct rta {
197
  struct rta *next, *prev;                /* Hash chain */
198
  struct rta *garbage;                        /* Garbage collector chain */
199
  struct proto *proto;                        /* Protocol instance */
200
  unsigned uc;                                /* Use count */
201
  byte source;                                /* Route source (RTS_...) */
202
  byte scope;                                /* Route scope (SCOPE_... -- see ip.h) */
203
  byte cast;                                /* Casting type (RTC_...) */
204
  byte dest;                                /* Route destination type (RTD_...) */
205
  byte flags;                                /* Route flags (RTF_...) */
206
  byte aflags;                                /* Attribute cache flags (RTAF_...) */
207
  byte rfu, rfu2;                        /* Padding */
208
  ip_addr gw;                                /* Next hop */
209
  ip_addr from;                                /* Advertising router */
210
  struct iface *iface;                        /* Outgoing interface */
211
  struct ea_list *attrs;                /* Extended Attribute chain */
212
} rta;
213

    
214
#define RTS_DUMMY 0                        /* Dummy route to be removed soon */
215
#define RTS_STATIC 1                        /* Normal static route */
216
#define RTS_INHERIT 2                        /* Route inherited from kernel */
217
#define RTS_DEVICE 3                        /* Device route */
218
#define RTS_STATIC_DEVICE 4                /* Static device route */
219
#define RTS_REDIRECT 5                        /* Learned via redirect */
220
#define RTS_RIP 6                        /* RIP route */
221
#define RTS_RIP_EXT 7                        /* RIP external route */
222
#define RTS_OSPF 8                        /* OSPF route */
223
#define RTS_OSPF_EXT 9                        /* OSPF external route */
224
#define RTS_OSPF_IA 10                        /* OSPF inter-area route */
225
#define RTS_OSPF_BOUNDARY 11                /* OSPF route to boundary router (???) */
226
#define RTS_BGP 12                        /* BGP route */
227
#define RTS_PIPE 13                        /* Inter-table wormhole */
228

    
229
#define RTC_UNICAST 0
230
#define RTC_BROADCAST 1
231
#define RTC_MULTICAST 2
232
#define RTC_ANYCAST 3                        /* IPv6 Anycast */
233

    
234
#define RTD_ROUTER 0                        /* Next hop is neighbor router */
235
#define RTD_DEVICE 1                        /* Points to device */
236
#define RTD_BLACKHOLE 2                        /* Silently drop packets */
237
#define RTD_UNREACHABLE 3                /* Reject as unreachable */
238
#define RTD_PROHIBIT 4                        /* Administratively prohibited */
239

    
240
#define RTF_EXTERIOR 1                        /* Learned via exterior protocol */
241
#define RTF_TAGGED 2                        /* Tagged external route learned via IGP */
242

    
243
#define RTAF_CACHED 1                        /* This is a cached rta */
244

    
245
/*
246
 *        Extended Route Attributes
247
 */
248

    
249
typedef struct eattr {
250
  word id;                                /* EA_CODE(EAP_..., protocol-dependent ID) */
251
  byte flags;                                /* Protocol-dependent flags */
252
  byte type;                                /* Attribute type and several flags (EAF_...) */
253
  union {
254
    u32 data;
255
    struct adata *ptr;                        /* Attribute data elsewhere */
256
  } u;
257
} eattr;
258

    
259
/* FIXME: Introduce real protocol numbers? */
260
#define EAP_GENERIC 0                        /* Generic attributes */
261
#define EAP_BGP 1                        /* BGP attributes */
262
#define EAP_RIP 2                        /* RIP */
263

    
264
#define EA_CODE(proto,id) (((proto) << 8) | (id))
265
#define EA_PROTO(ea) ((ea) >> 8)
266
#define EA_ID(ea) ((ea) & 0xff)
267

    
268
#define EAF_TYPE_MASK 0x0f                /* Mask with this to get type */
269
#define EAF_TYPE_INT 0x01                /* 32-bit signed integer number */
270
#define EAF_TYPE_OPAQUE 0x02                /* Opaque byte string (not filterable) */
271
#define EAF_TYPE_IP_ADDRESS 0x04        /* IP address [FIXME: embed at least for IPv4?] */
272
#define EAF_TYPE_AS_PATH 0x06                /* BGP AS path [FIXME: define storage layout] */
273
#define EAF_TYPE_INT_SET 0x0a                /* Set of integers (e.g., a community list) */
274
#define EAF_EMBEDDED 0x01                /* Data stored in eattr.u.data (part of type spec) */
275
#define EAF_VAR_LENGTH 0x02                /* Attribute length is variable */
276
#define EAF_INLINE 0x80                        /* Copy of an attribute inlined in rte (temporary ea_lists only) */
277

    
278
struct adata {
279
  unsigned int length;
280
  byte data[0];
281
};
282

    
283
typedef struct ea_list {
284
  struct ea_list *next;                        /* In case we have an override list */
285
  byte flags;                                /* Flags: EALF_... */
286
  byte rfu;
287
  word count;                                /* Number of attributes */
288
  eattr attrs[0];                        /* Attribute definitions themselves */
289
} ea_list;
290

    
291
#define EALF_SORTED 1                        /* Attributes are sorted by code */
292
#define EALF_BISECT 2                        /* Use interval bisection for searching */
293
#define EALF_CACHED 4                        /* Attributes belonging to cached rta */
294

    
295
eattr *ea_find(ea_list *, unsigned ea);
296
void ea_dump(ea_list *);
297
void ea_sort(ea_list *);                /* Sort entries in all sub-lists */
298
unsigned ea_scan(ea_list *);                /* How many bytes do we need for merged ea_list (0=merge not needed) */
299
void ea_merge(ea_list *from, ea_list *to); /* Merge sub-lists to allocated buffer */
300

    
301
void rta_init(void);
302
rta *rta_lookup(rta *);                        /* Get rta equivalent to this one, uc++ */
303
static inline rta *rta_clone(rta *r) { r->uc++; return r; }
304
void rta__free(rta *r);
305
static inline void rta_free(rta *r) { if (r && !--r->uc) rta__free(r); }
306
void rta_dump(rta *);
307
void rta_dump_all(void);
308
static inline eattr * rta_find(rta *a, unsigned ea) { return ea_find(a->attrs, ea); }
309

    
310
/*
311
 *        Default protocol preferences
312
 */
313

    
314
#define DEF_PREF_DIRECT                    240        /* Directly connected */
315
#define DEF_PREF_STATIC                200        /* Static route */
316
#define DEF_PREF_OSPF_INTERNAL        150        /* OSPF intra-area, inter-area and type 1 external routes */
317
#define DEF_PREF_RIP                120        /* RIP */
318
#define DEF_PREF_BGP                100        /* BGP */
319
#define DEF_PREF_OSPF_EXTERNAL        80        /* OSPF external routes */
320
#define DEF_PREF_RIP_EXTERNAL        70        /* RIP external routes */
321
#define DEF_PREF_PIPE                60        /* Routes piped from other tables */
322
#define DEF_PREF_UKR                50        /* Unidentified Kernel Route */
323
#define DEF_PREF_SINK                10        /* Sink route */
324

    
325
#endif