ip6: fix ip6-michain trace function
[vpp.git] / src / plugins / nsh / nsh_cli.c
1 /*
2  * nsh_cli.c - nsh cli functions
3  *
4  * Copyright (c) 2019 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/adj/adj.h>
22
23 /* format from network order */
24 u8 *
25 format_nsh_pop_header (u8 * s, va_list * args)
26 {
27   return format_nsh_header (s, args);
28 }
29
30 u8 *
31 format_nsh_pop_node_map_trace (u8 * s, va_list * args)
32 {
33   return format_nsh_node_map_trace (s, args);
34 }
35
36 static uword
37 unformat_nsh_action (unformat_input_t * input, va_list * args)
38 {
39   u32 *result = va_arg (*args, u32 *);
40   u32 tmp;
41
42   if (unformat (input, "swap"))
43     *result = NSH_ACTION_SWAP;
44   else if (unformat (input, "push"))
45     *result = NSH_ACTION_PUSH;
46   else if (unformat (input, "pop"))
47     *result = NSH_ACTION_POP;
48   else if (unformat (input, "%d", &tmp))
49     *result = tmp;
50   else
51     return 0;
52
53   return 1;
54 }
55
56 static u8 *
57 format_nsh_action (u8 * s, va_list * args)
58 {
59   u32 nsh_action = va_arg (*args, u32);
60
61   switch (nsh_action)
62     {
63     case NSH_ACTION_SWAP:
64       return format (s, "swap");
65     case NSH_ACTION_PUSH:
66       return format (s, "push");
67     case NSH_ACTION_POP:
68       return format (s, "pop");
69     default:
70       return format (s, "unknown %d", nsh_action);
71     }
72   return s;
73 }
74
75 u8 *
76 format_nsh_map (u8 * s, va_list * args)
77 {
78   nsh_map_t *map = va_arg (*args, nsh_map_t *);
79
80   s = format (s, "nsh entry nsp: %d nsi: %d ",
81               (map->nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK,
82               map->nsp_nsi & NSH_NSI_MASK);
83   s = format (s, "maps to nsp: %d nsi: %d ",
84               (map->mapped_nsp_nsi >> NSH_NSP_SHIFT) & NSH_NSP_MASK,
85               map->mapped_nsp_nsi & NSH_NSI_MASK);
86
87   s = format (s, " nsh_action %U\n", format_nsh_action, map->nsh_action);
88
89   switch (map->next_node)
90     {
91     case NSH_NODE_NEXT_ENCAP_GRE4:
92       {
93         s = format (s, "encapped by GRE4 intf: %d", map->sw_if_index);
94         break;
95       }
96     case NSH_NODE_NEXT_ENCAP_GRE6:
97       {
98         s = format (s, "encapped by GRE6 intf: %d", map->sw_if_index);
99         break;
100       }
101     case NSH_NODE_NEXT_ENCAP_VXLANGPE:
102       {
103         s = format (s, "encapped by VXLAN GPE intf: %d", map->sw_if_index);
104         break;
105       }
106     case NSH_NODE_NEXT_ENCAP_VXLAN4:
107       {
108         s = format (s, "encapped by VXLAN4 intf: %d", map->sw_if_index);
109         break;
110       }
111     case NSH_NODE_NEXT_ENCAP_VXLAN6:
112       {
113         s = format (s, "encapped by VXLAN6 intf: %d", map->sw_if_index);
114         break;
115       }
116     case NSH_NODE_NEXT_DECAP_ETH_INPUT:
117       {
118         s = format (s, "encap-none");
119         break;
120       }
121     case NSH_NODE_NEXT_ENCAP_LISP_GPE:
122       {
123         s = format (s, "encapped by LISP GPE intf: %d", map->sw_if_index);
124         break;
125       }
126     case NSH_NODE_NEXT_ENCAP_ETHERNET:
127       {
128         s = format (s, "encapped by Ethernet intf: %d", map->sw_if_index);
129         break;
130       }
131     default:
132       s = format (s, "only GRE and VXLANGPE support in this rev");
133     }
134
135   return s;
136 }
137
138 static adj_index_t
139 nsh_get_adj_by_sw_if_index (u32 sw_if_index)
140 {
141   adj_index_t ai = ~0;
142
143   pool_foreach_index (ai, adj_pool)
144    {
145       if (sw_if_index == adj_get_sw_if_index(ai))
146       {
147         return ai;
148       }
149   }
150
151   return ~0;
152 }
153
154
155 /**
156  * CLI command for NSH map
157  */
158 static clib_error_t *
159 nsh_add_del_map_command_fn (vlib_main_t * vm,
160                             unformat_input_t * input,
161                             vlib_cli_command_t * cmd)
162 {
163   unformat_input_t _line_input, *line_input = &_line_input;
164   u8 is_add = 1;
165   u32 nsp, nsi, mapped_nsp, mapped_nsi, nsh_action;
166   int nsp_set = 0, nsi_set = 0, mapped_nsp_set = 0, mapped_nsi_set = 0;
167   int nsh_action_set = 0;
168   u32 next_node = ~0;
169   u32 adj_index = ~0;
170   u32 sw_if_index = ~0;         // temporary requirement to get this moved over to NSHSFC
171   u32 rx_sw_if_index = ~0;      // temporary requirement to get this moved over to NSHSFC
172   nsh_add_del_map_args_t _a, *a = &_a;
173   u32 map_index;
174   int rv;
175
176   /* Get a line of input. */
177   if (!unformat_user (input, unformat_line_input, line_input))
178     return 0;
179
180   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
181     {
182       if (unformat (line_input, "del"))
183         is_add = 0;
184       else if (unformat (line_input, "nsp %d", &nsp))
185         nsp_set = 1;
186       else if (unformat (line_input, "nsi %d", &nsi))
187         nsi_set = 1;
188       else if (unformat (line_input, "mapped-nsp %d", &mapped_nsp))
189         mapped_nsp_set = 1;
190       else if (unformat (line_input, "mapped-nsi %d", &mapped_nsi))
191         mapped_nsi_set = 1;
192       else if (unformat (line_input, "nsh_action %U", unformat_nsh_action,
193                          &nsh_action))
194         nsh_action_set = 1;
195       else if (unformat (line_input, "encap-gre4-intf %d", &sw_if_index))
196         next_node = NSH_NODE_NEXT_ENCAP_GRE4;
197       else if (unformat (line_input, "encap-gre6-intf %d", &sw_if_index))
198         next_node = NSH_NODE_NEXT_ENCAP_GRE6;
199       else if (unformat (line_input, "encap-vxlan-gpe-intf %d", &sw_if_index))
200         next_node = NSH_NODE_NEXT_ENCAP_VXLANGPE;
201       else if (unformat (line_input, "encap-lisp-gpe-intf %d", &sw_if_index))
202         next_node = NSH_NODE_NEXT_ENCAP_LISP_GPE;
203       else if (unformat (line_input, "encap-vxlan4-intf %d", &sw_if_index))
204         next_node = NSH_NODE_NEXT_ENCAP_VXLAN4;
205       else if (unformat (line_input, "encap-vxlan6-intf %d", &sw_if_index))
206         next_node = NSH_NODE_NEXT_ENCAP_VXLAN6;
207       else if (unformat (line_input, "encap-eth-intf %d", &sw_if_index))
208         {
209           next_node = NSH_NODE_NEXT_ENCAP_ETHERNET;
210           adj_index = nsh_get_adj_by_sw_if_index (sw_if_index);
211         }
212       else
213         if (unformat
214             (line_input, "encap-none %d %d", &sw_if_index, &rx_sw_if_index))
215         next_node = NSH_NODE_NEXT_DECAP_ETH_INPUT;
216       else
217         return clib_error_return (0, "parse error: '%U'",
218                                   format_unformat_error, line_input);
219     }
220
221   unformat_free (line_input);
222
223   if (nsp_set == 0 || nsi_set == 0)
224     return clib_error_return (0, "nsp nsi pair required. Key: for NSH entry");
225
226   if (mapped_nsp_set == 0 || mapped_nsi_set == 0)
227     return clib_error_return (0,
228                               "mapped-nsp mapped-nsi pair required. Key: for NSH entry");
229
230   if (nsh_action_set == 0)
231     return clib_error_return (0, "nsh_action required: swap|push|pop.");
232
233   if (next_node == ~0)
234     return clib_error_return (0,
235                               "must specific action: [encap-gre-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-lisp-gpe-intf <nn> | encap-none <tx_sw_if_index> <rx_sw_if_index>]");
236
237   clib_memset (a, 0, sizeof (*a));
238
239   /* set args structure */
240   a->is_add = is_add;
241   a->map.nsp_nsi = (nsp << NSH_NSP_SHIFT) | nsi;
242   a->map.mapped_nsp_nsi = (mapped_nsp << NSH_NSP_SHIFT) | mapped_nsi;
243   a->map.nsh_action = nsh_action;
244   a->map.sw_if_index = sw_if_index;
245   a->map.rx_sw_if_index = rx_sw_if_index;
246   a->map.next_node = next_node;
247   a->map.adj_index = adj_index;
248
249   rv = nsh_add_del_map (a, &map_index);
250
251   switch (rv)
252     {
253     case 0:
254       break;
255     case -1:                    //TODO API_ERROR_INVALID_VALUE:
256       return clib_error_return (0,
257                                 "mapping already exists. Remove it first.");
258
259     case -2:                    // TODO API_ERROR_NO_SUCH_ENTRY:
260       return clib_error_return (0, "mapping does not exist.");
261
262     default:
263       return clib_error_return (0, "nsh_add_del_map returned %d", rv);
264     }
265
266   if ((a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN4)
267       | (a->map.next_node == NSH_NODE_NEXT_ENCAP_VXLAN6))
268     {
269       rv = nsh_add_del_proxy_session (a);
270
271       switch (rv)
272         {
273         case 0:
274           break;
275         case -1:                //TODO API_ERROR_INVALID_VALUE:
276           return clib_error_return (0,
277                                     "nsh-proxy-session already exists. Remove it first.");
278
279         case -2:                // TODO API_ERROR_NO_SUCH_ENTRY:
280           return clib_error_return (0, "nsh-proxy-session does not exist.");
281
282         default:
283           return clib_error_return
284             (0, "nsh_add_del_proxy_session() returned %d", rv);
285         }
286     }
287
288   return 0;
289 }
290
291 VLIB_CLI_COMMAND (create_nsh_map_command, static) = {
292   .path = "create nsh map",
293   .short_help =
294     "create nsh map nsp <nn> nsi <nn> [del] mapped-nsp <nn> mapped-nsi <nn> nsh_action [swap|push|pop] "
295     "[encap-gre4-intf <nn> | encap-gre4-intf <nn> | encap-vxlan-gpe-intf <nn> | encap-lisp-gpe-intf <nn> "
296     " encap-vxlan4-intf <nn> | encap-vxlan6-intf <nn>| encap-eth-intf <nn> | encap-none]\n",
297   .function = nsh_add_del_map_command_fn,
298 };
299
300 /**
301  * CLI command for showing the mapping between NSH entries
302  */
303 static clib_error_t *
304 show_nsh_map_command_fn (vlib_main_t * vm,
305                          unformat_input_t * input, vlib_cli_command_t * cmd)
306 {
307   nsh_main_t *nm = &nsh_main;
308   nsh_map_t *map;
309
310   if (pool_elts (nm->nsh_mappings) == 0)
311     vlib_cli_output (vm, "No nsh maps configured.");
312
313   pool_foreach (map, nm->nsh_mappings)
314   {
315     vlib_cli_output (vm, "%U", format_nsh_map, map);
316   }
317
318   return 0;
319 }
320
321 VLIB_CLI_COMMAND (show_nsh_map_command, static) = {
322   .path = "show nsh map",
323   .function = show_nsh_map_command_fn,
324 };
325
326 /**
327  * CLI command for adding NSH entry
328  */
329 static clib_error_t *
330 nsh_add_del_entry_command_fn (vlib_main_t * vm,
331                               unformat_input_t * input,
332                               vlib_cli_command_t * cmd)
333 {
334   unformat_input_t _line_input, *line_input = &_line_input;
335   u8 is_add = 1;
336   u8 ver_o_c = 0;
337   u8 ttl = 63;
338   u8 length = 0;
339   u8 md_type = 0;
340   u8 next_protocol = 1;         /* default: ip4 */
341   u32 nsp;
342   u8 nsp_set = 0;
343   u32 nsi;
344   u8 nsi_set = 0;
345   u32 nsp_nsi;
346   u32 c1 = 0;
347   u32 c2 = 0;
348   u32 c3 = 0;
349   u32 c4 = 0;
350   u8 *data = 0;
351   nsh_tlv_header_t tlv_header;
352   u8 cur_len = 0, tlvs_len = 0;
353   u8 *current;
354   nsh_main_t *nm = &nsh_main;
355   nsh_option_map_t _nsh_option, *nsh_option = &_nsh_option;
356   u8 option_size = 0;
357   u32 tmp;
358   int rv;
359   u32 entry_index;
360   nsh_add_del_entry_args_t _a, *a = &_a;
361   u8 has_ioam_trace_option = 0;
362
363   /* Get a line of input. */
364   if (!unformat_user (input, unformat_line_input, line_input))
365     return 0;
366
367   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
368     {
369       if (unformat (line_input, "del"))
370         is_add = 0;
371       else if (unformat (line_input, "version %d", &tmp))
372         ver_o_c |= (tmp & 3) << 6;
373       else if (unformat (line_input, "o-bit %d", &tmp))
374         ver_o_c |= (tmp & 1) << 5;
375       else if (unformat (line_input, "c-bit %d", &tmp))
376         ver_o_c |= (tmp & 1) << 4;
377       else if (unformat (line_input, "ttl %d", &ttl))
378         ver_o_c |= (ttl & NSH_LEN_MASK) >> 2;
379       else if (unformat (line_input, "md-type %d", &tmp))
380         md_type = tmp;
381       else if (unformat (line_input, "next-ip4"))
382         next_protocol = 1;
383       else if (unformat (line_input, "next-ip6"))
384         next_protocol = 2;
385       else if (unformat (line_input, "next-ethernet"))
386         next_protocol = 3;
387       else if (unformat (line_input, "c1 %d", &c1))
388         ;
389       else if (unformat (line_input, "c2 %d", &c2))
390         ;
391       else if (unformat (line_input, "c3 %d", &c3))
392         ;
393       else if (unformat (line_input, "c4 %d", &c4))
394         ;
395       else if (unformat (line_input, "nsp %d", &nsp))
396         nsp_set = 1;
397       else if (unformat (line_input, "nsi %d", &nsi))
398         nsi_set = 1;
399       else if (unformat (line_input, "tlv-ioam-trace"))
400         has_ioam_trace_option = 1;
401       else
402         return clib_error_return (0, "parse error: '%U'",
403                                   format_unformat_error, line_input);
404     }
405
406   unformat_free (line_input);
407
408   if (nsp_set == 0)
409     return clib_error_return (0, "nsp not specified");
410
411   if (nsi_set == 0)
412     return clib_error_return (0, "nsi not specified");
413
414   if (md_type == 1 && has_ioam_trace_option == 1)
415     return clib_error_return (0, "Invalid MD Type");
416
417   nsp_nsi = (nsp << 8) | nsi;
418
419   clib_memset (a, 0, sizeof (*a));
420   a->is_add = is_add;
421
422   if (md_type == 1)
423     {
424       a->nsh_entry.md.md1_data.c1 = c1;
425       a->nsh_entry.md.md1_data.c2 = c2;
426       a->nsh_entry.md.md1_data.c3 = c3;
427       a->nsh_entry.md.md1_data.c4 = c4;
428       length = (sizeof (nsh_base_header_t) + sizeof (nsh_md1_data_t)) >> 2;
429     }
430   else if (md_type == 2)
431     {
432       length = sizeof (nsh_base_header_t) >> 2;
433
434       vec_free (a->nsh_entry.tlvs_data);
435       tlvs_len = (MAX_METADATA_LEN << 2);
436       vec_validate_aligned (data, tlvs_len - 1, CLIB_CACHE_LINE_BYTES);
437       a->nsh_entry.tlvs_data = data;
438       current = data;
439
440       if (has_ioam_trace_option)
441         {
442           tlv_header.class = clib_host_to_net_u16 (NSH_MD2_IOAM_CLASS);
443           tlv_header.type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
444           /* Uses network order's class and type to lookup */
445           nsh_option =
446             nsh_md2_lookup_option (tlv_header.class, tlv_header.type);
447           if (nsh_option == NULL)
448             return clib_error_return (0, "iOAM Trace not registered");
449
450           if (nm->add_options[nsh_option->option_id] != NULL)
451             {
452               if (0 != nm->add_options[nsh_option->option_id] ((u8 *) current,
453                                                                &option_size))
454                 {
455                   return clib_error_return (0, "Invalid MD Type");
456                 }
457             }
458
459           nm->options_size[nsh_option->option_id] = option_size;
460           /* round to 4-byte */
461           option_size = (((option_size + 3) >> 2) << 2);
462
463           cur_len += option_size;
464           current = data + option_size;
465         }
466
467       /* Add more options' parsing */
468
469       a->nsh_entry.tlvs_len = cur_len;
470       length += (cur_len >> 2);
471     }
472   length = (length & NSH_LEN_MASK) | ((ttl & 0x3) << 6);
473
474 #define _(x) a->nsh_entry.nsh_base.x = x;
475   foreach_copy_nsh_base_hdr_field;
476 #undef _
477
478   rv = nsh_add_del_entry (a, &entry_index);
479
480   switch (rv)
481     {
482     case 0:
483       break;
484     default:
485       return clib_error_return (0, "nsh_add_del_entry returned %d", rv);
486     }
487
488   return 0;
489 }
490
491 VLIB_CLI_COMMAND (create_nsh_entry_command, static) = {
492   .path = "create nsh entry",
493   .short_help =
494     "create nsh entry {nsp <nn> nsi <nn>} [ttl <nn>] [md-type <nn>]"
495     "  [c1 <nn> c2 <nn> c3 <nn> c4 <nn>] [tlv-ioam-trace] [del]\n",
496   .function = nsh_add_del_entry_command_fn,
497 };
498
499 /* format from network order */
500 u8 *
501 format_nsh_header (u8 * s, va_list * args)
502 {
503   nsh_main_t *nm = &nsh_main;
504   nsh_md2_data_t *opt0;
505   nsh_md2_data_t *limit0;
506   nsh_option_map_t *nsh_option;
507   u8 option_len = 0;
508
509   u8 *header = va_arg (*args, u8 *);
510   nsh_base_header_t *nsh_base = (nsh_base_header_t *) header;
511   nsh_md1_data_t *nsh_md1 = (nsh_md1_data_t *) (nsh_base + 1);
512   nsh_md2_data_t *nsh_md2 = (nsh_md2_data_t *) (nsh_base + 1);
513   opt0 = (nsh_md2_data_t *) nsh_md2;
514   limit0 = (nsh_md2_data_t *) ((u8 *) nsh_md2 +
515                                ((nsh_base->length & NSH_LEN_MASK) * 4
516                                 - sizeof (nsh_base_header_t)));
517
518   s = format (s, "nsh ver %d ", (nsh_base->ver_o_c >> 6));
519   if (nsh_base->ver_o_c & NSH_O_BIT)
520     s = format (s, "O-set ");
521
522   if (nsh_base->ver_o_c & NSH_C_BIT)
523     s = format (s, "C-set ");
524
525   s = format (s, "ttl %d ", (nsh_base->ver_o_c & NSH_TTL_H4_MASK) << 2 |
526               (nsh_base->length & NSH_TTL_L2_MASK) >> 6);
527
528   s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
529               (nsh_base->length & NSH_LEN_MASK),
530               (nsh_base->length & NSH_LEN_MASK) * 4,
531               nsh_base->md_type, nsh_base->next_protocol);
532
533   s = format (s, "  service path %d service index %d\n",
534               (clib_net_to_host_u32 (nsh_base->nsp_nsi) >> NSH_NSP_SHIFT) &
535               NSH_NSP_MASK,
536               clib_net_to_host_u32 (nsh_base->nsp_nsi) & NSH_NSI_MASK);
537
538   if (nsh_base->md_type == 1)
539     {
540       s = format (s, "  c1 %d c2 %d c3 %d c4 %d\n",
541                   clib_net_to_host_u32 (nsh_md1->c1),
542                   clib_net_to_host_u32 (nsh_md1->c2),
543                   clib_net_to_host_u32 (nsh_md1->c3),
544                   clib_net_to_host_u32 (nsh_md1->c4));
545     }
546   else if (nsh_base->md_type == 2)
547     {
548       s = format (s, "  Supported TLVs: \n");
549
550       /* Scan the set of variable metadata, network order */
551       while (opt0 < limit0)
552         {
553           nsh_option = nsh_md2_lookup_option (opt0->class, opt0->type);
554           if (nsh_option != NULL)
555             {
556               if (nm->trace[nsh_option->option_id] != NULL)
557                 {
558                   s = (*nm->trace[nsh_option->option_id]) (s, opt0);
559                 }
560               else
561                 {
562                   s =
563                     format (s, "\n    untraced option %d length %d",
564                             opt0->type, opt0->length);
565                 }
566             }
567           else
568             {
569               s =
570                 format (s, "\n    unrecognized option %d length %d",
571                         opt0->type, opt0->length);
572             }
573
574           /* round to 4-byte */
575           option_len = ((opt0->length + 3) >> 2) << 2;
576           opt0 =
577             (nsh_md2_data_t *) (((u8 *) opt0) + sizeof (nsh_md2_data_t) +
578                                 option_len);
579         }
580     }
581
582   return s;
583 }
584
585 u8 *
586 format_nsh_node_map_trace (u8 * s, va_list * args)
587 {
588   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
589   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
590   nsh_input_trace_t *t = va_arg (*args, nsh_input_trace_t *);
591
592   s = format (s, "\n  %U", format_nsh_header, &(t->trace_data));
593
594   return s;
595 }
596
597 static clib_error_t *
598 show_nsh_entry_command_fn (vlib_main_t * vm,
599                            unformat_input_t * input, vlib_cli_command_t * cmd)
600 {
601   nsh_main_t *nm = &nsh_main;
602   nsh_entry_t *nsh_entry;
603
604   if (pool_elts (nm->nsh_entries) == 0)
605     vlib_cli_output (vm, "No nsh entries configured.");
606
607   pool_foreach (nsh_entry, nm->nsh_entries)
608   {
609     vlib_cli_output (vm, "%U", format_nsh_header, nsh_entry->rewrite);
610     vlib_cli_output (vm, "  rewrite_size: %d bytes", nsh_entry->rewrite_size);
611   }
612
613   return 0;
614 }
615
616 VLIB_CLI_COMMAND (show_nsh_entry_command, static) = {
617   .path = "show nsh entry",
618   .function = show_nsh_entry_command_fn,
619 };
620
621 /*
622  * fd.io coding-style-patch-verification: ON
623  *
624  * Local Variables:
625  * eval: (c-set-style "gnu")
626  * End:
627  */