summaryrefslogtreecommitdiffstats
path: root/src/plugins/dpdk/ipsec/esp_encrypt.c
blob: fb3f2a41b48a59f43dba683c7e60605ad6da1755 (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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
 * esp_encrypt.c : IPSec ESP encrypt node using DPDK Cryptodev
 *
 * Copyright (c) 2017 Intel 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>
#include <vnet/ipsec/esp.h>
#include <vnet/udp/udp.h>
#include <dpdk/ipsec/ipsec.h>
#include <dpdk/device/dpdk.h>
#include <dpdk/device/dpdk_priv.h>

#define foreach_esp_encrypt_next                   \
_(DROP, "error-drop")                              \
_(IP4_LOOKUP, "ip4-lookup")                        \
_(IP6_LOOKUP, "ip6-lookup")                        \
_(INTERFACE_OUTPUT, "interface-output")

#define _(v, s) ESP_ENCRYPT_NEXT_##v,
typedef enum
{
  foreach_esp_encrypt_next
#undef _
    ESP_ENCRYPT_N_NEXT,
} esp_encrypt_next_t;

#define foreach_esp_encrypt_error                   \
 _(RX_PKTS, "ESP pkts received")                    \
 _(SEQ_CYCLED, "Sequence number cycled")            \
 _(ENQ_FAIL, "Enqueue failed to crypto device")     \
 _(DISCARD, "Not enough crypto operations, discarding frame")  \
 _(SESSION, "Failed to get crypto session")         \
 _(NOSUP, "Cipher/Auth not supported")


typedef enum
{
#define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
  foreach_esp_encrypt_error
#undef _
    ESP_ENCRYPT_N_ERROR,
} esp_encrypt_error_t;

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

vlib_node_registration_t dpdk_esp4_encrypt_node;
vlib_node_registration_t dpdk_esp6_encrypt_node;

typedef struct
{
  ipsec_crypto_alg_t crypto_alg;
  ipsec_integ_alg_t integ_alg;
  u8 packet_data[64];
} esp_encrypt_trace_t;

/* packet trace format function */
static u8 *
format_esp_encrypt_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 *);
  esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
  ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
  u32 indent = format_get_indent (s), offset;

  s = format (s, "cipher %U auth %U\n",
	      format_ipsec_crypto_alg, t->crypto_alg,
	      format_ipsec_integ_alg, t->integ_alg);

  if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
    {
      s = format (s, "%U%U", format_white_space, indent,
		  format_ip6_header, ih4);
      offset = sizeof (ip6_header_t);
    }
  else
    {
      s = format (s, "%U%U", format_white_space, indent,
		  format_ip4_header, ih4);
      offset = ip4_header_bytes (ih4);
    }

  s = format (s, "\n%U%U", format_white_space, indent,
	      format_esp_header, t->packet_data + offset);

  return s;
}

always_inline uword
dpdk_esp_encrypt_inline (vlib_main_t * vm,
			 vlib_node_runtime_t * node,
			 vlib_frame_t * from_frame, int is_ip6)
{
  u32 n_left_from, *from, *to_next, next_index;
  ipsec_main_t *im = &ipsec_main;
  u32 thread_idx = vlib_get_thread_index ();
  dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
  crypto_resource_t *res = 0;
  ipsec_sa_t *sa0 = 0;
  crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
  struct rte_cryptodev_sym_session *session = 0;
  u32 ret, last_sa_index = ~0;
  u8 numa = rte_socket_id ();
  u8 is_aead = 0;
  crypto_worker_main_t *cwm =
    vec_elt_at_index (dcm->workers_main, thread_idx);
  struct rte_crypto_op **ops = cwm->ops;

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

  ret = crypto_alloc_ops (numa, ops, n_left_from);
  if (ret)
    {
      if (is_ip6)
	vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index,
				     ESP_ENCRYPT_ERROR_DISCARD, 1);
      else
	vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index,
				     ESP_ENCRYPT_ERROR_DISCARD, 1);
      /* Discard whole frame */
      return n_left_from;
    }

  next_index = ESP_ENCRYPT_NEXT_DROP;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  clib_error_t *error;
	  u32 bi0;
	  vlib_buffer_t *b0 = 0;
	  u32 sa_index0;
	  ip4_and_esp_header_t *ih0, *oh0 = 0;
	  ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
	  ip4_and_udp_and_esp_header_t *ouh0 = 0;
	  esp_header_t *esp0;
	  esp_footer_t *f0;
	  u8 next_hdr_type;
	  u32 iv_size;
	  u16 orig_sz;
	  u8 trunc_size;
	  u16 rewrite_len;
	  u16 udp_encap_adv = 0;
	  struct rte_mbuf *mb0 = 0;
	  struct rte_crypto_op *op;
	  u16 res_idx;

	  bi0 = from[0];
	  from += 1;
	  n_left_from -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  ih0 = vlib_buffer_get_current (b0);
	  mb0 = rte_mbuf_from_vlib_buffer (b0);

	  /* ih0/ih6_0 */
	  CLIB_PREFETCH (ih0, sizeof (ih6_0[0]), LOAD);
	  /* f0 */
	  CLIB_PREFETCH (vlib_buffer_get_tail (b0), 20, STORE);
	  /* mb0 */
	  CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);

	  op = ops[0];
	  ops += 1;
	  ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);

	  dpdk_op_priv_t *priv = crypto_op_get_priv (op);

	  u16 op_len =
	    sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
	  CLIB_PREFETCH (op, op_len, STORE);

	  sa_index0 = vnet_buffer (b0)->ipsec.sad_index;

	  if (sa_index0 != last_sa_index)
	    {
	      sa0 = pool_elt_at_index (im->sad, sa_index0);

	      cipher_alg =
		vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
	      auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);

	      is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);

	      if (is_aead)
		auth_alg = cipher_alg;

	      res_idx = get_resource (cwm, sa0);

	      if (PREDICT_FALSE (res_idx == (u16) ~ 0))
		{
		  clib_warning ("unsupported SA by thread index %u",
				thread_idx);
		  if (is_ip6)
		    vlib_node_increment_counter (vm,
						 dpdk_esp6_encrypt_node.index,
						 ESP_ENCRYPT_ERROR_NOSUP, 1);
		  else
		    vlib_node_increment_counter (vm,
						 dpdk_esp4_encrypt_node.index,
						 ESP_ENCRYPT_ERROR_NOSUP, 1);
		  to_next[0] = bi0;
		  to_next += 1;
		  n_left_to_next -= 1;
		  goto trace;
		}
	      res = vec_elt_at_index (dcm->resource, res_idx);

	      error = crypto_get_session (&session, sa_index0, res, cwm, 1);
	      if (PREDICT_FALSE (error || !session))
		{
		  clib_warning ("failed to get crypto session");
		  if (is_ip6)
		    vlib_node_increment_counter (vm,
						 dpdk_esp6_encrypt_node.index,
						 ESP_ENCRYPT_ERROR_SESSION,
						 1);
		  else
		    vlib_node_increment_counter (vm,
						 dpdk_esp4_encrypt_node.index,
						 ESP_ENCRYPT_ERROR_SESSION,
						 1);
		  to_next[0] = bi0;
		  to_next += 1;
		  n_left_to_next -= 1;
		  goto trace;
		}

	      last_sa_index = sa_index0;
	    }

	  if (PREDICT_FALSE (esp_seq_advance (sa0)))
	    {
	      clib_warning ("sequence number counter has cycled SPI %u",
			    sa0->spi);
	      if (is_ip6)
		vlib_node_increment_counter (vm,
					     dpdk_esp6_encrypt_node.index,
					     ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
	      else
		vlib_node_increment_counter (vm,
					     dpdk_esp4_encrypt_node.index,
					     ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
	      //TODO: rekey SA
	      to_next[0] = bi0;
	      to_next += 1;
	      n_left_to_next -= 1;
	      goto trace;
	    }

	  orig_sz = b0->current_length;

	  /* TODO multi-seg support - total_length_not_including_first_buffer */
	  sa0->total_data_size += b0->current_length;

	  res->ops[res->n_ops] = op;
	  res->bi[res->n_ops] = bi0;
	  res->n_ops += 1;

	  dpdk_gcm_cnt_blk *icb = &priv->cb;

	  crypto_set_icb (icb, sa0->salt, sa0->seq, sa0->seq_hi);

	  iv_size = cipher_alg->iv_len;
	  trunc_size = auth_alg->trunc_size;

	  /* if UDP encapsulation is used adjust the address of the IP header */
	  if (sa0->udp_encap && !is_ip6)
	    udp_encap_adv = sizeof (udp_header_t);

	  if (sa0->is_tunnel)
	    {
	      rewrite_len = 0;
	      if (!is_ip6 && !sa0->is_tunnel_ip6)	/* ip4inip4 */
		{
		  /* in tunnel mode send it back to FIB */
		  priv->next = DPDK_CRYPTO_INPUT_NEXT_IP4_LOOKUP;
		  u8 adv = sizeof (ip4_header_t) + udp_encap_adv +
		    sizeof (esp_header_t) + iv_size;
		  vlib_buffer_advance (b0, -adv);
		  oh0 = vlib_buffer_get_current (b0);
		  ouh0 = vlib_buffer_get_current (b0);
		  next_hdr_type = IP_PROTOCOL_IP_IN_IP;
		  /*
		   * oh0->ip4.ip_version_and_header_length = 0x45;
		   * oh0->ip4.tos = ih0->ip4.tos;
		   * oh0->ip4.fragment_id = 0;
		   * oh0->ip4.flags_and_fragment_offset = 0;
		   */
		  oh0->ip4.checksum_data_64[0] =
		    clib_host_to_net_u64 (0x45ULL << 56);
		  /*
		   * oh0->ip4.ttl = 254;
		   * oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
		   */
		  oh0->ip4.checksum_data_32[2] =
		    clib_host_to_net_u32 (0xfe320000);

		  oh0->ip4.src_address.as_u32 =
		    sa0->tunnel_src_addr.ip4.as_u32;
		  oh0->ip4.dst_address.as_u32 =
		    sa0->tunnel_dst_addr.ip4.as_u32;

		  if (sa0->udp_encap)
		    {
		      oh0->ip4.protocol = IP_PROTOCOL_UDP;
		      esp0 = &ouh0->esp;
		    }
		  else
		    esp0 = &oh0->esp;
		  esp0->spi = clib_host_to_net_u32 (sa0->spi);
		  esp0->seq = clib_host_to_net_u32 (sa0->seq);
		}
	      else if (is_ip6 && sa0->is_tunnel_ip6)	/* ip6inip6 */
		{
		  /* in tunnel mode send it back to FIB */
		  priv->next = DPDK_CRYPTO_INPUT_NEXT_IP6_LOOKUP;

		  u8 adv =
		    sizeof (ip6_header_t) + sizeof (esp_header_t) + iv_size;
		  vlib_buffer_advance (b0, -adv);
		  ih6_0 = (ip6_and_esp_header_t *) ih0;
		  oh6_0 = vlib_buffer_get_current (b0);

		  next_hdr_type = IP_PROTOCOL_IPV6;

		  oh6_0->ip6.ip_version_traffic_class_and_flow_label =
		    ih6_0->ip6.ip_version_traffic_class_and_flow_label;

		  oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
		  oh6_0->ip6.hop_limit = 254;
		  oh6_0->ip6.src_address.as_u64[0] =
		    sa0->tunnel_src_addr.ip6.as_u64[0];
		  oh6_0->ip6.src_address.as_u64[1] =
		    sa0->tunnel_src_addr.ip6.as_u64[1];
		  oh6_0->ip6.dst_address.as_u64[0] =
		    sa0->tunnel_dst_addr.ip6.as_u64[0];
		  oh6_0->ip6.dst_address.as_u64[1] =
		    sa0->tunnel_dst_addr.ip6.as_u64[1];
		  esp0 = &oh6_0->esp;
		  oh6_0->esp.spi = clib_host_to_net_u32 (sa0->spi);
		  oh6_0->esp.seq = clib_host_to_net_u32 (sa0->seq);
		}
	      else		/* unsupported ip4inip6, ip6inip4 */
		{
		  if (is_ip6)
		    vlib_node_increment_counter (vm,
						 dpdk_esp6_encrypt_node.index,
						 ESP_ENCRYPT_ERROR_NOSUP, 1);
		  else
		    vlib_node_increment_counter (vm,
						 dpdk_esp4_encrypt_node.index,
						 ESP_ENCRYPT_ERROR_NOSUP, 1);
		  to_next[0] = bi0;
		  to_next += 1;
		  n_left_to_next -= 1;
		  goto trace;
		}
	      vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
	    }
	  else			/* transport mode */
	    {
	      priv->next = DPDK_CRYPTO_INPUT_NEXT_INTERFACE_OUTPUT;
	      rewrite_len = vnet_buffer (b0)->ip.save_rewrite_length;
	      u16 adv = sizeof (esp_header_t) + iv_size + udp_encap_adv;
	      vlib_buffer_advance (b0, -adv - rewrite_len);
	      u8 *src = ((u8 *) ih0) - rewrite_len;
	      u8 *dst = vlib_buffer_get_current (b0);
	      oh0 = vlib_buffer_get_current (b0) + rewrite_len;

	      if (is_ip6)
		{
		  orig_sz -= sizeof (ip6_header_t);
		  ih6_0 = (ip6_and_esp_header_t *) ih0;
		  next_hdr_type = ih6_0->ip6.protocol;
		  memmove (dst, src, rewrite_len + sizeof (ip6_header_t));
		  oh6_0 = (ip6_and_esp_header_t *) oh0;
		  oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
		  esp0 = &oh6_0->esp;
		}
	      else		/* ipv4 */
		{
		  u16 ip_size = ip4_header_bytes (&ih0->ip4);
		  orig_sz -= ip_size;
		  next_hdr_type = ih0->ip4.protocol;
		  memmove (dst, src, rewrite_len + ip_size);
		  oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
		  esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
		  if (sa0->udp_encap)
		    {
		      oh0->ip4.protocol = IP_PROTOCOL_UDP;
		      esp0 = (esp_header_t *)
			(((u8 *) oh0) + ip_size + udp_encap_adv);
		    }
		  else
		    {
		      oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
		      esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
		    }
		}
	      esp0->spi = clib_host_to_net_u32 (sa0->spi);
	      esp0->seq = clib_host_to_net_u32 (sa0->seq);
	    }

	  if (sa0->udp_encap && ouh0)
	    {
	      ouh0->udp.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
	      ouh0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
	      ouh0->udp.checksum = 0;
	    }
	  ASSERT (is_pow2 (cipher_alg->boundary));
	  u16 mask = cipher_alg->boundary - 1;
	  u16 pad_payload_len = ((orig_sz + 2) + mask) & ~mask;
	  u8 pad_bytes = pad_payload_len - 2 - orig_sz;

	  u8 *padding =
	    vlib_buffer_put_uninit (b0, pad_bytes + 2 + trunc_size);

	  /* The extra pad bytes would be overwritten by the digest */
	  if (pad_bytes)
	    clib_memcpy (padding, pad_data, 16);

	  f0 = (esp_footer_t *) (padding + pad_bytes);
	  f0->pad_length = pad_bytes;
	  f0->next_header = next_hdr_type;

	  if (is_ip6)
	    {
	      u16 len = b0->current_length - sizeof (ip6_header_t);
	      oh6_0->ip6.payload_length =
		clib_host_to_net_u16 (len - rewrite_len);
	    }
	  else
	    {
	      oh0->ip4.length =
		clib_host_to_net_u16 (b0->current_length - rewrite_len);
	      oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
	      if (sa0->udp_encap && ouh0)
		{
		  ouh0->udp.length =
		    clib_host_to_net_u16 (clib_net_to_host_u16
					  (ouh0->ip4.length) -
					  ip4_header_bytes (&ouh0->ip4));
		}
	    }

	  vnet_buffer (b0)->sw_if_index[VLIB_RX] =
	    vnet_buffer (b0)->sw_if_index[VLIB_RX];
	  b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;

	  /* mbuf packet starts at ESP header */
	  mb0->data_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
	  mb0->pkt_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
	  mb0->data_off = ((void *) esp0) - mb0->buf_addr;

	  u32 cipher_off, cipher_len, auth_len = 0;
	  u32 *aad = NULL;

	  u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
	  u64 digest_paddr =
	    mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);

	  if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
	    {
	      cipher_off = sizeof (esp_header_t);
	      cipher_len = iv_size + pad_payload_len;
	    }
	  else			/* CTR/GCM */
	    {
	      u32 *esp_iv = (u32 *) (esp0 + 1);
	      esp_iv[0] = sa0->seq;
	      esp_iv[1] = sa0->seq_hi;

	      cipher_off = sizeof (esp_header_t) + iv_size;
	      cipher_len = pad_payload_len;
	    }

	  if (is_aead)
	    {
	      aad = (u32 *) priv->aad;
	      aad[0] = clib_host_to_net_u32 (sa0->spi);
	      aad[1] = clib_host_to_net_u32 (sa0->seq);

	      /* aad[3] should always be 0 */
	      if (PREDICT_FALSE (sa0->use_esn))
		aad[2] = clib_host_to_net_u32 (sa0->seq_hi);
	      else
		aad[2] = 0;
	    }
	  else
	    {
	      auth_len =
		vlib_buffer_get_tail (b0) - ((u8 *) esp0) - trunc_size;
	      if (sa0->use_esn)
		{
		  u32 *_digest = (u32 *) digest;
		  _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
		  auth_len += 4;
		}
	    }

	  crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
			   0, auth_len, (u8 *) aad, digest, digest_paddr);

	trace:
	  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
	    {
	      esp_encrypt_trace_t *tr =
		vlib_add_trace (vm, node, b0, sizeof (*tr));
	      tr->crypto_alg = sa0->crypto_alg;
	      tr->integ_alg = sa0->integ_alg;
	      u8 *p = vlib_buffer_get_current (b0);
	      if (!sa0->is_tunnel)
		p += vnet_buffer (b0)->ip.save_rewrite_length;
	      clib_memcpy (tr->packet_data, p, sizeof (tr->packet_data));
	    }
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
  if (is_ip6)
    {
      vlib_node_increment_counter (vm, dpdk_esp6_encrypt_node.index,
				   ESP_ENCRYPT_ERROR_RX_PKTS,
				   from_frame->n_vectors);

      crypto_enqueue_ops (vm, cwm, 1, dpdk_esp6_encrypt_node.index,
			  ESP_ENCRYPT_ERROR_ENQ_FAIL, numa);
    }
  else
    {
      vlib_node_increment_counter (vm, dpdk_esp4_encrypt_node.index,
				   ESP_ENCRYPT_ERROR_RX_PKTS,
				   from_frame->n_vectors);

      crypto_enqueue_ops (vm, cwm, 1, dpdk_esp4_encrypt_node.index,
			  ESP_ENCRYPT_ERROR_ENQ_FAIL, numa);
    }

  crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);

  return from_frame->n_vectors;
}

VLIB_NODE_FN (dpdk_esp4_encrypt_node) (vlib_main_t * vm,
				       vlib_node_runtime_t * node,
				       vlib_frame_t * from_frame)
{
  return dpdk_esp_encrypt_inline (vm, node, from_frame, 0 /*is_ip6 */ );
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (dpdk_esp4_encrypt_node) = {
  .name = "dpdk-esp4-encrypt",
  .flags = VLIB_NODE_FLAG_IS_OUTPUT,
  .vector_size = sizeof (u32),
  .format_trace = format_esp_encrypt_trace,
  .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
  .error_strings = esp_encrypt_error_strings,
  .n_next_nodes = 1,
  .next_nodes =
    {
      [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
    }
};
/* *INDENT-ON* */

VLIB_NODE_FN (dpdk_esp6_encrypt_node) (vlib_main_t * vm,
				       vlib_node_runtime_t * node,
				       vlib_frame_t * from_frame)
{
  return dpdk_esp_encrypt_inline (vm, node, from_frame, 1 /*is_ip6 */ );
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (dpdk_esp6_encrypt_node) = {
  .name = "dpdk-esp6-encrypt",
  .flags = VLIB_NODE_FLAG_IS_OUTPUT,
  .vector_size = sizeof (u32),
  .format_trace = format_esp_encrypt_trace,
  .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
  .error_strings = esp_encrypt_error_strings,
  .n_next_nodes = 1,
  .next_nodes =
    {
      [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
    }
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
on_t *s; u8 *appns_id; int error; /* * Create the loopbacks */ intf_addr[0].as_u32 = clib_host_to_net_u32 (0x01010101), session_create_lookpback (0, &sw_if_index[0], &intf_addr[0]); intf_addr[1].as_u32 = clib_host_to_net_u32 (0x02020202), session_create_lookpback (1, &sw_if_index[1], &intf_addr[1]); session_add_del_route_via_lookup_in_table (0, 1, &intf_addr[1], 32, 1 /* is_add */ ); session_add_del_route_via_lookup_in_table (1, 0, &intf_addr[0], 32, 1 /* is_add */ ); /* * Insert namespace */ appns_id = format (0, "appns1"); vnet_app_namespace_add_del_args_t ns_args = { .ns_id = appns_id, .secret = placeholder_secret, .sw_if_index = sw_if_index[1], .ip4_fib_id = 0, .is_add = 1 }; error = vnet_app_namespace_add_del (&ns_args); SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error); /* * Attach client/server */ clib_memset (options, 0, sizeof (options)); options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; vnet_app_attach_args_t attach_args = { .api_client_index = ~0, .options = options, .namespace_id = 0, .session_cb_vft = &placeholder_session_cbs, .name = format (0, "session_test_client"), }; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "client app attached"); client_index = attach_args.app_index; vec_free (attach_args.name); attach_args.name = format (0, "session_test_server"); attach_args.namespace_id = appns_id; attach_args.options[APP_OPTIONS_NAMESPACE_SECRET] = placeholder_secret; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server app attached: %U", format_clib_error, error); vec_free (attach_args.name); server_index = attach_args.app_index; server_sep.is_ip4 = 1; server_sep.port = placeholder_server_port; vnet_listen_args_t bind_args = { .sep_ext = server_sep, .app_index = server_index, }; error = vnet_listen (&bind_args); SESSION_TEST ((error == 0), "server bind should work"); /* * Connect and force lcl ip */ client_sep.is_ip4 = 1; client_sep.ip.ip4.as_u32 = clib_host_to_net_u32 (0x02020202); client_sep.port = placeholder_server_port; client_sep.peer.is_ip4 = 1; client_sep.peer.ip.ip4.as_u32 = clib_host_to_net_u32 (0x01010101); client_sep.peer.port = placeholder_client_port; client_sep.transport_proto = TRANSPORT_PROTO_TCP; vnet_connect_args_t connect_args = { .sep_ext = client_sep, .app_index = client_index, }; connected_session_index = connected_session_thread = ~0; accepted_session_index = accepted_session_thread = ~0; error = vnet_connect (&connect_args); SESSION_TEST ((error == 0), "connect should work"); /* wait for stuff to happen */ while (connected_session_index == ~0 && ++tries < 100) { vlib_worker_thread_barrier_release (vm); vlib_process_suspend (vm, 100e-3); vlib_worker_thread_barrier_sync (vm); } while (accepted_session_index == ~0 && ++tries < 100) { vlib_worker_thread_barrier_release (vm); vlib_process_suspend (vm, 100e-3); vlib_worker_thread_barrier_sync (vm); } clib_warning ("waited %.1f seconds for connections", tries / 10.0); SESSION_TEST ((connected_session_index != ~0), "session should exist"); SESSION_TEST ((connected_session_thread != ~0), "thread should exist"); SESSION_TEST ((accepted_session_index != ~0), "session should exist"); SESSION_TEST ((accepted_session_thread != ~0), "thread should exist"); s = session_get (connected_session_index, connected_session_thread); tc = session_get_transport (s); SESSION_TEST ((tc != 0), "transport should exist"); SESSION_TEST ((memcmp (&tc->lcl_ip, &client_sep.peer.ip, sizeof (tc->lcl_ip)) == 0), "ips should be equal"); SESSION_TEST ((tc->lcl_port == placeholder_client_port), "ports should be equal"); /* These sessions, because of the way they're established are pinned to * main thread, even when we have workers and we avoid polling main thread, * i.e., we can't cleanup pending disconnects, so force cleanup for both */ session_transport_cleanup (s); s = session_get (accepted_session_index, accepted_session_thread); session_transport_cleanup (s); vnet_app_detach_args_t detach_args = { .app_index = server_index, .api_client_index = ~0, }; vnet_application_detach (&detach_args); detach_args.app_index = client_index; vnet_application_detach (&detach_args); /* Allow the disconnects to finish before removing the routes. */ vlib_process_suspend (vm, 10e-3); session_add_del_route_via_lookup_in_table (0, 1, &intf_addr[1], 32, 0 /* is_add */ ); session_add_del_route_via_lookup_in_table (1, 0, &intf_addr[0], 32, 0 /* is_add */ ); session_delete_loopback (sw_if_index[0]); session_delete_loopback (sw_if_index[1]); return 0; } static int session_test_namespace (vlib_main_t * vm, unformat_input_t * input) { u64 options[APP_OPTIONS_N_OPTIONS], placeholder_secret = 1234, tries; u32 server_index, server_st_index, server_local_st_index; u32 placeholder_port = 1234, client_index, server_wrk_index; u32 placeholder_api_context = 4321, placeholder_client_api_index = ~0; u32 placeholder_server_api_index = ~0, sw_if_index = 0; session_endpoint_t server_sep = SESSION_ENDPOINT_NULL; session_endpoint_t client_sep = SESSION_ENDPOINT_NULL; session_endpoint_t intf_sep = SESSION_ENDPOINT_NULL; u8 *ns_id, *server_name, *client_name; app_namespace_t *app_ns; application_t *server; session_t *s; u64 handle; int error = 0; ns_id = format (0, "appns1"); server_name = format (0, "session_test"); client_name = format (0, "session_test_client"); server_sep.is_ip4 = 1; server_sep.port = placeholder_port; client_sep.is_ip4 = 1; client_sep.port = placeholder_port; clib_memset (options, 0, sizeof (options)); options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; vnet_app_attach_args_t attach_args = { .api_client_index = ~0, .options = options, .namespace_id = 0, .session_cb_vft = &placeholder_session_cbs, .name = server_name, }; vnet_listen_args_t bind_args = { .sep = server_sep, .app_index = 0, }; vnet_connect_args_t connect_args = { .app_index = 0, .api_context = 0, }; clib_memcpy (&connect_args.sep, &client_sep, sizeof (client_sep)); vnet_unlisten_args_t unbind_args = { .handle = bind_args.handle, .app_index = 0, }; vnet_app_detach_args_t detach_args = { .app_index = 0, .api_client_index = ~0, }; ip4_address_t intf_addr = { .as_u32 = clib_host_to_net_u32 (0x07000105), }; intf_sep.ip.ip4 = intf_addr; intf_sep.is_ip4 = 1; intf_sep.port = placeholder_port; /* * Insert namespace and lookup */ vnet_app_namespace_add_del_args_t ns_args = { .ns_id = ns_id, .secret = placeholder_secret, .sw_if_index = APP_NAMESPACE_INVALID_INDEX, .is_add = 1 }; error = vnet_app_namespace_add_del (&ns_args); SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error); app_ns = app_namespace_get_from_id (ns_id); SESSION_TEST ((app_ns != 0), "should find ns %v status", ns_id); SESSION_TEST ((app_ns->ns_secret == placeholder_secret), "secret should be %d", placeholder_secret); SESSION_TEST ((app_ns->sw_if_index == APP_NAMESPACE_INVALID_INDEX), "sw_if_index should be invalid"); /* * Try application attach with wrong secret */ options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; options[APP_OPTIONS_NAMESPACE_SECRET] = placeholder_secret - 1; attach_args.namespace_id = ns_id; attach_args.api_client_index = placeholder_server_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error != 0), "app attachment should fail"); SESSION_TEST ((error == VNET_API_ERROR_APP_WRONG_NS_SECRET), "code should be wrong ns secret: %d", error); /* * Attach server with global default scope */ options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; options[APP_OPTIONS_NAMESPACE_SECRET] = 0; attach_args.namespace_id = 0; attach_args.api_client_index = placeholder_server_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; server = application_get (server_index); server_wrk_index = application_get_default_worker (server)->wrk_index; SESSION_TEST ((server->ns_index == 0), "server should be in the default ns"); bind_args.app_index = server_index; error = vnet_listen (&bind_args); SESSION_TEST ((error == 0), "server bind should work"); server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s != 0), "listener should exist in global table"); SESSION_TEST ((s->app_wrk_index == server_wrk_index), "app_index should be" " that of the server"); server_local_st_index = application_local_session_table (server); SESSION_TEST ((server_local_st_index == APP_INVALID_INDEX), "server shouldn't have access to local table"); unbind_args.app_index = server_index; unbind_args.handle = bind_args.handle; error = vnet_unlisten (&unbind_args); SESSION_TEST ((error == 0), "unbind should work"); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s == 0), "listener should not exist in global table"); detach_args.app_index = server_index; vnet_application_detach (&detach_args); /* * Attach server with local and global scope */ options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; options[APP_OPTIONS_NAMESPACE_SECRET] = placeholder_secret; attach_args.namespace_id = ns_id; attach_args.api_client_index = placeholder_server_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; server = application_get (server_index); server_wrk_index = application_get_default_worker (server)->wrk_index; SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)), "server should be in the right ns"); bind_args.app_index = server_index; error = vnet_listen (&bind_args); SESSION_TEST ((error == 0), "bind should work"); server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s != 0), "listener should exist in global table"); SESSION_TEST ((s->app_wrk_index == server_wrk_index), "app_index should be" " that of the server"); server_local_st_index = application_local_session_table (server); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle != SESSION_INVALID_HANDLE), "listener should exist in local table"); /* * Try client connect with 1) local scope 2) global scope */ options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; attach_args.name = client_name; attach_args.api_client_index = placeholder_client_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "client attachment should work"); client_index = attach_args.app_index; connect_args.api_context = placeholder_api_context; connect_args.app_index = client_index; error = vnet_connect (&connect_args); SESSION_TEST ((error != 0), "client connect should return error code"); SESSION_TEST ((error == SESSION_E_INVALID_RMT_IP), "error code should be invalid value (zero ip)"); SESSION_TEST ((placeholder_segment_count == 0), "shouldn't have received request to map new segment"); connect_args.sep.ip.ip4.as_u8[0] = 127; error = vnet_connect (&connect_args); SESSION_TEST ((error == 0), "client connect should not return error code"); /* wait for accept */ if (vlib_num_workers ()) { tries = 0; while (!placeholder_accept && ++tries < 100) { vlib_worker_thread_barrier_release (vm); vlib_process_suspend (vm, 100e-3); vlib_worker_thread_barrier_sync (vm); } } SESSION_TEST ((placeholder_segment_count == 1), "should've received request to map new segment"); SESSION_TEST ((placeholder_accept == 1), "should've received accept request"); detach_args.app_index = client_index; vnet_application_detach (&detach_args); options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; attach_args.api_client_index = placeholder_client_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "client attachment should work"); error = vnet_connect (&connect_args); SESSION_TEST ((error != 0), "client connect should return error code"); SESSION_TEST ((error == SESSION_E_NOINTF), "error code should be connect (nothing in local scope)"); detach_args.app_index = client_index; vnet_application_detach (&detach_args); /* * Unbind and detach server and then re-attach with local scope only */ unbind_args.handle = bind_args.handle; unbind_args.app_index = server_index; error = vnet_unlisten (&unbind_args); SESSION_TEST ((error == 0), "unbind should work"); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s == 0), "listener should not exist in global table"); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle == SESSION_INVALID_HANDLE), "listener should not exist in local table"); detach_args.app_index = server_index; vnet_application_detach (&detach_args); options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; attach_args.api_client_index = placeholder_server_api_index; attach_args.name = server_name; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "app attachment should work"); server_index = attach_args.app_index; server = application_get (server_index); SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)), "app should be in the right ns"); bind_args.app_index = server_index; error = vnet_listen (&bind_args); SESSION_TEST ((error == 0), "bind should work"); server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s == 0), "listener should not exist in global table"); server_local_st_index = application_local_session_table (server); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle != SESSION_INVALID_HANDLE), "listener should exist in local table"); unbind_args.handle = bind_args.handle; error = vnet_unlisten (&unbind_args); SESSION_TEST ((error == 0), "unbind should work"); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle == SESSION_INVALID_HANDLE), "listener should not exist in local table"); /* * Client attach + connect in default ns with local scope */ options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; attach_args.namespace_id = 0; attach_args.api_client_index = placeholder_client_api_index; attach_args.name = client_name; vnet_application_attach (&attach_args); error = vnet_connect (&connect_args); SESSION_TEST ((error != 0), "client connect should return error code"); SESSION_TEST ((error == SESSION_E_NOROUTE), "error code should be noroute (not in same ns)"); detach_args.app_index = client_index; vnet_application_detach (&detach_args); /* * Detach server */ detach_args.app_index = server_index; vnet_application_detach (&detach_args); /* * Create loopback interface */ session_create_lookpback (0, &sw_if_index, &intf_addr); /* * Update namespace with interface */ ns_args.sw_if_index = sw_if_index; error = vnet_app_namespace_add_del (&ns_args); SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error); /* * Attach server with local and global scope */ options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; options[APP_OPTIONS_NAMESPACE_SECRET] = placeholder_secret; attach_args.namespace_id = ns_id; attach_args.api_client_index = placeholder_server_api_index; attach_args.name = server_name; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; server = application_get (server_index); server_wrk_index = application_get_default_worker (server)->wrk_index; bind_args.app_index = server_index; error = vnet_listen (&bind_args); server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); s = session_lookup_listener (server_st_index, &server_sep); SESSION_TEST ((s == 0), "zero listener should not exist in global table"); s = session_lookup_listener (server_st_index, &intf_sep); SESSION_TEST ((s != 0), "intf listener should exist in global table"); SESSION_TEST ((s->app_wrk_index == server_wrk_index), "app_index should be " "that of the server"); server_local_st_index = application_local_session_table (server); handle = session_lookup_local_endpoint (server_local_st_index, &server_sep); SESSION_TEST ((handle != SESSION_INVALID_HANDLE), "zero listener should exist in local table"); detach_args.app_index = server_index; vnet_application_detach (&detach_args); /* * Cleanup */ vec_free (server_name); vec_free (client_name); vec_free (ns_id); session_delete_loopback (sw_if_index); return 0; } static int session_test_rule_table (vlib_main_t * vm, unformat_input_t * input) { session_rules_table_t _srt, *srt = &_srt; u16 lcl_port = 1234, rmt_port = 4321; u32 action_index = 1, res; ip4_address_t lcl_lkup, rmt_lkup; int verbose = 0, error; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) verbose = 1; else { vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, input); return -1; } } clib_memset (srt, 0, sizeof (*srt)); session_rules_table_init (srt); ip4_address_t lcl_ip = { .as_u32 = clib_host_to_net_u32 (0x01020304), }; ip4_address_t rmt_ip = { .as_u32 = clib_host_to_net_u32 (0x05060708), }; ip4_address_t lcl_ip2 = { .as_u32 = clib_host_to_net_u32 (0x02020202), }; ip4_address_t rmt_ip2 = { .as_u32 = clib_host_to_net_u32 (0x06060606), }; ip4_address_t lcl_ip3 = { .as_u32 = clib_host_to_net_u32 (0x03030303), }; ip4_address_t rmt_ip3 = { .as_u32 = clib_host_to_net_u32 (0x07070707), }; fib_prefix_t lcl_pref = { .fp_addr.ip4.as_u32 = lcl_ip.as_u32, .fp_len = 16, .fp_proto = FIB_PROTOCOL_IP4, }; fib_prefix_t rmt_pref = { .fp_addr.ip4.as_u32 = rmt_ip.as_u32, .fp_len = 16, .fp_proto = FIB_PROTOCOL_IP4, }; session_rule_table_add_del_args_t args = { .lcl = lcl_pref, .rmt = rmt_pref, .lcl_port = lcl_port, .rmt_port = rmt_port, .action_index = action_index++, .is_add = 1, }; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 1.2.3.4/16 1234 5.6.7.8/16 4321 action %d", action_index - 1); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 1), "Lookup 1.2.3.4 1234 5.6.7.8 4321, action should " "be 1: %d", res); /* * Add 1.2.3.4/24 1234 5.6.7.8/16 4321 and 1.2.3.4/24 1234 5.6.7.8/24 4321 */ args.lcl.fp_addr.ip4 = lcl_ip; args.lcl.fp_len = 24; args.action_index = action_index++; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 1.2.3.4/24 1234 5.6.7.8/16 4321 action %d", action_index - 1); args.rmt.fp_addr.ip4 = rmt_ip; args.rmt.fp_len = 24; args.action_index = action_index++; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 1.2.3.4/24 1234 5.6.7.8/24 4321 action %d", action_index - 1); /* * Add 2.2.2.2/24 1234 6.6.6.6/16 4321 and 3.3.3.3/24 1234 7.7.7.7/16 4321 */ args.lcl.fp_addr.ip4 = lcl_ip2; args.lcl.fp_len = 24; args.rmt.fp_addr.ip4 = rmt_ip2; args.rmt.fp_len = 16; args.action_index = action_index++; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 2.2.2.2/24 1234 6.6.6.6/16 4321 action %d", action_index - 1); args.lcl.fp_addr.ip4 = lcl_ip3; args.rmt.fp_addr.ip4 = rmt_ip3; args.action_index = action_index++; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 3.3.3.3/24 1234 7.7.7.7/16 4321 action %d", action_index - 1); /* * Add again 3.3.3.3/24 1234 7.7.7.7/16 4321 */ args.lcl.fp_addr.ip4 = lcl_ip3; args.rmt.fp_addr.ip4 = rmt_ip3; args.action_index = action_index++; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "overwrite 3.3.3.3/24 1234 7.7.7.7/16 4321 " "action %d", action_index - 1); /* * Lookup 1.2.3.4/32 1234 5.6.7.8/32 4321, 1.2.2.4/32 1234 5.6.7.9/32 4321 * and 3.3.3.3 1234 7.7.7.7 4321 */ res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 3), "Lookup 1.2.3.4 1234 5.6.7.8 4321 action " "should be 3: %d", res); lcl_lkup.as_u32 = clib_host_to_net_u32 (0x01020204); rmt_lkup.as_u32 = clib_host_to_net_u32 (0x05060709); res = session_rules_table_lookup4 (srt, &lcl_lkup, &rmt_lkup, lcl_port, rmt_port); SESSION_TEST ((res == 1), "Lookup 1.2.2.4 1234 5.6.7.9 4321, action " "should be 1: %d", res); res = session_rules_table_lookup4 (srt, &lcl_ip3, &rmt_ip3, lcl_port, rmt_port); SESSION_TEST ((res == 6), "Lookup 3.3.3.3 1234 7.7.7.7 4321, action " "should be 6 (updated): %d", res); /* * Add 1.2.3.4/24 * 5.6.7.8/24 * * Lookup 1.2.3.4 1234 5.6.7.8 4321 and 1.2.3.4 1235 5.6.7.8 4321 */ args.lcl.fp_addr.ip4 = lcl_ip; args.rmt.fp_addr.ip4 = rmt_ip; args.lcl.fp_len = 24; args.rmt.fp_len = 24; args.lcl_port = 0; args.rmt_port = 0; args.action_index = action_index++; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 1.2.3.4/24 * 5.6.7.8/24 * action %d", action_index - 1); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 7), "Lookup 1.2.3.4 1234 5.6.7.8 4321, action should" " be 7 (lpm dst): %d", res); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port + 1, rmt_port); SESSION_TEST ((res == 7), "Lookup 1.2.3.4 1235 5.6.7.8 4321, action should " "be 7: %d", res); /* * Del 1.2.3.4/24 * 5.6.7.8/24 * * Add 1.2.3.4/16 * 5.6.7.8/16 * and 1.2.3.4/24 1235 5.6.7.8/24 4321 * Lookup 1.2.3.4 1234 5.6.7.8 4321, 1.2.3.4 1235 5.6.7.8 4321 and * 1.2.3.4 1235 5.6.7.8 4322 */ args.is_add = 0; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Del 1.2.3.4/24 * 5.6.7.8/24 *"); args.lcl.fp_addr.ip4 = lcl_ip; args.rmt.fp_addr.ip4 = rmt_ip; args.lcl.fp_len = 16; args.rmt.fp_len = 16; args.lcl_port = 0; args.rmt_port = 0; args.action_index = action_index++; args.is_add = 1; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 1.2.3.4/16 * 5.6.7.8/16 * action %d", action_index - 1); args.lcl.fp_addr.ip4 = lcl_ip; args.rmt.fp_addr.ip4 = rmt_ip; args.lcl.fp_len = 24; args.rmt.fp_len = 24; args.lcl_port = lcl_port + 1; args.rmt_port = rmt_port; args.action_index = action_index++; args.is_add = 1; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Add 1.2.3.4/24 1235 5.6.7.8/24 4321 action %d", action_index - 1); if (verbose) session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 3), "Lookup 1.2.3.4 1234 5.6.7.8 4321, action should " "be 3: %d", res); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port + 1, rmt_port); SESSION_TEST ((res == 9), "Lookup 1.2.3.4 1235 5.6.7.8 4321, action should " "be 9: %d", res); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port + 1, rmt_port + 1); SESSION_TEST ((res == 8), "Lookup 1.2.3.4 1235 5.6.7.8 4322, action should " "be 8: %d", res); /* * Delete 1.2.0.0/16 1234 5.6.0.0/16 4321 and 1.2.0.0/16 * 5.6.0.0/16 * * Lookup 1.2.3.4 1234 5.6.7.8 4321 */ args.lcl_port = 1234; args.rmt_port = 4321; args.lcl.fp_len = 16; args.rmt.fp_len = 16; args.is_add = 0; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Del 1.2.0.0/16 1234 5.6.0.0/16 4321"); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 3), "Lookup 1.2.3.4 1234 5.6.7.8 4321, action should " "be 3: %d", res); args.lcl_port = 0; args.rmt_port = 0; args.is_add = 0; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Del 1.2.0.0/16 * 5.6.0.0/16 *"); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 3), "Lookup 1.2.3.4 1234 5.6.7.8 4321, action should " "be 3: %d", res); /* * Delete 1.2.3.4/24 1234 5.6.7.5/24 */ args.lcl.fp_addr.ip4 = lcl_ip; args.rmt.fp_addr.ip4 = rmt_ip; args.lcl.fp_len = 24; args.rmt.fp_len = 24; args.lcl_port = 1234; args.rmt_port = 4321; args.is_add = 0; error = session_rules_table_add_del (srt, &args); SESSION_TEST ((error == 0), "Del 1.2.3.4/24 1234 5.6.7.5/24"); res = session_rules_table_lookup4 (srt, &lcl_ip, &rmt_ip, lcl_port, rmt_port); SESSION_TEST ((res == 2), "Action should be 2: %d", res); return 0; } static int session_test_rules (vlib_main_t * vm, unformat_input_t * input) { session_endpoint_t server_sep = SESSION_ENDPOINT_NULL; u64 options[APP_OPTIONS_N_OPTIONS]; u16 lcl_port = 1234, rmt_port = 4321; u32 server_index, server_index2; u32 placeholder_server_api_index = ~0; transport_connection_t *tc; u32 placeholder_port = 1111; u8 is_filtered = 0, *ns_id = format (0, "appns1"); session_t *listener, *s; app_namespace_t *default_ns = app_namespace_get_default (); u32 local_ns_index = default_ns->local_table_index; int verbose = 0; app_namespace_t *app_ns; app_listener_t *al; int error = 0; u64 handle; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) verbose = 1; else { vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, input); return -1; } } server_sep.is_ip4 = 1; server_sep.port = placeholder_port; clib_memset (options, 0, sizeof (options)); vnet_app_attach_args_t attach_args = { .api_client_index = ~0, .options = options, .namespace_id = 0, .session_cb_vft = &placeholder_session_cbs, .name = format (0, "session_test"), }; vnet_listen_args_t bind_args = { .sep = server_sep, .app_index = 0, }; /* * Attach server with global and local default scope */ options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; attach_args.namespace_id = 0; attach_args.api_client_index = placeholder_server_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attached"); server_index = attach_args.app_index; bind_args.app_index = server_index; error = vnet_listen (&bind_args); SESSION_TEST ((error == 0), "server bound to %U/%d", format_ip46_address, &server_sep.ip, 1, server_sep.port); al = app_listener_get_w_handle (bind_args.handle); listener = app_listener_get_session (al); ip4_address_t lcl_ip = { .as_u32 = clib_host_to_net_u32 (0x01020304), }; ip4_address_t rmt_ip = { .as_u32 = clib_host_to_net_u32 (0x05060708), }; fib_prefix_t lcl_pref = { .fp_addr.ip4.as_u32 = lcl_ip.as_u32, .fp_len = 16, .fp_proto = FIB_PROTOCOL_IP4, }; fib_prefix_t rmt_pref = { .fp_addr.ip4.as_u32 = rmt_ip.as_u32, .fp_len = 16, .fp_proto = FIB_PROTOCOL_IP4, }; tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "optimized lookup should not work (port)"); /* * Add 1.2.3.4/16 1234 5.6.7.8/16 4321 action server_index */ session_rule_add_del_args_t args = { .table_args.lcl = lcl_pref, .table_args.rmt = rmt_pref, .table_args.lcl_port = lcl_port, .table_args.rmt_port = rmt_port, .table_args.action_index = server_index, .table_args.is_add = 1, .appns_index = 0, }; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add 1.2.3.4/16 1234 5.6.7.8/16 4321 action %d", args.table_args.action_index); tc = session_lookup_connection4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP); SESSION_TEST ((tc->c_index == listener->connection_index), "optimized lookup should return the listener"); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc->c_index == listener->connection_index), "lookup should return the listener"); s = session_lookup_safe4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP); SESSION_TEST ((s->connection_index == listener->connection_index), "safe lookup should return the listener"); session_endpoint_t sep = { .ip = rmt_pref.fp_addr, .is_ip4 = 1, .port = rmt_port, .transport_proto = TRANSPORT_PROTO_TCP, }; handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle != server_index), "local session endpoint lookup " "should not work (global scope)"); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port + 1, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "optimized lookup for wrong lcl port + 1 should not work"); /* * Add 1.2.3.4/16 * 5.6.7.8/16 4321 */ args.table_args.lcl_port = 0; args.scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add 1.2.3.4/16 * 5.6.7.8/16 4321 action %d", args.table_args.action_index); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port + 1, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc->c_index == listener->connection_index), "optimized lookup for lcl port + 1 should work"); handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == server_index), "local session endpoint lookup " "should work (lcl ip was zeroed)"); /* * Add deny rule 1.2.3.4/32 1234 5.6.7.8/32 4321 action -2 (drop) */ args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 30; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 30; args.table_args.action_index = SESSION_RULES_TABLE_ACTION_DROP; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add 1.2.3.4/30 1234 5.6.7.8/30 4321 action %d", args.table_args.action_index); if (verbose) { session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup for 1.2.3.4/32 1234 5.6.7.8/16 4321 " "should fail (deny rule)"); SESSION_TEST ((is_filtered == SESSION_LOOKUP_RESULT_FILTERED), "lookup should be filtered (deny)"); handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_DROP_HANDLE), "lookup for 1.2.3.4/32 1234 " "5.6.7.8/16 4321 in local table should return deny"); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port + 1, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc->c_index == listener->connection_index), "lookup 1.2.3.4/32 123*5* 5.6.7.8/16 4321 should work"); /* * "Mask" deny rule with more specific allow: * Add allow rule 1.2.3.4/32 1234 5.6.7.8/32 4321 action -3 (allow) */ args.table_args.is_add = 1; args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 32; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 32; args.table_args.action_index = SESSION_RULES_TABLE_ACTION_ALLOW; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add masking rule 1.2.3.4/30 1234 5.6.7.8/32 " "4321 action %d", args.table_args.action_index); is_filtered = 0; tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup for 1.2.3.4/32 1234 5.6.7.8/16 4321 " "should fail (allow without app)"); SESSION_TEST ((is_filtered == 0), "lookup should NOT be filtered"); handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_INVALID_HANDLE), "lookup for 1.2.3.4/32 " "1234 5.6.7.8/32 4321 in local table should return invalid"); if (verbose) { vlib_cli_output (vm, "Local rules"); session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } sep.ip.ip4.as_u32 += 1 << 24; handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_DROP_HANDLE), "lookup for 1.2.3.4/32 1234" " 5.6.7.9/32 4321 in local table should return deny"); vnet_connect_args_t connect_args = { .app_index = attach_args.app_index, .api_context = 0, }; clib_memcpy (&connect_args.sep, &sep, sizeof (sep)); /* Try connecting */ error = vnet_connect (&connect_args); SESSION_TEST ((error != 0), "connect should fail"); SESSION_TEST ((error == SESSION_E_FILTERED), "connect should be filtered"); sep.ip.ip4.as_u32 -= 1 << 24; /* * Delete masking rule: 1.2.3.4/32 1234 5.6.7.8/32 4321 allow */ args.table_args.is_add = 0; args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 32; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 32; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Del 1.2.3.4/32 1234 5.6.7.8/32 4321 allow"); /* * Add local scope rule for 0/0 * 5.6.7.8/16 4321 action server_index */ args.table_args.is_add = 1; args.table_args.lcl_port = 0; args.table_args.lcl.fp_len = 0; args.table_args.rmt.fp_len = 16; args.table_args.action_index = -1; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add * * 5.6.7.8/16 4321 action %d", args.table_args.action_index); if (verbose) { session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_DROP_HANDLE), "local session endpoint lookup should return deny"); /* * Delete 1.2.3.4/32 1234 5.6.7.8/32 4321 deny */ args.table_args.is_add = 0; args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 30; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 30; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Del 1.2.3.4/32 1234 5.6.7.8/32 4321 deny"); handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_INVALID_HANDLE), "local session endpoint lookup should return invalid"); /* * Delete 0/0 * 5.6.7.8/16 4321, 1.2.3.4/16 * 5.6.7.8/16 4321 and * 1.2.3.4/16 1234 5.6.7.8/16 4321 */ args.table_args.is_add = 0; args.table_args.lcl_port = 0; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 0; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 16; args.table_args.rmt_port = 4321; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Del 0/0 * 5.6.7.8/16 4321"); handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle != server_index), "local session endpoint lookup " "should not work (removed)"); args.table_args.is_add = 0; args.table_args.lcl = lcl_pref; args.table_args.is_add = 0; args.table_args.lcl_port = 0; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 16; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 16; args.table_args.rmt_port = 4321; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Del 1.2.3.4/16 * 5.6.7.8/16 4321"); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port + 1, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup 1.2.3.4/32 123*5* 5.6.7.8/16 4321 should not " "work (del)"); args.table_args.is_add = 0; args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 16; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 16; args.table_args.rmt_port = 4321; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Del 1.2.3.4/16 1234 5.6.7.8/16 4321"); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup 1.2.3.4/32 1234 5.6.7.8/16 4321 should " "not work (del + deny)"); SESSION_TEST ((error == 0), "Del 1.2.3.4/32 1234 5.6.7.8/32 4321 deny"); tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup 1.2.3.4/32 1234 5.6.7.8/16 4321 should" " not work (no-rule)"); /* * Test tags. Add/overwrite/del rule with tag */ args.table_args.is_add = 1; args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 16; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 16; args.table_args.rmt_port = 4321; args.table_args.tag = format (0, "test_rule"); args.table_args.action_index = server_index; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add 1.2.3.4/16 1234 5.6.7.8/16 4321 deny " "tag test_rule"); if (verbose) { session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc->c_index == listener->connection_index), "lookup 1.2.3.4/32 1234 5.6.7.8/16 4321 should work"); vec_free (args.table_args.tag); args.table_args.lcl_port = 1234; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 16; args.table_args.tag = format (0, "test_rule_overwrite"); error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Overwrite 1.2.3.4/16 1234 5.6.7.8/16 4321 deny tag test_rule" " should work"); if (verbose) { session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } args.table_args.is_add = 0; args.table_args.lcl_port += 1; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Del 1.2.3.4/32 1234 5.6.7.8/32 4321 deny " "tag %v", args.table_args.tag); if (verbose) { session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4, &rmt_pref.fp_addr.ip4, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup 1.2.3.4/32 1234 5.6.7.8/32 4321 should not" " work (del)"); /* * Test local rules with multiple namespaces */ /* * Add deny rule 1.2.3.4/32 1234 5.6.7.8/32 0 action -2 (drop) */ args.table_args.is_add = 1; args.table_args.lcl_port = 1234; args.table_args.rmt_port = 0; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 32; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 32; args.table_args.action_index = SESSION_RULES_TABLE_ACTION_DROP; args.table_args.tag = 0; args.scope = SESSION_RULE_SCOPE_LOCAL; error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add 1.2.3.4/32 1234 5.6.7.8/32 4321 action %d", args.table_args.action_index); /* * Add 'white' rule 1.2.3.4/32 1234 5.6.7.8/32 4321 action -2 (drop) */ args.table_args.is_add = 1; args.table_args.lcl_port = 1234; args.table_args.rmt_port = 4321; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 32; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 32; args.table_args.action_index = SESSION_RULES_TABLE_ACTION_ALLOW; error = vnet_session_rule_add_del (&args); if (verbose) { session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4, TRANSPORT_PROTO_TCP); } vnet_app_namespace_add_del_args_t ns_args = { .ns_id = ns_id, .secret = 0, .sw_if_index = APP_NAMESPACE_INVALID_INDEX, .is_add = 1 }; error = vnet_app_namespace_add_del (&ns_args); SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", error); app_ns = app_namespace_get_from_id (ns_id); attach_args.namespace_id = ns_id; attach_args.api_client_index = placeholder_server_api_index; vec_free (attach_args.name); attach_args.name = format (0, "server_test2"); error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server2 attached"); server_index2 = attach_args.app_index; /* * Add deny rule 1.2.3.4/32 1234 5.6.7.8/32 0 action -2 (drop) */ args.table_args.lcl_port = 1234; args.table_args.rmt_port = 0; args.table_args.lcl.fp_addr.ip4 = lcl_ip; args.table_args.lcl.fp_len = 32; args.table_args.rmt.fp_addr.ip4 = rmt_ip; args.table_args.rmt.fp_len = 32; args.table_args.action_index = SESSION_RULES_TABLE_ACTION_DROP; args.appns_index = app_namespace_index (app_ns); error = vnet_session_rule_add_del (&args); SESSION_TEST ((error == 0), "Add 1.2.3.4/32 1234 5.6.7.8/32 4321 action %d " "in test namespace", args.table_args.action_index); /* * Lookup default namespace */ handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_INVALID_HANDLE), "lookup for 1.2.3.4/32 1234 5.6.7.8/32 4321 in local table " "should return allow (invalid)"); sep.port += 1; handle = session_lookup_local_endpoint (local_ns_index, &sep); SESSION_TEST ((handle == SESSION_DROP_HANDLE), "lookup for 1.2.3.4/32 1234 " "5.6.7.8/16 432*2* in local table should return deny"); connect_args.app_index = server_index; clib_memcpy (&connect_args.sep, &sep, sizeof (sep)); error = vnet_connect (&connect_args); SESSION_TEST ((error != 0), "connect should fail"); SESSION_TEST ((error == SESSION_E_FILTERED), "connect should be filtered"); /* * Lookup test namespace */ handle = session_lookup_local_endpoint (app_ns->local_table_index, &sep); SESSION_TEST ((handle == SESSION_DROP_HANDLE), "lookup for 1.2.3.4/32 1234 " "5.6.7.8/16 4321 in local table should return deny"); connect_args.app_index = server_index; error = vnet_connect (&connect_args); SESSION_TEST ((error != 0), "connect should fail"); SESSION_TEST ((error == SESSION_E_FILTERED), "connect should be filtered"); args.table_args.is_add = 0; vnet_session_rule_add_del (&args); args.appns_index = 0; args.table_args.is_add = 0; vnet_session_rule_add_del (&args); args.table_args.rmt_port = 4321; vnet_session_rule_add_del (&args); /* * Final Cleanup */ vec_free (args.table_args.tag); vnet_app_detach_args_t detach_args = { .app_index = server_index, .api_client_index = ~0, }; vnet_application_detach (&detach_args); detach_args.app_index = server_index2; vnet_application_detach (&detach_args); vec_free (ns_id); vec_free (attach_args.name); return 0; } static int session_test_proxy (vlib_main_t * vm, unformat_input_t * input) { u64 options[APP_OPTIONS_N_OPTIONS]; char *show_listeners = "sh session listeners tcp verbose"; char *show_local_listeners = "sh app ns table default"; unformat_input_t tmp_input; u32 server_index, app_index; u32 placeholder_server_api_index = ~0, sw_if_index = 0; u8 is_filtered = 0; session_t *s; transport_connection_t *tc; u16 lcl_port = 1234, rmt_port = 4321; app_namespace_t *app_ns; int verbose = 0, error = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) verbose = 1; else { vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, input); return -1; } } ip4_address_t lcl_ip = { .as_u32 = clib_host_to_net_u32 (0x01020304), }; ip4_address_t rmt_ip = { .as_u32 = clib_host_to_net_u32 (0x05060708), }; fib_prefix_t rmt_pref = { .fp_addr.ip4.as_u32 = rmt_ip.as_u32, .fp_len = 16, .fp_proto = FIB_PROTOCOL_IP4, }; session_endpoint_t sep = { .ip = rmt_pref.fp_addr, .is_ip4 = 1, .port = rmt_port, .transport_proto = TRANSPORT_PROTO_TCP, }; /* * Create loopback interface */ session_create_lookpback (0, &sw_if_index, &lcl_ip); app_ns = app_namespace_get_default (); app_ns->sw_if_index = sw_if_index; clib_memset (options, 0, sizeof (options)); options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ACCEPT_REDIRECT; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_PROXY; options[APP_OPTIONS_PROXY_TRANSPORT] = 1 << TRANSPORT_PROTO_TCP; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; vnet_app_attach_args_t attach_args = { .api_client_index = ~0, .options = options, .namespace_id = 0, .session_cb_vft = &placeholder_session_cbs, .name = format (0, "session_test"), }; attach_args.api_client_index = placeholder_server_api_index; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); server_index = attach_args.app_index; if (verbose) { unformat_init_string (&tmp_input, show_listeners, strlen (show_listeners)); vlib_cli_input (vm, &tmp_input, 0, 0); unformat_init_string (&tmp_input, show_local_listeners, strlen (show_local_listeners)); vlib_cli_input (vm, &tmp_input, 0, 0); } tc = session_lookup_connection_wt4 (0, &lcl_ip, &rmt_ip, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc != 0), "lookup 1.2.3.4 1234 5.6.7.8 4321 should be " "successful"); s = listen_session_get (tc->s_index); SESSION_TEST ((s->app_index == server_index), "lookup should return" " the server"); tc = session_lookup_connection_wt4 (0, &rmt_ip, &rmt_ip, lcl_port, rmt_port, TRANSPORT_PROTO_TCP, 0, &is_filtered); SESSION_TEST ((tc == 0), "lookup 5.6.7.8 1234 5.6.7.8 4321 should" " not work"); app_index = session_lookup_local_endpoint (app_ns->local_table_index, &sep); SESSION_TEST ((app_index == server_index), "local session endpoint lookup" " should work"); vnet_app_detach_args_t detach_args = { .app_index = server_index, .api_client_index = ~0, }; vnet_application_detach (&detach_args); if (verbose) { unformat_init_string (&tmp_input, show_listeners, strlen (show_listeners)); vlib_cli_input (vm, &tmp_input, 0, 0); unformat_init_string (&tmp_input, show_local_listeners, strlen (show_local_listeners)); vlib_cli_input (vm, &tmp_input, 0, 0); } app_index = session_lookup_local_endpoint (app_ns->local_table_index, &sep); SESSION_TEST ((app_index == SESSION_RULES_TABLE_INVALID_INDEX), "local session endpoint lookup should not work after detach"); if (verbose) unformat_free (&tmp_input); vec_free (attach_args.name); session_delete_loopback (sw_if_index); return 0; } static inline void wait_for_event (svm_msg_q_t * mq, int fd, int epfd, u8 use_eventfd) { if (!use_eventfd) { svm_msg_q_lock (mq); while (svm_msg_q_is_empty (mq)) svm_msg_q_wait (mq); } else { int __clib_unused n_read, rv; struct epoll_event ep_evt; u64 buf; while (1) { rv = epoll_wait (epfd, &ep_evt, 1, -1); if (rv < 0) { ST_DBG ("epoll error"); exit (1); } else if (rv > 0 && (ep_evt.events & EPOLLIN)) { n_read = read (fd, &buf, sizeof (buf)); } else continue; if (!svm_msg_q_is_empty (mq)) { svm_msg_q_lock (mq); break; } } } } static int session_test_mq_speed (vlib_main_t * vm, unformat_input_t * input) { int error, __clib_unused verbose, use_eventfd = 0; u64 i, n_test_msgs = 1 << 10, *counter; u64 options[APP_OPTIONS_N_OPTIONS]; int epfd = -1, rv, prod_fd = -1; svm_fifo_t *rx_fifo, *tx_fifo; vl_api_registration_t *reg; struct epoll_event ep_evt; u32 app_index, api_index; app_worker_t *app_wrk; segment_manager_t *sm; svm_msg_q_msg_t msg; application_t *app; svm_msg_q_t *mq; f64 start, diff; svm_queue_t *q; session_t s; pid_t pid; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) verbose = 1; else if (unformat (input, "%d", &n_test_msgs)) ; else if (unformat (input, "use-eventfd")) use_eventfd = 1; else { vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, input); return -1; } } q = clib_mem_alloc (sizeof (*q)); api_index = vl_api_memclnt_create_internal ("session_mq_test_api", q); clib_memset (options, 0, sizeof (options)); options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; options[APP_OPTIONS_EVT_QUEUE_SIZE] = 2048; reg = vl_api_client_index_to_registration (api_index); /* Shut up coverity */ if (reg == 0) abort (); vnet_app_attach_args_t attach_args = { .api_client_index = api_index, .options = options, .namespace_id = 0, .session_cb_vft = &placeholder_session_cbs, .name = format (0, "session_mq_test"), }; error = vnet_application_attach (&attach_args); SESSION_TEST ((error == 0), "server attachment should work"); app_index = attach_args.app_index; app = application_get (app_index); app_wrk = application_get_worker (app, 0); mq = app_wrk->event_queue; if (use_eventfd) { svm_msg_q_alloc_producer_eventfd (mq); svm_msg_q_alloc_consumer_eventfd (mq); prod_fd = svm_msg_q_get_producer_eventfd (mq); SESSION_TEST (prod_fd != -1, "mq producer eventd valid %u", prod_fd); } sm = app_worker_get_or_alloc_connect_segment_manager (app_wrk); segment_manager_alloc_session_fifos (sm, 0, &rx_fifo, &tx_fifo); s.rx_fifo = rx_fifo; s.tx_fifo = tx_fifo; s.session_state = SESSION_STATE_READY; counter = (u64 *) f_head_cptr (rx_fifo)->data; start = vlib_time_now (vm); pid = fork (); if (pid < 0) SESSION_TEST (0, "fork failed"); if (pid == 0) { if (use_eventfd) { epfd = epoll_create1 (0); SESSION_TEST (epfd != -1, "epfd created"); ep_evt.events = EPOLLIN; ep_evt.data.u64 = prod_fd; rv = epoll_ctl (epfd, EPOLL_CTL_ADD, prod_fd, &ep_evt); SESSION_TEST (rv == 0, "epoll returned %d", rv); } for (i = 0; i < n_test_msgs; i++) { wait_for_event (mq, prod_fd, epfd, use_eventfd); svm_msg_q_sub_w_lock (mq, &msg); svm_msg_q_free_msg (mq, &msg); svm_msg_q_unlock (mq); *counter = *counter + 1; svm_fifo_unset_event (rx_fifo); } exit (0); } else { ST_DBG ("client pid %u", pid); for (i = 0; i < n_test_msgs; i++) { while (svm_fifo_has_event (rx_fifo)) ; app_worker_lock_and_send_event (app_wrk, &s, SESSION_IO_EVT_RX); } } diff = vlib_time_now (vm) - start; ST_DBG ("done %u events in %.2f sec: %f evts/s", *counter, diff, *counter / diff); vnet_app_detach_args_t detach_args = { .app_index = app_index, .api_client_index = ~0, }; vnet_application_detach (&detach_args); return 0; } static int session_test_mq_basic (vlib_main_t * vm, unformat_input_t * input) { svm_msg_q_cfg_t _cfg, *cfg = &_cfg; svm_msg_q_msg_t msg1, msg2, msg[12]; int __clib_unused verbose, i, rv; svm_msg_q_t *mq; svm_msg_q_ring_t *ring; u8 *rings_ptr; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "verbose")) verbose = 1; else { vlib_cli_output (vm, "parse error: '%U'", format_unformat_error, input); return -1; } } svm_msg_q_ring_cfg_t rc[2] = { {8, 8, 0} , {8, 16, 0} }; cfg->consumer_pid = ~0; cfg->n_rings = 2; cfg->q_nitems = 16; cfg->ring_cfgs = rc; mq = svm_msg_q_alloc (cfg); SESSION_TEST (mq != 0, "svm_msg_q_alloc"); SESSION_TEST (vec_len (mq->rings) == 2, "ring allocation"); rings_ptr = (u8 *) mq->rings + vec_bytes (mq->rings); vec_foreach (ring, mq->rings) { SESSION_TEST (ring->data == rings_ptr, "ring data"); rings_ptr += (uword) ring->nitems * ring->elsize; } msg1 = svm_msg_q_alloc_msg (mq, 8); rv = (mq->rings[0].cursize != 1 || msg1.ring_index != 0 || msg1.elt_index != 0); SESSION_TEST (rv == 0, "msg alloc1"); msg2 = svm_msg_q_alloc_msg (mq, 15); rv = (mq->rings[1].cursize != 1 || msg2.ring_index != 1 || msg2.elt_index != 0); SESSION_TEST (rv == 0, "msg alloc2"); svm_msg_q_free_msg (mq, &msg1); SESSION_TEST (mq->rings[0].cursize == 0, "free msg"); for (i = 0; i < 12; i++) { msg[i] = svm_msg_q_alloc_msg (mq, 7); *(u32 *) svm_msg_q_msg_data (mq, &msg[i]) = i; } rv = (mq->rings[0].cursize != 8 || mq->rings[1].cursize != 5); SESSION_TEST (rv == 0, "msg alloc3"); *(u32 *) svm_msg_q_msg_data (mq, &msg2) = 123; svm_msg_q_add (mq, &msg2, SVM_Q_NOWAIT); for (i = 0; i < 12; i++) svm_msg_q_add (mq, &msg[i], SVM_Q_NOWAIT); rv = svm_msg_q_sub (mq, &msg2, SVM_Q_NOWAIT, 0); SESSION_TEST (rv == 0, "dequeue1"); SESSION_TEST (msg2.ring_index == 1 && msg2.elt_index == 0, "dequeue1 result"); rv = (*(u32 *) svm_msg_q_msg_data (mq, &msg2) == 123); SESSION_TEST (rv, "dequeue 1 data"); svm_msg_q_free_msg (mq, &msg2); for (i = 0; i < 12; i++) { if (svm_msg_q_sub (mq, &msg[i], SVM_Q_NOWAIT, 0)) SESSION_TEST (0, "dequeue2"); if (i < 8) { if (msg[i].ring_index != 0 || msg[i].elt_index != (i + 1) % 8) SESSION_TEST (0, "dequeue2 result2"); } else { if (msg[i].ring_index != 1 || msg[i].elt_index != (i - 8) + 1) SESSION_TEST (0, "dequeue2 result3"); } if (*(u32 *) svm_msg_q_msg_data (mq, &msg[i]) != i) SESSION_TEST (0, "dequeue2 wrong data"); svm_msg_q_free_msg (mq, &msg[i]); } rv = (mq->rings[0].cursize == 0 && mq->rings[1].cursize == 0); SESSION_TEST (rv, "post dequeue"); return 0; } static clib_error_t * session_test (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd_arg) { int res = 0; vnet_session_enable_disable (vm, 1); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "basic")) res = session_test_basic (vm, input); else if (unformat (input, "namespace")) res = session_test_namespace (vm, input); else if (unformat (input, "rules-table")) res = session_test_rule_table (vm, input); else if (unformat (input, "rules")) res = session_test_rules (vm, input); else if (unformat (input, "proxy")) res = session_test_proxy (vm, input); else if (unformat (input, "endpt-cfg")) res = session_test_endpoint_cfg (vm, input); else if (unformat (input, "mq-speed")) res = session_test_mq_speed (vm, input); else if (unformat (input, "mq-basic")) res = session_test_mq_basic (vm, input); else if (unformat (input, "all")) { if ((res = session_test_basic (vm, input))) goto done; if ((res = session_test_namespace (vm, input))) goto done; if ((res = session_test_rule_table (vm, input))) goto done; if ((res = session_test_rules (vm, input))) goto done; if ((res = session_test_proxy (vm, input))) goto done; if ((res = session_test_endpoint_cfg (vm, input))) goto done; if ((res = session_test_mq_speed (vm, input))) goto done; if ((res = session_test_mq_basic (vm, input))) goto done; } else break; } done: if (res) return clib_error_return (0, "Session unit test failed"); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (tcp_test_command, static) = { .path = "test session", .short_help = "internal session unit tests", .function = session_test, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */