2 * mpls_lookup.c: MPLS lookup
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
24 vlib_node_registration_t mpls_lookup_node;
30 u32 label_net_byte_order;
32 } mpls_lookup_trace_t;
35 format_mpls_lookup_trace (u8 * s, va_list * args)
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 *);
41 s = format (s, "MPLS: next [%d], lookup fib index %d, LB index %d hash %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));
52 * We'll use it to select which adjacency to use for this flow. And other things.
55 mpls_compute_flow_hash (const mpls_unicast_header_t * hdr,
56 flow_hash_config_t flow_hash_config)
59 return (vnet_mpls_uc_get_label(hdr->label_exp_s_ttl));
63 mpls_lookup (vlib_main_t * vm,
64 vlib_node_runtime_t * node,
65 vlib_frame_t * from_frame)
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();
72 from = vlib_frame_vector_args (from_frame);
73 n_left_from = from_frame->n_vectors;
74 next_index = node->cached_next_index;
76 while (n_left_from > 0)
80 vlib_get_next_frame (vm, node, next_index,
81 to_next, n_left_to_next);
83 while (n_left_from >= 4 && n_left_to_next >= 2)
85 u32 lbi0, next0, lfib_index0, bi0, hash_c0;
86 const mpls_unicast_header_t * h0;
87 const load_balance_t *lb0;
90 u32 lbi1, next1, lfib_index1, bi1, hash_c1;
91 const mpls_unicast_header_t * h1;
92 const load_balance_t *lb1;
96 /* Prefetch next iteration. */
98 vlib_buffer_t * p2, * p3;
100 p2 = vlib_get_buffer (vm, from[2]);
101 p3 = vlib_get_buffer (vm, from[3]);
103 vlib_prefetch_buffer_header (p2, STORE);
104 vlib_prefetch_buffer_header (p3, STORE);
106 CLIB_PREFETCH (p2->data, sizeof (h0[0]), STORE);
107 CLIB_PREFETCH (p3->data, sizeof (h0[0]), STORE);
110 bi0 = to_next[0] = from[0];
111 bi1 = to_next[1] = from[1];
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);
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]);
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);
133 hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
134 hash_c1 = vnet_buffer(b1)->ip.flow_hash = 0;
136 if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
138 hash_c0 = vnet_buffer (b0)->ip.flow_hash =
139 mpls_compute_flow_hash(h0, lb0->lb_hash_config);
141 if (PREDICT_FALSE(lb1->lb_n_buckets > 1))
143 hash_c1 = vnet_buffer (b1)->ip.flow_hash =
144 mpls_compute_flow_hash(h1, lb1->lb_hash_config);
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));
152 dpo0 = load_balance_get_bucket_i(lb0,
154 (lb0->lb_n_buckets_minus_1)));
155 dpo1 = load_balance_get_bucket_i(lb1,
157 (lb1->lb_n_buckets_minus_1)));
159 next0 = dpo0->dpoi_next_node;
160 next1 = dpo1->dpoi_next_node;
162 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
163 vnet_buffer (b1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
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));
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
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;
186 * pop the label that was just used in the lookup
188 vlib_buffer_advance(b0, sizeof(*h0));
189 vlib_buffer_advance(b1, sizeof(*h1));
191 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
193 mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
195 tr->next_index = next0;
197 tr->lfib_index = lfib_index0;
199 tr->label_net_byte_order = h0->label_exp_s_ttl;
202 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
204 mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
206 tr->next_index = next1;
208 tr->lfib_index = lfib_index1;
210 tr->label_net_byte_order = h1->label_exp_s_ttl;
213 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
214 to_next, n_left_to_next,
215 bi0, bi1, next0, next1);
218 while (n_left_from > 0 && n_left_to_next > 0)
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;
233 b0 = vlib_get_buffer (vm, bi0);
234 h0 = vlib_buffer_get_current (b0);
236 lfib_index0 = vec_elt(mm->fib_index_by_sw_if_index,
237 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
239 lbi0 = mpls_fib_table_forwarding_lookup(lfib_index0, h0);
240 lb0 = load_balance_get(lbi0);
242 hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
243 if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
245 hash_c0 = vnet_buffer (b0)->ip.flow_hash =
246 mpls_compute_flow_hash(h0, lb0->lb_hash_config);
249 ASSERT (lb0->lb_n_buckets > 0);
250 ASSERT (is_pow2 (lb0->lb_n_buckets));
252 dpo0 = load_balance_get_bucket_i(lb0,
254 (lb0->lb_n_buckets_minus_1)));
256 next0 = dpo0->dpoi_next_node;
257 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
259 vlib_increment_combined_counter
260 (cm, cpu_index, lbi0, 1,
261 vlib_buffer_length_in_chain (vm, b0));
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
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;
274 * pop the label that was just used in the lookup
276 vlib_buffer_advance(b0, sizeof(*h0));
278 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
280 mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
282 tr->next_index = next0;
284 tr->lfib_index = lfib_index0;
286 tr->label_net_byte_order = h0->label_exp_s_ttl;
289 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
290 to_next, n_left_to_next,
294 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
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;
301 static char * mpls_error_strings[] = {
302 #define mpls_error(n,s) s,
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,
315 .sibling_of = "ip4-lookup",
317 .format_buffer = format_mpls_header,
318 .format_trace = format_mpls_lookup_trace,
319 .unformat_buffer = unformat_mpls_header,
322 VLIB_NODE_FUNCTION_MULTIARCH (mpls_lookup_node, mpls_lookup)
328 } mpls_load_balance_trace_t;
331 format_mpls_load_balance_trace (u8 * s, va_list * args)
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 *);
337 s = format (s, "MPLS: next [%d], LB index %d hash %d",
338 t->next_index, t->lb_index, t->hash);
343 mpls_load_balance (vlib_main_t * vm,
344 vlib_node_runtime_t * node,
345 vlib_frame_t * frame)
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();
352 from = vlib_frame_vector_args (frame);
353 n_left_from = frame->n_vectors;
354 next = node->cached_next_index;
356 while (n_left_from > 0)
358 vlib_get_next_frame (vm, node, next,
359 to_next, n_left_to_next);
362 while (n_left_from >= 4 && n_left_to_next >= 2)
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;
371 /* Prefetch next iteration. */
373 vlib_buffer_t * p2, * p3;
375 p2 = vlib_get_buffer (vm, from[2]);
376 p3 = vlib_get_buffer (vm, from[3]);
378 vlib_prefetch_buffer_header (p2, STORE);
379 vlib_prefetch_buffer_header (p3, STORE);
381 CLIB_PREFETCH (p2->data, sizeof (mpls0[0]), STORE);
382 CLIB_PREFETCH (p3->data, sizeof (mpls0[0]), STORE);
385 pi0 = to_next[0] = from[0];
386 pi1 = to_next[1] = from[1];
393 p0 = vlib_get_buffer (vm, pi0);
394 p1 = vlib_get_buffer (vm, pi1);
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];
401 lb0 = load_balance_get(lbi0);
402 lb1 = load_balance_get(lbi1);
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
410 hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
411 hc1 = vnet_buffer (p1)->ip.flow_hash = 0;
413 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
415 if (PREDICT_TRUE (vnet_buffer(p0)->ip.flow_hash))
417 hc0 = vnet_buffer(p0)->ip.flow_hash = vnet_buffer(p0)->ip.flow_hash >> 1;
421 hc0 = vnet_buffer(p0)->ip.flow_hash = mpls_compute_flow_hash(mpls0, hc0);
424 if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
426 if (PREDICT_TRUE (vnet_buffer(p1)->ip.flow_hash))
428 hc1 = vnet_buffer(p1)->ip.flow_hash = vnet_buffer(p1)->ip.flow_hash >> 1;
432 hc1 = vnet_buffer(p1)->ip.flow_hash = mpls_compute_flow_hash(mpls1, hc1);
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));
439 next0 = dpo0->dpoi_next_node;
440 next1 = dpo1->dpoi_next_node;
442 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
443 vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
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));
452 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
454 mpls_load_balance_trace_t *tr = vlib_add_trace (vm, node,
456 tr->next_index = next0;
461 vlib_validate_buffer_enqueue_x2 (vm, node, next,
462 to_next, n_left_to_next,
463 pi0, pi1, next0, next1);
466 while (n_left_from > 0 && n_left_to_next > 0)
468 mpls_lookup_next_t next0;
469 const load_balance_t *lb0;
472 const mpls_unicast_header_t *mpls0;
473 const dpo_id_t *dpo0;
482 p0 = vlib_get_buffer (vm, pi0);
484 mpls0 = vlib_buffer_get_current (p0);
485 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
487 lb0 = load_balance_get(lbi0);
489 hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
490 if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
492 if (PREDICT_TRUE (vnet_buffer(p0)->ip.flow_hash))
494 hc0 = vnet_buffer(p0)->ip.flow_hash = vnet_buffer(p0)->ip.flow_hash >> 1;
498 hc0 = vnet_buffer(p0)->ip.flow_hash = mpls_compute_flow_hash(mpls0, hc0);
502 dpo0 = load_balance_get_bucket_i(lb0, hc0 & (lb0->lb_n_buckets_minus_1));
504 next0 = dpo0->dpoi_next_node;
505 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
507 vlib_increment_combined_counter
508 (cm, cpu_index, lbi0, 1,
509 vlib_buffer_length_in_chain (vm, p0));
511 vlib_validate_buffer_enqueue_x1 (vm, node, next,
512 to_next, n_left_to_next,
516 vlib_put_next_frame (vm, node, next, n_left_to_next);
519 return frame->n_vectors;
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",
528 .format_trace = format_mpls_load_balance_trace,
531 VLIB_NODE_FUNCTION_MULTIARCH (mpls_load_balance_node, mpls_load_balance)