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;
31 } mpls_lookup_trace_t;
34 format_mpls_lookup_trace (u8 * s, va_list * args)
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 *);
40 s = format (s, "MPLS: next [%d], lookup fib index %d, LB index %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));
51 * We'll use it to select which adjacency to use for this flow. And other things.
54 mpls_compute_flow_hash (const mpls_unicast_header_t * hdr,
55 flow_hash_config_t flow_hash_config)
58 return (vnet_mpls_uc_get_label(hdr->label_exp_s_ttl));
62 mpls_lookup (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
64 vlib_frame_t * from_frame)
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();
71 from = vlib_frame_vector_args (from_frame);
72 n_left_from = from_frame->n_vectors;
73 next_index = node->cached_next_index;
75 while (n_left_from > 0)
79 vlib_get_next_frame (vm, node, next_index,
80 to_next, n_left_to_next);
82 while (n_left_from > 0 && n_left_to_next > 0)
84 u32 lbi0, next0, lfib_index0, bi0, hash_c0;
85 const mpls_unicast_header_t * h0;
86 const load_balance_t *lb0;
97 b0 = vlib_get_buffer (vm, bi0);
98 h0 = vlib_buffer_get_current (b0);
100 lfib_index0 = vec_elt(mm->fib_index_by_sw_if_index,
101 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
103 lbi0 = mpls_fib_table_forwarding_lookup (lfib_index0, h0);
104 lb0 = load_balance_get(lbi0);
106 hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
107 if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
109 hash_c0 = vnet_buffer (b0)->ip.flow_hash =
110 mpls_compute_flow_hash(h0, lb0->lb_hash_config);
113 ASSERT (lb0->lb_n_buckets > 0);
114 ASSERT (is_pow2 (lb0->lb_n_buckets));
116 dpo0 = load_balance_get_bucket_i(lb0,
118 (lb0->lb_n_buckets_minus_1)));
120 next0 = dpo0->dpoi_next_node;
121 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
123 vlib_increment_combined_counter
124 (cm, cpu_index, lbi0, 1,
125 vlib_buffer_length_in_chain (vm, b0));
128 * pop the label that was just used in the lookup
130 vlib_buffer_advance(b0, sizeof(*h0));
132 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
134 mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
136 tr->next_index = next0;
138 tr->lfib_index = lfib_index0;
139 tr->label_net_byte_order = h0->label_exp_s_ttl;
142 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
143 to_next, n_left_to_next,
147 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
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;
154 static char * mpls_error_strings[] = {
155 #define mpls_error(n,s) s,
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,
168 .sibling_of = "ip4-lookup",
170 .format_buffer = format_mpls_header,
171 .format_trace = format_mpls_lookup_trace,
172 .unformat_buffer = unformat_mpls_header,
175 VLIB_NODE_FUNCTION_MULTIARCH (mpls_lookup_node, mpls_lookup)
180 } mpls_load_balance_trace_t;
183 format_mpls_load_balance_trace (u8 * s, va_list * args)
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 *);
189 s = format (s, "MPLS: next [%d], LB index %d ",
190 t->next_index, t->lb_index);
195 mpls_load_balance (vlib_main_t * vm,
196 vlib_node_runtime_t * node,
197 vlib_frame_t * frame)
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();
204 from = vlib_frame_vector_args (frame);
205 n_left_from = frame->n_vectors;
206 next = node->cached_next_index;
208 while (n_left_from > 0)
210 vlib_get_next_frame (vm, node, next,
211 to_next, n_left_to_next);
214 while (n_left_from > 0 && n_left_to_next > 0)
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;
225 p0 = vlib_get_buffer (vm, pi0);
227 hdr0 = vlib_buffer_get_current (p0);
228 lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
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);
234 dpo0 = load_balance_get_bucket_i(lb0,
235 vnet_buffer(p0)->ip.flow_hash &
236 (lb0->lb_n_buckets_minus_1));
238 next0 = dpo0->dpoi_next_node;
239 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
241 vlib_increment_combined_counter
242 (cm, cpu_index, lbi0, 1,
243 vlib_buffer_length_in_chain (vm, p0));
250 if (PREDICT_FALSE (next0 != next))
253 vlib_put_next_frame (vm, node, next, n_left_to_next);
255 vlib_get_next_frame (vm, node, next,
256 to_next, n_left_to_next);
263 vlib_put_next_frame (vm, node, next, n_left_to_next);
266 return frame->n_vectors;
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",
275 .format_trace = format_mpls_load_balance_trace,
278 VLIB_NODE_FUNCTION_MULTIARCH (mpls_load_balance_node, mpls_load_balance)