ikev2: add support for custom ipsec-over-udp port
[vpp.git] / src / plugins / nsh / nsh_pop.c
1 /*
2  * nsh_pop.c - nsh POP only processing
3  *
4  * Copyright (c) 2017 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 <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <nsh/nsh.h>
21 #include <vnet/gre/gre.h>
22 #include <vnet/vxlan/vxlan.h>
23 #include <vnet/vxlan-gpe/vxlan_gpe.h>
24 #include <vnet/l2/l2_classify.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 extern nsh_option_map_t * nsh_md2_lookup_option (u16 class, u8 type);
30
31 extern u8 * format_nsh_header (u8 * s, va_list * args);
32 extern u8 * format_nsh_node_map_trace (u8 * s, va_list * args);
33 extern u8 * format_nsh_pop_header (u8 * s, va_list * args);
34 extern u8 * format_nsh_pop_node_map_trace (u8 * s, va_list * args);
35
36 static uword
37 nsh_pop_inline (vlib_main_t * vm,
38                vlib_node_runtime_t * node,
39                vlib_frame_t * from_frame)
40 {
41   u32 n_left_from, next_index, *from, *to_next;
42   nsh_main_t * nm = &nsh_main;
43
44   from = vlib_frame_vector_args(from_frame);
45   n_left_from = from_frame->n_vectors;
46
47   next_index = node->cached_next_index;
48
49   while (n_left_from > 0)
50     {
51       u32 n_left_to_next;
52
53       vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
54
55       while (n_left_from >= 4 && n_left_to_next >= 2)
56         {
57           u32 bi0, bi1;
58           vlib_buffer_t * b0, *b1;
59           u32 next0 = NSH_NODE_NEXT_DROP, next1 = NSH_NODE_NEXT_DROP;
60           uword * entry0, *entry1;
61           nsh_base_header_t * hdr0 = 0, *hdr1 = 0;
62           u32 header_len0 = 0, header_len1 = 0;
63           u32 nsp_nsi0, nsp_nsi1;
64           u32 error0, error1;
65           nsh_map_t * map0 = 0, *map1 = 0;
66
67           /* Prefetch next iteration. */
68           {
69             vlib_buffer_t * p2, *p3;
70
71             p2 = vlib_get_buffer(vm, from[2]);
72             p3 = vlib_get_buffer(vm, from[3]);
73
74             vlib_prefetch_buffer_header(p2, LOAD);
75             vlib_prefetch_buffer_header(p3, LOAD);
76
77             CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
78             CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
79           }
80
81           bi0 = from[0];
82           bi1 = from[1];
83           to_next[0] = bi0;
84           to_next[1] = bi1;
85           from += 2;
86           to_next += 2;
87           n_left_from -= 2;
88           n_left_to_next -= 2;
89
90           error0 = 0;
91           error1 = 0;
92
93           b0 = vlib_get_buffer(vm, bi0);
94           b1 = vlib_get_buffer(vm, bi1);
95           hdr0 = vlib_buffer_get_current(b0);
96           nsp_nsi0 = hdr0->nsp_nsi;
97           header_len0 = hdr0->length * 4;
98
99           hdr1 = vlib_buffer_get_current(b1);
100           nsp_nsi1 = hdr1->nsp_nsi;
101           header_len1 = hdr1->length * 4;
102
103           /* Process packet 0 */
104           entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
105           if (PREDICT_FALSE(entry0 == 0))
106             {
107               error0 = NSH_NODE_ERROR_NO_MAPPING;
108               goto trace0;
109             }
110
111           /* Entry should point to a mapping ...*/
112           map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
113           if (PREDICT_FALSE(map0 == 0))
114             {
115               error0 = NSH_NODE_ERROR_NO_MAPPING;
116               goto trace0;
117             }
118
119           /* set up things for next node to transmit ie which node to handle it and where */
120           next0 = map0->next_node;
121           //vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
122
123           if(PREDICT_FALSE(map0->nsh_action == NSH_ACTION_POP))
124             {
125               /* Manipulate MD2 */
126               if(PREDICT_FALSE(hdr0->md_type == 2))
127                 {
128                   if (PREDICT_FALSE(next0 == NSH_NODE_NEXT_DROP))
129                     {
130                       error0 = NSH_NODE_ERROR_INVALID_OPTIONS;
131                       goto trace0;
132                     }
133                   //vnet_buffer(b0)->sw_if_index[VLIB_RX] = map0->sw_if_index;
134                 }
135
136               /* Pop NSH header */
137               vlib_buffer_advance(b0, (word)header_len0);
138               goto trace0;
139             }
140
141           entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
142           if (PREDICT_FALSE(entry0 == 0))
143             {
144               error0 = NSH_NODE_ERROR_NO_ENTRY;
145               goto trace0;
146             }
147
148         trace0: b0->error = error0 ? node->errors[error0] : 0;
149
150           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
151             {
152               nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
153               clib_memcpy_fast ( &(tr->trace_data), hdr0, (hdr0->length*4) );
154             }
155
156           /* Process packet 1 */
157           entry1 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi1);
158           if (PREDICT_FALSE(entry1 == 0))
159             {
160               error1 = NSH_NODE_ERROR_NO_MAPPING;
161               goto trace1;
162             }
163
164           /* Entry should point to a mapping ...*/
165           map1 = pool_elt_at_index(nm->nsh_mappings, entry1[0]);
166           if (PREDICT_FALSE(map1 == 0))
167             {
168               error1 = NSH_NODE_ERROR_NO_MAPPING;
169               goto trace1;
170             }
171
172           /* set up things for next node to transmit ie which node to handle it and where */
173           next1 = map1->next_node;
174           //vnet_buffer(b1)->sw_if_index[VLIB_TX] = map1->sw_if_index;
175
176           if(PREDICT_FALSE(map1->nsh_action == NSH_ACTION_POP))
177             {
178               /* Manipulate MD2 */
179               if(PREDICT_FALSE(hdr1->md_type == 2))
180                 {
181                   if (PREDICT_FALSE(next1 == NSH_NODE_NEXT_DROP))
182                     {
183                       error1 = NSH_NODE_ERROR_INVALID_OPTIONS;
184                       goto trace1;
185                     }
186                   //vnet_buffer(b1)->sw_if_index[VLIB_RX] = map1->sw_if_index;
187                 }
188
189               /* Pop NSH header */
190               vlib_buffer_advance(b1, (word)header_len1);
191               goto trace1;
192             }
193
194           entry1 = hash_get_mem(nm->nsh_entry_by_key, &map1->mapped_nsp_nsi);
195           if (PREDICT_FALSE(entry1 == 0))
196             {
197               error1 = NSH_NODE_ERROR_NO_ENTRY;
198               goto trace1;
199             }
200
201
202         trace1: b1->error = error1 ? node->errors[error1] : 0;
203
204           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
205             {
206               nsh_input_trace_t *tr = vlib_add_trace(vm, node, b1, sizeof(*tr));
207               clib_memcpy_fast ( &(tr->trace_data), hdr1, (hdr1->length*4) );
208             }
209
210           vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
211                                           n_left_to_next, bi0, bi1, next0, next1);
212
213         }
214
215       while (n_left_from > 0 && n_left_to_next > 0)
216         {
217           u32 bi0 = 0;
218           vlib_buffer_t * b0 = NULL;
219           u32 next0 = NSH_NODE_NEXT_DROP;
220           uword * entry0;
221           nsh_base_header_t * hdr0 = 0;
222           u32 header_len0 = 0;
223           u32 nsp_nsi0;
224           u32 error0;
225           nsh_map_t * map0 = 0;
226
227           bi0 = from[0];
228           to_next[0] = bi0;
229           from += 1;
230           to_next += 1;
231           n_left_from -= 1;
232           n_left_to_next -= 1;
233           error0 = 0;
234
235           b0 = vlib_get_buffer(vm, bi0);
236           hdr0 = vlib_buffer_get_current(b0);
237
238           nsp_nsi0 = hdr0->nsp_nsi;
239           header_len0 = hdr0->length * 4;
240
241           entry0 = hash_get_mem(nm->nsh_mapping_by_key, &nsp_nsi0);
242
243           if (PREDICT_FALSE(entry0 == 0))
244             {
245               error0 = NSH_NODE_ERROR_NO_MAPPING;
246               goto trace00;
247             }
248
249           /* Entry should point to a mapping ...*/
250           map0 = pool_elt_at_index(nm->nsh_mappings, entry0[0]);
251
252           if (PREDICT_FALSE(map0 == 0))
253             {
254               error0 = NSH_NODE_ERROR_NO_MAPPING;
255               goto trace00;
256             }
257
258           /* set up things for next node to transmit ie which node to handle it and where */
259           next0 = map0->next_node;
260           //vnet_buffer(b0)->sw_if_index[VLIB_TX] = map0->sw_if_index;
261
262           if(PREDICT_FALSE(map0->nsh_action == NSH_ACTION_POP))
263             {
264               /* Manipulate MD2 */
265               if(PREDICT_FALSE(hdr0->md_type == 2))
266                 {
267                   if (PREDICT_FALSE(next0 == NSH_NODE_NEXT_DROP))
268                     {
269                       error0 = NSH_NODE_ERROR_INVALID_OPTIONS;
270                       goto trace00;
271                     }
272                   //vnet_buffer(b0)->sw_if_index[VLIB_RX] = map0->sw_if_index;
273                 }
274
275               /* Pop NSH header */
276               vlib_buffer_advance(b0, (word)header_len0);
277               goto trace00;
278             }
279
280           entry0 = hash_get_mem(nm->nsh_entry_by_key, &map0->mapped_nsp_nsi);
281           if (PREDICT_FALSE(entry0 == 0))
282             {
283               error0 = NSH_NODE_ERROR_NO_ENTRY;
284               goto trace00;
285             }
286
287           trace00: b0->error = error0 ? node->errors[error0] : 0;
288
289           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
290             {
291               nsh_input_trace_t *tr = vlib_add_trace(vm, node, b0, sizeof(*tr));
292               clib_memcpy_fast ( &(tr->trace_data[0]), hdr0, (hdr0->length*4) );
293             }
294
295           vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
296                                           n_left_to_next, bi0, next0);
297         }
298
299       vlib_put_next_frame(vm, node, next_index, n_left_to_next);
300
301     }
302
303   return from_frame->n_vectors;
304 }
305
306 /**
307  * @brief Graph processing dispatch function for NSH Input
308  *
309  * @node nsh_input
310  * @param *vm
311  * @param *node
312  * @param *from_frame
313  *
314  * @return from_frame->n_vectors
315  *
316  */
317 VLIB_NODE_FN (nsh_pop_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
318                   vlib_frame_t * from_frame)
319 {
320   return nsh_pop_inline (vm, node, from_frame);
321 }
322
323 static char * nsh_pop_node_error_strings[] = {
324 #define _(sym,string) string,
325   foreach_nsh_node_error
326 #undef _
327 };
328
329 /* register nsh-input node */
330 VLIB_REGISTER_NODE (nsh_pop_node) = {
331   .name = "nsh-pop",
332   .vector_size = sizeof (u32),
333   .format_trace = format_nsh_pop_node_map_trace,
334   .format_buffer = format_nsh_pop_header,
335   .type = VLIB_NODE_TYPE_INTERNAL,
336
337   .n_errors = ARRAY_LEN(nsh_pop_node_error_strings),
338   .error_strings = nsh_pop_node_error_strings,
339
340   .n_next_nodes = NSH_NODE_N_NEXT,
341
342   .next_nodes = {
343 #define _(s,n) [NSH_NODE_NEXT_##s] = n,
344     foreach_nsh_node_next
345 #undef _
346   },
347 };
348