Statistics
| Branch: | Revision:

iof-bird-daemon / proto / bfd / bfd.c @ 864f52a5

History | View | Annotate | Download (27.9 KB)

1
/*
2
 *        BIRD -- Bidirectional Forwarding Detection (BFD)
3
 *
4
 *        Can be freely distributed and used under the terms of the GNU GPL.
5
 */
6

    
7
/**
8
 * DOC: Bidirectional Forwarding Detection
9
 *
10
 * The BFD protocol is implemented in three files: |bfd.c| containing the
11
 * protocol logic and the protocol glue with BIRD core, |packets.c| handling BFD
12
 * packet processing, RX, TX and protocol sockets. |io.c| then contains generic
13
 * code for the event loop, threads and event sources (sockets, microsecond
14
 * timers). This generic code will be merged to the main BIRD I/O code in the
15
 * future.
16
 *
17
 * The BFD implementation uses a separate thread with an internal event loop for
18
 * handling the protocol logic, which requires high-res and low-latency timing,
19
 * so it is not affected by the rest of BIRD, which has several low-granularity
20
 * hooks in the main loop, uses second-based timers and cannot offer good
21
 * latency. The core of BFD protocol (the code related to BFD sessions,
22
 * interfaces and packets) runs in the BFD thread, while the rest (the code
23
 * related to BFD requests, BFD neighbors and the protocol glue) runs in the
24
 * main thread.
25
 *
26
 * BFD sessions are represented by structure &bfd_session that contains a state
27
 * related to the session and two timers (TX timer for periodic packets and hold
28
 * timer for session timeout). These sessions are allocated from @session_slab
29
 * and are accessible by two hash tables, @session_hash_id (by session ID) and
30
 * @session_hash_ip (by IP addresses of neighbors). Slab and both hashes are in
31
 * the main protocol structure &bfd_proto. The protocol logic related to BFD
32
 * sessions is implemented in internal functions bfd_session_*(), which are
33
 * expected to be called from the context of BFD thread, and external functions
34
 * bfd_add_session(), bfd_remove_session() and bfd_reconfigure_session(), which
35
 * form an interface to the BFD core for the rest and are expected to be called
36
 * from the context of main thread.
37
 *
38
 * Each BFD session has an associated BFD interface, represented by structure
39
 * &bfd_iface. A BFD interface contains a socket used for TX (the one for RX is
40
 * shared in &bfd_proto), an interface configuration and reference counter.
41
 * Compared to interface structures of other protocols, these structures are not
42
 * created and removed based on interface notification events, but according to
43
 * the needs of BFD sessions. When a new session is created, it requests a
44
 * proper BFD interface by function bfd_get_iface(), which either finds an
45
 * existing one in &iface_list (from &bfd_proto) or allocates a new one. When a
46
 * session is removed, an associated iface is dicharged by bfd_free_iface().
47
 *
48
 * BFD requests are the external API for the other protocols. When a protocol
49
 * wants a BFD session, it calls bfd_request_session(), which creates a
50
 * structure &bfd_request containing approprite information and an notify hook.
51
 * This structure is a resource associated with the caller's resource pool. When
52
 * a BFD protocol is available, a BFD request is submitted to the protocol, an
53
 * appropriate BFD session is found or created and the request is attached to
54
 * the session. When a session changes state, all attached requests (and related
55
 * protocols) are notified. Note that BFD requests do not depend on BFD protocol
56
 * running. When the BFD protocol is stopped or removed (or not available from
57
 * beginning), related BFD requests are stored in @bfd_wait_list, where waits
58
 * for a new protocol.
59
 *
60
 * BFD neighbors are just a way to statically configure BFD sessions without
61
 * requests from other protocol. Structures &bfd_neighbor are part of BFD
62
 * configuration (like static routes in the static protocol). BFD neighbors are
63
 * handled by BFD protocol like it is a BFD client -- when a BFD neighbor is
64
 * ready, the protocol just creates a BFD request like any other protocol.
65
 * 
66
 * The protocol uses a new generic event loop (structure &birdloop) from |io.c|,
67
 * which supports sockets, timers and events like the main loop. Timers
68
 * (structure &timer2) are new microsecond based timers, while sockets and
69
 * events are the same. A birdloop is associated with a thread (field @thread)
70
 * in which event hooks are executed. Most functions for setting event sources
71
 * (like sk_start() or tm2_start()) must be called from the context of that
72
 * thread. Birdloop allows to temporarily acquire the context of that thread for
73
 * the main thread by calling birdloop_enter() and then birdloop_leave(), which
74
 * also ensures mutual exclusion with all event hooks. Note that resources
75
 * associated with a birdloop (like timers) should be attached to the
76
 * independent resource pool, detached from the main resource tree.
77
 *
78
 * There are two kinds of interaction between the BFD core (running in the BFD
79
 * thread) and the rest of BFD (running in the main thread). The first kind are
80
 * configuration calls from main thread to the BFD thread (like bfd_add_session()).
81
 * These calls are synchronous and use birdloop_enter() mechanism for mutual
82
 * exclusion. The second kind is a notification about session changes from the
83
 * BFD thread to the main thread. This is done in an asynchronous way, sesions
84
 * with pending notifications are linked (in the BFD thread) to @notify_list in
85
 * &bfd_proto, and then bfd_notify_hook() in the main thread is activated using
86
 * bfd_notify_kick() and a pipe. The hook then processes scheduled sessions and
87
 * calls hooks from associated BFD requests. This @notify_list (and state fields
88
 * in structure &bfd_session) is protected by a spinlock in &bfd_proto and
89
 * functions bfd_lock_sessions() / bfd_unlock_sessions().
90
 *
91
 * There are few data races (accessing @p->p.debug from TRACE() from the BFD
92
 * thread and accessing some some private fields of %bfd_session from
93
 * bfd_show_sessions() from the main thread, but these are harmless (i hope).
94
 *
95
 * TODO: document functions and access restrictions for fields in BFD structures.
96
 *
97
 * Supported standards:
98
 * - RFC 5880 - main BFD standard
99
 * - RFC 5881 - BFD for IP links
100
 * - RFC 5882 - generic application of BFD
101
 * - RFC 5883 - BFD for multihop paths
102
 */
103

    
104
#include "bfd.h"
105

    
106

    
107
#define HASH_ID_KEY(n)                n->loc_id
108
#define HASH_ID_NEXT(n)                n->next_id
109
#define HASH_ID_EQ(a,b)                a == b
110
#define HASH_ID_FN(k)                k
111

    
112
#define HASH_IP_KEY(n)                n->addr
113
#define HASH_IP_NEXT(n)                n->next_ip
114
#define HASH_IP_EQ(a,b)                ipa_equal(a,b)
115
#define HASH_IP_FN(k)                ipa_hash32(k)
116

    
117
static list bfd_proto_list;
118
static list bfd_wait_list;
119

    
120
const char *bfd_state_names[] = { "AdminDown", "Down", "Init", "Up" };
121

    
122
static void bfd_session_set_min_tx(struct bfd_session *s, u32 val);
123
static struct bfd_iface *bfd_get_iface(struct bfd_proto *p, ip_addr local, struct iface *iface);
124
static void bfd_free_iface(struct bfd_iface *ifa);
125
static inline void bfd_notify_kick(struct bfd_proto *p);
126

    
127

    
128
/*
129
 *        BFD sessions
130
 */
131

    
132
static void 
133
bfd_session_update_state(struct bfd_session *s, uint state, uint diag)
134
{
135
  struct bfd_proto *p = s->ifa->bfd;
136
  uint old_state = s->loc_state; 
137
  int notify;
138

    
139
  if (state == old_state)
140
    return;
141

    
142
  TRACE(D_EVENTS, "Session to %I changed state from %s to %s",
143
        s->addr, bfd_state_names[old_state], bfd_state_names[state]);
144

    
145
  bfd_lock_sessions(p);
146
  s->loc_state = state;
147
  s->loc_diag = diag;
148

    
149
  notify = !NODE_VALID(&s->n);
150
  if (notify)
151
    add_tail(&p->notify_list, &s->n);
152
  bfd_unlock_sessions(p);
153

    
154
  if (state == BFD_STATE_UP)
155
    bfd_session_set_min_tx(s, s->ifa->cf->min_tx_int);
156

    
157
  if (old_state == BFD_STATE_UP)
158
    bfd_session_set_min_tx(s, s->ifa->cf->idle_tx_int);
159

    
160
  if (notify)
161
    bfd_notify_kick(p);
162
}
163

    
164
static void
165
bfd_session_update_tx_interval(struct bfd_session *s)
166
{
167
  u32 tx_int = MAX(s->des_min_tx_int, s->rem_min_rx_int);
168
  u32 tx_int_l = tx_int - (tx_int / 4);         // 75 %
169
  u32 tx_int_h = tx_int - (tx_int / 10); // 90 %
170

    
171
  s->tx_timer->recurrent = tx_int_l;
172
  s->tx_timer->randomize = tx_int_h - tx_int_l;
173

    
174
  /* Do not set timer if no previous event */
175
  if (!s->last_tx)
176
    return;
177

    
178
  /* Set timer relative to last tx_timer event */
179
  tm2_set(s->tx_timer, s->last_tx + tx_int_l);
180
}
181

    
182
static void
183
bfd_session_update_detection_time(struct bfd_session *s, int kick)
184
{
185
  btime timeout = (btime) MAX(s->req_min_rx_int, s->rem_min_tx_int) * s->rem_detect_mult;
186

    
187
  if (kick)
188
    s->last_rx = current_time();
189

    
190
  if (!s->last_rx)
191
    return;
192

    
193
  tm2_set(s->hold_timer, s->last_rx + timeout);
194
}
195

    
196
static void
197
bfd_session_control_tx_timer(struct bfd_session *s, int reset)
198
{
199
  // if (!s->opened) goto stop;
200

    
201
  if (s->passive && (s->rem_id == 0))
202
    goto stop;
203

    
204
  if (s->rem_demand_mode && 
205
      !s->poll_active && 
206
      (s->loc_state == BFD_STATE_UP) &&
207
      (s->rem_state == BFD_STATE_UP))
208
    goto stop;
209

    
210
  if (s->rem_min_rx_int == 0)
211
    goto stop;
212

    
213
  /* So TX timer should run */
214
  if (reset || !tm2_active(s->tx_timer))
215
  {
216
    s->last_tx = 0;
217
    tm2_start(s->tx_timer, 0);
218
  }
219

    
220
  return;
221

    
222
 stop:
223
  tm2_stop(s->tx_timer);
224
  s->last_tx = 0;
225
}
226

    
227
static void
228
bfd_session_request_poll(struct bfd_session *s, u8 request)
229
{
230
  /* Not sure about this, but doing poll in this case does not make sense */
231
  if (s->rem_id == 0)
232
    return;
233

    
234
  s->poll_scheduled |= request;
235

    
236
  if (s->poll_active)
237
    return;
238

    
239
  s->poll_active = s->poll_scheduled;
240
  s->poll_scheduled = 0;
241

    
242
  bfd_session_control_tx_timer(s, 1);
243
}
244

    
245
static void
246
bfd_session_terminate_poll(struct bfd_session *s)
247
{
248
  u8 poll_done = s->poll_active & ~s->poll_scheduled;
249

    
250
  if (poll_done & BFD_POLL_TX)
251
    s->des_min_tx_int = s->des_min_tx_new;
252

    
253
  if (poll_done & BFD_POLL_RX)
254
    s->req_min_rx_int = s->req_min_rx_new;
255

    
256
  s->poll_active = s->poll_scheduled;
257
  s->poll_scheduled = 0;
258

    
259
  /* Timers are updated by caller - bfd_session_process_ctl() */
260
}
261

    
262
void
263
bfd_session_process_ctl(struct bfd_session *s, u8 flags, u32 old_tx_int, u32 old_rx_int)
264
{
265
  if (s->poll_active && (flags & BFD_FLAG_FINAL))
266
    bfd_session_terminate_poll(s);
267

    
268
  if ((s->des_min_tx_int != old_tx_int) || (s->rem_min_rx_int != old_rx_int))
269
    bfd_session_update_tx_interval(s);
270

    
271
  bfd_session_update_detection_time(s, 1);
272

    
273
  /* Update session state */
274
  int next_state = 0;
275
  int diag = BFD_DIAG_NOTHING;
276

    
277
  switch (s->loc_state)
278
  {
279
  case BFD_STATE_ADMIN_DOWN:
280
    return;
281

    
282
  case BFD_STATE_DOWN:
283
    if (s->rem_state == BFD_STATE_DOWN)                next_state = BFD_STATE_INIT;
284
    else if (s->rem_state == BFD_STATE_INIT)        next_state = BFD_STATE_UP;
285
    break;
286

    
287
  case BFD_STATE_INIT:
288
    if (s->rem_state == BFD_STATE_ADMIN_DOWN)        next_state = BFD_STATE_DOWN, diag = BFD_DIAG_NEIGHBOR_DOWN;
289
    else if (s->rem_state >= BFD_STATE_INIT)        next_state = BFD_STATE_UP;
290
    break;
291

    
292
  case BFD_STATE_UP:
293
    if (s->rem_state <= BFD_STATE_DOWN)                next_state = BFD_STATE_DOWN, diag = BFD_DIAG_NEIGHBOR_DOWN;
294
    break;
295
  }
296

    
297
  if (next_state)
298
    bfd_session_update_state(s, next_state, diag);
299

    
300
  bfd_session_control_tx_timer(s, 0);
301

    
302
  if (flags & BFD_FLAG_POLL)
303
    bfd_send_ctl(s->ifa->bfd, s, 1);
304
}
305

    
306
static void 
307
bfd_session_timeout(struct bfd_session *s)
308
{
309
  struct bfd_proto *p = s->ifa->bfd;
310

    
311
  TRACE(D_EVENTS, "Session to %I expired", s->addr);
312

    
313
  s->rem_state = BFD_STATE_DOWN;
314
  s->rem_id = 0;
315
  s->rem_min_tx_int = 0;
316
  s->rem_min_rx_int = 1;
317
  s->rem_demand_mode = 0;
318
  s->rem_detect_mult = 0;
319

    
320
  s->poll_active = 0;
321
  s->poll_scheduled = 0;
322

    
323
  bfd_session_update_state(s, BFD_STATE_DOWN, BFD_DIAG_TIMEOUT);
324

    
325
  bfd_session_control_tx_timer(s, 1);
326
}
327

    
328
static void
329
bfd_session_set_min_tx(struct bfd_session *s, u32 val)
330
{
331
  /* Note that des_min_tx_int <= des_min_tx_new */
332

    
333
  if (val == s->des_min_tx_new)
334
    return;
335

    
336
  s->des_min_tx_new = val;
337

    
338
  /* Postpone timer update if des_min_tx_int increases and the session is up */
339
  if ((s->loc_state != BFD_STATE_UP) || (val < s->des_min_tx_int))
340
  {
341
    s->des_min_tx_int = val;
342
    bfd_session_update_tx_interval(s);
343
  }
344

    
345
  bfd_session_request_poll(s, BFD_POLL_TX);
346
}
347

    
348
static void
349
bfd_session_set_min_rx(struct bfd_session *s, u32 val)
350
{
351
  /* Note that req_min_rx_int >= req_min_rx_new */
352

    
353
  if (val == s->req_min_rx_new)
354
    return;
355

    
356
  s->req_min_rx_new = val; 
357

    
358
  /* Postpone timer update if req_min_rx_int decreases and the session is up */
359
  if ((s->loc_state != BFD_STATE_UP) || (val > s->req_min_rx_int))
360
  {
361
    s->req_min_rx_int = val;
362
    bfd_session_update_detection_time(s, 0);
363
  }
364

    
365
  bfd_session_request_poll(s, BFD_POLL_RX);
366
}
367

    
368
struct bfd_session *
369
bfd_find_session_by_id(struct bfd_proto *p, u32 id)
370
{
371
  return HASH_FIND(p->session_hash_id, HASH_ID, id);
372
}
373

    
374
struct bfd_session *
375
bfd_find_session_by_addr(struct bfd_proto *p, ip_addr addr)
376
{
377
  return HASH_FIND(p->session_hash_ip, HASH_IP, addr);
378
}
379

    
380
static void
381
bfd_tx_timer_hook(timer2 *t)
382
{
383
  struct bfd_session *s = t->data;
384

    
385
  s->last_tx = current_time();
386
  bfd_send_ctl(s->ifa->bfd, s, 0);
387
}
388

    
389
static void
390
bfd_hold_timer_hook(timer2 *t)
391
{
392
  bfd_session_timeout(t->data);
393
}
394

    
395
static u32
396
bfd_get_free_id(struct bfd_proto *p)
397
{
398
  u32 id;
399
  for (id = random_u32(); 1; id++)
400
    if (id && !bfd_find_session_by_id(p, id))
401
      break;
402

    
403
  return id;
404
}
405

    
406
static struct bfd_session *
407
bfd_add_session(struct bfd_proto *p, ip_addr addr, ip_addr local, struct iface *iface)
408
{
409
  birdloop_enter(p->loop);
410

    
411
  struct bfd_iface *ifa = bfd_get_iface(p, local, iface);
412

    
413
  struct bfd_session *s = sl_alloc(p->session_slab);
414
  bzero(s, sizeof(struct bfd_session));
415

    
416
  s->addr = addr;
417
  s->ifa = ifa;
418
  s->loc_id = bfd_get_free_id(p);
419

    
420
  HASH_INSERT(p->session_hash_id, HASH_ID, s);
421
  HASH_INSERT(p->session_hash_ip, HASH_IP, s);
422

    
423

    
424
  /* Initialization of state variables - see RFC 5880 6.8.1 */
425
  s->loc_state = BFD_STATE_DOWN;
426
  s->rem_state = BFD_STATE_DOWN;
427
  s->des_min_tx_int = s->des_min_tx_new = ifa->cf->idle_tx_int;
428
  s->req_min_rx_int = s->req_min_rx_new = ifa->cf->min_rx_int;
429
  s->rem_min_rx_int = 1;
430
  s->detect_mult = ifa->cf->multiplier;
431
  s->passive = ifa->cf->passive;
432

    
433
  s->tx_timer = tm2_new_init(p->tpool, bfd_tx_timer_hook, s, 0, 0);
434
  s->hold_timer = tm2_new_init(p->tpool, bfd_hold_timer_hook, s, 0, 0);
435
  bfd_session_update_tx_interval(s);
436
  bfd_session_control_tx_timer(s, 1);
437

    
438
  init_list(&s->request_list);
439
  s->last_state_change = now;
440

    
441
  TRACE(D_EVENTS, "Session to %I added", s->addr);
442

    
443
  birdloop_leave(p->loop);
444

    
445
  return s;
446
}
447

    
448
/*
449
static void
450
bfd_open_session(struct bfd_proto *p, struct bfd_session *s, ip_addr local, struct iface *ifa)
451
{
452
  birdloop_enter(p->loop);
453

454
  s->opened = 1;
455

456
  bfd_session_control_tx_timer(s);
457

458
  birdloop_leave(p->loop);
459
}
460

461
static void
462
bfd_close_session(struct bfd_proto *p, struct bfd_session *s)
463
{
464
  birdloop_enter(p->loop);
465

466
  s->opened = 0;
467

468
  bfd_session_update_state(s, BFD_STATE_DOWN, BFD_DIAG_PATH_DOWN);
469
  bfd_session_control_tx_timer(s);
470

471
  birdloop_leave(p->loop);
472
}
473
*/
474

    
475
static void
476
bfd_remove_session(struct bfd_proto *p, struct bfd_session *s)
477
{
478
  ip_addr ip = s->addr;
479

    
480
  /* Caller should ensure that request list is empty */
481

    
482
  birdloop_enter(p->loop);
483

    
484
  /* Remove session from notify list if scheduled for notification */
485
  /* No need for bfd_lock_sessions(), we are already protected by birdloop_enter() */
486
  if (NODE_VALID(&s->n))
487
    rem_node(&s->n);
488

    
489
  bfd_free_iface(s->ifa);
490

    
491
  rfree(s->tx_timer);
492
  rfree(s->hold_timer);
493

    
494
  HASH_REMOVE(p->session_hash_id, HASH_ID, s);
495
  HASH_REMOVE(p->session_hash_ip, HASH_IP, s);
496

    
497
  sl_free(p->session_slab, s);
498

    
499
  TRACE(D_EVENTS, "Session to %I removed", ip);
500

    
501
  birdloop_leave(p->loop);
502
}
503

    
504
static void
505
bfd_reconfigure_session(struct bfd_proto *p, struct bfd_session *s)
506
{
507
  birdloop_enter(p->loop);
508

    
509
  struct bfd_iface_config *cf = s->ifa->cf;
510

    
511
  u32 tx = (s->loc_state == BFD_STATE_UP) ? cf->min_tx_int : cf->idle_tx_int;
512
  bfd_session_set_min_tx(s, tx);
513
  bfd_session_set_min_rx(s, cf->min_rx_int);
514
  s->detect_mult = cf->multiplier;
515
  s->passive = cf->passive;
516

    
517
  bfd_session_control_tx_timer(s, 0);
518

    
519
  birdloop_leave(p->loop);
520

    
521
  TRACE(D_EVENTS, "Session to %I reconfigured", s->addr);
522
}
523

    
524

    
525
/*
526
 *        BFD interfaces
527
 */
528

    
529
static struct bfd_iface_config bfd_default_iface = {
530
  .min_rx_int = BFD_DEFAULT_MIN_RX_INT,
531
  .min_tx_int = BFD_DEFAULT_MIN_TX_INT,
532
  .idle_tx_int = BFD_DEFAULT_IDLE_TX_INT,
533
  .multiplier = BFD_DEFAULT_MULTIPLIER
534
};
535

    
536
static inline struct bfd_iface_config *
537
bfd_find_iface_config(struct bfd_config *cf, struct iface *iface)
538
{
539
  struct bfd_iface_config *ic;
540

    
541
  ic = iface ? (void *) iface_patt_find(&cf->patt_list, iface, NULL) : cf->multihop;
542

    
543
  return ic ? ic : &bfd_default_iface;
544
}
545

    
546
static struct bfd_iface *
547
bfd_get_iface(struct bfd_proto *p, ip_addr local, struct iface *iface)
548
{
549
  struct bfd_iface *ifa;
550

    
551
  WALK_LIST(ifa, p->iface_list)
552
    if (ipa_equal(ifa->local, local) && (ifa->iface == iface))
553
      return ifa->uc++, ifa;
554

    
555
  struct bfd_config *cf = (struct bfd_config *) (p->p.cf);
556
  struct bfd_iface_config *ic = bfd_find_iface_config(cf, iface);
557

    
558
  ifa = mb_allocz(p->tpool, sizeof(struct bfd_iface));
559
  ifa->local = local;
560
  ifa->iface = iface;
561
  ifa->cf = ic;
562
  ifa->bfd = p;
563

    
564
  ifa->sk = bfd_open_tx_sk(p, local, iface);
565
  ifa->uc = 1;
566

    
567
  add_tail(&p->iface_list, &ifa->n);
568

    
569
  return ifa;
570
}
571

    
572
static void
573
bfd_free_iface(struct bfd_iface *ifa)
574
{
575
  if (!ifa || --ifa->uc)
576
    return;
577

    
578
  rem_node(&ifa->n);
579
  sk_stop(ifa->sk);
580
  rfree(ifa->sk);
581
  mb_free(ifa);
582
}
583

    
584
static void
585
bfd_reconfigure_iface(struct bfd_proto *p, struct bfd_iface *ifa, struct bfd_config *nc)
586
{
587
  struct bfd_iface_config *nic = bfd_find_iface_config(nc, ifa->iface);
588
  ifa->changed = !!memcmp(nic, ifa->cf, sizeof(struct bfd_iface_config));
589

    
590
  /* This should be probably changed to not access ifa->cf from the BFD thread */
591
  birdloop_enter(p->loop);
592
  ifa->cf = nic;
593
  birdloop_leave(p->loop);
594
}
595

    
596

    
597
/*
598
 *        BFD requests
599
 */
600

    
601
static void
602
bfd_request_notify(struct bfd_request *req, u8 state, u8 diag)
603
{
604
  u8 old_state = req->state;
605

    
606
  if (state == old_state)
607
    return;
608

    
609
  req->state = state;
610
  req->diag = diag;
611
  req->old_state = old_state;
612
  req->down = (old_state == BFD_STATE_UP) && (state == BFD_STATE_DOWN);
613

    
614
  if (req->hook)
615
    req->hook(req);
616
}
617

    
618
static int
619
bfd_add_request(struct bfd_proto *p, struct bfd_request *req)
620
{
621
  struct bfd_session *s = bfd_find_session_by_addr(p, req->addr);
622
  u8 state, diag;
623

    
624
  if (!s)
625
    s = bfd_add_session(p, req->addr, req->local, req->iface);
626

    
627
  rem_node(&req->n);
628
  add_tail(&s->request_list, &req->n);
629
  req->session = s;
630

    
631
  bfd_lock_sessions(p);
632
  state = s->loc_state;
633
  diag = s->loc_diag;
634
  bfd_unlock_sessions(p);
635

    
636
  bfd_request_notify(req, state, diag);
637

    
638
  return 1;
639
}
640

    
641
static void
642
bfd_submit_request(struct bfd_request *req)
643
{
644
  node *n;
645

    
646
  WALK_LIST(n, bfd_proto_list)
647
    if (bfd_add_request(SKIP_BACK(struct bfd_proto, bfd_node, n), req))
648
      return;
649

    
650
  rem_node(&req->n);
651
  add_tail(&bfd_wait_list, &req->n);
652
  req->session = NULL;
653
  bfd_request_notify(req, BFD_STATE_ADMIN_DOWN, 0);
654
}
655

    
656
static void
657
bfd_take_requests(struct bfd_proto *p)
658
{
659
  node *n, *nn;
660

    
661
  WALK_LIST_DELSAFE(n, nn, bfd_wait_list)
662
    bfd_add_request(p, SKIP_BACK(struct bfd_request, n, n));
663
}
664

    
665
static void
666
bfd_drop_requests(struct bfd_proto *p)
667
{
668
  node *n;
669

    
670
  HASH_WALK(p->session_hash_id, next_id, s)
671
  {
672
    /* We assume that p is not in bfd_proto_list */
673
    WALK_LIST_FIRST(n, s->request_list)
674
      bfd_submit_request(SKIP_BACK(struct bfd_request, n, n));
675
  }
676
  HASH_WALK_END;
677
}
678

    
679
static struct resclass bfd_request_class;
680

    
681
struct bfd_request *
682
bfd_request_session(pool *p, ip_addr addr, ip_addr local, struct iface *iface,
683
                    void (*hook)(struct bfd_request *), void *data)
684
{
685
  struct bfd_request *req = ralloc(p, &bfd_request_class);
686

    
687
  /* Hack: self-link req->n, we will call rem_node() on it */
688
  req->n.prev = req->n.next = &req->n;
689

    
690
  req->addr = addr;
691
  req->local = local;
692
  req->iface = iface;
693

    
694
  bfd_submit_request(req);
695

    
696
  req->hook = hook;
697
  req->data = data;
698

    
699
  return req;
700
}
701

    
702
static void
703
bfd_request_free(resource *r)
704
{
705
  struct bfd_request *req = (struct bfd_request *) r;
706
  struct bfd_session *s = req->session;
707

    
708
  rem_node(&req->n);
709

    
710
  /* Remove the session if there is no request for it. Skip that if
711
     inside notify hooks, will be handled by bfd_notify_hook() itself */
712

    
713
  if (s && EMPTY_LIST(s->request_list) && !s->notify_running)
714
    bfd_remove_session(s->ifa->bfd, s);
715
}
716

    
717
static void
718
bfd_request_dump(resource *r)
719
{
720
  struct bfd_request *req = (struct bfd_request *) r;
721

    
722
  debug("(code %p, data %p)\n", req->hook, req->data);
723
}
724

    
725
static struct resclass bfd_request_class = {
726
  "BFD request",
727
  sizeof(struct bfd_request),
728
  bfd_request_free,
729
  bfd_request_dump,
730
  NULL,
731
  NULL
732
};
733

    
734

    
735
/*
736
 *        BFD neighbors
737
 */
738

    
739
static void
740
bfd_neigh_notify(struct neighbor *nb)
741
{
742
  struct bfd_proto *p = (struct bfd_proto *) nb->proto;
743
  struct bfd_neighbor *n = nb->data;
744

    
745
  if (!n)
746
    return;
747

    
748
  if ((nb->scope > 0) && !n->req)
749
  {
750
    ip_addr local = ipa_nonzero(n->local) ? n->local : nb->ifa->ip;
751
    n->req = bfd_request_session(p->p.pool, n->addr, local, nb->iface, NULL, NULL);
752
  }
753

    
754
  if ((nb->scope <= 0) && n->req)
755
  {
756
    rfree(n->req);
757
    n->req = NULL;
758
  }
759
}
760

    
761
static void
762
bfd_start_neighbor(struct bfd_proto *p, struct bfd_neighbor *n)
763
{
764
  n->active = 1;
765

    
766
  if (n->multihop)
767
  {
768
    n->req = bfd_request_session(p->p.pool, n->addr, n->local, NULL, NULL, NULL);
769
    return;
770
  }
771

    
772
  struct neighbor *nb = neigh_find2(&p->p, &n->addr, n->iface, NEF_STICKY);
773
  if (!nb)
774
  {
775
    log(L_ERR "%s: Invalid remote address %I%J", p->p.name, n->addr, n->iface);
776
    return;
777
  }
778

    
779
  if (nb->data)
780
  {
781
    log(L_ERR "%s: Duplicate neighbor %I", p->p.name, n->addr);
782
    return;
783
  }
784

    
785
  n->neigh = nb;
786
  nb->data = n;
787

    
788
  if (nb->scope > 0)
789
    bfd_neigh_notify(nb);
790
  else
791
    TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", n->addr, n->iface);
792
}
793

    
794
static void
795
bfd_stop_neighbor(struct bfd_proto *p, struct bfd_neighbor *n)
796
{
797
  if (n->neigh)
798
    n->neigh->data = NULL;
799
  n->neigh = NULL;
800

    
801
  rfree(n->req);
802
  n->req = NULL;
803
}
804

    
805
static inline int
806
bfd_same_neighbor(struct bfd_neighbor *x, struct bfd_neighbor *y)
807
{
808
  return ipa_equal(x->addr, y->addr) && ipa_equal(x->local, y->local) &&
809
    (x->iface == y->iface) && (x->multihop == y->multihop);
810
}
811

    
812
static void
813
bfd_reconfigure_neighbors(struct bfd_proto *p, struct bfd_config *new)
814
{
815
  struct bfd_config *old = (struct bfd_config *) (p->p.cf);
816
  struct bfd_neighbor *on, *nn;
817

    
818
  WALK_LIST(on, old->neigh_list)
819
  {
820
    WALK_LIST(nn, new->neigh_list)
821
      if (bfd_same_neighbor(nn, on))
822
      {
823
        nn->neigh = on->neigh;
824
        if (nn->neigh)
825
          nn->neigh->data = nn;
826

    
827
        nn->req = on->req;
828
        nn->active = 1;
829
        return;
830
      }
831

    
832
    bfd_stop_neighbor(p, on);
833
  }
834

    
835
  WALK_LIST(nn, new->neigh_list)
836
    if (!nn->active)
837
      bfd_start_neighbor(p, nn);
838
}
839

    
840

    
841
/*
842
 *        BFD notify socket
843
 */
844

    
845
/* This core notify code should be replaced after main loop transition to birdloop */
846

    
847
int pipe(int pipefd[2]);
848
void pipe_drain(int fd);
849
void pipe_kick(int fd);
850

    
851
static int
852
bfd_notify_hook(sock *sk, int len)
853
{
854
  struct bfd_proto *p = sk->data;
855
  struct bfd_session *s;
856
  list tmp_list;
857
  u8 state, diag;
858
  node *n, *nn;
859

    
860
  pipe_drain(sk->fd);
861

    
862
  bfd_lock_sessions(p);
863
  init_list(&tmp_list);
864
  add_tail_list(&tmp_list, &p->notify_list);
865
  init_list(&p->notify_list);
866
  bfd_unlock_sessions(p);
867

    
868
  WALK_LIST_FIRST(s, tmp_list)
869
  {
870
    bfd_lock_sessions(p);
871
    rem2_node(&s->n);
872
    state = s->loc_state;
873
    diag = s->loc_diag;
874
    bfd_unlock_sessions(p);
875

    
876
    /* FIXME: convert to btime and move to bfd_session_update_state() */ 
877
    s->last_state_change = now;
878

    
879
    s->notify_running = 1;
880
    WALK_LIST_DELSAFE(n, nn, s->request_list)
881
      bfd_request_notify(SKIP_BACK(struct bfd_request, n, n), state, diag);
882
    s->notify_running = 0;
883

    
884
    /* Remove the session if all requests were removed in notify hooks */
885
    if (EMPTY_LIST(s->request_list))
886
      bfd_remove_session(p, s);
887
  }
888

    
889
  return 0;
890
}
891

    
892
static inline void
893
bfd_notify_kick(struct bfd_proto *p)
894
{
895
  pipe_kick(p->notify_ws->fd);
896
}
897

    
898
static void
899
bfd_noterr_hook(sock *sk, int err)
900
{
901
  struct bfd_proto *p = sk->data;
902
  log(L_ERR "%s: Notify socket error: %m", p->p.name, err);
903
}
904

    
905
static void
906
bfd_notify_init(struct bfd_proto *p)
907
{
908
  int pfds[2];
909
  sock *sk;
910

    
911
  int rv = pipe(pfds);
912
  if (rv < 0)
913
    die("pipe: %m");
914

    
915
  sk = sk_new(p->p.pool);
916
  sk->type = SK_MAGIC;
917
  sk->rx_hook = bfd_notify_hook;
918
  sk->err_hook = bfd_noterr_hook;
919
  sk->fd = pfds[0];
920
  sk->data = p;
921
  if (sk_open(sk) < 0)
922
    die("bfd: sk_open failed");
923
  p->notify_rs = sk;
924

    
925
  /* The write sock is not added to any event loop */
926
  sk = sk_new(p->p.pool);
927
  sk->type = SK_MAGIC;
928
  sk->fd = pfds[1];
929
  sk->data = p;
930
  sk->flags = SKF_THREAD;
931
  if (sk_open(sk) < 0)
932
    die("bfd: sk_open failed");
933
  p->notify_ws = sk;
934
}
935

    
936

    
937
/*
938
 *        BFD protocol glue
939
 */
940

    
941
void
942
bfd_init_all(void)
943
{
944
  init_list(&bfd_proto_list);
945
  init_list(&bfd_wait_list);
946
}
947

    
948
static struct proto *
949
bfd_init(struct proto_config *c)
950
{
951
  struct proto *p = proto_new(c, sizeof(struct bfd_proto));
952

    
953
  p->neigh_notify = bfd_neigh_notify;
954

    
955
  return p;
956
}
957

    
958
static int
959
bfd_start(struct proto *P)
960
{
961
  struct bfd_proto *p = (struct bfd_proto *) P;
962
  struct bfd_config *cf = (struct bfd_config *) (P->cf);
963

    
964
  p->loop = birdloop_new();
965
  p->tpool = rp_new(NULL, "BFD thread root");
966
  pthread_spin_init(&p->lock, PTHREAD_PROCESS_PRIVATE);
967

    
968
  p->session_slab = sl_new(P->pool, sizeof(struct bfd_session));
969
  HASH_INIT(p->session_hash_id, P->pool, 8);
970
  HASH_INIT(p->session_hash_ip, P->pool, 8);
971

    
972
  init_list(&p->iface_list);
973

    
974
  init_list(&p->notify_list);
975
  bfd_notify_init(p);
976

    
977
  add_tail(&bfd_proto_list, &p->bfd_node);
978

    
979
  birdloop_enter(p->loop);
980
  p->rx_1 = bfd_open_rx_sk(p, 0);
981
  p->rx_m = bfd_open_rx_sk(p, 1);
982
  birdloop_leave(p->loop);
983

    
984
  bfd_take_requests(p);
985

    
986
  struct bfd_neighbor *n;
987
  WALK_LIST(n, cf->neigh_list)
988
    bfd_start_neighbor(p, n);
989

    
990
  birdloop_start(p->loop);
991

    
992
  return PS_UP;
993
}
994

    
995

    
996
static int
997
bfd_shutdown(struct proto *P)
998
{
999
  struct bfd_proto *p = (struct bfd_proto *) P;
1000
  struct bfd_config *cf = (struct bfd_config *) (P->cf);
1001

    
1002
  rem_node(&p->bfd_node);
1003

    
1004
  birdloop_stop(p->loop);
1005

    
1006
  struct bfd_neighbor *n;
1007
  WALK_LIST(n, cf->neigh_list)
1008
    bfd_stop_neighbor(p, n);
1009

    
1010
  bfd_drop_requests(p);
1011

    
1012
  /* FIXME: This is hack */
1013
  birdloop_enter(p->loop);
1014
  rfree(p->tpool);
1015
  birdloop_leave(p->loop);
1016

    
1017
  birdloop_free(p->loop);
1018

    
1019
  return PS_DOWN;
1020
}
1021

    
1022
static int
1023
bfd_reconfigure(struct proto *P, struct proto_config *c)
1024
{
1025
  struct bfd_proto *p = (struct bfd_proto *) P;
1026
  // struct bfd_config *old = (struct bfd_config *) (P->cf);
1027
  struct bfd_config *new = (struct bfd_config *) c;
1028
  struct bfd_iface *ifa;
1029

    
1030
  birdloop_mask_wakeups(p->loop);
1031

    
1032
  WALK_LIST(ifa, p->iface_list)
1033
    bfd_reconfigure_iface(p, ifa, new);
1034

    
1035
  HASH_WALK(p->session_hash_id, next_id, s)
1036
  {
1037
    if (s->ifa->changed)
1038
      bfd_reconfigure_session(p, s);
1039
  }
1040
  HASH_WALK_END;
1041

    
1042
  bfd_reconfigure_neighbors(p, new);
1043

    
1044
  birdloop_unmask_wakeups(p->loop);
1045

    
1046
  return 1;
1047
}
1048

    
1049
/* Ensure one instance */
1050
struct bfd_config *bfd_cf;
1051

    
1052
static void
1053
bfd_preconfig(struct protocol *P UNUSED, struct config *c UNUSED)
1054
{
1055
  bfd_cf = NULL;
1056
}
1057

    
1058
static void
1059
bfd_copy_config(struct proto_config *dest, struct proto_config *src)
1060
{
1061
  struct bfd_config *d = (struct bfd_config *) dest;
1062
  // struct bfd_config *s = (struct bfd_config *) src;
1063

    
1064
  /* We clean up patt_list and neigh_list, neighbors and ifaces are non-sharable */
1065
  init_list(&d->patt_list);  
1066
  init_list(&d->neigh_list);
1067
}
1068

    
1069
void
1070
bfd_show_sessions(struct proto *P)
1071
{
1072
  byte tbuf[TM_DATETIME_BUFFER_SIZE];
1073
  struct bfd_proto *p = (struct bfd_proto *) P;
1074
  uint state, diag;
1075
  u32 tx_int, timeout;
1076
  const char *ifname;
1077

    
1078
  if (p->p.proto_state != PS_UP)
1079
  {
1080
    cli_msg(-1020, "%s: is not up", p->p.name);
1081
    cli_msg(0, "");
1082
    return;
1083
  }
1084

    
1085
  cli_msg(-1020, "%s:", p->p.name);
1086
  cli_msg(-1020, "%-25s %-10s %-10s %-10s  %8s %8s",
1087
          "IP address", "Interface", "State", "Since", "Interval", "Timeout");
1088

    
1089

    
1090
  HASH_WALK(p->session_hash_id, next_id, s)
1091
  {
1092
    /* FIXME: this is thread-unsafe, but perhaps harmless */
1093
    state = s->loc_state;
1094
    diag = s->loc_diag;
1095
    ifname = (s->ifa && s->ifa->sk->iface) ? s->ifa->sk->iface->name : "---";
1096
    tx_int = s->last_tx ? (MAX(s->des_min_tx_int, s->rem_min_rx_int) TO_MS) : 0;
1097
    timeout = (MAX(s->req_min_rx_int, s->rem_min_tx_int) TO_MS) * s->rem_detect_mult;
1098

    
1099
    state = (state < 4) ? state : 0;
1100
    tm_format_datetime(tbuf, &config->tf_proto, s->last_state_change);
1101

    
1102
    cli_msg(-1020, "%-25I %-10s %-10s %-10s  %3u.%03u  %3u.%03u",
1103
            s->addr, ifname, bfd_state_names[state], tbuf,
1104
            tx_int / 1000, tx_int % 1000, timeout / 1000, timeout % 1000);
1105
  }
1106
  HASH_WALK_END;
1107

    
1108
  cli_msg(0, "");
1109
}
1110

    
1111

    
1112
struct protocol proto_bfd = {
1113
  .name =                "BFD",
1114
  .template =                "bfd%d",
1115
  .init =                bfd_init,
1116
  .start =                bfd_start,
1117
  .shutdown =                bfd_shutdown,
1118
  .reconfigure =        bfd_reconfigure,
1119
  .preconfig =                 bfd_preconfig,
1120
  .copy_config =        bfd_copy_config,
1121
};