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_disposition.h>
18 #include <vnet/mpls/mpls.h>
21 * pool of all MPLS Label DPOs
23 mpls_disp_dpo_t *mpls_disp_dpo_pool;
25 static mpls_disp_dpo_t *
26 mpls_disp_dpo_alloc (void)
30 pool_get_aligned(mpls_disp_dpo_pool, mdd, CLIB_CACHE_LINE_BYTES);
31 memset(mdd, 0, sizeof(*mdd));
33 dpo_reset(&mdd->mdd_dpo);
39 mpls_disp_dpo_get_index (mpls_disp_dpo_t *mdd)
41 return (mdd - mpls_disp_dpo_pool);
45 mpls_disp_dpo_create (dpo_proto_t payload_proto,
51 mdd = mpls_disp_dpo_alloc();
53 mdd->mdd_payload_proto = payload_proto;
54 mdd->mdd_rpf_id = rpf_id;
56 dpo_stack(DPO_MPLS_DISPOSITION,
57 mdd->mdd_payload_proto,
61 return (mpls_disp_dpo_get_index(mdd));
65 format_mpls_disp_dpo (u8 *s, va_list *args)
67 index_t index = va_arg (*args, index_t);
68 u32 indent = va_arg (*args, u32);
71 mdd = mpls_disp_dpo_get(index);
73 s = format(s, "mpls-disposition:[%d]:[%U]",
75 format_dpo_proto, mdd->mdd_payload_proto);
77 s = format(s, "\n%U", format_white_space, indent);
78 s = format(s, "%U", format_dpo_id, &mdd->mdd_dpo, indent+2);
84 mpls_disp_dpo_lock (dpo_id_t *dpo)
88 mdd = mpls_disp_dpo_get(dpo->dpoi_index);
94 mpls_disp_dpo_unlock (dpo_id_t *dpo)
98 mdd = mpls_disp_dpo_get(dpo->dpoi_index);
102 if (0 == mdd->mdd_locks)
104 dpo_reset(&mdd->mdd_dpo);
105 pool_put(mpls_disp_dpo_pool, mdd);
110 * @brief A struct to hold tracing information for the MPLS label disposition
113 typedef struct mpls_label_disposition_trace_t_
116 } mpls_label_disposition_trace_t;
119 mpls_label_disposition_inline (vlib_main_t * vm,
120 vlib_node_runtime_t * node,
121 vlib_frame_t * from_frame,
125 u32 n_left_from, next_index, * from, * to_next;
127 from = vlib_frame_vector_args (from_frame);
128 n_left_from = from_frame->n_vectors;
130 next_index = node->cached_next_index;
132 while (n_left_from > 0)
136 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
138 while (n_left_from >= 4 && n_left_to_next >= 2)
140 mpls_disp_dpo_t *mdd0, *mdd1;
141 u32 bi0, mddi0, bi1, mddi1;
142 vlib_buffer_t * b0, *b1;
145 bi0 = to_next[0] = from[0];
146 bi1 = to_next[1] = from[1];
148 /* Prefetch next iteration. */
150 vlib_buffer_t * p2, * p3;
152 p2 = vlib_get_buffer (vm, from[2]);
153 p3 = vlib_get_buffer (vm, from[3]);
155 vlib_prefetch_buffer_header (p2, STORE);
156 vlib_prefetch_buffer_header (p3, STORE);
158 CLIB_PREFETCH (p2->data, sizeof (ip6_header_t), STORE);
159 CLIB_PREFETCH (p3->data, sizeof (ip6_header_t), STORE);
167 b0 = vlib_get_buffer (vm, bi0);
168 b1 = vlib_get_buffer (vm, bi1);
170 /* dst lookup was done by ip4 lookup */
171 mddi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
172 mddi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
173 mdd0 = mpls_disp_dpo_get(mddi0);
174 mdd1 = mpls_disp_dpo_get(mddi1);
179 * decrement the TTL on ingress to the LSP
182 else if (payload_is_ip6)
185 * decrement the TTL on ingress to the LSP
189 next0 = mdd0->mdd_dpo.dpoi_next_node;
190 next1 = mdd1->mdd_dpo.dpoi_next_node;
191 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mdd0->mdd_dpo.dpoi_index;
192 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = mdd1->mdd_dpo.dpoi_index;
193 vnet_buffer(b0)->ip.rpf_id = mdd0->mdd_rpf_id;
194 vnet_buffer(b1)->ip.rpf_id = mdd1->mdd_rpf_id;
196 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
198 mpls_label_disposition_trace_t *tr =
199 vlib_add_trace (vm, node, b0, sizeof (*tr));
203 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
205 mpls_label_disposition_trace_t *tr =
206 vlib_add_trace (vm, node, b1, sizeof (*tr));
210 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
212 bi0, bi1, next0, next1);
215 while (n_left_from > 0 && n_left_to_next > 0)
217 mpls_disp_dpo_t *mdd0;
229 b0 = vlib_get_buffer (vm, bi0);
231 /* dst lookup was done by ip4 lookup */
232 mddi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
233 mdd0 = mpls_disp_dpo_get(mddi0);
238 * decrement the TTL on ingress to the LSP
241 else if (payload_is_ip6)
244 * decrement the TTL on ingress to the LSP
251 next0 = mdd0->mdd_dpo.dpoi_next_node;
252 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mdd0->mdd_dpo.dpoi_index;
253 vnet_buffer(b0)->ip.rpf_id = mdd0->mdd_rpf_id;
255 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
257 mpls_label_disposition_trace_t *tr =
258 vlib_add_trace (vm, node, b0, sizeof (*tr));
262 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
263 n_left_to_next, bi0, next0);
265 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
267 return from_frame->n_vectors;
271 format_mpls_label_disposition_trace (u8 * s, va_list * args)
273 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
274 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
275 CLIB_UNUSED (mpls_label_disposition_trace_t * t);
277 t = va_arg (*args, mpls_label_disposition_trace_t *);
279 s = format(s, "disp:%d", t->mdd);
284 ip4_mpls_label_disposition (vlib_main_t * vm,
285 vlib_node_runtime_t * node,
286 vlib_frame_t * frame)
288 return (mpls_label_disposition_inline(vm, node, frame, 1, 0));
291 VLIB_REGISTER_NODE (ip4_mpls_label_disposition_node) = {
292 .function = ip4_mpls_label_disposition,
293 .name = "ip4-mpls-label-disposition",
294 .vector_size = sizeof (u32),
296 .format_trace = format_mpls_label_disposition_trace,
302 VLIB_NODE_FUNCTION_MULTIARCH (ip4_mpls_label_disposition_node,
303 ip4_mpls_label_disposition)
306 ip6_mpls_label_disposition (vlib_main_t * vm,
307 vlib_node_runtime_t * node,
308 vlib_frame_t * frame)
310 return (mpls_label_disposition_inline(vm, node, frame, 0, 1));
313 VLIB_REGISTER_NODE (ip6_mpls_label_disposition_node) = {
314 .function = ip6_mpls_label_disposition,
315 .name = "ip6-mpls-label-disposition",
316 .vector_size = sizeof (u32),
318 .format_trace = format_mpls_label_disposition_trace,
324 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mpls_label_disposition_node,
325 ip6_mpls_label_disposition)
328 mpls_disp_dpo_mem_show (void)
330 fib_show_memory_usage("MPLS label",
331 pool_elts(mpls_disp_dpo_pool),
332 pool_len(mpls_disp_dpo_pool),
333 sizeof(mpls_disp_dpo_t));
336 const static dpo_vft_t mdd_vft = {
337 .dv_lock = mpls_disp_dpo_lock,
338 .dv_unlock = mpls_disp_dpo_unlock,
339 .dv_format = format_mpls_disp_dpo,
340 .dv_mem_show = mpls_disp_dpo_mem_show,
343 const static char* const mpls_label_disp_ip4_nodes[] =
345 "ip4-mpls-label-disposition",
348 const static char* const mpls_label_disp_ip6_nodes[] =
350 "ip6-mpls-label-disposition",
353 const static char* const * const mpls_label_disp_nodes[DPO_PROTO_NUM] =
355 [DPO_PROTO_IP4] = mpls_label_disp_ip4_nodes,
356 [DPO_PROTO_IP6] = mpls_label_disp_ip6_nodes,
361 mpls_disp_dpo_module_init (void)
363 dpo_register(DPO_MPLS_DISPOSITION, &mdd_vft, mpls_label_disp_nodes);