Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / nest / rt-show.c @ 6b3f1a54

History | View | Annotate | Download (9.66 KB)

1
/*
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
  tm_format_time(tm, &config->tf_route, e->lastmod);
43
  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, "%-20s %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
      char *onlink = (nh->flags & RNF_ONLINK) ? " onlink" : "";
74
      char weight[16] = "";
75

    
76
      if (nh->labels)
77
        {
78
          lsp += bsprintf(lsp, " mpls %d", nh->label[0]);
79
          for (int i=1;i<nh->labels; i++)
80
            lsp += bsprintf(lsp, "/%d", nh->label[i]);
81
        }
82
      *lsp = '\0';
83

    
84
      if (a->nh.next)
85
        bsprintf(weight, " weight %d", nh->weight + 1);
86

    
87
      if (ipa_nonzero(nh->gw))
88
        cli_printf(c, -1007, "\tvia %I on %s%s%s%s",
89
                   nh->gw, nh->iface->name, mpls, onlink, weight);
90
      else
91
        cli_printf(c, -1007, "\tdev %s%s%s",
92
                   nh->iface->name, mpls,  onlink, weight);
93
    }
94

    
95
  if (d->verbose)
96
    rta_show(c, a, tmpa);
97
}
98

    
99
static void
100
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
101
{
102
  rte *e, *ee;
103
  byte ia[NET_MAX_TEXT_LENGTH+1];
104
  struct ea_list *tmpa;
105
  struct channel *ec = d->tab->export_channel;
106
  int first = 1;
107
  int pass = 0;
108

    
109
  bsnprintf(ia, sizeof(ia), "%N", n->n.addr);
110

    
111
  for (e = n->routes; e; e = e->next)
112
    {
113
      if (rte_is_filtered(e) != d->filtered)
114
        continue;
115

    
116
      d->rt_counter++;
117
      d->net_counter += first;
118
      first = 0;
119

    
120
      if (pass)
121
        continue;
122

    
123
      ee = e;
124
      tmpa = rte_make_tmp_attrs(e, c->show_pool);
125

    
126
      /* Export channel is down, do not try to export routes to it */
127
      if (ec && (ec->export_state == ES_DOWN))
128
        goto skip;
129

    
130
      /* Special case for merged export */
131
      if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_MERGED))
132
        {
133
          rte *rt_free;
134
          e = rt_export_merged(ec, n, &rt_free, &tmpa, c->show_pool, 1);
135
          pass = 1;
136

    
137
          if (!e)
138
          { e = ee; goto skip; }
139
        }
140
      else if (d->export_mode)
141
        {
142
          struct proto *ep = ec->proto;
143
          int ic = ep->import_control ? ep->import_control(ep, &e, &tmpa, c->show_pool) : 0;
144

    
145
          if (ec->ra_mode == RA_OPTIMAL || ec->ra_mode == RA_MERGED)
146
            pass = 1;
147

    
148
          if (ic < 0)
149
            goto skip;
150

    
151
          if (d->export_mode > RSEM_PREEXPORT)
152
            {
153
              /*
154
               * FIXME - This shows what should be exported according to current
155
               * filters, but not what was really exported. 'configure soft'
156
               * command may change the export filter and do not update routes.
157
               */
158
              int do_export = (ic > 0) ||
159
                (f_run(ec->out_filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
160

    
161
              if (do_export != (d->export_mode == RSEM_EXPORT))
162
                goto skip;
163

    
164
              if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_ACCEPTED))
165
                pass = 1;
166
            }
167
        }
168

    
169
      if (d->show_protocol && (d->show_protocol != e->attrs->src->proto))
170
        goto skip;
171

    
172
      if (f_run(d->filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
173
        goto skip;
174

    
175
      if (d->stats < 2)
176
        rt_show_rte(c, ia, e, d, tmpa);
177

    
178
      d->show_counter++;
179
      ia[0] = 0;
180

    
181
    skip:
182
      if (e != ee)
183
      {
184
        rte_free(e);
185
        e = ee;
186
      }
187
      lp_flush(c->show_pool);
188

    
189
      if (d->primary_only)
190
        break;
191
    }
192
}
193

    
194
static void
195
rt_show_cleanup(struct cli *c)
196
{
197
  struct rt_show_data *d = c->rover;
198
  struct rt_show_data_rtable *tab;
199

    
200
  /* Unlink the iterator */
201
  if (d->table_open)
202
    fit_get(&d->tab->table->fib, &d->fit);
203

    
204
  /* Unlock referenced tables */
205
  WALK_LIST(tab, d->tables)
206
    rt_unlock_table(tab->table);
207
}
208

    
209
static void
210
rt_show_cont(struct cli *c)
211
{
212
  struct rt_show_data *d = c->rover;
213
#ifdef DEBUGGING
214
  unsigned max = 4;
215
#else
216
  unsigned max = 64;
217
#endif
218
  struct fib *fib = &d->tab->table->fib;
219
  struct fib_iterator *it = &d->fit;
220

    
221
  if (d->running_on_config && (d->running_on_config != config))
222
  {
223
    cli_printf(c, 8004, "Stopped due to reconfiguration");
224
    goto done;
225
  }
226

    
227
  if (!d->table_open)
228
  {
229
    FIB_ITERATE_INIT(&d->fit, &d->tab->table->fib);
230
    d->table_open = 1;
231
    d->table_counter++;
232

    
233
    d->show_counter_last = d->show_counter;
234
    d->rt_counter_last   = d->rt_counter;
235
    d->net_counter_last  = d->net_counter;
236

    
237
    if (d->tables_defined_by & RSD_TDB_SET)
238
      rt_show_table(c, d);
239
  }
240

    
241
  FIB_ITERATE_START(fib, it, net, n)
242
  {
243
    if (!max--)
244
    {
245
      FIB_ITERATE_PUT(it);
246
      return;
247
    }
248
    rt_show_net(c, n, d);
249
  }
250
  FIB_ITERATE_END;
251

    
252
  if (d->stats)
253
  {
254
    if (d->last_table != d->tab)
255
      rt_show_table(c, d);
256

    
257
    cli_printf(c, -1007, "%d of %d routes for %d networks in table %s",
258
               d->show_counter - d->show_counter_last, d->rt_counter - d->rt_counter_last,
259
               d->net_counter - d->net_counter_last, d->tab->table->name);
260
  }
261

    
262
  d->table_open = 0;
263
  d->tab = NODE_NEXT(d->tab);
264

    
265
  if (NODE_VALID(d->tab))
266
    return;
267

    
268
  if (d->stats && (d->table_counter > 1))
269
  {
270
    if (d->last_table) cli_printf(c, -1007, "");
271
    cli_printf(c, 14, "Total: %d of %d routes for %d networks in %d tables",
272
               d->show_counter, d->rt_counter, d->net_counter, d->table_counter);
273
  }
274
  else
275
    cli_printf(c, 0, "");
276

    
277
done:
278
  rt_show_cleanup(c);
279
  c->cont = c->cleanup = NULL;
280
}
281

    
282
struct rt_show_data_rtable *
283
rt_show_add_table(struct rt_show_data *d, rtable *t)
284
{
285
  struct rt_show_data_rtable *tab = cfg_allocz(sizeof(struct rt_show_data_rtable));
286
  tab->table = t;
287
  add_tail(&(d->tables), &(tab->n));
288
  return tab;
289
}
290

    
291
static inline void
292
rt_show_get_default_tables(struct rt_show_data *d)
293
{
294
  struct channel *c;
295
  struct rt_show_data_rtable *tab;
296

    
297
  if (d->export_channel)
298
  {
299
    c = d->export_channel;
300
    tab = rt_show_add_table(d, c->table);
301
    tab->export_channel = c;
302
    return;
303
  }
304

    
305
  if (d->export_protocol)
306
  {
307
    WALK_LIST(c, d->export_protocol->channels)
308
    {
309
      if (c->export_state == ES_DOWN)
310
        continue;
311

    
312
      tab = rt_show_add_table(d, c->table);
313
      tab->export_channel = c;
314
    }
315
    return;
316
  }
317

    
318
  if (d->show_protocol)
319
  {
320
    WALK_LIST(c, d->show_protocol->channels)
321
      rt_show_add_table(d, c->table);
322
    return;
323
  }
324

    
325
  for (int i=1; i<NET_MAX; i++)
326
    if (config->def_tables[i])
327
      rt_show_add_table(d, config->def_tables[i]->table);
328
}
329

    
330
static inline void
331
rt_show_prepare_tables(struct rt_show_data *d)
332
{
333
  struct rt_show_data_rtable *tab, *tabx;
334

    
335
  /* Add implicit tables if no table is specified */
336
  if (EMPTY_LIST(d->tables))
337
    rt_show_get_default_tables(d);
338

    
339
  WALK_LIST_DELSAFE(tab, tabx, d->tables)
340
  {
341
    /* Ensure there is defined export_channel for each table */
342
    if (d->export_mode)
343
    {
344
      if (!tab->export_channel && d->export_channel &&
345
          (tab->table == d->export_channel->table))
346
        tab->export_channel = d->export_channel;
347

    
348
      if (!tab->export_channel && d->export_protocol)
349
        tab->export_channel = proto_find_channel_by_table(d->export_protocol, tab->table);
350

    
351
      if (!tab->export_channel)
352
      {
353
        if (d->tables_defined_by & RSD_TDB_NMN)
354
          cf_error("No export channel for table %s", tab->table->name);
355

    
356
        rem_node(&(tab->n));
357
        continue;
358
      }
359
    }
360

    
361
    /* Ensure specified network is compatible with each table */
362
    if (d->addr && (tab->table->addr_type != d->addr->type))
363
    {
364
      if (d->tables_defined_by & RSD_TDB_NMN)
365
        cf_error("Incompatible type of prefix/ip for table %s", tab->table->name);
366

    
367
      rem_node(&(tab->n));
368
      continue;
369
    }
370
  }
371

    
372
  /* Ensure there is at least one table */
373
  if (EMPTY_LIST(d->tables))
374
    cf_error("No valid tables");
375
}
376

    
377
void
378
rt_show(struct rt_show_data *d)
379
{
380
  struct rt_show_data_rtable *tab;
381
  net *n;
382

    
383
  /* Filtered routes are neither exported nor have sensible ordering */
384
  if (d->filtered && (d->export_mode || d->primary_only))
385
    cf_error("Incompatible show route options");
386

    
387
  rt_show_prepare_tables(d);
388

    
389
  if (!d->addr)
390
  {
391
    WALK_LIST(tab, d->tables)
392
      rt_lock_table(tab->table);
393

    
394
    /* There is at least one table */
395
    d->tab = HEAD(d->tables);
396
    this_cli->cont = rt_show_cont;
397
    this_cli->cleanup = rt_show_cleanup;
398
    this_cli->rover = d;
399
  }
400
  else
401
  {
402
    WALK_LIST(tab, d->tables)
403
    {
404
      d->tab = tab;
405

    
406
      if (d->show_for)
407
        n = net_route(tab->table, d->addr);
408
      else
409
        n = net_find(tab->table, d->addr);
410

    
411
      if (n)
412
        rt_show_net(this_cli, n, d);
413
    }
414

    
415
    if (d->rt_counter)
416
      cli_msg(0, "");
417
    else
418
      cli_msg(8001, "Network not found");
419
  }
420
}
421