Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-show.c @ 3b3b0910

History | View | Annotate | Download (9.57 KB)

1 f8d44b01 Jan Moskyto Matejka
/*
2
 *        BIRD -- Route Display Routines
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *        (c) 2017       Jan Moskyto Matejka <mq@jmq.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9
10
#undef LOCAL_DEBUG
11
12
#include "nest/bird.h"
13
#include "nest/route.h"
14
#include "nest/protocol.h"
15
#include "nest/cli.h"
16
#include "nest/iface.h"
17
#include "filter/filter.h"
18
19
static void
20
rt_show_table(struct cli *c, struct rt_show_data *d)
21
{
22
  /* No table blocks in 'show route count' */
23
  if (d->stats == 2)
24
    return;
25
26
  if (d->last_table) cli_printf(c, -1007, "");
27
  cli_printf(c, -1007, "Table %s:", d->tab->table->name);
28
  d->last_table = d->tab;
29
}
30
31
static void
32
rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa)
33
{
34
  byte from[IPA_MAX_TEXT_LENGTH+8];
35
  byte tm[TM_DATETIME_BUFFER_SIZE], info[256];
36
  rta *a = e->attrs;
37
  int primary = (e->net->routes == e);
38
  int sync_error = (e->net->n.flags & KRF_SYNC_ERROR);
39
  void (*get_route_info)(struct rte *, byte *buf, struct ea_list *attrs);
40
  struct nexthop *nh;
41
42 f047271c Ondrej Zajicek (work)
  tm_format_time(tm, &config->tf_route, e->lastmod);
43 f8d44b01 Jan Moskyto Matejka
  if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->nh.gw))
44
    bsprintf(from, " from %I", a->from);
45
  else
46
    from[0] = 0;
47
48
  get_route_info = a->src->proto->proto->get_route_info;
49
  if (get_route_info || d->verbose)
50
    {
51
      /* Need to normalize the extended attributes */
52
      ea_list *t = tmpa;
53
      t = ea_append(t, a->eattrs);
54
      tmpa = alloca(ea_scan(t));
55
      ea_merge(t, tmpa);
56
      ea_sort(tmpa);
57
    }
58
  if (get_route_info)
59
    get_route_info(e, info, tmpa);
60
  else
61
    bsprintf(info, " (%d)", e->pref);
62
63
  if (d->last_table != d->tab)
64
    rt_show_table(c, d);
65
66
  cli_printf(c, -1007, "%-18s %s [%s %s%s]%s%s", ia, rta_dest_name(a->dest),
67
             a->src->proto->name, tm, from, primary ? (sync_error ? " !" : " *") : "", info);
68
69
  if (a->dest == RTD_UNICAST)
70
    for (nh = &(a->nh); nh; nh = nh->next)
71
    {
72
      char mpls[MPLS_MAX_LABEL_STACK*12 + 5], *lsp = mpls;
73 a1f5e514 Ondrej Zajicek (work)
      char *onlink = (nh->flags & RNF_ONLINK) ? " onlink" : "";
74 f8d44b01 Jan Moskyto Matejka
75
      if (nh->labels)
76
        {
77
          lsp += bsprintf(lsp, " mpls %d", nh->label[0]);
78
          for (int i=1;i<nh->labels; i++)
79
            lsp += bsprintf(lsp, "/%d", nh->label[i]);
80
        }
81
      *lsp = '\0';
82
83
      if (a->nh.next)
84 a1f5e514 Ondrej Zajicek (work)
        cli_printf(c, -1007, "\tvia %I%s on %s%s weight %d",
85
                   nh->gw, mpls, nh->iface->name, onlink, nh->weight + 1);
86 f8d44b01 Jan Moskyto Matejka
      else
87 a1f5e514 Ondrej Zajicek (work)
        cli_printf(c, -1007, "\tvia %I%s on %s%s",
88
                   nh->gw, mpls, nh->iface->name, onlink);
89 f8d44b01 Jan Moskyto Matejka
    }
90
91
  if (d->verbose)
92
    rta_show(c, a, tmpa);
93
}
94
95
static void
96
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
97
{
98
  rte *e, *ee;
99
  byte ia[NET_MAX_TEXT_LENGTH+1];
100
  struct ea_list *tmpa;
101
  struct channel *ec = d->tab->export_channel;
102
  int first = 1;
103
  int pass = 0;
104
105
  bsnprintf(ia, sizeof(ia), "%N", n->n.addr);
106
107
  for (e = n->routes; e; e = e->next)
108
    {
109
      if (rte_is_filtered(e) != d->filtered)
110
        continue;
111
112
      d->rt_counter++;
113
      d->net_counter += first;
114
      first = 0;
115
116
      if (pass)
117
        continue;
118
119
      ee = e;
120
      tmpa = rte_make_tmp_attrs(e, c->show_pool);
121
122
      /* Export channel is down, do not try to export routes to it */
123
      if (ec && (ec->export_state == ES_DOWN))
124
        goto skip;
125
126
      /* Special case for merged export */
127
      if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_MERGED))
128
        {
129
          rte *rt_free;
130
          e = rt_export_merged(ec, n, &rt_free, &tmpa, c->show_pool, 1);
131
          pass = 1;
132
133
          if (!e)
134
          { e = ee; goto skip; }
135
        }
136
      else if (d->export_mode)
137
        {
138
          struct proto *ep = ec->proto;
139
          int ic = ep->import_control ? ep->import_control(ep, &e, &tmpa, c->show_pool) : 0;
140
141
          if (ec->ra_mode == RA_OPTIMAL || ec->ra_mode == RA_MERGED)
142
            pass = 1;
143
144
          if (ic < 0)
145
            goto skip;
146
147
          if (d->export_mode > RSEM_PREEXPORT)
148
            {
149
              /*
150
               * FIXME - This shows what should be exported according to current
151
               * filters, but not what was really exported. 'configure soft'
152
               * command may change the export filter and do not update routes.
153
               */
154
              int do_export = (ic > 0) ||
155
                (f_run(ec->out_filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
156
157
              if (do_export != (d->export_mode == RSEM_EXPORT))
158
                goto skip;
159
160
              if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_ACCEPTED))
161
                pass = 1;
162
            }
163
        }
164
165
      if (d->show_protocol && (d->show_protocol != e->attrs->src->proto))
166
        goto skip;
167
168
      if (f_run(d->filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
169
        goto skip;
170
171
      if (d->stats < 2)
172
        rt_show_rte(c, ia, e, d, tmpa);
173
174
      d->show_counter++;
175
      ia[0] = 0;
176
177
    skip:
178
      if (e != ee)
179
      {
180
        rte_free(e);
181
        e = ee;
182
      }
183
      lp_flush(c->show_pool);
184
185
      if (d->primary_only)
186
        break;
187
    }
188
}
189
190
static void
191
rt_show_cleanup(struct cli *c)
192
{
193
  struct rt_show_data *d = c->rover;
194
  struct rt_show_data_rtable *tab;
195
196
  /* Unlink the iterator */
197
  if (d->table_open)
198
    fit_get(&d->tab->table->fib, &d->fit);
199
200
  /* Unlock referenced tables */
201
  WALK_LIST(tab, d->tables)
202
    rt_unlock_table(tab->table);
203
}
204
205
static void
206
rt_show_cont(struct cli *c)
207
{
208
  struct rt_show_data *d = c->rover;
209
#ifdef DEBUGGING
210
  unsigned max = 4;
211
#else
212
  unsigned max = 64;
213
#endif
214
  struct fib *fib = &d->tab->table->fib;
215
  struct fib_iterator *it = &d->fit;
216
217
  if (d->running_on_config && (d->running_on_config != config))
218
  {
219
    cli_printf(c, 8004, "Stopped due to reconfiguration");
220
    goto done;
221
  }
222
223
  if (!d->table_open)
224
  {
225
    FIB_ITERATE_INIT(&d->fit, &d->tab->table->fib);
226
    d->table_open = 1;
227
    d->table_counter++;
228
229
    d->show_counter_last = d->show_counter;
230
    d->rt_counter_last   = d->rt_counter;
231
    d->net_counter_last  = d->net_counter;
232
233
    if (d->tables_defined_by & RSD_TDB_SET)
234
      rt_show_table(c, d);
235
  }
236
237
  FIB_ITERATE_START(fib, it, net, n)
238
  {
239
    if (!max--)
240
    {
241
      FIB_ITERATE_PUT(it);
242
      return;
243
    }
244
    rt_show_net(c, n, d);
245
  }
246
  FIB_ITERATE_END;
247
248
  if (d->stats)
249
  {
250
    if (d->last_table != d->tab)
251
      rt_show_table(c, d);
252
253
    cli_printf(c, -1007, "%d of %d routes for %d networks in table %s",
254
               d->show_counter - d->show_counter_last, d->rt_counter - d->rt_counter_last,
255
               d->net_counter - d->net_counter_last, d->tab->table->name);
256
  }
257
258
  d->table_open = 0;
259
  d->tab = NODE_NEXT(d->tab);
260
261
  if (NODE_VALID(d->tab))
262
    return;
263
264
  if (d->stats && (d->table_counter > 1))
265
  {
266
    if (d->last_table) cli_printf(c, -1007, "");
267
    cli_printf(c, 14, "Total: %d of %d routes for %d networks in %d tables",
268
               d->show_counter, d->rt_counter, d->net_counter, d->table_counter);
269
  }
270
  else
271
    cli_printf(c, 0, "");
272
273
done:
274
  rt_show_cleanup(c);
275
  c->cont = c->cleanup = NULL;
276
}
277
278
struct rt_show_data_rtable *
279
rt_show_add_table(struct rt_show_data *d, rtable *t)
280
{
281
  struct rt_show_data_rtable *tab = cfg_allocz(sizeof(struct rt_show_data_rtable));
282
  tab->table = t;
283
  add_tail(&(d->tables), &(tab->n));
284
  return tab;
285
}
286
287
static inline void
288
rt_show_get_default_tables(struct rt_show_data *d)
289
{
290
  struct channel *c;
291
  struct rt_show_data_rtable *tab;
292
293
  if (d->export_channel)
294
  {
295
    c = d->export_channel;
296
    tab = rt_show_add_table(d, c->table);
297
    tab->export_channel = c;
298
    return;
299
  }
300
301
  if (d->export_protocol)
302
  {
303
    WALK_LIST(c, d->export_protocol->channels)
304
    {
305
      if (c->export_state == ES_DOWN)
306
        continue;
307
308
      tab = rt_show_add_table(d, c->table);
309
      tab->export_channel = c;
310
    }
311
    return;
312
  }
313
314
  if (d->show_protocol)
315
  {
316
    WALK_LIST(c, d->show_protocol->channels)
317
      rt_show_add_table(d, c->table);
318
    return;
319
  }
320
321
  for (int i=1; i<NET_MAX; i++)
322
    if (config->def_tables[i])
323
      rt_show_add_table(d, config->def_tables[i]->table);
324
}
325
326
static inline void
327
rt_show_prepare_tables(struct rt_show_data *d)
328
{
329
  struct rt_show_data_rtable *tab, *tabx;
330
331
  /* Add implicit tables if no table is specified */
332
  if (EMPTY_LIST(d->tables))
333
    rt_show_get_default_tables(d);
334
335
  WALK_LIST_DELSAFE(tab, tabx, d->tables)
336
  {
337
    /* Ensure there is defined export_channel for each table */
338
    if (d->export_mode)
339
    {
340
      if (!tab->export_channel && d->export_channel &&
341
          (tab->table == d->export_channel->table))
342
        tab->export_channel = d->export_channel;
343
344
      if (!tab->export_channel && d->export_protocol)
345
        tab->export_channel = proto_find_channel_by_table(d->export_protocol, tab->table);
346
347
      if (!tab->export_channel)
348
      {
349
        if (d->tables_defined_by & RSD_TDB_NMN)
350
          cf_error("No export channel for table %s", tab->table->name);
351
352
        rem_node(&(tab->n));
353
        continue;
354
      }
355
    }
356
357
    /* Ensure specified network is compatible with each table */
358
    if (d->addr && (tab->table->addr_type != d->addr->type))
359
    {
360
      if (d->tables_defined_by & RSD_TDB_NMN)
361
        cf_error("Incompatible type of prefix/ip for table %s", tab->table->name);
362
363
      rem_node(&(tab->n));
364
      continue;
365
    }
366
  }
367
368
  /* Ensure there is at least one table */
369
  if (EMPTY_LIST(d->tables))
370
    cf_error("No valid tables");
371
}
372
373
void
374
rt_show(struct rt_show_data *d)
375
{
376
  struct rt_show_data_rtable *tab;
377
  net *n;
378
379
  /* Filtered routes are neither exported nor have sensible ordering */
380
  if (d->filtered && (d->export_mode || d->primary_only))
381
    cf_error("Incompatible show route options");
382
383
  rt_show_prepare_tables(d);
384
385
  if (!d->addr)
386
  {
387
    WALK_LIST(tab, d->tables)
388
      rt_lock_table(tab->table);
389
390
    /* There is at least one table */
391
    d->tab = HEAD(d->tables);
392
    this_cli->cont = rt_show_cont;
393
    this_cli->cleanup = rt_show_cleanup;
394
    this_cli->rover = d;
395
  }
396
  else
397
  {
398
    WALK_LIST(tab, d->tables)
399
    {
400
      d->tab = tab;
401
402
      if (d->show_for)
403
        n = net_route(tab->table, d->addr);
404
      else
405
        n = net_find(tab->table, d->addr);
406
407
      if (n)
408
        rt_show_net(this_cli, n, d);
409
    }
410
411
    if (d->rt_counter)
412
      cli_msg(0, "");
413
    else
414
      cli_msg(8001, "Network not found");
415
  }
416
}