MPLS Unifom mode
[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
53 /**
54  * @brief Packet trace recoed for a BIER output
55  */
56 typedef struct bier_output_trace_t_
57 {
58     u32 next_index;
59     index_t bfm_index;
60 } bier_output_trace_t;
61
62 static uword
63 bier_output (vlib_main_t * vm,
64              vlib_node_runtime_t * node,
65              vlib_frame_t * from_frame)
66 {
67     u32 n_left_from, next_index, * from, * to_next;
68
69     from = vlib_frame_vector_args (from_frame);
70     n_left_from = from_frame->n_vectors;
71
72     // vnet_buffer(b0)->sw_if_index[VLIB_TX] = d0->tx_fib_index;
73
74     /*
75      * objection your honour! speculation!
76      */
77     next_index = node->cached_next_index;
78
79     while (n_left_from > 0)
80     {
81         u32 n_left_to_next;
82
83         vlib_get_next_frame (vm, node, next_index,
84                              to_next, n_left_to_next);
85
86         while (n_left_from > 0 && n_left_to_next > 0)
87         {
88             bier_output_next_t next0;
89             bier_bit_string_t bbs;
90             vlib_buffer_t * b0;
91             bier_fmask_t *bfm0;
92             mpls_label_t *h0;
93             bier_hdr_t *bh0;
94             u32 bfmi0;
95             u32 bi0;
96
97             bi0 = from[0];
98             to_next[0] = bi0;
99             from += 1;
100             to_next += 1;
101             n_left_from -= 1;
102             n_left_to_next -= 1;
103
104             b0 = vlib_get_buffer (vm, bi0);
105             bh0 = vlib_buffer_get_current (b0);
106             bier_bit_string_init_from_hdr(bh0, &bbs);
107
108             /*
109              * In the BIER Lookup node we squirelled away the
110              * BIER fmask index as the adj index
111              */
112             bfmi0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
113             bfm0 = bier_fmask_get(bfmi0);
114
115             /*
116              * perform the logical AND of the packet's mask with
117              * that of the fmask objects, to reset the bits that
118              * are only on the shortest path the the fmask NH.
119              */
120             bier_bit_string_logical_and_string(
121                 &bfm0->bfm_bits.bfmb_input_reset_string,
122                 &bbs);
123
124             /*
125              * this is the last time we touch the BIER header
126              * so flip to network order
127              */
128             bier_hdr_hton(bh0);
129
130             /*
131              * paint the BIER peer's label
132              */
133             if (!(bfm0->bfm_flags & BIER_FMASK_FLAG_DISP))
134             {
135                 /*
136                  * since a BIFT value and a MPLS label are formated the
137                  * same, this painting works OK.
138                  */
139                 vlib_buffer_advance(b0, -(word)sizeof(mpls_label_t));
140                 h0 = vlib_buffer_get_current(b0);
141                 
142                 h0[0] = bfm0->bfm_label;
143
144                 ((char*)h0)[3]= vnet_buffer(b0)->mpls.ttl - 1;
145             }
146
147             /*
148              * setup next graph node
149              */
150             next0 = bfm0->bfm_dpo.dpoi_next_node;
151             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = bfm0->bfm_dpo.dpoi_index;
152
153             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
154             {
155                 bier_output_trace_t *tr;
156
157                 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
158                 tr->next_index = next0;
159                 tr->bfm_index = bfmi0;
160             }
161
162             vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
163                                              to_next, n_left_to_next,
164                                              bi0, next0);
165         }
166
167         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
168     }
169
170     vlib_node_increment_counter (vm, bier_output_node.index,
171                                  BIER_OUTPUT_ERROR_NONE,
172                                  from_frame->n_vectors);
173     return (from_frame->n_vectors);
174 }
175
176 static u8 *
177 format_bier_output_trace (u8 * s, va_list * args)
178 {
179     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
180     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
181     bier_output_trace_t * t = va_arg (*args, bier_output_trace_t *);
182
183     s = format (s, " next [%d], BFM index %d",
184                 t->next_index, t->bfm_index);
185     return s;
186 }
187
188 VLIB_REGISTER_NODE (bier_output_node) = {
189     .function = bier_output,
190     .name = "bier-output",
191     /* Takes a vector of packets. */
192     .vector_size = sizeof (u32),
193
194     .n_errors = BIER_OUTPUT_N_ERROR,
195     .error_strings = bier_output_error_strings,
196
197     .n_next_nodes = BIER_OUTPUT_N_NEXT,
198     .next_nodes = {
199         [BIER_OUTPUT_NEXT_DROP] = "bier-drop",
200     },
201
202     .format_trace = format_bier_output_trace,
203 };