From 4334f86251429eb39bfe81ff19496d141fccef84 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Mon, 15 Nov 2021 10:53:58 +0100 Subject: VRF setting reduced to one argument, using default dummy iface for default vrf --- proto/rip/rip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'proto/rip') diff --git a/proto/rip/rip.c b/proto/rip/rip.c index 8c2d5aeb..ca218846 100644 --- a/proto/rip/rip.c +++ b/proto/rip/rip.c @@ -797,7 +797,7 @@ rip_reconfigure_ifaces(struct rip_proto *p, struct rip_config *cf) WALK_LIST(iface, iface_list) { - if (p->p.vrf_set && p->p.vrf != iface->master) + if (p->p.vrf && p->p.vrf != iface->master) continue; if (!(iface->flags & IF_UP)) -- cgit v1.2.3 From 05d8c3699d51866c68747167556e7c1f06390afe Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 24 Jan 2023 11:01:34 +0100 Subject: Object locks use events Instead of calling custom hooks from object locks, we use standard event sending mechanism to inform protocols about object lock changes. This is a backport from version 3 where these events are passed across threads. This implementation of object locks doesn't use mutexes to lock the whole data structure. In version 3, this data structure may get accessed from multiple threads and must be protected by mutex. --- nest/locks.c | 59 +++++++++++++++++++++++------------------------------ nest/locks.h | 4 +--- proto/babel/babel.c | 10 +++++---- proto/bgp/bgp.c | 10 +++++---- proto/ospf/iface.c | 10 +++++---- proto/radv/radv.c | 10 +++++---- proto/rip/rip.c | 10 +++++---- 7 files changed, 57 insertions(+), 56 deletions(-) (limited to 'proto/rip') diff --git a/nest/locks.c b/nest/locks.c index 812a6534..e378fb1f 100644 --- a/nest/locks.c +++ b/nest/locks.c @@ -37,7 +37,6 @@ #include "nest/iface.h" static list olock_list; -static event *olock_event; static inline int olock_same(struct object_lock *x, struct object_lock *y) @@ -54,7 +53,8 @@ olock_same(struct object_lock *x, struct object_lock *y) static void olock_free(resource *r) { - struct object_lock *q, *l = (struct object_lock *) r; + /* Called externally from rfree() */ + struct object_lock *l = SKIP_BACK(struct object_lock, r, r); node *n; DBG("olock: Freeing %p\n", l); @@ -63,21 +63,35 @@ olock_free(resource *r) case OLOCK_STATE_FREE: break; case OLOCK_STATE_LOCKED: - case OLOCK_STATE_EVENT: + /* Remove myself from the olock_list */ rem_node(&l->n); + + /* Maybe the notification is still pending. */ + ev_postpone(&l->event); + + /* Get new lock candidate */ n = HEAD(l->waiters); - if (n->next) + if (NODE_VALID(n)) { - DBG("olock: -> %p becomes locked\n", n); - q = SKIP_BACK(struct object_lock, n, n); + struct object_lock *q = SKIP_BACK(struct object_lock, n, n); + + /* Remove this candidate from waiters list */ rem_node(n); + + /* Move waiter lists */ + DBG("olock: -> %p becomes locked\n", n); add_tail_list(&q->waiters, &l->waiters); - q->state = OLOCK_STATE_EVENT; + + /* Add the new olock to olock_list */ add_head(&olock_list, n); - ev_schedule(olock_event); + + /* Inform */ + q->state = OLOCK_STATE_LOCKED; + ev_schedule(&q->event); } break; case OLOCK_STATE_WAITING: + /* Remove from the waiters list */ rem_node(&l->n); break; default: @@ -148,36 +162,16 @@ olock_acquire(struct object_lock *l) l->state = OLOCK_STATE_WAITING; add_tail(&q->waiters, &l->n); DBG("olock: %p waits\n", l); + return; } } + DBG("olock: %p acquired immediately\n", l); - l->state = OLOCK_STATE_EVENT; add_head(&olock_list, &l->n); - ev_schedule(olock_event); -} -static void -olock_run_event(void *unused UNUSED) -{ - node *n; - struct object_lock *q; - - DBG("olock: Processing events\n"); - for(;;) - { - n = HEAD(olock_list); - if (!n->next) - break; - q = SKIP_BACK(struct object_lock, n, n); - if (q->state != OLOCK_STATE_EVENT) - break; - DBG("olock: %p locked\n", q); - q->state = OLOCK_STATE_LOCKED; - rem_node(&q->n); - add_tail(&olock_list, &q->n); - q->hook(q); - } + l->state = OLOCK_STATE_LOCKED; + ev_schedule(&l->event); } /** @@ -191,5 +185,4 @@ olock_init(void) { DBG("olock: init\n"); init_list(&olock_list); - olock_event = ev_new_init(&root_pool, olock_run_event, NULL); } diff --git a/nest/locks.h b/nest/locks.h index 37026c68..0cb33db9 100644 --- a/nest/locks.h +++ b/nest/locks.h @@ -31,8 +31,7 @@ struct object_lock { uint inst; /* ... instance ID */ struct iface *iface; /* ... interface */ struct iface *vrf; /* ... or VRF (if iface is unknown) */ - void (*hook)(struct object_lock *); /* Called when the lock succeeds */ - void *data; /* User data */ + event event; /* Enqueued when the lock succeeds */ /* ... internal to lock manager, don't touch ... */ node n; /* Node in list of olocks */ int state; /* OLOCK_STATE_xxx */ @@ -50,6 +49,5 @@ void olock_init(void); #define OLOCK_STATE_FREE 0 #define OLOCK_STATE_LOCKED 1 #define OLOCK_STATE_WAITING 2 -#define OLOCK_STATE_EVENT 3 /* waiting for unlock processing */ #endif diff --git a/proto/babel/babel.c b/proto/babel/babel.c index ad7981f6..e1b31e86 100644 --- a/proto/babel/babel.c +++ b/proto/babel/babel.c @@ -1762,9 +1762,9 @@ babel_find_iface(struct babel_proto *p, struct iface *what) } static void -babel_iface_locked(struct object_lock *lock) +babel_iface_locked(void *_ifa) { - struct babel_iface *ifa = lock->data; + struct babel_iface *ifa = _ifa; struct babel_proto *p = ifa->proto; if (!babel_open_socket(ifa)) @@ -1819,8 +1819,10 @@ babel_add_iface(struct babel_proto *p, struct iface *new, struct babel_iface_con lock->addr = IP6_BABEL_ROUTERS; lock->port = ifa->cf->port; lock->iface = ifa->iface; - lock->hook = babel_iface_locked; - lock->data = ifa; + lock->event = (event) { + .hook = babel_iface_locked, + .data = ifa, + }; olock_acquire(lock); } diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index 2e442e16..5b0569ae 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -1467,9 +1467,9 @@ bgp_feed_end(struct channel *C) static void -bgp_start_locked(struct object_lock *lock) +bgp_start_locked(void *_p) { - struct bgp_proto *p = lock->data; + struct bgp_proto *p = _p; const struct bgp_config *cf = p->cf; if (p->p.proto_state != PS_START) @@ -1574,8 +1574,10 @@ bgp_start(struct proto *P) lock->iface = p->cf->iface; lock->vrf = p->cf->iface ? NULL : p->p.vrf; lock->type = OBJLOCK_TCP; - lock->hook = bgp_start_locked; - lock->data = p; + lock->event = (event) { + .hook = bgp_start_locked, + .data = p, + }; /* For dynamic BGP, we use inst 1 to avoid collisions with regular BGP */ if (bgp_is_dynamic(p)) diff --git a/proto/ospf/iface.c b/proto/ospf/iface.c index 84c53aa1..c7a6d3d4 100644 --- a/proto/ospf/iface.c +++ b/proto/ospf/iface.c @@ -484,9 +484,9 @@ ospf_iface_find(struct ospf_proto *p, struct iface *what) } static void -ospf_iface_add(struct object_lock *lock) +ospf_iface_add(void *_ifa) { - struct ospf_iface *ifa = lock->data; + struct ospf_iface *ifa = _ifa; struct ospf_proto *p = ifa->oa->po; /* Open socket if interface is not stub */ @@ -668,8 +668,10 @@ ospf_iface_new(struct ospf_area *oa, struct ifa *addr, struct ospf_iface_patt *i lock->port = OSPF_PROTO; lock->inst = ifa->instance_id; lock->iface = iface; - lock->data = ifa; - lock->hook = ospf_iface_add; + lock->event = (event) { + .hook = ospf_iface_add, + .data = ifa, + }; olock_acquire(lock); } diff --git a/proto/radv/radv.c b/proto/radv/radv.c index 8b547f6d..ee1da36c 100644 --- a/proto/radv/radv.c +++ b/proto/radv/radv.c @@ -263,9 +263,9 @@ radv_iface_find(struct radv_proto *p, struct iface *what) } static void -radv_iface_add(struct object_lock *lock) +radv_iface_add(void *_ifa) { - struct radv_iface *ifa = lock->data; + struct radv_iface *ifa = _ifa; struct radv_proto *p = ifa->ra; if (! radv_sk_open(ifa)) @@ -302,8 +302,10 @@ radv_iface_new(struct radv_proto *p, struct iface *iface, struct radv_iface_conf lock->type = OBJLOCK_IP; lock->port = ICMPV6_PROTO; lock->iface = iface; - lock->data = ifa; - lock->hook = radv_iface_add; + lock->event = (event) { + .hook = radv_iface_add, + .data = ifa, + }; ifa->lock = lock; olock_acquire(lock); diff --git a/proto/rip/rip.c b/proto/rip/rip.c index ca218846..93b0d528 100644 --- a/proto/rip/rip.c +++ b/proto/rip/rip.c @@ -656,9 +656,9 @@ rip_iface_update_bfd(struct rip_iface *ifa) static void -rip_iface_locked(struct object_lock *lock) +rip_iface_locked(void *_ifa) { - struct rip_iface *ifa = lock->data; + struct rip_iface *ifa = _ifa; struct rip_proto *p = ifa->rip; if (!rip_open_socket(ifa)) @@ -720,8 +720,10 @@ rip_add_iface(struct rip_proto *p, struct iface *iface, struct rip_iface_config lock->type = OBJLOCK_UDP; lock->port = ic->port; lock->iface = iface; - lock->data = ifa; - lock->hook = rip_iface_locked; + lock->event = (event) { + .hook = rip_iface_locked, + .data = ifa, + }; ifa->lock = lock; olock_acquire(lock); -- cgit v1.2.3 From c354e8f4c199ca7dec441394156d18badac71b81 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 31 Jan 2023 13:07:46 +0100 Subject: Interface updates are asynchronous Instead of propagating interface updates as they are loaded from kernel, they are enqueued and all the notifications are called from a protocol-specific event. This change allows to break the locking loop between protocols and interfaces. Anyway, this change is based on v2 branch to keep the changes between v2 and v3 smaller. --- nest/iface.c | 203 ++++++++++++++++++++++++++++++++++++++++++++------ nest/iface.h | 66 +++++++++++++++- nest/neighbor.c | 63 +++++++++++----- nest/proto.c | 14 +++- nest/protocol.h | 4 +- nest/rt-dev.c | 4 +- proto/babel/babel.c | 2 +- proto/bfd/bfd.c | 2 +- proto/bgp/bgp.c | 2 +- proto/ospf/ospf.c | 4 +- proto/perf/perf.c | 2 +- proto/radv/radv.c | 4 +- proto/rip/rip.c | 4 +- proto/static/static.c | 2 +- sysdep/unix/krt.c | 2 +- 15 files changed, 315 insertions(+), 63 deletions(-) (limited to 'proto/rip') diff --git a/nest/iface.c b/nest/iface.c index fc63dc75..740c1878 100644 --- a/nest/iface.c +++ b/nest/iface.c @@ -34,6 +34,9 @@ #include "conf/conf.h" #include "sysdep/unix/krt.h" + +static TLIST_LIST(ifsub) iface_sub_list; +static slab *iface_sub_slab; static pool *if_pool; list iface_list; @@ -140,13 +143,51 @@ if_copy(struct iface *to, struct iface *from) to->flags = from->flags | (to->flags & IF_TMP_DOWN); to->mtu = from->mtu; to->master_index = from->master_index; - to->master = from->master; + + if_unlink(to->master); + if_link(to->master = from->master); +} + +void +if_enqueue_notify_to(struct iface_notification x, struct iface_subscription *s) +{ + switch (x.type) { + case IFNOT_ADDRESS: + if (!s->ifa_notify) return; + ifa_link(x.a); + break; + case IFNOT_INTERFACE: + if (!s->if_notify) return; + if_link(x.i); + break; + case IFNOT_NEIGHBOR: + if (!s->neigh_notify) return; + neigh_link(x.n); + break; + default: + bug("Unknown interface notification type: %d", x.type); + } + + struct iface_notification *in = sl_alloc(iface_sub_slab); + *in = x; + + ifnot_add_tail(&s->queue, in); + ev_schedule(&s->event); +} + +void +if_enqueue_notify(struct iface_notification x) +{ + WALK_TLIST(ifsub, s, &iface_sub_list) + if_enqueue_notify_to(x, s); } static inline void -ifa_send_notify(struct proto *p, unsigned c, struct ifa *a) +ifa_send_notify(struct iface_subscription *s, unsigned c, struct ifa *a) { - if (p->ifa_notify && + struct proto *p = SKIP_BACK(struct proto, iface_sub, s); + + if (s->ifa_notify && (p->proto_state != PS_DOWN) && (!p->vrf || p->vrf == a->iface->master)) { @@ -154,19 +195,21 @@ ifa_send_notify(struct proto *p, unsigned c, struct ifa *a) log(L_TRACE "%s < address %N on interface %s %s", p->name, &a->prefix, a->iface->name, (c & IF_CHANGE_UP) ? "added" : "removed"); - p->ifa_notify(p, c, a); + s->ifa_notify(p, c, a); } } static void ifa_notify_change_(unsigned c, struct ifa *a) { - struct proto *p; - DBG("IFA change notification (%x) for %s:%I\n", c, a->iface->name, a->ip); - WALK_LIST(p, proto_list) - ifa_send_notify(p, c, a); + if_enqueue_notify((struct iface_notification) { + .type = IFNOT_ADDRESS, + .a = a, + .flags = c, + }); + } static inline void @@ -182,9 +225,11 @@ ifa_notify_change(unsigned c, struct ifa *a) } static inline void -if_send_notify(struct proto *p, unsigned c, struct iface *i) +if_send_notify(struct iface_subscription *s, unsigned c, struct iface *i) { - if (p->if_notify && + struct proto *p = SKIP_BACK(struct proto, iface_sub, s); + + if (s->if_notify && (p->proto_state != PS_DOWN) && (!p->vrf || p->vrf == i->master)) { @@ -197,14 +242,13 @@ if_send_notify(struct proto *p, unsigned c, struct iface *i) (c & IF_CHANGE_PREFERRED) ? "changes preferred address" : (c & IF_CHANGE_CREATE) ? "created" : "sends unknown event"); - p->if_notify(p, c, i); + s->if_notify(p, c, i); } } static void if_notify_change(unsigned c, struct iface *i) { - struct proto *p; struct ifa *a; if (i->flags & IF_JUST_CREATED) @@ -225,8 +269,11 @@ if_notify_change(unsigned c, struct iface *i) WALK_LIST(a, i->addrs) ifa_notify_change_(IF_CHANGE_DOWN, a); - WALK_LIST(p, proto_list) - if_send_notify(p, c, i); + if_enqueue_notify((struct iface_notification) { + .type = IFNOT_INTERFACE, + .i = i, + .flags = c, + }); if (c & IF_CHANGE_UP) WALK_LIST(a, i->addrs) @@ -320,6 +367,7 @@ if_update(struct iface *new) new->llv6 = i->llv6; new->sysdep = i->sysdep; memcpy(&new->addrs, &i->addrs, sizeof(i->addrs)); + memcpy(&new->neighbors, &i->neighbors, sizeof(i->neighbors)); memcpy(i, new, sizeof(*i)); i->flags &= ~IF_UP; /* IF_TMP_DOWN will be added later */ goto newif; @@ -334,9 +382,10 @@ if_update(struct iface *new) } i = mb_alloc(if_pool, sizeof(struct iface)); memcpy(i, new, sizeof(*i)); + if_link(i->master); init_list(&i->addrs); -newif: init_list(&i->neighbors); +newif: i->flags |= IF_UPDATED | IF_TMP_DOWN; /* Tmp down as we don't have addresses yet */ add_tail(&iface_list, &i->n); return i; @@ -386,31 +435,116 @@ if_end_update(void) } } +void +if_link(struct iface *i) +{ + if (i) + i->uc++; +} + +void +if_unlink(struct iface *i) +{ + if (i) + i->uc--; + /* TODO: Do some interface object cleanup */ +} + +static void +iface_notify_hook(void *_s) +{ + struct iface_subscription *s = _s; + + while (!EMPTY_TLIST(ifnot, &s->queue)) + { + struct iface_notification *n = THEAD(ifnot, &s->queue); + switch (n->type) { + case IFNOT_ADDRESS: + ifa_send_notify(s, n->flags, n->a); + ifa_unlink(n->a); + break; + case IFNOT_INTERFACE: + if_send_notify(s, n->flags, n->i); + if_unlink(n->i); + break; + case IFNOT_NEIGHBOR: + s->neigh_notify(n->n); + neigh_unlink(n->n); + break; + default: + bug("Bad interface notification type: %d", n->type); + } + + ifnot_rem_node(&s->queue, n); + sl_free(n); + } +} + + /** - * if_feed_baby - advertise interfaces to a new protocol - * @p: protocol to feed + * iface_subscribe - request interface updates + * @s: subscription structure * * When a new protocol starts, this function sends it a series * of notifications about all existing interfaces. */ void -if_feed_baby(struct proto *p) +iface_subscribe(struct iface_subscription *s) { - struct iface *i; - struct ifa *a; + ifsub_add_tail(&iface_sub_list, s); + s->event = (event) { + .hook = iface_notify_hook, + .data = s, + }; - if (!p->if_notify && !p->ifa_notify) /* shortcut */ + if (!s->if_notify && !s->ifa_notify) /* shortcut */ return; + + struct iface *i; DBG("Announcing interfaces to new protocol %s\n", p->name); WALK_LIST(i, iface_list) { - if_send_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i); + if_send_notify(s, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i); + + struct ifa *a; if (i->flags & IF_UP) WALK_LIST(a, i->addrs) - ifa_send_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a); + ifa_send_notify(s, IF_CHANGE_CREATE | IF_CHANGE_UP, a); } } +/** + * iface_unsubscribe - unsubscribe from interface updates + * @s: subscription structure + */ +void +iface_unsubscribe(struct iface_subscription *s) +{ + ifsub_rem_node(&iface_sub_list, s); + ev_postpone(&s->event); + + WALK_TLIST_DELSAFE(ifnot, n, &s->queue) + { + switch (n->type) + { + case IFNOT_ADDRESS: + ifa_unlink(n->a); + break; + case IFNOT_INTERFACE: + if_unlink(n->i); + break; + case IFNOT_NEIGHBOR: + neigh_unlink(n->n); + break; + default: + bug("Bad interface notification type: %d", n->type); + } + + ifnot_rem_node(&s->queue, n); + sl_free(n); + } +} + /** * if_find_by_index - find interface by ifindex * @idx: ifindex @@ -600,6 +734,8 @@ ifa_update(struct ifa *a) b = mb_alloc(if_pool, sizeof(struct ifa)); memcpy(b, a, sizeof(struct ifa)); + ifa_link(b); + if_link(i); add_tail(&i->addrs, &b->n); b->flags |= IA_UPDATED; @@ -646,11 +782,29 @@ ifa_delete(struct ifa *a) if (i->flags & IF_UP) ifa_notify_change(IF_CHANGE_DOWN, b); - mb_free(b); + ifa_unlink(b); return; } } +void ifa_link(struct ifa *a) +{ + if (a) + a->uc++; +} + +void ifa_unlink(struct ifa *a) +{ + if (!a) + return; + + if (--a->uc) + return; + + if_unlink(a->iface); + mb_free(a); +} + u32 if_choose_router_id(struct iface_patt *mask, u32 old_id) { @@ -706,6 +860,7 @@ if_init(void) { if_pool = rp_new(&root_pool, "Interfaces"); init_list(&iface_list); + iface_sub_slab = sl_new(if_pool, sizeof(struct iface_notification)); strcpy(default_vrf.name, "default"); neigh_init(if_pool); } diff --git a/nest/iface.h b/nest/iface.h index fb27f99e..a3f4f30a 100644 --- a/nest/iface.h +++ b/nest/iface.h @@ -9,6 +9,7 @@ #ifndef _BIRD_IFACE_H_ #define _BIRD_IFACE_H_ +#include "lib/event.h" #include "lib/lists.h" #include "lib/tlists.h" #include "lib/ip.h" @@ -27,6 +28,7 @@ struct ifa { /* Interface address */ ip_addr opposite; /* Opposite end of a point-to-point link */ unsigned scope; /* Interface address scope */ unsigned flags; /* Analogous to iface->flags */ + unsigned uc; /* Use (link) count */ }; extern struct iface default_vrf; @@ -45,6 +47,7 @@ struct iface { struct ifa *llv6; /* Primary link-local address for IPv6 */ ip4_addr sysdep; /* Arbitrary IPv4 address for internal sysdep use */ list neighbors; /* All neighbors on this interface */ + unsigned uc; /* Use (link) count */ }; #define IF_UP 1 /* Currently just IF_ADMIN_UP */ @@ -115,12 +118,15 @@ void ifa_delete(struct ifa *); void if_start_update(void); void if_end_partial_update(struct iface *); void if_end_update(void); -void if_feed_baby(struct proto *); struct iface *if_find_by_index(unsigned); struct iface *if_find_by_name(const char *); struct iface *if_get_by_name(const char *); void if_recalc_all_preferred_addresses(void); +void if_link(struct iface *); +void if_unlink(struct iface *); +void ifa_link(struct ifa *); +void ifa_unlink(struct ifa *); /* The Neighbor Cache */ @@ -138,6 +144,7 @@ typedef struct neighbor { u16 flags; /* NEF_* flags */ s16 scope; /* Address scope, -1 for unreachable neighbors, SCOPE_HOST when it's our own address */ + uint uc; /* Use (link) count */ } neighbor; #define TLIST_PREFIX proto_neigh @@ -164,6 +171,63 @@ void neigh_ifa_up(struct ifa *a); void neigh_ifa_down(struct ifa *a); void neigh_init(struct pool *); +void neigh_link(neighbor *); +void neigh_unlink(neighbor *); + +/* + * Notification mechanism + */ + +#define TLIST_PREFIX ifnot +#define TLIST_TYPE struct iface_notification +#define TLIST_ITEM nn +#define TLIST_WANT_WALK +#define TLIST_WANT_ADD_TAIL + +struct iface_notification { + TLIST_DEFAULT_NODE; + enum { + IFNOT_INVALID, + IFNOT_ADDRESS, + IFNOT_INTERFACE, + IFNOT_NEIGHBOR, + } type; + unsigned flags; + union { + struct ifa *a; + struct iface *i; + neighbor *n; + }; +}; + +#include "lib/tlists.h" + +#define TLIST_PREFIX ifsub +#define TLIST_TYPE struct iface_subscription +#define TLIST_ITEM n +#define TLIST_WANT_WALK +#define TLIST_WANT_ADD_TAIL + +struct iface_subscription { + TLIST_DEFAULT_NODE; + + event event; + TLIST_LIST(ifnot) queue; + + void (*if_notify)(struct proto *, unsigned flags, struct iface *i); + void (*ifa_notify)(struct proto *, unsigned flags, struct ifa *a); + void (*neigh_notify)(struct neighbor *neigh); +}; + +#include "lib/tlists.h" + +void if_enqueue_notify(struct iface_notification); +void if_enqueue_notify_to(struct iface_notification x, struct iface_subscription *s); + +void iface_flush_notifications(struct iface_subscription *); +void iface_subscribe(struct iface_subscription *); +void iface_unsubscribe(struct iface_subscription *); + /* * Interface Pattern Lists */ diff --git a/nest/neighbor.c b/nest/neighbor.c index c27db989..88ac2860 100644 --- a/nest/neighbor.c +++ b/nest/neighbor.c @@ -258,13 +258,15 @@ neigh_find(struct proto *p, ip_addr a, struct iface *iface, uint flags) add_tail((scope >= 0) ? &iface->neighbors : &sticky_neigh_list, &n->if_n); proto_neigh_add_tail(&p->neighbors, n); n->addr = a; - n->ifa = addr; - n->iface = iface; - n->ifreq = ifreq; + ifa_link(n->ifa = addr); + if_link(n->iface = iface); + if_link(n->ifreq = ifreq); n->proto = p; n->flags = flags; n->scope = scope; + neigh_link(n); + return n; } @@ -309,19 +311,20 @@ neigh_dump_all(void) static inline void neigh_notify(neighbor *n) { - if (n->proto && n->proto->neigh_notify && (n->proto->proto_state != PS_STOP)) - n->proto->neigh_notify(n); + if_enqueue_notify_to((struct iface_notification) { .type = IFNOT_NEIGHBOR, .n = n, }, &n->proto->iface_sub); } static void neigh_up(neighbor *n, struct iface *i, struct ifa *a, int scope) { DBG("Waking up sticky neighbor %I\n", n->addr); - n->iface = i; - n->ifa = a; + if_link(n->iface = i); + ifa_link(n->ifa = a); + n->scope = scope; - rem_node(&n->if_n); + rem_node(&n->if_n); /* HACK: Here the neighbor is always in the sticky list, + regardless whether it is sticky or not */ add_tail(&i->neighbors, &n->if_n); neigh_notify(n); @@ -331,25 +334,48 @@ static void neigh_down(neighbor *n) { DBG("Flushing neighbor %I on %s\n", n->addr, n->iface->name); - n->iface = NULL; - n->ifa = NULL; + n->scope = -1; rem_node(&n->if_n); add_tail(&sticky_neigh_list, &n->if_n); + ifa_unlink(n->ifa); + n->ifa = NULL; + + if_unlink(n->iface); + n->iface = NULL; + neigh_notify(n); } -static inline void -neigh_free(neighbor *n) +void +neigh_link(neighbor *n) { - proto_neigh_rem_node(&n->proto->neighbors, n); + n->uc++; +} + +void +neigh_unlink(neighbor *n) +{ + if (--n->uc) + return; + + struct proto *p = n->proto; + proto_neigh_rem_node(&p->neighbors, n); + + if ((p->proto_state == PS_DOWN) && EMPTY_TLIST(proto_neigh, &p->neighbors)) + ev_schedule(p->event); + n->proto = NULL; rem_node(&n->n); rem_node(&n->if_n); + ifa_unlink(n->ifa); + if_unlink(n->iface); + if_unlink(n->ifreq); + sl_free(n); } @@ -399,7 +425,8 @@ neigh_update(neighbor *n, struct iface *iface) { if (ifa != n->ifa) { - n->ifa = ifa; + ifa_unlink(n->ifa); + ifa_link(n->ifa = ifa); neigh_notify(n); } @@ -413,7 +440,7 @@ neigh_update(neighbor *n, struct iface *iface) if ((n->scope < 0) && !(n->flags & NEF_STICKY)) { - neigh_free(n); + neigh_unlink(n); return; } @@ -534,8 +561,10 @@ neigh_ifa_down(struct ifa *a) void neigh_prune(struct proto *p) { - while (!EMPTY_TLIST(proto_neigh, &p->neighbors)) - neigh_free(THEAD(proto_neigh, &p->neighbors)); + WALK_TLIST_DELSAFE(proto_neigh, n, &p->neighbors) + neigh_unlink(n); + + ASSERT_DIE(EMPTY_TLIST(proto_neigh, &p->neighbors)); } /** diff --git a/nest/proto.c b/nest/proto.c index 2614943c..39e8b999 100644 --- a/nest/proto.c +++ b/nest/proto.c @@ -57,7 +57,11 @@ static inline void channel_reset_limit(struct channel_limit *l); static inline int proto_is_done(struct proto *p) -{ return (p->proto_state == PS_DOWN) && (p->active_channels == 0); } +{ + return (p->proto_state == PS_DOWN) + && (p->active_channels == 0) + && EMPTY_TLIST(proto_neigh, &p->neighbors); +} static inline int channel_is_active(struct channel *c) { return (c->channel_state == CS_START) || (c->channel_state == CS_UP); } @@ -962,14 +966,18 @@ proto_event(void *ptr) if (p->do_start) { - if_feed_baby(p); + iface_subscribe(&p->iface_sub); p->do_start = 0; } if (p->do_stop) { + iface_unsubscribe(&p->iface_sub); + neigh_prune(p); + p->do_stop = 0; } + if (proto_is_done(p)) { if (p->proto->cleanup) @@ -1860,7 +1868,6 @@ proto_do_stop(struct proto *p) p->down_sched = 0; p->gr_recovery = 0; - if (p->main_source) { rt_unlock_source(p->main_source); @@ -1877,7 +1884,6 @@ static void proto_do_down(struct proto *p) { p->down_code = 0; - neigh_prune(p); rfree(p->pool); p->pool = NULL; diff --git a/nest/protocol.h b/nest/protocol.h index 9fbe9158..6d5714a7 100644 --- a/nest/protocol.h +++ b/nest/protocol.h @@ -171,6 +171,7 @@ struct proto { struct rte_src *main_source; /* Primary route source */ struct iface *vrf; /* Related VRF instance, NULL if global */ TLIST_LIST(proto_neigh) neighbors; /* List of neighbor structures */ + struct iface_subscription iface_sub; /* Interface notification subscription */ const char *name; /* Name of this instance (== cf->name) */ u32 debug; /* Debugging flags */ @@ -210,10 +211,7 @@ struct proto { * feed_end Notify channel about finish of route feeding. */ - void (*if_notify)(struct proto *, unsigned flags, struct iface *i); - void (*ifa_notify)(struct proto *, unsigned flags, struct ifa *a); void (*rt_notify)(struct proto *, struct channel *, struct network *net, struct rte *new, struct rte *old); - void (*neigh_notify)(struct neighbor *neigh); int (*preexport)(struct channel *, struct rte *rt); void (*reload_routes)(struct channel *); void (*feed_begin)(struct channel *, int initial); diff --git a/nest/rt-dev.c b/nest/rt-dev.c index 7932b8b7..42b6b499 100644 --- a/nest/rt-dev.c +++ b/nest/rt-dev.c @@ -141,8 +141,8 @@ dev_init(struct proto_config *CF) proto_configure_channel(P, &p->ip4_channel, cf->ip4_channel); proto_configure_channel(P, &p->ip6_channel, cf->ip6_channel); - P->if_notify = dev_if_notify; - P->ifa_notify = dev_ifa_notify; + P->iface_sub.if_notify = dev_if_notify; + P->iface_sub.ifa_notify = dev_ifa_notify; return P; } diff --git a/proto/babel/babel.c b/proto/babel/babel.c index e1b31e86..15135922 100644 --- a/proto/babel/babel.c +++ b/proto/babel/babel.c @@ -2424,7 +2424,7 @@ babel_init(struct proto_config *CF) proto_configure_channel(P, &p->ip4_channel, cf->ip4_channel); proto_configure_channel(P, &p->ip6_channel, cf->ip6_channel); - P->if_notify = babel_if_notify; + P->iface_sub.if_notify = babel_if_notify; P->rt_notify = babel_rt_notify; P->preexport = babel_preexport; P->rte_better = babel_rte_better; diff --git a/proto/bfd/bfd.c b/proto/bfd/bfd.c index 69e42f3d..b2655952 100644 --- a/proto/bfd/bfd.c +++ b/proto/bfd/bfd.c @@ -1012,7 +1012,7 @@ bfd_init(struct proto_config *c) { struct proto *p = proto_new(c); - p->neigh_notify = bfd_neigh_notify; + p->iface_sub.neigh_notify = bfd_neigh_notify; return p; } diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index 5b0569ae..3531ce51 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -1686,7 +1686,7 @@ bgp_init(struct proto_config *CF) P->rt_notify = bgp_rt_notify; P->preexport = bgp_preexport; - P->neigh_notify = bgp_neigh_notify; + P->iface_sub.neigh_notify = bgp_neigh_notify; P->reload_routes = bgp_reload_routes; P->feed_begin = bgp_feed_begin; P->feed_end = bgp_feed_end; diff --git a/proto/ospf/ospf.c b/proto/ospf/ospf.c index ad4b2d14..77a14b7a 100644 --- a/proto/ospf/ospf.c +++ b/proto/ospf/ospf.c @@ -370,8 +370,8 @@ ospf_init(struct proto_config *CF) P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF)); P->rt_notify = ospf_rt_notify; - P->if_notify = ospf_if_notify; - P->ifa_notify = cf->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3; + P->iface_sub.if_notify = ospf_if_notify; + P->iface_sub.ifa_notify = cf->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3; P->preexport = ospf_preexport; P->reload_routes = ospf_reload_routes; P->feed_begin = ospf_feed_begin; diff --git a/proto/perf/perf.c b/proto/perf/perf.c index 75e405f0..8941d582 100644 --- a/proto/perf/perf.c +++ b/proto/perf/perf.c @@ -266,7 +266,7 @@ perf_init(struct proto_config *CF) switch (p->mode) { case PERF_MODE_IMPORT: - P->ifa_notify = perf_ifa_notify; + P->iface_sub.ifa_notify = perf_ifa_notify; break; case PERF_MODE_EXPORT: P->rt_notify = perf_rt_notify; diff --git a/proto/radv/radv.c b/proto/radv/radv.c index ee1da36c..8a7440e6 100644 --- a/proto/radv/radv.c +++ b/proto/radv/radv.c @@ -579,8 +579,8 @@ radv_init(struct proto_config *CF) P->preexport = radv_preexport; P->rt_notify = radv_rt_notify; - P->if_notify = radv_if_notify; - P->ifa_notify = radv_ifa_notify; + P->iface_sub.if_notify = radv_if_notify; + P->iface_sub.ifa_notify = radv_ifa_notify; return P; } diff --git a/proto/rip/rip.c b/proto/rip/rip.c index 93b0d528..abbd83f2 100644 --- a/proto/rip/rip.c +++ b/proto/rip/rip.c @@ -1135,9 +1135,9 @@ rip_init(struct proto_config *CF) P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF)); - P->if_notify = rip_if_notify; + P->iface_sub.if_notify = rip_if_notify; P->rt_notify = rip_rt_notify; - P->neigh_notify = rip_neigh_notify; + P->iface_sub.neigh_notify = rip_neigh_notify; P->reload_routes = rip_reload_routes; P->rte_better = rip_rte_better; P->rte_igp_metric = rip_rte_igp_metric; diff --git a/proto/static/static.c b/proto/static/static.c index bb93305e..ba0984b5 100644 --- a/proto/static/static.c +++ b/proto/static/static.c @@ -463,7 +463,7 @@ static_init(struct proto_config *CF) P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF)); - P->neigh_notify = static_neigh_notify; + P->iface_sub.neigh_notify = static_neigh_notify; P->reload_routes = static_reload_routes; P->rte_better = static_rte_better; P->rte_mergable = static_rte_mergable; diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index 7ec30eb0..52822c3d 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -1046,7 +1046,7 @@ krt_init(struct proto_config *CF) p->p.preexport = krt_preexport; p->p.rt_notify = krt_rt_notify; - p->p.if_notify = krt_if_notify; + p->p.iface_sub.if_notify = krt_if_notify; p->p.reload_routes = krt_reload_routes; p->p.feed_end = krt_feed_end; -- cgit v1.2.3