1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
|
/*
* BIRD -- Bidirectional Forwarding Detection (BFD)
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "bfd.h"
#define HASH_ID_KEY(n) n->loc_id
#define HASH_ID_NEXT(n) n->next_id
#define HASH_ID_EQ(a,b) (a == b)
#define HASH_ID_FN(k) (k)
#define HASH_IP_KEY(n) n->addr
#define HASH_IP_NEXT(n) n->next_ip
#define HASH_IP_EQ(a,b) ipa_equal(a,b)
#define HASH_IP_FN(k) ipa_hash(k)
static inline void bfd_notify_kick(struct bfd_proto *p);
static void
bfd_session_update_state(struct bfd_session *s, uint state, uint diag)
{
struct bfd_proto *p = s->bfd;
int notify;
if (s->loc_state == state)
return;
//TRACE(D_EVENTS, "Session changed %I %d %d", s->addr, state, diag);
debug("STATE %I %d %d %d\n", s->addr, s->loc_state, state, diag);
bfd_lock_sessions(p);
s->loc_state = state;
s->loc_diag = diag;
notify = !NODE_VALID(&s->n);
if (notify)
add_tail(&p->notify_list, &s->n);
bfd_unlock_sessions(p);
if (notify)
bfd_notify_kick(p);
}
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;
s->rem_detect_mult = 0;
bfd_session_update_state(s, BFD_STATE_DOWN, BFD_DIAG_TIMEOUT);
}
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)
{
btime timeout = (btime) MAX(s->req_min_rx_int, s->rem_min_tx_int) * s->rem_detect_mult;
if (kick)
s->last_rx = current_time();
if (!s->last_rx)
return;
tm2_set(s->hold_timer, s->last_rx + 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_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);
}
static 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_tx_int, u32 old_rx_int)
{
if (s->poll_active && (flags & BFD_FLAG_FINAL))
bfd_session_terminate_poll(s);
if ((s->des_min_tx_int != old_tx_int) || (s->rem_min_rx_int != old_rx_int))
bfd_session_update_tx_interval(s);
bfd_session_update_detection_time(s, 1);
/* Update session state */
int next_state = 0;
int diag = BFD_DIAG_NOTHING;
switch (s->loc_state)
{
case BFD_STATE_ADMIN_DOWN:
return;
case BFD_STATE_DOWN:
if (s->rem_state == BFD_STATE_DOWN) next_state = BFD_STATE_INIT;
else if (s->rem_state == BFD_STATE_INIT) next_state = BFD_STATE_UP;
break;
case BFD_STATE_INIT:
if (s->rem_state == BFD_STATE_ADMIN_DOWN) next_state = BFD_STATE_DOWN, diag = BFD_DIAG_NEIGHBOR_DOWN;
else if (s->rem_state >= BFD_STATE_INIT) next_state = BFD_STATE_UP;
break;
case BFD_STATE_UP:
if (s->rem_state <= BFD_STATE_DOWN) next_state = BFD_STATE_DOWN, diag = BFD_DIAG_NEIGHBOR_DOWN;
break;
}
if (next_state)
bfd_session_update_state(s, next_state, diag);
bfd_session_control_tx_timer(s);
if (flags & BFD_FLAG_POLL)
bfd_send_ctl(s->bfd, s, 1);
}
static void
bfd_session_set_min_tx(struct bfd_session *s, u32 val)
{
/* Note that des_min_tx_int <= des_min_tx_new */
if (val == s->des_min_tx_new)
return;
s->des_min_tx_new = val;
/* Postpone timer update if des_min_tx_int increases and the session is up */
if ((s->loc_state != BFD_STATE_UP) || (val < s->des_min_tx_int))
{
s->des_min_tx_int = val;
bfd_session_update_tx_interval(s);
}
bfd_session_request_poll(s, BFD_POLL_TX);
}
static void
bfd_session_set_min_rx(struct bfd_session *s, u32 val)
{
/* Note that req_min_rx_int >= req_min_rx_new */
if (val == s->req_min_rx_new)
return;
s->req_min_rx_new = val;
/* Postpone timer update if req_min_rx_int decreases and the session is up */
if ((s->loc_state != BFD_STATE_UP) || (val > s->req_min_rx_int))
{
s->req_min_rx_int = val;
bfd_session_update_detection_time(s, 0);
}
bfd_session_request_poll(s, BFD_POLL_RX);
}
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_tx_timer_hook(timer2 *t)
{
struct bfd_session *s = t->data;
s->last_tx = current_time();
// debug("TX %d\n", (s32) (s->last_tx TO_MS));
bfd_send_ctl(s->bfd, s, 0);
}
static void
bfd_hold_timer_hook(timer2 *t)
{
bfd_session_timeout(t->data);
}
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 struct bfd_session *
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));
s->addr = addr;
s->loc_id = bfd_get_free_id(p);
debug("XXX INS1 %d %d %u %I\n", p->session_hash_id.count, p->session_hash_ip.count, s->loc_id, s->addr);
HASH_INSERT(p->session_hash_id, HASH_ID, s);
debug("XXX INS2 %d %d\n", p->session_hash_id.count, p->session_hash_ip.count);
HASH_INSERT(p->session_hash_ip, HASH_IP, s);
debug("XXX INS3 %d %d\n", p->session_hash_id.count, p->session_hash_ip.count);
s->bfd = p;
/* Initialization of state variables - see RFC 5880 6.8.1 */
s->loc_state = BFD_STATE_DOWN;
s->rem_state = BFD_STATE_DOWN;
s->des_min_tx_int = s->des_min_tx_new = opts->min_tx_int; // XXX opts->idle_tx_int;
s->req_min_rx_int = s->req_min_rx_new = opts->min_rx_int;
s->rem_min_rx_int = 1;
s->detect_mult = opts->multiplier;
s->passive = opts->passive;
s->tx_timer = tm2_new_init(p->tpool, bfd_tx_timer_hook, s, 0, 0);
s->hold_timer = tm2_new_init(p->tpool, bfd_hold_timer_hook, s, 0, 0);
bfd_session_update_tx_interval(s);
birdloop_leave(p->loop);
return s;
}
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_update_state(s, BFD_STATE_DOWN, BFD_DIAG_PATH_DOWN);
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);
debug("XXX REM1 %d %d %u %I\n", p->session_hash_id.count, p->session_hash_ip.count, s->loc_id, s->addr);
HASH_REMOVE(p->session_hash_id, HASH_ID, s);
debug("XXX REM2 %d %d\n", p->session_hash_id.count, p->session_hash_ip.count);
HASH_REMOVE(p->session_hash_ip, HASH_IP, s);
debug("XXX REM3 %d %d\n", p->session_hash_id.count, p->session_hash_ip.count);
sl_free(p->session_slab, s);
birdloop_leave(p->loop);
}
static void
bfd_configure_session(struct bfd_proto *p, struct bfd_session *s,
struct bfd_session_config *opts)
{
birdloop_enter(p->loop);
// XXX opts->idle_tx_int;
bfd_session_set_min_tx(s, opts->min_tx_int);
bfd_session_set_min_rx(s, opts->min_rx_int);
s->detect_mult = opts->multiplier;
s->passive = opts->passive;
bfd_session_control_tx_timer(s);
birdloop_leave(p->loop);
}
static void
bfd_start_neighbor(struct bfd_proto *p, struct bfd_neighbor *n)
{
n->session = bfd_add_session(p, n->addr, n->opts);
if (n->opts->multihop)
{
bfd_open_session(p, n->session, n->local, NULL);
return;
}
struct neighbor *nb = neigh_find2(&p->p, &n->addr, n->iface, NEF_STICKY);
if (!nb)
{
log(L_ERR "%s: Invalid remote address %I%J", p->p.name, n->addr, n->iface);
return;
}
if (nb->data)
{
log(L_ERR "%s: Duplicate remote address %I", p->p.name, n->addr);
return;
}
nb->data = n->session;
if (nb->scope > 0)
bfd_open_session(p, n->session, nb->iface->addr->ip, nb->iface);
else
TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", n->addr, n->iface);
}
static void
bfd_stop_neighbor(struct bfd_proto *p, struct bfd_neighbor *n)
{
if (!n->opts->multihop)
{
struct neighbor *nb = neigh_find2(&p->p, &n->addr, n->iface, 0);
if (nb)
nb->data = NULL;
}
bfd_remove_session(p, n->session);
}
static void
bfd_neigh_notify(struct neighbor *nb)
{
struct bfd_proto *p = (struct bfd_proto *) nb->proto;
struct bfd_session *s = nb->data;
if (!s)
return;
if ((nb->scope > 0) && !s->opened)
bfd_open_session(p, s, nb->iface->addr->ip, nb->iface);
if ((nb->scope <= 0) && s->opened)
bfd_close_session(p, s);
}
/* This core notify code should be replaced after main loop transition to birdloop */
int pipe(int pipefd[2]);
void pipe_drain(int fd);
void pipe_kick(int fd);
static int
bfd_notify_hook(sock *sk, int len)
{
struct bfd_proto *p = sk->data;
struct bfd_session *s;
list tmp_list;
pipe_drain(sk->fd);
bfd_lock_sessions(p);
init_list(&tmp_list);
add_tail_list(&tmp_list, &p->notify_list);
init_list(&p->notify_list);
bfd_unlock_sessions(p);
WALK_LIST_FIRST(s, tmp_list)
{
bfd_lock_sessions(p);
rem2_node(&s->n);
bfd_unlock_sessions(p);
// XXX do something
TRACE(D_EVENTS, "Notify: session changed %I %d %d", s->addr, s->loc_state, s->loc_diag);
}
return 0;
}
static inline void
bfd_notify_kick(struct bfd_proto *p)
{
pipe_kick(p->notify_ws->fd);
}
static void
bfd_noterr_hook(sock *sk, int err)
{
struct bfd_proto *p = sk->data;
log(L_ERR "%s: Notify socket error: %m", p->p.name, err);
}
static void
bfd_notify_init(struct bfd_proto *p)
{
int pfds[2];
sock *sk;
int rv = pipe(pfds);
if (rv < 0)
die("pipe: %m");
sk = sk_new(p->p.pool);
sk->type = SK_MAGIC;
sk->rx_hook = bfd_notify_hook;
sk->err_hook = bfd_noterr_hook;
sk->fd = pfds[0];
sk->data = p;
if (sk_open(sk) < 0)
die("bfd: sk_open failed");
p->notify_rs = sk;
/* The write sock is not added to any event loop */
sk = sk_new(p->p.pool);
sk->type = SK_MAGIC;
sk->fd = pfds[1];
sk->data = p;
sk->flags = SKF_THREAD;
if (sk_open(sk) < 0)
die("bfd: sk_open failed");
p->notify_ws = sk;
}
static struct proto *
bfd_init(struct proto_config *c)
{
struct proto *p = proto_new(c, sizeof(struct bfd_proto));
p->neigh_notify = bfd_neigh_notify;
return p;
}
static int
bfd_start(struct proto *P)
{
struct bfd_proto *p = (struct bfd_proto *) P;
struct bfd_config *cf = (struct bfd_config *) (P->cf);
p->loop = birdloop_new(P->pool);
p->tpool = rp_new(NULL, "BFD thread root");
pthread_spin_init(&p->lock, PTHREAD_PROCESS_PRIVATE);
p->session_slab = sl_new(P->pool, sizeof(struct bfd_session));
HASH_INIT(p->session_hash_id, P->pool, 4);
HASH_INIT(p->session_hash_ip, P->pool, 4);
init_list(&p->sock_list);
birdloop_mask_wakeups(p->loop);
init_list(&p->notify_list);
bfd_notify_init(p);
birdloop_enter(p->loop);
p->rx_1 = bfd_open_rx_sk(p, 0);
p->rx_m = bfd_open_rx_sk(p, 1);
birdloop_leave(p->loop);
struct bfd_neighbor *n;
WALK_LIST(n, cf->neigh_list)
bfd_start_neighbor(p, n);
birdloop_unmask_wakeups(p->loop);
return PS_UP;
}
static int
bfd_shutdown(struct proto *P)
{
struct bfd_proto *p = (struct bfd_proto *) P;
return PS_DOWN;
}
static inline int
bfd_same_neighbor(struct bfd_neighbor *x, struct bfd_neighbor *y)
{
return ipa_equal(x->addr, y->addr) && ipa_equal(x->local, y->local) &&
(x->iface == y->iface) && (x->opts->multihop == y->opts->multihop);
}
static void
bfd_match_neighbor(struct bfd_proto *p, struct bfd_neighbor *on, struct bfd_config *new)
{
struct bfd_neighbor *nn;
WALK_LIST(nn, new->neigh_list)
if (bfd_same_neighbor(nn, on))
{
nn->session = on->session;
bfd_configure_session(p, nn->session, nn->opts);
return;
}
bfd_stop_neighbor(p, on);
}
static int
bfd_reconfigure(struct proto *P, struct proto_config *c)
{
struct bfd_proto *p = (struct bfd_proto *) P;
struct bfd_config *old = (struct bfd_config *) (P->cf);
struct bfd_config *new = (struct bfd_config *) c;
struct bfd_neighbor *n;
birdloop_mask_wakeups(p->loop);
WALK_LIST(n, old->neigh_list)
bfd_match_neighbor(p, n, new);
WALK_LIST(n, new->neigh_list)
if (!n->session)
bfd_start_neighbor(p, n);
birdloop_unmask_wakeups(p->loop);
return 1;
}
static void
bfd_copy_config(struct proto_config *dest, struct proto_config *src)
{
struct bfd_config *d = (struct bfd_config *) dest;
// struct bfd_config *s = (struct bfd_config *) src;
/* We clean up neigh_list, ifaces are non-sharable */
init_list(&d->neigh_list);
}
void
bfd_show_sessions(struct proto *P)
{
struct bfd_proto *p = (struct bfd_proto *) P;
uint state, diag;
u32 tx_int, timeout;
const char *ifname;
if (p->p.proto_state != PS_UP)
{
cli_msg(-1013, "%s: is not up", p->p.name);
cli_msg(0, "");
return;
}
cli_msg(-1013, "%s:", p->p.name);
cli_msg(-1013, "%-12s\t%s\t%s\t%s\t%s", "Router IP", "Iface",
"State", "TX Int", "Timeout");
debug("XXX WALK %d %d\n", p->session_hash_id.count, p->session_hash_ip.count);
HASH_WALK(p->session_hash_id, next_id, s)
{
// FIXME this is unsafe
state = s->loc_state;
diag = s->loc_diag;
ifname = (s->bsock && s->bsock->sk->iface) ? s->bsock->sk->iface->name : "---";
tx_int = (MAX(s->des_min_tx_int, s->rem_min_rx_int) TO_MS);
timeout = (MAX(s->req_min_rx_int, s->rem_min_tx_int) TO_MS) * s->rem_detect_mult;
cli_msg(-1013, "%I\t%s\t%d %d\t%u\t%u",
s->addr, ifname, state, diag, tx_int, timeout);
}
HASH_WALK_END;
cli_msg(0, "");
}
struct protocol proto_bfd = {
.name = "BFD",
.template = "bfd%d",
.init = bfd_init,
.start = bfd_start,
.shutdown = bfd_shutdown,
.reconfigure = bfd_reconfigure,
.copy_config = bfd_copy_config,
};
|