MPLS Unifom mode
[vpp.git] / src / vnet / dpo / mpls_disposition.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
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>
20
21 /*
22  * pool of all MPLS Label DPOs
23  */
24 mpls_disp_dpo_t *mpls_disp_dpo_pool;
25
26 static mpls_disp_dpo_t *
27 mpls_disp_dpo_alloc (void)
28 {
29     mpls_disp_dpo_t *mdd;
30
31     pool_get_aligned(mpls_disp_dpo_pool, mdd, CLIB_CACHE_LINE_BYTES);
32     memset(mdd, 0, sizeof(*mdd));
33
34     dpo_reset(&mdd->mdd_dpo);
35
36     return (mdd);
37 }
38
39 static index_t
40 mpls_disp_dpo_get_index (mpls_disp_dpo_t *mdd)
41 {
42     return (mdd - mpls_disp_dpo_pool);
43 }
44
45 void
46 mpls_disp_dpo_create (dpo_proto_t payload_proto,
47                       fib_rpf_id_t rpf_id,
48                       fib_mpls_lsp_mode_t mode,
49                       const dpo_id_t *parent,
50                       dpo_id_t *dpo)
51 {
52     mpls_disp_dpo_t *mdd;
53     dpo_type_t dtype;
54
55     mdd = mpls_disp_dpo_alloc();
56
57     mdd->mdd_payload_proto = payload_proto;
58     mdd->mdd_rpf_id = rpf_id;
59     mdd->mdd_mode = mode;
60     dtype = (FIB_MPLS_LSP_MODE_PIPE == mode ?
61              DPO_MPLS_DISPOSITION_PIPE :
62              DPO_MPLS_DISPOSITION_UNIFORM);
63
64     /*
65      * stack this disposition object on the parent given
66      */
67     dpo_stack(dtype,
68               mdd->mdd_payload_proto,
69               &mdd->mdd_dpo,
70               parent);
71
72     /*
73      * set up the return DPO to refer to this object
74      */
75     dpo_set(dpo,
76             dtype,
77             payload_proto,
78             mpls_disp_dpo_get_index(mdd));
79 }
80
81 u8*
82 format_mpls_disp_dpo (u8 *s, va_list *args)
83 {
84     index_t index = va_arg(*args, index_t);
85     u32 indent = va_arg(*args, u32);
86     mpls_disp_dpo_t *mdd;
87
88     mdd = mpls_disp_dpo_get(index);
89
90     s = format(s, "mpls-disposition:[%d]:[%U, %U]",
91                index,
92                format_dpo_proto, mdd->mdd_payload_proto,
93                format_fib_mpls_lsp_mode, mdd->mdd_mode);
94
95     s = format(s, "\n%U", format_white_space, indent);
96     s = format(s, "%U", format_dpo_id, &mdd->mdd_dpo, indent+2);
97
98     return (s);
99 }
100
101 static void
102 mpls_disp_dpo_lock (dpo_id_t *dpo)
103 {
104     mpls_disp_dpo_t *mdd;
105
106     mdd = mpls_disp_dpo_get(dpo->dpoi_index);
107
108     mdd->mdd_locks++;
109 }
110
111 static void
112 mpls_disp_dpo_unlock (dpo_id_t *dpo)
113 {
114     mpls_disp_dpo_t *mdd;
115
116     mdd = mpls_disp_dpo_get(dpo->dpoi_index);
117
118     mdd->mdd_locks--;
119
120     if (0 == mdd->mdd_locks)
121     {
122         dpo_reset(&mdd->mdd_dpo);
123         pool_put(mpls_disp_dpo_pool, mdd);
124     }
125 }
126
127 /**
128  * @brief A struct to hold tracing information for the MPLS label disposition
129  * node.
130  */
131 typedef struct mpls_label_disposition_trace_t_
132 {
133     index_t mdd;
134 } mpls_label_disposition_trace_t;
135
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;
140
141 always_inline uword
142 mpls_label_disposition_inline (vlib_main_t * vm,
143                                vlib_node_runtime_t * node,
144                                vlib_frame_t * from_frame,
145                                u8 payload_is_ip4,
146                                u8 payload_is_ip6,
147                                fib_mpls_lsp_mode_t mode)
148 {
149     u32 n_left_from, next_index, * from, * to_next;
150     vlib_node_runtime_t *error_node;
151
152     if (payload_is_ip4)
153     {
154         if (FIB_MPLS_LSP_MODE_PIPE == mode)
155             error_node =
156                 vlib_node_get_runtime(vm, ip4_mpls_label_disposition_pipe_node.index);
157         else
158             error_node =
159                 vlib_node_get_runtime(vm, ip4_mpls_label_disposition_uniform_node.index);
160     }
161     else
162     {
163         if (FIB_MPLS_LSP_MODE_PIPE == mode)
164             error_node =
165                 vlib_node_get_runtime(vm, ip6_mpls_label_disposition_uniform_node.index);
166         else
167             error_node =
168                 vlib_node_get_runtime(vm, ip6_mpls_label_disposition_uniform_node.index);
169     }
170     from = vlib_frame_vector_args(from_frame);
171     n_left_from = from_frame->n_vectors;
172
173     next_index = node->cached_next_index;
174
175     while (n_left_from > 0)
176     {
177         u32 n_left_to_next;
178
179         vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
180
181         while (n_left_from >= 4 && n_left_to_next >= 2)
182         {
183             mpls_disp_dpo_t *mdd0, *mdd1;
184             u32 bi0, mddi0, bi1, mddi1;
185             vlib_buffer_t * b0, *b1;
186             u32 next0, next1;
187
188             bi0 = to_next[0] = from[0];
189             bi1 = to_next[1] = from[1];
190
191             /* Prefetch next iteration. */
192             {
193                 vlib_buffer_t * p2, * p3;
194
195                 p2 = vlib_get_buffer(vm, from[2]);
196                 p3 = vlib_get_buffer(vm, from[3]);
197
198                 vlib_prefetch_buffer_header(p2, STORE);
199                 vlib_prefetch_buffer_header(p3, STORE);
200
201                 CLIB_PREFETCH(p2->data, sizeof(ip6_header_t), STORE);
202                 CLIB_PREFETCH(p3->data, sizeof(ip6_header_t), STORE);
203             }
204
205             from += 2;
206             to_next += 2;
207             n_left_from -= 2;
208             n_left_to_next -= 2;
209
210             b0 = vlib_get_buffer(vm, bi0);
211             b1 = vlib_get_buffer(vm, bi1);
212
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);
218
219             next0 = mdd0->mdd_dpo.dpoi_next_node;
220             next1 = mdd1->mdd_dpo.dpoi_next_node;
221
222             if (payload_is_ip4)
223             {
224                 ip4_header_t *ip0, *ip1;
225
226                 ip0 = vlib_buffer_get_current(b0);
227                 ip1 = vlib_buffer_get_current(b1);
228
229                 /*
230                  * IPv4 input checks on the exposed IP header
231                  * including checksum
232                  */
233                 ip4_input_check_x2(vm, error_node,
234                                    b0, b1, ip0, ip1,
235                                    &next0, &next1, 1);
236
237                 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
238                 {
239                     /*
240                      * Copy the TTL from the MPLS packet into the
241                      * exposed IP. recalc the chksum
242                      */
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);
247
248                     ip0->checksum = ip4_header_checksum(ip0);
249                     ip1->checksum = ip4_header_checksum(ip1);
250                 }
251             }
252             else if (payload_is_ip6)
253             {
254                 ip6_header_t *ip0, *ip1;
255
256                 ip0 = vlib_buffer_get_current(b0);
257                 ip1 = vlib_buffer_get_current(b1);
258
259                 /*
260                  * IPv6 input checks on the exposed IP header
261                  */
262                 ip6_input_check_x2(vm, error_node,
263                                    b0, b1, ip0, ip1,
264                                    &next0, &next1);
265
266                 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
267                 {
268                     /*
269                      * Copy the TTL from the MPLS packet into the
270                      * exposed IP
271                      */
272                     ip0->hop_limit = vnet_buffer(b0)->mpls.ttl;
273                     ip1->hop_limit = vnet_buffer(b1)->mpls.ttl;
274
275                     ip6_set_traffic_class_network_order(
276                         ip0,
277                         mpls_exp_to_ip_dscp(vnet_buffer(b0)->mpls.exp));
278                     ip6_set_traffic_class_network_order(
279                         ip1,
280                         mpls_exp_to_ip_dscp(vnet_buffer(b1)->mpls.exp));
281                 }
282             }
283
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;
288
289             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
290             {
291                 mpls_label_disposition_trace_t *tr =
292                     vlib_add_trace(vm, node, b0, sizeof(*tr));
293
294                 tr->mdd = mddi0;
295             }
296             if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
297             {
298                 mpls_label_disposition_trace_t *tr =
299                     vlib_add_trace(vm, node, b1, sizeof(*tr));
300                 tr->mdd = mddi1;
301             }
302
303             vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
304                                             n_left_to_next,
305                                             bi0, bi1, next0, next1);
306         }
307
308         while (n_left_from > 0 && n_left_to_next > 0)
309         {
310             mpls_disp_dpo_t *mdd0;
311             vlib_buffer_t * b0;
312             u32 bi0, mddi0;
313             u32 next0;
314
315             bi0 = from[0];
316             to_next[0] = bi0;
317             from += 1;
318             to_next += 1;
319             n_left_from -= 1;
320             n_left_to_next -= 1;
321
322             b0 = vlib_get_buffer(vm, bi0);
323
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;
328
329             if (payload_is_ip4)
330             {
331                 ip4_header_t *ip0;
332
333                 ip0 = vlib_buffer_get_current(b0);
334
335                 /*
336                  * IPv4 input checks on the exposed IP header
337                  * including checksum
338                  */
339                 ip4_input_check_x1(vm, error_node, b0, ip0, &next0, 1);
340
341                 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
342                 {
343                     /*
344                      * Copy the TTL from the MPLS packet into the
345                      * exposed IP. recalc the chksum
346                      */
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);
350                 }
351             }
352             else if (payload_is_ip6)
353             {
354                 ip6_header_t *ip0;
355
356                 ip0 = vlib_buffer_get_current(b0);
357
358                 /*
359                  * IPv6 input checks on the exposed IP header
360                  */
361                 ip6_input_check_x1(vm, error_node, b0, ip0, &next0);
362
363                 if (FIB_MPLS_LSP_MODE_UNIFORM == mode)
364                 {
365                     /*
366                      * Copy the TTL from the MPLS packet into the
367                      * exposed IP
368                      */
369                     ip0->hop_limit = vnet_buffer(b0)->mpls.ttl;
370
371                     ip6_set_traffic_class_network_order(
372                         ip0,
373                         mpls_exp_to_ip_dscp(vnet_buffer(b0)->mpls.exp));
374                 }
375             }
376
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;
379
380             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
381             {
382                 mpls_label_disposition_trace_t *tr =
383                     vlib_add_trace(vm, node, b0, sizeof(*tr));
384                 tr->mdd = mddi0;
385             }
386
387             vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
388                                             n_left_to_next, bi0, next0);
389         }
390         vlib_put_next_frame(vm, node, next_index, n_left_to_next);
391     }
392     return from_frame->n_vectors;
393 }
394
395 static u8 *
396 format_mpls_label_disposition_trace (u8 * s, va_list * args)
397 {
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);
401
402     t = va_arg(*args, mpls_label_disposition_trace_t *);
403
404     s = format(s, "disp:%d", t->mdd);
405     return (s);
406 }
407
408 static uword
409 ip4_mpls_label_disposition_pipe (vlib_main_t * vm,
410                                  vlib_node_runtime_t * node,
411                                  vlib_frame_t * frame)
412 {
413     return (mpls_label_disposition_inline(vm, node, frame, 1, 0,
414                                           FIB_MPLS_LSP_MODE_PIPE));
415 }
416
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),
421
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,
426 };
427 VLIB_NODE_FUNCTION_MULTIARCH(ip4_mpls_label_disposition_pipe_node,
428                               ip4_mpls_label_disposition_pipe)
429
430 static uword
431 ip6_mpls_label_disposition_pipe (vlib_main_t * vm,
432                                  vlib_node_runtime_t * node,
433                                  vlib_frame_t * frame)
434 {
435     return (mpls_label_disposition_inline(vm, node, frame, 0, 1,
436                                           FIB_MPLS_LSP_MODE_PIPE));
437 }
438
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),
443
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,
448 };
449 VLIB_NODE_FUNCTION_MULTIARCH(ip6_mpls_label_disposition_pipe_node,
450                              ip6_mpls_label_disposition_pipe)
451
452 static uword
453 ip4_mpls_label_disposition_uniform (vlib_main_t * vm,
454                                  vlib_node_runtime_t * node,
455                                  vlib_frame_t * frame)
456 {
457     return (mpls_label_disposition_inline(vm, node, frame, 1, 0,
458                                           FIB_MPLS_LSP_MODE_UNIFORM));
459 }
460
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),
465
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,
470 };
471 VLIB_NODE_FUNCTION_MULTIARCH(ip4_mpls_label_disposition_uniform_node,
472                              ip4_mpls_label_disposition_uniform)
473
474 static uword
475 ip6_mpls_label_disposition_uniform (vlib_main_t * vm,
476                                     vlib_node_runtime_t * node,
477                                     vlib_frame_t * frame)
478 {
479     return (mpls_label_disposition_inline(vm, node, frame, 0, 1,
480                                           FIB_MPLS_LSP_MODE_UNIFORM));
481 }
482
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),
487
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,
492 };
493 VLIB_NODE_FUNCTION_MULTIARCH(ip6_mpls_label_disposition_uniform_node,
494                              ip6_mpls_label_disposition_uniform)
495
496 static void
497 mpls_disp_dpo_mem_show (void)
498 {
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));
503 }
504
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,
510 };
511
512 const static char* const mpls_label_disp_pipe_ip4_nodes[] =
513 {
514     "ip4-mpls-label-disposition-pipe",
515     NULL,
516 };
517 const static char* const mpls_label_disp_pipe_ip6_nodes[] =
518 {
519     "ip6-mpls-label-disposition-pipe",
520     NULL,
521 };
522 const static char* const * const mpls_label_disp_pipe_nodes[DPO_PROTO_NUM] =
523 {
524     [DPO_PROTO_IP4]  = mpls_label_disp_pipe_ip4_nodes,
525     [DPO_PROTO_IP6]  = mpls_label_disp_pipe_ip6_nodes,
526 };
527
528 const static char* const mpls_label_disp_uniform_ip4_nodes[] =
529 {
530     "ip4-mpls-label-disposition-uniform",
531     NULL,
532 };
533 const static char* const mpls_label_disp_uniform_ip6_nodes[] =
534 {
535     "ip6-mpls-label-disposition-uniform",
536     NULL,
537 };
538 const static char* const * const mpls_label_disp_uniform_nodes[DPO_PROTO_NUM] =
539 {
540     [DPO_PROTO_IP4]  = mpls_label_disp_uniform_ip4_nodes,
541     [DPO_PROTO_IP6]  = mpls_label_disp_uniform_ip6_nodes,
542 };
543
544
545 void
546 mpls_disp_dpo_module_init(void)
547 {
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);
552 }