Statistics
| Branch: | Revision:

iof-bird-daemon / proto / pipe / pipe.c @ f4a60a9b

History | View | Annotate | Download (8.23 KB)

1
/*
2
 *        BIRD -- Table-to-Table Routing Protocol a.k.a Pipe
3
 *
4
 *        (c) 1999--2000 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Pipe
11
 *
12
 * The Pipe protocol is very simple. It just connects to two routing tables
13
 * using proto_add_announce_hook() and whenever it receives a rt_notify()
14
 * about a change in one of the tables, it converts it to a rte_update()
15
 * in the other one.
16
 *
17
 * To avoid pipe loops, Pipe keeps a `being updated' flag in each routing
18
 * table.
19
 *
20
 * A pipe has two announce hooks, the first connected to the main
21
 * table, the second connected to the peer table. When a new route is
22
 * announced on the main table, it gets checked by an export filter in
23
 * ahook 1, and, after that, it is announced to the peer table via
24
 * rte_update(), an import filter in ahook 2 is called. When a new
25
 * route is announced in the peer table, an export filter in ahook2
26
 * and an import filter in ahook 1 are used. Oviously, there is no
27
 * need in filtering the same route twice, so both import filters are
28
 * set to accept, while user configured 'import' and 'export' filters
29
 * are used as export filters in ahooks 2 and 1. Route limits are
30
 * handled similarly, but on the import side of ahooks.
31
 */
32

    
33
#undef LOCAL_DEBUG
34

    
35
#include "nest/bird.h"
36
#include "nest/iface.h"
37
#include "nest/protocol.h"
38
#include "nest/route.h"
39
#include "nest/cli.h"
40
#include "conf/conf.h"
41
#include "filter/filter.h"
42
#include "lib/string.h"
43

    
44
#include "pipe.h"
45

    
46
static void
47
pipe_rt_notify(struct proto *P, rtable *src_table, net *n, rte *new, rte *old, ea_list *attrs)
48
{
49
  struct pipe_proto *p = (void *) P;
50
  struct channel *dst = (src_table == p->pri->table) ? p->sec : p->pri;
51
  struct rte_src *src;
52

    
53
  net *nn;
54
  rte *e;
55
  rta a;
56

    
57
  if (!new && !old)
58
    return;
59

    
60
  if (dst->table->pipe_busy)
61
    {
62
      log(L_ERR "Pipe loop detected when sending %N to table %s",
63
          n->n.addr, dst->table->name);
64
      return;
65
    }
66

    
67
  nn = net_get(dst->table, n->n.addr);
68
  if (new)
69
    {
70
      memcpy(&a, new->attrs, sizeof(rta));
71

    
72
      a.aflags = 0;
73
      a.eattrs = attrs;
74
      a.hostentry = NULL;
75
      e = rte_get_temp(&a);
76
      e->net = nn;
77
      e->pflags = 0;
78

    
79
      /* Copy protocol specific embedded attributes. */
80
      memcpy(&(e->u), &(new->u), sizeof(e->u));
81
      e->pref = new->pref;
82
      e->pflags = new->pflags;
83

    
84
      src = a.src;
85
    }
86
  else
87
    {
88
      e = NULL;
89
      src = old->attrs->src;
90
    }
91

    
92
  src_table->pipe_busy = 1;
93
  rte_update2(dst, nn, e, src);
94
  src_table->pipe_busy = 0;
95
}
96

    
97
static int
98
pipe_import_control(struct proto *P, rte **ee, ea_list **ea UNUSED, struct linpool *p UNUSED)
99
{
100
  struct proto *pp = (*ee)->sender->proto;
101

    
102
  if (pp == P)
103
    return -1;        /* Avoid local loops automatically */
104

    
105
  return 0;
106
}
107

    
108
static void
109
pipe_reload_routes(struct channel *C)
110
{
111
  struct pipe_proto *p = (void *) C->proto;
112

    
113
  /* Route reload on one channel is just refeed on the other */
114
  channel_request_feeding((C == p->pri) ? p->sec : p->pri);
115
}
116

    
117

    
118
static void
119
pipe_postconfig(struct proto_config *CF)
120
{
121
  struct pipe_config *cf = (void *) CF;
122
  struct channel_config *cc = proto_cf_main_channel(CF);
123

    
124
  if (!cc->table)
125
    cf_error("Primary routing table not specified");
126

    
127
  if (!cf->peer)
128
    cf_error("Secondary routing table not specified");
129

    
130
  if (cc->table == cf->peer)
131
    cf_error("Primary table and peer table must be different");
132

    
133
  if (cc->table->addr_type != cf->peer->addr_type)
134
    cf_error("Primary table and peer table must have the same type");
135

    
136
  if (cc->rx_limit.action)
137
    cf_error("Pipe protocol does not support receive limits");
138

    
139
  if (cc->in_keep_filtered)
140
    cf_error("Pipe protocol prohibits keeping filtered routes");
141
}
142

    
143
static int
144
pipe_configure_channels(struct pipe_proto *p, struct pipe_config *cf)
145
{
146
  struct channel_config *cc = proto_cf_main_channel(&cf->c);
147

    
148
  struct channel_config pri_cf = {
149
    .name = "pri",
150
    .channel = cc->channel,
151
    .table = cc->table,
152
    .out_filter = cc->out_filter,
153
    .in_limit = cc->in_limit,
154
    .ra_mode = RA_ANY
155
  };
156

    
157
  struct channel_config sec_cf = {
158
    .name = "sec",
159
    .channel = cc->channel,
160
    .table = cf->peer,
161
    .out_filter = cc->in_filter,
162
    .in_limit = cc->out_limit,
163
    .ra_mode = RA_ANY
164
  };
165

    
166
  return
167
    proto_configure_channel(&p->p, &p->pri, &pri_cf) &&
168
    proto_configure_channel(&p->p, &p->sec, &sec_cf);
169
}
170

    
171
static struct proto *
172
pipe_init(struct proto_config *CF)
173
{
174
  struct proto *P = proto_new(CF);
175
  struct pipe_proto *p = (void *) P;
176
  struct pipe_config *cf = (void *) CF;
177

    
178
  P->rt_notify = pipe_rt_notify;
179
  P->import_control = pipe_import_control;
180
  P->reload_routes = pipe_reload_routes;
181

    
182
  pipe_configure_channels(p, cf);
183

    
184
  return P;
185
}
186

    
187
static int
188
pipe_reconfigure(struct proto *P, struct proto_config *CF)
189
{
190
  struct pipe_proto *p = (void *) P;
191
  struct pipe_config *cf = (void *) CF;
192

    
193
  return pipe_configure_channels(p, cf);
194
}
195

    
196
static void
197
pipe_copy_config(struct proto_config *dest, struct proto_config *src)
198
{
199
  /* Just a shallow copy, not many items here */
200
}
201

    
202
static void
203
pipe_get_status(struct proto *P, byte *buf)
204
{
205
  struct pipe_proto *p = (void *) P;
206

    
207
  bsprintf(buf, "%s <=> %s", p->pri->table->name, p->sec->table->name);
208
}
209

    
210
static void
211
pipe_show_stats(struct pipe_proto *p)
212
{
213
  struct proto_stats *s1 = &p->pri->stats;
214
  struct proto_stats *s2 = &p->sec->stats;
215

    
216
  /*
217
   * Pipe stats (as anything related to pipes) are a bit tricky. There
218
   * are two sets of stats - s1 for ahook to the primary routing and
219
   * s2 for the ahook to the secondary routing table. The user point
220
   * of view is that routes going from the primary routing table to
221
   * the secondary routing table are 'exported', while routes going in
222
   * the other direction are 'imported'.
223
   *
224
   * Each route going through a pipe is, technically, first exported
225
   * to the pipe and then imported from that pipe and such operations
226
   * are counted in one set of stats according to the direction of the
227
   * route propagation. Filtering is done just in the first part
228
   * (export). Therefore, we compose stats for one directon for one
229
   * user direction from both import and export stats, skipping
230
   * immediate and irrelevant steps (exp_updates_accepted,
231
   * imp_updates_received, imp_updates_filtered, ...).
232
   *
233
   * Rule of thumb is that stats s1 have the correct 'polarity'
234
   * (imp/exp), while stats s2 have switched 'polarity'.
235
   */
236

    
237
  cli_msg(-1006, "  Routes:         %u imported, %u exported",
238
          s1->imp_routes, s2->imp_routes);
239
  cli_msg(-1006, "  Route change stats:     received   rejected   filtered    ignored   accepted");
240
  cli_msg(-1006, "    Import updates:     %10u %10u %10u %10u %10u",
241
          s2->exp_updates_received, s2->exp_updates_rejected + s1->imp_updates_invalid,
242
          s2->exp_updates_filtered, s1->imp_updates_ignored, s1->imp_updates_accepted);
243
  cli_msg(-1006, "    Import withdraws:   %10u %10u        --- %10u %10u",
244
          s2->exp_withdraws_received, s1->imp_withdraws_invalid,
245
          s1->imp_withdraws_ignored, s1->imp_withdraws_accepted);
246
  cli_msg(-1006, "    Export updates:     %10u %10u %10u %10u %10u",
247
          s1->exp_updates_received, s1->exp_updates_rejected + s2->imp_updates_invalid,
248
          s1->exp_updates_filtered, s2->imp_updates_ignored, s2->imp_updates_accepted);
249
  cli_msg(-1006, "    Export withdraws:   %10u %10u        --- %10u %10u",
250
          s1->exp_withdraws_received, s2->imp_withdraws_invalid,
251
          s2->imp_withdraws_ignored, s2->imp_withdraws_accepted);
252
}
253

    
254
static void
255
pipe_show_proto_info(struct proto *P)
256
{
257
  struct pipe_proto *p = (void *) P;
258

    
259
  cli_msg(-1006, "  Channel %s", "main");
260
  cli_msg(-1006, "    Table:          %s", p->pri->table->name);
261
  cli_msg(-1006, "    Peer table:     %s", p->sec->table->name);
262
  cli_msg(-1006, "    Import filter:  %s", filter_name(p->sec->out_filter));
263
  cli_msg(-1006, "    Export filter:  %s", filter_name(p->pri->out_filter));
264

    
265
  channel_show_limit(&p->pri->in_limit, "Import limit:");
266
  channel_show_limit(&p->sec->in_limit, "Export limit:");
267

    
268
  if (P->proto_state != PS_DOWN)
269
    pipe_show_stats(p);
270
}
271

    
272

    
273
struct protocol proto_pipe = {
274
  .name =                "Pipe",
275
  .template =                "pipe%d",
276
  .proto_size =                sizeof(struct pipe_proto),
277
  .config_size =        sizeof(struct pipe_config),
278
  .postconfig =                pipe_postconfig,
279
  .init =                pipe_init,
280
  .reconfigure =        pipe_reconfigure,
281
  .copy_config =         pipe_copy_config,
282
  .get_status =         pipe_get_status,
283
  .show_proto_info =         pipe_show_proto_info
284
};