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/ip4_input.h>
17 #include <vnet/ip/ip6_input.h>
18 #include <vnet/dpo/mpls_disposition.h>
19 #include <vnet/mpls/mpls.h>
22 * pool of all MPLS Label DPOs
24 mpls_disp_dpo_t *mpls_disp_dpo_pool;
26 static mpls_disp_dpo_t *
27 mpls_disp_dpo_alloc (void)
31 pool_get_aligned(mpls_disp_dpo_pool, mdd, CLIB_CACHE_LINE_BYTES);
32 memset(mdd, 0, sizeof(*mdd));
34 dpo_reset(&mdd->mdd_dpo);
40 mpls_disp_dpo_get_index (mpls_disp_dpo_t *mdd)
42 return (mdd - mpls_disp_dpo_pool);
46 mpls_disp_dpo_create (dpo_proto_t payload_proto,
48 fib_mpls_lsp_mode_t mode,
49 const dpo_id_t *parent,
55 mdd = mpls_disp_dpo_alloc();
57 mdd->mdd_payload_proto = payload_proto;
58 mdd->mdd_rpf_id = rpf_id;
60 dtype = (FIB_MPLS_LSP_MODE_PIPE == mode ?
61 DPO_MPLS_DISPOSITION_PIPE :
62 DPO_MPLS_DISPOSITION_UNIFORM);
65 * stack this disposition object on the parent given
68 mdd->mdd_payload_proto,
73 * set up the return DPO to refer to this object
78 mpls_disp_dpo_get_index(mdd));
82 format_mpls_disp_dpo (u8 *s, va_list *args)
84 index_t index = va_arg(*args, index_t);
85 u32 indent = va_arg(*args, u32);
88 mdd = mpls_disp_dpo_get(index);
90 s = format(s, "mpls-disposition:[%d]:[%U, %U]",
92 format_dpo_proto, mdd->mdd_payload_proto,
93 format_fib_mpls_lsp_mode, mdd->mdd_mode);
95 s = format(s, "\n%U", format_white_space, indent);
96 s = format(s, "%U", format_dpo_id, &mdd->mdd_dpo, indent+2);
102 mpls_disp_dpo_lock (dpo_id_t *dpo)
104 mpls_disp_dpo_t *mdd;
106 mdd = mpls_disp_dpo_get(dpo->dpoi_index);
112 mpls_disp_dpo_unlock (dpo_id_t *dpo)
114 mpls_disp_dpo_t *mdd;
116 mdd = mpls_disp_dpo_get(dpo->dpoi_index);
120 if (0 == mdd->mdd_locks)
122 dpo_reset(&mdd->mdd_dpo);
123 pool_put(mpls_disp_dpo_pool, mdd);
128 * @brief A struct to hold tracing information for the MPLS label disposition
131 typedef struct mpls_label_disposition_trace_t_
134 } mpls_label_disposition_trace_t;
136 extern vlib_node_registration_t ip4_mpls_label_disposition_pipe_node;
137 extern vlib_node_registration_t ip6_mpls_label_disposition_pipe_node;
138 extern vlib_node_registration_t ip4_mpls_label_disposition_uniform_node;
139 extern vlib_node_registration_t ip6_mpls_label_disposition_uniform_node;
142 mpls_label_disposition_inline (vlib_main_t * vm,
143 vlib_node_runtime_t * node,
144 vlib_frame_t * from_frame,
147 fib_mpls_lsp_mode_t mode)
149 u32 n_left_from, next_index, * from, * to_next;
150 vlib_node_runtime_t *error_node;
154 if (FIB_MPLS_LSP_MODE_PIPE == mode)
156 vlib_node_get_runtime(vm, ip4_mpls_label_disposition_pipe_node.index);
159 vlib_node_get_runtime(vm, ip4_mpls_label_disposition_uniform_node.index);
163 if (FIB_MPLS_LSP_MODE_PIPE == mode)
165 vlib_node_get_runtime(vm, ip6_mpls_label_disposition_pipe_node.index);
168 vlib_node_get_runtime(vm, ip6_mpls_label_disposition_uniform_node.index);
170 from = vlib_frame_vector_args(from_frame);
171 n_left_from = from_frame->n_vectors;
173 next_index = node->cached_next_index;
175 while (n_left_from > 0)
179 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
181 while (n_left_from >= 4 && n_left_to_next >= 2)
183 mpls_disp_dpo_t *mdd0, *mdd1;
184 u32 bi0, mddi0, bi1, mddi1;
185 vlib_buffer_t * b0, *b1;
188 bi0 = to_next[0] = from[0];
189 bi1 = to_next[1] = from[1];
191 /* Prefetch next iteration. */
193 vlib_buffer_t * p2, * p3;
195 p2 = vlib_get_buffer(vm, from[2]);
196 p3 = vlib_get_buffer(vm, from[3]);
198 vlib_prefetch_buffer_header(p2, STORE);
199 vlib_prefetch_buffer_header(p3, STORE);
201 CLIB_PREFETCH(p2->data, sizeof(ip6_header_t), STORE);
202 CLIB_PREFETCH(p3->data, sizeof(ip6_header_t), STORE);
210 b0 = vlib_get_buffer(vm, bi0);
211 b1 = vlib_get_buffer(vm, bi1);
213 /* dst lookup was done by ip4 lookup */
214 mddi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
215 mddi1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
216 mdd0 = mpls_disp_dpo_get(mddi0);
217 mdd1 = mpls_disp_dpo_get(mddi1);
219 next0 = mdd0->mdd_dpo.dpoi_next_node;
220 next1 = mdd1->mdd_dpo.dpoi_next_node;
224 ip4_header_t *ip0, *ip1;
226 ip0 = vlib_buffer_get_current(b0);
227 ip1 = vlib_buffer_get_current(b1);
230 * IPv4 input checks on the exposed IP header
233 ip4_input_check_x2(vm, error_node,
237 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
240 * Copy the TTL from the MPLS packet into the
241 * exposed IP. recalc the chksum
243 ip0->ttl = vnet_buffer(b0)->mpls.ttl;
244 ip1->ttl = vnet_buffer(b1)->mpls.ttl;
245 ip0->tos = mpls_exp_to_ip_dscp(vnet_buffer(b0)->mpls.exp);
246 ip1->tos = mpls_exp_to_ip_dscp(vnet_buffer(b1)->mpls.exp);
248 ip0->checksum = ip4_header_checksum(ip0);
249 ip1->checksum = ip4_header_checksum(ip1);
252 else if (payload_is_ip6)
254 ip6_header_t *ip0, *ip1;
256 ip0 = vlib_buffer_get_current(b0);
257 ip1 = vlib_buffer_get_current(b1);
260 * IPv6 input checks on the exposed IP header
262 ip6_input_check_x2(vm, error_node,
266 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
269 * Copy the TTL from the MPLS packet into the
272 ip0->hop_limit = vnet_buffer(b0)->mpls.ttl;
273 ip1->hop_limit = vnet_buffer(b1)->mpls.ttl;
275 ip6_set_traffic_class_network_order(
277 mpls_exp_to_ip_dscp(vnet_buffer(b0)->mpls.exp));
278 ip6_set_traffic_class_network_order(
280 mpls_exp_to_ip_dscp(vnet_buffer(b1)->mpls.exp));
284 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mdd0->mdd_dpo.dpoi_index;
285 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = mdd1->mdd_dpo.dpoi_index;
286 vnet_buffer(b0)->ip.rpf_id = mdd0->mdd_rpf_id;
287 vnet_buffer(b1)->ip.rpf_id = mdd1->mdd_rpf_id;
289 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
291 mpls_label_disposition_trace_t *tr =
292 vlib_add_trace(vm, node, b0, sizeof(*tr));
296 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
298 mpls_label_disposition_trace_t *tr =
299 vlib_add_trace(vm, node, b1, sizeof(*tr));
303 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
305 bi0, bi1, next0, next1);
308 while (n_left_from > 0 && n_left_to_next > 0)
310 mpls_disp_dpo_t *mdd0;
322 b0 = vlib_get_buffer(vm, bi0);
324 /* dst lookup was done by ip4 lookup */
325 mddi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
326 mdd0 = mpls_disp_dpo_get(mddi0);
327 next0 = mdd0->mdd_dpo.dpoi_next_node;
333 ip0 = vlib_buffer_get_current(b0);
336 * IPv4 input checks on the exposed IP header
339 ip4_input_check_x1(vm, error_node, b0, ip0, &next0, 1);
341 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
344 * Copy the TTL from the MPLS packet into the
345 * exposed IP. recalc the chksum
347 ip0->ttl = vnet_buffer(b0)->mpls.ttl;
348 ip0->tos = mpls_exp_to_ip_dscp(vnet_buffer(b0)->mpls.exp);
349 ip0->checksum = ip4_header_checksum(ip0);
352 else if (payload_is_ip6)
356 ip0 = vlib_buffer_get_current(b0);
359 * IPv6 input checks on the exposed IP header
361 ip6_input_check_x1(vm, error_node, b0, ip0, &next0);
363 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
366 * Copy the TTL from the MPLS packet into the
369 ip0->hop_limit = vnet_buffer(b0)->mpls.ttl;
371 ip6_set_traffic_class_network_order(
373 mpls_exp_to_ip_dscp(vnet_buffer(b0)->mpls.exp));
377 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mdd0->mdd_dpo.dpoi_index;
378 vnet_buffer(b0)->ip.rpf_id = mdd0->mdd_rpf_id;
380 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
382 mpls_label_disposition_trace_t *tr =
383 vlib_add_trace(vm, node, b0, sizeof(*tr));
387 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
388 n_left_to_next, bi0, next0);
390 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
392 return from_frame->n_vectors;
396 format_mpls_label_disposition_trace (u8 * s, va_list * args)
398 CLIB_UNUSED(vlib_main_t * vm) = va_arg(*args, vlib_main_t *);
399 CLIB_UNUSED(vlib_node_t * node) = va_arg(*args, vlib_node_t *);
400 CLIB_UNUSED(mpls_label_disposition_trace_t * t);
402 t = va_arg(*args, mpls_label_disposition_trace_t *);
404 s = format(s, "disp:%d", t->mdd);
409 ip4_mpls_label_disposition_pipe (vlib_main_t * vm,
410 vlib_node_runtime_t * node,
411 vlib_frame_t * frame)
413 return (mpls_label_disposition_inline(vm, node, frame, 1, 0,
414 FIB_MPLS_LSP_MODE_PIPE));
417 VLIB_REGISTER_NODE(ip4_mpls_label_disposition_pipe_node) = {
418 .function = ip4_mpls_label_disposition_pipe,
419 .name = "ip4-mpls-label-disposition-pipe",
420 .vector_size = sizeof(u32),
422 .format_trace = format_mpls_label_disposition_trace,
423 .sibling_of = "ip4-input",
424 .n_errors = IP4_N_ERROR,
425 .error_strings = ip4_error_strings,
427 VLIB_NODE_FUNCTION_MULTIARCH(ip4_mpls_label_disposition_pipe_node,
428 ip4_mpls_label_disposition_pipe)
431 ip6_mpls_label_disposition_pipe (vlib_main_t * vm,
432 vlib_node_runtime_t * node,
433 vlib_frame_t * frame)
435 return (mpls_label_disposition_inline(vm, node, frame, 0, 1,
436 FIB_MPLS_LSP_MODE_PIPE));
439 VLIB_REGISTER_NODE(ip6_mpls_label_disposition_pipe_node) = {
440 .function = ip6_mpls_label_disposition_pipe,
441 .name = "ip6-mpls-label-disposition-pipe",
442 .vector_size = sizeof(u32),
444 .format_trace = format_mpls_label_disposition_trace,
445 .sibling_of = "ip6-input",
446 .n_errors = IP6_N_ERROR,
447 .error_strings = ip6_error_strings,
449 VLIB_NODE_FUNCTION_MULTIARCH(ip6_mpls_label_disposition_pipe_node,
450 ip6_mpls_label_disposition_pipe)
453 ip4_mpls_label_disposition_uniform (vlib_main_t * vm,
454 vlib_node_runtime_t * node,
455 vlib_frame_t * frame)
457 return (mpls_label_disposition_inline(vm, node, frame, 1, 0,
458 FIB_MPLS_LSP_MODE_UNIFORM));
461 VLIB_REGISTER_NODE(ip4_mpls_label_disposition_uniform_node) = {
462 .function = ip4_mpls_label_disposition_uniform,
463 .name = "ip4-mpls-label-disposition-uniform",
464 .vector_size = sizeof(u32),
466 .format_trace = format_mpls_label_disposition_trace,
467 .sibling_of = "ip4-input",
468 .n_errors = IP4_N_ERROR,
469 .error_strings = ip4_error_strings,
471 VLIB_NODE_FUNCTION_MULTIARCH(ip4_mpls_label_disposition_uniform_node,
472 ip4_mpls_label_disposition_uniform)
475 ip6_mpls_label_disposition_uniform (vlib_main_t * vm,
476 vlib_node_runtime_t * node,
477 vlib_frame_t * frame)
479 return (mpls_label_disposition_inline(vm, node, frame, 0, 1,
480 FIB_MPLS_LSP_MODE_UNIFORM));
483 VLIB_REGISTER_NODE(ip6_mpls_label_disposition_uniform_node) = {
484 .function = ip6_mpls_label_disposition_uniform,
485 .name = "ip6-mpls-label-disposition-uniform",
486 .vector_size = sizeof(u32),
488 .format_trace = format_mpls_label_disposition_trace,
489 .sibling_of = "ip6-input",
490 .n_errors = IP6_N_ERROR,
491 .error_strings = ip6_error_strings,
493 VLIB_NODE_FUNCTION_MULTIARCH(ip6_mpls_label_disposition_uniform_node,
494 ip6_mpls_label_disposition_uniform)
497 mpls_disp_dpo_mem_show (void)
499 fib_show_memory_usage("MPLS label",
500 pool_elts(mpls_disp_dpo_pool),
501 pool_len(mpls_disp_dpo_pool),
502 sizeof(mpls_disp_dpo_t));
505 const static dpo_vft_t mdd_vft = {
506 .dv_lock = mpls_disp_dpo_lock,
507 .dv_unlock = mpls_disp_dpo_unlock,
508 .dv_format = format_mpls_disp_dpo,
509 .dv_mem_show = mpls_disp_dpo_mem_show,
512 const static char* const mpls_label_disp_pipe_ip4_nodes[] =
514 "ip4-mpls-label-disposition-pipe",
517 const static char* const mpls_label_disp_pipe_ip6_nodes[] =
519 "ip6-mpls-label-disposition-pipe",
522 const static char* const * const mpls_label_disp_pipe_nodes[DPO_PROTO_NUM] =
524 [DPO_PROTO_IP4] = mpls_label_disp_pipe_ip4_nodes,
525 [DPO_PROTO_IP6] = mpls_label_disp_pipe_ip6_nodes,
528 const static char* const mpls_label_disp_uniform_ip4_nodes[] =
530 "ip4-mpls-label-disposition-uniform",
533 const static char* const mpls_label_disp_uniform_ip6_nodes[] =
535 "ip6-mpls-label-disposition-uniform",
538 const static char* const * const mpls_label_disp_uniform_nodes[DPO_PROTO_NUM] =
540 [DPO_PROTO_IP4] = mpls_label_disp_uniform_ip4_nodes,
541 [DPO_PROTO_IP6] = mpls_label_disp_uniform_ip6_nodes,
546 mpls_disp_dpo_module_init(void)
548 dpo_register(DPO_MPLS_DISPOSITION_PIPE, &mdd_vft,
549 mpls_label_disp_pipe_nodes);
550 dpo_register(DPO_MPLS_DISPOSITION_UNIFORM, &mdd_vft,
551 mpls_label_disp_uniform_nodes);