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,
51 mpls_label_dpo_t *mld;
53 mld = mpls_label_dpo_alloc();
55 vnet_mpls_uc_set_label(&mld->mld_hdr.label_exp_s_ttl, label);
56 vnet_mpls_uc_set_ttl(&mld->mld_hdr.label_exp_s_ttl, ttl);
57 vnet_mpls_uc_set_exp(&mld->mld_hdr.label_exp_s_ttl, exp);
58 vnet_mpls_uc_set_s(&mld->mld_hdr.label_exp_s_ttl, eos);
61 * get the header in network byte order since we will paint it
62 * on a packet in the data-plane
64 mld->mld_hdr.label_exp_s_ttl =
65 clib_host_to_net_u32(mld->mld_hdr.label_exp_s_ttl);
67 dpo_stack(DPO_MPLS_LABEL, DPO_PROTO_MPLS, &mld->mld_dpo, dpo);
69 return (mpls_label_dpo_get_index(mld));
73 format_mpls_label_dpo (u8 *s, va_list *args)
75 index_t index = va_arg (*args, index_t);
76 u32 indent = va_arg (*args, u32);
77 mpls_unicast_header_t hdr;
78 mpls_label_dpo_t *mld;
80 mld = mpls_label_dpo_get(index);
83 clib_net_to_host_u32(mld->mld_hdr.label_exp_s_ttl);
85 s = format(s, "mpls-label:[%d]:", index);
86 s = format(s, "%U\n", format_mpls_header, hdr);
87 s = format(s, "%U", format_white_space, indent);
88 s = format(s, "%U", format_dpo_id, &mld->mld_dpo, indent+2);
94 mpls_label_dpo_lock (dpo_id_t *dpo)
96 mpls_label_dpo_t *mld;
98 mld = mpls_label_dpo_get(dpo->dpoi_index);
104 mpls_label_dpo_unlock (dpo_id_t *dpo)
106 mpls_label_dpo_t *mld;
108 mld = mpls_label_dpo_get(dpo->dpoi_index);
112 if (0 == mld->mld_locks)
114 dpo_reset(&mld->mld_dpo);
115 pool_put(mpls_label_dpo_pool, mld);
120 * @brief A struct to hold tracing information for the MPLS label imposition
123 typedef struct mpls_label_imposition_trace_t_
126 * The MPLS header imposed
128 mpls_unicast_header_t hdr;
129 } mpls_label_imposition_trace_t;
132 mpls_label_imposition (vlib_main_t * vm,
133 vlib_node_runtime_t * node,
134 vlib_frame_t * from_frame)
136 u32 n_left_from, next_index, * from, * to_next;
138 from = vlib_frame_vector_args (from_frame);
139 n_left_from = from_frame->n_vectors;
141 next_index = node->cached_next_index;
143 while (n_left_from > 0)
147 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
149 while (n_left_from > 0 && n_left_to_next > 0)
151 mpls_unicast_header_t *hdr0;
152 mpls_label_dpo_t *mld0;
164 b0 = vlib_get_buffer (vm, bi0);
166 /* dst lookup was done by ip4 lookup */
167 mldi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
168 mld0 = mpls_label_dpo_get(mldi0);
170 /* Paint the MPLS header */
171 vlib_buffer_advance(b0, -sizeof(*hdr0));
172 hdr0 = vlib_buffer_get_current(b0);
175 // need to copy the TTL from the correct place.
176 // for IPvX imposition from the IP header
177 // so we need a deidcated ipx-to-mpls-label-imp-node
178 // for mpls switch and stack another solution is required.
179 *hdr0 = mld0->mld_hdr;
181 next0 = mld0->mld_dpo.dpoi_next_node;
182 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mld0->mld_dpo.dpoi_index;
184 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
186 mpls_label_imposition_trace_t *tr =
187 vlib_add_trace (vm, node, b0, sizeof (*tr));
191 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
192 n_left_to_next, bi0, next0);
194 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
196 return from_frame->n_vectors;
200 format_mpls_label_imposition_trace (u8 * s, va_list * args)
202 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
203 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
204 mpls_label_imposition_trace_t * t;
205 mpls_unicast_header_t hdr;
208 t = va_arg (*args, mpls_label_imposition_trace_t *);
209 indent = format_get_indent (s);
210 hdr.label_exp_s_ttl = clib_net_to_host_u32(t->hdr.label_exp_s_ttl);
212 s = format (s, "%Umpls-header:%U",
213 format_white_space, indent,
214 format_mpls_header, hdr);
218 VLIB_REGISTER_NODE (mpls_label_imposition_node) = {
219 .function = mpls_label_imposition,
220 .name = "mpls-label-imposition",
221 .vector_size = sizeof (u32),
223 .format_trace = format_mpls_label_imposition_trace,
229 VLIB_NODE_FUNCTION_MULTIARCH (mpls_label_imposition_node, mpls_label_imposition)
232 mpls_label_dpo_mem_show (void)
234 fib_show_memory_usage("MPLS label",
235 pool_elts(mpls_label_dpo_pool),
236 pool_len(mpls_label_dpo_pool),
237 sizeof(mpls_label_dpo_t));
240 const static dpo_vft_t mld_vft = {
241 .dv_lock = mpls_label_dpo_lock,
242 .dv_unlock = mpls_label_dpo_unlock,
243 .dv_format = format_mpls_label_dpo,
244 .dv_mem_show = mpls_label_dpo_mem_show,
247 const static char* const mpls_label_imp_ip4_nodes[] =
249 "mpls-label-imposition",
252 const static char* const mpls_label_imp_ip6_nodes[] =
254 "mpls-label-imposition",
257 const static char* const mpls_label_imp_mpls_nodes[] =
259 "mpls-label-imposition",
262 const static char* const * const mpls_label_imp_nodes[DPO_PROTO_NUM] =
264 [DPO_PROTO_IP4] = mpls_label_imp_ip4_nodes,
265 [DPO_PROTO_IP6] = mpls_label_imp_ip6_nodes,
266 [DPO_PROTO_MPLS] = mpls_label_imp_mpls_nodes,
271 mpls_label_dpo_module_init (void)
273 dpo_register(DPO_MPLS_LABEL, &mld_vft, mpls_label_imp_nodes);