VPP-632 : InBand OAM Analyser
[vpp.git] / src / plugins / ioam / analyse / ip6 / ip6_ioam_analyse.c
1 /*
2  * Copyright (c) 2017 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 <ioam/analyse/ioam_analyse.h>
17 #include <ioam/export-common/ioam_export.h>
18 #include <ioam/analyse/ip6/ip6_ioam_analyse.h>
19 #include <ioam/analyse/ioam_summary_export.h>
20 #include <vnet/ip/ip.h>
21 #include <ioam/ipfixcollector/ipfixcollector.h>
22
23 static clib_error_t *
24 ioam_analyse_enable_disable (vlib_main_t * vm,
25                              int is_add, int is_export, int remote_listen)
26 {
27   ipfix_client_add_del_t ipfix_reg;
28   clib_error_t *rv = 0;
29
30   ipfix_reg.client_name = format (0, "ip6-hbh-analyse-remote");
31   ipfix_reg.client_node = analyse_node_remote.index;
32   ipfix_reg.ipfix_setid = IPFIX_IOAM_EXPORT_ID;
33
34   if (is_export)
35     {
36       rv = ioam_flow_create (!is_add);
37       if (rv)
38         goto ret;
39     }
40
41   if (is_add)
42     {
43       ip6_ioam_analyse_register_handlers ();
44       if (remote_listen)
45         {
46           ipfix_reg.del = 0;
47           ipfix_collector_reg_setid (vm, &ipfix_reg);
48         }
49       else
50         {
51           ioam_export_set_next_node (&ioam_export_main,
52                                      (u8 *) "ip6-hbh-analyse-local");
53         }
54     }
55   else
56     {
57       ip6_ioam_analyse_unregister_handlers ();
58       if (remote_listen)
59         {
60           ipfix_reg.del = 1;
61           ipfix_collector_reg_setid (vm, &ipfix_reg);
62         }
63       else
64         ioam_export_reset_next_node (&ioam_export_main);
65     }
66
67 ret:
68   vec_free (ipfix_reg.client_name);
69   return rv;
70 }
71
72 static clib_error_t *
73 set_ioam_analyse_command_fn (vlib_main_t * vm, unformat_input_t * input,
74                              vlib_cli_command_t * cmd)
75 {
76   int is_export = 0;
77   int is_add = 1;
78   int remote_listen = 0;
79
80   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
81     {
82       if (unformat (input, "export-ipfix-collector"))
83         is_export = 1;
84       else if (unformat (input, "disable"))
85         is_add = 0;
86       else if (unformat (input, "listen-ipfix"))
87         remote_listen = 1;
88       else
89         break;
90     }
91
92   return (ioam_analyse_enable_disable (vm, is_add, is_export, remote_listen));
93 }
94
95 /* *INDENT-OFF* */
96 VLIB_CLI_COMMAND (set_ioam_analyse_command, static) = {
97   .path = "set ioam analyse",
98   .short_help = "set ioam analyse [export-ipfix-collector] [disable] [listen-ipfix]",
99   .function = set_ioam_analyse_command_fn,
100 };
101 /* *INDENT-ON* */
102
103 static clib_error_t *
104 show_ioam_analyse_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
105                           vlib_cli_command_t * cmd)
106 {
107   ip6_ioam_analyser_main_t *am = &ioam_analyser_main;
108   ioam_analyser_data_t *record = NULL;
109   u8 i;
110   u8 *s = 0;
111
112   vec_reset_length (s);
113   s = format (0, "iOAM Analyse Information: \n");
114   vec_foreach_index (i, am->aggregated_data)
115   {
116     record = am->aggregated_data + i;
117     if (record->is_free)
118       continue;
119
120     s = format (s, "Flow Number: %u\n", i);
121     s = print_analyse_flow (s, record);
122     s = format (s, "\n");
123   }
124   vlib_cli_output (vm, "%v", s);
125
126   vec_free (s);
127
128   return 0;
129 }
130
131 /* *INDENT-OFF* */
132 VLIB_CLI_COMMAND (ip6_show_ioam_ipfix_cmd, static) = {
133   .path = "show ioam analyse ",
134   .short_help = "show ioam analyser information",
135   .function = show_ioam_analyse_cmd_fn,
136 };
137 /* *INDENT-ON* */
138
139 static clib_error_t *
140 ioam_analyse_init (vlib_main_t * vm)
141 {
142   ip6_ioam_analyser_main_t *am = &ioam_analyser_main;
143   u16 i;
144
145   vec_validate_aligned (am->aggregated_data, 50, CLIB_CACHE_LINE_BYTES);
146   vec_foreach_index (i, am->aggregated_data)
147   {
148     ioam_analyse_init_data (am->aggregated_data + i);
149   }
150
151   return 0;
152 }
153
154 VLIB_INIT_FUNCTION (ioam_analyse_init);
155
156 /*
157  * fd.io coding-style-patch-verification: ON
158  *
159  * Local Variables:
160  * eval: (c-set-style "gnu")
161  * End:
162  */