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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/ip/ip.h>
17 #include <vnet/dpo/mpls_label_dpo.h>
18 #include <vnet/mpls/mpls.h>
21 * pool of all MPLS Label DPOs
23 mpls_label_dpo_t *mpls_label_dpo_pool;
25 static mpls_label_dpo_t *
26 mpls_label_dpo_alloc (void)
28 mpls_label_dpo_t *mld;
30 pool_get_aligned(mpls_label_dpo_pool, mld, CLIB_CACHE_LINE_BYTES);
31 memset(mld, 0, sizeof(*mld));
33 dpo_reset(&mld->mld_dpo);
39 mpls_label_dpo_get_index (mpls_label_dpo_t *mld)
41 return (mld - mpls_label_dpo_pool);
45 mpls_label_dpo_create (mpls_label_t *label_stack,
49 dpo_proto_t payload_proto,
52 mpls_label_dpo_t *mld;
55 mld = mpls_label_dpo_alloc();
56 mld->mld_n_labels = vec_len(label_stack);
57 mld->mld_payload_proto = payload_proto;
60 * construct label rewrite headers for each value value passed.
61 * get the header in network byte order since we will paint it
62 * on a packet in the data-plane
65 for (ii = 0; ii < mld->mld_n_labels-1; ii++)
67 vnet_mpls_uc_set_label(&mld->mld_hdr[ii].label_exp_s_ttl, label_stack[ii]);
68 vnet_mpls_uc_set_ttl(&mld->mld_hdr[ii].label_exp_s_ttl, 255);
69 vnet_mpls_uc_set_exp(&mld->mld_hdr[ii].label_exp_s_ttl, 0);
70 vnet_mpls_uc_set_s(&mld->mld_hdr[ii].label_exp_s_ttl, MPLS_NON_EOS);
71 mld->mld_hdr[ii].label_exp_s_ttl =
72 clib_host_to_net_u32(mld->mld_hdr[ii].label_exp_s_ttl);
76 * the inner most label
78 ii = mld->mld_n_labels-1;
80 vnet_mpls_uc_set_label(&mld->mld_hdr[ii].label_exp_s_ttl, label_stack[ii]);
81 vnet_mpls_uc_set_ttl(&mld->mld_hdr[ii].label_exp_s_ttl, ttl);
82 vnet_mpls_uc_set_exp(&mld->mld_hdr[ii].label_exp_s_ttl, exp);
83 vnet_mpls_uc_set_s(&mld->mld_hdr[ii].label_exp_s_ttl, eos);
84 mld->mld_hdr[ii].label_exp_s_ttl =
85 clib_host_to_net_u32(mld->mld_hdr[ii].label_exp_s_ttl);
88 * stack this label objct on its parent.
90 dpo_stack(DPO_MPLS_LABEL,
91 mld->mld_payload_proto,
95 return (mpls_label_dpo_get_index(mld));
99 format_mpls_label_dpo (u8 *s, va_list *args)
101 index_t index = va_arg (*args, index_t);
102 u32 indent = va_arg (*args, u32);
103 mpls_unicast_header_t hdr;
104 mpls_label_dpo_t *mld;
107 mld = mpls_label_dpo_get(index);
109 s = format(s, "mpls-label:[%d]:", index);
111 for (ii = 0; ii < mld->mld_n_labels; ii++)
113 hdr.label_exp_s_ttl =
114 clib_net_to_host_u32(mld->mld_hdr[ii].label_exp_s_ttl);
115 s = format(s, "%U", format_mpls_header, hdr);
118 s = format(s, "\n%U", format_white_space, indent);
119 s = format(s, "%U", format_dpo_id, &mld->mld_dpo, indent+2);
125 mpls_label_dpo_lock (dpo_id_t *dpo)
127 mpls_label_dpo_t *mld;
129 mld = mpls_label_dpo_get(dpo->dpoi_index);
135 mpls_label_dpo_unlock (dpo_id_t *dpo)
137 mpls_label_dpo_t *mld;
139 mld = mpls_label_dpo_get(dpo->dpoi_index);
143 if (0 == mld->mld_locks)
145 dpo_reset(&mld->mld_dpo);
146 pool_put(mpls_label_dpo_pool, mld);
151 * @brief A struct to hold tracing information for the MPLS label imposition
154 typedef struct mpls_label_imposition_trace_t_
157 * The MPLS header imposed
159 mpls_unicast_header_t hdr;
160 } mpls_label_imposition_trace_t;
163 mpls_label_imposition_inline (vlib_main_t * vm,
164 vlib_node_runtime_t * node,
165 vlib_frame_t * from_frame,
169 u32 n_left_from, next_index, * from, * to_next;
171 from = vlib_frame_vector_args (from_frame);
172 n_left_from = from_frame->n_vectors;
174 next_index = node->cached_next_index;
176 while (n_left_from > 0)
180 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
182 while (n_left_from > 0 && n_left_to_next > 0)
184 mpls_unicast_header_t *hdr0;
185 mpls_label_dpo_t *mld0;
198 b0 = vlib_get_buffer (vm, bi0);
200 /* dst lookup was done by ip4 lookup */
201 mldi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
202 mld0 = mpls_label_dpo_get(mldi0);
207 * decrement the TTL on ingress to the LSP
209 ip4_header_t * ip0 = vlib_buffer_get_current(b0);
212 checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
213 checksum0 += checksum0 >= 0xffff;
215 ip0->checksum = checksum0;
219 else if (payload_is_ip6)
222 * decrement the TTL on ingress to the LSP
224 ip6_header_t * ip0 = vlib_buffer_get_current(b0);
227 ttl = ip0->hop_limit;
232 * else, the packet to be encapped is an MPLS packet
234 if (vnet_buffer(b0)->mpls.first)
237 * The first label to be imposed on the packet. this is a label swap.
238 * in which case we stashed the TTL and EXP bits in the
239 * packet in the lookup node
241 ASSERT(0 != vnet_buffer (b0)->mpls.ttl);
243 ttl = vnet_buffer(b0)->mpls.ttl - 1;
248 * not the first label. implying we are recusring down a chain of
250 * Each layer is considered a new LSP - hence the TTL is reset.
255 vnet_buffer(b0)->mpls.first = 0;
257 /* Paint the MPLS header */
258 vlib_buffer_advance(b0, -(sizeof(*hdr0) * mld0->mld_n_labels));
259 hdr0 = vlib_buffer_get_current(b0);
261 clib_memcpy(hdr0, mld0->mld_hdr,
262 sizeof(*hdr0) * mld0->mld_n_labels);
264 /* fixup the TTL for the inner most label */
265 hdr0 = hdr0 + (mld0->mld_n_labels - 1);
266 ((char*)hdr0)[3] = ttl;
268 next0 = mld0->mld_dpo.dpoi_next_node;
269 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mld0->mld_dpo.dpoi_index;
271 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
273 mpls_label_imposition_trace_t *tr =
274 vlib_add_trace (vm, node, b0, sizeof (*tr));
278 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
279 n_left_to_next, bi0, next0);
281 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
283 return from_frame->n_vectors;
287 format_mpls_label_imposition_trace (u8 * s, va_list * args)
289 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
290 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
291 mpls_label_imposition_trace_t * t;
292 mpls_unicast_header_t hdr;
295 t = va_arg (*args, mpls_label_imposition_trace_t *);
296 indent = format_get_indent (s);
297 hdr.label_exp_s_ttl = clib_net_to_host_u32(t->hdr.label_exp_s_ttl);
299 s = format (s, "%Umpls-header:%U",
300 format_white_space, indent,
301 format_mpls_header, hdr);
306 mpls_label_imposition (vlib_main_t * vm,
307 vlib_node_runtime_t * node,
308 vlib_frame_t * frame)
310 return (mpls_label_imposition_inline(vm, node, frame, 0, 0));
313 VLIB_REGISTER_NODE (mpls_label_imposition_node) = {
314 .function = mpls_label_imposition,
315 .name = "mpls-label-imposition",
316 .vector_size = sizeof (u32),
318 .format_trace = format_mpls_label_imposition_trace,
324 VLIB_NODE_FUNCTION_MULTIARCH (mpls_label_imposition_node,
325 mpls_label_imposition)
328 ip4_mpls_label_imposition (vlib_main_t * vm,
329 vlib_node_runtime_t * node,
330 vlib_frame_t * frame)
332 return (mpls_label_imposition_inline(vm, node, frame, 1, 0));
335 VLIB_REGISTER_NODE (ip4_mpls_label_imposition_node) = {
336 .function = ip4_mpls_label_imposition,
337 .name = "ip4-mpls-label-imposition",
338 .vector_size = sizeof (u32),
340 .format_trace = format_mpls_label_imposition_trace,
346 VLIB_NODE_FUNCTION_MULTIARCH (ip4_mpls_label_imposition_node,
347 ip4_mpls_label_imposition)
350 ip6_mpls_label_imposition (vlib_main_t * vm,
351 vlib_node_runtime_t * node,
352 vlib_frame_t * frame)
354 return (mpls_label_imposition_inline(vm, node, frame, 0, 1));
357 VLIB_REGISTER_NODE (ip6_mpls_label_imposition_node) = {
358 .function = ip6_mpls_label_imposition,
359 .name = "ip6-mpls-label-imposition",
360 .vector_size = sizeof (u32),
362 .format_trace = format_mpls_label_imposition_trace,
368 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mpls_label_imposition_node,
369 ip6_mpls_label_imposition)
372 mpls_label_dpo_mem_show (void)
374 fib_show_memory_usage("MPLS label",
375 pool_elts(mpls_label_dpo_pool),
376 pool_len(mpls_label_dpo_pool),
377 sizeof(mpls_label_dpo_t));
380 const static dpo_vft_t mld_vft = {
381 .dv_lock = mpls_label_dpo_lock,
382 .dv_unlock = mpls_label_dpo_unlock,
383 .dv_format = format_mpls_label_dpo,
384 .dv_mem_show = mpls_label_dpo_mem_show,
387 const static char* const mpls_label_imp_ip4_nodes[] =
389 "ip4-mpls-label-imposition",
392 const static char* const mpls_label_imp_ip6_nodes[] =
394 "ip6-mpls-label-imposition",
397 const static char* const mpls_label_imp_mpls_nodes[] =
399 "mpls-label-imposition",
402 const static char* const * const mpls_label_imp_nodes[DPO_PROTO_NUM] =
404 [DPO_PROTO_IP4] = mpls_label_imp_ip4_nodes,
405 [DPO_PROTO_IP6] = mpls_label_imp_ip6_nodes,
406 [DPO_PROTO_MPLS] = mpls_label_imp_mpls_nodes,
411 mpls_label_dpo_module_init (void)
413 dpo_register(DPO_MPLS_LABEL, &mld_vft, mpls_label_imp_nodes);