MPLS infrastructure improvments
[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_stack,
46                        mpls_eos_bit_t eos,
47                        u8 ttl,
48                        u8 exp,
49                        dpo_proto_t payload_proto,
50                        const dpo_id_t *dpo)
51 {
52     mpls_label_dpo_t *mld;
53     u32 ii;
54
55     mld = mpls_label_dpo_alloc();
56     mld->mld_n_labels = vec_len(label_stack);
57     mld->mld_payload_proto = payload_proto;
58
59     /*
60      * construct label rewrite headers for each value value passed.
61      * get the header in network byte order since we will paint it
62      * on a packet in the data-plane
63      */
64
65     for (ii = 0; ii < mld->mld_n_labels-1; ii++)
66     {
67         vnet_mpls_uc_set_label(&mld->mld_hdr[ii].label_exp_s_ttl, label_stack[ii]);
68         vnet_mpls_uc_set_ttl(&mld->mld_hdr[ii].label_exp_s_ttl, 255);
69         vnet_mpls_uc_set_exp(&mld->mld_hdr[ii].label_exp_s_ttl, 0);
70         vnet_mpls_uc_set_s(&mld->mld_hdr[ii].label_exp_s_ttl, MPLS_NON_EOS);
71         mld->mld_hdr[ii].label_exp_s_ttl =
72             clib_host_to_net_u32(mld->mld_hdr[ii].label_exp_s_ttl);
73     }
74
75     /*
76      * the inner most label
77      */
78     ii = mld->mld_n_labels-1;
79
80     vnet_mpls_uc_set_label(&mld->mld_hdr[ii].label_exp_s_ttl, label_stack[ii]);
81     vnet_mpls_uc_set_ttl(&mld->mld_hdr[ii].label_exp_s_ttl, ttl);
82     vnet_mpls_uc_set_exp(&mld->mld_hdr[ii].label_exp_s_ttl, exp);
83     vnet_mpls_uc_set_s(&mld->mld_hdr[ii].label_exp_s_ttl, eos);
84     mld->mld_hdr[ii].label_exp_s_ttl =
85         clib_host_to_net_u32(mld->mld_hdr[ii].label_exp_s_ttl);
86
87     /*
88      * stack this label objct on its parent.
89      */
90     dpo_stack(DPO_MPLS_LABEL,
91               mld->mld_payload_proto,
92               &mld->mld_dpo,
93               dpo);
94
95     return (mpls_label_dpo_get_index(mld));
96 }
97
98 u8*
99 format_mpls_label_dpo (u8 *s, va_list *args)
100 {
101     index_t index = va_arg (*args, index_t);
102     u32 indent = va_arg (*args, u32);
103     mpls_unicast_header_t hdr;
104     mpls_label_dpo_t *mld;
105     u32 ii;
106
107     mld = mpls_label_dpo_get(index);
108
109     s = format(s, "mpls-label:[%d]:", index);
110
111     for (ii = 0; ii < mld->mld_n_labels; ii++)
112     {
113         hdr.label_exp_s_ttl =
114             clib_net_to_host_u32(mld->mld_hdr[ii].label_exp_s_ttl);
115         s = format(s, "%U", format_mpls_header, hdr);
116     }
117
118     s = format(s, "\n%U", format_white_space, indent);
119     s = format(s, "%U", format_dpo_id, &mld->mld_dpo, indent+2);
120
121     return (s);
122 }
123
124 static void
125 mpls_label_dpo_lock (dpo_id_t *dpo)
126 {
127     mpls_label_dpo_t *mld;
128
129     mld = mpls_label_dpo_get(dpo->dpoi_index);
130
131     mld->mld_locks++;
132 }
133
134 static void
135 mpls_label_dpo_unlock (dpo_id_t *dpo)
136 {
137     mpls_label_dpo_t *mld;
138
139     mld = mpls_label_dpo_get(dpo->dpoi_index);
140
141     mld->mld_locks--;
142
143     if (0 == mld->mld_locks)
144     {
145         dpo_reset(&mld->mld_dpo);
146         pool_put(mpls_label_dpo_pool, mld);
147     }
148 }
149
150 /**
151  * @brief A struct to hold tracing information for the MPLS label imposition
152  * node.
153  */
154 typedef struct mpls_label_imposition_trace_t_
155 {
156     /**
157      * The MPLS header imposed
158      */
159     mpls_unicast_header_t hdr;
160 } mpls_label_imposition_trace_t;
161
162 always_inline uword
163 mpls_label_imposition_inline (vlib_main_t * vm,
164                               vlib_node_runtime_t * node,
165                               vlib_frame_t * from_frame,
166                               u8 payload_is_ip4,
167                               u8 payload_is_ip6)
168 {
169     u32 n_left_from, next_index, * from, * to_next;
170
171     from = vlib_frame_vector_args (from_frame);
172     n_left_from = from_frame->n_vectors;
173
174     next_index = node->cached_next_index;
175
176     while (n_left_from > 0)
177     {
178         u32 n_left_to_next;
179
180         vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
181
182         while (n_left_from > 0 && n_left_to_next > 0)
183         {
184             mpls_unicast_header_t *hdr0;
185             mpls_label_dpo_t *mld0;
186             vlib_buffer_t * b0;
187             u32 bi0, mldi0;
188             u32 next0;
189             u8 ttl;
190
191             bi0 = from[0];
192             to_next[0] = bi0;
193             from += 1;
194             to_next += 1;
195             n_left_from -= 1;
196             n_left_to_next -= 1;
197
198             b0 = vlib_get_buffer (vm, bi0);
199
200             /* dst lookup was done by ip4 lookup */
201             mldi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
202             mld0 = mpls_label_dpo_get(mldi0);
203
204             if (payload_is_ip4)
205             {
206                 /*
207                  * decrement the TTL on ingress to the LSP
208                  */
209                 ip4_header_t * ip0 = vlib_buffer_get_current(b0);
210                 u32 checksum0;
211
212                 checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100);
213                 checksum0 += checksum0 >= 0xffff;
214
215                 ip0->checksum = checksum0;
216                 ip0->ttl -= 1;
217                 ttl = ip0->ttl;
218             }
219             else if (payload_is_ip6)
220             {
221                 /*
222                  * decrement the TTL on ingress to the LSP
223                  */
224                 ip6_header_t * ip0 = vlib_buffer_get_current(b0);
225
226                 ip0->hop_limit -= 1;
227                 ttl = ip0->hop_limit;
228             }
229             else
230             {
231                 /*
232                  * else, the packet to be encapped is an MPLS packet
233                  */
234                 if (vnet_buffer(b0)->mpls.first)
235                 {
236                     /*
237                      * The first label to be imposed on the packet. this is a label swap.
238                      * in which case we stashed the TTL and EXP bits in the
239                      * packet in the lookup node
240                      */
241                     ASSERT(0 != vnet_buffer (b0)->mpls.ttl);
242
243                     ttl = vnet_buffer(b0)->mpls.ttl - 1;
244                 }
245                 else
246                 {
247                     /*
248                      * not the first label. implying we are recusring down a chain of
249                      * output labels.
250                      * Each layer is considered a new LSP - hence the TTL is reset.
251                      */
252                     ttl = 255;
253                 }
254             }
255             vnet_buffer(b0)->mpls.first = 0;
256
257             /* Paint the MPLS header */
258             vlib_buffer_advance(b0, -(sizeof(*hdr0) * mld0->mld_n_labels));
259             hdr0 = vlib_buffer_get_current(b0);
260
261             clib_memcpy(hdr0, mld0->mld_hdr,
262                         sizeof(*hdr0) * mld0->mld_n_labels);
263
264             /* fixup the TTL for the inner most label */
265             hdr0 = hdr0 + (mld0->mld_n_labels - 1);
266             ((char*)hdr0)[3] = ttl;
267
268             next0 = mld0->mld_dpo.dpoi_next_node;
269             vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mld0->mld_dpo.dpoi_index;
270
271             if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) 
272             {
273                 mpls_label_imposition_trace_t *tr =
274                     vlib_add_trace (vm, node, b0, sizeof (*tr));
275                 tr->hdr = *hdr0;
276             }
277
278             vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
279                                             n_left_to_next, bi0, next0);
280         }
281         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
282     }
283     return from_frame->n_vectors;
284 }
285
286 static u8 *
287 format_mpls_label_imposition_trace (u8 * s, va_list * args)
288 {
289     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
290     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
291     mpls_label_imposition_trace_t * t;
292     mpls_unicast_header_t hdr;
293     uword indent;
294
295     t = va_arg (*args, mpls_label_imposition_trace_t *);
296     indent = format_get_indent (s);
297     hdr.label_exp_s_ttl = clib_net_to_host_u32(t->hdr.label_exp_s_ttl);
298
299     s = format (s, "%Umpls-header:%U",
300                 format_white_space, indent,
301                 format_mpls_header, hdr);
302     return (s);
303 }
304
305 static uword
306 mpls_label_imposition (vlib_main_t * vm,
307                        vlib_node_runtime_t * node,
308                        vlib_frame_t * frame)
309 {
310     return (mpls_label_imposition_inline(vm, node, frame, 0, 0));
311 }
312
313 VLIB_REGISTER_NODE (mpls_label_imposition_node) = {
314     .function = mpls_label_imposition,
315     .name = "mpls-label-imposition",
316     .vector_size = sizeof (u32),
317
318     .format_trace = format_mpls_label_imposition_trace,
319     .n_next_nodes = 1,
320     .next_nodes = {
321         [0] = "error-drop",
322     }
323 };
324 VLIB_NODE_FUNCTION_MULTIARCH (mpls_label_imposition_node,
325                               mpls_label_imposition)
326
327 static uword
328 ip4_mpls_label_imposition (vlib_main_t * vm,
329                            vlib_node_runtime_t * node,
330                            vlib_frame_t * frame)
331 {
332     return (mpls_label_imposition_inline(vm, node, frame, 1, 0));
333 }
334
335 VLIB_REGISTER_NODE (ip4_mpls_label_imposition_node) = {
336     .function = ip4_mpls_label_imposition,
337     .name = "ip4-mpls-label-imposition",
338     .vector_size = sizeof (u32),
339
340     .format_trace = format_mpls_label_imposition_trace,
341     .n_next_nodes = 1,
342     .next_nodes = {
343         [0] = "error-drop",
344     }
345 };
346 VLIB_NODE_FUNCTION_MULTIARCH (ip4_mpls_label_imposition_node,
347                               ip4_mpls_label_imposition)
348
349 static uword
350 ip6_mpls_label_imposition (vlib_main_t * vm,
351                            vlib_node_runtime_t * node,
352                            vlib_frame_t * frame)
353 {
354     return (mpls_label_imposition_inline(vm, node, frame, 0, 1));
355 }
356
357 VLIB_REGISTER_NODE (ip6_mpls_label_imposition_node) = {
358     .function = ip6_mpls_label_imposition,
359     .name = "ip6-mpls-label-imposition",
360     .vector_size = sizeof (u32),
361
362     .format_trace = format_mpls_label_imposition_trace,
363     .n_next_nodes = 1,
364     .next_nodes = {
365         [0] = "error-drop",
366     }
367 };
368 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mpls_label_imposition_node,
369                               ip6_mpls_label_imposition)
370
371 static void
372 mpls_label_dpo_mem_show (void)
373 {
374     fib_show_memory_usage("MPLS label",
375                           pool_elts(mpls_label_dpo_pool),
376                           pool_len(mpls_label_dpo_pool),
377                           sizeof(mpls_label_dpo_t));
378 }
379
380 const static dpo_vft_t mld_vft = {
381     .dv_lock = mpls_label_dpo_lock,
382     .dv_unlock = mpls_label_dpo_unlock,
383     .dv_format = format_mpls_label_dpo,
384     .dv_mem_show = mpls_label_dpo_mem_show,
385 };
386
387 const static char* const mpls_label_imp_ip4_nodes[] =
388 {
389     "ip4-mpls-label-imposition",
390     NULL,
391 };
392 const static char* const mpls_label_imp_ip6_nodes[] =
393 {
394     "ip6-mpls-label-imposition",
395     NULL,
396 };
397 const static char* const mpls_label_imp_mpls_nodes[] =
398 {
399     "mpls-label-imposition",
400     NULL,
401 };
402 const static char* const * const mpls_label_imp_nodes[DPO_PROTO_NUM] =
403 {
404     [DPO_PROTO_IP4]  = mpls_label_imp_ip4_nodes,
405     [DPO_PROTO_IP6]  = mpls_label_imp_ip6_nodes,
406     [DPO_PROTO_MPLS] = mpls_label_imp_mpls_nodes,
407 };
408
409
410 void
411 mpls_label_dpo_module_init (void)
412 {
413     dpo_register(DPO_MPLS_LABEL, &mld_vft, mpls_label_imp_nodes);
414 }