summaryrefslogtreecommitdiffstats
path: root/src/vnet/l2tp/l2tp.c
blob: a59c789350366ad1f0de1d5c3a18aded3ac01fbe (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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
 * l2tp.c : L2TPv3 tunnel support
 *
 * Copyright (c) 2013 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 <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/l2/l2_input.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/l2tp/l2tp.h>

l2t_main_t l2t_main;

/* packet trace format function */
u8 *
format_l2t_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 *);
  l2t_trace_t *t = va_arg (*args, l2t_trace_t *);

  if (t->is_user_to_network)
    s = format (s, "L2T: %U (client) -> %U (our) session %d",
		format_ip6_address, &t->client_address,
		format_ip6_address, &t->our_address, t->session_index);
  else
    s = format (s, "L2T: %U (our) -> %U (client) session %d)",
		format_ip6_address, &t->our_address,
		format_ip6_address, &t->client_address, t->session_index);
  return s;
}

u8 *
format_l2t_session (u8 * s, va_list * args)
{
  l2t_session_t *session = va_arg (*args, l2t_session_t *);
  l2t_main_t *lm = &l2t_main;
  u32 counter_index;
  vlib_counter_t v;

  s = format (s, "[%d] %U (our) %U (client) %U (sw_if_index %d)\n",
	      session - lm->sessions,
	      format_ip6_address, &session->our_address,
	      format_ip6_address, &session->client_address,
	      format_vnet_sw_interface_name, lm->vnet_main,
	      vnet_get_sw_interface (lm->vnet_main, session->sw_if_index),
	      session->sw_if_index);

  s = format (s, "   local cookies %016llx %016llx remote cookie %016llx\n",
	      clib_net_to_host_u64 (session->local_cookie[0]),
	      clib_net_to_host_u64 (session->local_cookie[1]),
	      clib_net_to_host_u64 (session->remote_cookie));

  s = format (s, "   local session-id %d remote session-id %d\n",
	      clib_net_to_host_u32 (session->local_session_id),
	      clib_net_to_host_u32 (session->remote_session_id));

  s = format (s, "   l2 specific sublayer %s\n",
	      session->l2_sublayer_present ? "preset" : "absent");

  counter_index =
    session_index_to_counter_index (session - lm->sessions,
				    SESSION_COUNTER_USER_TO_NETWORK);

  vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
  if (v.packets != 0)
    s = format (s, "   user-to-net: %llu pkts %llu bytes\n",
		v.packets, v.bytes);

  vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v);

  if (v.packets != 0)
    s = format (s, "   net-to-user: %llu pkts %llu bytes\n",
		v.packets, v.bytes);
  return s;
}

static clib_error_t *
show_l2tp_command_fn (vlib_main_t * vm,
		      unformat_input_t * input, vlib_cli_command_t * cmd)
{
  l2t_session_t *session;
  l2t_main_t *lm = &l2t_main;
  char *keystr = 0;
  int verbose = 0;

  if (unformat (input, "verbose") || unformat (input, "v"))
    verbose = 1;

  if (pool_elts (lm->sessions) == 0)
    vlib_cli_output (vm, "No l2tp sessions...");
  else
    vlib_cli_output (vm, "%u l2tp sessions...", pool_elts (lm->sessions));

  if (verbose)
    {
      switch (lm->lookup_type)
	{
	case L2T_LOOKUP_SRC_ADDRESS:
	  keystr = "src address";
	  break;

	case L2T_LOOKUP_DST_ADDRESS:
	  keystr = "dst address";
	  break;

	case L2T_LOOKUP_SESSION_ID:
	  keystr = "session id";
	  break;

	default:
	  keystr = "BOGUS!";
	  break;
	}

      vlib_cli_output (vm, "L2tp session lookup on %s", keystr);

      /* *INDENT-OFF* */
      pool_foreach (session, lm->sessions,
      ({
        vlib_cli_output (vm, "%U", format_l2t_session, session);
      }));
      /* *INDENT-ON* */
    }

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_session_detail_command, static) = {
  .path = "show l2tpv3",
  .short_help = "show l2tpv3 [verbose]",
  .function = show_l2tp_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
test_counters_command_fn (vlib_main_t * vm,
			  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  l2t_session_t *session;
  l2t_main_t *lm = &l2t_main;
  u32 session_index;
  u32 counter_index;
  u32 nincr = 0;
  u32 thread_index = vm->thread_index;

  /* *INDENT-OFF* */
  pool_foreach (session, lm->sessions,
  ({
    session_index = session - lm->sessions;
    counter_index =
      session_index_to_counter_index (session_index,
                                      SESSION_COUNTER_USER_TO_NETWORK);
    vlib_increment_combined_counter (&lm->counter_main,
                                     thread_index,
                                     counter_index,
                                     1/*pkt*/, 1111 /*bytes*/);
    vlib_increment_combined_counter (&lm->counter_main,
                                     thread_index,
                                     counter_index+1,
                                     1/*pkt*/, 2222 /*bytes*/);
    nincr++;

  }));
  /* *INDENT-ON* */
  vlib_cli_output (vm, "Incremented %d active counters\n", nincr);

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (test_counters_command, static) = {
    .path = "test counters",
    .short_help = "increment all active counters",
    .function = test_counters_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
clear_counters_command_fn (vlib_main_t * vm,
			   unformat_input_t * input, vlib_cli_command_t * cmd)
{
  l2t_session_t *session;
  l2t_main_t *lm = &l2t_main;
  u32 session_index;
  u32 counter_index;
  u32 nincr = 0;

  /* *INDENT-OFF* */
  pool_foreach (session, lm->sessions,
  ({
    session_index = session - lm->sessions;
    counter_index =
      session_index_to_counter_index (session_index,
                                      SESSION_COUNTER_USER_TO_NETWORK);
    vlib_zero_combined_counter (&lm->counter_main, counter_index);
    vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
    nincr++;
  }));
  /* *INDENT-ON* */
  vlib_cli_output (vm, "Cleared %d active counters\n", nincr);

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (clear_counters_command, static) = {
  .path = "clear counters",
  .short_help = "clear all active counters",
  .function = clear_counters_command_fn,
};
/* *INDENT-ON* */

static u8 *
format_l2tpv3_name (u8 * s, va_list * args)
{
  l2t_main_t *lm = &l2t_main;
  u32 i = va_arg (*args, u32);
  u32 show_dev_instance = ~0;

  if (i < vec_len (lm->dev_inst_by_real))
    show_dev_instance = lm->dev_inst_by_real[i];

  if (show_dev_instance != ~0)
    i = show_dev_instance;

  return format (s, "l2tpv3_tunnel%d", i);
}

static int
l2tpv3_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
{
  l2t_main_t *lm = &l2t_main;

  vec_validate_init_empty (lm->dev_inst_by_real, hi->dev_instance, ~0);

  lm->dev_inst_by_real[hi->dev_instance] = new_dev_instance;

  return 0;
}

static uword
dummy_interface_tx (vlib_main_t * vm,
		    vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  clib_warning ("you shouldn't be here, leaking buffers...");
  return frame->n_vectors;
}

/* *INDENT-OFF* */
VNET_DEVICE_CLASS (l2tpv3_device_class,static) = {
  .name = "L2TPv3",
  .format_device_name = format_l2tpv3_name,
  .name_renumber = l2tpv3_name_renumber,
  .tx_function = dummy_interface_tx,
};
/* *INDENT-ON* */

static u8 *
format_l2tp_header_with_length (u8 * s, va_list * args)
{
  u32 dev_instance = va_arg (*args, u32);
  s = format (s, "unimplemented dev %u", dev_instance);
  return s;
}

/* *INDENT-OFF* */
VNET_HW_INTERFACE_CLASS (l2tpv3_hw_class) = {
  .name = "L2TPV3",
  .format_header = format_l2tp_header_with_length,
  .build_rewrite = default_build_rewrite,
  .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
};
/* *INDENT-ON* */

int
create_l2tpv3_ipv6_tunnel (l2t_main_t * lm,
			   ip6_address_t * client_address,
			   ip6_address_t * our_address,
			   u32 local_session_id,
			   u32 remote_session_id,
			   u64 local_cookie,
			   u64 remote_cookie,
			   int l2_sublayer_present,
			   u32 encap_fib_index, u32 * sw_if_index)
{
  l2t_session_t *s = 0;
  vnet_main_t *vnm = lm->vnet_main;
  vnet_hw_interface_t *hi;
  uword *p = (uword *) ~ 0;
  u32 hw_if_index;
  l2tpv3_header_t l2tp_hdr;
  ip6_address_t *dst_address_copy, *src_address_copy;
  u32 counter_index;

  remote_session_id = clib_host_to_net_u32 (remote_session_id);
  local_session_id = clib_host_to_net_u32 (local_session_id);

  switch (lm->lookup_type)
    {
    case L2T_LOOKUP_SRC_ADDRESS:
      p = hash_get_mem (lm->session_by_src_address, client_address);
      break;

    case L2T_LOOKUP_DST_ADDRESS:
      p = hash_get_mem (lm->session_by_dst_address, our_address);
      break;

    case L2T_LOOKUP_SESSION_ID:
      p = hash_get (lm->session_by_session_id, local_session_id);
      break;

    default:
      ASSERT (0);
    }

  /* adding a session: session must not already exist */
  if (p)
    return VNET_API_ERROR_INVALID_VALUE;

  pool_get (lm->sessions, s);
  memset (s, 0, sizeof (*s));
  clib_memcpy (&s->our_address, our_address, sizeof (s->our_address));
  clib_memcpy (&s->client_address, client_address,
	       sizeof (s->client_address));
  s->local_cookie[0] = clib_host_to_net_u64 (local_cookie);
  s->remote_cookie = clib_host_to_net_u64 (remote_cookie);
  s->local_session_id = local_session_id;
  s->remote_session_id = remote_session_id;
  s->l2_sublayer_present = l2_sublayer_present;
  /* precompute l2tp header size */
  s->l2tp_hdr_size = l2_sublayer_present ?
    sizeof (l2tpv3_header_t) :
    sizeof (l2tpv3_header_t) - sizeof (l2tp_hdr.l2_specific_sublayer);
  s->admin_up = 0;
  s->encap_fib_index = encap_fib_index;

  /* Setup hash table entries */
  switch (lm->lookup_type)
    {
    case L2T_LOOKUP_SRC_ADDRESS:
      src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
      clib_memcpy (src_address_copy, client_address,
		   sizeof (*src_address_copy));
      hash_set_mem (lm->session_by_src_address, src_address_copy,
		    s - lm->sessions);
      break;
    case L2T_LOOKUP_DST_ADDRESS:
      dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
      clib_memcpy (dst_address_copy, our_address, sizeof (*dst_address_copy));
      hash_set_mem (lm->session_by_dst_address, dst_address_copy,
		    s - lm->sessions);
      break;
    case L2T_LOOKUP_SESSION_ID:
      hash_set (lm->session_by_session_id, local_session_id,
		s - lm->sessions);
      break;

    default:
      ASSERT (0);
    }

  /* validate counters */
  counter_index =
    session_index_to_counter_index (s - lm->sessions,
				    SESSION_COUNTER_USER_TO_NETWORK);
  vlib_validate_combined_counter (&lm->counter_main, counter_index);
  vlib_validate_combined_counter (&lm->counter_main, counter_index + 1);

  if (vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) > 0)
    {
      hw_if_index = lm->free_l2tpv3_tunnel_hw_if_indices
	[vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) - 1];
      _vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) -= 1;

      hi = vnet_get_hw_interface (vnm, hw_if_index);
      hi->dev_instance = s - lm->sessions;
      hi->hw_instance = hi->dev_instance;
    }
  else
    {
      hw_if_index = vnet_register_interface
	(vnm, l2tpv3_device_class.index, s - lm->sessions,
	 l2tpv3_hw_class.index, s - lm->sessions);
      hi = vnet_get_hw_interface (vnm, hw_if_index);
      hi->output_node_index = l2t_encap_node.index;
      /* $$$$ initialize custom dispositions, if needed */
    }

  s->hw_if_index = hw_if_index;
  s->sw_if_index = hi->sw_if_index;

  if (sw_if_index)
    *sw_if_index = hi->sw_if_index;

  return 0;
}

static clib_error_t *
create_l2tpv3_tunnel_command_fn (vlib_main_t * vm,
				 unformat_input_t * input,
				 vlib_cli_command_t * cmd)
{
  ip6_address_t client_address, our_address;
  unformat_input_t _line_input, *line_input = &_line_input;
  l2t_main_t *lm = &l2t_main;
  u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
  u32 local_session_id = 1, remote_session_id = 1;
  int our_address_set = 0, client_address_set = 0;
  int l2_sublayer_present = 0;
  int rv;
  u32 sw_if_index;
  u32 encap_fib_id = ~0;
  u32 encap_fib_index = ~0;
  clib_error_t *error = NULL;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "client %U",
		    unformat_ip6_address, &client_address))
	client_address_set = 1;
      else if (unformat (line_input, "our %U",
			 unformat_ip6_address, &our_address))
	our_address_set = 1;
      else if (unformat (line_input, "local-cookie %llx", &local_cookie))
	;
      else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
	;
      else if (unformat (line_input, "local-session-id %d",
			 &local_session_id))
	;
      else if (unformat (line_input, "remote-session-id %d",
			 &remote_session_id))
	;
      else if (unformat (line_input, "fib-id %d", &encap_fib_id))
	;
      else if (unformat (line_input, "l2-sublayer-present"))
	l2_sublayer_present = 1;
      else
	{
	  error = clib_error_return (0, "parse error: '%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  if (encap_fib_id != ~0)
    {
      uword *p;
      ip6_main_t *im = &ip6_main;
      if (!(p = hash_get (im->fib_index_by_table_id, encap_fib_id)))
	{
	  error = clib_error_return (0, "No fib with id %d", encap_fib_id);
	  goto done;
	}
      encap_fib_index = p[0];
    }
  else
    {
      encap_fib_index = ~0;
    }

  if (our_address_set == 0)
    {
      error = clib_error_return (0, "our address not specified");
      goto done;
    }
  if (client_address_set == 0)
    {
      error = clib_error_return (0, "client address not specified");
      goto done;
    }

  rv = create_l2tpv3_ipv6_tunnel (lm, &client_address, &our_address,
				  local_session_id, remote_session_id,
				  local_cookie, remote_cookie,
				  l2_sublayer_present,
				  encap_fib_index, &sw_if_index);
  switch (rv)
    {
    case 0:
      vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
		       vnet_get_main (), sw_if_index);
      break;
    case VNET_API_ERROR_INVALID_VALUE:
      error = clib_error_return (0, "session already exists...");
      goto done;

    case VNET_API_ERROR_NO_SUCH_ENTRY:
      error = clib_error_return (0, "session does not exist...");
      goto done;

    default:
      error = clib_error_return (0, "l2tp_session_add_del returned %d", rv);
      goto done;
    }

done:
  unformat_free (line_input);

  return error;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (create_l2tpv3_tunnel_command, static) =
{
  .path = "create l2tpv3 tunnel",
  .short_help =
  "create l2tpv3 tunnel client <ip6> our <ip6> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec>",
  .function = create_l2tpv3_tunnel_command_fn,
};
/* *INDENT-ON* */

int
l2tpv3_set_tunnel_cookies (l2t_main_t * lm,
			   u32 sw_if_index,
			   u64 new_local_cookie, u64 new_remote_cookie)
{
  l2t_session_t *s;
  vnet_hw_interface_t *hi;
  vnet_main_t *vnm = vnet_get_main ();
  hi = vnet_get_sup_hw_interface (vnm, sw_if_index);

  if (pool_is_free_index (lm->sessions, hi->dev_instance))
    return VNET_API_ERROR_INVALID_VALUE;

  s = pool_elt_at_index (lm->sessions, hi->dev_instance);

  s->local_cookie[1] = s->local_cookie[0];
  s->local_cookie[0] = clib_host_to_net_u64 (new_local_cookie);
  s->remote_cookie = clib_host_to_net_u64 (new_remote_cookie);

  return 0;
}


static clib_error_t *
set_l2tp_tunnel_cookie_command_fn (vlib_main_t * vm,
				   unformat_input_t * input,
				   vlib_cli_command_t * cmd)
{
  l2t_main_t *lm = &l2t_main;
  vnet_main_t *vnm = vnet_get_main ();
  u32 sw_if_index = ~0;
  u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;

  int rv;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
		    &sw_if_index))
	;
      else if (unformat (input, "local %llx", &local_cookie))
	;
      else if (unformat (input, "remote %llx", &remote_cookie))
	;
      else
	break;
    }
  if (sw_if_index == ~0)
    return clib_error_return (0, "unknown interface");
  if (local_cookie == ~0)
    return clib_error_return (0, "local cookie required");
  if (remote_cookie == ~0)
    return clib_error_return (0, "remote cookie required");

  rv = l2tpv3_set_tunnel_cookies (lm, sw_if_index,
				  local_cookie, remote_cookie);

  switch (rv)
    {
    case 0:
      break;

    case VNET_API_ERROR_INVALID_SW_IF_INDEX:
      return clib_error_return (0, "invalid interface");

    default:
      return clib_error_return (0, "l2tp_session_set_cookies returned %d",
				rv);
    }

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (set_l2tp_tunnel_cookie_command, static) =
{
  .path = "set l2tpv3 tunnel cookie",
  .short_help =
  "set l2tpv3 tunnel cookie <intfc> local <hex> remote <hex>",
  .function = set_l2tp_tunnel_cookie_command_fn,
};
/* *INDENT-ON* */

int
l2tpv3_interface_enable_disable (vnet_main_t * vnm,
				 u32 sw_if_index, int enable_disable)
{

  if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
    return VNET_API_ERROR_INVALID_SW_IF_INDEX;

  vnet_feature_enable_disable ("ip6-unicast", "l2tp-decap", sw_if_index,
			       enable_disable, 0, 0);
  return 0;
}

/* Enable/disable L2TPv3 intercept on IP6 fowarding path */
static clib_error_t *
set_ip6_l2tpv3 (vlib_main_t * vm,
		unformat_input_t * input, vlib_cli_command_t * cmd)
{
  u32 sw_if_index = ~0;
  int is_add = 1;
  int rv;
  vnet_main_t *vnm = vnet_get_main ();

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
		    &sw_if_index))
	;
      else if (unformat (input, "del"))
	is_add = 0;
      else
	break;
    }

  if (sw_if_index == ~0)
    return clib_error_return (0, "interface required");

  rv = l2tpv3_interface_enable_disable (vnm, sw_if_index, is_add);

  switch (rv)
    {
    case 0:
      break;

    case VNET_API_ERROR_INVALID_SW_IF_INDEX:
      return clib_error_return (0, "invalid interface");

    default:
      return clib_error_return (0,
				"l2tp_interface_enable_disable returned %d",
				rv);
    }
  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (set_interface_ip6_l2tpv3, static) =
{
  .path = "set interface ip6 l2tpv3",
  .function = set_ip6_l2tpv3,
  .short_help = "set interface ip6 l2tpv3 <intfc> [del]",
};
/* *INDENT-ON* */

static clib_error_t *
l2tp_config (vlib_main_t * vm, unformat_input_t * input)
{
  l2t_main_t *lm = &l2t_main;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "lookup-v6-src"))
	lm->lookup_type = L2T_LOOKUP_SRC_ADDRESS;
      else if (unformat (input, "lookup-v6-dst"))
	lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
      else if (unformat (input, "lookup-session-id"))
	lm->lookup_type = L2T_LOOKUP_SESSION_ID;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, input);
    }
  return 0;
}

VLIB_CONFIG_FUNCTION (l2tp_config, "l2tp");


clib_error_t *
l2tp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
{
  l2t_main_t *lm = &l2t_main;
  vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
  if (hi->hw_class_index != l2tpv3_hw_class.index)
    return 0;

  u32 session_index = hi->dev_instance;
  l2t_session_t *s = pool_elt_at_index (lm->sessions, session_index);
  s->admin_up = ! !(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
  return 0;
}

VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2tp_sw_interface_up_down);

clib_error_t *
l2tp_init (vlib_main_t * vm)
{
  l2t_main_t *lm = &l2t_main;
  ip_main_t *im = &ip_main;
  ip_protocol_info_t *pi;

  lm->vnet_main = vnet_get_main ();
  lm->vlib_main = vm;
  lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;

  lm->session_by_src_address = hash_create_mem
    (0, sizeof (ip6_address_t) /* key bytes */ ,
     sizeof (u32) /* value bytes */ );
  lm->session_by_dst_address = hash_create_mem
    (0, sizeof (ip6_address_t) /* key bytes */ ,
     sizeof (u32) /* value bytes */ );
  lm->session_by_session_id = hash_create (0, sizeof (uword));

  pi = ip_get_protocol_info (im, IP_PROTOCOL_L2TP);
  pi->unformat_pg_edit = unformat_pg_l2tp_header;

  /* insure these nodes are included in build */
  l2tp_encap_init (vm);
  l2tp_decap_init ();

  return 0;
}

VLIB_INIT_FUNCTION (l2tp_init);

clib_error_t *
l2tp_worker_init (vlib_main_t * vm)
{
  l2tp_encap_init (vm);

  return 0;
}

VLIB_WORKER_INIT_FUNCTION (l2tp_worker_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
e; int i, errors = 0, lock; stream_session_t *s; u32 *indices; indices = smm->session_to_enqueue[transport_proto][thread_index]; tp_service = transport_protocol_service_type (transport_proto); lock = tp_service == TRANSPORT_SERVICE_CL; for (i = 0; i < vec_len (indices); i++) { s = session_get_if_valid (indices[i], thread_index); if (PREDICT_FALSE (!s)) { errors++; continue; } if (PREDICT_FALSE (session_enqueue_notify (s, lock))) errors++; } vec_reset_length (indices); smm->session_to_enqueue[transport_proto][thread_index] = indices; smm->current_enqueue_epoch[transport_proto][thread_index]++; return errors; } int session_manager_flush_all_enqueue_events (u8 transport_proto) { vlib_thread_main_t *vtm = vlib_get_thread_main (); int i, errors = 0; for (i = 0; i < 1 + vtm->n_threads; i++) errors += session_manager_flush_enqueue_events (transport_proto, i); return errors; } /** * Init fifo tail and head pointers * * Useful if transport uses absolute offsets for tracking ooo segments. */ void stream_session_init_fifos_pointers (transport_connection_t * tc, u32 rx_pointer, u32 tx_pointer) { stream_session_t *s; s = session_get (tc->s_index, tc->thread_index); svm_fifo_init_pointers (s->server_rx_fifo, rx_pointer); svm_fifo_init_pointers (s->server_tx_fifo, tx_pointer); } int session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) { u32 opaque = 0, new_ti, new_si; stream_session_t *new_s = 0; segment_manager_t *sm; application_t *app; u8 alloc_fifos; int error = 0; u64 handle; /* * Find connection handle and cleanup half-open table */ handle = session_lookup_half_open_handle (tc); if (handle == HALF_OPEN_LOOKUP_INVALID_VALUE) { SESSION_DBG ("half-open was removed!"); return -1; } session_lookup_del_half_open (tc); /* Get the app's index from the handle we stored when opening connection * and the opaque (api_context for external apps) from transport session * index */ app = application_get_if_valid (handle >> 32); if (!app) return -1; opaque = tc->s_index; /* * Allocate new session with fifos (svm segments are allocated if needed) */ if (!is_fail) { sm = application_get_connect_segment_manager (app); alloc_fifos = !application_is_builtin_proxy (app); if (session_alloc_and_init (sm, tc, alloc_fifos, &new_s)) { is_fail = 1; error = -1; } else { new_s->app_index = app->index; new_si = new_s->session_index; new_ti = new_s->thread_index; } } /* * Notify client application */ if (app->cb_fns.session_connected_callback (app->index, opaque, new_s, is_fail)) { SESSION_DBG ("failed to notify app"); if (!is_fail) { new_s = session_get (new_si, new_ti); stream_session_disconnect_transport (new_s); } } else { if (!is_fail) { new_s = session_get (new_si, new_ti); new_s->session_state = SESSION_STATE_READY; } } return error; } typedef struct _session_switch_pool_args { u32 session_index; u32 thread_index; u32 new_thread_index; u32 new_session_index; } session_switch_pool_args_t; static void session_switch_pool (void *cb_args) { session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args; transport_proto_t tp; stream_session_t *s; ASSERT (args->thread_index == vlib_get_thread_index ()); s = session_get (args->session_index, args->thread_index); s->server_tx_fifo->master_session_index = args->new_session_index; s->server_tx_fifo->master_thread_index = args->new_thread_index; tp = session_get_transport_proto (s); tp_vfts[tp].cleanup (s->connection_index, s->thread_index); session_free (s); clib_mem_free (cb_args); } /** * Move dgram session to the right thread */ int session_dgram_connect_notify (transport_connection_t * tc, u32 old_thread_index, stream_session_t ** new_session) { stream_session_t *new_s; session_switch_pool_args_t *rpc_args; /* * Clone half-open session to the right thread. */ new_s = session_clone_safe (tc->s_index, old_thread_index); new_s->connection_index = tc->c_index; new_s->server_rx_fifo->master_session_index = new_s->session_index; new_s->server_rx_fifo->master_thread_index = new_s->thread_index; new_s->session_state = SESSION_STATE_READY; session_lookup_add_connection (tc, session_handle (new_s)); /* * Ask thread owning the old session to clean it up and make us the tx * fifo owner */ rpc_args = clib_mem_alloc (sizeof (*rpc_args)); rpc_args->new_session_index = new_s->session_index; rpc_args->new_thread_index = new_s->thread_index; rpc_args->session_index = tc->s_index; rpc_args->thread_index = old_thread_index; session_send_rpc_evt_to_thread (rpc_args->thread_index, session_switch_pool, rpc_args); tc->s_index = new_s->session_index; new_s->connection_index = tc->c_index; *new_session = new_s; return 0; } void stream_session_accept_notify (transport_connection_t * tc) { application_t *server; stream_session_t *s; s = session_get (tc->s_index, tc->thread_index); server = application_get (s->app_index); server->cb_fns.session_accept_callback (s); } /** * Notification from transport that connection is being closed. * * A disconnect is sent to application but state is not removed. Once * disconnect is acknowledged by application, session disconnect is called. * Ultimately this leads to close being called on transport (passive close). */ void stream_session_disconnect_notify (transport_connection_t * tc) { application_t *server; stream_session_t *s; s = session_get (tc->s_index, tc->thread_index); s->session_state = SESSION_STATE_CLOSING; server = application_get_if_valid (s->app_index); if (server) server->cb_fns.session_disconnect_callback (s); } /** * Cleans up session and lookup table. * * Transport connection must still be valid. */ void stream_session_delete (stream_session_t * s) { int rv; /* Delete from the main lookup table. */ if ((rv = session_lookup_del_session (s))) clib_warning ("hash delete error, rv %d", rv); /* Cleanup fifo segments */ segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo, s->server_tx_fifo); session_free (s); } /** * Notification from transport that connection is being deleted * * This removes the session if it is still valid. It should be called only on * previously fully established sessions. For instance failed connects should * call stream_session_connect_notify and indicate that the connect has * failed. */ void stream_session_delete_notify (transport_connection_t * tc) { stream_session_t *s; /* App might've been removed already */ s = session_get_if_valid (tc->s_index, tc->thread_index); if (!s) return; stream_session_delete (s); } /** * Notify application that connection has been reset. */ void stream_session_reset_notify (transport_connection_t * tc) { stream_session_t *s; application_t *app; s = session_get (tc->s_index, tc->thread_index); s->session_state = SESSION_STATE_CLOSED; app = application_get (s->app_index); app->cb_fns.session_reset_callback (s); } /** * Accept a stream session. Optionally ping the server by callback. */ int stream_session_accept (transport_connection_t * tc, u32 listener_index, u8 notify) { application_t *server; stream_session_t *s, *listener; segment_manager_t *sm; int rv; /* Find the server */ listener = listen_session_get (listener_index); server = application_get (listener->app_index); sm = application_get_listen_segment_manager (server, listener); if ((rv = session_alloc_and_init (sm, tc, 1, &s))) return rv; s->app_index = server->index; s->listener_index = listener_index; s->session_state = SESSION_STATE_ACCEPTING; /* Shoulder-tap the server */ if (notify) { server->cb_fns.session_accept_callback (s); } return 0; } int session_open_cl (u32 app_index, session_endpoint_t * rmt, u32 opaque) { transport_connection_t *tc; transport_endpoint_t *tep; segment_manager_t *sm; stream_session_t *s; application_t *app; int rv; tep = session_endpoint_to_transport (rmt); rv = tp_vfts[rmt->transport_proto].open (tep); if (rv < 0) { SESSION_DBG ("Transport failed to open connection."); return VNET_API_ERROR_SESSION_CONNECT; } tc = tp_vfts[rmt->transport_proto].get_half_open ((u32) rv); /* For dgram type of service, allocate session and fifos now. */ app = application_get (app_index); sm = application_get_connect_segment_manager (app); if (session_alloc_and_init (sm, tc, 1, &s)) return -1; s->app_index = app->index; s->session_state = SESSION_STATE_OPENED; /* Tell the app about the new event fifo for this session */ app->cb_fns.session_connected_callback (app->index, opaque, s, 0); return 0; } int session_open_vc (u32 app_index, session_endpoint_t * rmt, u32 opaque) { transport_connection_t *tc; transport_endpoint_t *tep; u64 handle; int rv; tep = session_endpoint_to_transport (rmt); rv = tp_vfts[rmt->transport_proto].open (tep); if (rv < 0) { SESSION_DBG ("Transport failed to open connection."); return VNET_API_ERROR_SESSION_CONNECT; } tc = tp_vfts[rmt->transport_proto].get_half_open ((u32) rv); /* If transport offers a stream service, only allocate session once the * connection has been established. * Add connection to half-open table and save app and tc index. The * latter is needed to help establish the connection while the former * is needed when the connect notify comes and we have to notify the * external app */ handle = (((u64) app_index) << 32) | (u64) tc->c_index; session_lookup_add_half_open (tc, handle); /* Store api_context (opaque) for when the reply comes. Not the nicest * thing but better than allocating a separate half-open pool. */ tc->s_index = opaque; return 0; } int session_open_app (u32 app_index, session_endpoint_t * rmt, u32 opaque) { session_endpoint_extended_t *sep = (session_endpoint_extended_t *) rmt; sep->app_index = app_index; sep->opaque = opaque; return tp_vfts[rmt->transport_proto].open ((transport_endpoint_t *) sep); } typedef int (*session_open_service_fn) (u32, session_endpoint_t *, u32); /* *INDENT-OFF* */ static session_open_service_fn session_open_srv_fns[TRANSPORT_N_SERVICES] = { session_open_vc, session_open_cl, session_open_app, }; /* *INDENT-ON* */ /** * Ask transport to open connection to remote transport endpoint. * * Stores handle for matching request with reply since the call can be * asynchronous. For instance, for TCP the 3-way handshake must complete * before reply comes. Session is only created once connection is established. * * @param app_index Index of the application requesting the connect * @param st Session type requested. * @param tep Remote transport endpoint * @param opaque Opaque data (typically, api_context) the application expects * on open completion. */ int session_open (u32 app_index, session_endpoint_t * rmt, u32 opaque) { transport_service_type_t tst = tp_vfts[rmt->transport_proto].service_type; return session_open_srv_fns[tst] (app_index, rmt, opaque); } int session_listen_vc (stream_session_t * s, session_endpoint_t * sep) { transport_connection_t *tc; u32 tci; /* Transport bind/listen */ tci = tp_vfts[sep->transport_proto].bind (s->session_index, session_endpoint_to_transport (sep)); if (tci == (u32) ~ 0) return -1; /* Attach transport to session */ s->connection_index = tci; tc = tp_vfts[sep->transport_proto].get_listener (tci); /* Weird but handle it ... */ if (tc == 0) return -1; /* Add to the main lookup table */ session_lookup_add_connection (tc, s->session_index); return 0; } int session_listen_cl (stream_session_t * s, session_endpoint_t * sep) { transport_connection_t *tc; application_t *server; segment_manager_t *sm; u32 tci; /* Transport bind/listen */ tci = tp_vfts[sep->transport_proto].bind (s->session_index, session_endpoint_to_transport (sep)); if (tci == (u32) ~ 0) return -1; /* Attach transport to session */ s->connection_index = tci; tc = tp_vfts[sep->transport_proto].get_listener (tci); /* Weird but handle it ... */ if (tc == 0) return -1; server = application_get (s->app_index); sm = application_get_listen_segment_manager (server, s); if (session_alloc_fifos (sm, s)) return -1; /* Add to the main lookup table */ session_lookup_add_connection (tc, s->session_index); return 0; } int session_listen_app (stream_session_t * s, session_endpoint_t * sep) { session_endpoint_extended_t esep; clib_memcpy (&esep, sep, sizeof (*sep)); esep.app_index = s->app_index; return tp_vfts[sep->transport_proto].bind (s->session_index, (transport_endpoint_t *) & esep); } typedef int (*session_listen_service_fn) (stream_session_t *, session_endpoint_t *); /* *INDENT-OFF* */ static session_listen_service_fn session_listen_srv_fns[TRANSPORT_N_SERVICES] = { session_listen_vc, session_listen_cl, session_listen_app, }; /* *INDENT-ON* */ /** * Ask transport to listen on local transport endpoint. * * @param s Session for which listen will be called. Note that unlike * established sessions, listen sessions are not associated to a * thread. * @param tep Local endpoint to be listened on. */ int stream_session_listen (stream_session_t * s, session_endpoint_t * sep) { transport_service_type_t tst = tp_vfts[sep->transport_proto].service_type; return session_listen_srv_fns[tst] (s, sep); } /** * Ask transport to stop listening on local transport endpoint. * * @param s Session to stop listening on. It must be in state LISTENING. */ int stream_session_stop_listen (stream_session_t * s) { transport_proto_t tp = session_get_transport_proto (s); transport_connection_t *tc; if (s->session_state != SESSION_STATE_LISTENING) { clib_warning ("not a listening session"); return -1; } tc = tp_vfts[tp].get_listener (s->connection_index); if (!tc) { clib_warning ("no transport"); return VNET_API_ERROR_ADDRESS_NOT_IN_USE; } session_lookup_del_connection (tc); tp_vfts[tp].unbind (s->connection_index); return 0; } /** * Initialize session disconnect. * * Request is always sent to session node to ensure that all outstanding * requests are served before transport is notified. */ void stream_session_disconnect (stream_session_t * s) { u32 thread_index = vlib_get_thread_index (); session_manager_main_t *smm = &session_manager_main; session_event_t *evt; if (!s) return; if (s->session_state >= SESSION_STATE_CLOSING) { /* Session already closed. Clear the tx fifo */ if (s->session_state == SESSION_STATE_CLOSED) svm_fifo_dequeue_drop_all (s->server_tx_fifo); return; } s->session_state = SESSION_STATE_CLOSING; /* If we are in the handler thread, or being called with the worker barrier * held (api/cli), just append a new event to pending disconnects vector. */ if ((thread_index == 0 && !vlib_get_current_process (vlib_get_main ())) || thread_index == s->thread_index) { ASSERT (s->thread_index == thread_index || thread_index == 0); vec_add2 (smm->pending_disconnects[s->thread_index], evt, 1); memset (evt, 0, sizeof (*evt)); evt->session_handle = session_handle (s); evt->event_type = FIFO_EVENT_DISCONNECT; } else session_send_ctrl_evt_to_thread (s, FIFO_EVENT_DISCONNECT); } /** * Notify transport the session can be disconnected. This should eventually * result in a delete notification that allows us to cleanup session state. * Called for both active/passive disconnects. * * Must be called from the session's thread. */ void stream_session_disconnect_transport (stream_session_t * s) { s->session_state = SESSION_STATE_CLOSED; tp_vfts[session_get_transport_proto (s)].close (s->connection_index, s->thread_index); } /** * Cleanup transport and session state. * * Notify transport of the cleanup and free the session. This should * be called only if transport reported some error and is already * closed. */ void stream_session_cleanup (stream_session_t * s) { s->session_state = SESSION_STATE_CLOSED; /* Delete from main lookup table before we axe the the transport */ session_lookup_del_session (s); tp_vfts[session_get_transport_proto (s)].cleanup (s->connection_index, s->thread_index); /* Since we called cleanup, no delete notification will come. So, make * sure the session is properly freed. */ segment_manager_dealloc_fifos (s->svm_segment_index, s->server_rx_fifo, s->server_tx_fifo); session_free (s); } transport_service_type_t session_transport_service_type (stream_session_t * s) { transport_proto_t tp; tp = session_get_transport_proto (s); return transport_protocol_service_type (tp); } transport_tx_fn_type_t session_transport_tx_fn_type (stream_session_t * s) { transport_proto_t tp; tp = session_get_transport_proto (s); return transport_protocol_tx_fn_type (tp); } u8 session_tx_is_dgram (stream_session_t * s) { return (session_transport_tx_fn_type (s) == TRANSPORT_TX_DGRAM); } /** * Allocate event queues in the shared-memory segment * * That can either be a newly created memfd segment, that will need to be * mapped by all stack users, or the binary api's svm region. The latter is * assumed to be already mapped. NOTE that this assumption DOES NOT hold if * api clients bootstrap shm api over sockets (i.e. use memfd segments) and * vpp uses api svm region for event queues. */ void session_vpp_event_queues_allocate (session_manager_main_t * smm) { u32 evt_q_length = 2048, evt_size = sizeof (session_event_t); ssvm_private_t *eqs = &smm->evt_qs_segment; api_main_t *am = &api_main; u64 eqs_size = 64 << 20; pid_t vpp_pid = getpid (); void *oldheap; int i; if (smm->configured_event_queue_length) evt_q_length = smm->configured_event_queue_length; if (smm->evt_qs_use_memfd_seg) { if (smm->evt_qs_segment_size) eqs_size = smm->evt_qs_segment_size; eqs->ssvm_size = eqs_size; eqs->i_am_master = 1; eqs->my_pid = vpp_pid; eqs->name = format (0, "%s%c", "evt-qs-segment", 0); eqs->requested_va = smm->session_baseva; if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD)) { clib_warning ("failed to initialize queue segment"); return; } } if (smm->evt_qs_use_memfd_seg) oldheap = ssvm_push_heap (eqs->sh); else oldheap = svm_push_data_heap (am->vlib_rp); for (i = 0; i < vec_len (smm->vpp_event_queues); i++) { svm_msg_q_cfg_t _cfg, *cfg = &_cfg; u32 notif_q_size = clib_max (16, evt_q_length >> 4); svm_msg_q_ring_cfg_t rc[SESSION_MQ_N_RINGS] = { {evt_q_length, evt_size, 0} , {notif_q_size, 256, 0} }; cfg->consumer_pid = 0; cfg->n_rings = 2; cfg->q_nitems = evt_q_length; cfg->ring_cfgs = rc; smm->vpp_event_queues[i] = svm_msg_q_alloc (cfg); } if (smm->evt_qs_use_memfd_seg) ssvm_pop_heap (oldheap); else svm_pop_heap (oldheap); } ssvm_private_t * session_manager_get_evt_q_segment (void) { session_manager_main_t *smm = &session_manager_main; if (smm->evt_qs_use_memfd_seg) return &smm->evt_qs_segment; return 0; } /* *INDENT-OFF* */ static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = { session_tx_fifo_peek_and_snd, session_tx_fifo_dequeue_and_snd, session_tx_fifo_dequeue_internal, session_tx_fifo_dequeue_and_snd }; /* *INDENT-ON* */ /** * Initialize session layer for given transport proto and ip version * * Allocates per session type (transport proto + ip version) data structures * and adds arc from session queue node to session type output node. */ void session_register_transport (transport_proto_t transport_proto, const transport_proto_vft_t * vft, u8 is_ip4, u32 output_node) { session_manager_main_t *smm = &session_manager_main; session_type_t session_type; u32 next_index = ~0; session_type = session_type_from_proto_and_ip (transport_proto, is_ip4); vec_validate (smm->session_type_to_next, session_type); vec_validate (smm->session_tx_fns, session_type); /* *INDENT-OFF* */ if (output_node != ~0) { foreach_vlib_main (({ next_index = vlib_node_add_next (this_vlib_main, session_queue_node.index, output_node); })); } /* *INDENT-ON* */ smm->session_type_to_next[session_type] = next_index; smm->session_tx_fns[session_type] = session_tx_fns[vft->tx_type]; } transport_connection_t * session_get_transport (stream_session_t * s) { transport_proto_t tp; if (s->session_state != SESSION_STATE_LISTENING) { tp = session_get_transport_proto (s); return tp_vfts[tp].get_connection (s->connection_index, s->thread_index); } return 0; } transport_connection_t * listen_session_get_transport (stream_session_t * s) { transport_proto_t tp = session_get_transport_proto (s); return tp_vfts[tp].get_listener (s->connection_index); } int listen_session_get_local_session_endpoint (stream_session_t * listener, session_endpoint_t * sep) { transport_proto_t tp = session_get_transport_proto (listener); transport_connection_t *tc; tc = tp_vfts[tp].get_listener (listener->connection_index); if (!tc) { clib_warning ("no transport"); return -1; } /* N.B. The ip should not be copied because this is the local endpoint */ sep->port = tc->lcl_port; sep->transport_proto = tc->proto; sep->is_ip4 = tc->is_ip4; return 0; } void session_flush_frames_main_thread (vlib_main_t * vm) { ASSERT (vlib_get_thread_index () == 0); vlib_process_signal_event_mt (vm, session_queue_process_node.index, SESSION_Q_PROCESS_FLUSH_FRAMES, 0); } static clib_error_t * session_manager_main_enable (vlib_main_t * vm) { segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args; session_manager_main_t *smm = &session_manager_main; vlib_thread_main_t *vtm = vlib_get_thread_main (); u32 num_threads, preallocated_sessions_per_worker; int i, j; num_threads = 1 /* main thread */ + vtm->n_threads; if (num_threads < 1) return clib_error_return (0, "n_thread_stacks not set"); /* configure per-thread ** vectors */ vec_validate (smm->sessions, num_threads - 1); vec_validate (smm->tx_buffers, num_threads - 1); vec_validate (smm->pending_event_vector, num_threads - 1); vec_validate (smm->pending_disconnects, num_threads - 1); vec_validate (smm->free_event_vector, num_threads - 1); vec_validate (smm->vpp_event_queues, num_threads - 1); vec_validate (smm->peekers_rw_locks, num_threads - 1); vec_validate_aligned (smm->ctx, num_threads - 1, CLIB_CACHE_LINE_BYTES); for (i = 0; i < TRANSPORT_N_PROTO; i++) { vec_validate (smm->current_enqueue_epoch[i], num_threads - 1); vec_validate (smm->session_to_enqueue[i], num_threads - 1); for (j = 0; j < num_threads; j++) smm->current_enqueue_epoch[i][j] = 1; } for (i = 0; i < num_threads; i++) { vec_validate (smm->free_event_vector[i], 0); _vec_len (smm->free_event_vector[i]) = 0; vec_validate (smm->pending_event_vector[i], 0); _vec_len (smm->pending_event_vector[i]) = 0; vec_validate (smm->pending_disconnects[i], 0); _vec_len (smm->pending_disconnects[i]) = 0; if (num_threads > 1) clib_rwlock_init (&smm->peekers_rw_locks[i]); } #if SESSION_DEBUG vec_validate (smm->last_event_poll_by_thread, num_threads - 1); #endif /* Allocate vpp event queues segment and queue */ session_vpp_event_queues_allocate (smm); /* Initialize fifo segment main baseva and timeout */ sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size; sm_args->size = smm->session_va_space_size; segment_manager_main_init (sm_args); /* Preallocate sessions */ if (smm->preallocated_sessions) { if (num_threads == 1) { pool_init_fixed (smm->sessions[0], smm->preallocated_sessions); } else { int j; preallocated_sessions_per_worker = (1.1 * (f64) smm->preallocated_sessions / (f64) (num_threads - 1)); for (j = 1; j < num_threads; j++) { pool_init_fixed (smm->sessions[j], preallocated_sessions_per_worker); } } } session_lookup_init (); app_namespaces_init (); transport_init (); smm->is_enabled = 1; /* Enable transports */ transport_enable_disable (vm, 1); return 0; } void session_node_enable_disable (u8 is_en) { u8 state = is_en ? VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_DISABLED; vlib_thread_main_t *vtm = vlib_get_thread_main (); u8 have_workers = vtm->n_threads != 0; /* *INDENT-OFF* */ foreach_vlib_main (({ if (have_workers && ii == 0) { vlib_node_set_state (this_vlib_main, session_queue_process_node.index, state); if (is_en) { vlib_node_t *n = vlib_get_node (this_vlib_main, session_queue_process_node.index); vlib_start_process (this_vlib_main, n->runtime_index); } else { vlib_process_signal_event_mt (this_vlib_main, session_queue_process_node.index, SESSION_Q_PROCESS_STOP, 0); } continue; } vlib_node_set_state (this_vlib_main, session_queue_node.index, state); })); /* *INDENT-ON* */ } clib_error_t * vnet_session_enable_disable (vlib_main_t * vm, u8 is_en) { clib_error_t *error = 0; if (is_en) { if (session_manager_main.is_enabled) return 0; session_node_enable_disable (is_en); error = session_manager_main_enable (vm); } else { session_manager_main.is_enabled = 0; session_node_enable_disable (is_en); } return error; } clib_error_t * session_manager_main_init (vlib_main_t * vm) { session_manager_main_t *smm = &session_manager_main; smm->session_baseva = 0x200000000ULL; smm->session_va_space_size = (u64) 128 << 30; smm->evt_qs_segment_size = 64 << 20; smm->is_enabled = 0; return 0; } VLIB_INIT_FUNCTION (session_manager_main_init); static clib_error_t * session_config_fn (vlib_main_t * vm, unformat_input_t * input) { session_manager_main_t *smm = &session_manager_main; u32 nitems; uword tmp; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "event-queue-length %d", &nitems)) { if (nitems >= 2048) smm->configured_event_queue_length = nitems; else clib_warning ("event queue length %d too small, ignored", nitems); } else if (unformat (input, "preallocated-sessions %d", &smm->preallocated_sessions)) ; else if (unformat (input, "v4-session-table-buckets %d", &smm->configured_v4_session_table_buckets)) ; else if (unformat (input, "v4-halfopen-table-buckets %d", &smm->configured_v4_halfopen_table_buckets)) ; else if (unformat (input, "v6-session-table-buckets %d", &smm->configured_v6_session_table_buckets)) ; else if (unformat (input, "v6-halfopen-table-buckets %d", &smm->configured_v6_halfopen_table_buckets)) ; else if (unformat (input, "v4-session-table-memory %U", unformat_memory_size, &tmp)) { if (tmp >= 0x100000000) return clib_error_return (0, "memory size %llx (%lld) too large", tmp, tmp); smm->configured_v4_session_table_memory = tmp; } else if (unformat (input, "v4-halfopen-table-memory %U", unformat_memory_size, &tmp)) { if (tmp >= 0x100000000) return clib_error_return (0, "memory size %llx (%lld) too large", tmp, tmp); smm->configured_v4_halfopen_table_memory = tmp; } else if (unformat (input, "v6-session-table-memory %U", unformat_memory_size, &tmp)) { if (tmp >= 0x100000000) return clib_error_return (0, "memory size %llx (%lld) too large", tmp, tmp); smm->configured_v6_session_table_memory = tmp; } else if (unformat (input, "v6-halfopen-table-memory %U", unformat_memory_size, &tmp)) { if (tmp >= 0x100000000) return clib_error_return (0, "memory size %llx (%lld) too large", tmp, tmp); smm->configured_v6_halfopen_table_memory = tmp; } else if (unformat (input, "local-endpoints-table-memory %U", unformat_memory_size, &tmp)) { if (tmp >= 0x100000000) return clib_error_return (0, "memory size %llx (%lld) too large", tmp, tmp); smm->local_endpoints_table_memory = tmp; } else if (unformat (input, "local-endpoints-table-buckets %d", &smm->local_endpoints_table_buckets)) ; else if (unformat (input, "evt_qs_memfd_seg")) smm->evt_qs_use_memfd_seg = 1; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } return 0; } VLIB_CONFIG_FUNCTION (session_config_fn, "session"); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */