blob: d0be0f0eaff70c99018fab8a1648db56533b1e34 [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>
20#include <vnet/fib/fib_node_list.h>
21
22/*
23 * Special Adj with index zero. we need to define this since the v4 mtrie
24 * assumes an index of 0 implies the ply is empty. therefore all 'real'
25 * adjs need a non-zero index.
26 */
27static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
28
29/* Adjacency packet/byte counters indexed by adjacency index. */
30vlib_combined_counter_main_t adjacency_counters;
31
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010032/*
33 * the single adj pool
34 */
35ip_adjacency_t *adj_pool;
36
Neale Ranns0bfe5d82016-08-25 15:29:12 +010037always_inline void
38adj_poison (ip_adjacency_t * adj)
39{
40 if (CLIB_DEBUG > 0)
41 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010042 memset (adj, 0xfe, sizeof (adj[0]));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 }
44}
45
46ip_adjacency_t *
47adj_alloc (fib_protocol_t proto)
48{
49 ip_adjacency_t *adj;
50
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010051 pool_get(adj_pool, adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052
53 adj_poison(adj);
54
55 /* Make sure certain fields are always initialized. */
56 /* Validate adjacency counters. */
57 vlib_validate_combined_counter(&adjacency_counters,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010058 adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010059
60 adj->rewrite_header.sw_if_index = ~0;
61 adj->mcast_group_index = ~0;
62 adj->saved_lookup_next_index = 0;
63 adj->n_adj = 1;
Neale Ranns177bbdc2016-11-15 09:46:51 +000064 adj->lookup_next_index = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065
66 fib_node_init(&adj->ia_node,
67 FIB_NODE_TYPE_ADJ);
68 adj->ia_nh_proto = proto;
Neale Ranns19c68d22016-12-07 15:38:14 +000069 adj->ia_flags = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010071 ip4_main.lookup_main.adjacency_heap = adj_pool;
72 ip6_main.lookup_main.adjacency_heap = adj_pool;
73
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074 return (adj);
75}
76
77static int
78adj_index_is_special (adj_index_t adj_index)
79{
80 if (ADJ_INDEX_INVALID == adj_index)
81 return (!0);
82
83 return (0);
84}
85
86/**
87 * @brief Pretty print helper function for formatting specific adjacencies.
88 * @param s - input string to format
89 * @param args - other args passed to format function such as:
90 * - vnet_main_t
91 * - ip_lookup_main_t
92 * - adj_index
93 */
94u8 *
95format_ip_adjacency (u8 * s, va_list * args)
96{
Neale Rannsb80c5362016-10-08 13:03:40 +010097 format_ip_adjacency_flags_t fiaf;
98 ip_adjacency_t * adj;
99 u32 adj_index;
100
101 adj_index = va_arg (*args, u32);
102 fiaf = va_arg (*args, format_ip_adjacency_flags_t);
103 adj = adj_get(adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100104
Neale Rannsb80c5362016-10-08 13:03:40 +0100105 switch (adj->lookup_next_index)
106 {
107 case IP_LOOKUP_NEXT_REWRITE:
108 s = format (s, "%U", format_adj_nbr, adj_index, 0);
109 break;
110 case IP_LOOKUP_NEXT_ARP:
111 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
112 break;
113 case IP_LOOKUP_NEXT_GLEAN:
114 s = format (s, "%U", format_adj_glean, adj_index, 0);
115 break;
116 case IP_LOOKUP_NEXT_MIDCHAIN:
117 s = format (s, "%U", format_adj_midchain, adj_index, 2);
118 break;
119 default:
120 break;
121 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100122
Neale Rannsb80c5362016-10-08 13:03:40 +0100123 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
124 {
Neale Ranns044183f2017-01-24 01:34:25 -0800125 vlib_counter_t counts;
126
127 vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
128 s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100129 s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
130 s = format (s, " node:[%d]:%U",
131 adj->rewrite_header.node_index,
132 format_vlib_node_name, vlib_get_main(),
133 adj->rewrite_header.node_index);
134 s = format (s, " next:[%d]:%U",
135 adj->rewrite_header.next_index,
136 format_vlib_next_node_name,
137 vlib_get_main(),
138 adj->rewrite_header.node_index,
139 adj->rewrite_header.next_index);
140 s = format(s, "\n children:\n ");
141 s = fib_node_children_format(adj->ia_node.fn_children, s);
142 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100143
Neale Rannsb80c5362016-10-08 13:03:40 +0100144 return s;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145}
146
147/*
148 * adj_last_lock_gone
149 *
150 * last lock/reference to the adj has gone, we no longer need it.
151 */
152static void
153adj_last_lock_gone (ip_adjacency_t *adj)
154{
Neale Rannsb80c5362016-10-08 13:03:40 +0100155 vlib_main_t * vm = vlib_get_main();
156
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100157 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
158 ADJ_DBG(adj, "last-lock-gone");
159
Neale Rannsb80c5362016-10-08 13:03:40 +0100160 vlib_worker_thread_barrier_sync (vm);
161
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100162 switch (adj->lookup_next_index)
163 {
164 case IP_LOOKUP_NEXT_MIDCHAIN:
165 dpo_reset(&adj->sub_type.midchain.next_dpo);
166 /* FALL THROUGH */
167 case IP_LOOKUP_NEXT_ARP:
168 case IP_LOOKUP_NEXT_REWRITE:
169 /*
170 * complete and incomplete nbr adjs
171 */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000172 adj_nbr_remove(adj_get_index(adj),
173 adj->ia_nh_proto,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100174 adj->ia_link,
175 &adj->sub_type.nbr.next_hop,
176 adj->rewrite_header.sw_if_index);
177 break;
178 case IP_LOOKUP_NEXT_GLEAN:
179 adj_glean_remove(adj->ia_nh_proto,
180 adj->rewrite_header.sw_if_index);
181 break;
182 default:
183 /*
184 * type not stored in any DB from which we need to remove it
185 */
186 break;
187 }
188
Neale Rannsb80c5362016-10-08 13:03:40 +0100189 vlib_worker_thread_barrier_release(vm);
190
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191 fib_node_deinit(&adj->ia_node);
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100192 pool_put(adj_pool, adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100193}
194
195void
196adj_lock (adj_index_t adj_index)
197{
198 ip_adjacency_t *adj;
199
200 if (adj_index_is_special(adj_index))
201 {
202 return;
203 }
204
205 adj = adj_get(adj_index);
206 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100207
208 ADJ_DBG(adj, "lock");
209 fib_node_lock(&adj->ia_node);
210}
211
212void
213adj_unlock (adj_index_t adj_index)
214{
215 ip_adjacency_t *adj;
216
217 if (adj_index_is_special(adj_index))
218 {
219 return;
220 }
221
222 adj = adj_get(adj_index);
223 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100224
225 ADJ_DBG(adj, "unlock");
226 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100227
228 fib_node_unlock(&adj->ia_node);
229}
230
231u32
232adj_child_add (adj_index_t adj_index,
233 fib_node_type_t child_type,
234 fib_node_index_t child_index)
235{
236 ASSERT(ADJ_INDEX_INVALID != adj_index);
237 if (adj_index_is_special(adj_index))
238 {
239 return (~0);
240 }
241
242 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
243 adj_index,
244 child_type,
245 child_index));
246}
247
248void
249adj_child_remove (adj_index_t adj_index,
250 u32 sibling_index)
251{
252 if (adj_index_is_special(adj_index))
253 {
254 return;
255 }
256
257 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
258 adj_index,
259 sibling_index);
260}
261
Neale Rannsb80c5362016-10-08 13:03:40 +0100262/**
263 * @brief Return the link type of the adjacency
264 */
265vnet_link_t
266adj_get_link_type (adj_index_t ai)
267{
268 const ip_adjacency_t *adj;
269
270 adj = adj_get(ai);
271
272 return (adj->ia_link);
273}
274
275/**
276 * @brief Return the sw interface index of the adjacency.
277 */
278u32
279adj_get_sw_if_index (adj_index_t ai)
280{
281 const ip_adjacency_t *adj;
282
283 adj = adj_get(ai);
284
285 return (adj->rewrite_header.sw_if_index);
286}
287
288/**
289 * @brief Return the link type of the adjacency
290 */
291const u8*
292adj_get_rewrite (adj_index_t ai)
293{
294 vnet_rewrite_header_t *rw;
295 ip_adjacency_t *adj;
296
297 adj = adj_get(ai);
298 rw = &adj->rewrite_header;
299
300 ASSERT (rw->data_bytes != 0xfefe);
301
302 return (rw->data - rw->data_bytes);
303}
304
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100305static fib_node_t *
306adj_get_node (fib_node_index_t index)
307{
308 ip_adjacency_t *adj;
309
310 adj = adj_get(index);
311
312 return (&adj->ia_node);
313}
314
315#define ADJ_FROM_NODE(_node) \
316 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
317
318static void
319adj_node_last_lock_gone (fib_node_t *node)
320{
321 adj_last_lock_gone(ADJ_FROM_NODE(node));
322}
323
324static fib_node_back_walk_rc_t
325adj_back_walk_notify (fib_node_t *node,
326 fib_node_back_walk_ctx_t *ctx)
327{
328 /*
329 * Que pasa. yo soj en el final!
330 */
331 ASSERT(0);
332
333 return (FIB_NODE_BACK_WALK_CONTINUE);
334}
335
336/*
337 * Adjacency's graph node virtual function table
338 */
339static const fib_node_vft_t adj_vft = {
340 .fnv_get = adj_get_node,
341 .fnv_last_lock = adj_node_last_lock_gone,
342 .fnv_back_walk = adj_back_walk_notify,
343};
344
345static clib_error_t *
346adj_module_init (vlib_main_t * vm)
347{
348 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
349
350 adj_nbr_module_init();
351 adj_glean_module_init();
352 adj_midchain_module_init();
353
354 /*
Neale Rannsb80c5362016-10-08 13:03:40 +0100355 * one special adj to reserve index 0
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100356 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100357 special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
358
359 return (NULL);
360}
361
362VLIB_INIT_FUNCTION (adj_module_init);
363
Neale Rannsb80c5362016-10-08 13:03:40 +0100364static clib_error_t *
365adj_show (vlib_main_t * vm,
366 unformat_input_t * input,
367 vlib_cli_command_t * cmd)
368{
369 adj_index_t ai = ADJ_INDEX_INVALID;
370 u32 sw_if_index = ~0;
371
372 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
373 {
374 if (unformat (input, "%d", &ai))
375 ;
376 else if (unformat (input, "%U",
377 unformat_vnet_sw_interface, vnet_get_main(),
378 &sw_if_index))
379 ;
380 else
381 break;
382 }
383
384 if (ADJ_INDEX_INVALID != ai)
385 {
Neale Ranns177bbdc2016-11-15 09:46:51 +0000386 if (pool_is_free_index(adj_pool, ai))
387 {
388 vlib_cli_output (vm, "adjacency %d invalid", ai);
389 return 0;
390 }
391
Neale Rannsb80c5362016-10-08 13:03:40 +0100392 vlib_cli_output (vm, "[@%d] %U",
393 ai,
394 format_ip_adjacency, ai,
395 FORMAT_IP_ADJACENCY_DETAIL);
396 }
397 else
398 {
399 /* *INDENT-OFF* */
400 pool_foreach_index(ai, adj_pool,
401 ({
402 if (~0 != sw_if_index &&
Neale Ranns8b37b872016-11-21 12:25:22 +0000403 sw_if_index != adj_get_sw_if_index(ai))
Neale Rannsb80c5362016-10-08 13:03:40 +0100404 {
Neale Ranns8b37b872016-11-21 12:25:22 +0000405 }
406 else
407 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100408 vlib_cli_output (vm, "[@%d] %U",
409 ai,
410 format_ip_adjacency, ai,
411 FORMAT_IP_ADJACENCY_NONE);
412 }
413 }));
414 /* *INDENT-ON* */
415 }
416
417 return 0;
418}
419
420/*?
421 * Show all adjacencies.
422 * @cliexpar
423 * @cliexstart{sh adj}
424 * [@0]
425 * [@1] glean: loop0
426 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
427 * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
428 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
429 * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
430 * @cliexend
431 ?*/
432VLIB_CLI_COMMAND (adj_show_command, static) = {
433 .path = "show adj",
434 .short_help = "show adj [<adj_index>] [interface]",
435 .function = adj_show,
436};
437
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100438/*
439 * DEPRECATED: DO NOT USE
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100440 */
441ip_adjacency_t *
442ip_add_adjacency (ip_lookup_main_t * lm,
443 ip_adjacency_t * copy_adj,
444 u32 n_adj,
445 u32 * adj_index_return)
446{
447 ip_adjacency_t * adj;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100448
449 ASSERT(1==n_adj);
450
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100451 adj = adj_alloc(FIB_PROTOCOL_IP4);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100452
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100453 if (copy_adj)
454 *adj = *copy_adj;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100455
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100456 *adj_index_return = adj_get_index(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100457 return adj;
458}