2d34cbde341f381ba8e6990e3bb8a6ec0bed24be
[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   u32 hash;
32 } mpls_lookup_trace_t;
33
34 static u8 *
35 format_mpls_lookup_trace (u8 * s, va_list * args)
36 {
37   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39   mpls_lookup_trace_t * t = va_arg (*args, mpls_lookup_trace_t *);
40
41   s = format (s, "MPLS: next [%d], lookup fib index %d, LB index %d hash %d"
42               "label %d eos %d", 
43               t->next_index, t->lfib_index, t->lb_index, t->hash,
44               vnet_mpls_uc_get_label(
45                   clib_net_to_host_u32(t->label_net_byte_order)),
46               vnet_mpls_uc_get_s(t->label_net_byte_order));
47   return s;
48 }
49
50 /*
51  * Compute flow hash. 
52  * We'll use it to select which adjacency to use for this flow.  And other things.
53  */
54 always_inline u32
55 mpls_compute_flow_hash (const mpls_unicast_header_t * hdr,
56                         flow_hash_config_t flow_hash_config)
57 {
58     // FIXME
59     return (vnet_mpls_uc_get_label(hdr->label_exp_s_ttl));
60 }
61
62 static inline uword
63 mpls_lookup (vlib_main_t * vm,
64              vlib_node_runtime_t * node,
65              vlib_frame_t * from_frame)
66 {
67   vlib_combined_counter_main_t * cm = &load_balance_main.lbm_to_counters;
68   u32 n_left_from, next_index, * from, * to_next;
69   mpls_main_t * mm = &mpls_main;
70   u32 cpu_index = os_get_cpu_number();
71
72   from = vlib_frame_vector_args (from_frame);
73   n_left_from = from_frame->n_vectors;
74   next_index = node->cached_next_index;
75
76   while (n_left_from > 0)
77     {
78       u32 n_left_to_next;
79
80       vlib_get_next_frame (vm, node, next_index,
81                            to_next, n_left_to_next);
82
83       while (n_left_from >= 4 && n_left_to_next >= 2)
84         {
85           u32 lbi0, next0, lfib_index0, bi0, hash_c0;
86           const mpls_unicast_header_t * h0;
87           const load_balance_t *lb0;
88           const dpo_id_t *dpo0;
89           vlib_buffer_t * b0;
90           u32 lbi1, next1, lfib_index1, bi1, hash_c1;
91           const mpls_unicast_header_t * h1;
92           const load_balance_t *lb1;
93           const dpo_id_t *dpo1;
94           vlib_buffer_t * b1;
95
96            /* Prefetch next iteration. */
97           {
98             vlib_buffer_t * p2, * p3;
99
100             p2 = vlib_get_buffer (vm, from[2]);
101             p3 = vlib_get_buffer (vm, from[3]);
102
103             vlib_prefetch_buffer_header (p2, STORE);
104             vlib_prefetch_buffer_header (p3, STORE);
105
106             CLIB_PREFETCH (p2->data, sizeof (h0[0]), STORE);
107             CLIB_PREFETCH (p3->data, sizeof (h0[0]), STORE);
108           }
109
110           bi0 = to_next[0] = from[0];
111           bi1 = to_next[1] = from[1];
112
113           from += 2;
114           n_left_from -= 2;
115           to_next += 2;
116           n_left_to_next -= 2;
117
118           b0 = vlib_get_buffer (vm, bi0);
119           b1 = vlib_get_buffer (vm, bi1);
120           h0 = vlib_buffer_get_current (b0);
121           h1 = vlib_buffer_get_current (b1);
122
123           lfib_index0 = vec_elt(mm->fib_index_by_sw_if_index,
124                                 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
125           lfib_index1 = vec_elt(mm->fib_index_by_sw_if_index,
126                                 vnet_buffer(b1)->sw_if_index[VLIB_RX]);
127
128           lbi0 = mpls_fib_table_forwarding_lookup (lfib_index0, h0);
129           lbi1 = mpls_fib_table_forwarding_lookup (lfib_index1, h1);
130           lb0 = load_balance_get(lbi0);
131           lb1 = load_balance_get(lbi1);
132
133           hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
134           hash_c1 = vnet_buffer(b1)->ip.flow_hash = 0;
135
136           if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
137           {
138               hash_c0 = vnet_buffer (b0)->ip.flow_hash =
139                   mpls_compute_flow_hash(h0, lb0->lb_hash_config);
140           }
141           if (PREDICT_FALSE(lb1->lb_n_buckets > 1))
142           {
143               hash_c1 = vnet_buffer (b1)->ip.flow_hash =
144                   mpls_compute_flow_hash(h1, lb1->lb_hash_config);
145           }
146
147           ASSERT (lb0->lb_n_buckets > 0);
148           ASSERT (is_pow2 (lb0->lb_n_buckets));
149           ASSERT (lb1->lb_n_buckets > 0);
150           ASSERT (is_pow2 (lb1->lb_n_buckets));
151
152           dpo0 = load_balance_get_bucket_i(lb0,
153                                            (hash_c0 &
154                                             (lb0->lb_n_buckets_minus_1)));
155           dpo1 = load_balance_get_bucket_i(lb1,
156                                            (hash_c1 &
157                                             (lb1->lb_n_buckets_minus_1)));
158
159           next0 = dpo0->dpoi_next_node;
160           next1 = dpo1->dpoi_next_node;
161
162           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
163           vnet_buffer (b1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
164
165           vlib_increment_combined_counter
166               (cm, cpu_index, lbi0, 1,
167                vlib_buffer_length_in_chain (vm, b0));
168           vlib_increment_combined_counter
169               (cm, cpu_index, lbi1, 1,
170                vlib_buffer_length_in_chain (vm, b1));
171
172           /*
173            * before we pop the label copy th values we need to maintain.
174            * The label header is in network byte order.
175            *  last byte is the TTL.
176            *  bits 2 to 4 inclusive are the EXP bits
177            */
178           vnet_buffer (b0)->mpls.ttl = ((char*)h0)[3];
179           vnet_buffer (b0)->mpls.exp = (((char*)h0)[2] & 0xe) >> 1;
180           vnet_buffer (b0)->mpls.first = 1;
181           vnet_buffer (b1)->mpls.ttl = ((char*)h1)[3];
182           vnet_buffer (b1)->mpls.exp = (((char*)h1)[2] & 0xe) >> 1;
183           vnet_buffer (b1)->mpls.first = 1;
184
185           /*
186            * pop the label that was just used in the lookup
187            */
188           vlib_buffer_advance(b0, sizeof(*h0));
189           vlib_buffer_advance(b1, sizeof(*h1));
190
191           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
192           {
193               mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
194                                                         b0, sizeof (*tr));
195               tr->next_index = next0;
196               tr->lb_index = lbi0;
197               tr->lfib_index = lfib_index0;
198               tr->hash = hash_c0;
199               tr->label_net_byte_order = h0->label_exp_s_ttl;
200           }
201
202           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
203           {
204               mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
205                                                         b1, sizeof (*tr));
206               tr->next_index = next1;
207               tr->lb_index = lbi1;
208               tr->lfib_index = lfib_index1;
209               tr->hash = hash_c1;
210               tr->label_net_byte_order = h1->label_exp_s_ttl;
211           }
212
213           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
214                                            to_next, n_left_to_next,
215                                            bi0, bi1, next0, next1);
216         }
217
218       while (n_left_from > 0 && n_left_to_next > 0)
219       {
220           u32 lbi0, next0, lfib_index0, bi0, hash_c0;
221           const mpls_unicast_header_t * h0;
222           const load_balance_t *lb0;
223           const dpo_id_t *dpo0;
224           vlib_buffer_t * b0;
225
226           bi0 = from[0];
227           to_next[0] = bi0;
228           from += 1;
229           to_next += 1;
230           n_left_from -= 1;
231           n_left_to_next -= 1;
232
233           b0 = vlib_get_buffer (vm, bi0);
234           h0 = vlib_buffer_get_current (b0);
235
236           lfib_index0 = vec_elt(mm->fib_index_by_sw_if_index,
237                                 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
238
239           lbi0 = mpls_fib_table_forwarding_lookup(lfib_index0, h0);
240           lb0 = load_balance_get(lbi0);
241
242           hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
243           if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
244           {
245               hash_c0 = vnet_buffer (b0)->ip.flow_hash =
246                   mpls_compute_flow_hash(h0, lb0->lb_hash_config);
247           }
248
249           ASSERT (lb0->lb_n_buckets > 0);
250           ASSERT (is_pow2 (lb0->lb_n_buckets));
251
252           dpo0 = load_balance_get_bucket_i(lb0,
253                                            (hash_c0 &
254                                             (lb0->lb_n_buckets_minus_1)));
255
256           next0 = dpo0->dpoi_next_node;
257           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
258
259           vlib_increment_combined_counter
260               (cm, cpu_index, lbi0, 1,
261                vlib_buffer_length_in_chain (vm, b0));
262
263           /*
264            * before we pop the label copy th values we need to maintain.
265            * The label header is in network byte order.
266            *  last byte is the TTL.
267            *  bits 2 to 4 inclusive are the EXP bits
268            */
269           vnet_buffer (b0)->mpls.ttl = ((char*)h0)[3];
270           vnet_buffer (b0)->mpls.exp = (((char*)h0)[2] & 0xe) >> 1;
271           vnet_buffer (b0)->mpls.first = 1;
272
273           /*
274            * pop the label that was just used in the lookup
275            */
276           vlib_buffer_advance(b0, sizeof(*h0));
277
278           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
279           {
280               mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
281                                                         b0, sizeof (*tr));
282               tr->next_index = next0;
283               tr->lb_index = lbi0;
284               tr->lfib_index = lfib_index0;
285               tr->hash = hash_c0;
286               tr->label_net_byte_order = h0->label_exp_s_ttl;
287           }
288
289           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
290                                            to_next, n_left_to_next,
291                                            bi0, next0);
292         }
293
294       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
295     }
296   vlib_node_increment_counter (vm, mpls_lookup_node.index,
297                                MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
298   return from_frame->n_vectors;
299 }
300
301 static char * mpls_error_strings[] = {
302 #define mpls_error(n,s) s,
303 #include "error.def"
304 #undef mpls_error
305 };
306
307 VLIB_REGISTER_NODE (mpls_lookup_node) = {
308   .function = mpls_lookup,
309   .name = "mpls-lookup",
310   /* Takes a vector of packets. */
311   .vector_size = sizeof (u32),
312   .n_errors = MPLS_N_ERROR,
313   .error_strings = mpls_error_strings,
314
315   .sibling_of = "ip4-lookup",
316
317   .format_buffer = format_mpls_header,
318   .format_trace = format_mpls_lookup_trace,
319   .unformat_buffer = unformat_mpls_header,
320 };
321
322 VLIB_NODE_FUNCTION_MULTIARCH (mpls_lookup_node, mpls_lookup)
323
324 typedef struct {
325   u32 next_index;
326   u32 lb_index;
327   u32 hash;
328 } mpls_load_balance_trace_t;
329
330 static u8 *
331 format_mpls_load_balance_trace (u8 * s, va_list * args)
332 {
333   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
334   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
335   mpls_load_balance_trace_t * t = va_arg (*args, mpls_load_balance_trace_t *);
336
337   s = format (s, "MPLS: next [%d], LB index %d hash %d",
338               t->next_index, t->lb_index, t->hash);
339   return s;
340 }
341
342 always_inline uword
343 mpls_load_balance (vlib_main_t * vm,
344                   vlib_node_runtime_t * node,
345                   vlib_frame_t * frame)
346 {
347   vlib_combined_counter_main_t * cm = &load_balance_main.lbm_via_counters;
348   u32 n_left_from, n_left_to_next, * from, * to_next;
349   u32 cpu_index = os_get_cpu_number();
350   u32 next;
351
352   from = vlib_frame_vector_args (frame);
353   n_left_from = frame->n_vectors;
354   next = node->cached_next_index;
355
356   while (n_left_from > 0)
357     {
358       vlib_get_next_frame (vm, node, next,
359                            to_next, n_left_to_next);
360
361
362       while (n_left_from >= 4 && n_left_to_next >= 2)
363         {
364           mpls_lookup_next_t next0, next1;
365           const load_balance_t *lb0, *lb1;
366           vlib_buffer_t * p0, *p1;
367           u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
368           const mpls_unicast_header_t *mpls0, *mpls1;
369           const dpo_id_t *dpo0, *dpo1;
370
371           /* Prefetch next iteration. */
372           {
373             vlib_buffer_t * p2, * p3;
374
375             p2 = vlib_get_buffer (vm, from[2]);
376             p3 = vlib_get_buffer (vm, from[3]);
377
378             vlib_prefetch_buffer_header (p2, STORE);
379             vlib_prefetch_buffer_header (p3, STORE);
380
381             CLIB_PREFETCH (p2->data, sizeof (mpls0[0]), STORE);
382             CLIB_PREFETCH (p3->data, sizeof (mpls0[0]), STORE);
383           }
384
385           pi0 = to_next[0] = from[0];
386           pi1 = to_next[1] = from[1];
387
388           from += 2;
389           n_left_from -= 2;
390           to_next += 2;
391           n_left_to_next -= 2;
392
393           p0 = vlib_get_buffer (vm, pi0);
394           p1 = vlib_get_buffer (vm, pi1);
395
396           mpls0 = vlib_buffer_get_current (p0);
397           mpls1 = vlib_buffer_get_current (p1);
398           lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
399           lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
400
401           lb0 = load_balance_get(lbi0);
402           lb1 = load_balance_get(lbi1);
403
404           /*
405            * this node is for via FIBs we can re-use the hash value from the
406            * to node if present.
407            * We don't want to use the same hash value at each level in the recursion
408            * graph as that would lead to polarisation
409            */
410           hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
411           hc1 = vnet_buffer (p1)->ip.flow_hash = 0;
412
413           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
414           {
415               if (PREDICT_TRUE (vnet_buffer(p0)->ip.flow_hash))
416               {
417                   hc0 = vnet_buffer(p0)->ip.flow_hash = vnet_buffer(p0)->ip.flow_hash >> 1;
418               }
419               else
420               {
421                   hc0 = vnet_buffer(p0)->ip.flow_hash = mpls_compute_flow_hash(mpls0, hc0);
422               }
423           }
424           if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
425           {
426               if (PREDICT_TRUE (vnet_buffer(p1)->ip.flow_hash))
427               {
428                   hc1 = vnet_buffer(p1)->ip.flow_hash = vnet_buffer(p1)->ip.flow_hash >> 1;
429               }
430               else
431               {
432                   hc1 = vnet_buffer(p1)->ip.flow_hash = mpls_compute_flow_hash(mpls1, hc1);
433               }
434           }
435
436           dpo0 = load_balance_get_bucket_i(lb0, hc0 & (lb0->lb_n_buckets_minus_1));
437           dpo1 = load_balance_get_bucket_i(lb1, hc1 & (lb1->lb_n_buckets_minus_1));
438
439           next0 = dpo0->dpoi_next_node;
440           next1 = dpo1->dpoi_next_node;
441
442           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
443           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
444
445           vlib_increment_combined_counter
446               (cm, cpu_index, lbi0, 1,
447                vlib_buffer_length_in_chain (vm, p0));
448           vlib_increment_combined_counter
449               (cm, cpu_index, lbi1, 1,
450                vlib_buffer_length_in_chain (vm, p1));
451
452           if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
453           {
454               mpls_load_balance_trace_t *tr = vlib_add_trace (vm, node,
455                                                               p0, sizeof (*tr));
456               tr->next_index = next0;
457               tr->lb_index = lbi0;
458               tr->hash = hc0;
459           }
460
461           vlib_validate_buffer_enqueue_x2 (vm, node, next,
462                                            to_next, n_left_to_next,
463                                            pi0, pi1, next0, next1);
464        }
465
466       while (n_left_from > 0 && n_left_to_next > 0)
467         {
468           mpls_lookup_next_t next0;
469           const load_balance_t *lb0;
470           vlib_buffer_t * p0;
471           u32 pi0, lbi0, hc0;
472           const mpls_unicast_header_t *mpls0;
473           const dpo_id_t *dpo0;
474
475           pi0 = from[0];
476           to_next[0] = pi0;
477           from += 1;
478           to_next += 1;
479           n_left_to_next -= 1;
480           n_left_from -= 1;
481
482           p0 = vlib_get_buffer (vm, pi0);
483
484           mpls0 = vlib_buffer_get_current (p0);
485           lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
486
487           lb0 = load_balance_get(lbi0);
488
489           hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
490           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
491           {
492               if (PREDICT_TRUE (vnet_buffer(p0)->ip.flow_hash))
493               {
494                   hc0 = vnet_buffer(p0)->ip.flow_hash = vnet_buffer(p0)->ip.flow_hash >> 1;
495               }
496               else
497               {
498                   hc0 = vnet_buffer(p0)->ip.flow_hash = mpls_compute_flow_hash(mpls0, hc0);
499               }
500           }
501
502           dpo0 = load_balance_get_bucket_i(lb0, hc0 & (lb0->lb_n_buckets_minus_1));
503
504           next0 = dpo0->dpoi_next_node;
505           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
506
507           vlib_increment_combined_counter
508               (cm, cpu_index, lbi0, 1,
509                vlib_buffer_length_in_chain (vm, p0));
510
511           vlib_validate_buffer_enqueue_x1 (vm, node, next,
512                                            to_next, n_left_to_next,
513                                            pi0, next0);
514         }
515
516       vlib_put_next_frame (vm, node, next, n_left_to_next);
517     }
518
519   return frame->n_vectors;
520 }
521
522 VLIB_REGISTER_NODE (mpls_load_balance_node) = {
523   .function = mpls_load_balance,
524   .name = "mpls-load-balance",
525   .vector_size = sizeof (u32),
526   .sibling_of = "mpls-lookup",
527
528   .format_trace = format_mpls_load_balance_trace,
529 };
530
531 VLIB_NODE_FUNCTION_MULTIARCH (mpls_load_balance_node, mpls_load_balance)