48c03be57fc5f4559ac97e76a469a10d44ddd2ee
[vpp.git] / vnet / vnet / dpo / mpls_label_dpo.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/ip.h>
17 #include <vnet/dpo/mpls_label_dpo.h>
18 #include <vnet/mpls/mpls.h>
19
20 /*
21  * pool of all MPLS Label DPOs
22  */
23 mpls_label_dpo_t *mpls_label_dpo_pool;
24
25 static mpls_label_dpo_t *
26 mpls_label_dpo_alloc (void)
27 {
28     mpls_label_dpo_t *mld;
29
30     pool_get_aligned(mpls_label_dpo_pool, mld, CLIB_CACHE_LINE_BYTES);
31     memset(mld, 0, sizeof(*mld));
32
33     dpo_reset(&mld->mld_dpo);
34
35     return (mld);
36 }
37
38 static index_t
39 mpls_label_dpo_get_index (mpls_label_dpo_t *mld)
40 {
41     return (mld - mpls_label_dpo_pool);
42 }
43
44 index_t
45 mpls_label_dpo_create (mpls_label_t label,
46                        mpls_eos_bit_t eos,
47                        u8 ttl,
48                        u8 exp,
49                        const dpo_id_t *dpo)
50 {
51     mpls_label_dpo_t *mld;
52
53     mld = mpls_label_dpo_alloc();
54
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);
59
60     /*
61      * get the header in network byte order since we will paint it
62      * on a packet in the data-plane
63      */
64     mld->mld_hdr.label_exp_s_ttl =
65         clib_host_to_net_u32(mld->mld_hdr.label_exp_s_ttl);
66
67     dpo_stack(DPO_MPLS_LABEL, DPO_PROTO_MPLS, &mld->mld_dpo, dpo);
68
69     return (mpls_label_dpo_get_index(mld));
70 }
71
72 u8*
73 format_mpls_label_dpo (u8 *s, va_list *args)
74 {
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;
79
80     mld = mpls_label_dpo_get(index);
81
82     hdr.label_exp_s_ttl =
83         clib_net_to_host_u32(mld->mld_hdr.label_exp_s_ttl);
84
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);
89
90     return (s);
91 }
92
93 static void
94 mpls_label_dpo_lock (dpo_id_t *dpo)
95 {
96     mpls_label_dpo_t *mld;
97
98     mld = mpls_label_dpo_get(dpo->dpoi_index);
99
100     mld->mld_locks++;
101 }
102
103 static void
104 mpls_label_dpo_unlock (dpo_id_t *dpo)
105 {
106     mpls_label_dpo_t *mld;
107
108     mld = mpls_label_dpo_get(dpo->dpoi_index);
109
110     mld->mld_locks--;
111
112     if (0 == mld->mld_locks)
113     {
114         dpo_reset(&mld->mld_dpo);
115         pool_put(mpls_label_dpo_pool, mld);
116     }
117 }
118
119 /**
120  * @brief A struct to hold tracing information for the MPLS label imposition
121  * node.
122  */
123 typedef struct mpls_label_imposition_trace_t_
124 {
125     /**
126      * The MPLS header imposed
127      */
128     mpls_unicast_header_t hdr;
129 } mpls_label_imposition_trace_t;
130
131 always_inline uword
132 mpls_label_imposition (vlib_main_t * vm,
133                        vlib_node_runtime_t * node,
134                        vlib_frame_t * from_frame)
135 {
136     u32 n_left_from, next_index, * from, * to_next;
137
138     from = vlib_frame_vector_args (from_frame);
139     n_left_from = from_frame->n_vectors;
140
141     next_index = node->cached_next_index;
142
143     while (n_left_from > 0)
144     {
145         u32 n_left_to_next;
146
147         vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
148
149         while (n_left_from > 0 && n_left_to_next > 0)
150         {
151             mpls_unicast_header_t *hdr0;
152             mpls_label_dpo_t *mld0;
153             vlib_buffer_t * b0;
154             u32 bi0, mldi0;
155             u32 next0;
156
157             bi0 = from[0];
158             to_next[0] = bi0;
159             from += 1;
160             to_next += 1;
161             n_left_from -= 1;
162             n_left_to_next -= 1;
163
164             b0 = vlib_get_buffer (vm, bi0);
165
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);
169
170             /* Paint the MPLS header */
171             vlib_buffer_advance(b0, -sizeof(*hdr0));
172             hdr0 = vlib_buffer_get_current(b0);
173
174             // FIXME.
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;
180
181             next0 = mld0->mld_dpo.dpoi_next_node;
182             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mld0->mld_dpo.dpoi_index;
183
184             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
185             {
186                 mpls_label_imposition_trace_t *tr =
187                     vlib_add_trace (vm, node, b0, sizeof (*tr));
188                 tr->hdr = *hdr0;
189             }
190
191             vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
192                                             n_left_to_next, bi0, next0);
193         }
194         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
195     }
196     return from_frame->n_vectors;
197 }
198
199 static u8 *
200 format_mpls_label_imposition_trace (u8 * s, va_list * args)
201 {
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;
206     uword indent;
207
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);
211
212     s = format (s, "%Umpls-header:%U",
213                 format_white_space, indent,
214                 format_mpls_header, hdr);
215     return (s);
216 }
217
218 VLIB_REGISTER_NODE (mpls_label_imposition_node) = {
219     .function = mpls_label_imposition,
220     .name = "mpls-label-imposition",
221     .vector_size = sizeof (u32),
222
223     .format_trace = format_mpls_label_imposition_trace,
224     .n_next_nodes = 1,
225     .next_nodes = {
226         [0] = "error-drop",
227     }
228 };
229 VLIB_NODE_FUNCTION_MULTIARCH (mpls_label_imposition_node, mpls_label_imposition)
230
231 static void
232 mpls_label_dpo_mem_show (void)
233 {
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));
238 }
239
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,
245 };
246
247 const static char* const mpls_label_imp_ip4_nodes[] =
248 {
249     "mpls-label-imposition",
250     NULL,
251 };
252 const static char* const mpls_label_imp_ip6_nodes[] =
253 {
254     "mpls-label-imposition",
255     NULL,
256 };
257 const static char* const mpls_label_imp_mpls_nodes[] =
258 {
259     "mpls-label-imposition",
260     NULL,
261 };
262 const static char* const * const mpls_label_imp_nodes[DPO_PROTO_NUM] =
263 {
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,
267 };
268
269
270 void
271 mpls_label_dpo_module_init (void)
272 {
273     dpo_register(DPO_MPLS_LABEL, &mld_vft, mpls_label_imp_nodes);
274 }