aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/ipsec/ipsec_output.c
blob: 6b99965d501c08dbe467e75eb1157173e249c8ae (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * ipsec_output.c : IPSec output node
 *
 * Copyright (c) 2015 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/vnet.h>
#include <vnet/api_errno.h>
#include <vnet/ip/ip.h>

#include <vnet/ipsec/ipsec.h>

#if WITH_LIBSSL > 0

#define foreach_ipsec_output_error                   \
 _(RX_PKTS, "IPSec pkts received")                   \
 _(POLICY_DISCARD, "IPSec policy discard")           \
 _(POLICY_NO_MATCH, "IPSec policy (no match)")       \
 _(POLICY_PROTECT, "IPSec policy protect")           \
 _(POLICY_BYPASS, "IPSec policy bypass")             \
 _(ENCAPS_FAILED, "IPSec encapsulation failed")

typedef enum
{
#define _(sym,str) IPSEC_OUTPUT_ERROR_##sym,
  foreach_ipsec_output_error
#undef _
    IPSEC_DECAP_N_ERROR,
} ipsec_output_error_t;

static char *ipsec_output_error_strings[] = {
#define _(sym,string) string,
  foreach_ipsec_output_error
#undef _
};

typedef struct
{
  u32 spd_id;
} ipsec_output_trace_t;

/* packet trace format function */
static u8 *
format_ipsec_output_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  ipsec_output_trace_t *t = va_arg (*args, ipsec_output_trace_t *);

  if (t->spd_id != ~0)
    {
      s = format (s, "spd %u ", t->spd_id);
    }
  else
    {
      s = format (s, "no spd");
    }
  return s;
}

always_inline ipsec_policy_t *
ipsec_output_policy_match (ipsec_spd_t * spd, u8 pr, u32 la, u32 ra, u16 lp,
			   u16 rp)
{
  ipsec_policy_t *p;
  u32 *i;

  if (!spd)
    return 0;

  vec_foreach (i, spd->ipv4_outbound_policies)
  {
    p = pool_elt_at_index (spd->policies, *i);
    if (PREDICT_FALSE (p->protocol && (p->protocol != pr)))
      continue;

    if (ra < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
      continue;

    if (ra > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
      continue;

    if (la < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
      continue;

    if (la > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
      continue;

    if (PREDICT_FALSE
	((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)
	 && (pr != IP_PROTOCOL_SCTP)))
      return p;

    if (lp < p->lport.start)
      continue;

    if (lp > p->lport.stop)
      continue;

    if (rp < p->rport.start)
      continue;

    if (rp > p->rport.stop)
      continue;

    return p;
  }
  return 0;
}

always_inline uword
ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
		      ip6_address_t * ua)
{
  if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
      (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
    return 1;
  return 0;
}

always_inline ipsec_policy_t *
ipsec6_output_policy_match (ipsec_spd_t * spd,
			    ip6_address_t * la,
			    ip6_address_t * ra, u16 lp, u16 rp, u8 pr)
{
  ipsec_policy_t *p;
  u32 *i;

  if (!spd)
    return 0;

  vec_foreach (i, spd->ipv6_outbound_policies)
  {
    p = pool_elt_at_index (spd->policies, *i);
    if (PREDICT_FALSE (p->protocol && (p->protocol != pr)))
      continue;

    if (!ip6_addr_match_range (ra, &p->raddr.start.ip6, &p->raddr.stop.ip6))
      continue;

    if (!ip6_addr_match_range (la, &p->laddr.start.ip6, &p->laddr.stop.ip6))
      continue;

    if (PREDICT_FALSE
	((pr != IP_PROTOCOL_TCP) && (pr != IP_PROTOCOL_UDP)
	 && (pr != IP_PROTOCOL_SCTP)))
      return p;

    if (lp < p->lport.start)
      continue;

    if (lp > p->lport.stop)
      continue;

    if (rp < p->rport.start)
      continue;

    if (rp > p->rport.stop)
      continue;

    return p;
  }

  return 0;
}

static inline uword
ipsec_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
		     vlib_frame_t * from_frame, int is_ipv6)
{
  ipsec_main_t *im = &ipsec_main;

  u32 *from, *to_next = 0;
  u32 n_left_from, sw_if_index0, last_sw_if_index = (u32) ~ 0;
  u32 next_node_index = (u32) ~ 0, last_next_node_index = (u32) ~ 0;
  vlib_frame_t *f = 0;
  u32 spd_index0 = ~0;
  ipsec_spd_t *spd0 = 0;
  int bogus;
  u64 nc_protect = 0, nc_bypass = 0, nc_discard = 0, nc_nomatch = 0;

  from = vlib_frame_vector_args (from_frame);
  n_left_from = from_frame->n_vectors;

  while (n_left_from > 0)
    {
      u32 bi0;
      vlib_buffer_t *b0;
      ipsec_policy_t *p0;
      ip4_header_t *ip0;
      ip6_header_t *ip6_0 = 0;
      udp_header_t *udp0;
      u32 iph_offset = 0;
      tcp_header_t *tcp0;

      bi0 = from[0];
      b0 = vlib_get_buffer (vm, bi0);
      sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
      iph_offset = vnet_buffer (b0)->ip.save_rewrite_length;
      ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0)
			      + iph_offset);

      /* lookup for SPD only if sw_if_index is changed */
      if (PREDICT_FALSE (last_sw_if_index != sw_if_index0))
	{
	  uword *p = hash_get (im->spd_index_by_sw_if_index, sw_if_index0);
	  ASSERT (p);
	  spd_index0 = p[0];
	  spd0 = pool_elt_at_index (im->spds, spd_index0);
	  last_sw_if_index = sw_if_index0;
	}

      if (is_ipv6)
	{
	  ip6_0 = (ip6_header_t *) ((u8 *) vlib_buffer_get_current (b0)
				    + iph_offset);

	  udp0 = ip6_next_header (ip6_0);
#if 0
	  clib_warning
	    ("packet received from %U port %u to %U port %u spd_id %u",
	     format_ip6_address, &ip6_0->src_address,
	     clib_net_to_host_u16 (udp0->src_port), format_ip6_address,
	     &ip6_0->dst_address, clib_net_to_host_u16 (udp0->dst_port),
	     spd0->id);
#endif

	  p0 = ipsec6_output_policy_match (spd0,
					   &ip6_0->src_address,
					   &ip6_0->dst_address,
					   clib_net_to_host_u16
					   (udp0->src_port),
					   clib_net_to_host_u16
					   (udp0->dst_port), ip6_0->protocol);
	}
      else
	{
	  udp0 = (udp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));

#if 0
	  clib_warning ("packet received from %U to %U port %u",
			format_ip4_address, ip0->src_address.as_u8,
			format_ip4_address, ip0->dst_address.as_u8,
			clib_net_to_host_u16 (udp0->dst_port));
	  clib_warning ("sw_if_index0 %u spd_index0 %u spd_id %u",
			sw_if_index0, spd_index0, spd0->id);
#endif

	  p0 = ipsec_output_policy_match (spd0, ip0->protocol,
					  clib_net_to_host_u32
					  (ip0->src_address.as_u32),
					  clib_net_to_host_u32
					  (ip0->dst_address.as_u32),
					  clib_net_to_host_u16
					  (udp0->src_port),
					  clib_net_to_host_u16
					  (udp0->dst_port));
	}
      tcp0 = (void *) udp0;

      if (PREDICT_TRUE (p0 != NULL))
	{
	  if (p0->policy == IPSEC_POLICY_ACTION_PROTECT)
	    {
	      ipsec_sa_t *sa = 0;
	      nc_protect++;
	      sa = pool_elt_at_index (im->sad, p0->sa_index);
	      if (sa->protocol == IPSEC_PROTOCOL_ESP)
		if (is_ipv6)
		  next_node_index = im->esp6_encrypt_node_index;
		else
		  next_node_index = im->esp4_encrypt_node_index;
	      else if (is_ipv6)
		next_node_index = im->ah6_encrypt_node_index;
	      else
		next_node_index = im->ah4_encrypt_node_index;
	      vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
	      p0->counter.packets++;
	      if (is_ipv6)
		{
		  p0->counter.bytes +=
		    clib_net_to_host_u16 (ip6_0->payload_length);
		  p0->counter.bytes += sizeof (ip6_header_t);
		  if (PREDICT_FALSE
		      (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM))
		    {
		      tcp0->checksum =
			ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6_0,
							   &bogus);
		      b0->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
		    }
		  if (PREDICT_FALSE
		      (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
		    {
		      udp0->checksum =
			ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6_0,
							   &bogus);
		      b0->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
		    }
		}
	      else
		{
		  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
		  if (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
		    {
		      ip0->checksum = ip4_header_checksum (ip0);
		      b0->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
		    }
		  if (PREDICT_FALSE
		      (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM))
		    {
		      tcp0->checksum =
			ip4_tcp_udp_compute_checksum (vm, b0, ip0);
		      b0->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
		    }
		  if (PREDICT_FALSE
		      (b0->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
		    {
		      udp0->checksum =
			ip4_tcp_udp_compute_checksum (vm, b0, ip0);
		      b0->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
		    }
		}
	      vlib_buffer_advance (b0, iph_offset);
	    }
	  else if (p0->policy == IPSEC_POLICY_ACTION_BYPASS)
	    {
	      nc_bypass++;
	      next_node_index = get_next_output_feature_node_index (b0, node);
	      p0->counter.packets++;
	      if (is_ipv6)
		{
		  p0->counter.bytes +=
		    clib_net_to_host_u16 (ip6_0->payload_length);
		  p0->counter.bytes += sizeof (ip6_header_t);
		}
	      else
		{
		  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
		}
	    }
	  else
	    {
	      nc_discard++;
	      p0->counter.packets++;
	      if (is_ipv6)
		{
		  p0->counter.bytes +=
		    clib_net_to_host_u16 (ip6_0->payload_length);
		  p0->counter.bytes += sizeof (ip6_header_t);
		}
	      else
		{
		  p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
		}
	      next_node_index = im->error_drop_node_index;
	    }
	}
      else
	{
	  nc_nomatch++;
	  next_node_index = im->error_drop_node_index;
	}

      from += 1;
      n_left_from -= 1;

      if (PREDICT_FALSE ((last_next_node_index != next_node_index) || f == 0))
	{
	  /* if this is not 1st frame */
	  if (f)
	    vlib_put_frame_to_node (vm, last_next_node_index, f);

	  last_next_node_index = next_node_index;

	  f = vlib_get_frame_to_node (vm, next_node_index);

	  /* frame->frame_flags, copy it from node */
	  /* Copy trace flag from next_frame and from runtime. */
	  f->frame_flags |= node->flags & VLIB_NODE_FLAG_TRACE;

	  to_next = vlib_frame_vector_args (f);
	}

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

      if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
	{
	  ipsec_output_trace_t *tr =
	    vlib_add_trace (vm, node, b0, sizeof (*tr));
	  if (spd0)
	    tr->spd_id = spd0->id;
	}
    }

  vlib_put_frame_to_node (vm, next_node_index, f);
  vlib_node_increment_counter (vm, node->node_index,
			       IPSEC_OUTPUT_ERROR_POLICY_PROTECT, nc_protect);
  vlib_node_increment_counter (vm, node->node_index,
			       IPSEC_OUTPUT_ERROR_POLICY_BYPASS, nc_bypass);
  vlib_node_increment_counter (vm, node->node_index,
			       IPSEC_OUTPUT_ERROR_POLICY_DISCARD, nc_discard);
  vlib_node_increment_counter (vm, node->node_index,
			       IPSEC_OUTPUT_ERROR_POLICY_NO_MATCH,
			       nc_nomatch);
  return from_frame->n_vectors;
}

VLIB_NODE_FN (ipsec4_output_node) (vlib_main_t * vm,
				   vlib_node_runtime_t * node,
				   vlib_frame_t * frame)
{
  return ipsec_output_inline (vm, node, frame, 0);
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (ipsec4_output_node) = {
  .name = "ipsec4-output",
  .vector_size = sizeof (u32),
  .format_trace = format_ipsec_output_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = ARRAY_LEN(ipsec_output_error_strings),
  .error_strings = ipsec_output_error_strings,

  .n_next_nodes = IPSEC_OUTPUT_N_NEXT,
  .next_nodes = {
#define _(s,n) [IPSEC_OUTPUT_NEXT_##s] = n,
    foreach_ipsec_output_next
#undef _
  },
};
/* *INDENT-ON* */

VLIB_NODE_FN (ipsec6_output_node) (vlib_main_t * vm,
				   vlib_node_runtime_t * node,
				   vlib_frame_t * frame)
{
  return ipsec_output_inline (vm, node, frame, 1);
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (ipsec6_output_node) = {
  .name = "ipsec6-output",
  .vector_size = sizeof (u32),
  .format_trace = format_ipsec_output_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = ARRAY_LEN(ipsec_output_error_strings),
  .error_strings = ipsec_output_error_strings,

  .n_next_nodes = IPSEC_OUTPUT_N_NEXT,
  .next_nodes = {
#define _(s,n) [IPSEC_OUTPUT_NEXT_##s] = n,
    foreach_ipsec_output_next
#undef _
  },
};
/* *INDENT-ON* */

#else /* IPSEC > 1 */

/* Dummy ipsec output node, in case when IPSec is disabled */

static uword
ipsec_output_node_fn (vlib_main_t * vm,
		      vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  clib_warning ("IPSec disabled");
  return 0;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (ipsec4_output_node) = {
  .vector_size = sizeof (u32),
  .function = ipsec_output_node_fn,
  .name = "ipsec4-output",
};

VLIB_REGISTER_NODE (ipsec6_output_node) = {
  .vector_size = sizeof (u32),
  .function = ipsec_output_node_fn,
  .name = "ipsec6-output",
};
/* *INDENT-ON* */
#endif

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
return res except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def get_running_info (self): """ Performs single poll of TRex running data and process it into the result object (named `result_obj`). .. tip:: This method will throw an exception if TRex isn't running. Always consider using :func:`trex_client.CTRexClient.is_running` which handles a single poll operation in safer manner. :parameters: None :return: dictionary containing the most updated data dump from TRex. :raises: + :exc:`trex_exceptions.TRexIncompleteRunError`, in case one of failed TRex run (unexpected termination). + :exc:`TypeError`, in case JSON stream decoding error. + ProtocolError, in case of error in JSON-RPC protocol. """ if not self.is_query_relevance(): # if requested in timeframe smaller than the original sample rate, return the last known data without interacting with server return self.result_obj.get_latest_dump() else: try: latest_dump = self.decoder.decode( self.server.get_running_info() ) # latest dump is not a dict, but json string. decode it. self.result_obj.update_result_data(latest_dump) return latest_dump except TypeError as inst: raise TypeError('JSON-RPC data decoding failed. Check out incoming JSON stream.') except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def sample_until_condition (self, condition_func, time_between_samples = 5): """ Automatically sets ongoing sampling of TRex data, with sampling rate described by time_between_samples. On each fetched dump, the condition_func is applied on the result objects, and if returns True, the sampling will stop. :parameters: condition_func : function function that operates on result_obj and checks if a condition has been met .. note:: `condition_finc` is applied on `CTRexResult` object. Make sure to design a relevant method. time_between_samples : int determines the time between each sample of the server default value : **5** :return: the first result object (see :class:`CTRexResult` for further details) of the TRex run on which the condition has been met. :raises: + :exc:`UserWarning`, in case the condition_func method condition hasn't been met + :exc:`trex_exceptions.TRexIncompleteRunError`, in case one of failed TRex run (unexpected termination). + :exc:`TypeError`, in case JSON stream decoding error. + ProtocolError, in case of error in JSON-RPC protocol. + :exc:`Exception`, in case the condition_func suffered from any kind of exception """ # make sure TRex is running. raise exceptions here if any self.wait_until_kickoff_finish() try: while self.is_running(): results = self.get_result_obj() if condition_func(results): # if condition satisfied, stop TRex and return result object self.stop_trex() return results time.sleep(time_between_samples) except TRexWarning: # means we're back to Idle state, and didn't meet our condition raise UserWarning("TRex results condition wasn't met during TRex run.") except Exception: # this could come from provided method 'condition_func' raise def sample_to_run_finish (self, time_between_samples = 5): """ Automatically sets automatically sampling of TRex data with sampling rate described by time_between_samples until TRex run finished. :parameters: time_between_samples : int determines the time between each sample of the server default value : **5** :return: the latest result object (see :class:`CTRexResult` for further details) with sampled data. :raises: + :exc:`UserWarning`, in case the condition_func method condition hasn't been met + :exc:`trex_exceptions.TRexIncompleteRunError`, in case one of failed TRex run (unexpected termination). + :exc:`TypeError`, in case JSON stream decoding error. + ProtocolError, in case of error in JSON-RPC protocol. """ self.wait_until_kickoff_finish() try: while self.is_running(): time.sleep(time_between_samples) except TRexWarning: pass results = self.get_result_obj() return results def sample_x_seconds (self, sample_time, time_between_samples = 5): """ Automatically sets ongoing sampling of TRex data for sample_time seconds, with sampling rate described by time_between_samples. Does not stop the TRex afterwards! .. tip:: Useful for changing the device (Router, ASA etc.) configuration after given time. :parameters: sample_time : int sample the TRex this number of seconds time_between_samples : int determines the time between each sample of the server default value : **5** :return: the first result object (see :class:`CTRexResult` for further details) of the TRex run after given sample_time. :raises: + :exc:`UserWarning`, in case the TRex run ended before sample_time duration + :exc:`trex_exceptions.TRexIncompleteRunError`, in case one of failed TRex run (unexpected termination). + :exc:`TypeError`, in case JSON stream decoding error. + ProtocolError, in case of error in JSON-RPC protocol. """ # make sure TRex is running. raise exceptions here if any self.wait_until_kickoff_finish() elapsed_time = 0 while self.is_running(): if elapsed_time >= sample_time: return self.get_result_obj() time.sleep(time_between_samples) elapsed_time += time_between_samples raise UserWarning("TRex has stopped at %s seconds (before expected %s seconds)\nTry increasing test duration or decreasing sample_time" % (elapsed_time, sample_time)) def get_result_obj (self, copy_obj = True): """ Returns the result object of the trex_client's instance. By default, returns a **copy** of the objects (so that changes to the original object are masked). :parameters: copy_obj : bool False means that a reference to the original (possibly changing) object are passed defaul value : **True** :return: the latest result object (see :class:`CTRexResult` for further details) with sampled data. """ if copy_obj: return copy.deepcopy(self.result_obj) else: return self.result_obj def is_reserved (self): """ Checks if TRex is currently reserved to any user or not. :parameters: None :return: + **True** if TRex is reserved. + **False** otherwise. :raises: ProtocolError, in case of error in JSON-RPC protocol. """ try: return self.server.is_reserved() except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def get_trex_daemon_log (self): """ Get Trex daemon log. :return: String representation of TRex daemon log :raises: + :exc:`trex_exceptions.TRexRequestDenied`, in case file could not be read. + ProtocolError, in case of error in JSON-RPC protocol. """ try: return binascii.a2b_base64(self.server.get_trex_daemon_log()) except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def get_trex_log (self): """ Get TRex CLI output log :return: String representation of TRex log :raises: + :exc:`trex_exceptions.TRexRequestDenied`, in case file could not be fetched at server side. + ProtocolError, in case of error in JSON-RPC protocol. """ try: return binascii.a2b_base64(self.server.get_trex_log()) except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def get_trex_version (self): """ Get TRex version details. :return: Trex details (Version, User, Date, Uuid, Git SHA) as ordered dictionary :raises: + :exc:`trex_exceptions.TRexRequestDenied`, in case TRex version could not be determined. + ProtocolError, in case of error in JSON-RPC protocol. + General Exception is case one of the keys is missing in response """ try: version_dict = OrderedDict() result_lines = binascii.a2b_base64(self.server.get_trex_version()).split('\n') for line in result_lines: if not line: continue key, value = line.strip().split(':', 1) version_dict[key.strip()] = value.strip() for key in ('Version', 'User', 'Date', 'Uuid', 'Git SHA'): if key not in version_dict: raise Exception('get_trex_version: got server response without key: {0}'.format(key)) return version_dict except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def reserve_trex (self, user = None): """ Reserves the usage of TRex to a certain user. When TRex is reserved, it can't be reserved. :parameters: user : str a username of the desired owner of TRex default: current logged user :return: **True** if reservation made successfully :raises: + :exc:`trex_exceptions.TRexRequestDenied`, in case TRex is reserved for another user than the one trying to make the reservation. + :exc:`trex_exceptions.TRexInUseError`, in case TRex is currently running. + ProtocolError, in case of error in JSON-RPC protocol. """ username = user or self.__default_user try: return self.server.reserve_trex(user = username) except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def cancel_reservation (self, user = None): """ Cancels a current reservation of TRex to a certain user. When TRex is reserved, no other user can start new TRex runs. :parameters: user : str a username of the desired owner of TRex default: current logged user :return: + **True** if reservation canceled successfully, + **False** if there was no reservation at all. :raises: + :exc:`trex_exceptions.TRexRequestDenied`, in case TRex is reserved for another user than the one trying to cancel the reservation. + ProtocolError, in case of error in JSON-RPC protocol. """ username = user or self.__default_user try: return self.server.cancel_reservation(user = username) except AppError as err: self._handle_AppError_exception(err.args[0]) except ProtocolError: raise finally: self.prompt_verbose_data() def push_files (self, filepaths): """ Pushes a file (or a list of files) to store locally on server. :parameters: filepaths : str or list a path to a file to be pushed to server. if a list of paths is passed, all of those will be pushed to server :return: + **True** if file(s) copied successfully. + **False** otherwise. :raises: + :exc:`IOError`, in case specified file wasn't found or could not be accessed. + ProtocolError, in case of error in JSON-RPC protocol. """ paths_list = None if isinstance(filepaths, str): paths_list = [filepaths] elif isinstance(filepaths, list): paths_list = filepaths else: raise TypeError("filepaths argument must be of type str or list") for filepath in paths_list: try: if not os.path.exists(filepath): raise IOError(errno.ENOENT, "The requested `{fname}` file wasn't found. Operation aborted.".format( fname = filepath) ) else: filename = os.path.basename(filepath) with open(filepath, 'rb') as f: file_content = f.read() self.server.push_file(filename, binascii.b2a_base64(file_content)) finally: self.prompt_verbose_data() return True def is_query_relevance(self): """ Checks if time between any two consecutive server queries (asking for live running data) passed. .. note:: The allowed minimum time between each two consecutive samples is 0.5 seconds. :parameters: None :return: + **True** if more than 0.5 seconds has been past from last server query. + **False** otherwise. """ cur_time = time.time() if cur_time-self._last_sample < 0.5: return False else: self._last_sample = cur_time return True def call_server_mathod_safely (self, method_to_call): try: return method_to_call() except socket.error as e: if e.errno == errno.ECONNREFUSED: raise SocketError(errno.ECONNREFUSED, "Connection from TRex server was refused. Please make sure the server is up.") def check_server_connectivity (self): """ Checks for server valid connectivity. """ try: socket.gethostbyname(self.trex_host) return self.server.connectivity_check() except socket.gaierror as e: raise socket.gaierror(e.errno, "Could not resolve server hostname. Please make sure hostname entered correctly.") except socket.error as e: if e.errno == errno.ECONNREFUSED: raise socket.error(errno.ECONNREFUSED, "Connection from TRex server was refused. Please make sure the server is up.") finally: self.prompt_verbose_data() def prompt_verbose_data(self): """ This method prompts any verbose data available, only if `verbose` option has been turned on. """ if self.verbose: print ('\n') print ("(*) JSON-RPC request:", self.history.request) print ("(*) JSON-RPC response:", self.history.response) def __verbose_print(self, print_str): """ This private method prints the `print_str` string only in case self.verbose flag is turned on. :parameters: print_str : str a string to be printed :returns: None """ if self.verbose: print (print_str) def _handle_AppError_exception(self, err): """ This private method triggres the TRex dedicated exception generation in case a general ProtocolError has been raised. """ # handle known exceptions based on known error codes. # if error code is not known, raise ProtocolError raise exception_handler.gen_exception(err) class CTRexResult(object): """ A class containing all results received from TRex. Ontop to containing the results, this class offers easier data access and extended results processing options """ def __init__(self, max_history_size): """ Instatiate a TRex result object :parameters: max_history_size : int a number to set the maximum history size of a single TRex run. Each sampling adds a new item to history. """ self._history = deque(maxlen = max_history_size) self.clear_results() self.latency_checked = True def __repr__(self): return ("Is valid history? {arg}\n".format( arg = self.is_valid_hist() ) + "Done warmup? {arg}\n".format( arg = self.is_done_warmup() ) + "Expected tx rate: {arg}\n".format( arg = self.get_expected_tx_rate() ) + "Current tx rate: {arg}\n".format( arg = self.get_current_tx_rate() ) + "Maximum latency: {arg}\n".format( arg = self.get_max_latency() ) + "Average latency: {arg}\n".format( arg = self.get_avg_latency() ) + "Average window latency: {arg}\n".format( arg = self.get_avg_window_latency() ) + "Total drops: {arg}\n".format( arg = self.get_total_drops() ) + "Drop rate: {arg}\n".format( arg = self.get_drop_rate() ) + "History size so far: {arg}\n".format( arg = len(self._history) ) ) def get_expected_tx_rate (self): """ Fetches the expected TX rate in various units representation :parameters: None :return: dictionary containing the expected TX rate, where the key is the measurement units, and the value is the measurement value. """ return self._expected_tx_rate def get_current_tx_rate (self): """ Fetches the current TX rate in various units representation :parameters: None :return: dictionary containing the current TX rate, where the key is the measurement units, and the value is the measurement value. """ return self._current_tx_rate def get_max_latency (self): """ Fetches the maximum latency measured on each of the interfaces :parameters: None :return: dictionary containing the maximum latency, where the key is the measurement interface (`c` indicates client), and the value is the measurement value. """ return self._max_latency def get_avg_latency (self): """ Fetches the average latency measured on each of the interfaces from the start of TRex run :parameters: None :return: dictionary containing the average latency, where the key is the measurement interface (`c` indicates client), and the value is the measurement value. The `all` key represents the average of all interfaces' average """ return self._avg_latency def get_avg_window_latency (self): """ Fetches the average latency measured on each of the interfaces from all the sampled currently stored in window. :parameters: None :return: dictionary containing the average latency, where the key is the measurement interface (`c` indicates client), and the value is the measurement value. The `all` key represents the average of all interfaces' average """ return self._avg_window_latency def get_total_drops (self): """ Fetches the total number of drops identified from the moment TRex run began. :parameters: None :return: total drops count (as int) """ return self._total_drops def get_drop_rate (self): """ Fetches the most recent drop rate in pkts/sec units. :parameters: None :return: current drop rate (as float) """ return self._drop_rate def is_valid_hist (self): """ Checks if result obejct contains valid data. :parameters: None :return: + **True** if history is valid. + **False** otherwise. """ return self.valid def set_valid_hist (self, valid_stat = True): """ Sets result obejct validity status. :parameters: valid_stat : bool defines the validity status dafault value : **True** :return: None """ self.valid = valid_stat def is_done_warmup (self): """ Checks if TRex latest results TX-rate indicates that TRex has reached its expected TX-rate. :parameters: None :return: + **True** if expected TX-rate has been reached. + **False** otherwise. """ return self._done_warmup def get_last_value (self, tree_path_to_key, regex = None): """ A dynamic getter from the latest sampled data item stored in the result object. :parameters: tree_path_to_key : str defines a path to desired data. .. tip:: | Use '.' to enter one level deeper in dictionary hierarchy. | Use '[i]' to access the i'th indexed object of an array. tree_path_to_key : regex apply a regex to filter results out from a multiple results set. Filter applies only on keys of dictionary type. dafault value : **None** :return: + a list of values relevant to the specified path + None if no results were fetched or the history isn't valid. """ if not self.is_valid_hist(): return None else: return CTRexResult.__get_value_by_path(self._history[len(self._history)-1], tree_path_to_key, regex) def get_value_list (self, tree_path_to_key, regex = None, filter_none = True): """ A dynamic getter from all sampled data items stored in the result object. :parameters: tree_path_to_key : str defines a path to desired data. .. tip:: | Use '.' to enter one level deeper in dictionary hierarchy. | Use '[i]' to access the i'th indexed object of an array. tree_path_to_key : regex apply a regex to filter results out from a multiple results set. Filter applies only on keys of dictionary type. dafault value : **None** filter_none : bool specify if None results should be filtered out or not. dafault value : **True** :return: + a list of values relevant to the specified path. Each item on the list refers to a single server sample. + None if no results were fetched or the history isn't valid. """ if not self.is_valid_hist(): return None else: raw_list = list( map(lambda x: CTRexResult.__get_value_by_path(x, tree_path_to_key, regex), self._history) ) if filter_none: return list (filter(lambda x: x!=None, raw_list) ) else: return raw_list def get_latest_dump(self): """ A getter to the latest sampled data item stored in the result object. :parameters: None :return: + a dictionary of the latest data item + an empty dictionary if history is empty. """ history_size = len(self._history) if history_size != 0: return self._history[len(self._history) - 1] else: return {} def update_result_data (self, latest_dump): """ Integrates a `latest_dump` dictionary into the CTRexResult object. :parameters: latest_dump : dict a dictionary with the items desired to be integrated into the object history and stats :return: None """ # add latest dump to history if latest_dump != {}: self._history.append(latest_dump) if not self.valid: self.valid = True # parse important fields and calculate averages and others if self._expected_tx_rate is None: # get the expected data only once since it doesn't change self._expected_tx_rate = CTRexResult.__get_value_by_path(latest_dump, "trex-global.data", "m_tx_expected_\w+") self._current_tx_rate = CTRexResult.__get_value_by_path(latest_dump, "trex-global.data", "m_tx_(?!expected_)\w+") if not self._done_warmup and self._expected_tx_rate is not None: # check for up to 2% change between expected and actual if (self._current_tx_rate['m_tx_bps']/self._expected_tx_rate['m_tx_expected_bps'] > 0.98): self._done_warmup = True # handle latency data if self.latency_checked: latency_pre = "trex-latency" self._max_latency = self.get_last_value("{latency}.data".format(latency = latency_pre), "max-")#None # TBC # support old typo if self._max_latency is None: latency_pre = "trex-latecny" self._max_latency = self.get_last_value("{latency}.data".format(latency = latency_pre), "max-") self._avg_latency = self.get_last_value("{latency}.data".format(latency = latency_pre), "avg-")#None # TBC self._avg_latency = CTRexResult.__avg_all_and_rename_keys(self._avg_latency) avg_win_latency_list = self.get_value_list("{latency}.data".format(latency = latency_pre), "avg-") self._avg_window_latency = CTRexResult.__calc_latency_win_stats(avg_win_latency_list) tx_pkts = CTRexResult.__get_value_by_path(latest_dump, "trex-global.data.m_total_tx_pkts") rx_pkts = CTRexResult.__get_value_by_path(latest_dump, "trex-global.data.m_total_rx_pkts") if tx_pkts is not None and rx_pkts is not None: self._total_drops = tx_pkts - rx_pkts self._drop_rate = CTRexResult.__get_value_by_path(latest_dump, "trex-global.data.m_rx_drop_bps") def clear_results (self): """ Clears all results and sets the history's validity to `False` :parameters: None :return: None """ self.valid = False self._done_warmup = False self._expected_tx_rate = None self._current_tx_rate = None self._max_latency = None self._avg_latency = None self._avg_window_latency = None self._total_drops = None self._drop_rate = None self._history.clear() @staticmethod def __get_value_by_path (dct, tree_path, regex = None): try: for i, p in re.findall(r'(\d+)|([\w|-]+)', tree_path): dct = dct[p or int(i)] if regex is not None and isinstance(dct, dict): res = {} for key,val in dct.items(): match = re.match(regex, key) if match: res[key]=val return res else: return dct except (KeyError, TypeError): return None @staticmethod def __calc_latency_win_stats (latency_win_list): res = {'all' : None } port_dict = {'all' : []} list( map(lambda x: CTRexResult.__update_port_dict(x, port_dict), latency_win_list) ) # finally, calculate everages for each list res['all'] = float("%.3f" % (sum(port_dict['all'])/float(len(port_dict['all']))) ) port_dict.pop('all') for port, avg_list in port_dict.items(): res[port] = float("%.3f" % (sum(avg_list)/float(len(avg_list))) ) return res @staticmethod def __update_port_dict (src_avg_dict, dest_port_dict): all_list = src_avg_dict.values() dest_port_dict['all'].extend(all_list) for key, val in src_avg_dict.items(): reg_res = re.match("avg-(\d+)", key) if reg_res: tmp_key = "port"+reg_res.group(1) if tmp_key in dest_port_dict: dest_port_dict[tmp_key].append(val) else: dest_port_dict[tmp_key] = [val] @staticmethod def __avg_all_and_rename_keys (src_dict): res = {} all_list = src_dict.values() res['all'] = float("%.3f" % (sum(all_list)/float(len(all_list))) ) for key, val in src_dict.items(): reg_res = re.match("avg-(\d+)", key) if reg_res: tmp_key = "port"+reg_res.group(1) res[tmp_key] = val # don't touch original fields values return res if __name__ == "__main__": pass