1999-12-09 18:54:20 +00:00
|
|
|
/*
|
|
|
|
* BIRD Object Locks
|
|
|
|
*
|
|
|
|
* (c) 1999 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-01 16:16:49 +00:00
|
|
|
/**
|
|
|
|
* DOC: Object locks
|
|
|
|
*
|
|
|
|
* The lock module provides a simple mechanism for avoiding conflicts between
|
|
|
|
* various protocols which would like to use a single physical resource (for
|
|
|
|
* example a network port). It would be easy to say that such collisions can
|
|
|
|
* occur only when the user specifies an invalid configuration and therefore
|
|
|
|
* he deserves to get what he has asked for, but unfortunately they can also
|
|
|
|
* arise legitimately when the daemon is reconfigured and there exists (although
|
2000-06-07 13:25:53 +00:00
|
|
|
* for a short time period only) an old protocol instance being shut down and a new one
|
2000-06-01 16:16:49 +00:00
|
|
|
* willing to start up on the same interface.
|
|
|
|
*
|
|
|
|
* The solution is very simple: when any protocol wishes to use a network port
|
2000-06-07 13:25:53 +00:00
|
|
|
* or some other non-shareable resource, it asks the core to lock it and it doesn't
|
2000-06-01 16:16:49 +00:00
|
|
|
* use the resource until it's notified that it has acquired the lock.
|
|
|
|
*
|
2014-07-18 16:24:12 +00:00
|
|
|
* Object locks are represented by &object_lock structures which are in turn a
|
|
|
|
* kind of resource. Lockable resources are uniquely determined by resource type
|
2000-06-01 16:16:49 +00:00
|
|
|
* (%OBJLOCK_UDP for a UDP port etc.), IP address (usually a broadcast or
|
2014-07-18 16:24:12 +00:00
|
|
|
* multicast address the port is bound to), port number, interface and optional
|
|
|
|
* instance ID.
|
2000-06-01 16:16:49 +00:00
|
|
|
*/
|
|
|
|
|
2000-05-04 09:08:28 +00:00
|
|
|
#undef LOCAL_DEBUG
|
1999-12-09 18:54:20 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "nest/locks.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
|
|
|
|
static list olock_list;
|
2023-01-24 10:01:34 +00:00
|
|
|
|
|
|
|
DEFINE_DOMAIN(attrs);
|
|
|
|
static DOMAIN(attrs) olock_domain;
|
|
|
|
#define OBJ_LOCK LOCK_DOMAIN(attrs, olock_domain)
|
|
|
|
#define OBJ_UNLOCK UNLOCK_DOMAIN(attrs, olock_domain)
|
1999-12-09 18:54:20 +00:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
olock_same(struct object_lock *x, struct object_lock *y)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
x->type == y->type &&
|
|
|
|
x->iface == y->iface &&
|
2017-09-12 13:49:36 +00:00
|
|
|
x->vrf == y->vrf &&
|
1999-12-09 18:54:20 +00:00
|
|
|
x->port == y->port &&
|
2014-07-18 16:24:12 +00:00
|
|
|
x->inst == y->inst &&
|
1999-12-09 18:54:20 +00:00
|
|
|
ipa_equal(x->addr, y->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
olock_free(resource *r)
|
|
|
|
{
|
2023-01-24 10:01:34 +00:00
|
|
|
/* Called externally from rfree() */
|
|
|
|
struct object_lock *l = SKIP_BACK(struct object_lock, r, r);
|
1999-12-09 18:54:20 +00:00
|
|
|
node *n;
|
|
|
|
|
2023-01-24 10:01:34 +00:00
|
|
|
OBJ_LOCK;
|
1999-12-09 18:54:20 +00:00
|
|
|
DBG("olock: Freeing %p\n", l);
|
|
|
|
switch (l->state)
|
|
|
|
{
|
|
|
|
case OLOCK_STATE_FREE:
|
|
|
|
break;
|
|
|
|
case OLOCK_STATE_LOCKED:
|
2023-01-24 10:01:34 +00:00
|
|
|
/* Remove myself from the olock_list */
|
1999-12-09 18:54:20 +00:00
|
|
|
rem_node(&l->n);
|
2023-01-24 10:01:34 +00:00
|
|
|
|
|
|
|
/* Maybe the notification is still pending. */
|
|
|
|
ev_postpone(&l->event);
|
|
|
|
|
|
|
|
/* Get new lock candidate */
|
1999-12-09 18:54:20 +00:00
|
|
|
n = HEAD(l->waiters);
|
2023-01-24 10:01:34 +00:00
|
|
|
if (NODE_VALID(n))
|
1999-12-09 18:54:20 +00:00
|
|
|
{
|
2023-01-24 10:01:34 +00:00
|
|
|
struct object_lock *q = SKIP_BACK(struct object_lock, n, n);
|
|
|
|
|
|
|
|
/* Remove this candidate from waiters list */
|
1999-12-09 18:54:20 +00:00
|
|
|
rem_node(n);
|
2023-01-24 10:01:34 +00:00
|
|
|
|
|
|
|
/* Move waiter lists */
|
|
|
|
DBG("olock: -> %p becomes locked\n", n);
|
2015-02-21 11:27:05 +00:00
|
|
|
add_tail_list(&q->waiters, &l->waiters);
|
2023-01-24 10:01:34 +00:00
|
|
|
|
|
|
|
/* Add the new olock to olock_list */
|
1999-12-09 18:54:20 +00:00
|
|
|
add_head(&olock_list, n);
|
2023-01-24 10:01:34 +00:00
|
|
|
|
|
|
|
/* Inform */
|
|
|
|
q->state = OLOCK_STATE_LOCKED;
|
|
|
|
ev_send(q->target, &q->event);
|
1999-12-09 18:54:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OLOCK_STATE_WAITING:
|
2023-01-24 10:01:34 +00:00
|
|
|
/* Remove from the waiters list */
|
1999-12-09 18:54:20 +00:00
|
|
|
rem_node(&l->n);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
2023-01-24 10:01:34 +00:00
|
|
|
OBJ_UNLOCK;
|
1999-12-09 18:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-02-28 09:42:47 +00:00
|
|
|
olock_dump(resource *r, unsigned indent UNUSED)
|
1999-12-09 18:54:20 +00:00
|
|
|
{
|
|
|
|
struct object_lock *l = (struct object_lock *) r;
|
|
|
|
static char *olock_states[] = { "free", "locked", "waiting", "event" };
|
|
|
|
|
2014-07-18 16:24:12 +00:00
|
|
|
debug("(%d:%s:%I:%d:%d) [%s]\n", l->type, (l->iface ? l->iface->name : "?"), l->addr, l->port, l->inst, olock_states[l->state]);
|
1999-12-09 18:54:20 +00:00
|
|
|
if (!EMPTY_LIST(l->waiters))
|
|
|
|
debug(" [wanted]\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct resclass olock_class = {
|
|
|
|
"ObjLock",
|
|
|
|
sizeof(struct object_lock),
|
|
|
|
olock_free,
|
2010-02-21 13:34:53 +00:00
|
|
|
olock_dump,
|
2016-10-14 13:37:04 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
1999-12-09 18:54:20 +00:00
|
|
|
};
|
|
|
|
|
2000-06-01 16:16:49 +00:00
|
|
|
/**
|
|
|
|
* olock_new - create an object lock
|
|
|
|
* @p: resource pool to create the lock in.
|
|
|
|
*
|
|
|
|
* The olock_new() function creates a new resource of type &object_lock
|
|
|
|
* and returns a pointer to it. After filling in the structure, the caller
|
|
|
|
* should call olock_acquire() to do the real locking.
|
|
|
|
*/
|
1999-12-09 18:54:20 +00:00
|
|
|
struct object_lock *
|
|
|
|
olock_new(pool *p)
|
|
|
|
{
|
|
|
|
struct object_lock *l = ralloc(p, &olock_class);
|
|
|
|
|
|
|
|
l->state = OLOCK_STATE_FREE;
|
|
|
|
init_list(&l->waiters);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2000-06-01 16:16:49 +00:00
|
|
|
/**
|
|
|
|
* olock_acquire - acquire a lock
|
|
|
|
* @l: the lock to acquire
|
|
|
|
*
|
|
|
|
* This function attempts to acquire exclusive access to the non-shareable
|
|
|
|
* resource described by the lock @l. It returns immediately, but as soon
|
|
|
|
* as the resource becomes available, it calls the hook() function set up
|
|
|
|
* by the caller.
|
|
|
|
*
|
|
|
|
* When you want to release the resource, just rfree() the lock.
|
|
|
|
*/
|
1999-12-09 18:54:20 +00:00
|
|
|
void
|
|
|
|
olock_acquire(struct object_lock *l)
|
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
struct object_lock *q;
|
|
|
|
|
2023-01-24 10:01:34 +00:00
|
|
|
OBJ_LOCK;
|
|
|
|
|
1999-12-09 18:54:20 +00:00
|
|
|
WALK_LIST(n, olock_list)
|
|
|
|
{
|
|
|
|
q = SKIP_BACK(struct object_lock, n, n);
|
|
|
|
if (olock_same(q, l))
|
|
|
|
{
|
|
|
|
l->state = OLOCK_STATE_WAITING;
|
|
|
|
add_tail(&q->waiters, &l->n);
|
|
|
|
DBG("olock: %p waits\n", l);
|
2023-01-24 10:01:34 +00:00
|
|
|
|
|
|
|
OBJ_UNLOCK;
|
1999-12-09 18:54:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 10:01:34 +00:00
|
|
|
|
1999-12-09 18:54:20 +00:00
|
|
|
DBG("olock: %p acquired immediately\n", l);
|
|
|
|
add_head(&olock_list, &l->n);
|
|
|
|
|
2023-01-24 10:01:34 +00:00
|
|
|
l->state = OLOCK_STATE_LOCKED;
|
|
|
|
ev_send(l->target, &l->event);
|
1999-12-09 18:54:20 +00:00
|
|
|
|
2023-01-24 10:01:34 +00:00
|
|
|
OBJ_UNLOCK;
|
1999-12-09 18:54:20 +00:00
|
|
|
}
|
|
|
|
|
2000-06-01 16:16:49 +00:00
|
|
|
/**
|
|
|
|
* olock_init - initialize the object lock mechanism
|
|
|
|
*
|
|
|
|
* This function is called during BIRD startup. It initializes
|
|
|
|
* all the internal data structures of the lock module.
|
|
|
|
*/
|
1999-12-09 18:54:20 +00:00
|
|
|
void
|
|
|
|
olock_init(void)
|
|
|
|
{
|
|
|
|
DBG("olock: init\n");
|
|
|
|
init_list(&olock_list);
|
2023-04-24 14:10:59 +00:00
|
|
|
olock_domain = DOMAIN_NEW(attrs);
|
|
|
|
DOMAIN_SETUP(attrs, olock_domain, "Object lock", NULL);
|
1999-12-09 18:54:20 +00:00
|
|
|
}
|