blob: a99f173f6d0185a2c04f0a7f0adbfcc9220e99a2 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/adj/adj.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010017#include <vnet/adj/adj_internal.h>
18#include <vnet/adj/adj_glean.h>
19#include <vnet/adj/adj_midchain.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000020#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/fib_node_list.h>
22
23/*
24 * Special Adj with index zero. we need to define this since the v4 mtrie
25 * assumes an index of 0 implies the ply is empty. therefore all 'real'
26 * adjs need a non-zero index.
27 */
28static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
29
30/* Adjacency packet/byte counters indexed by adjacency index. */
31vlib_combined_counter_main_t adjacency_counters;
32
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010033/*
34 * the single adj pool
35 */
36ip_adjacency_t *adj_pool;
37
Neale Ranns0bfe5d82016-08-25 15:29:12 +010038always_inline void
39adj_poison (ip_adjacency_t * adj)
40{
41 if (CLIB_DEBUG > 0)
42 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 memset (adj, 0xfe, sizeof (adj[0]));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010044 }
45}
46
47ip_adjacency_t *
48adj_alloc (fib_protocol_t proto)
49{
50 ip_adjacency_t *adj;
51
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010052 pool_get(adj_pool, adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010053
54 adj_poison(adj);
55
56 /* Make sure certain fields are always initialized. */
57 /* Validate adjacency counters. */
58 vlib_validate_combined_counter(&adjacency_counters,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010059 adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010060
61 adj->rewrite_header.sw_if_index = ~0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010062 adj->n_adj = 1;
Neale Ranns177bbdc2016-11-15 09:46:51 +000063 adj->lookup_next_index = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010064
65 fib_node_init(&adj->ia_node,
66 FIB_NODE_TYPE_ADJ);
67 adj->ia_nh_proto = proto;
Neale Ranns19c68d22016-12-07 15:38:14 +000068 adj->ia_flags = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010069
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010070 ip4_main.lookup_main.adjacency_heap = adj_pool;
71 ip6_main.lookup_main.adjacency_heap = adj_pool;
72
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 return (adj);
74}
75
76static int
77adj_index_is_special (adj_index_t adj_index)
78{
79 if (ADJ_INDEX_INVALID == adj_index)
80 return (!0);
81
82 return (0);
83}
84
85/**
86 * @brief Pretty print helper function for formatting specific adjacencies.
87 * @param s - input string to format
88 * @param args - other args passed to format function such as:
89 * - vnet_main_t
90 * - ip_lookup_main_t
91 * - adj_index
92 */
93u8 *
94format_ip_adjacency (u8 * s, va_list * args)
95{
Neale Rannsb80c5362016-10-08 13:03:40 +010096 format_ip_adjacency_flags_t fiaf;
97 ip_adjacency_t * adj;
98 u32 adj_index;
99
100 adj_index = va_arg (*args, u32);
101 fiaf = va_arg (*args, format_ip_adjacency_flags_t);
102 adj = adj_get(adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100103
Neale Rannsb80c5362016-10-08 13:03:40 +0100104 switch (adj->lookup_next_index)
105 {
106 case IP_LOOKUP_NEXT_REWRITE:
107 s = format (s, "%U", format_adj_nbr, adj_index, 0);
108 break;
109 case IP_LOOKUP_NEXT_ARP:
110 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
111 break;
112 case IP_LOOKUP_NEXT_GLEAN:
113 s = format (s, "%U", format_adj_glean, adj_index, 0);
114 break;
115 case IP_LOOKUP_NEXT_MIDCHAIN:
116 s = format (s, "%U", format_adj_midchain, adj_index, 2);
117 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000118 case IP_LOOKUP_NEXT_MCAST:
119 s = format (s, "%U", format_adj_mcast, adj_index, 0);
120 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100121 default:
122 break;
123 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100124
Neale Rannsb80c5362016-10-08 13:03:40 +0100125 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
126 {
Neale Ranns044183f2017-01-24 01:34:25 -0800127 vlib_counter_t counts;
128
129 vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
130 s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100131 s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
132 s = format (s, " node:[%d]:%U",
133 adj->rewrite_header.node_index,
134 format_vlib_node_name, vlib_get_main(),
135 adj->rewrite_header.node_index);
136 s = format (s, " next:[%d]:%U",
137 adj->rewrite_header.next_index,
138 format_vlib_next_node_name,
139 vlib_get_main(),
140 adj->rewrite_header.node_index,
141 adj->rewrite_header.next_index);
142 s = format(s, "\n children:\n ");
143 s = fib_node_children_format(adj->ia_node.fn_children, s);
144 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145
Neale Rannsb80c5362016-10-08 13:03:40 +0100146 return s;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147}
148
149/*
150 * adj_last_lock_gone
151 *
152 * last lock/reference to the adj has gone, we no longer need it.
153 */
154static void
155adj_last_lock_gone (ip_adjacency_t *adj)
156{
Neale Rannsb80c5362016-10-08 13:03:40 +0100157 vlib_main_t * vm = vlib_get_main();
158
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100159 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
160 ADJ_DBG(adj, "last-lock-gone");
161
Neale Rannsb80c5362016-10-08 13:03:40 +0100162 vlib_worker_thread_barrier_sync (vm);
163
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 switch (adj->lookup_next_index)
165 {
166 case IP_LOOKUP_NEXT_MIDCHAIN:
167 dpo_reset(&adj->sub_type.midchain.next_dpo);
168 /* FALL THROUGH */
169 case IP_LOOKUP_NEXT_ARP:
170 case IP_LOOKUP_NEXT_REWRITE:
171 /*
172 * complete and incomplete nbr adjs
173 */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000174 adj_nbr_remove(adj_get_index(adj),
175 adj->ia_nh_proto,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100176 adj->ia_link,
177 &adj->sub_type.nbr.next_hop,
178 adj->rewrite_header.sw_if_index);
179 break;
180 case IP_LOOKUP_NEXT_GLEAN:
181 adj_glean_remove(adj->ia_nh_proto,
182 adj->rewrite_header.sw_if_index);
183 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000184 case IP_LOOKUP_NEXT_MCAST:
185 adj_mcast_remove(adj->ia_nh_proto,
186 adj->rewrite_header.sw_if_index);
187 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100188 default:
189 /*
190 * type not stored in any DB from which we need to remove it
191 */
192 break;
193 }
194
Neale Rannsb80c5362016-10-08 13:03:40 +0100195 vlib_worker_thread_barrier_release(vm);
196
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100197 fib_node_deinit(&adj->ia_node);
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100198 pool_put(adj_pool, adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100199}
200
201void
202adj_lock (adj_index_t adj_index)
203{
204 ip_adjacency_t *adj;
205
206 if (adj_index_is_special(adj_index))
207 {
208 return;
209 }
210
211 adj = adj_get(adj_index);
212 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213
214 ADJ_DBG(adj, "lock");
215 fib_node_lock(&adj->ia_node);
216}
217
218void
219adj_unlock (adj_index_t adj_index)
220{
221 ip_adjacency_t *adj;
222
223 if (adj_index_is_special(adj_index))
224 {
225 return;
226 }
227
228 adj = adj_get(adj_index);
229 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100230
231 ADJ_DBG(adj, "unlock");
232 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100233
234 fib_node_unlock(&adj->ia_node);
235}
236
237u32
238adj_child_add (adj_index_t adj_index,
239 fib_node_type_t child_type,
240 fib_node_index_t child_index)
241{
242 ASSERT(ADJ_INDEX_INVALID != adj_index);
243 if (adj_index_is_special(adj_index))
244 {
245 return (~0);
246 }
247
248 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
249 adj_index,
250 child_type,
251 child_index));
252}
253
254void
255adj_child_remove (adj_index_t adj_index,
256 u32 sibling_index)
257{
258 if (adj_index_is_special(adj_index))
259 {
260 return;
261 }
262
263 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
264 adj_index,
265 sibling_index);
266}
267
Neale Rannsb80c5362016-10-08 13:03:40 +0100268/**
269 * @brief Return the link type of the adjacency
270 */
271vnet_link_t
272adj_get_link_type (adj_index_t ai)
273{
274 const ip_adjacency_t *adj;
275
276 adj = adj_get(ai);
277
278 return (adj->ia_link);
279}
280
281/**
282 * @brief Return the sw interface index of the adjacency.
283 */
284u32
285adj_get_sw_if_index (adj_index_t ai)
286{
287 const ip_adjacency_t *adj;
288
289 adj = adj_get(ai);
290
291 return (adj->rewrite_header.sw_if_index);
292}
293
294/**
295 * @brief Return the link type of the adjacency
296 */
297const u8*
298adj_get_rewrite (adj_index_t ai)
299{
300 vnet_rewrite_header_t *rw;
301 ip_adjacency_t *adj;
302
303 adj = adj_get(ai);
304 rw = &adj->rewrite_header;
305
306 ASSERT (rw->data_bytes != 0xfefe);
307
308 return (rw->data - rw->data_bytes);
309}
310
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100311static fib_node_t *
312adj_get_node (fib_node_index_t index)
313{
314 ip_adjacency_t *adj;
315
316 adj = adj_get(index);
317
318 return (&adj->ia_node);
319}
320
321#define ADJ_FROM_NODE(_node) \
322 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
323
324static void
325adj_node_last_lock_gone (fib_node_t *node)
326{
327 adj_last_lock_gone(ADJ_FROM_NODE(node));
328}
329
330static fib_node_back_walk_rc_t
331adj_back_walk_notify (fib_node_t *node,
332 fib_node_back_walk_ctx_t *ctx)
333{
334 /*
335 * Que pasa. yo soj en el final!
336 */
337 ASSERT(0);
338
339 return (FIB_NODE_BACK_WALK_CONTINUE);
340}
341
342/*
343 * Adjacency's graph node virtual function table
344 */
345static const fib_node_vft_t adj_vft = {
346 .fnv_get = adj_get_node,
347 .fnv_last_lock = adj_node_last_lock_gone,
348 .fnv_back_walk = adj_back_walk_notify,
349};
350
351static clib_error_t *
352adj_module_init (vlib_main_t * vm)
353{
354 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
355
356 adj_nbr_module_init();
357 adj_glean_module_init();
358 adj_midchain_module_init();
Neale Ranns32e1c012016-11-22 17:07:28 +0000359 adj_mcast_module_init();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100360
361 /*
Neale Rannsb80c5362016-10-08 13:03:40 +0100362 * one special adj to reserve index 0
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100363 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100364 special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
365
366 return (NULL);
367}
368
369VLIB_INIT_FUNCTION (adj_module_init);
370
Neale Rannsb80c5362016-10-08 13:03:40 +0100371static clib_error_t *
372adj_show (vlib_main_t * vm,
373 unformat_input_t * input,
374 vlib_cli_command_t * cmd)
375{
376 adj_index_t ai = ADJ_INDEX_INVALID;
377 u32 sw_if_index = ~0;
378
379 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
380 {
381 if (unformat (input, "%d", &ai))
382 ;
383 else if (unformat (input, "%U",
384 unformat_vnet_sw_interface, vnet_get_main(),
385 &sw_if_index))
386 ;
387 else
388 break;
389 }
390
391 if (ADJ_INDEX_INVALID != ai)
392 {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000393 if (pool_is_free_index(adj_pool, ai))
394 {
395 vlib_cli_output (vm, "adjacency %d invalid", ai);
396 return 0;
397 }
398
Neale Rannsb80c5362016-10-08 13:03:40 +0100399 vlib_cli_output (vm, "[@%d] %U",
400 ai,
401 format_ip_adjacency, ai,
402 FORMAT_IP_ADJACENCY_DETAIL);
403 }
404 else
405 {
406 /* *INDENT-OFF* */
407 pool_foreach_index(ai, adj_pool,
408 ({
409 if (~0 != sw_if_index &&
Neale Ranns8b37b872016-11-21 12:25:22 +0000410 sw_if_index != adj_get_sw_if_index(ai))
Neale Rannsb80c5362016-10-08 13:03:40 +0100411 {
Neale Ranns8b37b872016-11-21 12:25:22 +0000412 }
413 else
414 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100415 vlib_cli_output (vm, "[@%d] %U",
416 ai,
417 format_ip_adjacency, ai,
418 FORMAT_IP_ADJACENCY_NONE);
419 }
420 }));
421 /* *INDENT-ON* */
422 }
423
424 return 0;
425}
426
427/*?
428 * Show all adjacencies.
429 * @cliexpar
430 * @cliexstart{sh adj}
431 * [@0]
432 * [@1] glean: loop0
433 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
434 * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
435 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
436 * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
437 * @cliexend
438 ?*/
439VLIB_CLI_COMMAND (adj_show_command, static) = {
440 .path = "show adj",
441 .short_help = "show adj [<adj_index>] [interface]",
442 .function = adj_show,
443};
444
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100445/*
446 * DEPRECATED: DO NOT USE
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100447 */
448ip_adjacency_t *
449ip_add_adjacency (ip_lookup_main_t * lm,
450 ip_adjacency_t * copy_adj,
451 u32 n_adj,
452 u32 * adj_index_return)
453{
454 ip_adjacency_t * adj;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100455
456 ASSERT(1==n_adj);
457
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100458 adj = adj_alloc(FIB_PROTOCOL_IP4);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100459
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100460 if (copy_adj)
461 *adj = *copy_adj;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100462
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100463 *adj_index_return = adj_get_index(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100464 return adj;
465}