FIB Memory Usage Diagnostics
[vpp.git] / vnet / vnet / adj / adj_midchain.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_nbr.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/adj/adj_l2.h>
19 #include <vnet/adj/adj_midchain.h>
20 #include <vnet/ethernet/arp_packet.h>
21 #include <vnet/dpo/drop_dpo.h>
22 #include <vnet/fib/fib_walk.h>
23
24 /**
25  * The two midchain tx feature node indices
26  */
27 static u32 adj_midchain_tx_feature_node[FIB_LINK_NUM];
28 static u32 adj_midchain_tx_no_count_feature_node[FIB_LINK_NUM];
29
30 /**
31  * @brief Trace data for packets traversing the midchain tx node
32  */
33 typedef struct adj_midchain_tx_trace_t_
34 {
35     /**
36      * @brief the midchain adj we are traversing
37      */
38     adj_index_t ai;
39 } adj_midchain_tx_trace_t;
40
41 always_inline uword
42 adj_mdichain_tx_inline (vlib_main_t * vm,
43                         vlib_node_runtime_t * node,
44                         vlib_frame_t * frame,
45                         int interface_count)
46 {
47     u32 * from, * to_next, n_left_from, n_left_to_next;
48     u32 next_index;
49     vnet_main_t *vnm = vnet_get_main ();
50     vnet_interface_main_t *im = &vnm->interface_main;
51     u32 cpu_index = vm->cpu_index;
52
53     /* Vector of buffer / pkt indices we're supposed to process */
54     from = vlib_frame_vector_args (frame);
55
56     /* Number of buffers / pkts */
57     n_left_from = frame->n_vectors;
58
59     /* Speculatively send the first buffer to the last disposition we used */
60     next_index = node->cached_next_index;
61
62     while (n_left_from > 0)
63     {
64         /* set up to enqueue to our disposition with index = next_index */
65         vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
66
67         /*
68          * FIXME DUAL LOOP
69          */
70
71         while (n_left_from > 0 && n_left_to_next > 0)
72         {
73             u32 bi0, adj_index0, next0;
74             const ip_adjacency_t * adj0;
75             const dpo_id_t *dpo0;
76             vlib_buffer_t * b0;
77
78             bi0 = from[0];
79             to_next[0] = bi0;
80             from += 1;
81             to_next += 1;
82             n_left_from -= 1;
83             n_left_to_next -= 1;
84
85             b0 = vlib_get_buffer(vm, bi0);
86
87             /* Follow the DPO on which the midchain is stacked */
88             adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
89             adj0 = adj_get(adj_index0);
90             dpo0 = &adj0->sub_type.midchain.next_dpo;
91             next0 = dpo0->dpoi_next_node;
92             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
93
94             if (interface_count)
95             {
96                 vlib_increment_combined_counter (im->combined_sw_if_counters
97                                                  + VNET_INTERFACE_COUNTER_TX,
98                                                  cpu_index,
99                                                  adj0->rewrite_header.sw_if_index,
100                                                  1,
101                                                  vlib_buffer_length_in_chain (vm, b0));
102             }
103
104             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
105             {
106                 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
107                                                               b0, sizeof (*tr));
108                 tr->ai = adj_index0;
109             }
110
111             vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
112                                              to_next, n_left_to_next,
113                                              bi0, next0);
114         }
115
116         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
117     }
118
119     vlib_node_increment_counter (vm, gre_input_node.index,
120                                  GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
121
122     return frame->n_vectors;
123 }
124
125 static u8 *
126 format_adj_midchain_tx_trace (u8 * s, va_list * args)
127 {
128     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
129     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
130     adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*);
131
132     s = format(s, "adj-midchain:[%d]:%U", tr->ai,
133                format_ip_adjacency, vnet_get_main(), tr->ai,
134                FORMAT_IP_ADJACENCY_NONE);
135
136     return (s);
137 }
138
139 static uword
140 adj_midchain_tx (vlib_main_t * vm,
141                  vlib_node_runtime_t * node,
142                  vlib_frame_t * frame)
143 {
144     return (adj_mdichain_tx_inline(vm, node, frame, 1));
145 }
146
147 VLIB_REGISTER_NODE (adj_midchain_tx_node, static) = {
148     .function = adj_midchain_tx,
149     .name = "adj-midchain-tx",
150     .vector_size = sizeof (u32),
151
152     .format_trace = format_adj_midchain_tx_trace,
153
154     .n_next_nodes = 1,
155     .next_nodes = {
156         [0] = "error-drop",
157     },
158 };
159
160 static uword
161 adj_midchain_tx_no_count (vlib_main_t * vm,
162                           vlib_node_runtime_t * node,
163                           vlib_frame_t * frame)
164 {
165     return (adj_mdichain_tx_inline(vm, node, frame, 0));
166 }
167
168 VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node, static) = {
169     .function = adj_midchain_tx_no_count,
170     .name = "adj-midchain-tx-no-count",
171     .vector_size = sizeof (u32),
172
173     .format_trace = format_adj_midchain_tx_trace,
174
175     .n_next_nodes = 1,
176     .next_nodes = {
177         [0] = "error-drop",
178     },
179 };
180
181 VNET_IP4_TX_FEATURE_INIT (adj_midchain_tx_ip4, static) = {
182     .node_name = "adj-midchain-tx",
183     .runs_before = ORDER_CONSTRAINTS {"interface-output"},
184     .feature_index = &adj_midchain_tx_feature_node[FIB_LINK_IP4],
185 };
186 VNET_IP4_TX_FEATURE_INIT (adj_midchain_tx_no_count_ip4, static) = {
187     .node_name = "adj-midchain-tx-no-count",
188     .runs_before = ORDER_CONSTRAINTS {"interface-output"},
189     .feature_index = &adj_midchain_tx_no_count_feature_node[FIB_LINK_IP4],
190 };
191 VNET_IP6_TX_FEATURE_INIT (adj_midchain_tx_ip6, static) = {
192     .node_name = "adj-midchain-tx",
193     .runs_before = ORDER_CONSTRAINTS {"interface-output"},
194     .feature_index = &adj_midchain_tx_feature_node[FIB_LINK_IP6],
195 };
196 VNET_IP6_TX_FEATURE_INIT (adj_midchain_tx_no_count_ip6, static) = {
197     .node_name = "adj-midchain-tx-no-count",
198     .runs_before = ORDER_CONSTRAINTS {"interface-output"},
199     .feature_index = &adj_midchain_tx_no_count_feature_node[FIB_LINK_IP6],
200 };
201 VNET_MPLS_TX_FEATURE_INIT (adj_midchain_tx_mpls, static) = {
202     .node_name = "adj-midchain-txs",
203     .runs_before = ORDER_CONSTRAINTS {"interface-output"},
204     .feature_index = &adj_midchain_tx_feature_node[FIB_LINK_MPLS],
205 };
206 VNET_MPLS_TX_FEATURE_INIT (adj_midchain_tx_no_count_mpls, static) = {
207     .node_name = "adj-midchain-tx-no-count",
208     .runs_before = ORDER_CONSTRAINTS {"interface-output"},
209     .feature_index = &adj_midchain_tx_no_count_feature_node[FIB_LINK_MPLS],
210 };
211 VNET_ETHERNET_TX_FEATURE_INIT (adj_midchain_tx_ethernet, static) = {
212     .node_name = "adj-midchain-tx",
213     .runs_before = ORDER_CONSTRAINTS {"error-drop"},
214     .feature_index = &adj_midchain_tx_feature_node[FIB_LINK_ETHERNET],
215 };
216 VNET_ETHERNET_TX_FEATURE_INIT (adj_midchain_tx_no_count_ethernet, static) = {
217     .node_name = "adj-midchain-tx-no-count",
218     .runs_before = ORDER_CONSTRAINTS {"error-drop"},
219     .feature_index = &adj_midchain_tx_no_count_feature_node[FIB_LINK_ETHERNET],
220 };
221
222 static inline u32
223 adj_get_midchain_node (fib_link_t link)
224 {
225     switch (link) {
226     case FIB_LINK_IP4:
227         return (ip4_midchain_node.index);
228     case FIB_LINK_IP6:
229         return (ip6_midchain_node.index);
230     case FIB_LINK_MPLS:
231         return (mpls_midchain_node.index);
232     case FIB_LINK_ETHERNET:
233         return (adj_l2_midchain_node.index);
234     }
235     ASSERT(0);
236     return (0);
237 }
238
239 static ip_config_main_t *
240 adj_midchain_get_cofing_for_link_type (const ip_adjacency_t *adj)
241 {
242     ip_config_main_t *cm = NULL;
243
244     switch (adj->ia_link)
245     {
246     case FIB_LINK_IP4:
247         {
248             ip4_main_t * im = &ip4_main;
249             ip_lookup_main_t * lm = &im->lookup_main;
250             cm = &lm->feature_config_mains[VNET_IP_TX_FEAT];
251             break;
252         }
253     case FIB_LINK_IP6:
254         {
255             ip6_main_t * im = &ip6_main;
256             ip_lookup_main_t * lm = &im->lookup_main;
257             cm = &lm->feature_config_mains[VNET_IP_TX_FEAT];
258             break;
259         }
260     case FIB_LINK_MPLS:
261         {
262             mpls_main_t * mm = &mpls_main;
263             cm = &mm->feature_config_mains[VNET_IP_TX_FEAT];
264             break;
265         }
266     case FIB_LINK_ETHERNET:
267         {
268             cm = &ethernet_main.feature_config_mains[VNET_IP_TX_FEAT];
269             break;
270         }
271     }
272
273     return (cm);
274 }
275
276 /**
277  * adj_nbr_midchain_update_rewrite
278  *
279  * Update the adjacency's rewrite string. A NULL string implies the
280  * rewrite is reset (i.e. when ARP/ND etnry is gone).
281  * NB: the adj being updated may be handling traffic in the DP.
282  */
283 void
284 adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
285                                  adj_midchain_fixup_t fixup,
286                                  adj_midchain_flag_t flags,
287                                  u8 *rewrite)
288 {
289     vnet_config_main_t * vcm;
290     ip_config_main_t *cm;
291     ip_adjacency_t *adj;
292     u32 ci;
293
294     ASSERT(ADJ_INDEX_INVALID != adj_index);
295
296     adj = adj_get(adj_index);
297     adj->lookup_next_index = IP_LOOKUP_NEXT_MIDCHAIN;
298     adj->sub_type.midchain.fixup_func = fixup;
299
300     cm = adj_midchain_get_cofing_for_link_type(adj);
301     vcm = &(cm->config_main);
302     vec_validate_init_empty(cm->config_index_by_sw_if_index,
303                             adj->rewrite_header.sw_if_index, ~0);
304     ci = cm->config_index_by_sw_if_index[adj->rewrite_header.sw_if_index];
305
306     /*
307      * Choose the adj tx function based on whether the client wants
308      * to count against the interface or not and insert the appropriate
309      * TX feature.
310      */
311     if (flags & ADJ_MIDCHAIN_FLAG_NO_COUNT)
312     {
313         adj->sub_type.midchain.tx_function_node =
314             adj_midchain_tx_no_count_node.index;
315
316         ci = vnet_config_add_feature(
317                  vlib_get_main(),
318                  vcm, ci,
319                  adj_midchain_tx_no_count_feature_node[adj->ia_link],
320                  /* config data */ 0,
321                  /* # bytes of config data */ 0);
322     }
323     else
324     {
325         adj->sub_type.midchain.tx_function_node =
326             adj_midchain_tx_node.index;
327         ci = vnet_config_add_feature(
328                  vlib_get_main(),
329                  vcm, ci,
330                  adj_midchain_tx_feature_node[adj->ia_link],
331                  /* config data */ 0,
332                  /* # bytes of config data */ 0);
333     }
334
335     cm->config_index_by_sw_if_index[adj->rewrite_header.sw_if_index] = ci;
336
337     if (NULL != rewrite)
338     {
339         /*
340          * new rewrite provided.
341          * use a dummy rewrite header to get the interface to print into.
342          */
343         ip_adjacency_t dummy;
344         dpo_id_t tmp = DPO_NULL;
345
346         vnet_rewrite_for_tunnel(vnet_get_main(),
347                                 adj->rewrite_header.sw_if_index,
348                                 adj_get_midchain_node(adj->ia_link),
349                                 adj->sub_type.midchain.tx_function_node,
350                                 &dummy.rewrite_header,
351                                 rewrite,
352                                 vec_len(rewrite));
353
354         /*
355          * this is an update of an existing rewrite.
356          * packets are in flight. we'll need to briefly stack on the drop DPO
357          * whilst the rewrite is written, so any packets that see the partial update
358          * are binned.
359          */
360         if (!dpo_id_is_valid(&adj->sub_type.midchain.next_dpo))
361         {
362             /*
363              * not stacked yet. stack on the drop
364              */
365             dpo_stack(DPO_ADJACENCY_MIDCHAIN,
366                       fib_link_to_dpo_proto(adj->ia_link),
367                       &adj->sub_type.midchain.next_dpo,
368                       drop_dpo_get(fib_link_to_dpo_proto(adj->ia_link)));
369         }
370
371         dpo_copy(&tmp, &adj->sub_type.midchain.next_dpo);
372         dpo_stack(DPO_ADJACENCY_MIDCHAIN,
373                   fib_link_to_dpo_proto(adj->ia_link),
374                   &adj->sub_type.midchain.next_dpo,
375                   drop_dpo_get(fib_link_to_dpo_proto(adj->ia_link)));
376
377         CLIB_MEMORY_BARRIER();
378
379         clib_memcpy(&adj->rewrite_header,
380                     &dummy.rewrite_header,
381                     VLIB_BUFFER_PRE_DATA_SIZE);
382
383         CLIB_MEMORY_BARRIER();
384
385         /*
386          * The graph arc used/created here is from the midchain-tx node to the
387          * child's registered node. This is because post adj processing the next
388          * node are any output features, then the midchain-tx.  from there we
389          * need to get to the stacked child's node.
390          */
391         dpo_stack_from_node(adj->sub_type.midchain.tx_function_node,
392                             &adj->sub_type.midchain.next_dpo,
393                             &tmp);
394         dpo_reset(&tmp);
395     }
396     else
397     {
398         ASSERT(0);
399     }
400
401     /*
402      * time for walkies fido.
403      */
404     fib_node_back_walk_ctx_t bw_ctx = {
405         .fnbw_reason = FIB_NODE_BW_REASON_ADJ_UPDATE,
406     };
407
408     fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
409 }
410
411 /**
412  * adj_nbr_midchain_unstack
413  *
414  * Unstack the adj. stack it on drop
415  */
416 void
417 adj_nbr_midchain_unstack (adj_index_t adj_index)
418 {
419     ip_adjacency_t *adj;
420
421     ASSERT(ADJ_INDEX_INVALID != adj_index);
422
423     adj = adj_get(adj_index);
424
425     /*
426      * stack on the drop
427      */
428     dpo_stack(DPO_ADJACENCY_MIDCHAIN,
429               fib_link_to_dpo_proto(adj->ia_link),
430               &adj->sub_type.midchain.next_dpo,
431               drop_dpo_get(fib_link_to_dpo_proto(adj->ia_link)));
432
433     CLIB_MEMORY_BARRIER();
434 }
435
436 /**
437  * adj_nbr_midchain_stack
438  */
439 void
440 adj_nbr_midchain_stack (adj_index_t adj_index,
441                         const dpo_id_t *next)
442 {
443     ip_adjacency_t *adj;
444
445     ASSERT(ADJ_INDEX_INVALID != adj_index);
446
447     adj = adj_get(adj_index);
448
449     ASSERT(IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index);
450
451     dpo_stack_from_node(adj->sub_type.midchain.tx_function_node,
452                         &adj->sub_type.midchain.next_dpo,
453                         next);
454 }
455
456 u8*
457 format_adj_midchain (u8* s, va_list *ap)
458 {
459     index_t index = va_arg(ap, index_t);
460     u32 indent = va_arg(ap, u32);
461     vnet_main_t * vnm = vnet_get_main();
462     ip_adjacency_t * adj = adj_get(index);
463
464     s = format (s, "%U", format_fib_link, adj->ia_link);
465     s = format (s, " via %U ",
466                 format_ip46_address, &adj->sub_type.nbr.next_hop);
467     s = format (s, " %U",
468                 format_vnet_rewrite,
469                 vnm->vlib_main, &adj->rewrite_header,
470                 sizeof (adj->rewrite_data), indent);
471     s = format (s, "\n%Ustacked-on:\n%U%U",
472                 format_white_space, indent,
473                 format_white_space, indent+2,
474                 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
475
476     return (s);
477 }
478
479 static void
480 adj_dpo_lock (dpo_id_t *dpo)
481 {
482     adj_lock(dpo->dpoi_index);
483 }
484 static void
485 adj_dpo_unlock (dpo_id_t *dpo)
486 {
487     adj_unlock(dpo->dpoi_index);
488 }
489
490 const static dpo_vft_t adj_midchain_dpo_vft = {
491     .dv_lock = adj_dpo_lock,
492     .dv_unlock = adj_dpo_unlock,
493     .dv_format = format_adj_midchain,
494 };
495
496 /**
497  * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
498  *        object.
499  *
500  * this means that these graph nodes are ones from which a midchain is the
501  * parent object in the DPO-graph.
502  */
503 const static char* const midchain_ip4_nodes[] =
504 {
505     "ip4-midchain",
506     NULL,
507 };
508 const static char* const midchain_ip6_nodes[] =
509 {
510     "ip6-midchain",
511     NULL,
512 };
513 const static char* const midchain_mpls_nodes[] =
514 {
515     "mpls-midchain",
516     NULL,
517 };
518 const static char* const midchain_ethernet_nodes[] =
519 {
520     "adj-l2-midchain",
521     NULL,
522 };
523
524 const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
525 {
526     [DPO_PROTO_IP4]  = midchain_ip4_nodes,
527     [DPO_PROTO_IP6]  = midchain_ip6_nodes,
528     [DPO_PROTO_MPLS] = midchain_mpls_nodes,
529     [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
530 };
531
532 void
533 adj_midchain_module_init (void)
534 {
535     dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
536 }