Deprecate MPLSoGRE tunnels (VPP-502)
[vpp.git] / vnet / vnet / mpls / mpls_lookup.c
1 /*
2  * mpls_lookup.c: MPLS lookup
3  *
4  * Copyright (c) 2012-2014 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/mpls/mpls.h>
21 #include <vnet/fib/mpls_fib.h>
22 #include <vnet/dpo/load_balance.h>
23
24 vlib_node_registration_t mpls_lookup_node;
25
26 typedef struct {
27   u32 next_index;
28   u32 lb_index;
29   u32 lfib_index;
30   u32 label_net_byte_order;
31 } mpls_lookup_trace_t;
32
33 static u8 *
34 format_mpls_lookup_trace (u8 * s, va_list * args)
35 {
36   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38   mpls_lookup_trace_t * t = va_arg (*args, mpls_lookup_trace_t *);
39
40   s = format (s, "MPLS: next [%d], lookup fib index %d, LB index %d "
41               "label %d eos %d", 
42               t->next_index, t->lfib_index, t->lb_index,
43               vnet_mpls_uc_get_label(
44                   clib_net_to_host_u32(t->label_net_byte_order)),
45               vnet_mpls_uc_get_s(t->label_net_byte_order));
46   return s;
47 }
48
49 /*
50  * Compute flow hash. 
51  * We'll use it to select which adjacency to use for this flow.  And other things.
52  */
53 always_inline u32
54 mpls_compute_flow_hash (const mpls_unicast_header_t * hdr,
55                         flow_hash_config_t flow_hash_config)
56 {
57     // FIXME
58     return (vnet_mpls_uc_get_label(hdr->label_exp_s_ttl));
59 }
60
61 static inline uword
62 mpls_lookup (vlib_main_t * vm,
63              vlib_node_runtime_t * node,
64              vlib_frame_t * from_frame)
65 {
66   vlib_combined_counter_main_t * cm = &load_balance_main.lbm_to_counters;
67   u32 n_left_from, next_index, * from, * to_next;
68   mpls_main_t * mm = &mpls_main;
69   u32 cpu_index = os_get_cpu_number();
70
71   from = vlib_frame_vector_args (from_frame);
72   n_left_from = from_frame->n_vectors;
73   next_index = node->cached_next_index;
74
75   while (n_left_from > 0)
76     {
77       u32 n_left_to_next;
78
79       vlib_get_next_frame (vm, node, next_index,
80                            to_next, n_left_to_next);
81
82       while (n_left_from > 0 && n_left_to_next > 0)
83       {
84           u32 lbi0, next0, lfib_index0, bi0, hash_c0;
85           const mpls_unicast_header_t * h0;
86           const load_balance_t *lb0;
87           const dpo_id_t *dpo0;
88           vlib_buffer_t * b0;
89
90           bi0 = from[0];
91           to_next[0] = bi0;
92           from += 1;
93           to_next += 1;
94           n_left_from -= 1;
95           n_left_to_next -= 1;
96
97           b0 = vlib_get_buffer (vm, bi0);
98           h0 = vlib_buffer_get_current (b0);
99
100           lfib_index0 = vec_elt(mm->fib_index_by_sw_if_index,
101                                 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
102
103           lbi0 = mpls_fib_table_forwarding_lookup (lfib_index0, h0);
104           lb0 = load_balance_get(lbi0);
105
106           hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
107           if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
108           {
109               hash_c0 = vnet_buffer (b0)->ip.flow_hash = 
110                   mpls_compute_flow_hash(h0, lb0->lb_hash_config);
111           }
112
113           ASSERT (lb0->lb_n_buckets > 0);
114           ASSERT (is_pow2 (lb0->lb_n_buckets));
115
116           dpo0 = load_balance_get_bucket_i(lb0,
117                                            (hash_c0 &
118                                             (lb0->lb_n_buckets_minus_1)));
119
120           next0 = dpo0->dpoi_next_node;
121           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
122
123           vlib_increment_combined_counter 
124               (cm, cpu_index, lbi0, 1,
125                vlib_buffer_length_in_chain (vm, b0));
126
127           /*
128            * pop the label that was just used in the lookup
129            */
130           vlib_buffer_advance(b0, sizeof(*h0));
131
132           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
133           {
134               mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
135                                                         b0, sizeof (*tr));
136               tr->next_index = next0;
137               tr->lb_index = lbi0;
138               tr->lfib_index = lfib_index0;
139               tr->label_net_byte_order = h0->label_exp_s_ttl;
140           }
141
142           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
143                                            to_next, n_left_to_next,
144                                            bi0, next0);
145         }
146
147       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
148     }
149   vlib_node_increment_counter (vm, mpls_lookup_node.index,
150                                MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
151   return from_frame->n_vectors;
152 }
153
154 static char * mpls_error_strings[] = {
155 #define mpls_error(n,s) s,
156 #include "error.def"
157 #undef mpls_error
158 };
159
160 VLIB_REGISTER_NODE (mpls_lookup_node) = {
161   .function = mpls_lookup,
162   .name = "mpls-lookup",
163   /* Takes a vector of packets. */
164   .vector_size = sizeof (u32),
165   .n_errors = MPLS_N_ERROR,
166   .error_strings = mpls_error_strings,
167
168   .sibling_of = "ip4-lookup",
169
170   .format_buffer = format_mpls_header,
171   .format_trace = format_mpls_lookup_trace,
172   .unformat_buffer = unformat_mpls_header,
173 };
174
175 VLIB_NODE_FUNCTION_MULTIARCH (mpls_lookup_node, mpls_lookup)
176
177 typedef struct {
178   u32 next_index;
179   u32 lb_index;
180 } mpls_load_balance_trace_t;
181
182 static u8 *
183 format_mpls_load_balance_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   mpls_load_balance_trace_t * t = va_arg (*args, mpls_load_balance_trace_t *);
188
189   s = format (s, "MPLS: next [%d], LB index %d ", 
190               t->next_index, t->lb_index);
191   return s;
192 }
193
194 always_inline uword
195 mpls_load_balance (vlib_main_t * vm,
196                   vlib_node_runtime_t * node,
197                   vlib_frame_t * frame)
198 {
199   vlib_combined_counter_main_t * cm = &load_balance_main.lbm_via_counters;
200   u32 n_left_from, n_left_to_next, * from, * to_next;
201   ip_lookup_next_t next;
202   u32 cpu_index = os_get_cpu_number();
203
204   from = vlib_frame_vector_args (frame);
205   n_left_from = frame->n_vectors;
206   next = node->cached_next_index;
207
208   while (n_left_from > 0)
209     {
210       vlib_get_next_frame (vm, node, next,
211                            to_next, n_left_to_next);
212
213     
214       while (n_left_from > 0 && n_left_to_next > 0)
215         {
216           const mpls_unicast_header_t *hdr0;
217           const load_balance_t *lb0;
218           u32 pi0, lbi0, hc0, next0;
219           const dpo_id_t *dpo0;
220           vlib_buffer_t * p0;
221
222           pi0 = from[0];
223           to_next[0] = pi0;
224
225           p0 = vlib_get_buffer (vm, pi0);
226
227           hdr0 = vlib_buffer_get_current (p0);
228           lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
229
230           lb0 = load_balance_get(lbi0);
231           hc0 = lb0->lb_hash_config;
232           vnet_buffer(p0)->ip.flow_hash = mpls_compute_flow_hash(hdr0, hc0);
233
234           dpo0 = load_balance_get_bucket_i(lb0, 
235                                            vnet_buffer(p0)->ip.flow_hash &
236                                            (lb0->lb_n_buckets_minus_1));
237
238           next0 = dpo0->dpoi_next_node;
239           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
240
241           vlib_increment_combined_counter 
242               (cm, cpu_index, lbi0, 1,
243                vlib_buffer_length_in_chain (vm, p0));
244
245           from += 1;
246           to_next += 1;
247           n_left_to_next -= 1;
248           n_left_from -= 1;
249
250           if (PREDICT_FALSE (next0 != next))
251             {
252               n_left_to_next += 1;
253               vlib_put_next_frame (vm, node, next, n_left_to_next);
254               next = next0;
255               vlib_get_next_frame (vm, node, next,
256                                    to_next, n_left_to_next);
257               to_next[0] = pi0;
258               to_next += 1;
259               n_left_to_next -= 1;
260             }
261         }
262
263       vlib_put_next_frame (vm, node, next, n_left_to_next);
264     }
265
266   return frame->n_vectors;
267 }
268
269 VLIB_REGISTER_NODE (mpls_load_balance_node) = {
270   .function = mpls_load_balance,
271   .name = "mpls-load-balance",
272   .vector_size = sizeof (u32),
273   .sibling_of = "mpls-lookup",
274
275   .format_trace = format_mpls_load_balance_trace,
276 };
277
278 VLIB_NODE_FUNCTION_MULTIARCH (mpls_load_balance_node, mpls_load_balance)