BIER neighbor stats
[vpp.git] / src / vnet / bier / bier_output.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/buffer.h>
17
18 #include <vnet/bier/bier_fmask.h>
19 #include <vnet/bier/bier_hdr_inlines.h>
20 #include <vlib/vlib.h>
21
22 static char * bier_output_error_strings[] = {
23 #define bier_error(n,s) s,
24 #include <vnet/bier/bier_output_error.def>
25 #undef bier_error
26 };
27
28 /*
29  * Keep these values sematically the same as BIER output
30  */
31 #define foreach_bier_output_next                \
32     _(DROP, "bier-drop")
33
34 typedef enum {
35 #define _(s,n) BIER_OUTPUT_NEXT_##s,
36     foreach_bier_output_next
37 #undef _
38     BIER_OUTPUT_N_NEXT,
39 } bier_output_next_t;
40
41 typedef enum {
42 #define bier_error(n,s) BIER_OUTPUT_ERROR_##n,
43 #include <vnet/bier/bier_output_error.def>
44 #undef bier_error
45     BIER_OUTPUT_N_ERROR,
46 } bier_output_error_t;
47
48 /**
49  * Forward declaration
50  */
51 vlib_node_registration_t bier_output_node;
52 extern vlib_combined_counter_main_t bier_fmask_counters;
53
54 /**
55  * @brief Packet trace recoed for a BIER output
56  */
57 typedef struct bier_output_trace_t_
58 {
59     u32 next_index;
60     index_t bfm_index;
61 } bier_output_trace_t;
62
63 static uword
64 bier_output (vlib_main_t * vm,
65              vlib_node_runtime_t * node,
66              vlib_frame_t * from_frame)
67 {
68   vlib_combined_counter_main_t *cm = &bier_fmask_counters;
69     u32 n_left_from, next_index, * from, * to_next;
70     u32 thread_index;
71
72     thread_index = vlib_get_thread_index ();
73     from = vlib_frame_vector_args (from_frame);
74     n_left_from = from_frame->n_vectors;
75
76     /*
77      * objection your honour! speculation!
78      */
79     next_index = node->cached_next_index;
80
81     while (n_left_from > 0)
82     {
83         u32 n_left_to_next;
84
85         vlib_get_next_frame (vm, node, next_index,
86                              to_next, n_left_to_next);
87
88         while (n_left_from > 0 && n_left_to_next > 0)
89         {
90             bier_output_next_t next0;
91             bier_bit_string_t bbs;
92             vlib_buffer_t * b0;
93             bier_fmask_t *bfm0;
94             mpls_label_t *h0;
95             bier_hdr_t *bh0;
96             u32 bfmi0;
97             u32 bi0;
98
99             bi0 = from[0];
100             to_next[0] = bi0;
101             from += 1;
102             to_next += 1;
103             n_left_from -= 1;
104             n_left_to_next -= 1;
105
106             b0 = vlib_get_buffer (vm, bi0);
107             bh0 = vlib_buffer_get_current (b0);
108             bier_bit_string_init_from_hdr(bh0, &bbs);
109
110             /*
111              * In the BIER Lookup node we squirelled away the
112              * BIER fmask index as the adj index
113              */
114             bfmi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
115             bfm0 = bier_fmask_get(bfmi0);
116
117             vlib_increment_combined_counter(
118                 cm, thread_index, bfmi0, 1,
119                 vlib_buffer_length_in_chain (vm, b0));
120
121             /*
122              * perform the logical AND of the packet's mask with
123              * that of the fmask objects, to reset the bits that
124              * are only on the shortest path the the fmask NH.
125              */
126             bier_bit_string_logical_and_string(
127                 &bfm0->bfm_bits.bfmb_input_reset_string,
128                 &bbs);
129
130             /*
131              * this is the last time we touch the BIER header
132              * so flip to network order
133              */
134             bier_hdr_hton(bh0);
135
136             /*
137              * paint the BIER peer's label
138              */
139             if (!(bfm0->bfm_flags & BIER_FMASK_FLAG_DISP))
140             {
141                 /*
142                  * since a BIFT value and a MPLS label are formated the
143                  * same, this painting works OK.
144                  */
145                 vlib_buffer_advance(b0, -(word)sizeof(mpls_label_t));
146                 h0 = vlib_buffer_get_current(b0);
147                 
148                 h0[0] = bfm0->bfm_label;
149
150                 ((char*)h0)[3]= vnet_buffer(b0)->mpls.ttl - 1;
151             }
152
153             /*
154              * setup next graph node
155              */
156             next0 = bfm0->bfm_dpo.dpoi_next_node;
157             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = bfm0->bfm_dpo.dpoi_index;
158
159             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
160             {
161                 bier_output_trace_t *tr;
162
163                 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
164                 tr->next_index = next0;
165                 tr->bfm_index = bfmi0;
166             }
167
168             vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
169                                              to_next, n_left_to_next,
170                                              bi0, next0);
171         }
172
173         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
174     }
175
176     vlib_node_increment_counter (vm, bier_output_node.index,
177                                  BIER_OUTPUT_ERROR_NONE,
178                                  from_frame->n_vectors);
179     return (from_frame->n_vectors);
180 }
181
182 static u8 *
183 format_bier_output_trace (u8 * s, va_list * args)
184 {
185     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
186     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
187     bier_output_trace_t * t = va_arg (*args, bier_output_trace_t *);
188
189     s = format (s, " next [%d], BFM index %d",
190                 t->next_index, t->bfm_index);
191     return s;
192 }
193
194 VLIB_REGISTER_NODE (bier_output_node) = {
195     .function = bier_output,
196     .name = "bier-output",
197     /* Takes a vector of packets. */
198     .vector_size = sizeof (u32),
199
200     .n_errors = BIER_OUTPUT_N_ERROR,
201     .error_strings = bier_output_error_strings,
202
203     .n_next_nodes = BIER_OUTPUT_N_NEXT,
204     .next_nodes = {
205         [BIER_OUTPUT_NEXT_DROP] = "bier-drop",
206     },
207
208     .format_trace = format_bier_output_trace,
209 };