Port NSH plugin to VPP
[vpp.git] / src / plugins / nsh / nsh-md2-ioam / export-nsh-md2-ioam / nsh_md2_ioam_export.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  *------------------------------------------------------------------
17  * nsh_md2_ioam_export.c - ioam export API / debug CLI handling
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <ioam/export-common/ioam_export.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27
28 #include <nsh/nsh.h>
29 #include <nsh/nsh-md2-ioam/nsh_md2_ioam.h>
30
31
32 ioam_export_main_t nsh_md2_ioam_export_main;
33
34
35 extern vlib_node_registration_t nsh_md2_ioam_export_node;
36 extern void nsh_md2_set_next_ioam_export_override (uword next);
37 /* Action function shared between message handler and debug CLI */
38 int
39 nsh_md2_ioam_export_enable_disable (ioam_export_main_t * em,
40                                     u8 is_disable,
41                                     ip4_address_t * collector_address,
42                                     ip4_address_t * src_address)
43 {
44   vlib_main_t *vm = em->vlib_main;
45   u32 node_index = nsh_md2_ioam_export_node.index;
46   vlib_node_t *nsh_input_node = NULL;
47
48   if (is_disable == 0)
49     {
50       if (em->my_hbh_slot == ~0)
51         {
52           /* Hook this export node to nsh-input */
53           nsh_input_node = vlib_get_node_by_name (vm, (u8 *) "nsh-input");
54           if (!nsh_input_node)
55             {
56               /* node does not exist give up */
57               return (-1);
58             }
59           em->my_hbh_slot =
60             vlib_node_add_next (vm, nsh_input_node->index, node_index);
61         }
62       if (1 == ioam_export_header_create (em, collector_address, src_address))
63         {
64           ioam_export_thread_buffer_init (em, vm);
65           nsh_md2_set_next_ioam_export_override (em->my_hbh_slot);
66           /* Turn on the export buffer check process */
67           vlib_process_signal_event (vm, em->export_process_node_index, 1, 0);
68
69         }
70       else
71         {
72           return (-2);
73         }
74     }
75   else
76     {
77       nsh_md2_set_next_ioam_export_override (0);        // VXLAN_GPE_DECAP_IOAM_V4_NEXT_POP
78       ioam_export_header_cleanup (em, collector_address, src_address);
79       ioam_export_thread_buffer_free (em);
80       /* Turn off the export buffer check process */
81       vlib_process_signal_event (vm, em->export_process_node_index, 2, 0);
82
83     }
84
85   return 0;
86 }
87
88
89
90 static clib_error_t *
91 set_nsh_md2_ioam_export_ipfix_command_fn (vlib_main_t * vm,
92                                           unformat_input_t * input,
93                                           vlib_cli_command_t * cmd)
94 {
95   ioam_export_main_t *em = &nsh_md2_ioam_export_main;
96   ip4_address_t collector, src;
97   u8 is_disable = 0;
98
99   collector.as_u32 = 0;
100   src.as_u32 = 0;
101
102   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
105         ;
106       else if (unformat (input, "src %U", unformat_ip4_address, &src))
107         ;
108       else if (unformat (input, "disable"))
109         is_disable = 1;
110       else
111         break;
112     }
113
114   if (collector.as_u32 == 0)
115     return clib_error_return (0, "collector address required");
116
117   if (src.as_u32 == 0)
118     return clib_error_return (0, "src address required");
119
120   em->ipfix_collector.as_u32 = collector.as_u32;
121   em->src_address.as_u32 = src.as_u32;
122
123   vlib_cli_output (vm, "Collector %U, src address %U",
124                    format_ip4_address, &em->ipfix_collector,
125                    format_ip4_address, &em->src_address);
126
127   /* Turn on the export timer process */
128   // vlib_process_signal_event (vm, flow_report_process_node.index,
129   //1, 0);
130   if (0 !=
131       nsh_md2_ioam_export_enable_disable (em, is_disable, &collector, &src))
132     {
133       return clib_error_return (0, "Unable to set ioam nsh-md2 export");
134     }
135
136   return 0;
137 }
138
139 /* *INDENT-OFF* */
140 VLIB_CLI_COMMAND (set_nsh_md2_ioam_ipfix_command, static) =
141 {
142 .path = "set nsh-md2-ioam export ipfix",
143 .short_help = "set nsh-md2-ioam export ipfix collector <ip4-address> src <ip4-address>",
144 .function = set_nsh_md2_ioam_export_ipfix_command_fn,
145 };
146 /* *INDENT-ON* */
147
148
149 #define IPFIX_NSH_MD2_IOAM_EXPORT_ID 274        // TODO: Move this to ioam/ioam_export.h
150 static clib_error_t *
151 nsh_md2_ioam_export_init (vlib_main_t * vm)
152 {
153   ioam_export_main_t *em = &nsh_md2_ioam_export_main;
154   clib_error_t *error = 0;
155
156   em->set_id = IPFIX_NSH_MD2_IOAM_EXPORT_ID;
157   em->unix_time_0 = (u32) time (0);     /* Store starting time */
158   em->vlib_time_0 = vlib_time_now (vm);
159
160   em->my_hbh_slot = ~0;
161   em->vlib_main = vm;
162   em->vnet_main = vnet_get_main ();
163   ioam_export_reset_next_node (em);
164
165   return error;
166 }
167
168 VLIB_INIT_FUNCTION (nsh_md2_ioam_export_init);
169
170
171 /*
172  * fd.io coding-style-patch-verification: ON
173  *
174  * Local Variables:
175  * eval: (c-set-style "gnu")
176  * End:
177  */