GRE tests and fixes
[vpp.git] / vnet / vnet / adj / adj.c
1 /*
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>
17 #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  */
27 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
28
29 /* Adjacency packet/byte counters indexed by adjacency index. */
30 vlib_combined_counter_main_t adjacency_counters;
31
32 /*
33  * the single adj pool
34  */
35 ip_adjacency_t *adj_pool;
36
37 always_inline void
38 adj_poison (ip_adjacency_t * adj)
39 {
40     if (CLIB_DEBUG > 0)
41     {
42         memset (adj, 0xfe, sizeof (adj[0]));
43     }
44 }
45
46 ip_adjacency_t *
47 adj_alloc (fib_protocol_t proto)
48 {
49     ip_adjacency_t *adj;
50
51     pool_get(adj_pool, adj);
52
53     adj_poison(adj);
54
55     /* Make sure certain fields are always initialized. */
56     /* Validate adjacency counters. */
57     vlib_validate_combined_counter(&adjacency_counters,
58                                    adj_get_index(adj));
59
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;
64     adj->lookup_next_index = 0;
65
66     fib_node_init(&adj->ia_node,
67                   FIB_NODE_TYPE_ADJ);
68     adj->ia_nh_proto = proto;
69
70     ip4_main.lookup_main.adjacency_heap = adj_pool;
71     ip6_main.lookup_main.adjacency_heap = adj_pool;
72
73     return (adj);
74 }
75
76 static int
77 adj_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  */
93 u8 *
94 format_ip_adjacency (u8 * s, va_list * args)
95 {
96     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);
103   
104     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;
118     default:
119         break;
120     }
121
122     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
123     {
124         s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
125         s = format (s, " node:[%d]:%U",
126                     adj->rewrite_header.node_index,
127                     format_vlib_node_name, vlib_get_main(),
128                     adj->rewrite_header.node_index);
129         s = format (s, " next:[%d]:%U",
130                     adj->rewrite_header.next_index,
131                     format_vlib_next_node_name,
132                     vlib_get_main(),
133                     adj->rewrite_header.node_index,
134                     adj->rewrite_header.next_index);
135         s = format(s, "\n children:\n  ");
136         s = fib_node_children_format(adj->ia_node.fn_children, s);
137     }
138
139     return s;
140 }
141
142 /*
143  * adj_last_lock_gone
144  *
145  * last lock/reference to the adj has gone, we no longer need it.
146  */
147 static void
148 adj_last_lock_gone (ip_adjacency_t *adj)
149 {
150     vlib_main_t * vm = vlib_get_main();
151
152     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
153     ADJ_DBG(adj, "last-lock-gone");
154
155     vlib_worker_thread_barrier_sync (vm);
156
157     switch (adj->lookup_next_index)
158     {
159     case IP_LOOKUP_NEXT_MIDCHAIN:
160         dpo_reset(&adj->sub_type.midchain.next_dpo);
161         /* FALL THROUGH */
162     case IP_LOOKUP_NEXT_ARP:
163     case IP_LOOKUP_NEXT_REWRITE:
164         /*
165          * complete and incomplete nbr adjs
166          */
167         adj_nbr_remove(adj_get_index(adj),
168                        adj->ia_nh_proto,
169                        adj->ia_link,
170                        &adj->sub_type.nbr.next_hop,
171                        adj->rewrite_header.sw_if_index);
172         break;
173     case IP_LOOKUP_NEXT_GLEAN:
174         adj_glean_remove(adj->ia_nh_proto,
175                          adj->rewrite_header.sw_if_index);
176         break;
177     default:
178         /*
179          * type not stored in any DB from which we need to remove it
180          */
181         break;
182     }
183
184     vlib_worker_thread_barrier_release(vm);
185
186     fib_node_deinit(&adj->ia_node);
187     pool_put(adj_pool, adj);
188 }
189
190 void
191 adj_lock (adj_index_t adj_index)
192 {
193     ip_adjacency_t *adj;
194
195     if (adj_index_is_special(adj_index))
196     {
197         return;
198     }
199
200     adj = adj_get(adj_index);
201     ASSERT(adj);
202
203     ADJ_DBG(adj, "lock");
204     fib_node_lock(&adj->ia_node);
205 }
206
207 void
208 adj_unlock (adj_index_t adj_index)
209 {
210     ip_adjacency_t *adj;
211
212     if (adj_index_is_special(adj_index))
213     {
214         return;
215     }
216
217     adj = adj_get(adj_index);
218     ASSERT(adj);
219
220     ADJ_DBG(adj, "unlock");
221     ASSERT(adj);
222
223     fib_node_unlock(&adj->ia_node);
224 }
225
226 u32
227 adj_child_add (adj_index_t adj_index,
228                fib_node_type_t child_type,
229                fib_node_index_t child_index)
230 {
231     ASSERT(ADJ_INDEX_INVALID != adj_index);
232     if (adj_index_is_special(adj_index))
233     {
234         return (~0);
235     }
236
237     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
238                                adj_index,
239                                child_type,
240                                child_index));
241 }
242
243 void
244 adj_child_remove (adj_index_t adj_index,
245                   u32 sibling_index)
246 {
247     if (adj_index_is_special(adj_index))
248     {
249         return;
250     }
251
252     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
253                           adj_index,
254                           sibling_index);
255 }
256
257 /**
258  * @brief Return the link type of the adjacency
259  */
260 vnet_link_t
261 adj_get_link_type (adj_index_t ai)
262 {
263     const ip_adjacency_t *adj;
264
265     adj = adj_get(ai);
266
267     return (adj->ia_link); 
268 }
269
270 /**
271  * @brief Return the sw interface index of the adjacency.
272  */
273 u32
274 adj_get_sw_if_index (adj_index_t ai)
275 {
276     const ip_adjacency_t *adj;
277
278     adj = adj_get(ai);
279
280     return (adj->rewrite_header.sw_if_index);
281 }
282
283 /**
284  * @brief Return the link type of the adjacency
285  */
286 const u8*
287 adj_get_rewrite (adj_index_t ai)
288 {
289     vnet_rewrite_header_t *rw;
290     ip_adjacency_t *adj;
291
292     adj = adj_get(ai);
293     rw = &adj->rewrite_header;
294
295     ASSERT (rw->data_bytes != 0xfefe);
296
297     return (rw->data - rw->data_bytes);
298 }
299
300 static fib_node_t *
301 adj_get_node (fib_node_index_t index)
302 {
303     ip_adjacency_t *adj;
304
305     adj = adj_get(index);
306
307     return (&adj->ia_node);
308 }
309
310 #define ADJ_FROM_NODE(_node)                                            \
311     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
312
313 static void
314 adj_node_last_lock_gone (fib_node_t *node)
315 {
316     adj_last_lock_gone(ADJ_FROM_NODE(node));
317 }
318
319 static fib_node_back_walk_rc_t
320 adj_back_walk_notify (fib_node_t *node,
321                       fib_node_back_walk_ctx_t *ctx)
322 {
323     /*
324      * Que pasa. yo soj en el final!
325      */
326     ASSERT(0);
327
328     return (FIB_NODE_BACK_WALK_CONTINUE);
329 }
330
331 /*
332  * Adjacency's graph node virtual function table
333  */
334 static const fib_node_vft_t adj_vft = {
335     .fnv_get = adj_get_node,
336     .fnv_last_lock = adj_node_last_lock_gone,
337     .fnv_back_walk = adj_back_walk_notify,
338 };
339
340 static clib_error_t *
341 adj_module_init (vlib_main_t * vm)
342 {
343     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
344
345     adj_nbr_module_init();
346     adj_glean_module_init();
347     adj_midchain_module_init();
348
349     /*
350      * one special adj to reserve index 0
351      */
352     special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
353
354     return (NULL);
355 }
356
357 VLIB_INIT_FUNCTION (adj_module_init);
358
359 static clib_error_t *
360 adj_show (vlib_main_t * vm,
361           unformat_input_t * input,
362           vlib_cli_command_t * cmd)
363 {
364     adj_index_t ai = ADJ_INDEX_INVALID;
365     u32 sw_if_index = ~0;
366
367     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
368     {
369         if (unformat (input, "%d", &ai))
370             ;
371         else if (unformat (input, "%U",
372                            unformat_vnet_sw_interface, vnet_get_main(),
373                            &sw_if_index))
374             ;
375         else
376             break;
377     }
378
379     if (ADJ_INDEX_INVALID != ai)
380     {
381         if (pool_is_free_index(adj_pool, ai))
382         {
383             vlib_cli_output (vm, "adjacency %d invalid", ai);
384             return 0;
385         }
386
387         vlib_cli_output (vm, "[@%d] %U",
388                          ai,
389                          format_ip_adjacency,  ai,
390                          FORMAT_IP_ADJACENCY_DETAIL);
391     }
392     else
393     {
394         /* *INDENT-OFF* */
395         pool_foreach_index(ai, adj_pool,
396         ({
397             if (~0 != sw_if_index &&
398                 sw_if_index != adj_get_sw_if_index(ai))
399             {
400             }
401             else
402             {
403                 vlib_cli_output (vm, "[@%d] %U",
404                                  ai,
405                                  format_ip_adjacency, ai,
406                                  FORMAT_IP_ADJACENCY_NONE);
407             }
408         }));
409         /* *INDENT-ON* */
410     }
411
412     return 0;
413 }
414
415 /*?
416  * Show all adjacencies.
417  * @cliexpar
418  * @cliexstart{sh adj}
419  * [@0]
420  * [@1]  glean: loop0
421  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
422  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
423  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
424  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
425  * @cliexend
426  ?*/
427 VLIB_CLI_COMMAND (adj_show_command, static) = {
428     .path = "show adj",
429     .short_help = "show adj [<adj_index>] [interface]",
430     .function = adj_show,
431 };
432
433 /* 
434  * DEPRECATED: DO NOT USE
435  */
436 ip_adjacency_t *
437 ip_add_adjacency (ip_lookup_main_t * lm,
438                   ip_adjacency_t * copy_adj,
439                   u32 n_adj,
440                   u32 * adj_index_return)
441 {
442   ip_adjacency_t * adj;
443
444   ASSERT(1==n_adj);
445
446   adj = adj_alloc(FIB_PROTOCOL_IP4);
447
448   if (copy_adj)
449       *adj = *copy_adj;
450
451   *adj_index_return = adj_get_index(adj);
452   return adj;
453 }