Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (11.3 KB)

1 58ef912c Martin Mares
/*
2
 *        BIRD Internet Routing Daemon -- Routing Table
3
 *
4 e2dc2f30 Martin Mares
 *        (c) 1998--1999 Martin Mares <mj@ucw.cz>
5 58ef912c Martin Mares
 *
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 0e02abfd Martin Mares
#include "lib/lists.h"
13 1feea03e Martin Mares
#include "lib/resource.h"
14 a2ccbb0b Martin Mares
#include "lib/timer.h"
15 58ef912c Martin Mares
16 2326b001 Martin Mares
struct protocol;
17 4cc78c50 Martin Mares
struct proto;
18 2326b001 Martin Mares
19 58ef912c Martin Mares
/*
20
 *        Generic data structure for storing network prefixes. Also used
21 3ab001b9 Martin Mares
 *        for the master routing table. Currently implemented as a hash
22 62aa008a Martin Mares
 *        table.
23 58ef912c Martin Mares
 *
24
 *        Available operations:
25
 *                - insertion of new entry
26
 *                - deletion of entry
27 62aa008a Martin Mares
 *                - searching for entry by network prefix
28 3ab001b9 Martin Mares
 *                - asynchronous retrieval of fib contents
29 58ef912c Martin Mares
 */
30
31
struct fib_node {
32 3ab001b9 Martin Mares
  struct fib_node *next;                /* Next in hash chain */
33
  struct fib_iterator *readers;                /* List of readers of this node */
34 58ef912c Martin Mares
  byte pxlen;
35 4c45595e Martin Mares
  byte flags;                                /* User-defined */
36 12df4d90 Martin Mares
  byte x0, x1;                                /* User-defined */
37 3ab001b9 Martin Mares
  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 58ef912c Martin Mares
};
47
48
struct fib {
49 62aa008a Martin Mares
  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 a8b60382 Martin Mares
  unsigned int hash_size;                /* Number of hash table entries (a power of two) */
53 3ab001b9 Martin Mares
  unsigned int hash_order;                /* Binary logarithm of hash_size */
54
  unsigned int hash_shift;                /* 16 - hash_log */
55 a8b60382 Martin Mares
  unsigned int entries;                        /* Number of entries */
56
  unsigned int entries_min, entries_max;/* Entry count limits (else start rehashing) */
57 58ef912c Martin Mares
  void (*init)(struct fib_node *);        /* Constructor */
58
};
59
60 3ab001b9 Martin Mares
void fib_init(struct fib *, pool *, unsigned node_size, unsigned hash_order, void (*init)(struct fib_node *));
61 58ef912c Martin Mares
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 62aa008a Martin Mares
void fib_delete(struct fib *, void *);        /* Remove fib entry */
64 a8b60382 Martin Mares
void fib_free(struct fib *);                /* Destroy the fib */
65 3ab001b9 Martin Mares
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 a8b60382 Martin Mares
71 236d4eb8 Martin Mares
#define FIB_WALK(fib, z) do {                                        \
72 62aa008a Martin Mares
        struct fib_node *z, **ff = (fib)->hash_table;                \
73 a8b60382 Martin Mares
        unsigned int count = (fib)->hash_size;                        \
74
        while (count--)                                                \
75 236d4eb8 Martin Mares
          for(z = *ff++; z; z=z->next)
76
77
#define FIB_WALK_END } while (0)
78 a8b60382 Martin Mares
79 3ab001b9 Martin Mares
#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 2569bc40 Martin Mares
        fis_again:        if (!z) {                                \
87 3ab001b9 Martin Mares
                        if (++hpos >= count)                        \
88
                                break;                                \
89
                        z = (fib)->hash_table[hpos];                \
90 2569bc40 Martin Mares
                        goto fis_again;                                \
91 3ab001b9 Martin Mares
                }
92
93 2569bc40 Martin Mares
#define FIB_ITERATE_END(z) z = z->next; } } while(0)
94 3ab001b9 Martin Mares
95
#define FIB_ITERATE_PUT(it, z) fit_put(it, z)
96
97 a8b60382 Martin Mares
/*
98 08e2d625 Martin Mares
 *        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 58ef912c Martin Mares
 *        Each of the RTE's contains variable data (the preference and protocol-dependent
103 62aa008a Martin Mares
 *        metrics) and a pointer to a route attribute block common for many routes).
104 08e2d625 Martin Mares
 *
105
 *        It's guaranteed that there is at most one RTE for every (prefix,proto) pair.
106 58ef912c Martin Mares
 */
107
108 0e02abfd Martin Mares
struct rtable_config {
109
  node n;
110
  char *name;
111
  struct rtable *table;
112 7de45ba4 Martin Mares
  struct proto_config *krt_attached;        /* Kernel syncer attached to this table */
113 0e02abfd Martin Mares
};
114
115 62aa008a Martin Mares
typedef struct rtable {
116 0e02abfd Martin Mares
  node n;                                /* Node in list of all tables */
117 62aa008a Martin Mares
  struct fib fib;
118
  char *name;                                /* Name of this table */
119 0e02abfd Martin Mares
  list hooks;                                /* List of announcement hooks */
120 9c11ec9e Martin Mares
  int pipe_busy;                        /* Pipe loop detection */
121 62aa008a Martin Mares
} rtable;
122
123 58ef912c Martin Mares
typedef struct network {
124 3ab001b9 Martin Mares
  struct fib_node n;                        /* FIB flags reserved for kernel syncer */
125 58ef912c Martin Mares
  struct rte *routes;                        /* Available routes for this network */
126
} net;
127
128
typedef struct rte {
129
  struct rte *next;
130 a0762910 Martin Mares
  net *net;                                /* Network this RTE belongs to */
131 1b769b08 Martin Mares
  struct rta *attrs;                        /* Attributes of this route */
132 58ef912c Martin Mares
  byte flags;                                /* Flags (REF_...) */
133 481f6985 Martin Mares
  byte pflags;                                /* Protocol-specific flags */
134 58ef912c Martin Mares
  word pref;                                /* Route preference */
135 a2ccbb0b Martin Mares
  bird_clock_t lastmod;                        /* Last modified */
136 58ef912c Martin Mares
  union {                                /* Protocol-dependent data (metrics etc.) */
137
#ifdef CONFIG_RIP
138
    struct {
139 feb6abe0 Pavel Machek
      node garbage;                        /* List for garbage collection */
140 58ef912c Martin Mares
      byte metric;                        /* RIP metric */
141 481f6985 Martin Mares
      u16 tag;                                /* External route tag */
142 58ef912c Martin Mares
    } rip;
143
#endif
144
#ifdef CONFIG_OSPF
145
    struct {
146
      u32 metric1, metric2;                /* OSPF Type 1 and Type 2 metrics */
147 481f6985 Martin Mares
      u32 tag;                                /* External route tag */
148 58ef912c Martin Mares
    } ospf;
149
#endif
150
#ifdef CONFIG_BGP
151
    struct {
152
    } bgp;
153
#endif
154 c10421d3 Martin Mares
    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 58ef912c Martin Mares
  } u;
162
} rte;
163
164 e2dc2f30 Martin Mares
#define REF_COW 1                        /* Copy this rte on write */
165
166 0e02abfd Martin Mares
struct config;
167 2326b001 Martin Mares
168
void rt_init(void);
169 0e02abfd Martin Mares
void rt_preconfig(struct config *);
170
void rt_commit(struct config *);
171 c10421d3 Martin Mares
void rt_setup(pool *, rtable *, char *);
172 08e2d625 Martin Mares
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 2326b001 Martin Mares
rte *rte_find(net *net, struct proto *p);
175 1b769b08 Martin Mares
rte *rte_get_temp(struct rta *);
176 0e02abfd Martin Mares
void rte_update(rtable *tab, net *net, struct proto *p, rte *new);
177
void rte_discard(rtable *tab, rte *old);
178 a0762910 Martin Mares
void rte_dump(rte *);
179 04925e90 Martin Mares
void rte_free(rte *);
180 e2dc2f30 Martin Mares
rte *rte_do_cow(rte *);
181
static inline rte * rte_cow(rte *r) { return (r->flags & REF_COW) ? rte_do_cow(r) : r; }
182 2326b001 Martin Mares
void rt_dump(rtable *);
183 a2ccbb0b Martin Mares
void rt_dump_all(void);
184 47b79306 Martin Mares
void rt_feed_baby(struct proto *p);
185 2569bc40 Martin Mares
void rt_prune(rtable *tab);
186 0e02abfd Martin Mares
void rt_prune_all(void);
187 2326b001 Martin Mares
188 58ef912c Martin Mares
/*
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 1b769b08 Martin Mares
typedef struct rta {
197
  struct rta *next, *prev;                /* Hash chain */
198
  struct rta *garbage;                        /* Garbage collector chain */
199 58ef912c Martin Mares
  struct proto *proto;                        /* Protocol instance */
200
  unsigned uc;                                /* Use count */
201
  byte source;                                /* Route source (RTS_...) */
202 a8b60382 Martin Mares
  byte scope;                                /* Route scope (SCOPE_... -- see ip.h) */
203 58ef912c Martin Mares
  byte cast;                                /* Casting type (RTC_...) */
204
  byte dest;                                /* Route destination type (RTD_...) */
205
  byte flags;                                /* Route flags (RTF_...) */
206 04925e90 Martin Mares
  byte aflags;                                /* Attribute cache flags (RTAF_...) */
207 08e2d625 Martin Mares
  byte rfu, rfu2;                        /* Padding */
208 58ef912c Martin Mares
  ip_addr gw;                                /* Next hop */
209 a8b60382 Martin Mares
  ip_addr from;                                /* Advertising router */
210 58ef912c Martin Mares
  struct iface *iface;                        /* Outgoing interface */
211
  struct ea_list *attrs;                /* Extended Attribute chain */
212
} rta;
213
214 618533af Martin Mares
#define RTS_DUMMY 0                        /* Dummy route to be removed soon */
215 58ef912c Martin Mares
#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 481f6985 Martin Mares
#define RTS_OSPF_BOUNDARY 11                /* OSPF route to boundary router (???) */
226 58ef912c Martin Mares
#define RTS_BGP 12                        /* BGP route */
227 9c11ec9e Martin Mares
#define RTS_PIPE 13                        /* Inter-table wormhole */
228 58ef912c Martin Mares
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 481f6985 Martin Mares
#define RTF_EXTERIOR 1                        /* Learned via exterior protocol */
241 a8b60382 Martin Mares
#define RTF_TAGGED 2                        /* Tagged external route learned via IGP */
242 481f6985 Martin Mares
243 04925e90 Martin Mares
#define RTAF_CACHED 1                        /* This is a cached rta */
244
245 58ef912c Martin Mares
/*
246
 *        Extended Route Attributes
247
 */
248
249
typedef struct eattr {
250 b77ae37d Martin Mares
  word id;                                /* EA_CODE(EAP_..., protocol-dependent ID) */
251
  byte flags;                                /* Protocol-dependent flags */
252
  byte type;                                /* Attribute type and several flags (EAF_...) */
253 58ef912c Martin Mares
  union {
254
    u32 data;
255
    struct adata *ptr;                        /* Attribute data elsewhere */
256
  } u;
257
} eattr;
258
259 b77ae37d Martin Mares
/* FIXME: Introduce real protocol numbers? */
260 58ef912c Martin Mares
#define EAP_GENERIC 0                        /* Generic attributes */
261
#define EAP_BGP 1                        /* BGP attributes */
262 9c9e49ac Pavel Machek
#define EAP_RIP 2                        /* RIP */
263 58ef912c Martin Mares
264 b77ae37d Martin Mares
#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 58ef912c Martin Mares
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 b77ae37d Martin Mares
  byte flags;                                /* Flags: EALF_... */
286 58ef912c Martin Mares
  byte rfu;
287 b77ae37d Martin Mares
  word count;                                /* Number of attributes */
288 58ef912c Martin Mares
  eattr attrs[0];                        /* Attribute definitions themselves */
289
} ea_list;
290
291 b77ae37d Martin Mares
#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 58ef912c Martin Mares
295 b77ae37d Martin Mares
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 58ef912c Martin Mares
301 2326b001 Martin Mares
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 b77ae37d Martin Mares
void rta__free(rta *r);
305
static inline void rta_free(rta *r) { if (r && !--r->uc) rta__free(r); }
306 2326b001 Martin Mares
void rta_dump(rta *);
307
void rta_dump_all(void);
308 b77ae37d Martin Mares
static inline eattr * rta_find(rta *a, unsigned ea) { return ea_find(a->attrs, ea); }
309 2326b001 Martin Mares
310 a8b60382 Martin Mares
/*
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 9c11ec9e Martin Mares
#define DEF_PREF_PIPE                60        /* Routes piped from other tables */
322 0432c017 Martin Mares
#define DEF_PREF_UKR                50        /* Unidentified Kernel Route */
323 a8b60382 Martin Mares
#define DEF_PREF_SINK                10        /* Sink route */
324
325 58ef912c Martin Mares
#endif