diff options
author | Martin Mares <mj@ucw.cz> | 1999-03-03 19:49:56 +0000 |
---|---|---|
committer | Martin Mares <mj@ucw.cz> | 1999-03-03 19:49:56 +0000 |
commit | 2d14045224f2233aed386eddf155d10a81892c3f (patch) | |
tree | 26d1e8fa4aefcdb04fb8c09c66eef92c1b6fa6eb /sysdep/unix | |
parent | b2280748ad5087b5dab54dd4e423053ffe1f2387 (diff) |
Rewrote the kernel syncer. The old layering was horrible.
The new kernel syncer is cleanly split between generic UNIX module
and OS dependent submodules:
- krt.c (the generic part)
- krt-iface (low-level functions for interface handling)
- krt-scan (low-level functions for routing table scanning)
- krt-set (low-level functions for setting of kernel routes)
krt-set and krt-iface are common for all BSD-like Unices, krt-scan is heavily
system dependent (most Unices require /dev/kmem parsing, Linux uses /proc),
Netlink substitues all three modules.
We expect each UNIX port supports kernel routing table scanning, kernel
interface table scanning, kernel route manipulation and possibly also
asynchronous event notifications (new route, interface state change;
not implemented yet) and build the KRT protocol on the top of these
primitive operations.
Diffstat (limited to 'sysdep/unix')
-rw-r--r-- | sysdep/unix/Modules | 16 | ||||
-rw-r--r-- | sysdep/unix/krt-iface.Y | 30 | ||||
-rw-r--r-- | sysdep/unix/krt-iface.c (renamed from sysdep/unix/sync-if.c) | 42 | ||||
-rw-r--r-- | sysdep/unix/krt-iface.h | 5 | ||||
-rw-r--r-- | sysdep/unix/krt-set.Y | 29 | ||||
-rw-r--r-- | sysdep/unix/krt-set.c | 26 | ||||
-rw-r--r-- | sysdep/unix/krt-set.h | 8 | ||||
-rw-r--r-- | sysdep/unix/krt.Y | 15 | ||||
-rw-r--r-- | sysdep/unix/krt.c | 291 | ||||
-rw-r--r-- | sysdep/unix/krt.h | 43 | ||||
-rw-r--r-- | sysdep/unix/main.c | 2 | ||||
-rw-r--r-- | sysdep/unix/sync-rt.c | 76 |
12 files changed, 360 insertions, 223 deletions
diff --git a/sysdep/unix/Modules b/sysdep/unix/Modules index e2ff0f85..bb0385d5 100644 --- a/sysdep/unix/Modules +++ b/sysdep/unix/Modules @@ -4,14 +4,16 @@ timer.h io.c unix.h -#ifndef CONFIG_NETLINK -sync-if.c -sync-rt.c -krt.Y +krt.c krt.h +krt.Y + +#ifdef CONFIG_UNIX_IFACE +krt-iface.c +krt-iface.h +#endif + +#ifdef CONFIG_UNIX_SET krt-set.c krt-set.h -krt-set.Y -krt-iface.h -krt-iface.Y #endif diff --git a/sysdep/unix/krt-iface.Y b/sysdep/unix/krt-iface.Y deleted file mode 100644 index c74eb02b..00000000 --- a/sysdep/unix/krt-iface.Y +++ /dev/null @@ -1,30 +0,0 @@ -/* - * BIRD -- UNIX Interface Syncer Configuration - * - * (c) 1998 Martin Mares <mj@ucw.cz> - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -CF_HDR - -#include "lib/krt-scan.h" - -CF_DECLS - -CF_KEYWORDS(LEARN, ROUTE, SCAN, TIME) - -CF_GRAMMAR - -CF_ADDTO(kern_proto, kern_proto krt_if_item ';') - -krt_if_item: - SCAN TIME expr { - /* Scan time of 0 means scan on startup only */ - ((struct krt_config *) this_proto)->ifopt.scan_time = $3; - } - ; - -CF_CODE - -CF_END diff --git a/sysdep/unix/sync-if.c b/sysdep/unix/krt-iface.c index 3dfa4f01..3b665a44 100644 --- a/sysdep/unix/sync-if.c +++ b/sysdep/unix/krt-iface.c @@ -24,9 +24,7 @@ #include "unix.h" -int if_scan_sock; - -static timer *if_scan_timer; +int if_scan_sock = -1; static void scan_ifs(struct ifreq *r, int cnt) @@ -132,15 +130,13 @@ scan_ifs(struct ifreq *r, int cnt) if_end_update(); } -static void -scan_if(timer *t) +void +krt_if_scan(struct krt_proto *p) { struct ifconf ic; static int last_ifbuf_size = 4*sizeof(struct ifreq); int res; - struct krt_proto *p = t->data; - DBG("It's interface scan time...\n"); for(;;) { if (last_ifbuf_size) @@ -173,40 +169,26 @@ scan_if(timer *t) DBG("Increased ifconf buffer size to %d\n", last_ifbuf_size); #endif } - krt_scan_ifaces_done(p); -} - -void -krt_if_start(struct krt_proto *p) -{ - struct krt_config *c = (struct krt_config *) p->p.cf; - - if_scan_timer = tm_new(p->p.pool); - if_scan_timer->hook = scan_if; - if_scan_timer->data = p; - if_scan_timer->recurrent = c->ifopt.scan_time; - scan_if(if_scan_timer); - tm_start(if_scan_timer, c->ifopt.scan_time); } void krt_if_preconfig(struct krt_config *c) { - c->ifopt.scan_time = 60; } void -krt_if_shutdown(struct krt_proto *p) +krt_if_start(struct krt_proto *p) { - tm_stop(if_scan_timer); - /* FIXME: What should we do with interfaces? */ + if (if_scan_sock < 0) + { + if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + DBG("Using socket %d for interface and route scanning\n", if_scan_sock); + if (if_scan_sock < 0) + die("Cannot create scanning socket: %m"); + } } void -scan_if_init(void) +krt_if_shutdown(struct krt_proto *p) { - if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); - DBG("Using socket %d for interface and route scanning\n", if_scan_sock); - if (if_scan_sock < 0) - die("Cannot create scanning socket: %m"); } diff --git a/sysdep/unix/krt-iface.h b/sysdep/unix/krt-iface.h index 87c88515..b27e86dc 100644 --- a/sysdep/unix/krt-iface.h +++ b/sysdep/unix/krt-iface.h @@ -1,5 +1,5 @@ /* - * BIRD -- Unix Kernel Interface Syncer -- Setting Parameters + * BIRD -- Unix Kernel Interface Syncer * * (c) 1998--1999 Martin Mares <mj@ucw.cz> * @@ -10,10 +10,11 @@ #define _BIRD_KRT_IFACE_H_ struct krt_if_params { - int scan_time; }; struct krt_if_status { }; +extern int if_scan_sock; + #endif diff --git a/sysdep/unix/krt-set.Y b/sysdep/unix/krt-set.Y deleted file mode 100644 index 551ee16d..00000000 --- a/sysdep/unix/krt-set.Y +++ /dev/null @@ -1,29 +0,0 @@ -/* - * BIRD -- UNIX Kernel Syncer Configuration - * - * (c) 1998--1999 Martin Mares <mj@ucw.cz> - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -CF_HDR - -#include "lib/krt-scan.h" - -CF_DECLS - -CF_KEYWORDS(PERSIST) - -CF_GRAMMAR - -CF_ADDTO(kern_proto, kern_proto krt_set_item ';') - -krt_set_item: - PERSIST bool { - ((struct krt_config *) this_proto)->setopt.persist = $2; - } - ; - -CF_CODE - -CF_END diff --git a/sysdep/unix/krt-set.c b/sysdep/unix/krt-set.c index e77015b3..dae01f46 100644 --- a/sysdep/unix/krt-set.c +++ b/sysdep/unix/krt-set.c @@ -84,7 +84,7 @@ krt_ioctl(int ioc, rte *e, char *name) log(L_ERR "%s(%I/%d): %m", name, net->n.prefix, net->n.pxlen); } -void +static void krt_remove_route(rte *old) { net *net = old->net; @@ -98,7 +98,7 @@ krt_remove_route(rte *old) krt_ioctl(SIOCDELRT, old, "SIOCDELRT"); } -void +static void krt_add_route(rte *new) { net *net = new->net; @@ -126,7 +126,6 @@ krt_set_start(struct krt_proto *x) { if (if_scan_sock < 0) bug("krt set: missing socket"); - x->p.rt_notify = krt_set_notify; } void @@ -137,25 +136,4 @@ krt_set_preconfig(struct krt_config *c) void krt_set_shutdown(struct krt_proto *x) { - struct rtable *t = &master_table; - - if (((struct krt_config *) x->p.cf)->setopt.persist) - return; - DBG("Flushing kernel routes...\n"); - while (t && t->tos) - t = t->sibling; - if (!t) - return; - FIB_WALK(&t->fib, f) - { - net *n = (net *) f; - rte *e = n->routes; - if (e) - { - rta *a = e->attrs; - if (a->source != RTS_DEVICE && a->source != RTS_INHERIT) - krt_remove_route(e); - } - } - FIB_WALK_END; } diff --git a/sysdep/unix/krt-set.h b/sysdep/unix/krt-set.h index ec96e652..cfa5fb76 100644 --- a/sysdep/unix/krt-set.h +++ b/sysdep/unix/krt-set.h @@ -1,5 +1,5 @@ /* - * BIRD -- Unix Kernel Route Syncer -- Setting Parameters + * BIRD -- Unix Kernel Route Syncer -- Setting * * (c) 1998--1999 Martin Mares <mj@ucw.cz> * @@ -10,15 +10,9 @@ #define _BIRD_KRT_SET_H_ struct krt_set_params { - int persist; }; struct krt_set_status { }; -void krt_remove_route(rte *old); -void krt_add_route(rte *new); -int krt_capable(rte *e); -void krt_set_notify(struct proto *x, net *net, rte *new, rte *old); - #endif diff --git a/sysdep/unix/krt.Y b/sysdep/unix/krt.Y index 7711f3ba..661e5052 100644 --- a/sysdep/unix/krt.Y +++ b/sysdep/unix/krt.Y @@ -10,9 +10,11 @@ CF_HDR #include "lib/krt.h" +#define THIS_KRT ((struct krt_config *) this_proto) + CF_DECLS -CF_KEYWORDS(KERNEL) +CF_KEYWORDS(KERNEL, PERSIST, SCAN, TIME, ROUTE, LEARN) CF_GRAMMAR @@ -28,6 +30,17 @@ kern_proto_start: proto_start KERNEL { CF_ADDTO(kern_proto, kern_proto_start '{') CF_ADDTO(kern_proto, kern_proto proto_item ';') +CF_ADDTO(kern_proto, kern_proto kern_item ';') + +kern_item: + PERSIST bool { THIS_KRT->persist = $2; } + | SCAN TIME expr { + /* Scan time of 0 means scan on startup only */ + THIS_KRT->scan_time = $3; + } + | ROUTE SCAN TIME expr { THIS_KRT->route_scan_time = $4; } + | LEARN bool { THIS_KRT->learn = $2; } + ; CF_CODE diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c new file mode 100644 index 00000000..d44a57d8 --- /dev/null +++ b/sysdep/unix/krt.c @@ -0,0 +1,291 @@ +/* + * BIRD -- UNIX Kernel Synchronization + * + * (c) 1998--1999 Martin Mares <mj@ucw.cz> + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#define LOCAL_DEBUG + +#include "nest/bird.h" +#include "nest/iface.h" +#include "nest/route.h" +#include "nest/protocol.h" +#include "lib/timer.h" + +#include "unix.h" +#include "krt.h" + +struct proto_config *cf_krt; + +/* + * Routes + */ + +static void +krt_flush_routes(struct krt_proto *p) +{ + struct rtable *t = &master_table; + + DBG("Flushing kernel routes...\n"); + while (t && t->tos) + t = t->sibling; + if (!t) + return; + FIB_WALK(&t->fib, f) + { + net *n = (net *) f; + rte *e = n->routes; + if (e) + { + rta *a = e->attrs; + if (a->source != RTS_DEVICE && a->source != RTS_INHERIT) + krt_set_notify(&p->p, e->net, e, NULL); + } + } + FIB_WALK_END; +} + +/* FIXME: Inbound/outbound route filtering? */ +/* FIXME: Synchronization of multiple routing tables? */ + +static int +krt_uptodate(rte *k, rte *e) +{ + rta *ka = k->attrs, *ea = e->attrs; + + if (ka->dest != ea->dest) + return 0; + switch (ka->dest) + { + case RTD_ROUTER: + return ipa_equal(ka->gw, ea->gw); + case RTD_DEVICE: + return !strcmp(ka->iface->name, ea->iface->name); + default: + return 1; + } +} + +/* + * This gets called back when the low-level scanning code discovers a route. + * We expect that the route is a temporary rte and its attributes are uncached. + */ + +void +krt_got_route(struct krt_proto *p, rte *e) +{ + rte *old; + net *net = e->net; + int verdict; + + if (net->n.flags) + { + /* Route to this destination was already seen. Strange, but it happens... */ + DBG("Already seen.\n"); + return; + } + + old = net->routes; + if (old && !krt_capable(old)) + old = NULL; + if (old) + { + if (krt_uptodate(e, net->routes)) + verdict = KRF_SEEN; + else + verdict = KRF_UPDATE; + } + else if (KRT_CF->learn && !net->routes) + verdict = KRF_LEARN; + else + verdict = KRF_DELETE; + + DBG("krt_parse_entry: verdict=%s\n", ((char *[]) { "CREATE", "SEEN", "UPDATE", "DELETE", "LEARN" }) [verdict]); + + net->n.flags = verdict; + if (verdict != KRF_SEEN) + { + /* Get a cached copy of attributes and link the route */ + rta *a = e->attrs; + a->source = RTS_DUMMY; + e->attrs = rta_lookup(a); + e->next = net->routes; + net->routes = e; + } + else + rte_free(e); +} + +static void +krt_prune(struct krt_proto *p) +{ + struct proto *pp = &p->p; + struct rtable *t = &master_table; + struct fib_node *f; + + DBG("Pruning routes...\n"); + while (t && t->tos) + t = t->sibling; + if (!t) + return; + FIB_WALK(&t->fib, f) + { + net *n = (net *) f; + int verdict = f->flags; + rte *new, *old; + + if (verdict != KRF_CREATE && verdict != KRF_SEEN) + { + old = n->routes; + n->routes = old->next; + } + else + old = NULL; + new = n->routes; + + switch (verdict) + { + case KRF_CREATE: + if (new) + { + if (new->attrs->source == RTS_INHERIT) + { + DBG("krt_prune: removing inherited %I/%d\n", n->n.prefix, n->n.pxlen); + rte_update(n, pp, NULL); + } + else + { + DBG("krt_prune: reinstalling %I/%d\n", n->n.prefix, n->n.pxlen); + krt_set_notify(pp, n, new, NULL); + } + } + break; + case KRF_SEEN: + /* Nothing happens */ + break; + case KRF_UPDATE: + DBG("krt_prune: updating %I/%d\n", n->n.prefix, n->n.pxlen); + krt_set_notify(pp, n, new, old); + break; + case KRF_DELETE: + DBG("krt_prune: deleting %I/%d\n", n->n.prefix, n->n.pxlen); + krt_set_notify(pp, n, NULL, old); + break; + case KRF_LEARN: + DBG("krt_prune: learning %I/%d\n", n->n.prefix, n->n.pxlen); + rte_update(n, pp, new); + break; + default: + bug("krt_prune: invalid route status"); + } + + if (old) + rte_free(old); + f->flags = 0; + } + FIB_WALK_END; +} + +/* + * Periodic scanning + */ + +static timer *krt_scan_timer; + +static void +krt_scan(timer *t) +{ + struct krt_proto *p = t->data; + + DBG("KRT: It's scan time...\n"); + krt_if_scan(p); + + p->accum_time += KRT_CF->scan_time; + if (KRT_CF->route_scan_time && p->accum_time >= KRT_CF->route_scan_time) + { + p->accum_time %= KRT_CF->route_scan_time; + DBG("Scanning kernel routing table...\n"); + krt_scan_fire(p); + krt_prune(p); + } +} + +/* + * Protocol glue + */ + +static int +krt_start(struct proto *P) +{ + struct krt_proto *p = (struct krt_proto *) P; + + p->accum_time = 0; + + krt_if_start(p); + krt_scan_start(p); + krt_set_start(p); + + /* Start periodic interface scanning */ + krt_scan_timer = tm_new(P->pool); + krt_scan_timer->hook = krt_scan; + krt_scan_timer->data = p; + krt_scan_timer->recurrent = KRT_CF->scan_time; + krt_scan(krt_scan_timer); + tm_start(krt_scan_timer, KRT_CF->scan_time); + + return PS_UP; +} + +int +krt_shutdown(struct proto *P) +{ + struct krt_proto *p = (struct krt_proto *) P; + + if (!KRT_CF->persist) + krt_flush_routes(p); + + krt_set_shutdown(p); + krt_scan_shutdown(p); + + /* Stop periodic interface scans */ + tm_stop(krt_scan_timer); + krt_if_shutdown(p); + /* FIXME: What should we do with interfaces? */ + + return PS_DOWN; +} + +static void +krt_preconfig(struct protocol *x, struct config *c) +{ + struct krt_config *z = proto_config_new(&proto_unix_kernel, sizeof(struct krt_config)); + + cf_krt = &z->c; + z->c.preference = DEF_PREF_UKR; + z->scan_time = z->route_scan_time = 60; + z->learn = z->persist = 0; + + krt_scan_preconfig(z); + krt_set_preconfig(z); + krt_if_preconfig(z); +} + +static struct proto * +krt_init(struct proto_config *c) +{ + struct krt_proto *p = proto_new(c, sizeof(struct krt_proto)); + + p->p.rt_notify = krt_set_notify; + return &p->p; +} + +struct protocol proto_unix_kernel = { + name: "Kernel", + priority: 90, + preconfig: krt_preconfig, + init: krt_init, + start: krt_start, + shutdown: krt_shutdown, +}; diff --git a/sysdep/unix/krt.h b/sysdep/unix/krt.h index cebf3354..1c59799f 100644 --- a/sysdep/unix/krt.h +++ b/sysdep/unix/krt.h @@ -1,7 +1,7 @@ /* - * BIRD -- Unix Kernel Route Syncer + * BIRD -- UNIX Kernel Route Syncer * - * (c) 1998 Martin Mares <mj@ucw.cz> + * (c) 1998--1999 Martin Mares <mj@ucw.cz> * * Can be freely distributed and used under the terms of the GNU GPL. */ @@ -9,6 +9,9 @@ #ifndef _BIRD_KRT_H_ #define _BIRD_KRT_H_ +struct krt_config; +struct krt_proto; + #include "lib/krt-scan.h" #include "lib/krt-set.h" #include "lib/krt-iface.h" @@ -21,32 +24,42 @@ #define KRF_DELETE 3 /* Should be deleted */ #define KRF_LEARN 4 /* We should learn this route */ -/* sync-rt.c */ +/* krt.c */ extern struct protocol proto_unix_kernel; struct krt_config { struct proto_config c; - struct krt_set_params setopt; - struct krt_scan_params scanopt; - struct krt_if_params ifopt; + struct krt_set_params set; + struct krt_scan_params scan; + struct krt_if_params iface; + int persist; /* Keep routes when we exit */ + int scan_time; /* How often we re-scan interfaces */ + int route_scan_time; /* How often we re-scan routes */ + int learn; /* Learn routes from other sources */ }; struct krt_proto { struct proto p; - struct krt_set_status setstat; - struct krt_scan_status scanstat; - struct krt_if_status ifstat; + struct krt_set_status set; + struct krt_scan_status scan; + struct krt_if_status iface; + int accum_time; /* Accumulated route scanning time */ }; extern struct proto_config *cf_krt; +#define KRT_CF ((struct krt_config *)p->p.cf) + +void krt_got_route(struct krt_proto *p, struct rte *e); + /* krt-scan.c */ void krt_scan_preconfig(struct krt_config *); void krt_scan_start(struct krt_proto *); void krt_scan_shutdown(struct krt_proto *); -void krt_scan_ifaces_done(struct krt_proto *); + +void krt_scan_fire(struct krt_proto *); /* krt-set.c */ @@ -54,15 +67,15 @@ void krt_set_preconfig(struct krt_config *); void krt_set_start(struct krt_proto *); void krt_set_shutdown(struct krt_proto *); -/* sync-if.c */ - -extern int if_scan_sock; -extern int if_scan_period; +int krt_capable(rte *e); +void krt_set_notify(struct proto *x, net *net, rte *new, rte *old); -void scan_if_init(void); +/* krt-iface.c */ void krt_if_preconfig(struct krt_config *); void krt_if_start(struct krt_proto *); void krt_if_shutdown(struct krt_proto *); +void krt_if_scan(struct krt_proto *); + #endif diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 71bb712a..e5e9934c 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -204,8 +204,6 @@ main(int argc, char **argv) signal_init(); - scan_if_init(); - protos_start(); ev_run_list(&global_event_list); diff --git a/sysdep/unix/sync-rt.c b/sysdep/unix/sync-rt.c deleted file mode 100644 index 99ef92a0..00000000 --- a/sysdep/unix/sync-rt.c +++ /dev/null @@ -1,76 +0,0 @@ -/* - * BIRD -- Unix Routing Table Scanning and Syncing - * - * (c) 1998--1999 Martin Mares <mj@ucw.cz> - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#include <string.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <sys/ioctl.h> -#include <errno.h> - -#define LOCAL_DEBUG - -#include "nest/bird.h" -#include "nest/iface.h" -#include "nest/route.h" -#include "nest/protocol.h" -#include "lib/timer.h" - -#include "unix.h" -#include "krt.h" - -struct proto_config *cf_krt; - -static int -krt_start(struct proto *p) -{ - struct krt_proto *k = (struct krt_proto *) p; - - krt_scan_start(k); - krt_set_start(k); - krt_if_start(k); - return PS_UP; -} - -int -krt_shutdown(struct proto *p) -{ - struct krt_proto *k = (struct krt_proto *) p; - - krt_scan_shutdown(k); - krt_if_shutdown(k); - krt_set_shutdown(k); - return PS_DOWN; -} - -static void -krt_preconfig(struct protocol *x, struct config *c) -{ - struct krt_config *z = proto_config_new(&proto_unix_kernel, sizeof(struct krt_config)); - - cf_krt = &z->c; - z->c.preference = DEF_PREF_UKR; - krt_scan_preconfig(z); - krt_set_preconfig(z); - krt_if_preconfig(z); -} - -static struct proto * -krt_init(struct proto_config *c) -{ - struct krt_proto *p = proto_new(c, sizeof(struct krt_proto)); - - return &p->p; -} - -struct protocol proto_unix_kernel = { - name: "Kernel", - preconfig: krt_preconfig, - init: krt_init, - start: krt_start, - shutdown: krt_shutdown, -}; |