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