Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / topology.c @ 9669362f

History | View | Annotate | Download (12.6 KB)

1
/*
2
 *        BIRD -- OSPF Topological Database
3
 *
4
 *        (c) 1999        Martin Mares <mj@ucw.cz>
5
 *        (c) 1999 - 2000 Ondrej Filip <feela@network.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#include "nest/bird.h"
11
#include "lib/string.h"
12

    
13
#include "ospf.h"
14

    
15
#define HASH_DEF_ORDER 6                /* FIXME: Increase */
16
#define HASH_HI_MARK *4
17
#define HASH_HI_STEP 2
18
#define HASH_HI_MAX 16
19
#define HASH_LO_MARK /5
20
#define HASH_LO_STEP 2
21
#define HASH_LO_MIN 8
22

    
23
void *
24
originate_rt_lsa_body(struct ospf_area *oa, u16 *length, struct proto_ospf *p)
25
{
26
  struct ospf_iface *ifa;
27
  int j=0,k=0,v=0,e=0,b=0;
28
  u16 i=0;
29
  struct ospf_lsa_rt *rt;
30
  struct ospf_lsa_rt_link *ln;
31
  struct ospf_neighbor *neigh;
32
  struct top_hash_entry *old;
33
  struct proto_ospf *po=(struct proto_ospf *)p;
34

    
35
  DBG("%s: Originating RT_lsa body for area \"%I\".\n", po->proto.name,
36
    oa->areaid);
37

    
38
  WALK_LIST (ifa, p->iface_list)
39
  {
40
    if((ifa->an==oa->areaid) && (ifa->state!=OSPF_IS_DOWN))
41
    {
42
      i++;
43
      if(ifa->type==OSPF_IT_VLINK) v=1;
44
    }
45
  }
46
  rt=mb_allocz(p->proto.pool, sizeof(struct ospf_lsa_rt)+
47
    i*sizeof(struct ospf_lsa_rt_link));
48
  if((p->areano>1) && (!oa->stub)) e=1;
49
  rt->VEB=(v>>LSA_RT_V)+(e>>LSA_RT_E)+(b>>LSA_RT_B);
50
  ln=(struct ospf_lsa_rt_link *)(rt+1);
51

    
52
  WALK_LIST (ifa, p->iface_list)
53
  {
54
    if((ifa->an==oa->areaid) && (ifa->state!=OSPF_IS_DOWN))
55
    {
56
      if(ifa->state==OSPF_IS_LOOP)
57
      {
58
        ln->type=3;
59
        ln->id=ipa_to_u32(ifa->iface->addr->ip);
60
        ln->data=0xffffffff;
61
        ln->metric=0;
62
        ln->notos=0;
63
      }
64
      else
65
      {
66
        switch(ifa->type)
67
        {
68
          case OSPF_IT_PTP:                /* rfc2328 - pg126 */
69
            neigh=(struct ospf_neighbor *)HEAD(ifa->neigh_list);
70
            if((neigh!=NULL) || (neigh->state==NEIGHBOR_FULL))
71
            {
72
               ln->type=LSART_PTP;
73
               ln->id=neigh->rid;
74
               ln->metric=ifa->cost;
75
               ln->notos=0;
76
               if(ifa->iface->flags && IA_UNNUMBERED)
77
               {
78
                 ln->data=ifa->iface->index;
79
               }
80
               else
81
               {
82
                 ln->id=ipa_to_u32(ifa->iface->addr->ip);
83
               }
84
            }
85
            else
86
            {
87
              if(ifa->state==OSPF_IS_PTP)
88
              {
89
                ln->type=LSART_STUB;
90
                ln->id=ln->id=ipa_to_u32(ifa->iface->addr->opposite);
91
                ln->metric=ifa->cost;
92
                ln->notos=0;
93
                ln->data=0xffffffff;
94
              }
95
              else
96
              {
97
                i--; /* No link added */
98
              }
99
            }
100
            break;
101
          case OSPF_IT_BCAST: /*FIXME Go on */
102
          case OSPF_IT_NBMA:
103
            if(ifa->state==OSPF_IS_WAITING)
104
            {
105
              ln->type=LSART_STUB;
106
              ln->id=ipa_to_u32(ifa->iface->addr->prefix);
107
              ln->data=ipa_to_u32(ipa_mkmask(ifa->iface->addr->pxlen));
108
              ln->metric=ifa->cost;
109
              ln->notos=0;
110
            }
111
            else
112
            {
113
              j=0,k=0;
114
              WALK_LIST(neigh, ifa->neigh_list)
115
              {
116
                if((neigh->rid==ifa->drid) &&
117
                  (neigh->state==NEIGHBOR_FULL)) k=1;
118
                if(neigh->state==NEIGHBOR_FULL) j=1;
119
              }
120
              if(((ifa->state==OSPF_IS_DR) && (j==1)) || (k==1))
121
              {
122
                ln->type=LSART_NET;
123
                ln->id=ipa_to_u32(ifa->drip);
124
                ln->data=ipa_to_u32(ifa->iface->addr->ip);
125
                ln->metric=ifa->cost;
126
                ln->notos=0;
127
              }
128
              else
129
              {
130
                ln->type=LSART_STUB;
131
                  ln->id=ipa_to_u32(ifa->iface->addr->prefix);
132
                ln->data=ipa_to_u32(ipa_mkmask(ifa->iface->addr->pxlen));
133
                ln->metric=ifa->cost;
134
                ln->notos=0;
135
              }
136
            }
137
            break;
138
          case OSPF_IT_VLINK:        /* FIXME Add virtual links! */
139
            i--;
140
            break;
141
        }
142
      }
143
      if(ifa->type==OSPF_IT_VLINK) v=1;
144
    }
145
    ln=(ln+1);
146
  }
147
  rt->links=i;
148
  *length=i*sizeof(struct ospf_lsa_rt_link)+sizeof(struct ospf_lsa_rt)+
149
    sizeof(struct ospf_lsa_header);
150
  return rt;
151
}
152

    
153
void
154
age_timer_hook(timer *timer)
155
{
156
  struct ospf_area *oa=timer->data;
157
  bird_clock_t delta;
158
  struct top_hash_entry *en,*nxt;
159
  int flush=0;
160

    
161
  flush=can_flush_lsa(oa);
162

    
163
  if((delta=now-oa->lage)>=AGINGDELTA)
164
  {
165
    WALK_SLIST_DELSAFE(en,nxt,oa->lsal) ospf_age(en,delta,flush,oa);
166
    oa->lage=now;
167
  }
168
}
169
        
170

    
171
void
172
addifa_rtlsa(struct ospf_iface *ifa)
173
{
174
  struct ospf_area *oa;
175
  struct proto_ospf *po=ifa->proto;
176
  u32 rtid;
177
  struct top_graph_rtlsa_link *li, *lih;
178

    
179
  rtid=po->proto.cf->global->router_id;
180
  DBG("%s: New OSPF area \"%d\" adding.\n", po->proto.name, ifa->an);
181
  oa=NULL;
182

    
183

    
184
  WALK_LIST(NODE oa,po->area_list)
185
  {
186
    if(oa->areaid==ifa->an) break;
187
  }
188

    
189
  if(EMPTY_LIST(po->area_list) || (oa->areaid!=ifa->an))        /* New area */
190
  {
191
    struct ospf_lsa_header *lsa;
192

    
193
    oa=mb_allocz(po->proto.pool, sizeof(struct ospf_area));
194
    add_tail(&po->area_list,NODE oa);
195
    oa->areaid=ifa->an;
196
    oa->gr=ospf_top_new(po);
197
    s_init_list(&(oa->lsal));
198
    oa->rt=NULL;
199
    oa->lage=now;
200
    oa->po=po;
201
    oa->age_timer=tm_new(po->proto.pool);
202
    oa->age_timer->data=oa;
203
    oa->age_timer->randomize=0;
204
    oa->age_timer->hook=age_timer_hook;
205
    oa->age_timer->recurrent=AGINGDELTA;
206
    tm_start(oa->age_timer,AGINGDELTA);
207
    fib_init(&oa->infib,po->proto.pool,sizeof(struct infib),16,init_infib);
208
            /* FIXME 16?? (Oh, sweet 16.... :-) */
209
    po->areano++;
210
    DBG("%s: New OSPF area \"%d\" added.\n", po->proto.name, ifa->an);
211
  }
212

    
213
  ifa->oa=oa;
214

    
215
  originate_rt_lsa(oa,po);
216
  DBG("RT LSA: rt: %I, id: %I, type: %u\n",oa->rt->lsa.rt,oa->rt->lsa.id,oa->rt->lsa.type);
217
  flood_lsa(NULL,NULL,&oa->rt->lsa,po,NULL,oa);
218
}
219

    
220
void
221
originate_rt_lsa(struct ospf_area *oa, struct proto_ospf *po)
222
{
223
  struct ospf_lsa_header lsa;
224
  u32 rtid=po->proto.cf->global->router_id;
225
  struct top_hash_entry *en;
226
  void *body;
227

    
228
  debug("%s: Originating RT_lsa for area \"%I\".\n",po->proto.name,oa->areaid);
229

    
230
  lsa.age=0;
231
  lsa.id=rtid;
232
  lsa.type=LSA_T_RT;
233
  lsa.rt=rtid;
234
  if(oa->rt==NULL)
235
  {
236
    lsa.sn=LSA_INITSEQNO;
237
  }
238
  else
239
  {
240
    lsa.sn=oa->rt->lsa.sn+1;
241
  }
242
  body=originate_rt_lsa_body(oa, &lsa.length, po);
243
  lsasum_calculate(&lsa,body,po);
244
  en=lsa_install_new(&lsa, body, oa, &po->proto);
245
  oa->rt=en;
246
  flood_lsa(NULL,NULL,&oa->rt->lsa,po,NULL,oa);
247
}
248

    
249
void *
250
originate_net_lsa_body(struct ospf_iface *ifa, u16 *length,
251
  struct proto_ospf *po)
252
{
253
  u16 i=1;
254
  struct ospf_neighbor *n;
255
  u32 *body;
256
  struct ospf_lsa_net *net;
257

    
258
  net=mb_alloc(po->proto.pool,sizeof(u32)*(ifa->fadj+1)+
259
    sizeof(struct ospf_lsa_net));
260
  net->netmask=ipa_mkmask(ifa->iface->addr->pxlen);
261

    
262
  body=(u32 *)(net+1);
263
  i=1;
264
  *body=po->proto.cf->global->router_id;
265
  WALK_LIST(n,ifa->neigh_list)
266
  {
267
    if(n->state==NEIGHBOR_FULL)
268
    {
269
      *(body+i)=n->rid;
270
      i++;
271
    }
272
  }
273
  *length=i*sizeof(u32)+sizeof(struct ospf_lsa_header)+
274
    sizeof(struct ospf_lsa_net);
275
  return net;
276
}
277

    
278
void
279
originate_net_lsa(struct ospf_iface *ifa, struct proto_ospf *po)
280
{
281
  struct ospf_lsa_header lsa;
282
  u32 rtid=po->proto.cf->global->router_id;
283
  struct top_hash_entry *en;
284
  void *body;
285

    
286

    
287
  if(ifa->state!=OSPF_IS_DR) return;
288

    
289
  debug("%s: Originating Net lsa for iface \"%s\".\n", po->proto.name,
290
    ifa->iface->name);
291

    
292
  if(ifa->fadj==0)
293
  {
294
    if(ifa->nlsa==NULL) return;
295

    
296
    lsa.sn+=1;
297
    lsa.age=LSA_MAXAGE;
298
    flood_lsa(NULL,NULL,&ifa->nlsa->lsa,po,NULL,ifa->oa);
299
    /* FIXME delete LSA */
300
    ifa->nlsa=NULL;
301
    return ;
302
  }
303

    
304
  lsa.age=0;
305
  lsa.id=rtid;
306
  lsa.type=LSA_T_NET;
307
  lsa.rt=rtid;
308
  if(ifa->nlsa==NULL)
309
  {
310
    lsa.sn=LSA_INITSEQNO;
311
  }
312
  else
313
  {
314
    lsa.sn+=1;
315
  }
316
  body=originate_net_lsa_body(ifa, &lsa.length, po);
317
  lsasum_calculate(&lsa,body,po);
318
  en=lsa_install_new(&lsa, body, ifa->oa, &po->proto);
319
  flood_lsa(NULL,NULL,&ifa->nlsa->lsa,po,NULL,ifa->oa);
320
}
321

    
322
static void
323
ospf_top_ht_alloc(struct top_graph *f)
324
{
325
  f->hash_size = 1 << f->hash_order;
326
  f->hash_mask = f->hash_size - 1;
327
  if (f->hash_order > HASH_HI_MAX - HASH_HI_STEP)
328
    f->hash_entries_max = ~0;
329
  else
330
    f->hash_entries_max = f->hash_size HASH_HI_MARK;
331
  if (f->hash_order < HASH_LO_MIN + HASH_LO_STEP)
332
    f->hash_entries_min = 0;
333
  else
334
    f->hash_entries_min = f->hash_size HASH_LO_MARK;
335
  DBG("Allocating OSPF hash of order %d: %d hash_entries, %d low, %d high\n",
336
      f->hash_order, f->hash_size, f->hash_entries_min, f->hash_entries_max);
337
  f->hash_table = mb_alloc(f->pool, f->hash_size * sizeof(struct top_hash_entry *));
338
  bzero(f->hash_table, f->hash_size * sizeof(struct top_hash_entry *));
339
}
340

    
341
static inline void
342
ospf_top_ht_free(struct top_hash_entry **h)
343
{
344
  mb_free(h);
345
}
346

    
347
static inline u32
348
ospf_top_hash_u32(u32 a)
349
{
350
  /* Shamelessly stolen from IP address hashing in ipv4.h */
351
  a ^= a >> 16;
352
  a ^= a << 10;
353
  return a;
354
}
355

    
356
static inline unsigned
357
ospf_top_hash(struct top_graph *f, u32 lsaid, u32 rtrid, u32 type)
358
{
359
#if 1        /* Dirty patch to make rt table calculation work. */
360
  return (ospf_top_hash_u32(lsaid) + ospf_top_hash_u32((type==2) ? lsaid : rtrid) + type) & f->hash_mask;
361
#else
362
  return (ospf_top_hash_u32(lsaid) + ospf_top_hash_u32(rtrid) + type) & f->hash_mask;
363
#endif
364
}
365

    
366
struct top_graph *
367
ospf_top_new(struct proto_ospf *p)
368
{
369
  struct top_graph *f;
370

    
371
  f = mb_allocz(p->proto.pool, sizeof(struct top_graph));
372
  f->pool = p->proto.pool;
373
  f->hash_slab = sl_new(f->pool, sizeof(struct top_hash_entry));
374
  f->hash_order = HASH_DEF_ORDER;
375
  ospf_top_ht_alloc(f);
376
  f->hash_entries = 0;
377
  f->hash_entries_min = 0;
378
  return f;
379
}
380

    
381
void
382
ospf_top_free(struct top_graph *f)
383
{
384
  rfree(f->hash_slab);
385
  ospf_top_ht_free(f->hash_table);
386
  mb_free(f);
387
}
388

    
389
static void
390
ospf_top_rehash(struct top_graph *f, int step)
391
{
392
  unsigned int oldn, oldh;
393
  struct top_hash_entry **n, **oldt, **newt, *e, *x;
394

    
395
  oldn = f->hash_size;
396
  oldt = f->hash_table;
397
  DBG("Re-hashing topology hash from order %d to %d\n", f->hash_order, f->hash_order+step);
398
  f->hash_order += step;
399
  ospf_top_ht_alloc(f);
400
  newt = f->hash_table;
401

    
402
  for(oldh=0; oldh < oldn; oldh++)
403
    {
404
      e = oldt[oldh];
405
      while (e)
406
        {
407
          x = e->next;
408
          n = newt + ospf_top_hash(f, e->lsa.id, e->lsa.rt, e->lsa.type);
409
          e->next = *n;
410
          *n = e;
411
          e = x;
412
        }
413
    }
414
  ospf_top_ht_free(oldt);
415
}
416

    
417
struct top_hash_entry *
418
ospf_hash_find_header(struct top_graph *f, struct ospf_lsa_header *h)
419
{
420
  return ospf_hash_find(f,h->id,h->rt,h->type);
421
}
422

    
423
struct top_hash_entry *
424
ospf_hash_get_header(struct top_graph *f, struct ospf_lsa_header *h)
425
{
426
  return ospf_hash_get(f,h->id,h->rt,h->type);
427
}
428

    
429
struct top_hash_entry *
430
ospf_hash_find(struct top_graph *f, u32 lsa, u32 rtr, u32 type)
431
{
432
  struct top_hash_entry *e = f->hash_table[ospf_top_hash(f, lsa, rtr, type)];
433

    
434
#if 1
435
  if(type==2 && lsa==rtr)
436
  {
437
    while (e && (e->lsa.id != lsa || e->lsa.type != 2 ))
438
      e = e->next;
439
  }
440
  else
441
  {
442
    while (e && (e->lsa.id != lsa || e->lsa.type != type || e->lsa.rt != rtr))
443
      e = e->next;
444
  }
445
#else
446
  while (e && (e->lsa.id != lsa || e->lsa.rt != rtr || e->lsa.type != type))
447
    e = e->next;
448
#endif
449
  return e;
450
}
451

    
452
struct top_hash_entry *
453
ospf_hash_get(struct top_graph *f, u32 lsa, u32 rtr, u32 type)
454
{
455
  struct top_hash_entry **ee = f->hash_table + ospf_top_hash(f, lsa, rtr, type);
456
  struct top_hash_entry *e = *ee;
457

    
458
  while (e && (e->lsa.id != lsa || e->lsa.rt != rtr || e->lsa.type != type))
459
    e = e->next;
460
  if (e)
461
    return e;
462
  e = sl_alloc(f->hash_slab);
463
  e->lsa.id = lsa;
464
  e->lsa.rt = rtr;
465
  e->lsa.type = type;
466
  e->lsa_body = NULL;
467
  e->nhi=NULL;
468
  e->next=*ee;                /* MJ you forgot this :-) */
469
  *ee=e;
470
  if (f->hash_entries++ > f->hash_entries_max)
471
    ospf_top_rehash(f, HASH_HI_STEP);
472
  return e;
473
}
474

    
475
void
476
ospf_hash_delete(struct top_graph *f, struct top_hash_entry *e)
477
{
478
  unsigned int h = ospf_top_hash(f, e->lsa.id, e->lsa.rt, e->lsa.type);
479
  struct top_hash_entry **ee = f->hash_table + h;
480

    
481
  while (*ee)
482
    {
483
      if (*ee == e)
484
        {
485
          *ee = e->next;
486
          sl_free(f->hash_slab, e);
487
          if (f->hash_entries-- < f->hash_entries_min)
488
            ospf_top_rehash(f, -HASH_LO_STEP);
489
          return;
490
        }
491
      ee = &((*ee)->next);
492
    }
493
  bug("ospf_hash_delete() called for invalid node");
494
}
495

    
496
void
497
ospf_top_dump(struct top_graph *f)
498
{
499
  unsigned int i;
500
  debug("Hash entries: %d\n", f->hash_entries);
501

    
502
  for(i=0; i<f->hash_size; i++)
503
    {
504
      struct top_hash_entry *e = f->hash_table[i];
505
      while (e)
506
        {
507
          debug("\t%01x %08I %08I %p\n", e->lsa.type, e->lsa.id,
508
            e->lsa.rt, e->lsa_body);
509
          e = e->next;
510
        }
511
    }
512
}
513

    
514
/* This is very uneficient, please don't call it often */
515

    
516
/* I should also test for every LSA if it's in some link state
517
 * retransmision list for every neighbor. I will not test it.
518
 * It can happen that I'll receive some strange ls ack's.
519
 */
520

    
521
int
522
can_flush_lsa(struct ospf_area *oa)
523
{
524
  struct ospf_iface *ifa;
525
  struct ospf_neighbor *n;
526
  struct proto_ospf *po=oa->po;
527

    
528
  WALK_LIST(ifa, iface_list)
529
  {
530
    if(ifa->oa==oa)
531
    {
532
      WALK_LIST(n, ifa->neigh_list)
533
      {
534
        if((n->state==NEIGHBOR_EXCHANGE)||(n->state==NEIGHBOR_LOADING))
535
        {
536
          return 0;
537
        }
538
      }
539
    }
540
  }
541

    
542
  return 1;
543
}