Revision f8d44b01

View differences:

nest/Makefile
1
src := a-path.c a-set.c cli.c cmds.c iface.c locks.c neighbor.c password.c proto.c rt-attr.c rt-dev.c rt-fib.c rt-table.c
1
src := a-path.c a-set.c cli.c cmds.c iface.c locks.c neighbor.c password.c proto.c rt-attr.c rt-dev.c rt-fib.c rt-show.c rt-table.c
2 2
obj := $(src-o-files)
3 3
$(all-daemon)
4 4
$(cf-local)
nest/cli.c
313 313
  c->event->hook = cli_event;
314 314
  c->event->data = c;
315 315
  c->cont = cli_hello;
316
  c->parser_pool = lp_new(c->pool, 4096);
316
  c->parser_pool = lp_new(c->pool, 4080);
317
  c->show_pool = lp_new(c->pool, 4080);
317 318
  c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE);
318 319
  ev_schedule(c->event);
319 320
  return c;
nest/cli.h
38 38
  int last_reply;
39 39
  int restricted;			/* CLI is restricted to read-only commands */
40 40
  struct linpool *parser_pool;		/* Pool used during parsing */
41
  struct linpool *show_pool;		/* Pool used during route show */
41 42
  byte *ring_buf;			/* Ring buffer for asynchronous messages */
42 43
  byte *ring_end, *ring_read, *ring_write;	/* Pointers to the ring buffer */
43 44
  uint ring_overflow;			/* Counter of ring overflows */
nest/protocol.h
270 270
  return pc->router_id ? pc->router_id : pc->global->router_id;
271 271
}
272 272

  
273
static inline struct ea_list *
274
rte_make_tmp_attrs(struct rte *rt, struct linpool *pool)
275
{
276
  struct ea_list *(*mta)(struct rte *rt, struct linpool *pool);
277
  mta = rt->attrs->src->proto->make_tmp_attrs;
278
  return mta ? mta(rt, pool) : NULL;
279
}
280

  
273 281
/* Moved from route.h to avoid dependency conflicts */
274 282
static inline void rte_update(struct proto *p, const net_addr *n, rte *new) { rte_update2(p->main_channel, n, new, p->main_source); }
275 283

  
nest/rt-show.c
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_datetime(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, "%-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

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

  
82
      if (a->nh.next)
83
	cli_printf(c, -1007, "\tvia %I%s on %s weight %d", nh->gw, mpls, nh->iface->name, nh->weight + 1);
84
      else
85
	cli_printf(c, -1007, "\tvia %I%s on %s", nh->gw, mpls, nh->iface->name);
86
    }
87

  
88
  if (d->verbose)
89
    rta_show(c, a, tmpa);
90
}
91

  
92
static void
93
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
94
{
95
  rte *e, *ee;
96
  byte ia[NET_MAX_TEXT_LENGTH+1];
97
  struct ea_list *tmpa;
98
  struct channel *ec = d->tab->export_channel;
99
  int first = 1;
100
  int pass = 0;
101

  
102
  bsnprintf(ia, sizeof(ia), "%N", n->n.addr);
103

  
104
  for (e = n->routes; e; e = e->next)
105
    {
106
      if (rte_is_filtered(e) != d->filtered)
107
	continue;
108

  
109
      d->rt_counter++;
110
      d->net_counter += first;
111
      first = 0;
112

  
113
      if (pass)
114
	continue;
115

  
116
      ee = e;
117
      tmpa = rte_make_tmp_attrs(e, c->show_pool);
118

  
119
      /* Export channel is down, do not try to export routes to it */
120
      if (ec && (ec->export_state == ES_DOWN))
121
	goto skip;
122

  
123
      /* Special case for merged export */
124
      if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_MERGED))
125
        {
126
	  rte *rt_free;
127
	  e = rt_export_merged(ec, n, &rt_free, &tmpa, c->show_pool, 1);
128
	  pass = 1;
129

  
130
	  if (!e)
131
	  { e = ee; goto skip; }
132
	}
133
      else if (d->export_mode)
134
	{
135
	  struct proto *ep = ec->proto;
136
	  int ic = ep->import_control ? ep->import_control(ep, &e, &tmpa, c->show_pool) : 0;
137

  
138
	  if (ec->ra_mode == RA_OPTIMAL || ec->ra_mode == RA_MERGED)
139
	    pass = 1;
140

  
141
	  if (ic < 0)
142
	    goto skip;
143

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

  
154
	      if (do_export != (d->export_mode == RSEM_EXPORT))
155
		goto skip;
156

  
157
	      if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_ACCEPTED))
158
		pass = 1;
159
	    }
160
	}
161

  
162
      if (d->show_protocol && (d->show_protocol != e->attrs->src->proto))
163
	goto skip;
164

  
165
      if (f_run(d->filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
166
	goto skip;
167

  
168
      if (d->stats < 2)
169
	rt_show_rte(c, ia, e, d, tmpa);
170

  
171
      d->show_counter++;
172
      ia[0] = 0;
173

  
174
    skip:
175
      if (e != ee)
176
      {
177
	rte_free(e);
178
	e = ee;
179
      }
180
      lp_flush(c->show_pool);
181

  
182
      if (d->primary_only)
183
	break;
184
    }
185
}
186

  
187
static void
188
rt_show_cleanup(struct cli *c)
189
{
190
  struct rt_show_data *d = c->rover;
191
  struct rt_show_data_rtable *tab;
192

  
193
  /* Unlink the iterator */
194
  if (d->table_open)
195
    fit_get(&d->tab->table->fib, &d->fit);
196

  
197
  /* Unlock referenced tables */
198
  WALK_LIST(tab, d->tables)
199
    rt_unlock_table(tab->table);
200
}
201

  
202
static void
203
rt_show_cont(struct cli *c)
204
{
205
  struct rt_show_data *d = c->rover;
206
#ifdef DEBUGGING
207
  unsigned max = 4;
208
#else
209
  unsigned max = 64;
210
#endif
211
  struct fib *fib = &d->tab->table->fib;
212
  struct fib_iterator *it = &d->fit;
213

  
214
  if (d->running_on_config && (d->running_on_config != config))
215
  {
216
    cli_printf(c, 8004, "Stopped due to reconfiguration");
217
    goto done;
218
  }
219

  
220
  if (!d->table_open)
221
  {
222
    FIB_ITERATE_INIT(&d->fit, &d->tab->table->fib);
223
    d->table_open = 1;
224
    d->table_counter++;
225

  
226
    d->show_counter_last = d->show_counter;
227
    d->rt_counter_last   = d->rt_counter;
228
    d->net_counter_last  = d->net_counter;
229

  
230
    if (d->tables_defined_by & RSD_TDB_SET)
231
      rt_show_table(c, d);
232
  }
233

  
234
  FIB_ITERATE_START(fib, it, net, n)
235
  {
236
    if (!max--)
237
    {
238
      FIB_ITERATE_PUT(it);
239
      return;
240
    }
241
    rt_show_net(c, n, d);
242
  }
243
  FIB_ITERATE_END;
244

  
245
  if (d->stats)
246
  {
247
    if (d->last_table != d->tab)
248
      rt_show_table(c, d);
249

  
250
    cli_printf(c, -1007, "%d of %d routes for %d networks in table %s",
251
	       d->show_counter - d->show_counter_last, d->rt_counter - d->rt_counter_last,
252
	       d->net_counter - d->net_counter_last, d->tab->table->name);
253
  }
254

  
255
  d->table_open = 0;
256
  d->tab = NODE_NEXT(d->tab);
257

  
258
  if (NODE_VALID(d->tab))
259
    return;
260

  
261
  if (d->stats && (d->table_counter > 1))
262
  {
263
    if (d->last_table) cli_printf(c, -1007, "");
264
    cli_printf(c, 14, "Total: %d of %d routes for %d networks in %d tables",
265
	       d->show_counter, d->rt_counter, d->net_counter, d->table_counter);
266
  }
267
  else
268
    cli_printf(c, 0, "");
269

  
270
done:
271
  rt_show_cleanup(c);
272
  c->cont = c->cleanup = NULL;
273
}
274

  
275
struct rt_show_data_rtable *
276
rt_show_add_table(struct rt_show_data *d, rtable *t)
277
{
278
  struct rt_show_data_rtable *tab = cfg_allocz(sizeof(struct rt_show_data_rtable));
279
  tab->table = t;
280
  add_tail(&(d->tables), &(tab->n));
281
  return tab;
282
}
283

  
284
static inline void
285
rt_show_get_default_tables(struct rt_show_data *d)
286
{
287
  struct channel *c;
288
  struct rt_show_data_rtable *tab;
289

  
290
  if (d->export_channel)
291
  {
292
    c = d->export_channel;
293
    tab = rt_show_add_table(d, c->table);
294
    tab->export_channel = c;
295
    return;
296
  }
297

  
298
  if (d->export_protocol)
299
  {
300
    WALK_LIST(c, d->export_protocol->channels)
301
    {
302
      if (c->export_state == ES_DOWN)
303
	continue;
304

  
305
      tab = rt_show_add_table(d, c->table);
306
      tab->export_channel = c;
307
    }
308
    return;
309
  }
310

  
311
  if (d->show_protocol)
312
  {
313
    WALK_LIST(c, d->show_protocol->channels)
314
      rt_show_add_table(d, c->table);
315
    return;
316
  }
317

  
318
  for (int i=1; i<NET_MAX; i++)
319
    if (config->def_tables[i])
320
      rt_show_add_table(d, config->def_tables[i]->table);
321
}
322

  
323
static inline void
324
rt_show_prepare_tables(struct rt_show_data *d)
325
{
326
  struct rt_show_data_rtable *tab, *tabx;
327

  
328
  /* Add implicit tables if no table is specified */
329
  if (EMPTY_LIST(d->tables))
330
    rt_show_get_default_tables(d);
331

  
332
  WALK_LIST_DELSAFE(tab, tabx, d->tables)
333
  {
334
    /* Ensure there is defined export_channel for each table */
335
    if (d->export_mode)
336
    {
337
      if (!tab->export_channel && d->export_channel &&
338
	  (tab->table == d->export_channel->table))
339
	tab->export_channel = d->export_channel;
340

  
341
      if (!tab->export_channel && d->export_protocol)
342
	tab->export_channel = proto_find_channel_by_table(d->export_protocol, tab->table);
343

  
344
      if (!tab->export_channel)
345
      {
346
	if (d->tables_defined_by & RSD_TDB_NMN)
347
	  cf_error("No export channel for table %s", tab->table->name);
348

  
349
	rem_node(&(tab->n));
350
	continue;
351
      }
352
    }
353

  
354
    /* Ensure specified network is compatible with each table */
355
    if (d->addr && (tab->table->addr_type != d->addr->type))
356
    {
357
      if (d->tables_defined_by & RSD_TDB_NMN)
358
	cf_error("Incompatible type of prefix/ip for table %s", tab->table->name);
359

  
360
      rem_node(&(tab->n));
361
      continue;
362
    }
363
  }
364

  
365
  /* Ensure there is at least one table */
366
  if (EMPTY_LIST(d->tables))
367
    cf_error("No valid tables");
368
}
369

  
370
void
371
rt_show(struct rt_show_data *d)
372
{
373
  struct rt_show_data_rtable *tab;
374
  net *n;
375

  
376
  /* Filtered routes are neither exported nor have sensible ordering */
377
  if (d->filtered && (d->export_mode || d->primary_only))
378
    cf_error("Incompatible show route options");
379

  
380
  rt_show_prepare_tables(d);
381

  
382
  if (!d->addr)
383
  {
384
    WALK_LIST(tab, d->tables)
385
      rt_lock_table(tab->table);
386

  
387
    /* There is at least one table */
388
    d->tab = HEAD(d->tables);
389
    this_cli->cont = rt_show_cont;
390
    this_cli->cleanup = rt_show_cleanup;
391
    this_cli->rover = d;
392
  }
393
  else
394
  {
395
    WALK_LIST(tab, d->tables)
396
    {
397
      d->tab = tab;
398

  
399
      if (d->show_for)
400
	n = net_route(tab->table, d->addr);
401
      else
402
	n = net_find(tab->table, d->addr);
403

  
404
      if (n)
405
	rt_show_net(this_cli, n, d);
406
    }
407

  
408
    if (d->rt_counter)
409
      cli_msg(0, "");
410
    else
411
      cli_msg(8001, "Network not found");
412
  }
413
}
414

  
nest/rt-table.c
33 33
#include "nest/bird.h"
34 34
#include "nest/route.h"
35 35
#include "nest/protocol.h"
36
#include "nest/cli.h"
37 36
#include "nest/iface.h"
38 37
#include "lib/resource.h"
39 38
#include "lib/event.h"
......
57 56
static inline void rt_prune_table(rtable *tab);
58 57

  
59 58

  
60
static inline struct ea_list *
61
make_tmp_attrs(struct rte *rt, struct linpool *pool)
62
{
63
  struct ea_list *(*mta)(struct rte *rt, struct linpool *pool);
64
  mta = rt->attrs->src->proto->make_tmp_attrs;
65
  return mta ? mta(rt, pool) : NULL;
66
}
67

  
68

  
69 59
/* Like fib_route(), but skips empty net entries */
70 60
static inline void *
71 61
net_route_ip4(rtable *t, net_addr_ip4 *n)
......
376 366
  if (!tmpa)
377 367
    tmpa = &tmpb;
378 368

  
379
  *tmpa = make_tmp_attrs(rt, pool);
369
  *tmpa = rte_make_tmp_attrs(rt, pool);
380 370

  
381 371
  v = p->import_control ? p->import_control(p, &rt, tmpa, pool) : 0;
382 372
  if (v < 0)
......
1344 1334
	}
1345 1335
      else
1346 1336
	{
1347
	  tmpa = make_tmp_attrs(new, rte_update_pool);
1337
	  tmpa = rte_make_tmp_attrs(new, rte_update_pool);
1348 1338
	  if (filter && (filter != FILTER_REJECT))
1349 1339
	    {
1350 1340
	      ea_list *old_tmpa = tmpa;
......
1424 1414
  rte_update_lock();
1425 1415

  
1426 1416
  /* Rest is stripped down export_filter() */
1427
  ea_list *tmpa = make_tmp_attrs(rt, rte_update_pool);
1417
  ea_list *tmpa = rte_make_tmp_attrs(rt, rte_update_pool);
1428 1418
  int v = p->import_control ? p->import_control(p, &rt, &tmpa, rte_update_pool) : 0;
1429 1419
  if (v == RIC_PROCESS)
1430 1420
    v = (f_run(filter, &rt, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
......
2500 2490

  
2501 2491

  
2502 2492
/*
2503
 *  CLI commands
2504
 */
2505

  
2506
static void
2507
rt_show_table(struct cli *c, struct rt_show_data *d)
2508
{
2509
  /* No table blocks in 'show route count' */
2510
  if (d->stats == 2)
2511
    return;
2512

  
2513
  if (d->last_table) cli_printf(c, -1007, "");
2514
  cli_printf(c, -1007, "Table %s:", d->tab->table->name);
2515
  d->last_table = d->tab;
2516
}
2517

  
2518
static void
2519
rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa)
2520
{
2521
  byte from[IPA_MAX_TEXT_LENGTH+8];
2522
  byte tm[TM_DATETIME_BUFFER_SIZE], info[256];
2523
  rta *a = e->attrs;
2524
  int primary = (e->net->routes == e);
2525
  int sync_error = (e->net->n.flags & KRF_SYNC_ERROR);
2526
  void (*get_route_info)(struct rte *, byte *buf, struct ea_list *attrs);
2527
  struct nexthop *nh;
2528

  
2529
  tm_format_datetime(tm, &config->tf_route, e->lastmod);
2530
  if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->nh.gw))
2531
    bsprintf(from, " from %I", a->from);
2532
  else
2533
    from[0] = 0;
2534

  
2535
  get_route_info = a->src->proto->proto->get_route_info;
2536
  if (get_route_info || d->verbose)
2537
    {
2538
      /* Need to normalize the extended attributes */
2539
      ea_list *t = tmpa;
2540
      t = ea_append(t, a->eattrs);
2541
      tmpa = alloca(ea_scan(t));
2542
      ea_merge(t, tmpa);
2543
      ea_sort(tmpa);
2544
    }
2545
  if (get_route_info)
2546
    get_route_info(e, info, tmpa);
2547
  else
2548
    bsprintf(info, " (%d)", e->pref);
2549

  
2550
  if (d->last_table != d->tab)
2551
    rt_show_table(c, d);
2552

  
2553
  cli_printf(c, -1007, "%-18s %s [%s %s%s]%s%s", ia, rta_dest_name(a->dest),
2554
	     a->src->proto->name, tm, from, primary ? (sync_error ? " !" : " *") : "", info);
2555

  
2556
  if (a->dest == RTD_UNICAST)
2557
    for (nh = &(a->nh); nh; nh = nh->next)
2558
    {
2559
      char mpls[MPLS_MAX_LABEL_STACK*12 + 5], *lsp = mpls;
2560

  
2561
      if (nh->labels)
2562
        {
2563
	  lsp += bsprintf(lsp, " mpls %d", nh->label[0]);
2564
	  for (int i=1;i<nh->labels; i++)
2565
	    lsp += bsprintf(lsp, "/%d", nh->label[i]);
2566
	}
2567
      *lsp = '\0';
2568

  
2569
      if (a->nh.next)
2570
	cli_printf(c, -1007, "\tvia %I%s on %s weight %d", nh->gw, mpls, nh->iface->name, nh->weight + 1);
2571
      else
2572
	cli_printf(c, -1007, "\tvia %I%s on %s", nh->gw, mpls, nh->iface->name);
2573
    }
2574

  
2575
  if (d->verbose)
2576
    rta_show(c, a, tmpa);
2577
}
2578

  
2579
static void
2580
rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
2581
{
2582
  rte *e, *ee;
2583
  byte ia[NET_MAX_TEXT_LENGTH+1];
2584
  struct ea_list *tmpa;
2585
  struct channel *ec = d->tab->export_channel;
2586
  int first = 1;
2587
  int pass = 0;
2588

  
2589
  bsnprintf(ia, sizeof(ia), "%N", n->n.addr);
2590

  
2591
  for (e = n->routes; e; e = e->next)
2592
    {
2593
      if (rte_is_filtered(e) != d->filtered)
2594
	continue;
2595

  
2596
      d->rt_counter++;
2597
      d->net_counter += first;
2598
      first = 0;
2599

  
2600
      if (pass)
2601
	continue;
2602

  
2603
      ee = e;
2604
      rte_update_lock();		/* We use the update buffer for filtering */
2605
      tmpa = make_tmp_attrs(e, rte_update_pool);
2606

  
2607
      /* Export channel is down, do not try to export routes to it */
2608
      if (ec && (ec->export_state == ES_DOWN))
2609
	goto skip;
2610

  
2611
      /* Special case for merged export */
2612
      if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_MERGED))
2613
        {
2614
	  rte *rt_free;
2615
	  e = rt_export_merged(ec, n, &rt_free, &tmpa, rte_update_pool, 1);
2616
	  pass = 1;
2617

  
2618
	  if (!e)
2619
	  { e = ee; goto skip; }
2620
	}
2621
      else if (d->export_mode)
2622
	{
2623
	  struct proto *ep = ec->proto;
2624
	  int ic = ep->import_control ? ep->import_control(ep, &e, &tmpa, rte_update_pool) : 0;
2625

  
2626
	  if (ec->ra_mode == RA_OPTIMAL || ec->ra_mode == RA_MERGED)
2627
	    pass = 1;
2628

  
2629
	  if (ic < 0)
2630
	    goto skip;
2631

  
2632
	  if (d->export_mode > RSEM_PREEXPORT)
2633
	    {
2634
	      /*
2635
	       * FIXME - This shows what should be exported according to current
2636
	       * filters, but not what was really exported. 'configure soft'
2637
	       * command may change the export filter and do not update routes.
2638
	       */
2639
	      int do_export = (ic > 0) ||
2640
		(f_run(ec->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT);
2641

  
2642
	      if (do_export != (d->export_mode == RSEM_EXPORT))
2643
		goto skip;
2644

  
2645
	      if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_ACCEPTED))
2646
		pass = 1;
2647
	    }
2648
	}
2649

  
2650
      if (d->show_protocol && (d->show_protocol != e->attrs->src->proto))
2651
	goto skip;
2652

  
2653
      if (f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT)
2654
	goto skip;
2655

  
2656
      if (d->stats < 2)
2657
	rt_show_rte(c, ia, e, d, tmpa);
2658

  
2659
      d->show_counter++;
2660
      ia[0] = 0;
2661

  
2662
    skip:
2663
      if (e != ee)
2664
      {
2665
	rte_free(e);
2666
	e = ee;
2667
      }
2668
      rte_update_unlock();
2669

  
2670
      if (d->primary_only)
2671
	break;
2672
    }
2673
}
2674

  
2675
static void
2676
rt_show_cleanup(struct cli *c)
2677
{
2678
  struct rt_show_data *d = c->rover;
2679
  struct rt_show_data_rtable *tab;
2680

  
2681
  /* Unlink the iterator */
2682
  if (d->table_open)
2683
    fit_get(&d->tab->table->fib, &d->fit);
2684

  
2685
  /* Unlock referenced tables */
2686
  WALK_LIST(tab, d->tables)
2687
    rt_unlock_table(tab->table);
2688
}
2689

  
2690
static void
2691
rt_show_cont(struct cli *c)
2692
{
2693
  struct rt_show_data *d = c->rover;
2694
#ifdef DEBUGGING
2695
  unsigned max = 4;
2696
#else
2697
  unsigned max = 64;
2698
#endif
2699
  struct fib *fib = &d->tab->table->fib;
2700
  struct fib_iterator *it = &d->fit;
2701

  
2702
  if (d->running_on_config && (d->running_on_config != config))
2703
  {
2704
    cli_printf(c, 8004, "Stopped due to reconfiguration");
2705
    goto done;
2706
  }
2707

  
2708
  if (!d->table_open)
2709
  {
2710
    FIB_ITERATE_INIT(&d->fit, &d->tab->table->fib);
2711
    d->table_open = 1;
2712
    d->table_counter++;
2713

  
2714
    d->show_counter_last = d->show_counter;
2715
    d->rt_counter_last   = d->rt_counter;
2716
    d->net_counter_last  = d->net_counter;
2717

  
2718
    if (d->tables_defined_by & RSD_TDB_SET)
2719
      rt_show_table(c, d);
2720
  }
2721

  
2722
  FIB_ITERATE_START(fib, it, net, n)
2723
  {
2724
    if (!max--)
2725
    {
2726
      FIB_ITERATE_PUT(it);
2727
      return;
2728
    }
2729
    rt_show_net(c, n, d);
2730
  }
2731
  FIB_ITERATE_END;
2732

  
2733
  if (d->stats)
2734
  {
2735
    if (d->last_table != d->tab)
2736
      rt_show_table(c, d);
2737

  
2738
    cli_printf(c, -1007, "%d of %d routes for %d networks in table %s",
2739
	       d->show_counter - d->show_counter_last, d->rt_counter - d->rt_counter_last,
2740
	       d->net_counter - d->net_counter_last, d->tab->table->name);
2741
  }
2742

  
2743
  d->table_open = 0;
2744
  d->tab = NODE_NEXT(d->tab);
2745

  
2746
  if (NODE_VALID(d->tab))
2747
    return;
2748

  
2749
  if (d->stats && (d->table_counter > 1))
2750
  {
2751
    if (d->last_table) cli_printf(c, -1007, "");
2752
    cli_printf(c, 14, "Total: %d of %d routes for %d networks in %d tables",
2753
	       d->show_counter, d->rt_counter, d->net_counter, d->table_counter);
2754
  }
2755
  else
2756
    cli_printf(c, 0, "");
2757

  
2758
done:
2759
  rt_show_cleanup(c);
2760
  c->cont = c->cleanup = NULL;
2761
}
2762

  
2763
struct rt_show_data_rtable *
2764
rt_show_add_table(struct rt_show_data *d, rtable *t)
2765
{
2766
  struct rt_show_data_rtable *tab = cfg_allocz(sizeof(struct rt_show_data_rtable));
2767
  tab->table = t;
2768
  add_tail(&(d->tables), &(tab->n));
2769
  return tab;
2770
}
2771

  
2772
static inline void
2773
rt_show_get_default_tables(struct rt_show_data *d)
2774
{
2775
  struct channel *c;
2776
  struct rt_show_data_rtable *tab;
2777

  
2778
  if (d->export_channel)
2779
  {
2780
    c = d->export_channel;
2781
    tab = rt_show_add_table(d, c->table);
2782
    tab->export_channel = c;
2783
    return;
2784
  }
2785

  
2786
  if (d->export_protocol)
2787
  {
2788
    WALK_LIST(c, d->export_protocol->channels)
2789
    {
2790
      if (c->export_state == ES_DOWN)
2791
	continue;
2792

  
2793
      tab = rt_show_add_table(d, c->table);
2794
      tab->export_channel = c;
2795
    }
2796
    return;
2797
  }
2798

  
2799
  if (d->show_protocol)
2800
  {
2801
    WALK_LIST(c, d->show_protocol->channels)
2802
      rt_show_add_table(d, c->table);
2803
    return;
2804
  }
2805

  
2806
  for (int i=1; i<NET_MAX; i++)
2807
    if (config->def_tables[i])
2808
      rt_show_add_table(d, config->def_tables[i]->table);
2809
}
2810

  
2811
static inline void
2812
rt_show_prepare_tables(struct rt_show_data *d)
2813
{
2814
  struct rt_show_data_rtable *tab, *tabx;
2815

  
2816
  /* Add implicit tables if no table is specified */
2817
  if (EMPTY_LIST(d->tables))
2818
    rt_show_get_default_tables(d);
2819

  
2820
  WALK_LIST_DELSAFE(tab, tabx, d->tables)
2821
  {
2822
    /* Ensure there is defined export_channel for each table */
2823
    if (d->export_mode)
2824
    {
2825
      if (!tab->export_channel && d->export_channel &&
2826
	  (tab->table == d->export_channel->table))
2827
	tab->export_channel = d->export_channel;
2828

  
2829
      if (!tab->export_channel && d->export_protocol)
2830
	tab->export_channel = proto_find_channel_by_table(d->export_protocol, tab->table);
2831

  
2832
      if (!tab->export_channel)
2833
      {
2834
	if (d->tables_defined_by & RSD_TDB_NMN)
2835
	  cf_error("No export channel for table %s", tab->table->name);
2836

  
2837
	rem_node(&(tab->n));
2838
	continue;
2839
      }
2840
    }
2841

  
2842
    /* Ensure specified network is compatible with each table */
2843
    if (d->addr && (tab->table->addr_type != d->addr->type))
2844
    {
2845
      if (d->tables_defined_by & RSD_TDB_NMN)
2846
	cf_error("Incompatible type of prefix/ip for table %s", tab->table->name);
2847

  
2848
      rem_node(&(tab->n));
2849
      continue;
2850
    }
2851
  }
2852

  
2853
  /* Ensure there is at least one table */
2854
  if (EMPTY_LIST(d->tables))
2855
    cf_error("No valid tables");
2856
}
2857

  
2858
void
2859
rt_show(struct rt_show_data *d)
2860
{
2861
  struct rt_show_data_rtable *tab;
2862
  net *n;
2863

  
2864
  /* Filtered routes are neither exported nor have sensible ordering */
2865
  if (d->filtered && (d->export_mode || d->primary_only))
2866
    cf_error("Incompatible show route options");
2867

  
2868
  rt_show_prepare_tables(d);
2869

  
2870
  if (!d->addr)
2871
  {
2872
    WALK_LIST(tab, d->tables)
2873
      rt_lock_table(tab->table);
2874

  
2875
    /* There is at least one table */
2876
    d->tab = HEAD(d->tables);
2877
    this_cli->cont = rt_show_cont;
2878
    this_cli->cleanup = rt_show_cleanup;
2879
    this_cli->rover = d;
2880
  }
2881
  else
2882
  {
2883
    WALK_LIST(tab, d->tables)
2884
    {
2885
      d->tab = tab;
2886

  
2887
      if (d->show_for)
2888
	n = net_route(tab->table, d->addr);
2889
      else
2890
	n = net_find(tab->table, d->addr);
2891

  
2892
      if (n)
2893
	rt_show_net(this_cli, n, d);
2894
    }
2895

  
2896
    if (d->rt_counter)
2897
      cli_msg(0, "");
2898
    else
2899
      cli_msg(8001, "Network not found");
2900
  }
2901
}
2902

  
2903
/*
2904 2493
 *  Documentation for functions declared inline in route.h
2905 2494
 */
2906 2495
#if 0

Also available in: Unified diff