Statistics
| Branch: | Revision:

iof-bird-daemon / nest / proto.c @ 67bd949a

History | View | Annotate | Download (5.76 KB)

1
/*
2
 *        BIRD -- Protocols
3
 *
4
 *        (c) 1998--1999 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#define LOCAL_DEBUG
10

    
11
#include <string.h>
12

    
13
#include "nest/bird.h"
14
#include "nest/protocol.h"
15
#include "lib/resource.h"
16
#include "lib/lists.h"
17
#include "lib/event.h"
18
#include "conf/conf.h"
19
#include "nest/route.h"
20
#include "nest/iface.h"
21

    
22
static pool *proto_pool;
23

    
24
list protocol_list;
25
list proto_list;
26

    
27
static list inactive_proto_list;
28
static list initial_proto_list;
29

    
30
static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
31
static char *c_states[] = { "HUNGRY", "FEEDING", "HAPPY", "FLUSHING" };
32

    
33
static void
34
proto_relink(struct proto *p)
35
{
36
  rem_node(&p->n);
37
  add_tail(p->core_state == FS_HAPPY ? &proto_list : &inactive_proto_list, &p->n);
38
}
39

    
40
void *
41
proto_new(struct proto_config *c, unsigned size)
42
{
43
  struct protocol *pr = c->proto;
44
  pool *r = rp_new(proto_pool, c->name);
45
  struct proto *p = mb_alloc(r, size);
46

    
47
  p->cf = c;
48
  p->debug = c->debug;
49
  p->name = c->name;
50
  p->preference = c->preference;
51
  p->disabled = c->disabled;
52
  p->proto = pr;
53
  p->pool = r;
54
  p->attn = ev_new(r);
55
  p->attn->data = p;
56
  return p;
57
}
58

    
59
void *
60
proto_config_new(struct protocol *pr, unsigned size)
61
{
62
  struct proto_config *c = cfg_allocz(size);
63

    
64
  add_tail(&new_config->protos, &c->n);
65
  c->global = new_config;
66
  c->proto = pr;
67
  c->debug = pr->debug;
68
  c->name = pr->name;
69
  return c;
70
}
71

    
72
void
73
protos_preconfig(struct config *c)
74
{
75
  struct protocol *p;
76

    
77
  init_list(&proto_list);
78
  init_list(&inactive_proto_list);
79
  init_list(&initial_proto_list);
80
  debug("Protocol preconfig:");
81
  WALK_LIST(p, protocol_list)
82
    {
83
      debug(" %s", p->name);
84
      if (p->preconfig)
85
        p->preconfig(p, c);
86
    }
87
  debug("\n");
88
}
89

    
90
void
91
protos_postconfig(struct config *c)
92
{
93
  struct proto_config *x;
94
  struct protocol *p;
95

    
96
  debug("Protocol postconfig:");
97
  WALK_LIST(x, c->protos)
98
    {
99
      debug(" %s", x->name);
100
      p = x->proto;
101
      if (p->postconfig)
102
        p->postconfig(x);
103
    }
104
  debug("\n");
105
}
106

    
107
void
108
protos_commit(struct config *c)
109
{
110
  struct proto_config *x;
111
  struct protocol *p;
112
  struct proto *q;
113

    
114
  debug("Protocol commit:");
115
  WALK_LIST(x, c->protos)
116
    {
117
      debug(" %s", x->name);
118
      p = x->proto;
119
      q = p->init(x);
120
      q->proto_state = PS_DOWN;
121
      q->core_state = FS_HUNGRY;
122
      add_tail(&initial_proto_list, &q->n);
123
    }
124
  debug("\n");
125
}
126

    
127
static void
128
proto_rethink_goal(struct proto *p)
129
{
130
  struct protocol *q = p->proto;
131

    
132
  if (p->core_state == p->core_goal)
133
    return;
134
  if (p->core_goal == FS_HAPPY)                /* Going up */
135
    {
136
      if (p->core_state == FS_HUNGRY && p->proto_state == PS_DOWN)
137
        {
138
          DBG("Kicking %s up\n", p->name);
139
          proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
140
        }
141
    }
142
  else                                         /* Going down */
143
    {
144
      if (p->proto_state == PS_START || p->proto_state == PS_UP)
145
        {
146
          DBG("Kicking %s down\n", p->name);
147
          proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
148
        }
149
    }
150
}
151

    
152
static void
153
proto_set_goal(struct proto *p, unsigned goal)
154
{
155
  if (p->disabled)
156
    goal = FS_HUNGRY;
157
  p->core_goal = goal;
158
  proto_rethink_goal(p);
159
}
160

    
161
void
162
protos_start(void)
163
{
164
  struct proto *p, *n;
165

    
166
  debug("Protocol start\n");
167
  WALK_LIST_DELSAFE(p, n, initial_proto_list)
168
    proto_set_goal(p, FS_HAPPY);
169
}
170

    
171
void
172
protos_dump_all(void)
173
{
174
  struct proto *p;
175

    
176
  debug("Protocols:\n");
177

    
178
  WALK_LIST(p, proto_list)
179
    {
180
      debug("  protocol %s: state %s/%s\n", p->name, p_states[p->proto_state], c_states[p->core_state]);
181
      if (p->disabled)
182
        debug("\tDISABLED\n");
183
      else if (p->proto->dump)
184
        p->proto->dump(p);
185
    }
186
  WALK_LIST(p, inactive_proto_list)
187
    debug("  inactive %s: state %s/%s\n", p->name, p_states[p->proto_state], c_states[p->core_state]);
188
  WALK_LIST(p, initial_proto_list)
189
    debug("  initial %s\n", p->name);
190
}
191

    
192
void
193
protos_build(void)
194
{
195
  init_list(&protocol_list);
196
  add_tail(&protocol_list, &proto_device.n);
197
#ifdef CONFIG_RIP
198
  add_tail(&protocol_list, &proto_rip.n);
199
#endif
200
#ifdef CONFIG_STATIC
201
  add_tail(&protocol_list, &proto_static.n);
202
#endif
203
  proto_pool = rp_new(&root_pool, "Protocols");
204
}
205

    
206
static void
207
proto_fell_down(struct proto *p)
208
{
209
  DBG("Protocol %s down\n", p->name);
210
  proto_rethink_goal(p);
211
}
212

    
213
static void
214
proto_feed(void *P)
215
{
216
  struct proto *p = P;
217

    
218
  DBG("Feeding protocol %s\n", p->name);
219
  if_feed_baby(p);
220
  rt_feed_baby(p);
221
  p->core_state = FS_HAPPY;
222
  proto_relink(p);
223
  DBG("Protocol %s up and running\n", p->name);
224
}
225

    
226
static void
227
proto_flush(void *P)
228
{
229
  struct proto *p = P;
230

    
231
  DBG("Flushing protocol %s\n", p->name);
232
  bug("Protocol flushing not supported yet!"); /* FIXME */
233
}
234

    
235
void
236
proto_notify_state(struct proto *p, unsigned ps)
237
{
238
  unsigned ops = p->proto_state;
239
  unsigned cs = p->core_state;
240

    
241
  DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
242
  if (ops == ps)
243
    return;
244

    
245
  switch (ps)
246
    {
247
    case PS_DOWN:
248
      if (cs == FS_HUNGRY)                /* Shutdown finished */
249
        proto_fell_down(p);
250
      else if (cs == FS_FLUSHING)        /* Still flushing... */
251
        ;
252
      else                                /* Need to start flushing */
253
        goto schedule_flush;
254
      break;
255
    case PS_START:
256
      ASSERT(ops == PS_DOWN);
257
      ASSERT(cs == FS_HUNGRY);
258
      break;
259
    case PS_UP:
260
      ASSERT(ops == PS_DOWN || ops == PS_START);
261
      ASSERT(cs == FS_HUNGRY);
262
      DBG("%s: Scheduling meal\n", p->name);
263
      cs = FS_FEEDING;
264
      p->attn->hook = proto_feed;
265
      ev_schedule(p->attn);
266
      break;
267
    case PS_STOP:
268
      if (cs == FS_FEEDING || cs == FS_HAPPY)
269
        {
270
        schedule_flush:
271
          DBG("%s: Scheduling flush\n", p->name);
272
          cs = FS_FLUSHING;
273
          p->attn->hook = proto_flush;
274
          ev_schedule(p->attn);
275
        }
276
    default:
277
    error:
278
      bug("Invalid state transition for %s from %s/%s to */%s", p->name, c_states[cs], p_states[ops], p_states[ps]);
279
    }
280
  p->proto_state = ps;
281
  p->core_state = cs;
282
  proto_relink(p);
283
}