Commit bf139664 authored by Ondřej Zajíček's avatar Ondřej Zajíček
Browse files

Initial BFD commit, work in progress.

parent bff9ce51
#define BUFFER(type) struct { type *data; uint used, size; }
#define BUFFER_SIZE(v) ((v).size * sizeof(* (v).data))
#define BUFFER_INIT(v,pool,isize) \
({ \
(v).used = 0; \
(v).size = (isize); \
(v).data = mb_alloc(pool, BUFFER_SIZE(v)); \
})
#define BUFFER_SET(v,nsize) \
({ \
(v).used = (nsize); \
if ((v).used > (v).size) \
buffer_realloc((void **) &((v).data), &((v).size), (v).used, sizeof(* (v).data)); \
})
#define BUFFER_INC(v,step) \
({ \
uint _o = (v).used; \
BUFFER_SET(v, (v).used + (step)); \
(v).data + _o; \
})
#define BUFFER_DEC(v,step) ({ (v).used -= (step); })
#define BUFFER_PUSH(v) (*BUFFER_INC(v,1))
#define BUFFER_POP(v) BUFFER_DEC(v,1)
#define BUFFER_FLUSH(v) ({ (v).used = 0; })
#define HASH(type) struct { type **data; uint used, size; }
#define HASH_TYPE(v) typeof(** (v).data)
#define HASH_SIZE(v) ((v).size * sizeof(* (v).data))
#define HASH_INIT(v,pool,isize) \
({ \
(v).used = 0; \
(v).size = (isize); \
(v).data = mb_allocz(pool, HASH_SIZE(v)); \
})
#define HASH_FIND(v,id,key) \
({ \
HASH_TYPE(v) *_n = (v).data[id##_FN(key, (v).size)]; \
while (_n && !id##_EQ(_n, key)) \
_n = _n->id##_NEXT; \
_n; \
})
#define HASH_INSERT(v,id,key,node) \
({ \
HASH_TYPE(v) **_nn = (v).data + id##_FN(key, (v).size); \
node->id##_NEXT = *_nn; \
*_nn = node; \
})
#define HASH_DELETE(v,id,key) \
({ \
HASH_TYPE(v) **_nn = (v).data + id##_FN(key, (v).size); \
while ((*_nn) && !id##_EQ(*_nn, key)) \
_nn = &((*_nn)->id##_NEXT); \
\
HASH_TYPE(v) *_n = *_nn; \
if (_n) \
*_nn = _n->id##_NEXT; \
_n; \
})
#define HASH_REMOVE(v,id,node) \
({ \
HASH_TYPE(v) **_nn = (v).data + id##_FN(key, (v).size); \
while ((*_nn) && (*_nn != (node))) \
_nn = &((*_nn)->id##_NEXT); \
\
HASH_TYPE(v) *_n = *_nn; \
if (_n) \
*_nn = _n->id##_NEXT; \
_n; \
})
#define HASH_WALK(v,next,n) \
do { \
HASH_TYPE(v) *n; \
uint _i; \
for (_i = 0; _i < ((v).size); _i++) \
for (n = (v).data[_i]; n; n = n->next)
#define HASH_WALK_END } while (0)
#define HASH_WALK_DELSAFE(v,next,n) \
do { \
HASH_TYPE(v) *n, *_next; \
uint _i; \
for (_i = 0; _i < ((v).size); _i++) \
for (n = (v).data[_i]; n && (_next = n->next, 1); n = _next)
#define HASH_WALK_DELSAFE_END } while (0)
/*
define HASH_REHASH(s) \
({ \
type *_n; \
uint _i; \
for (_i = 0; _i < (size_f); _i++) \
for (_n = (hash)[_i]; _n != NULL; _n =
*/
/*
* UCW Library -- Universal Heap Macros
*
* (c) 2001 Martin Mares <mj@ucw.cz>
* (c) 2005 Tomas Valla <tom@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
*/
/**
* [[intro]]
* Introduction
* ------------
*
* Binary heap is a simple data structure, which for example supports efficient insertions, deletions
* and access to the minimal inserted item. We define several macros for such operations.
* Note that because of simplicity of heaps, we have decided to define direct macros instead
* of a <<generic:,macro generator>> as for several other data structures in the Libucw.
*
* A heap is represented by a number of elements and by an array of values. Beware that we
* index this array from one, not from zero as do the standard C arrays.
*
* Most macros use these parameters:
*
* - @type - the type of elements
* - @num - a variable (signed or unsigned integer) with the number of elements
* - @heap - a C array of type @type; the heap is stored in `heap[1] .. heap[num]`; `heap[0]` is unused
* - @less - a callback to compare two element values; `less(x, y)` shall return a non-zero value iff @x is lower than @y
* - @swap - a callback to swap two array elements; `swap(heap, i, j, t)` must swap `heap[i]` with `heap[j]` with possible help of temporary variable @t (type @type).
*
* A valid heap must follow these rules:
*
* - `num >= 0`
* - `heap[i] >= heap[i / 2]` for each `i` in `[2, num]`
*
* The first element `heap[1]` is always lower or equal to all other elements.
*
* [[macros]]
* Macros
* ------
*/
/* For internal usage. */
#define HEAP_BUBBLE_DOWN_J(heap,num,less,swap) \
for (;;) \
{ \
_l = 2*_j; \
if (_l > num) \
break; \
if (less(heap[_j],heap[_l]) && (_l == num || less(heap[_j],heap[_l+1]))) \
break; \
if (_l != num && less(heap[_l+1],heap[_l])) \
_l++; \
swap(heap,_j,_l,x); \
_j = _l; \
}
/* For internal usage. */
#define HEAP_BUBBLE_UP_J(heap,num,less,swap) \
while (_j > 1) \
{ \
_u = _j/2; \
if (less(heap[_u], heap[_j])) \
break; \
swap(heap,_u,_j,x); \
_j = _u; \
}
/**
* Shuffle the unordered array @heap of @num elements to become a valid heap. The time complexity is linear.
**/
#define HEAP_INIT(heap,num,type,less,swap) \
do { \
uns _i = num; \
uns _j, _l; \
type x; \
while (_i >= 1) \
{ \
_j = _i; \
HEAP_BUBBLE_DOWN_J(heap,num,less,swap) \
_i--; \
} \
} while(0)
/**
* Delete the minimum element `heap[1]` in `O(log(n))` time.
* The removed value is moved just after the resulting heap (`heap[num + 1]`).
**/
#define HEAP_DELMIN(heap,num,type,less,swap) \
do { \
uns _j, _l; \
type x; \
swap(heap,1,num,x); \
num--; \
_j = 1; \
HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \
} while(0)
/**
* Insert `heap[num]` in `O(log(n))` time. The value of @num must be increased before.
**/
#define HEAP_INSERT(heap,num,type,less,swap) \
do { \
uns _j, _u; \
type x; \
_j = num; \
HEAP_BUBBLE_UP_J(heap,num,less,swap); \
} while(0)
/**
* If you need to increase the value of `heap[pos]`, just do it and then call this macro to rebuild the heap.
* Only `heap[pos]` can be changed, the rest of the array must form a valid heap.
* The time complexity is `O(log(n))`.
**/
#define HEAP_INCREASE(heap,num,type,less,swap,pos) \
do { \
uns _j, _l; \
type x; \
_j = pos; \
HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \
} while(0)
/**
* If you need to decrease the value of `heap[pos]`, just do it and then call this macro to rebuild the heap.
* Only `heap[pos]` can be changed, the rest of the array must form a valid heap.
* The time complexity is `O(log(n))`.
**/
#define HEAP_DECREASE(heap,num,type,less,swap,pos) \
do { \
uns _j, _u; \
type x; \
_j = pos; \
HEAP_BUBBLE_UP_J(heap,num,less,swap); \
} while(0)
/**
* Delete `heap[pos]` in `O(log(n))` time.
**/
#define HEAP_DELETE(heap,num,type,less,swap,pos) \
do { \
uns _j, _l, _u; \
type x; \
_j = pos; \
swap(heap,_j,num,x); \
num--; \
if (less(heap[_j], heap[num+1])) \
HEAP_BUBBLE_UP_J(heap,num,less,swap) \
else \
HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \
} while(0)
/**
* Default swapping macro.
**/
#define HEAP_SWAP(heap,a,b,t) (t=heap[a], heap[a]=heap[b], heap[b]=t)
......@@ -100,6 +100,27 @@ rem_node(node *n)
x->prev = z;
}
/**
* replace_node - replace a node in a list with another one
* @old: node to be removed
* @new: node to be inserted
*
* Replaces node @old in the list it's linked in with node @new. Node
* @old may be a copy of the original node, which is not accessed
* through the list. The function could be called with @old == @new,
* which just fixes neighbors' pointers in the case that the node
* was reallocated.
*/
LIST_INLINE void
replace_node(node *old, node *new)
{
old->next->prev = new;
old->prev->next = new;
new->prev = old->prev;
new->next = old->next;
}
/**
* init_list - create an empty list
* @l: list
......
......@@ -366,21 +366,21 @@ mb_allocz(pool *p, unsigned size)
/**
* mb_realloc - reallocate a memory block
* @p: pool
* @m: memory block
* @size: new size of the block
*
* mb_realloc() changes the size of the memory block @m to a given size.
* The contents will be unchanged to the minimum of the old and new sizes;
* newly allocated memory will be uninitialized. If @m is NULL, the call
* is equivalent to mb_alloc(@p, @size).
* newly allocated memory will be uninitialized. Contrary to realloc()
* behavior, @m must be non-NULL, because the resource pool is inherited
* from it.
*
* Like mb_alloc(), mb_realloc() also returns a pointer to the memory
* chunk , not to the resource, hence you have to free it using
* chunk, not to the resource, hence you have to free it using
* mb_free(), not rfree().
*/
void *
mb_realloc(pool *p, void *m, unsigned size)
mb_realloc(void *m, unsigned size)
{
struct mblock *ob = NULL;
......@@ -392,9 +392,7 @@ mb_realloc(pool *p, void *m, unsigned size)
}
struct mblock *b = xrealloc(ob, sizeof(struct mblock) + size);
b->r.class = &mb_class;
add_tail(&p->inside, &b->r.n);
replace_node(&b->r.n, &b->r.n);
b->size = size;
return b->data;
}
......@@ -413,3 +411,18 @@ mb_free(void *m)
rfree(b);
}
#define STEP_UP(x) ((x) + (x)/2 + 4)
void
buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_size)
{
unsigned nsize = MIN(*size, need);
while (nsize < need)
nsize = STEP_UP(nsize);
*buf = mb_realloc(*buf, nsize*isize);
*size = nsize;
}
......@@ -44,6 +44,7 @@ typedef struct birdsock {
/* laddr and lifindex are valid only if SKF_LADDR_RX flag is set to request it */
int fd; /* System-dependent data */
int index; /* Index in poll buffer */
node n;
void *rbuf_alloc, *tbuf_alloc;
char *password; /* Password for MD5 authentication */
......@@ -91,6 +92,7 @@ extern int sk_priority_control; /* Suggested priority for control traffic, shoul
#define SKF_LADDR_TX 4 /* Allow to specify local address for TX packets */
#define SKF_TTL_RX 8 /* Report TTL / Hop Limit for RX packets */
#define SKF_THREAD 0x100 /* Socked used in thread, Do not add to main loop */
/*
* Socket types SA SP DA DP IF TTL SendTo (?=may, -=must not, *=must)
......
H Protocols
C bfd
C bgp
C ospf
C pipe
......
source=bfd.c
root-rel=../../
dir-name=proto/bfd
include ../../Rules
#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/protocol.h"
#include "nest/route.h"
#include "nest/cli.h"
#include "conf/conf.h"
#include "lib/socket.h"
#include "lib/resource.h"
#include "lib/string.h"
#include "bfd.h"
#define HASH_ID_KEY loc_id
#define HASH_ID_NEXT next_id
#define HASH_ID_EQ(a,b) ((a)==(b))
#define HASH_ID_FN(a) (a)
#define HASH_IP_KEY addr
#define HASH_IP_NEXT next_ip
#define HASH_IP_EQ(a,b) ((a)==(b))
#define HASH_IP_FN(a) (a == b)
static u32
bfd_get_free_id(struct bfd_proto *p)
{
u32 id;
for (id = random_u32(); 1; id++)
if (id && !bfd_find_session_by_id(p, id))
break;
return id;
}
static void
bfd_add_session(struct bfd_proto *p, ip_addr addr, struct bfd_session_config *opts)
{
birdloop_enter(p->loop);
struct bfd_session *s = sl_alloc(p->session_slab);
bzero(s, sizeof(struct bfd_session));
/* Initialization of state variables - see RFC 5880 3.8.1 */
s->loc_state = BFD_STATE_DOWN;
s->rem_state = BFD_STATE_DOWN;
s->loc_id = bfd_get_free_id(p);
s->des_min_tx_int = s->des_min_tx_new = s->opts->idle_tx_int;
s->req_min_rx_int = s->req_min_rx_new = s->opts->min_rx_int;
s->detect_mult = s->opts->multiplier;
s->rem_min_rx_int = 1;
HASH_INSERT(p->session_hash_id, HASH_ID, s);
HASH_INSERT(p->session_hash_ip, HASH_IP, s);
s->tx_timer = tm2_new_set(xxx, bfd_rx_timer_hook, s, 0, 0);
s->hold_timer = tm2_new_set(xxx, bfd_hold_timer_hook, s, 0, 0);
bfd_session_update_tx_interval(s);
birdloop_leave(p->loop);
}
static void
bfd_open_session(struct bfd_proto *p, struct bfd_session *s, ip_addr local, struct iface *ifa)
{
birdloop_enter(p->loop);
s->bsock = bfd_get_socket(p, local, ifa);
s->local = local;
s->iface = ifa;
s->opened = 1;
bfd_session_control_tx_timer(s);
birdloop_leave(p->loop);
}
static void
bfd_close_session(struct bfd_proto *p, struct bfd_session *s)
{
birdloop_enter(p->loop);
bfd_free_socket(s->bsock);
s->bsock = NULL;
s->local = IPA_NONE;
s->iface = NULL;
s->opened = 0;
bfd_session_control_tx_timer(s);
birdloop_leave(p->loop);
}
static void
bfd_remove_session(struct bfd_proto *p, struct bfd_session *s)
{
birdloop_enter(p->loop);
bfd_free_socket(s->bsock);
rfree(s->tx_timer);
rfree(s->hold_timer);
HASH_REMOVE(p->session_hash_id, HASH_ID, s);
HASH_REMOVE(p->session_hash_ip, HASH_IP, s);
sl_free(p->session_slab, s);
birdloop_leave(p->loop);
}
struct bfd_session *
bfd_find_session_by_id(struct bfd_proto *p, u32 id)
{
return HASH_FIND(p->session_hash_id, HASH_ID, id);
}
struct bfd_session *
bfd_find_session_by_addr(struct bfd_proto *p, ip_addr addr)
{
return HASH_FIND(p->session_hash_ip, HASH_IP, addr);
}
static void
bfd_rx_timer_hook(timer2 *t)
{
struct bfd_session *s = timer->data;
s->last_tx = xxx_now;
bfd_send_ctl(s->bfd, s, 0);
}
static void
bfd_hold_timer_hook(timer2 *t)
{
bfd_session_timeout(timer->data);
}
static void
bfd_session_timeout(struct bfd_session *s)
{
s->rem_state = BFD_STATE_DOWN;
s->rem_id = 0;
s->rem_min_tx_int = 0;
s->rem_min_rx_int = 1;
s->rem_demand_mode = 0;
bfd_session_update_state(s, BFD_STATE_DOWN, BFD_DIAG_TIMEOUT);
}
static void
bfd_session_control_tx_timer(struct bfd_session *s)
{
if (!s->opened)
goto stop;
if (s->passive && (s->rem_id == 0))
goto stop;
if (s->rem_demand_mode &&
!s->poll_active &&
(s->loc_state == BFD_STATE_UP) &&
(s->rem_state == BFD_STATE_UP))
goto stop;
if (s->rem_min_rx_int == 0)
goto stop;
/* So TX timer should run */
if (tm2_active(s->tx_timer))
return;
tm2_start(s->tx_timer, 0);
return;
stop:
tm2_stop(s->tx_timer);
s->last_tx = 0;
}
static void
bfd_session_update_tx_interval(struct bfd_session *s)
{
u32 tx_int = MAX(s->des_min_tx_int, s->rem_min_rx_int);
u32 tx_int_l = tx_int - (tx_int / 4); // 75 %
u32 tx_int_h = tx_int - (tx_int / 10); // 90 %
s->tx_timer->recurrent = tx_int_l;
s->tx_timer->randomize = tx_int_h - tx_int_l;
/* Do not set timer if no previous event */
if (!s->last_tx)
return;
/* Set timer relative to last tx_timer event */
tm2_set(s->tx_timer, s->last_tx + tx_int_l);
}
static void
bfd_session_update_detection_time(struct bfd_session *s, int kick)
{
xxx_time timeout = (xxx_time) MAX(s->req_min_rx_int, s->rem_min_tx_int) * s->rem_detect_mult;
if (kick)
s->last_rx = xxx_now;
if (!s->last_rx)
return;
tm2_set(s->hold_timer, s->last_rx + timeout);
}
void
bfd_session_request_poll(struct bfd_session *s, u8 request)
{
s->poll_scheduled |= request;
if (s->poll_active)
return;
s->poll_active = s->poll_scheduled;
s->poll_scheduled = 0;
bfd_send_ctl(s->bfd, s, 0);
}
void
bfd_session_terminate_poll(struct bfd_session *s)
{
u8 poll_done = s->poll_active & ~s->poll_scheduled;
if (poll_done & BFD_POLL_TX)
s->des_min_tx_int = s->des_min_tx_new;
if (poll_done & BFD_POLL_RX)
s->req_min_rx_int = s->req_min_rx_new;
s->poll_active = 0;
/* Timers are updated by caller - bfd_session_process_ctl() */
xxx_restart_poll();
}
void
bfd_session_process_ctl(struct bfd_session *s, u8 flags, u32 old_rx_int, u32 old_tx_int)
{
if (s->poll_active && (flags & BFD_FLAG_FINAL))
bfd_session_terminate_poll(s);
if ((s->des_min_tx_int != old_rx_int) || (s->rem_min_rx_int != old_tx_int))
bfd_session_update_tx_interval(s);
bfd_session_update_detection_time(s, 1);