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