Commit 396dfa90 authored by Ondřej Zajíček's avatar Ondřej Zajíček

Cleanup in sysdep KRT code, part 1.

OS-dependent functions renamed to be more consistent,
prepared to merge krt-set and krt-scan headers.

Name changes:

struct krt_if_params -> struct kif_params
struct krt_if_status -> struct kif_status
struct krt_set/scan_params -> struct krt_params
struct krt_set/scan_status -> struct krt_status

krt_if_params_same -> kif_sys_reconfigure
krt_if_copy_params -> kif_sys_copy_config
krt_set/scan_params_same -> krt_sys_reconfigure
krt_set/scan_copy_params -> krt_sys_copy_config

krt_if_scan -> kif_do_scan
krt_set_notify -> krt_do_notify
krt_scan_fire -> krt_do_scan

krt_if_ -> kif_sys_
krt_scan_ -> krt_sys_
krt_set_ -> krt_sys_
parent 182a7895
...@@ -13,13 +13,21 @@ ...@@ -13,13 +13,21 @@
* We don't have split iface/scan/set parts. See krt-sock.h. * We don't have split iface/scan/set parts. See krt-sock.h.
*/ */
struct krt_if_params { struct kif_params {
}; };
struct krt_if_status { struct kif_status {
}; };
static inline int kif_params_same(struct krt_if_params *old UNUSED, struct krt_if_params *new UNUSED) { return 1; }
static inline void kif_copy_params(struct krt_if_params *dest UNUSED, struct krt_if_params *src UNUSED) { } static inline void kif_sys_init(struct kif_proto *p UNUSED) { }
static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; }
static inline void kif_sys_preconfig(struct config *c UNUSED) { }
static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { }
static inline void kif_sys_init_config(struct kif_config *c UNUSED) { }
static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { }
static inline void kif_sys_io_init(void) { }
#endif #endif
...@@ -13,10 +13,18 @@ struct krt_scan_params { ...@@ -13,10 +13,18 @@ struct krt_scan_params {
}; };
struct krt_scan_status { struct krt_scan_status {
list temp_ifs; /* Temporary interfaces */
}; };
static inline int krt_scan_params_same(struct krt_scan_params *o UNUSED, struct krt_scan_params *n UNUSED) { return 1; }
static inline void krt_scan_copy_params(struct krt_scan_params *d UNUSED, struct krt_scan_params *s UNUSED) { } static inline void krt_sys_init(struct krt_proto *p UNUSED) { }
static inline int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n UNUSED, struct krt_config *o UNUSED) { return 1; }
static inline void krt_sys_preconfig(struct config *c UNUSED) { }
static inline void krt_sys_postconfig(struct krt_config *c UNUSED) { }
static inline void krt_sys_init_config(struct krt_config *c UNUSED) { }
static inline void krt_sys_copy_config(struct krt_config *d UNUSED, struct krt_config *s UNUSED) { }
#endif #endif
...@@ -33,7 +33,19 @@ ...@@ -33,7 +33,19 @@
#include "lib/string.h" #include "lib/string.h"
#include "lib/socket.h" #include "lib/socket.h"
int rt_sock = 0;
#ifndef RTAX_MAX
#define RTAX_MAX 8
#endif
struct ks_msg
{
struct rt_msghdr rtm;
struct sockaddr_storage buf[RTAX_MAX];
};
static int rt_sock = 0;
int int
krt_capable(rte *e) krt_capable(rte *e)
...@@ -189,8 +201,8 @@ krt_sock_send(int cmd, rte *e) ...@@ -189,8 +201,8 @@ krt_sock_send(int cmd, rte *e)
} }
void void
krt_set_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, krt_do_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old,
struct ea_list *eattrs UNUSED) struct ea_list *eattrs UNUSED)
{ {
int err = 0; int err = 0;
...@@ -206,45 +218,6 @@ krt_set_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, ...@@ -206,45 +218,6 @@ krt_set_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old,
n->n.flags &= ~KRF_SYNC_ERROR; n->n.flags &= ~KRF_SYNC_ERROR;
} }
static int
krt_set_hook(sock *sk, int size UNUSED)
{
struct ks_msg msg;
int l = read(sk->fd, (char *)&msg, sizeof(msg));
if(l <= 0)
log(L_ERR "krt-sock: read failed");
else
krt_read_msg((struct proto *)sk->data, &msg, 0);
return 0;
}
void
krt_set_start(struct krt_proto *x, int first UNUSED)
{
sock *sk_rt;
static int ks_open_tried = 0;
if (ks_open_tried)
return;
ks_open_tried = 1;
DBG("KRT: Opening kernel socket\n");
if( (rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0)
die("Cannot open kernel socket for routes");
sk_rt = sk_new(krt_pool);
sk_rt->type = SK_MAGIC;
sk_rt->rx_hook = krt_set_hook;
sk_rt->fd = rt_sock;
sk_rt->data = x;
if (sk_open(sk_rt))
bug("krt-sock: sk_open failed");
}
#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0) #define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0)
static void static void
...@@ -648,32 +621,6 @@ krt_read_msg(struct proto *p, struct ks_msg *msg, int scan) ...@@ -648,32 +621,6 @@ krt_read_msg(struct proto *p, struct ks_msg *msg, int scan)
} }
} }
void
krt_scan_construct(struct krt_config *c UNUSED)
{
}
void
krt_scan_preconfig(struct config *c UNUSED)
{
}
void
krt_scan_postconfig(struct krt_config *c UNUSED)
{
}
void
krt_scan_start(struct krt_proto *x, int first UNUSED)
{
init_list(&x->scan.temp_ifs);
}
void
krt_scan_shutdown(struct krt_proto *x UNUSED, int last UNUSED)
{
}
static void static void
krt_sysctl_scan(struct proto *p, pool *pool, byte **buf, size_t *bl, int cmd) krt_sysctl_scan(struct proto *p, pool *pool, byte **buf, size_t *bl, int cmd)
{ {
...@@ -732,13 +679,13 @@ static size_t krt_buflen = 32768; ...@@ -732,13 +679,13 @@ static size_t krt_buflen = 32768;
static size_t kif_buflen = 4096; static size_t kif_buflen = 4096;
void void
krt_scan_fire(struct krt_proto *p) krt_do_scan(struct krt_proto *p)
{ {
krt_sysctl_scan((struct proto *)p, p->krt_pool, &krt_buffer, &krt_buflen, NET_RT_DUMP); krt_sysctl_scan((struct proto *)p, p->krt_pool, &krt_buffer, &krt_buflen, NET_RT_DUMP);
} }
void void
krt_if_scan(struct kif_proto *p) kif_do_scan(struct kif_proto *p)
{ {
struct proto *P = (struct proto *)p; struct proto *P = (struct proto *)p;
if_start_update(); if_start_update();
...@@ -746,14 +693,47 @@ krt_if_scan(struct kif_proto *p) ...@@ -746,14 +693,47 @@ krt_if_scan(struct kif_proto *p)
if_end_update(); if_end_update();
} }
static int
krt_sock_hook(sock *sk, int size UNUSED)
{
struct ks_msg msg;
int l = read(sk->fd, (char *)&msg, sizeof(msg));
if(l <= 0)
log(L_ERR "krt-sock: read failed");
else
krt_read_msg((struct proto *)sk->data, &msg, 0);
return 0;
}
void void
krt_set_construct(struct krt_config *c UNUSED) krt_sys_start(struct krt_proto *x, int first UNUSED)
{ {
sock *sk_rt;
static int ks_open_tried = 0;
if (ks_open_tried)
return;
ks_open_tried = 1;
DBG("KRT: Opening kernel socket\n");
if( (rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0)
die("Cannot open kernel socket for routes");
sk_rt = sk_new(krt_pool);
sk_rt->type = SK_MAGIC;
sk_rt->rx_hook = krt_sock_hook;
sk_rt->fd = rt_sock;
sk_rt->data = x;
if (sk_open(sk_rt))
bug("krt-sock: sk_open failed");
} }
void void
krt_set_shutdown(struct krt_proto *x UNUSED, int last UNUSED) krt_sys_shutdown(struct krt_proto *x UNUSED, int last UNUSED)
{ {
if (!krt_buffer) if (!krt_buffer)
return; return;
...@@ -762,23 +742,14 @@ krt_set_shutdown(struct krt_proto *x UNUSED, int last UNUSED) ...@@ -762,23 +742,14 @@ krt_set_shutdown(struct krt_proto *x UNUSED, int last UNUSED)
krt_buffer = NULL; krt_buffer = NULL;
} }
void
krt_if_io_init(void)
{
}
void
krt_if_construct(struct kif_config *c UNUSED)
{
}
void void
krt_if_start(struct kif_proto *p UNUSED) kif_sys_start(struct kif_proto *p UNUSED)
{ {
} }
void void
krt_if_shutdown(struct kif_proto *p UNUSED) kif_sys_shutdown(struct kif_proto *p UNUSED)
{ {
if (!kif_buffer) if (!kif_buffer)
return; return;
......
...@@ -9,29 +9,10 @@ ...@@ -9,29 +9,10 @@
#ifndef _BIRD_KRT_SOCK_H_ #ifndef _BIRD_KRT_SOCK_H_
#define _BIRD_KRT_SOCK_H_ #define _BIRD_KRT_SOCK_H_
#include <sys/socket.h> struct krt_params {
#include <net/route.h>
#include "lib/socket.h"
#ifndef RTAX_MAX
#define RTAX_MAX 8
#endif
struct ks_msg
{
struct rt_msghdr rtm;
struct sockaddr_storage buf[RTAX_MAX];
};
extern int krt_set_sock;
struct krt_set_params {
}; };
struct krt_set_status { struct krt_status {
}; };
static inline int krt_set_params_same(struct krt_set_params *o UNUSED, struct krt_set_params *n UNUSED) { return 1; } static inline int krt_set_params_same(struct krt_set_params *o UNUSED, struct krt_set_params *n UNUSED) { return 1; }
......
...@@ -13,17 +13,21 @@ ...@@ -13,17 +13,21 @@
* We don't have split iface/scan/set parts. See krt-scan.h. * We don't have split iface/scan/set parts. See krt-scan.h.
*/ */
struct krt_if_params { struct kif_params {
}; };
struct krt_if_status { struct kif_status {
}; };
static inline void krt_if_construct(struct kif_config *c UNUSED) { };
static inline void krt_if_shutdown(struct kif_proto *p UNUSED) { };
static inline void krt_if_io_init(void) { };
static inline int kif_params_same(struct krt_if_params *old UNUSED, struct krt_if_params *new UNUSED) { return 1; } static inline void kif_sys_init(struct kif_proto *p UNUSED) { }
static inline void kif_copy_params(struct krt_if_params *dest UNUSED, struct krt_if_params *src UNUSED) { } static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; }
static inline void kif_sys_preconfig(struct config *c UNUSED) { }
static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { }
static inline void kif_sys_init_config(struct kif_config *c UNUSED) { }
static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { }
static inline void kif_sys_io_init(void) { }
#endif #endif
...@@ -17,20 +17,14 @@ ...@@ -17,20 +17,14 @@
#define NL_NUM_TABLES 256 #define NL_NUM_TABLES 256
struct krt_scan_params { struct krt_params {
int table_id; /* Kernel table ID we sync with */ int table_id; /* Kernel table ID we sync with */
}; };
struct krt_scan_status { struct krt_status {
list temp_ifs; /* Temporary interfaces */
}; };
static inline int krt_scan_params_same(struct krt_scan_params *o, struct krt_scan_params *n)
{
return o->table_id == n->table_id;
}
static inline void krt_scan_copy_params(struct krt_scan_params *d UNUSED, struct krt_scan_params *s UNUSED) { } static inline void krt_sys_init(struct krt_proto *p UNUSED) { }
/* table_id copied in krt_copy_config() */
#endif #endif
...@@ -20,7 +20,7 @@ nl_item: ...@@ -20,7 +20,7 @@ nl_item:
KERNEL TABLE expr { KERNEL TABLE expr {
if ($3 <= 0 || $3 >= NL_NUM_TABLES) if ($3 <= 0 || $3 >= NL_NUM_TABLES)
cf_error("Kernel routing table number out of range"); cf_error("Kernel routing table number out of range");
THIS_KRT->scan.table_id = $3; THIS_KRT->sys.table_id = $3;
} }
; ;
......
...@@ -548,7 +548,7 @@ nl_parse_addr(struct nlmsghdr *h) ...@@ -548,7 +548,7 @@ nl_parse_addr(struct nlmsghdr *h)
} }
void void
krt_if_scan(struct kif_proto *p UNUSED) kif_do_scan(struct kif_proto *p UNUSED)
{ {
struct nlmsghdr *h; struct nlmsghdr *h;
...@@ -634,7 +634,7 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new) ...@@ -634,7 +634,7 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new)
r.r.rtm_family = BIRD_AF; r.r.rtm_family = BIRD_AF;
r.r.rtm_dst_len = net->n.pxlen; r.r.rtm_dst_len = net->n.pxlen;
r.r.rtm_tos = 0; r.r.rtm_tos = 0;
r.r.rtm_table = KRT_CF->scan.table_id; r.r.rtm_table = KRT_CF->sys.table_id;
r.r.rtm_protocol = RTPROT_BIRD; r.r.rtm_protocol = RTPROT_BIRD;
r.r.rtm_scope = RT_SCOPE_UNIVERSE; r.r.rtm_scope = RT_SCOPE_UNIVERSE;
nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix); nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix);
...@@ -687,7 +687,7 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new) ...@@ -687,7 +687,7 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new)
} }
void void
krt_set_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs) krt_do_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs)
{ {
int err = 0; int err = 0;
...@@ -940,7 +940,7 @@ nl_parse_route(struct nlmsghdr *h, int scan) ...@@ -940,7 +940,7 @@ nl_parse_route(struct nlmsghdr *h, int scan)
} }
void void
krt_scan_fire(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */ krt_do_scan(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */
{ {
struct nlmsghdr *h; struct nlmsghdr *h;
...@@ -1084,15 +1084,38 @@ nl_open_async(void) ...@@ -1084,15 +1084,38 @@ nl_open_async(void)
static u8 nl_cf_table[(NL_NUM_TABLES+7) / 8]; static u8 nl_cf_table[(NL_NUM_TABLES+7) / 8];
void void
krt_scan_preconfig(struct config *c UNUSED) krt_sys_start(struct krt_proto *p, int first)
{
nl_table_map[KRT_CF->sys.table_id] = p;
if (first)
{
nl_open();
nl_open_async();
}
}
void
krt_sys_shutdown(struct krt_proto *p UNUSED, int last UNUSED)
{
}
int
krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o)
{
return n->sys.table_id == o->sys.table_id;
}
void
krt_sys_preconfig(struct config *c UNUSED)
{ {
bzero(&nl_cf_table, sizeof(nl_cf_table)); bzero(&nl_cf_table, sizeof(nl_cf_table));
} }
void void
krt_scan_postconfig(struct krt_config *x) krt_sys_postconfig(struct krt_config *x)
{ {
int id = x->scan.table_id; int id = x->sys.table_id;
if (nl_cf_table[id/8] & (1 << (id%8))) if (nl_cf_table[id/8] & (1 << (id%8)))
cf_error("Multiple kernel syncers defined for table #%d", id); cf_error("Multiple kernel syncers defined for table #%d", id);
...@@ -1100,35 +1123,27 @@ krt_scan_postconfig(struct krt_config *x) ...@@ -1100,35 +1123,27 @@ krt_scan_postconfig(struct krt_config *x)
} }
void void
krt_scan_construct(struct krt_config *x) krt_sys_init_config(struct krt_config *cf)
{ {
#ifndef IPV6 cf->sys.table_id = RT_TABLE_MAIN;
x->scan.table_id = RT_TABLE_MAIN;
#else
x->scan.table_id = 254;
#endif
} }
void void
krt_scan_start(struct krt_proto *p, int first) krt_sys_copy_config(struct krt_config *d, struct krt_config *s)
{ {
init_list(&p->scan.temp_ifs); d->sys.table_id = s->sys.table_id;
nl_table_map[KRT_CF->scan.table_id] = p;
if (first)
{
nl_open();
nl_open_async();
}
} }
void void
krt_scan_shutdown(struct krt_proto *p UNUSED, int last UNUSED) kif_sys_start(struct kif_proto *p UNUSED)
{ {
nl_open();
nl_open_async();
} }
void void
krt_if_start(struct kif_proto *p UNUSED) kif_sys_shutdown(struct kif_proto *p UNUSED)
{ {
nl_open();
nl_open_async();
} }
...@@ -186,7 +186,7 @@ scan_ifs(struct ifreq *r, int cnt) ...@@ -186,7 +186,7 @@ scan_ifs(struct ifreq *r, int cnt)
} }
void void
krt_if_scan(struct kif_proto *p) kif_do_scan(struct kif_proto *p)
{ {
struct ifconf ic; struct ifconf ic;
static int last_ifbuf_size = 4*sizeof(struct ifreq); static int last_ifbuf_size = 4*sizeof(struct ifreq);
...@@ -208,22 +208,17 @@ krt_if_scan(struct kif_proto *p) ...@@ -208,22 +208,17 @@ krt_if_scan(struct kif_proto *p)
} }
void void
krt_if_construct(struct kif_config *c) kif_start(struct kif_proto *p)
{ {
} }
void void
krt_if_start(struct kif_proto *p) kif_shutdown(struct kif_proto *p)
{ {
} }
void void
krt_if_shutdown(struct kif_proto *p) kif_io_init(void)
{
}
void
krt_if_io_init(void)
{ {
if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
DBG("Using socket %d for interface and route scanning\n", if_scan_sock); DBG("Using socket %d for interface and route scanning\n", if_scan_sock);
......
...@@ -9,15 +9,21 @@ ...@@ -9,15 +9,21 @@
#ifndef _BIRD_KRT_IFACE_H_ #ifndef _BIRD_KRT_IFACE_H_
#define _BIRD_KRT_IFACE_H_ #define _BIRD_KRT_IFACE_H_
struct krt_if_params { struct kif_params {
}; };
struct krt_if_status { struct kif_status {
}; };
extern int if_scan_sock; extern int if_scan_sock;
static inline int kif_params_same(struct krt_if_params *old UNUSED, struct krt_if_params *new UNUSED) { return 1; }
static inline void kif_copy_params(struct krt_if_params *dest UNUSED, struct krt_if_params *src UNUSED) { } static inline void kif_sys_init(struct kif_proto *p UNUSED) { }
static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; }
static inline void kif_sys_preconfig(struct config *c UNUSED) { }
static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { }
static inline void kif_sys_init_config(struct kif_config *c UNUSED) { }
static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { }
#endif #endif
...@@ -79,7 +79,7 @@ krt_ioctl(int ioc, rte *e, char *name) ...@@ -79,7 +79,7 @@ krt_ioctl(int ioc, rte *e, char *name)
} }
void void
krt_set_notify(struct krt_proto *p, net *net, rte *new, rte *old) krt_do_notify(struct krt_proto *p, net *net, rte *new, rte *old)
{ {
if (old) if (old)
{ {
...@@ -94,18 +94,19 @@ krt_set_notify(struct krt_proto *p, net *net, rte *new, rte *old) ...@@ -94,18 +94,19 @@ krt_set_notify(struct krt_proto *p, net *net, rte *new, rte *old)
} }
void void
krt_set_start(struct krt_proto *x, int first) krt_sys_start(struct krt_proto *x, int first)
{ {
if (if_scan_sock < 0) if (if_scan_sock < 0)
bug("krt set: missing socket"); bug("krt set: missing socket");
} }
void void
krt_set_construct(struct krt_config *c) krt_sys_shutdown(struct krt_proto *x, int last)
{ {
} }
void void
krt_set_shutdown(struct krt_proto *x, int last) krt_sys_construct(struct krt_config *c)
{ {
} }
/* /*
* BIRD -- Unix Kernel Route Syncer -- Setting * BIRD -- Unix Kernel Route Syncer
* *
* (c) 1998--2000 Martin Mares <mj@ucw.cz> * (c) 1998--2000 Martin Mares <mj@ucw.cz>
* *
...@@ -9,13 +9,19 @@ ...@@ -9,13 +9,19 @@
#ifndef _BIRD_KRT_SET_H_ #ifndef _BIRD_KRT_SET_H_
#define _BIRD_KRT_SET_H_ #define _BIRD_KRT_SET_H_
struct krt_set_params { struct krt_params {
}; };
struct krt_set_status { struct krt_status {
}; };