summaryrefslogtreecommitdiff
path: root/nest/rt-attr.c
blob: 5f386a76110b2532cbbd1c96aa56a437e42a53a0 (plain)
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
/*
 *	BIRD -- Route Attribute Cache
 *
 *	(c) 1998 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <string.h>

#include "nest/bird.h"
#include "nest/route.h"
#include "nest/protocol.h"
#include "lib/resource.h"

/*
 *	FIXME: Implement hash tables and garbage collection!
 */

static rta *first_rta;
static slab *rta_slab;
static pool *rta_pool;

static inline int
ea_same(ea_list *x, ea_list *y)
{
  int c;

  while (x && y)
    {
      if (x->nattrs != y->nattrs)
	return 0;
      for(c=0; c<x->nattrs; c++)
	{
	  eattr *a = &x->attrs[c];
	  eattr *b = &y->attrs[c];

	  if (a->protocol != b->protocol ||
	      a->flags != b->flags ||
	      a->id != b->id ||
	      ((a->flags & EAF_LONGWORD) ? a->u.data != b->u.data :
	       (a->u.ptr->length != b->u.ptr->length || memcmp(a->u.ptr, b->u.ptr, a->u.ptr->length))))
	    return 0;
	}
      x = x->next;
      y = y->next;
    }
  return (!x && !y);
}

static inline int
rta_same(rta *x, rta *y)
{
  return (x->proto == y->proto &&
	  x->source == y->source &&
	  x->scope == y->scope &&
	  x->cast == y->cast &&
	  x->dest == y->dest &&
	  x->tos == y->tos &&
	  x->flags == y->flags &&
	  ipa_equal(x->gw, y->gw) &&
	  ipa_equal(x->from, y->from) &&
	  x->iface == y->iface &&
	  ea_same(x->attrs, y->attrs) &&
	  (!x->proto->rta_same || x->proto->rta_same(x, y)));
}

static inline ea_list *
ea_list_copy(ea_list *o)
{
  ea_list *n, **p, *z;
  unsigned i;

  p = &n;
  while (o)
    {
      z = mb_alloc(rta_pool, sizeof(ea_list) + sizeof(eattr) * o->nattrs);
      memcpy(z, o, sizeof(ea_list) + sizeof(eattr) * o->nattrs);
      *p = z;
      p = &z->next;
      for(i=0; i<o->nattrs; i++)
	{
	  eattr *a = o->attrs + i;
	  if (!(a->flags & EAF_LONGWORD))
	    {
	      unsigned size = sizeof(struct adata) + a->u.ptr->length;
	      struct adata *d = mb_alloc(rta_pool, size);
	      memcpy(d, a->u.ptr, size);
	      a->u.ptr = d;
	    }
	}
      o = o->next;
    }
  *p = NULL;
  return n;
}

static rta *
rta_copy(rta *o)
{
  rta *r = sl_alloc(rta_slab);

  memcpy(r, o, sizeof(rta));
  r->uc = 1;
  r->attrs = ea_list_copy(o->attrs);
  return r;
}

rta *
rta_lookup(rta *o)
{
  rta *r;

  for(r=first_rta; r; r=r->next)
    if (rta_same(r, o))
      return rta_clone(r);
  r = rta_copy(o);
  r->next = first_rta;
  first_rta = r;
  return r;
}

void
_rta_free(rta *r)
{
}

void
rta_dump(rta *r)
{
}

void
rta_dump_all(void)
{
}

void
rta_init(void)
{
  rta_pool = rp_new(&root_pool, "Attributes");
  rta_slab = sl_new(rta_pool, sizeof(rta));
}