summaryrefslogtreecommitdiffstats
path: root/src/vnet/ipip/ipip.c
blob: c49be099d9a67bb95b7f49156add161365ca18c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
 * ipip.c: ipip
 *
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or aipiped to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stddef.h>
#include <vnet/adj/adj_midchain.h>
#include <vnet/ipip/ipip.h>
#include <vnet/vnet.h>
#include <vnet/adj/adj_nbr.h>
#include <vnet/fib/ip4_fib.h>
#include <vnet/fib/ip6_fib.h>
#include <vnet/ip/format.h>
#include <vnet/ipip/ipip.h>

ipip_main_t ipip_main;

/* Packet trace structure */
typedef struct
{
  u32 tunnel_id;
  u32 length;
  ip46_address_t src;
  ip46_address_t dst;
} ipip_tx_trace_t;

u8 *
format_ipip_tx_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  ipip_tx_trace_t *t = va_arg (*args, ipip_tx_trace_t *);

  s =
    format (s, "IPIP: tunnel %d len %d src %U dst %U", t->tunnel_id,
	    t->length, format_ip46_address, &t->src, IP46_TYPE_ANY,
	    format_ip46_address, &t->dst, IP46_TYPE_ANY);
  return s;
}

static u8 *
ipip_build_rewrite (vnet_main_t * vnm, u32 sw_if_index,
		    vnet_link_t link_type, const void *dst_address)
{
  ip4_header_t *ip4;
  ip6_header_t *ip6;
  u8 *rewrite = NULL;
  ipip_tunnel_t *t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);

  if (!t)
    /* not one of ours */
    return (0);

  switch (t->transport)
    {
    case IPIP_TRANSPORT_IP4:
      vec_validate (rewrite, sizeof (*ip4) - 1);
      ip4 = (ip4_header_t *) rewrite;
      ip4->ip_version_and_header_length = 0x45;
      ip4->ttl = 64;
      /* fixup ip4 header length, protocol and checksum after-the-fact */
      ip4->src_address.as_u32 = t->tunnel_src.ip4.as_u32;
      ip4->dst_address.as_u32 = t->tunnel_dst.ip4.as_u32;
      ip4->checksum = ip4_header_checksum (ip4);
      if (t->tc_tos != 0xFF)
	ip4->tos = t->tc_tos;
      break;

    case IPIP_TRANSPORT_IP6:
      vec_validate (rewrite, sizeof (*ip6) - 1);
      ip6 = (ip6_header_t *) rewrite;
      ip6->ip_version_traffic_class_and_flow_label =
	clib_host_to_net_u32 (6 << 28);
      if (t->tc_tos != 0xFF)
	ip6_set_traffic_class_network_order (ip6, t->tc_tos);
      ip6->hop_limit = 64;
      /* fixup ip6 header length and protocol after-the-fact */
      ip6->src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0];
      ip6->src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1];
      ip6->dst_address.as_u64[0] = t->tunnel_dst.ip6.as_u64[0];
      ip6->dst_address.as_u64[1] = t->tunnel_dst.ip6.as_u64[1];
      break;

    default:
      /* pass through */
      ;
    }
  return (rewrite);
}

static void
ipip4_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b,
	     const void *data)
{
  ip4_header_t *ip4;
  const ipip_tunnel_t *t = data;

  ip4 = vlib_buffer_get_current (b);
  ip4->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b));
  switch (adj->ia_link)
    {
    case VNET_LINK_IP6:
      ip4->protocol = IP_PROTOCOL_IPV6;
      if (t->tc_tos == 0xFF)
	ip4->tos =
	  ip6_traffic_class_network_order ((const ip6_header_t *) (ip4 + 1));
      break;

    case VNET_LINK_IP4:
      ip4->protocol = IP_PROTOCOL_IP_IN_IP;
      if (t->tc_tos == 0xFF)
	ip4->tos = ((ip4_header_t *) (ip4 + 1))->tos;
      break;

    default:
      break;
    }

  ip4->checksum = ip4_header_checksum (ip4);
}

static void
ipip6_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b,
	     const void *data)
{
  ip6_header_t *ip6;
  const ipip_tunnel_t *t = data;

  ip6 = vlib_buffer_get_current (b);
  ip6->payload_length =
    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b) -
			  sizeof (*ip6));
  switch (adj->ia_link)
    {
    case VNET_LINK_IP6:
      ip6->protocol = IP_PROTOCOL_IPV6;
      if (t->tc_tos == 0xFF)
	ip6_set_traffic_class_network_order (ip6,
					     ip6_traffic_class_network_order ((const ip6_header_t *) (ip6 + 1)));
      break;

    case VNET_LINK_IP4:
      ip6->protocol = IP_PROTOCOL_IP_IN_IP;
      if (t->tc_tos == 0xFF)
	ip6_set_traffic_class_network_order (ip6,
					     ((ip4_header_t *) (ip6 +
								1))->tos);
      break;

    default:
      break;
    }
}

static void
ipip_tunnel_stack (adj_index_t ai)
{
  ip_adjacency_t *adj;
  ipip_tunnel_t *t;
  u32 sw_if_index;

  adj = adj_get (ai);
  sw_if_index = adj->rewrite_header.sw_if_index;

  t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
  if (!t)
    return;

  if ((vnet_hw_interface_get_flags (vnet_get_main (), t->hw_if_index) &
       VNET_HW_INTERFACE_FLAG_LINK_UP) == 0)
    {
      adj_nbr_midchain_unstack (ai);
      return;
    }

  dpo_id_t tmp = DPO_INVALID;
  fib_forward_chain_type_t fib_fwd =
    t->transport ==
    IPIP_TRANSPORT_IP6 ? FIB_FORW_CHAIN_TYPE_UNICAST_IP6 :
    FIB_FORW_CHAIN_TYPE_UNICAST_IP4;

  fib_entry_contribute_forwarding (t->p2p.fib_entry_index, fib_fwd, &tmp);
  if (DPO_LOAD_BALANCE == tmp.dpoi_type)
    {
      /*
       * post IPIP rewrite we will load-balance. However, the IPIP encap
       * is always the same for this adjacency/tunnel and hence the IP/IPIP
       * src,dst hash is always the same result too. So we do that hash now and
       * stack on the choice.
       * If the choice is an incomplete adj then we will need a poke when
       * it becomes complete. This happens since the adj update walk propagates
       * as far a recursive paths.
       */
      const dpo_id_t *choice;
      load_balance_t *lb;
      int hash;

      lb = load_balance_get (tmp.dpoi_index);

      if (fib_fwd == FIB_FORW_CHAIN_TYPE_UNICAST_IP4)
	hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
				      lb->lb_hash_config);
      else
	hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
				      lb->lb_hash_config);
      choice =
	load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
      dpo_copy (&tmp, choice);
    }

  adj_nbr_midchain_stack (ai, &tmp);
  dpo_reset (&tmp);
}

static adj_walk_rc_t
ipip_adj_walk_cb (adj_index_t ai, void *ctx)
{
  ipip_tunnel_stack (ai);

  return (ADJ_WALK_RC_CONTINUE);
}

static void
ipip_tunnel_restack (ipip_tunnel_t * gt)
{
  fib_protocol_t proto;

  /*
   * walk all the adjacencies on th IPIP interface and restack them
   */
  FOR_EACH_FIB_IP_PROTOCOL (proto)
  {
    adj_nbr_walk (gt->sw_if_index, proto, ipip_adj_walk_cb, NULL);
  }
}

void
ipip_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
{
  ipip_tunnel_t *t;
  adj_midchain_fixup_t f;

  t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
  if (!t)
    return;

  f = t->transport == IPIP_TRANSPORT_IP6 ? ipip6_fixup : ipip4_fixup;

  adj_nbr_midchain_update_rewrite (ai, f, t,
				   (VNET_LINK_ETHERNET ==
				    adj_get_link_type (ai) ?
				    ADJ_FLAG_MIDCHAIN_NO_COUNT :
				    ADJ_FLAG_NONE), ipip_build_rewrite (vnm,
									sw_if_index,
									adj_get_link_type
									(ai),
									NULL));
  ipip_tunnel_stack (ai);
}

static u8 *
format_ipip_tunnel_name (u8 * s, va_list * args)
{
  u32 dev_instance = va_arg (*args, u32);
  ipip_main_t *gm = &ipip_main;
  ipip_tunnel_t *t;

  if (dev_instance >= vec_len (gm->tunnels))
    return format (s, "<improperly-referenced>");

  t = pool_elt_at_index (gm->tunnels, dev_instance);
  return format (s, "ipip%d", t->user_instance);
}

static u8 *
format_ipip_device (u8 * s, va_list * args)
{
  u32 dev_instance = va_arg (*args, u32);
  CLIB_UNUSED (int verbose) = va_arg (*args, int);

  s = format (s, "IPIP tunnel: id %d\n", dev_instance);
  return s;
}

static clib_error_t *
ipip_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
{
  vnet_hw_interface_t *hi;
  ipip_tunnel_t *t;

  hi = vnet_get_hw_interface (vnm, hw_if_index);

  t = ipip_tunnel_db_find_by_sw_if_index (hi->sw_if_index);
  if (!t)
    return 0;

  if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
    vnet_hw_interface_set_flags (vnm, hw_if_index,
				 VNET_HW_INTERFACE_FLAG_LINK_UP);
  else
    vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );

  ipip_tunnel_restack (t);

  return /* no error */ 0;
}

/* *INDENT-OFF* */
VNET_DEVICE_CLASS(ipip_device_class) = {
    .name = "IPIP tunnel device",
    .format_device_name = format_ipip_tunnel_name,
    .format_device = format_ipip_device,
    .format_tx_trace = format_ipip_tx_trace,
    .admin_up_down_function = ipip_interface_admin_up_down,
#ifdef SOON
    .clear counter = 0;
#endif
};

VNET_HW_INTERFACE_CLASS(ipip_hw_interface_class) = {
    .name = "IPIP",
    //.format_header = format_ipip_header_with_length,
    //.unformat_header = unformat_ipip_header,
    .build_rewrite = ipip_build_rewrite,
    .update_adjacency = ipip_update_adj,
    .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
};
/* *INDENT-ON* */

ipip_tunnel_t *
ipip_tunnel_db_find (ipip_tunnel_key_t * key)
{
  ipip_main_t *gm = &ipip_main;
  uword *p;

  p = hash_get_mem (gm->tunnel_by_key, key);
  if (!p)
    return (NULL);
  return (pool_elt_at_index (gm->tunnels, p[0]));
}

ipip_tunnel_t *
ipip_tunnel_db_find_by_sw_if_index (u32 sw_if_index)
{
  ipip_main_t *gm = &ipip_main;
  if (vec_len (gm->tunnel_index_by_sw_if_index) <= sw_if_index)
    return NULL;
  u32 ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
  if (ti == ~0)
    return NULL;
  return pool_elt_at_index (gm->tunnels, ti);
}

void
ipip_tunnel_db_add (ipip_tunnel_t * t, ipip_tunnel_key_t * key)
{
  ipip_main_t *gm = &ipip_main;

  t->key = clib_mem_alloc (sizeof (*t->key));
  clib_memcpy (t->key, key, sizeof (*key));
  hash_set_mem (gm->tunnel_by_key, t->key, t->dev_instance);
}

void
ipip_tunnel_db_remove (ipip_tunnel_t * t)
{
  ipip_main_t *gm = &ipip_main;

  hash_unset_mem (gm->tunnel_by_key, t->key);
  clib_mem_free (t->key);
  t->key = NULL;
}

static ipip_tunnel_t *
ipip_tunnel_from_fib_node (fib_node_t * node)
{
  ipip_main_t *gm = &ipip_main;
  ASSERT (gm->fib_node_type == node->fn_type);
  return ((ipip_tunnel_t *) (((char *) node) -
			     offsetof (ipip_tunnel_t, p2p.node)));
}

static fib_node_back_walk_rc_t
ipip_tunnel_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
{
  ipip_tunnel_restack (ipip_tunnel_from_fib_node (node));

  return (FIB_NODE_BACK_WALK_CONTINUE);
}

static fib_node_t *
ipip_tunnel_fib_node_get (fib_node_index_t index)
{
  ipip_tunnel_t *gt;
  ipip_main_t *gm;

  gm = &ipip_main;
  gt = pool_elt_at_index (gm->tunnels, index);

  return (&gt->p2p.node);
}

static void
ipip_tunnel_last_lock_gone (fib_node_t * node)
{
  /*
   * The MPLS IPIP tunnel is a root of the graph. As such
   * it never has children and thus is never locked.
   */
  ASSERT (0);
}

/*
 * Virtual function table registered by IPIP tunnels
 * for participation in the FIB object graph.
 */
const static fib_node_vft_t ipip_vft = {
  .fnv_get = ipip_tunnel_fib_node_get,
  .fnv_last_lock = ipip_tunnel_last_lock_gone,
  .fnv_back_walk = ipip_tunnel_back_walk,
};

static void
ipip_fib_add (ipip_tunnel_t * t)
{
  ipip_main_t *gm = &ipip_main;
  fib_prefix_t dst = {.fp_len = t->transport == IPIP_TRANSPORT_IP6 ? 128 : 32,
    .fp_proto =
      t->transport ==
      IPIP_TRANSPORT_IP6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4,
    .fp_addr = t->tunnel_dst
  };

  t->p2p.fib_entry_index =
    fib_table_entry_special_add (t->fib_index, &dst, FIB_SOURCE_RR,
				 FIB_ENTRY_FLAG_NONE);
  t->p2p.sibling_index =
    fib_entry_child_add (t->p2p.fib_entry_index, gm->fib_node_type,
			 t->dev_instance);
}

static void
ipip_fib_delete (ipip_tunnel_t * t)
{
  fib_entry_child_remove (t->p2p.fib_entry_index, t->p2p.sibling_index);
  fib_table_entry_delete_index (t->p2p.fib_entry_index, FIB_SOURCE_RR);
  fib_node_deinit (&t->p2p.node);
}

int
ipip_add_tunnel (ipip_transport_t transport,
		 u32 instance, ip46_address_t * src, ip46_address_t * dst,
		 u32 fib_index, u8 tc_tos, u32 * sw_if_indexp)
{
  ipip_main_t *gm = &ipip_main;
  vnet_main_t *vnm = gm->vnet_main;
  ip4_main_t *im4 = &ip4_main;
  ip6_main_t *im6 = &ip6_main;
  ipip_tunnel_t *t;
  vnet_hw_interface_t *hi;
  u32 hw_if_index, sw_if_index;
  ipip_tunnel_key_t key = {.transport = transport,
    .fib_index = fib_index,
    .src = *src,
    .dst = *dst
  };
  t = ipip_tunnel_db_find (&key);
  if (t)
    return VNET_API_ERROR_IF_ALREADY_EXISTS;

  pool_get_aligned (gm->tunnels, t, CLIB_CACHE_LINE_BYTES);
  memset (t, 0, sizeof (*t));

  /* Reconcile the real dev_instance and a possible requested instance */
  u32 t_idx = t - gm->tunnels;	/* tunnel index (or instance) */
  u32 u_idx = instance;		/* user specified instance */
  if (u_idx == ~0)
    u_idx = t_idx;
  if (hash_get (gm->instance_used, u_idx))
    {
      pool_put (gm->tunnels, t);
      return VNET_API_ERROR_INSTANCE_IN_USE;
    }
  hash_set (gm->instance_used, u_idx, 1);

  t->dev_instance = t_idx;	/* actual */
  t->user_instance = u_idx;	/* name */
  fib_node_init (&t->p2p.node, gm->fib_node_type);

  hw_if_index = vnet_register_interface (vnm, ipip_device_class.index, t_idx,
					 ipip_hw_interface_class.index,
					 t_idx);

  hi = vnet_get_hw_interface (vnm, hw_if_index);
  sw_if_index = hi->sw_if_index;

  t->hw_if_index = hw_if_index;
  t->fib_index = fib_index;
  t->sw_if_index = sw_if_index;
  t->tc_tos = tc_tos;

  t->transport = transport;
  vec_validate_init_empty (gm->tunnel_index_by_sw_if_index, sw_if_index, ~0);
  gm->tunnel_index_by_sw_if_index[sw_if_index] = t_idx;

  if (t->transport == IPIP_TRANSPORT_IP4)
    {
      vec_validate (im4->fib_index_by_sw_if_index, sw_if_index);
      hi->min_packet_bytes = 64 + sizeof (ip4_header_t);
    }
  else
    {
      vec_validate (im6->fib_index_by_sw_if_index, sw_if_index);
      hi->min_packet_bytes = 64 + sizeof (ip6_header_t);
    }

  /* Standard default ipip MTU. */
  vnet_sw_interface_set_mtu (vnm, sw_if_index, 9000);

  t->tunnel_src = *src;
  t->tunnel_dst = *dst;

  ipip_tunnel_db_add (t, &key);

  /*
   * Source the FIB entry for the tunnel's destination and become a
   * child thereof. The tunnel will then get poked when the forwarding
   * for the entry updates, and the tunnel can re-stack accordingly
   */
  ipip_fib_add (t);
  if (sw_if_indexp)
    *sw_if_indexp = sw_if_index;

  if (t->transport == IPIP_TRANSPORT_IP6 && !gm->ip6_protocol_registered)
    {
      ip6_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip6_input_node.index);
      ip6_register_protocol (IP_PROTOCOL_IPV6, ipip6_input_node.index);
      gm->ip6_protocol_registered = true;
    }
  else if (t->transport == IPIP_TRANSPORT_IP4 && !gm->ip4_protocol_registered)
    {
      ip4_register_protocol (IP_PROTOCOL_IP_IN_IP, ipip4_input_node.index);
      ip4_register_protocol (IP_PROTOCOL_IPV6, ipip4_input_node.index);
      gm->ip4_protocol_registered = true;
    }
  return 0;
}

int
ipip_del_tunnel (u32 sw_if_index)
{
  ipip_main_t *gm = &ipip_main;
  vnet_main_t *vnm = gm->vnet_main;
  ipip_tunnel_t *t;


  t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
  if (t == NULL)
    return VNET_API_ERROR_NO_SUCH_ENTRY;

  vnet_sw_interface_set_flags (vnm, sw_if_index, 0 /* down */ );
  gm->tunnel_index_by_sw_if_index[sw_if_index] = ~0;
  vnet_delete_hw_interface (vnm, t->hw_if_index);
  ipip_fib_delete (t);
  hash_unset (gm->instance_used, t->user_instance);
  ipip_tunnel_db_remove (t);
  pool_put (gm->tunnels, t);

  return 0;
}

static clib_error_t *
ipip_init (vlib_main_t * vm)
{
  ipip_main_t *gm = &ipip_main;

  memset (gm, 0, sizeof (gm[0]));
  gm->vlib_main = vm;
  gm->vnet_main = vnet_get_main ();
  gm->tunnel_by_key =
    hash_create_mem (0, sizeof (ipip_tunnel_key_t), sizeof (uword));
  gm->fib_node_type = fib_node_register_new_type (&ipip_vft);

  return 0;
}

VLIB_INIT_FUNCTION (ipip_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
="o">->fnbw_reason || FIB_NODE_BW_REASON_FLAG_ADJ_DOWN & ctx->fnbw_reason || FIB_NODE_BW_REASON_FLAG_INTERFACE_UP & ctx->fnbw_reason || FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN & ctx->fnbw_reason || FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE & ctx->fnbw_reason) { fib_entry_src_action_reactivate(fib_entry, fib_entry_get_best_source( fib_entry_get_index(fib_entry))); } /* * all other walk types can be reclassifed to a re-evaluate to * all recursive dependents. * By reclassifying we ensure that should any of these walk types meet * they can be merged. */ ctx->fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE; /* * ... and nothing is forced sync from now on. */ ctx->fnbw_flags &= ~FIB_NODE_BW_FLAG_FORCE_SYNC; FIB_ENTRY_DBG(fib_entry, "bw:%U", format_fib_node_bw_reason, ctx->fnbw_reason); /* * propagate the backwalk further if we haven't already reached the * maximum depth. */ fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_get_index(fib_entry), ctx); return (FIB_NODE_BACK_WALK_CONTINUE); } static void fib_entry_show_memory (void) { u32 n_srcs = 0, n_exts = 0; fib_entry_src_t *esrc; fib_entry_t *entry; fib_show_memory_usage("Entry", pool_elts(fib_entry_pool), pool_len(fib_entry_pool), sizeof(fib_entry_t)); pool_foreach(entry, fib_entry_pool, ({ n_srcs += vec_len(entry->fe_srcs); vec_foreach(esrc, entry->fe_srcs) { n_exts += fib_path_ext_list_length(&esrc->fes_path_exts); } })); fib_show_memory_usage("Entry Source", n_srcs, n_srcs, sizeof(fib_entry_src_t)); fib_show_memory_usage("Entry Path-Extensions", n_exts, n_exts, sizeof(fib_path_ext_t)); } /* * The FIB path-list's graph node virtual function table */ static const fib_node_vft_t fib_entry_vft = { .fnv_get = fib_entry_get_node, .fnv_last_lock = fib_entry_last_lock_gone, .fnv_back_walk = fib_entry_back_walk_notify, .fnv_mem_show = fib_entry_show_memory, }; /** * @brief Contribute the set of Adjacencies that this entry forwards with * to build the uRPF list of its children */ void fib_entry_contribute_urpf (fib_node_index_t entry_index, index_t urpf) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(entry_index); return (fib_path_list_contribute_urpf(fib_entry->fe_parent, urpf)); } /* * If the client is request a chain for multicast forwarding then swap * the chain type to one that can provide such transport. */ static fib_forward_chain_type_t fib_entry_chain_type_mcast_to_ucast (fib_forward_chain_type_t fct) { switch (fct) { case FIB_FORW_CHAIN_TYPE_MCAST_IP4: case FIB_FORW_CHAIN_TYPE_MCAST_IP6: /* * we can only transport IP multicast packets if there is an * LSP. */ fct = FIB_FORW_CHAIN_TYPE_MPLS_EOS; break; case FIB_FORW_CHAIN_TYPE_MPLS_EOS: case FIB_FORW_CHAIN_TYPE_UNICAST_IP4: case FIB_FORW_CHAIN_TYPE_UNICAST_IP6: case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS: case FIB_FORW_CHAIN_TYPE_ETHERNET: case FIB_FORW_CHAIN_TYPE_NSH: case FIB_FORW_CHAIN_TYPE_BIER: break; } return (fct); } /* * fib_entry_contribute_forwarding * * Get an lock the forwarding information (DPO) contributed by the FIB entry. */ void fib_entry_contribute_forwarding (fib_node_index_t fib_entry_index, fib_forward_chain_type_t fct, dpo_id_t *dpo) { fib_entry_delegate_t *fed; fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); /* * mfib children ask for mcast chains. fix these to the appropriate ucast types. */ fct = fib_entry_chain_type_mcast_to_ucast(fct); if (fct == fib_entry_get_default_chain_type(fib_entry)) { dpo_copy(dpo, &fib_entry->fe_lb); } else { fed = fib_entry_delegate_find(fib_entry, fib_entry_chain_type_to_delegate_type(fct)); if (NULL == fed) { fed = fib_entry_delegate_find_or_add( fib_entry, fib_entry_chain_type_to_delegate_type(fct)); /* * on-demand create eos/non-eos. * There is no on-demand delete because: * - memory versus complexity & reliability: * leaving unrequired [n]eos LB arounds wastes memory, cleaning * then up on the right trigger is more code. i favour the latter. */ fib_entry_src_mk_lb(fib_entry, fib_entry_get_best_src_i(fib_entry), fct, &fed->fd_dpo); } dpo_copy(dpo, &fed->fd_dpo); } /* * use the drop DPO is nothing else is present */ if (!dpo_id_is_valid(dpo)) { dpo_copy(dpo, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct))); } /* * don't allow the special index indicating replicate.vs.load-balance * to escape to the clients */ dpo->dpoi_index &= ~MPLS_IS_REPLICATE; } const dpo_id_t * fib_entry_contribute_ip_forwarding (fib_node_index_t fib_entry_index) { fib_forward_chain_type_t fct; fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); fct = fib_entry_get_default_chain_type(fib_entry); ASSERT((fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP4 || fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP6)); if (dpo_id_is_valid(&fib_entry->fe_lb)) { return (&fib_entry->fe_lb); } return (drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct))); } adj_index_t fib_entry_get_adj (fib_node_index_t fib_entry_index) { const dpo_id_t *dpo; dpo = fib_entry_contribute_ip_forwarding(fib_entry_index); if (dpo_id_is_valid(dpo)) { dpo = load_balance_get_bucket(dpo->dpoi_index, 0); if (dpo_is_adj(dpo)) { return (dpo->dpoi_index); } } return (ADJ_INDEX_INVALID); } fib_node_index_t fib_entry_get_path_list (fib_node_index_t fib_entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); return (fib_entry->fe_parent); } u32 fib_entry_child_add (fib_node_index_t fib_entry_index, fib_node_type_t child_type, fib_node_index_t child_index) { return (fib_node_child_add(FIB_NODE_TYPE_ENTRY, fib_entry_index, child_type, child_index)); }; void fib_entry_child_remove (fib_node_index_t fib_entry_index, u32 sibling_index) { fib_node_child_remove(FIB_NODE_TYPE_ENTRY, fib_entry_index, sibling_index); if (0 == fib_node_get_n_children(FIB_NODE_TYPE_ENTRY, fib_entry_index)) { /* * if there are no children left then there is no reason to keep * the non-default forwarding chains. those chains are built only * because the children want them. */ fib_entry_delegate_type_t fdt; fib_entry_delegate_t *fed; fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed, { dpo_reset(&fed->fd_dpo); fib_entry_delegate_remove(fib_entry, fdt); }); } } static fib_entry_t * fib_entry_alloc (u32 fib_index, const fib_prefix_t *prefix, fib_node_index_t *fib_entry_index) { fib_entry_t *fib_entry; fib_prefix_t *fep; pool_get(fib_entry_pool, fib_entry); clib_memset(fib_entry, 0, sizeof(*fib_entry)); fib_node_init(&fib_entry->fe_node, FIB_NODE_TYPE_ENTRY); fib_entry->fe_fib_index = fib_index; /* * the one time we need to update the const prefix is when * the entry is first created */ fep = (fib_prefix_t*)&(fib_entry->fe_prefix); *fep = *prefix; if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto) { fep->fp_len = 21; if (MPLS_NON_EOS == fep->fp_eos) { fep->fp_payload_proto = DPO_PROTO_MPLS; } ASSERT(DPO_PROTO_NONE != fib_entry->fe_prefix.fp_payload_proto); } dpo_reset(&fib_entry->fe_lb); *fib_entry_index = fib_entry_get_index(fib_entry); return (fib_entry); } static fib_entry_t* fib_entry_post_flag_update_actions (fib_entry_t *fib_entry, fib_entry_flag_t old_flags) { fib_node_index_t fei; /* * save the index so we can recover from pool reallocs */ fei = fib_entry_get_index(fib_entry); /* * handle changes to attached export for import entries */ int is_import = (FIB_ENTRY_FLAG_IMPORT & fib_entry_get_flags_i(fib_entry)); int was_import = (FIB_ENTRY_FLAG_IMPORT & old_flags); if (!was_import && is_import) { /* * transition from not exported to exported */ /* * there is an assumption here that the entry resolves via only * one interface and that it is the cross VRF interface. */ u32 sw_if_index = fib_path_list_get_resolving_interface(fib_entry->fe_parent); fib_attached_export_import(fib_entry, fib_table_get_index_for_sw_if_index( fib_entry_get_proto(fib_entry), sw_if_index)); } else if (was_import && !is_import) { /* * transition from exported to not exported */ fib_attached_export_purge(fib_entry); } /* * else * no change. nothing to do. */ /* * reload the entry address post possible pool realloc */ fib_entry = fib_entry_get(fei); /* * handle changes to attached export for export entries */ int is_attached = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(fib_entry)); int was_attached = (FIB_ENTRY_FLAG_ATTACHED & old_flags); if (!was_attached && is_attached) { /* * transition to attached. time to export */ // FIXME } // else FIXME return (fib_entry); } static void fib_entry_post_install_actions (fib_entry_t *fib_entry, fib_source_t source, fib_entry_flag_t old_flags) { fib_entry = fib_entry_post_flag_update_actions(fib_entry, old_flags); fib_entry_src_action_installed(fib_entry, source); } fib_node_index_t fib_entry_create (u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const fib_route_path_t *paths) { fib_node_index_t fib_entry_index; fib_entry_t *fib_entry; ASSERT(0 < vec_len(paths)); fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index); /* * since this is a new entry create, we don't need to check for winning * sources - there is only one. */ fib_entry = fib_entry_src_action_add(fib_entry, source, flags, drop_dpo_get( fib_proto_to_dpo( fib_entry_get_proto(fib_entry)))); fib_entry_src_action_path_swap(fib_entry, source, flags, paths); /* * handle possible realloc's by refetching the pointer */ fib_entry = fib_entry_get(fib_entry_index); fib_entry_src_action_activate(fib_entry, source); fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE); FIB_ENTRY_DBG(fib_entry, "create"); return (fib_entry_index); } fib_node_index_t fib_entry_create_special (u32 fib_index, const fib_prefix_t *prefix, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo) { fib_node_index_t fib_entry_index; fib_entry_t *fib_entry; /* * create and initialize the new enty */ fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index); /* * create the path-list */ fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo); fib_entry_src_action_activate(fib_entry, source); fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE); FIB_ENTRY_DBG(fib_entry, "create-special"); return (fib_entry_index); } static void fib_entry_post_update_actions (fib_entry_t *fib_entry, fib_source_t source, fib_entry_flag_t old_flags) { /* * backwalk to children to inform then of the change to forwarding. */ fib_node_back_walk_ctx_t bw_ctx = { .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE, }; fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_get_index(fib_entry), &bw_ctx); /* * then inform any covered prefixes */ fib_entry_cover_update_notify(fib_entry); fib_entry_post_install_actions(fib_entry, source, old_flags); } void fib_entry_recalculate_forwarding (fib_node_index_t fib_entry_index) { fib_source_t best_source; fib_entry_t *fib_entry; fib_entry_src_t *bsrc; fib_entry = fib_entry_get(fib_entry_index); bsrc = fib_entry_get_best_src_i(fib_entry); best_source = fib_entry_src_get_source(bsrc); fib_entry_src_action_reactivate(fib_entry, best_source); } static void fib_entry_source_change_w_flags (fib_entry_t *fib_entry, fib_source_t old_source, fib_entry_flag_t old_flags, fib_source_t new_source) { if (new_source < old_source) { /* * we have a new winning source. */ fib_entry_src_action_deactivate(fib_entry, old_source); fib_entry_src_action_activate(fib_entry, new_source); } else if (new_source > old_source) { /* * the new source loses. Re-activate the winning sources * in case it is an interposer and hence relied on the losing * source's path-list. */ fib_entry_src_action_reactivate(fib_entry, old_source); return; } else { /* * the new source is one this entry already has. * But the path-list was updated, which will contribute new forwarding, * so install it. */ fib_entry_src_action_reactivate(fib_entry, new_source); } fib_entry_post_update_actions(fib_entry, new_source, old_flags); } void fib_entry_source_change (fib_entry_t *fib_entry, fib_source_t old_source, fib_source_t new_source) { fib_entry_flag_t old_flags; old_flags = fib_entry_get_flags_for_source( fib_entry_get_index(fib_entry), old_source); return (fib_entry_source_change_w_flags(fib_entry, old_source, old_flags, new_source)); } void fib_entry_special_add (fib_node_index_t fib_entry_index, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo) { fib_source_t best_source; fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); best_source = fib_entry_get_best_source(fib_entry_index); fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo); fib_entry_source_change(fib_entry, best_source, source); FIB_ENTRY_DBG(fib_entry, "special-add:%U", format_fib_source, source); } void fib_entry_special_update (fib_node_index_t fib_entry_index, fib_source_t source, fib_entry_flag_t flags, const dpo_id_t *dpo) { fib_source_t best_source; fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); best_source = fib_entry_get_best_source(fib_entry_index); fib_entry = fib_entry_src_action_update(fib_entry, source, flags, dpo); fib_entry_source_change(fib_entry, best_source, source); FIB_ENTRY_DBG(fib_entry, "special-updated:%U", format_fib_source, source); } void fib_entry_path_add (fib_node_index_t fib_entry_index, fib_source_t source, fib_entry_flag_t flags, const fib_route_path_t *rpaths) { fib_source_t best_source; fib_entry_t *fib_entry; fib_entry_src_t *bsrc; fib_entry = fib_entry_get(fib_entry_index); ASSERT(NULL != fib_entry); bsrc = fib_entry_get_best_src_i(fib_entry); best_source = fib_entry_src_get_source(bsrc); fib_entry = fib_entry_src_action_path_add(fib_entry, source, flags, rpaths); fib_entry_source_change(fib_entry, best_source, source); FIB_ENTRY_DBG(fib_entry, "path add:%U", format_fib_source, source); } static fib_entry_src_flag_t fib_entry_src_burn_only_inherited (fib_entry_t *fib_entry) { fib_entry_src_t *src; fib_source_t source; int has_only_inherited_sources = 1; FOR_EACH_SRC_ADDED(fib_entry, src, source, ({ if (!(src->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED)) { has_only_inherited_sources = 0; break; } })); if (has_only_inherited_sources) { FOR_EACH_SRC_ADDED(fib_entry, src, source, ({ fib_entry_src_action_remove(fib_entry, source); })); return (FIB_ENTRY_SRC_FLAG_NONE); } else { return (FIB_ENTRY_SRC_FLAG_ADDED); } } static fib_entry_src_flag_t fib_entry_source_removed (fib_entry_t *fib_entry, fib_entry_flag_t old_flags) { const fib_entry_src_t *bsrc; fib_source_t best_source; /* * if all that is left are inherited sources, then burn them */ fib_entry_src_burn_only_inherited(fib_entry); bsrc = fib_entry_get_best_src_i(fib_entry); best_source = fib_entry_src_get_source(bsrc); if (FIB_SOURCE_MAX == best_source) { /* * no more sources left. this entry is toast. */ fib_entry = fib_entry_post_flag_update_actions(fib_entry, old_flags); fib_entry_src_action_uninstall(fib_entry); return (FIB_ENTRY_SRC_FLAG_NONE); } else { fib_entry_src_action_activate(fib_entry, best_source); } fib_entry_post_update_actions(fib_entry, best_source, old_flags); /* * still have sources */ return (FIB_ENTRY_SRC_FLAG_ADDED); } /* * fib_entry_path_remove * * remove a path from the entry. * return the fib_entry's index if it is still present, INVALID otherwise. */ fib_entry_src_flag_t fib_entry_path_remove (fib_node_index_t fib_entry_index, fib_source_t source, const fib_route_path_t *rpaths) { fib_entry_src_flag_t sflag; fib_source_t best_source; fib_entry_flag_t bflags; fib_entry_t *fib_entry; fib_entry_src_t *bsrc; fib_entry = fib_entry_get(fib_entry_index); ASSERT(NULL != fib_entry); bsrc = fib_entry_get_best_src_i(fib_entry); best_source = fib_entry_src_get_source(bsrc); bflags = fib_entry_src_get_flags(bsrc); sflag = fib_entry_src_action_path_remove(fib_entry, source, rpaths); FIB_ENTRY_DBG(fib_entry, "path remove:%U", format_fib_source, source); /* * if the path list for the source passed is invalid, * then we need to create a new one. else we are updating * an existing. */ if (source < best_source) { /* * Que! removing a path from a source that is better than the * one this entry is using. */ ASSERT(0); } else if (source > best_source ) { /* * the source is not the best. no need to update forwarding */ if (FIB_ENTRY_SRC_FLAG_ADDED & sflag) { /* * the source being removed still has paths */ return (FIB_ENTRY_SRC_FLAG_ADDED); } else { /* * that was the last path from this source, check if those * that remain are non-inherited */ return (fib_entry_src_burn_only_inherited(fib_entry)); } } else { /* * removing a path from the path-list we were using. */ if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag)) { /* * the last path from the source was removed. * fallback to lower source */ return (fib_entry_source_removed(fib_entry, bflags)); } else { /* * re-install the new forwarding information */ fib_entry_src_action_reactivate(fib_entry, source); } } fib_entry_post_update_actions(fib_entry, source, bflags); /* * still have sources */ return (FIB_ENTRY_SRC_FLAG_ADDED); } /* * fib_entry_special_remove * * remove a special source from the entry. * return the fib_entry's index if it is still present, INVALID otherwise. */ fib_entry_src_flag_t fib_entry_special_remove (fib_node_index_t fib_entry_index, fib_source_t source) { fib_entry_src_flag_t sflag; fib_source_t best_source; fib_entry_flag_t bflags; fib_entry_t *fib_entry; fib_entry_src_t *bsrc; fib_entry = fib_entry_get(fib_entry_index); ASSERT(NULL != fib_entry); bsrc = fib_entry_get_best_src_i(fib_entry); best_source = fib_entry_src_get_source(bsrc); bflags = fib_entry_src_get_flags(bsrc); FIB_ENTRY_DBG(fib_entry, "special remove:%U", format_fib_source, source); sflag = fib_entry_src_action_remove_or_update_inherit(fib_entry, source); /* * if the path list for the source passed is invalid, * then we need to create a new one. else we are updating * an existing. */ if (source < best_source ) { /* * Que! removing a path from a source that is better than the * one this entry is using. This can only mean it is a source * this prefix does not have. */ return (FIB_ENTRY_SRC_FLAG_ADDED); } else if (source > best_source ) { /* * the source is not the best. no need to update forwarding */ if (FIB_ENTRY_SRC_FLAG_ADDED & sflag) { /* * the source being removed still has paths */ return (FIB_ENTRY_SRC_FLAG_ADDED); } else { /* * that was the last path from this source, check if those * that remain are non-inherited */ if (FIB_ENTRY_SRC_FLAG_NONE == fib_entry_src_burn_only_inherited(fib_entry)) { /* * no more sources left. this entry is toast. */ fib_entry = fib_entry_post_flag_update_actions(fib_entry, bflags); fib_entry_src_action_uninstall(fib_entry); return (FIB_ENTRY_SRC_FLAG_NONE); } /* * reactivate the best source so the interposer gets restacked */ fib_entry_src_action_reactivate(fib_entry, best_source); return (FIB_ENTRY_SRC_FLAG_ADDED); } } else { if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag)) { /* * the source was removed. use the next best. */ return (fib_entry_source_removed(fib_entry, bflags)); } else { /* * re-install the new forwarding information */ fib_entry_src_action_reactivate(fib_entry, source); } } fib_entry_post_update_actions(fib_entry, source, bflags); /* * still have sources */ return (FIB_ENTRY_SRC_FLAG_ADDED); } /** * fib_entry_inherit * * If the source on the cover is inheriting then push this source * down to the covered. */ void fib_entry_inherit (fib_node_index_t cover, fib_node_index_t covered) { fib_entry_src_inherit(fib_entry_get(cover), fib_entry_get(covered)); } /** * fib_entry_delete * * The source is withdrawing all the paths it provided */ fib_entry_src_flag_t fib_entry_delete (fib_node_index_t fib_entry_index, fib_source_t source) { return (fib_entry_special_remove(fib_entry_index, source)); } /** * fib_entry_update * * The source has provided a new set of paths that will replace the old. */ void fib_entry_update (fib_node_index_t fib_entry_index, fib_source_t source, fib_entry_flag_t flags, const fib_route_path_t *paths) { fib_source_t best_source; fib_entry_flag_t bflags; fib_entry_t *fib_entry; fib_entry_src_t *bsrc; fib_entry = fib_entry_get(fib_entry_index); ASSERT(NULL != fib_entry); bsrc = fib_entry_get_best_src_i(fib_entry); best_source = fib_entry_src_get_source(bsrc); bflags = fib_entry_get_flags_i(fib_entry); fib_entry = fib_entry_src_action_path_swap(fib_entry, source, flags, paths); fib_entry_source_change_w_flags(fib_entry, best_source, bflags, source); FIB_ENTRY_DBG(fib_entry, "update"); } /* * fib_entry_cover_changed * * this entry is tracking its cover and that cover has changed. */ void fib_entry_cover_changed (fib_node_index_t fib_entry_index) { fib_entry_src_cover_res_t res = { .install = !0, .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE, }; CLIB_UNUSED(fib_source_t source); fib_source_t best_source; fib_entry_flag_t bflags; fib_entry_t *fib_entry; fib_entry_src_t *esrc; u32 index; bflags = FIB_ENTRY_FLAG_NONE; best_source = FIB_SOURCE_FIRST; fib_entry = fib_entry_get(fib_entry_index); fib_attached_export_cover_change(fib_entry); /* * propagate the notification to each of the added sources */ index = 0; FOR_EACH_SRC_ADDED(fib_entry, esrc, source, ({ if (0 == index) { /* * only the best source gets to set the back walk flags */ res = fib_entry_src_action_cover_change(fib_entry, esrc); bflags = fib_entry_src_get_flags(esrc); best_source = fib_entry_src_get_source(esrc); } else { fib_entry_src_action_cover_change(fib_entry, esrc); } index++; })); if (res.install) { fib_entry_src_action_reactivate(fib_entry, fib_entry_src_get_source( fib_entry_get_best_src_i(fib_entry))); fib_entry_post_install_actions(fib_entry, best_source, bflags); } else { fib_entry_src_action_uninstall(fib_entry); } if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason) { /* * time for walkies fido. */ fib_node_back_walk_ctx_t bw_ctx = { .fnbw_reason = res.bw_reason, }; fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx); } FIB_ENTRY_DBG(fib_entry, "cover-changed"); } /* * fib_entry_cover_updated * * this entry is tracking its cover and that cover has been updated * (i.e. its forwarding information has changed). */ void fib_entry_cover_updated (fib_node_index_t fib_entry_index) { fib_entry_src_cover_res_t res = { .install = !0, .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE, }; CLIB_UNUSED(fib_source_t source); fib_source_t best_source; fib_entry_flag_t bflags; fib_entry_t *fib_entry; fib_entry_src_t *esrc; u32 index; bflags = FIB_ENTRY_FLAG_NONE; best_source = FIB_SOURCE_FIRST; fib_entry = fib_entry_get(fib_entry_index); fib_attached_export_cover_update(fib_entry); /* * propagate the notification to each of the added sources */ index = 0; FOR_EACH_SRC_ADDED(fib_entry, esrc, source, ({ if (0 == index) { /* * only the best source gets to set the back walk flags */ res = fib_entry_src_action_cover_update(fib_entry, esrc); bflags = fib_entry_src_get_flags(esrc); best_source = fib_entry_src_get_source(esrc); } else { fib_entry_src_action_cover_update(fib_entry, esrc); } index++; })); if (res.install) { fib_entry_src_action_reactivate(fib_entry, fib_entry_src_get_source( fib_entry_get_best_src_i(fib_entry))); fib_entry_post_install_actions(fib_entry, best_source, bflags); } else { fib_entry_src_action_uninstall(fib_entry); } if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason) { /* * time for walkies fido. */ fib_node_back_walk_ctx_t bw_ctx = { .fnbw_reason = res.bw_reason, }; fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx); } FIB_ENTRY_DBG(fib_entry, "cover-updated"); } int fib_entry_recursive_loop_detect (fib_node_index_t entry_index, fib_node_index_t **entry_indicies) { fib_entry_t *fib_entry; int was_looped, is_looped; fib_entry = fib_entry_get(entry_index); if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent) { fib_node_index_t *entries = *entry_indicies; vec_add1(entries, entry_index); was_looped = fib_path_list_is_looped(fib_entry->fe_parent); is_looped = fib_path_list_recursive_loop_detect(fib_entry->fe_parent, &entries); *entry_indicies = entries; if (!!was_looped != !!is_looped) { /* * re-evaluate all the entry's forwarding * NOTE: this is an inplace modify */ fib_entry_delegate_type_t fdt; fib_entry_delegate_t *fed; FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed, { fib_entry_src_mk_lb(fib_entry, fib_entry_get_best_src_i(fib_entry), fib_entry_delegate_type_to_chain_type(fdt), &fed->fd_dpo); }); } } else { /* * the entry is currently not linked to a path-list. this happens * when it is this entry that is re-linking path-lists and has thus * broken the loop */ is_looped = 0; } return (is_looped); } u32 fib_entry_get_resolving_interface (fib_node_index_t entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(entry_index); return (fib_path_list_get_resolving_interface(fib_entry->fe_parent)); } fib_source_t fib_entry_get_best_source (fib_node_index_t entry_index) { fib_entry_t *fib_entry; fib_entry_src_t *bsrc; fib_entry = fib_entry_get(entry_index); bsrc = fib_entry_get_best_src_i(fib_entry); return (fib_entry_src_get_source(bsrc)); } /** * Return !0 is the entry represents a host prefix */ int fib_entry_is_host (fib_node_index_t fib_entry_index) { return (fib_prefix_is_host(fib_entry_get_prefix(fib_entry_index))); } /** * Return !0 is the entry is resolved, i.e. will return a valid forwarding * chain */ int fib_entry_is_resolved (fib_node_index_t fib_entry_index) { fib_entry_delegate_t *fed; fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); fed = fib_entry_delegate_find(fib_entry, FIB_ENTRY_DELEGATE_BFD); if (NULL == fed) { /* * no BFD tracking - consider it resolved. */ return (!0); } else { /* * defer to the state of the BFD tracking */ return (FIB_BFD_STATE_UP == fed->fd_bfd_state); } } void fib_entry_set_flow_hash_config (fib_node_index_t fib_entry_index, flow_hash_config_t hash_config) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); /* * pass the hash-config on to the load-balance object where it is cached. * we can ignore LBs in the delegate chains, since they will not be of the * correct protocol type (i.e. they are not IP) * There's no way, nor need, to change the hash config for MPLS. */ if (dpo_id_is_valid(&fib_entry->fe_lb)) { load_balance_t *lb; ASSERT(DPO_LOAD_BALANCE == fib_entry->fe_lb.dpoi_type); lb = load_balance_get(fib_entry->fe_lb.dpoi_index); /* * atomic update for packets in flight */ lb->lb_hash_config = hash_config; } } u32 fib_entry_get_stats_index (fib_node_index_t fib_entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); return (fib_entry->fe_lb.dpoi_index); } static int fib_ip4_address_compare (const ip4_address_t * a1, const ip4_address_t * a2) { /* * IP addresses are unsigned ints. the return value here needs to be signed * a simple subtraction won't cut it. * If the addresses are the same, the sort order is undefined, so phoey. */ return ((clib_net_to_host_u32(a1->data_u32) > clib_net_to_host_u32(a2->data_u32) ) ? 1 : -1); } static int fib_ip6_address_compare (const ip6_address_t * a1, const ip6_address_t * a2) { int i; for (i = 0; i < ARRAY_LEN (a1->as_u16); i++) { int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) - clib_net_to_host_u16 (a2->as_u16[i])); if (cmp != 0) return cmp; } return 0; } static int fib_entry_cmp (fib_node_index_t fib_entry_index1, fib_node_index_t fib_entry_index2) { fib_entry_t *fib_entry1, *fib_entry2; int cmp = 0; fib_entry1 = fib_entry_get(fib_entry_index1); fib_entry2 = fib_entry_get(fib_entry_index2); switch (fib_entry1->fe_prefix.fp_proto) { case FIB_PROTOCOL_IP4: cmp = fib_ip4_address_compare(&fib_entry1->fe_prefix.fp_addr.ip4, &fib_entry2->fe_prefix.fp_addr.ip4); break; case FIB_PROTOCOL_IP6: cmp = fib_ip6_address_compare(&fib_entry1->fe_prefix.fp_addr.ip6, &fib_entry2->fe_prefix.fp_addr.ip6); break; case FIB_PROTOCOL_MPLS: cmp = (fib_entry1->fe_prefix.fp_label - fib_entry2->fe_prefix.fp_label); if (0 == cmp) { cmp = (fib_entry1->fe_prefix.fp_eos - fib_entry2->fe_prefix.fp_eos); } break; } if (0 == cmp) { cmp = (fib_entry1->fe_prefix.fp_len - fib_entry2->fe_prefix.fp_len); } return (cmp); } int fib_entry_cmp_for_sort (void *i1, void *i2) { fib_node_index_t *fib_entry_index1 = i1, *fib_entry_index2 = i2; return (fib_entry_cmp(*fib_entry_index1, *fib_entry_index2)); } void fib_entry_lock (fib_node_index_t fib_entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); fib_node_lock(&fib_entry->fe_node); } void fib_entry_unlock (fib_node_index_t fib_entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); fib_node_unlock(&fib_entry->fe_node); } void fib_entry_module_init (void) { fib_node_register_type(FIB_NODE_TYPE_ENTRY, &fib_entry_vft); fib_entry_logger = vlib_log_register_class("fib", "entry"); fib_entry_track_module_init(); } fib_route_path_t * fib_entry_encode (fib_node_index_t fib_entry_index) { fib_path_ext_list_t *ext_list; fib_path_encode_ctx_t ctx = { .rpaths = NULL, }; fib_entry_t *fib_entry; fib_entry_src_t *bsrc; ext_list = NULL; fib_entry = fib_entry_get(fib_entry_index); bsrc = fib_entry_get_best_src_i(fib_entry); if (bsrc) { ext_list = &bsrc->fes_path_exts; } if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent) { fib_path_list_walk_w_ext(fib_entry->fe_parent, ext_list, fib_path_encode, &ctx); } return (ctx.rpaths); } const fib_prefix_t * fib_entry_get_prefix (fib_node_index_t fib_entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); return (&fib_entry->fe_prefix); } u32 fib_entry_get_fib_index (fib_node_index_t fib_entry_index) { fib_entry_t *fib_entry; fib_entry = fib_entry_get(fib_entry_index); return (fib_entry->fe_fib_index); } u32 fib_entry_pool_size (void) { return (pool_elts(fib_entry_pool)); } static clib_error_t * show_fib_entry_command (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { fib_node_index_t fei; if (unformat (input, "%d", &fei)) { /* * show one in detail */ if (!pool_is_free_index(fib_entry_pool, fei)) { vlib_cli_output (vm, "%d@%U", fei, format_fib_entry, fei, FIB_ENTRY_FORMAT_DETAIL2); } else { vlib_cli_output (vm, "entry %d invalid", fei); } } else { /* * show all */ vlib_cli_output (vm, "FIB Entries:"); pool_foreach_index(fei, fib_entry_pool, ({ vlib_cli_output (vm, "%d@%U", fei, format_fib_entry, fei, FIB_ENTRY_FORMAT_BRIEF); })); } return (NULL); } VLIB_CLI_COMMAND (show_fib_entry, static) = { .path = "show fib entry", .function = show_fib_entry_command, .short_help = "show fib entry", };