A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / mpls / policy_encap.c
1 /*
2  * policy_encap.c: mpls-o-e policy encap
3  *
4  * Copyright (c) 2012-2014 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/mpls/mpls.h>
21
22 typedef struct {
23   u32 next_index;
24   u32 encap_index;
25 } mpls_policy_encap_trace_t;
26
27 u8 * format_mpls_policy_encap_trace (u8 * s, va_list * args)
28 {
29   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
30   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
31   mpls_policy_encap_trace_t * t = va_arg (*args, mpls_policy_encap_trace_t *);
32
33   s = format (s, "MPLS-POLICY-ENCAP: next-index %d encap-index %d",
34               t->next_index, t->encap_index);
35
36   return s;
37 }
38
39 vlib_node_registration_t mpls_policy_encap_node;
40
41 #define foreach_mpls_policy_encap_next          \
42 _(DROP, "error-drop")
43
44 typedef enum {
45 #define _(s,n) MPLS_POLICY_ENCAP_NEXT_##s,
46   foreach_mpls_policy_encap_next
47 #undef _
48   MPLS_POLICY_ENCAP_N_NEXT,
49 } mpls_policy_encap_next_t;
50
51 #define foreach_mpls_policy_error                               \
52 _(PKTS_ENCAP, "mpls policy tunnel packets encapsulated")
53
54 typedef enum {
55 #define _(n,s) MPLS_POLICY_ENCAP_ERROR_##n,
56   foreach_mpls_policy_error
57   MPLS_POLICY_ENCAP_N_ERROR,
58 #undef _
59 } mpls_policy_encap_error_t;
60
61 static char * mpls_policy_encap_error_strings[] =
62   {
63 #define _(n,s) s,
64     foreach_mpls_policy_error
65 #undef _
66 };
67     
68 static uword
69 mpls_policy_encap (vlib_main_t * vm,
70                    vlib_node_runtime_t * node,
71                    vlib_frame_t * from_frame)
72 {
73   u32 n_left_from, next_index, * from, * to_next;
74   mpls_main_t * mm = &mpls_main;
75   
76   from = vlib_frame_vector_args (from_frame);
77   n_left_from = from_frame->n_vectors;
78   
79   next_index = node->cached_next_index;
80   
81   while (n_left_from > 0)
82     {
83       u32 n_left_to_next;
84
85       vlib_get_next_frame (vm, node, next_index,
86                            to_next, n_left_to_next);
87
88       while (n_left_from > 0 && n_left_to_next > 0)
89         {
90           u32 bi0;
91           vlib_buffer_t * b0;
92           u8 * h0;
93           u32 encap_index0;
94           u32 next0;
95           mpls_encap_t * e0;
96
97           bi0 = from[0];
98           to_next[0] = bi0;
99           from += 1;
100           to_next += 1;
101           n_left_from -= 1;
102           n_left_to_next -= 1;
103
104           b0 = vlib_get_buffer (vm, bi0);
105
106           encap_index0 = vnet_buffer(b0)->l2_classify.opaque_index;
107
108           e0 = pool_elt_at_index (mm->encaps, encap_index0);
109
110           vlib_buffer_advance (b0, -(word)vec_len(e0->rewrite));
111           h0 = vlib_buffer_get_current (b0);
112           clib_memcpy (h0, e0->rewrite, vec_len(e0->rewrite));
113
114           next0 = e0->output_next_index;
115
116           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
117             {
118               mpls_policy_encap_trace_t *tr = 
119                 vlib_add_trace (vm, node, b0, sizeof (*tr));
120               tr->next_index = next0;
121               tr->encap_index = encap_index0;
122             }
123           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
124                                            to_next, n_left_to_next,
125                                            bi0, next0);
126         }
127       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
128     }
129   vlib_node_increment_counter (vm, mpls_policy_encap_node.index,
130                                MPLS_POLICY_ENCAP_ERROR_PKTS_ENCAP, 
131                                from_frame->n_vectors);
132   return from_frame->n_vectors;
133 }
134
135 VLIB_REGISTER_NODE (mpls_policy_encap_node) =  {
136   .function = mpls_policy_encap,
137   .name = "mpls-policy-encap",
138   /* Takes a vector of packets. */
139   .vector_size = sizeof (u32),
140   
141   .runtime_data_bytes = 0,
142   
143   .n_errors = MPLS_POLICY_ENCAP_N_ERROR,
144   .error_strings = mpls_policy_encap_error_strings,
145   
146   .format_trace = format_mpls_policy_encap_trace,
147   
148   .n_next_nodes = MPLS_POLICY_ENCAP_N_NEXT,
149   .next_nodes = {
150 #define _(s,n) [MPLS_POLICY_ENCAP_NEXT_##s] = n,
151     foreach_mpls_policy_encap_next
152 #undef _
153   },
154 };
155
156 VLIB_NODE_FUNCTION_MULTIARCH (mpls_policy_encap_node, mpls_policy_encap)
157
158 static clib_error_t *
159 mpls_policy_encap_init (vlib_main_t * vm)
160 {
161   mpls_main_t * mm = &mpls_main;
162   clib_error_t * error;
163
164   if ((error = vlib_call_init_function (vm, mpls_init)))
165     return error;
166   
167   mm->ip4_classify_mpls_policy_encap_next_index =
168     vlib_node_add_next (mm->vlib_main,
169                         ip4_classify_node.index, 
170                         mpls_policy_encap_node.index);
171
172   mm->ip6_classify_mpls_policy_encap_next_index =
173     vlib_node_add_next (mm->vlib_main,
174                         ip6_classify_node.index, 
175                         mpls_policy_encap_node.index);
176
177   return 0;
178 }
179
180 VLIB_INIT_FUNCTION (mpls_policy_encap_init);