Mcast rewrite no memcpy
[vpp.git] / src / 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/adj/adj_mcast.h>
21 #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  */
28 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
29
30 /* Adjacency packet/byte counters indexed by adjacency index. */
31 vlib_combined_counter_main_t adjacency_counters;
32
33 /*
34  * the single adj pool
35  */
36 ip_adjacency_t *adj_pool;
37
38 /**
39  * @brief Global Config for enabling per-adjacency counters.
40  * By default these are disabled.
41  */
42 int adj_per_adj_counters;
43
44 always_inline void
45 adj_poison (ip_adjacency_t * adj)
46 {
47     if (CLIB_DEBUG > 0)
48     {
49         memset (adj, 0xfe, sizeof (adj[0]));
50     }
51 }
52
53 ip_adjacency_t *
54 adj_alloc (fib_protocol_t proto)
55 {
56     ip_adjacency_t *adj;
57
58     pool_get(adj_pool, adj);
59
60     adj_poison(adj);
61
62     /* Make sure certain fields are always initialized. */
63     /* Validate adjacency counters. */
64     vlib_validate_combined_counter(&adjacency_counters,
65                                    adj_get_index(adj));
66
67     adj->rewrite_header.sw_if_index = ~0;
68     adj->n_adj = 1;
69     adj->lookup_next_index = 0;
70
71     fib_node_init(&adj->ia_node,
72                   FIB_NODE_TYPE_ADJ);
73     adj->ia_nh_proto = proto;
74     adj->ia_flags = 0;
75
76     ip4_main.lookup_main.adjacency_heap = adj_pool;
77     ip6_main.lookup_main.adjacency_heap = adj_pool;
78
79     return (adj);
80 }
81
82 static int
83 adj_index_is_special (adj_index_t adj_index)
84 {
85     if (ADJ_INDEX_INVALID == adj_index)
86         return (!0);
87
88     return (0);
89 }
90
91 /**
92  * @brief Pretty print helper function for formatting specific adjacencies.
93  * @param s - input string to format
94  * @param args - other args passed to format function such as:
95  *                 - vnet_main_t
96  *                 - ip_lookup_main_t
97  *                 - adj_index
98  */
99 u8 *
100 format_ip_adjacency (u8 * s, va_list * args)
101 {
102     format_ip_adjacency_flags_t fiaf;
103     ip_adjacency_t * adj;
104     u32 adj_index;
105
106     adj_index = va_arg (*args, u32);
107     fiaf = va_arg (*args, format_ip_adjacency_flags_t);
108     adj = adj_get(adj_index);
109   
110     switch (adj->lookup_next_index)
111     {
112     case IP_LOOKUP_NEXT_REWRITE:
113         s = format (s, "%U", format_adj_nbr, adj_index, 0);
114         break;
115     case IP_LOOKUP_NEXT_ARP:
116         s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
117         break;
118     case IP_LOOKUP_NEXT_GLEAN:
119         s = format (s, "%U", format_adj_glean, adj_index, 0);
120         break;
121     case IP_LOOKUP_NEXT_MIDCHAIN:
122         s = format (s, "%U", format_adj_midchain, adj_index, 2);
123         break;
124     case IP_LOOKUP_NEXT_MCAST:
125         s = format (s, "%U", format_adj_mcast, adj_index, 0);
126         break;
127     default:
128         break;
129     }
130
131     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
132     {
133         vlib_counter_t counts;
134
135         vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
136         s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
137         s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
138         s = format(s, "\n children:\n  ");
139         s = fib_node_children_format(adj->ia_node.fn_children, s);
140     }
141
142     return s;
143 }
144
145 /*
146  * adj_last_lock_gone
147  *
148  * last lock/reference to the adj has gone, we no longer need it.
149  */
150 static void
151 adj_last_lock_gone (ip_adjacency_t *adj)
152 {
153     vlib_main_t * vm = vlib_get_main();
154
155     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
156     ADJ_DBG(adj, "last-lock-gone");
157
158     vlib_worker_thread_barrier_sync (vm);
159
160     switch (adj->lookup_next_index)
161     {
162     case IP_LOOKUP_NEXT_MIDCHAIN:
163         dpo_reset(&adj->sub_type.midchain.next_dpo);
164         /* FALL THROUGH */
165     case IP_LOOKUP_NEXT_ARP:
166     case IP_LOOKUP_NEXT_REWRITE:
167         /*
168          * complete and incomplete nbr adjs
169          */
170         adj_nbr_remove(adj_get_index(adj),
171                        adj->ia_nh_proto,
172                        adj->ia_link,
173                        &adj->sub_type.nbr.next_hop,
174                        adj->rewrite_header.sw_if_index);
175         break;
176     case IP_LOOKUP_NEXT_GLEAN:
177         adj_glean_remove(adj->ia_nh_proto,
178                          adj->rewrite_header.sw_if_index);
179         break;
180     case IP_LOOKUP_NEXT_MCAST:
181         adj_mcast_remove(adj->ia_nh_proto,
182                          adj->rewrite_header.sw_if_index);
183         break;
184     default:
185         /*
186          * type not stored in any DB from which we need to remove it
187          */
188         break;
189     }
190
191     vlib_worker_thread_barrier_release(vm);
192
193     fib_node_deinit(&adj->ia_node);
194     pool_put(adj_pool, adj);
195 }
196
197 void
198 adj_lock (adj_index_t adj_index)
199 {
200     ip_adjacency_t *adj;
201
202     if (adj_index_is_special(adj_index))
203     {
204         return;
205     }
206
207     adj = adj_get(adj_index);
208     ASSERT(adj);
209
210     ADJ_DBG(adj, "lock");
211     fib_node_lock(&adj->ia_node);
212 }
213
214 void
215 adj_unlock (adj_index_t adj_index)
216 {
217     ip_adjacency_t *adj;
218
219     if (adj_index_is_special(adj_index))
220     {
221         return;
222     }
223
224     adj = adj_get(adj_index);
225     ASSERT(adj);
226
227     ADJ_DBG(adj, "unlock");
228     ASSERT(adj);
229
230     fib_node_unlock(&adj->ia_node);
231 }
232
233 u32
234 adj_child_add (adj_index_t adj_index,
235                fib_node_type_t child_type,
236                fib_node_index_t child_index)
237 {
238     ASSERT(ADJ_INDEX_INVALID != adj_index);
239     if (adj_index_is_special(adj_index))
240     {
241         return (~0);
242     }
243
244     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
245                                adj_index,
246                                child_type,
247                                child_index));
248 }
249
250 void
251 adj_child_remove (adj_index_t adj_index,
252                   u32 sibling_index)
253 {
254     if (adj_index_is_special(adj_index))
255     {
256         return;
257     }
258
259     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
260                           adj_index,
261                           sibling_index);
262 }
263
264 /*
265  * Context for the walk to update the cached feture flags.
266  */
267 typedef struct adj_feature_update_t_
268 {
269     u8 arc;
270     u8 enable;
271 } adj_feature_update_ctx_t;
272
273 static adj_walk_rc_t
274 adj_feature_update_walk_cb (adj_index_t ai,
275                             void *arg)
276 {
277     adj_feature_update_ctx_t *ctx = arg;
278     ip_adjacency_t *adj;
279
280     adj = adj_get(ai);
281
282     /*
283      * this ugly mess matches the feature arc that is changing with affected
284      * adjacencies
285      */
286     if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) &&
287          (VNET_LINK_IP6 == adj->ia_link)) ||
288         ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) &&
289          (VNET_LINK_IP4 == adj->ia_link)) ||
290         ((ctx->arc == mpls_main.output_feature_arc_index) &&
291          (VNET_LINK_MPLS == adj->ia_link)))
292     {
293         if (ctx->enable)
294             adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
295         else
296             adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES;
297     }
298     return (ADJ_WALK_RC_CONTINUE);
299 }
300
301 void
302 adj_feature_update (u32 sw_if_index,
303                     u8 arc_index,
304                     u8 is_enable)
305 {
306     /*
307      * Walk all the adjacencies on the interface to update the cached
308      * 'has-features' flag
309      */
310     adj_feature_update_ctx_t ctx = {
311         .arc = arc_index,
312         .enable = is_enable,
313     };
314     adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
315 }
316
317 /**
318  * @brief Walk the Adjacencies on a given interface
319  */
320 void
321 adj_walk (u32 sw_if_index,
322           adj_walk_cb_t cb,
323           void *ctx)
324 {
325     /*
326      * walk all the neighbor adjacencies
327      */
328     fib_protocol_t proto;
329
330     FOR_EACH_FIB_IP_PROTOCOL(proto)
331     {
332         adj_nbr_walk(sw_if_index, proto, cb, ctx);
333     }
334 }
335
336 /**
337  * @brief Return the link type of the adjacency
338  */
339 vnet_link_t
340 adj_get_link_type (adj_index_t ai)
341 {
342     const ip_adjacency_t *adj;
343
344     adj = adj_get(ai);
345
346     return (adj->ia_link); 
347 }
348
349 /**
350  * @brief Return the sw interface index of the adjacency.
351  */
352 u32
353 adj_get_sw_if_index (adj_index_t ai)
354 {
355     const ip_adjacency_t *adj;
356
357     adj = adj_get(ai);
358
359     return (adj->rewrite_header.sw_if_index);
360 }
361
362 /**
363  * @brief Return the rewrite string of the adjacency
364  */
365 const u8*
366 adj_get_rewrite (adj_index_t ai)
367 {
368     vnet_rewrite_header_t *rw;
369     ip_adjacency_t *adj;
370
371     adj = adj_get(ai);
372     rw = &adj->rewrite_header;
373
374     ASSERT (rw->data_bytes != 0xfefe);
375
376     return (rw->data - rw->data_bytes);
377 }
378
379 static fib_node_t *
380 adj_get_node (fib_node_index_t index)
381 {
382     ip_adjacency_t *adj;
383
384     adj = adj_get(index);
385
386     return (&adj->ia_node);
387 }
388
389 #define ADJ_FROM_NODE(_node)                                            \
390     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
391
392 static void
393 adj_node_last_lock_gone (fib_node_t *node)
394 {
395     adj_last_lock_gone(ADJ_FROM_NODE(node));
396 }
397
398 static fib_node_back_walk_rc_t
399 adj_back_walk_notify (fib_node_t *node,
400                       fib_node_back_walk_ctx_t *ctx)
401 {
402     /*
403      * Que pasa. yo soj en el final!
404      */
405     ASSERT(0);
406
407     return (FIB_NODE_BACK_WALK_CONTINUE);
408 }
409
410 /*
411  * Adjacency's graph node virtual function table
412  */
413 static const fib_node_vft_t adj_vft = {
414     .fnv_get = adj_get_node,
415     .fnv_last_lock = adj_node_last_lock_gone,
416     .fnv_back_walk = adj_back_walk_notify,
417 };
418
419 static clib_error_t *
420 adj_module_init (vlib_main_t * vm)
421 {
422     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
423
424     adj_nbr_module_init();
425     adj_glean_module_init();
426     adj_midchain_module_init();
427     adj_mcast_module_init();
428
429     /*
430      * one special adj to reserve index 0
431      */
432     special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
433
434     return (NULL);
435 }
436
437 VLIB_INIT_FUNCTION (adj_module_init);
438
439 static clib_error_t *
440 adj_show (vlib_main_t * vm,
441           unformat_input_t * input,
442           vlib_cli_command_t * cmd)
443 {
444     adj_index_t ai = ADJ_INDEX_INVALID;
445     u32 sw_if_index = ~0;
446     int summary = 0;
447
448     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
449     {
450         if (unformat (input, "%d", &ai))
451             ;
452         else if (unformat (input, "sum"))
453             summary = 1;
454         else if (unformat (input, "summary"))
455             summary = 1;
456         else if (unformat (input, "%U",
457                            unformat_vnet_sw_interface, vnet_get_main(),
458                            &sw_if_index))
459             ;
460         else
461             break;
462     }
463
464     if (summary)
465     {
466         vlib_cli_output (vm, "Number of adjacenies: %d", pool_elts(adj_pool));
467         vlib_cli_output (vm, "Per-adjacency counters: %s",
468                          (adj_are_counters_enabled() ?
469                           "enabled":
470                           "disabled"));
471     }
472     else
473     {
474         if (ADJ_INDEX_INVALID != ai)
475         {
476             if (pool_is_free_index(adj_pool, ai))
477             {
478                 vlib_cli_output (vm, "adjacency %d invalid", ai);
479                 return 0;
480             }
481
482             vlib_cli_output (vm, "[@%d] %U",
483                              ai,
484                              format_ip_adjacency,  ai,
485                              FORMAT_IP_ADJACENCY_DETAIL);
486         }
487         else
488         {
489             /* *INDENT-OFF* */
490             pool_foreach_index(ai, adj_pool,
491             ({
492                 if (~0 != sw_if_index &&
493                     sw_if_index != adj_get_sw_if_index(ai))
494                 {
495                 }
496                 else
497                 {
498                     vlib_cli_output (vm, "[@%d] %U",
499                                      ai,
500                                      format_ip_adjacency, ai,
501                                      FORMAT_IP_ADJACENCY_NONE);
502                 }
503             }));
504             /* *INDENT-ON* */
505         }
506     }
507     return 0;
508 }
509
510 /*?
511  * Show all adjacencies.
512  * @cliexpar
513  * @cliexstart{sh adj}
514  * [@0]
515  * [@1]  glean: loop0
516  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
517  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
518  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
519  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
520  * @cliexend
521  ?*/
522 VLIB_CLI_COMMAND (adj_show_command, static) = {
523     .path = "show adj",
524     .short_help = "show adj [<adj_index>] [interface] [summary]",
525     .function = adj_show,
526 };
527
528 /**
529  * @brief CLI invoked function to enable/disable per-adj counters
530  */
531 static clib_error_t *
532 adj_cli_counters_set (vlib_main_t * vm,
533                       unformat_input_t * input,
534                       vlib_cli_command_t * cmd)
535 {
536     clib_error_t *error = NULL;
537     int enable = ~0;
538
539     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
540     {
541         if (unformat (input, "enable"))
542             enable = 1;
543         else if (unformat (input, "disable"))
544             enable = 0;
545         else
546             break;
547     }
548
549     if (enable != ~0)
550     {
551         /* user requested something sensible */
552         adj_per_adj_counters = enable;
553     }
554     else
555     {
556         error = clib_error_return (0, "specify 'enable' or 'disable'");
557     }
558
559     return (error);
560 }
561
562 /*?
563  * Enabe/disble per-adjacency counters. This is optional because it comes with
564  * a non-negligible performance cost.
565  ?*/
566 VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
567     .path = "adjacency counters",
568     .short_help = "adjacency counters [enable|disable]",
569     .function = adj_cli_counters_set,
570 };