]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Moved to a much more systematic way of configuring kernel protocols.
authorMartin Mares <mj@ucw.cz>
Fri, 26 Mar 1999 21:44:38 +0000 (21:44 +0000)
committerMartin Mares <mj@ucw.cz>
Fri, 26 Mar 1999 21:44:38 +0000 (21:44 +0000)
  o  Nothing is configured automatically. You _need_ to specify
     the kernel syncer in config file in order to get it started.
  o  Syncing has been split to route syncer (protocol "Kernel") and
     interface syncer (protocol "Device"), device routes are generated
     by protocol "Direct" (now can exist in multiple instances, so that
     it will be possible to feed different device routes to different
     routing tables once multiple tables get supported).

See doc/bird.conf.example for a living example of these shiny features.

13 files changed:
TODO
doc/bird.conf.example
nest/config.Y
nest/rt-dev.c
sysdep/linux/netlink/krt-iface.h
sysdep/linux/netlink/netlink.c
sysdep/unix/io.c
sysdep/unix/krt-iface.c
sysdep/unix/krt.Y
sysdep/unix/krt.c
sysdep/unix/krt.h
sysdep/unix/main.c
sysdep/unix/unix.h

diff --git a/TODO b/TODO
index 1544345ff0fc03a06906fa076abb174e3a740b7a..7fc453dd5169af7aba2aa730ff274b71ca824eda 100644 (file)
--- a/TODO
+++ b/TODO
@@ -6,6 +6,8 @@ Core
 - config: define ipaddr constants?
 - config: better default protocol names
 
+- do we really need preconfig?
+
 - counters (according to SNMP MIB?)
 - better memory allocators
 - default preferences of protocols: prefer BGP over OSPF/RIP external routes?
index 1d8203dc82df03e0d0e812339e66c4e5d8fb369a..d91a792a9b4b538cdea34b1adb94953e24e93adb 100644 (file)
@@ -16,7 +16,7 @@
 #      debug all
 #}
 
-protocol device {
+protocol direct {
 #      disabled
 #      interface "-eth*", "*"
 }
@@ -25,13 +25,16 @@ protocol kernel {
 #      disabled
 #      learn                   # Learn all routes from the kernel
        persist                 # Don't remove routes on bird shutdown
-       scan time 10            # Scan kernel tables every 10 seconds
-       route scan time 20      # But routes only every 20 seconds
+       scan time 20            # Scan kernel routing table every 20 seconds
 #      async off               # Netlink: Disable asynchronous events
 #      input filter sink
 #      output filter okay
 }
 
+protocol device {
+       scan time 10            # Scan interfaces every 10 seconds
+}
+
 protocol static {
 #      disabled
 #      route 0.0.0.0/0 via 62.168.0.13
index 83001328f53bdd9514fd5cb3b64cbda8733f2d76..61a12cab7b570535d28cba9adb1563d887152f8a 100644 (file)
@@ -16,7 +16,7 @@ void rt_dev_add_iface(char *);
 
 CF_DECLS
 
-CF_KEYWORDS(ROUTER, ID, PROTOCOL, PREFERENCE, DISABLED, DEBUG, ALL, OFF, DEVICE)
+CF_KEYWORDS(ROUTER, ID, PROTOCOL, PREFERENCE, DISABLED, DEBUG, ALL, OFF, DIRECT)
 CF_KEYWORDS(INTERFACE, INPUT, OUTPUT, FILTER)
 
 %type <i> idval
@@ -44,7 +44,7 @@ proto_start: PROTOCOL
 
 proto_name:
    /* EMPTY */ {
-     struct symbol *s = cf_default_name(this_proto->proto->name);
+     struct symbol *s = cf_default_name(this_proto->proto->name, &this_proto->proto->name_counter);
      s->class = SYM_PROTO;
      s->def = this_proto;
      this_proto->name = s->name;
@@ -71,24 +71,30 @@ proto_item:
  | OUTPUT FILTER filter { this_proto->out_filter = $3; }
  ;
 
-/* Device protocol */
+/* Direct device route protocol */
 
 CF_ADDTO(proto, dev_proto '}')
 
-dev_proto_start: proto_start DEVICE {
-     if (!(this_proto = cf_dev_proto)) cf_error("Device protocol already defined");
-     cf_dev_proto = NULL;
+dev_proto_start: proto_start DIRECT {
+     struct rt_dev_config *p = proto_config_new(&proto_device, sizeof(struct rt_dev_config));
+     struct iface_patt *k = cfg_alloc(sizeof(struct iface_patt));
+     this_proto = &p->c;
+     p->c.preference = DEF_PREF_DIRECT;
+     init_list(&p->iface_list);
+     k->pattern = "*";
+     add_tail(&p->iface_list, &k->n);
    }
  ;
 
 dev_proto:
-   dev_proto_start '{'
+   dev_proto_start proto_name '{'
  | dev_proto proto_item ';'
  | dev_proto dev_iface_list ';'
  ;
 
 dev_iface_list:
    INTERFACE TEXT {
+     /* FIXME: Beware of obscure semantics. */
      init_list(&((struct rt_dev_config *) this_proto)->iface_list);
      rt_dev_add_iface($2);
    }
index fcaabd8b0db00e84a9b19de3d12320002b3caac0..c122b967b0b7089313bcfeab32dd926680e56ecc 100644 (file)
@@ -18,8 +18,6 @@
 #include "conf/conf.h"
 #include "lib/resource.h"
 
-struct proto_config *cf_dev_proto;
-
 static void
 dev_if_notify(struct proto *p, unsigned c, struct iface *new, struct iface *old)
 {
@@ -77,22 +75,8 @@ dev_init(struct proto_config *c)
   return p;
 }
 
-static void
-dev_preconfig(struct protocol *x, struct config *c)
-{
-  struct rt_dev_config *p = proto_config_new(&proto_device, sizeof(struct rt_dev_config));
-  struct iface_patt *k = cfg_alloc(sizeof(struct iface_patt));
-
-  cf_dev_proto = &p->c;
-  p->c.preference = DEF_PREF_DIRECT;
-  init_list(&p->iface_list);
-  k->pattern = "*";
-  add_tail(&p->iface_list, &k->n);
-}
-
 struct protocol proto_device = {
-  name:                "Device",
-  priority:    100,
-  preconfig:   dev_preconfig,
+  name:                "Direct",
+  priority:    90,
   init:                dev_init,
 };
index 5dfb934f069f50490e264fca508b49944b7c1c3b..53167c86ed2d8b1fe5214f33ec62f8cf3206b423 100644 (file)
@@ -19,8 +19,9 @@ struct krt_if_params {
 struct krt_if_status {
 };
 
-static inline void krt_if_preconfig(struct krt_config *c) { };
-static inline void krt_if_start(struct krt_proto *p) { };
-static inline void krt_if_shutdown(struct krt_proto *p) { };
+static inline void krt_if_preconfig(struct kif_config *c) { };
+static inline void krt_if_start(struct kif_proto *p) { };
+static inline void krt_if_shutdown(struct kif_proto *p) { };
+static inline void krt_if_io_init(void) { };
 
 #endif
index 93eb4ad73e2e811a3389c71d97b03b1bd76157e2..57e18991b1d14423d3e8712e8ec43d20ba4d3c8d 100644 (file)
@@ -392,7 +392,7 @@ nl_parse_addr(struct nlmsghdr *h)
 }
 
 void
-krt_if_scan(struct krt_proto *p)
+krt_if_scan(struct kif_proto *p)
 {
   struct nlmsghdr *h;
 
index 8427ba47f95d902d6672e74599265a34f9f318a1..6e1952e71b1a3300988715f1d25df95d57527901 100644 (file)
@@ -693,6 +693,7 @@ io_init(void)
   init_list(&far_timers);
   init_list(&sock_list);
   init_list(&global_event_list);
+  krt_io_init();
   now = time(NULL);
 }
 
index 3b665a44e80e67a59dd2e814fad7f016724a18c8..630355886be3c20f63d26847df6549751007f1e0 100644 (file)
@@ -131,7 +131,7 @@ scan_ifs(struct ifreq *r, int cnt)
 }
 
 void
-krt_if_scan(struct krt_proto *p)
+krt_if_scan(struct kif_proto *p)
 {
   struct ifconf ic;
   static int last_ifbuf_size = 4*sizeof(struct ifreq);
@@ -172,23 +172,25 @@ krt_if_scan(struct krt_proto *p)
 }
 
 void
-krt_if_preconfig(struct krt_config *c)
+krt_if_preconfig(struct kif_config *c)
 {
 }
 
 void
-krt_if_start(struct krt_proto *p)
+krt_if_start(struct kif_proto *p)
+{
+}
+
+void
+krt_if_shutdown(struct kif_proto *p)
 {
-  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
-krt_if_shutdown(struct krt_proto *p)
+krt_if_io_init(void)
 {
+  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");
 }
index 661e5052f7569a3b2a2a23c75b90d6f101951c4d..083df7d60d1fbc7b245293c20b7ab7f0a745bf6a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     BIRD -- UNIX Kernel Syncer Configuration
  *
- *     (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.
  */
@@ -11,24 +11,31 @@ CF_HDR
 #include "lib/krt.h"
 
 #define THIS_KRT ((struct krt_config *) this_proto)
+#define THIS_KIF ((struct kif_config *) this_proto)
 
 CF_DECLS
 
-CF_KEYWORDS(KERNEL, PERSIST, SCAN, TIME, ROUTE, LEARN)
+CF_KEYWORDS(KERNEL, PERSIST, SCAN, TIME, LEARN, DEVICE)
 
 CF_GRAMMAR
 
-/* Kernel protocol */
+/* Kernel syncer protocol */
 
 CF_ADDTO(proto, kern_proto '}')
 
 kern_proto_start: proto_start KERNEL {
-     if (!(this_proto = cf_krt)) cf_error("Kernel protocol already defined");
-     cf_krt = NULL;
+     if (cf_krt)
+       cf_error("Kernel protocol already defined");
+     cf_krt = this_proto = proto_config_new(&proto_unix_kernel, sizeof(struct krt_config));
+     this_proto->preference = 0;
+     THIS_KRT->scan_time = 60;
+     THIS_KRT->learn = THIS_KRT->persist = 0;
+     krt_scan_preconfig(THIS_KRT);
+     krt_set_preconfig(THIS_KRT);
    }
  ;
 
-CF_ADDTO(kern_proto, kern_proto_start '{')
+CF_ADDTO(kern_proto, kern_proto_start proto_name '{')
 CF_ADDTO(kern_proto, kern_proto proto_item ';')
 CF_ADDTO(kern_proto, kern_proto kern_item ';')
 
@@ -38,10 +45,34 @@ kern_item:
       /* 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; }
  ;
 
+/* Kernel interface protocol */
+
+CF_ADDTO(proto, kif_proto '}')
+
+kif_proto_start: proto_start DEVICE {
+     if (cf_kif)
+       cf_error("Kernel device protocol already defined");
+     cf_kif = this_proto = proto_config_new(&proto_unix_iface, sizeof(struct kif_config));
+     this_proto->preference = DEF_PREF_DIRECT;
+     THIS_KIF->scan_time = 60;
+     krt_if_preconfig(THIS_KIF);
+   }
+ ;
+
+CF_ADDTO(kif_proto, kif_proto_start '{')
+CF_ADDTO(kif_proto, kif_proto proto_item ';')
+CF_ADDTO(kif_proto, kif_proto kif_item ';')
+
+kif_item:
+   SCAN TIME expr {
+      /* Scan time of 0 means scan on startup only */
+      THIS_KIF->scan_time = $3;
+   }
+ ;
+
 CF_CODE
 
 CF_END
index 9ad4237953167c5893357d3777d0470cb2b3920e..8a3db3f13808a749373a6ee8dedf6e0481fd4344 100644 (file)
 #include "unix.h"
 #include "krt.h"
 
-struct proto_config *cf_krt;
+/*
+ *     Global resources
+ */
+
+void
+krt_io_init(void)
+{
+  krt_if_io_init();
+}
+
+/*
+ *     Interfaces
+ */
+
+struct proto_config *cf_kif;
+
+static struct kif_proto *kif_proto;
+static timer *kif_scan_timer;
+static bird_clock_t kif_last_shot;
+
+static void
+kif_scan(timer *t)
+{
+  struct kif_proto *p = t->data;
+
+  DBG("KIF: It's interface scan time...\n");
+  kif_last_shot = now;
+  krt_if_scan(p);
+}
+
+static void
+kif_force_scan(void)
+{
+  if (kif_proto && kif_last_shot + 2 < now)
+    {
+      kif_scan(kif_scan_timer);
+      tm_start(kif_scan_timer, ((struct kif_config *) kif_proto->p.cf)->scan_time);
+    }
+}
+
+static struct proto *
+kif_init(struct proto_config *c)
+{
+  struct kif_proto *p = proto_new(c, sizeof(struct kif_proto));
+  return &p->p;
+}
+
+static int
+kif_start(struct proto *P)
+{
+  struct kif_proto *p = (struct kif_proto *) P;
+
+  kif_proto = p;
+  krt_if_start(p);
+
+  /* Start periodic interface scanning */
+  kif_scan_timer = tm_new(P->pool);
+  kif_scan_timer->hook = kif_scan;
+  kif_scan_timer->data = p;
+  kif_scan_timer->recurrent = KIF_CF->scan_time;
+  kif_scan(kif_scan_timer);
+  tm_start(kif_scan_timer, KIF_CF->scan_time);
+
+  return PS_UP;
+}
+
+static int
+kif_shutdown(struct proto *P)
+{
+  struct kif_proto *p = (struct kif_proto *) P;
+
+  tm_stop(kif_scan_timer);
+  krt_if_shutdown(p);
+  kif_proto = NULL;
+
+  if_start_update();   /* Remove all interfaces */
+  if_end_update();
+
+  return PS_DOWN;
+}
+
+struct protocol proto_unix_iface = {
+  name:                "Device",
+  priority:    100,
+  init:                kif_init,
+  start:       kif_start,
+  shutdown:    kif_shutdown,
+};
 
 /*
  *     Routes
@@ -210,7 +297,7 @@ krt_got_route_async(struct krt_proto *p, rte *e, int new)
           * FIXME: This is limited to one inherited route per destination as we
           * use single protocol for all inherited routes. Probably leave it
           * as-is (and document it :)), because the correct solution is to
-          * multiple kernel tables anyway.
+          * use multiple kernel tables anyway.
           */
          DBG("Learning\n");
          rte_update(net, &p->p, e);
@@ -234,35 +321,27 @@ 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);
-    }
+  kif_force_scan();
+  DBG("KRT: It's route scan time...\n");
+  krt_scan_fire(p);
+  krt_prune(p);
 }
 
 /*
  *     Protocol glue
  */
 
+struct proto_config *cf_krt;
+
 static int
 krt_start(struct proto *P)
 {
   struct krt_proto *p = (struct krt_proto *) P;
 
-  p->accum_time = KRT_CF->route_scan_time - KRT_CF->scan_time;
-
-  krt_if_start(p);
   krt_scan_start(p);
   krt_set_start(p);
 
-  /* Start periodic interface scanning */
+  /* Start periodic routing table scanning */
   krt_scan_timer = tm_new(P->pool);
   krt_scan_timer->hook = krt_scan;
   krt_scan_timer->data = p;
@@ -278,35 +357,17 @@ krt_shutdown(struct proto *P)
 {
   struct krt_proto *p = (struct krt_proto *) P;
 
+  tm_stop(krt_scan_timer);
+
   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)
 {
@@ -318,8 +379,7 @@ krt_init(struct proto_config *c)
 
 struct protocol proto_unix_kernel = {
   name:                "Kernel",
-  priority:    90,
-  preconfig:   krt_preconfig,
+  priority:    80,
   init:                krt_init,
   start:       krt_start,
   shutdown:    krt_shutdown,
index 02814c683a764f0d6ae7f1eeed579361b6a72591..bde509c1c55c0ecbfddddd386d73c8562b2af187 100644 (file)
@@ -11,6 +11,8 @@
 
 struct krt_config;
 struct krt_proto;
+struct kif_config;
+struct kif_proto;
 
 #include "lib/krt-scan.h"
 #include "lib/krt-set.h"
@@ -34,8 +36,7 @@ struct krt_config {
   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 scan_time;               /* How often we re-scan routes */
   int learn;                   /* Learn routes from other sources */
 };
 
@@ -44,7 +45,6 @@ struct krt_proto {
   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;
@@ -60,6 +60,23 @@ void krt_got_route_async(struct krt_proto *p, struct rte *e, int new);
 #define KRT_SRC_REDIRECT 1     /* Redirect route, delete it */
 #define KRT_SRC_ALIEN   2      /* Route installed by someone else */
 
+extern struct protocol proto_unix_iface;
+
+struct kif_config {
+  struct proto_config c;
+  struct krt_if_params iface;
+  int scan_time;               /* How often we re-scan interfaces */
+};
+
+struct kif_proto {
+  struct proto p;
+  struct krt_if_status iface;
+};
+
+extern struct proto_config *cf_kif;
+
+#define KIF_CF ((struct kif_config *)p->p.cf)
+
 /* krt-scan.c */
 
 void krt_scan_preconfig(struct krt_config *);
@@ -79,10 +96,11 @@ void krt_set_notify(struct proto *x, net *net, rte *new, rte *old);
 
 /* 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_preconfig(struct kif_config *);
+void krt_if_start(struct kif_proto *);
+void krt_if_shutdown(struct kif_proto *);
 
-void krt_if_scan(struct krt_proto *);
+void krt_if_scan(struct kif_proto *);
+void krt_if_io_init(void);
 
 #endif
index 45940fb9e89d80bd2b6e42412fb5b9ce4e3a3555..5f260caa9bd81c544f8fa2f853b8934730b86895 100644 (file)
@@ -205,6 +205,7 @@ main(int argc, char **argv)
 
   protos_build();
   add_tail(&protocol_list, &proto_unix_kernel.n);
+  add_tail(&protocol_list, &proto_unix_iface.n);
 
   read_config();
 
index 7b338714e1cdf1cff2be93468689afc31acbc4c4..2d260744a18d15fddb9479772ff2fbf13d72041e 100644 (file)
@@ -26,4 +26,8 @@ void io_loop(void);
 void fill_in_sockaddr(struct sockaddr_in *sa, ip_addr a, unsigned port);
 void get_sockaddr(struct sockaddr_in *sa, ip_addr *a, unsigned *port);
 
+/* krt.c bits */
+
+void krt_io_init(void);
+
 #endif