summaryrefslogtreecommitdiffstats
path: root/src/plugins/ioam/udp-ping/udp_ping_export.c
blob: de0bad7a8551f79065262bcbe7f38414245cb5c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/ipfix-export/flow_report.h>
#include <ioam/analyse/ioam_summary_export.h>
#include <vnet/api_errno.h>
#include <vnet/ip/ip4.h>
#include <vnet/udp/udp_local.h>
#include <ioam/udp-ping/udp_ping.h>

#define UDP_PING_EXPORT_RECORD_SIZE 400

static u8 *
udp_ping_template_rewrite (ipfix_exporter_t *exp, flow_report_t *fr,
			   u16 collector_port, ipfix_report_element_t *elts,
			   u32 n_elts, u32 *stream_index)
{
  return ioam_template_rewrite (exp, fr, collector_port, elts, n_elts,
				stream_index);
}

static vlib_frame_t *
udp_ping_send_flows (flow_report_main_t *frm, ipfix_exporter_t *exp,
		     flow_report_t *fr, vlib_frame_t *f, u32 *to_next,
		     u32 node_index)
{
  vlib_buffer_t *b0 = NULL;
  u32 next_offset = 0;
  u32 bi0 = ~0;
  int i, j;
  ip4_ipfix_template_packet_t *tp;
  ipfix_message_header_t *h;
  ipfix_set_header_t *s = NULL;
  ip4_header_t *ip;
  udp_header_t *udp;
  u16 new_l0, old_l0;
  ip_csum_t sum0;
  vlib_main_t *vm = vlib_get_main ();
  flow_report_stream_t *stream;
  udp_ping_flow_data *stats;
  ip46_udp_ping_flow *ip46_flow;
  u16 src_port, dst_port;
  u16 data_len;

  stream = &exp->streams[fr->stream_index];
  data_len = vec_len (udp_ping_main.ip46_flow);

  for (i = 0; i < data_len; i++)
    {
      if (pool_is_free_index (udp_ping_main.ip46_flow, i))
	continue;

      ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
      j = 0;
      for (src_port = ip46_flow->udp_data.start_src_port;
	   src_port <= ip46_flow->udp_data.end_src_port; src_port++)
	{
	  for (dst_port = ip46_flow->udp_data.start_dst_port;
	       dst_port <= ip46_flow->udp_data.end_dst_port; dst_port++, j++)
	    {
	      stats = ip46_flow->udp_data.stats + j;
	      if (PREDICT_FALSE (b0 == NULL))
		{
		  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
		    break;


		  b0 = vlib_get_buffer (vm, bi0);
		  memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
		  b0->current_data = 0;
		  b0->current_length = vec_len (fr->rewrite);
		  b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
		  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
		  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;

		  tp = vlib_buffer_get_current (b0);
		  ip = &tp->ip4;
		  h = &tp->ipfix.h;
		  s = &tp->ipfix.s;

		  /* FIXUP: message header export_time */
		  h->export_time = clib_host_to_net_u32 (((u32) time (NULL)));

		  /* FIXUP: message header sequence_number */
		  h->sequence_number = stream->sequence_number++;
		  h->sequence_number =
		    clib_host_to_net_u32 (h->sequence_number);
		  next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
		}

	      next_offset = ioam_analyse_add_ipfix_record (fr,
							   &stats->analyse_data,
							   b0, next_offset,
							   &ip46_flow->
							   src.ip6,
							   &ip46_flow->
							   dst.ip6, src_port,
							   dst_port);

	      //u32 pak_sent = clib_host_to_net_u32(stats->pak_sent);
	      //memcpy (b0->data + next_offset, &pak_sent, sizeof(u32));
	      //next_offset += sizeof(u32);

	      /* Flush data if packet len is about to reach path mtu */
	      if (next_offset > (exp->path_mtu - UDP_PING_EXPORT_RECORD_SIZE))
		{
		  b0->current_length = next_offset;
		  b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
		  tp = vlib_buffer_get_current (b0);
		  ip = (ip4_header_t *) & tp->ip4;
		  udp = (udp_header_t *) (ip + 1);
		  h = &tp->ipfix.h;
		  s = &tp->ipfix.s;

		  s->set_id_length =
		    ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
					 next_offset - (sizeof (*ip) +
							sizeof (*udp) +
							sizeof (*h)));
		  h->version_length =
		    version_length (next_offset -
				    (sizeof (*ip) + sizeof (*udp)));

		  sum0 = ip->checksum;
		  old_l0 = ip->length;
		  new_l0 = clib_host_to_net_u16 ((u16) next_offset);
		  sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
					 length /* changed member */ );

		  ip->checksum = ip_csum_fold (sum0);
		  ip->length = new_l0;
		  udp->length =
		    clib_host_to_net_u16 (b0->current_length - sizeof (*ip));

		  udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
		  if (udp->checksum == 0)
		    udp->checksum = 0xffff;

		  ASSERT (ip4_header_checksum_is_valid (ip));

		  to_next[0] = bi0;
		  f->n_vectors++;
		  to_next++;

		  if (f->n_vectors == VLIB_FRAME_SIZE)
		    {
		      vlib_put_frame_to_node (vm, node_index, f);
		      f = vlib_get_frame_to_node (vm, node_index);
		      f->n_vectors = 0;
		      to_next = vlib_frame_vector_args (f);
		    }
		  b0 = 0;
		  bi0 = ~0;
		}
	    }
	}
    }

  if (b0)
    {
      b0->current_length = next_offset;
      b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
      tp = vlib_buffer_get_current (b0);
      ip = (ip4_header_t *) & tp->ip4;
      udp = (udp_header_t *) (ip + 1);
      h = &tp->ipfix.h;
      s = &tp->ipfix.s;

      s->set_id_length = ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
					      next_offset - (sizeof (*ip) +
							     sizeof (*udp) +
							     sizeof (*h)));
      h->version_length =
	version_length (next_offset - (sizeof (*ip) + sizeof (*udp)));

      sum0 = ip->checksum;
      old_l0 = ip->length;
      new_l0 = clib_host_to_net_u16 ((u16) next_offset);
      sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
			     length /* changed member */ );

      ip->checksum = ip_csum_fold (sum0);
      ip->length = new_l0;
      udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));

      udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
      if (udp->checksum == 0)
	udp->checksum = 0xffff;

      ASSERT (ip4_header_checksum_is_valid (ip));

      to_next[0] = bi0;
      f->n_vectors++;
      to_next++;

      if (f->n_vectors == VLIB_FRAME_SIZE)
	{
	  vlib_put_frame_to_node (vm, node_index, f);
	  f = vlib_get_frame_to_node (vm, node_index);
	  f->n_vectors = 0;
	  to_next = vlib_frame_vector_args (f);
	}
      b0 = 0;
      bi0 = ~0;
    }
  return f;
}

clib_error_t *
udp_ping_flow_create (u8 del)
{
  vnet_flow_report_add_del_args_t args;
  int rv;
  u32 domain_id = 0;
  ipfix_exporter_t *exp = &flow_report_main.exporters[0];
  u16 template_id;

  clib_memset (&args, 0, sizeof (args));
  args.rewrite_callback = udp_ping_template_rewrite;
  args.flow_data_callback = udp_ping_send_flows;
  del ? (args.is_add = 0) : (args.is_add = 1);
  args.domain_id = domain_id;
  args.src_port = UDP_DST_PORT_ipfix;

  rv = vnet_flow_report_add_del (exp, &args, &template_id);

  switch (rv)
    {
    case 0:
      break;
    case VNET_API_ERROR_NO_SUCH_ENTRY:
      return clib_error_return (0, "registration not found...");
    default:
      return clib_error_return (0, "vnet_flow_report_add_del returned %d",
				rv);
    }

  return 0;
}

static clib_error_t *
set_udp_ping_export_command_fn (vlib_main_t * vm, unformat_input_t * input,
				vlib_cli_command_t * cmd)
{
  //int rv;
  int is_add = 1;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "export-ipfix"))
	is_add = 1;
      else if (unformat (input, "disable"))
	is_add = 0;
      else
	break;
    }

  if (is_add)
    (void) udp_ping_flow_create (0);
  else
    (void) udp_ping_flow_create (1);

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (set_udp_ping_export_command, static) = {
    .path = "set udp-ping export-ipfix",
    .short_help = "set udp-ping export-ipfix [disable]",
    .function = set_udp_ping_export_command_fn,
};
/* *INDENT-ON* */

clib_error_t *
udp_ping_flow_report_init (vlib_main_t * vm)
{
  return 0;
}

/* *INDENT-OFF* */
VLIB_INIT_FUNCTION (udp_ping_flow_report_init) =
{
  .runs_after = VLIB_INITS ("flow_report_init"),
};
/* *INDENT-ON* */


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */