summaryrefslogtreecommitdiffstats
path: root/src/vnet/l2/l2_input_classify.c
blob: 53d46399daf5599726a24f30ac9dbad1058a0c78 (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
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * l2_classify.c
 */

#include <vnet/l2/l2_classify.h>
#include <vnet/api_errno.h>

/**
 * @file
 * @brief L2 input classifier.
 *
 * @sa @ref vnet/vnet/classify/vnet_classify.c
 * @sa @ref vnet/vnet/classify/vnet_classify.h
 */

/**
 * @brief l2_input_classifier packet trace record.
 */
typedef struct
{
  /** interface handle for the ith packet */
  u32 sw_if_index;
  /** graph arc index selected for this packet */
  u32 next_index;
  /** classifier table which provided the final result */
  u32 table_index;
  /** offset in classifier heap of the corresponding session */
  u32 session_offset;
} l2_input_classify_trace_t;

/**
 * @brief vlib node runtime.
 */
typedef struct
{
  /** use-case independent main object pointer */
  vnet_classify_main_t *vcm;
  /** l2 input classifier main object pointer */
  l2_input_classify_main_t *l2cm;
} l2_input_classify_runtime_t;

/** Packet trace format function. */
static u8 *
format_l2_input_classify_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 *);
  l2_input_classify_trace_t *t = va_arg (*args, l2_input_classify_trace_t *);

  s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d",
	      t->sw_if_index, t->table_index, t->session_offset,
	      t->next_index);
  return s;
}

extern l2_input_classify_main_t l2_input_classify_main;

#ifndef CLIB_MARCH_VARIANT
/** l2 input classifier main data structure. */
l2_input_classify_main_t l2_input_classify_main;
#endif /* CLIB_MARCH_VARIANT */

#define foreach_l2_input_classify_error               \
_(MISS, "Classify misses")                      \
_(HIT, "Classify hits")                         \
_(CHAIN_HIT, "Classify hits after chain walk")  \
_(DROP, "L2 Classify Drops")

typedef enum
{
#define _(sym,str) L2_INPUT_CLASSIFY_ERROR_##sym,
  foreach_l2_input_classify_error
#undef _
    L2_INPUT_CLASSIFY_N_ERROR,
} l2_input_classify_error_t;

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

/**
 * @brief l2 input classifier node.
 * @node l2-input-classify
 *
 * This is the l2 input classifier dispatch node
 *
 * @param vm    vlib_main_t corresponding to the current thread.
 * @param node  vlib_node_runtime_t data for this node.
 * @param frame vlib_frame_t whose contents should be dispatched.
 *
 * @par Graph mechanics: buffer metadata, next index usage
 *
 * @em Uses:
 * - <code>(l2_input_classify_runtime_t *)
 *         rt->classify_table_index_by_sw_if_index</code>
 *	- Head of the per-interface, per-protocol classifier table chain
 * 	  for a specific interface.
 *      - @c ~0 => send pkts to the next feature in the L2 feature chain.
 * - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
 * 	- Indicates the @c sw_if_index value of the interface that the
 * 	  packet was received on.
 * - <code>vnet_buffer(b0)->l2.feature_bitmap</code>
 * 	- Used to steer packets across l2 features enabled on the interface
 * - <code>(vnet_classify_entry_t) e0->next_index</code>
 *	- Used to steer traffic when the classifier hits on a session
 * - <code>(vnet_classify_entry_t) e0->advance</code>
 *	- Signed quantity applied via <code>vlib_buffer_advance</code>
 * 	  when the classifier hits on a session
 * - <code>(vnet_classify_table_t) t0->miss_next_index</code>
 *	- Used to steer traffic when the classifier misses
 *
 * @em Sets:
 * - <code>vnet_buffer (b0)->l2_classify.table_index</code>
 * 	- Classifier table index of the first classifier table in
 *	the classifier table chain
 * - <code>vnet_buffer (b0)->l2_classify.hash</code>
 * 	- Bounded-index extensible hash corresponding to the
 *	masked fields in the current packet
 * - <code>vnet_buffer (b0)->l2.feature_bitmap</code>
 * 	- Used to steer packets across l2 features enabled on the interface
 * - <code>vnet_buffer (b0)->l2_classify.opaque_index</code>
 * 	- Copied from the classifier session object upon classifier hit
 *
 * @em Counters:
 * - <code>L2_INPUT_CLASSIFY_ERROR_MISS</code> Classifier misses
 * - <code>L2_INPUT_CLASSIFY_ERROR_HIT</code> Classifier hits
 * - <code>L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT</code>
 *   Classifier hits in other than the first table
 */

VLIB_NODE_FN (l2_input_classify_node) (vlib_main_t * vm,
				       vlib_node_runtime_t * node,
				       vlib_frame_t * frame)
{
  u32 n_left_from, *from, *to_next;
  l2_input_classify_next_t next_index;
  l2_input_classify_main_t *cm = &l2_input_classify_main;
  vnet_classify_main_t *vcm = cm->vnet_classify_main;
  l2_input_classify_runtime_t *rt =
    (l2_input_classify_runtime_t *) node->runtime_data;
  u32 hits = 0;
  u32 misses = 0;
  u32 chain_hits = 0;
  f64 now;
  u32 n_next_nodes;

  n_next_nodes = node->n_next_nodes;

  now = vlib_time_now (vm);

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

  /* First pass: compute hash */

  while (n_left_from >= 4)
    {
      vlib_buffer_t *b0, *b1;
      u32 bi0, bi1;
      ethernet_header_t *h0, *h1;
      u32 sw_if_index0, sw_if_index1;
      u16 type0, type1;
      int type_index0, type_index1;
      vnet_classify_table_t *t0, *t1;
      u32 table_index0, table_index1;
      u64 hash0, hash1;


      /* prefetch next iteration */
      {
	vlib_buffer_t *p2, *p3;

	p2 = vlib_get_buffer (vm, from[2]);
	p3 = vlib_get_buffer (vm, from[3]);

	vlib_prefetch_buffer_header (p2, STORE);
	clib_prefetch_store (p2->data);
	vlib_prefetch_buffer_header (p3, STORE);
	clib_prefetch_store (p3->data);
      }

      bi0 = from[0];
      b0 = vlib_get_buffer (vm, bi0);
      h0 = vlib_buffer_get_current (b0);

      bi1 = from[1];
      b1 = vlib_get_buffer (vm, bi1);
      h1 = vlib_buffer_get_current (b1);

      sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
      vnet_buffer (b0)->l2_classify.table_index = ~0;

      sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
      vnet_buffer (b1)->l2_classify.table_index = ~0;

      /* Select classifier table based on ethertype */
      type0 = clib_net_to_host_u16 (h0->type);
      type1 = clib_net_to_host_u16 (h1->type);

      type_index0 = (type0 == ETHERNET_TYPE_IP4)
	? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER;
      type_index0 = (type0 == ETHERNET_TYPE_IP6)
	? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0;

      type_index1 = (type1 == ETHERNET_TYPE_IP4)
	? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER;
      type_index1 = (type1 == ETHERNET_TYPE_IP6)
	? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index1;

      vnet_buffer (b0)->l2_classify.table_index =
	table_index0 =
	rt->l2cm->classify_table_index_by_sw_if_index
	[type_index0][sw_if_index0];

      if (table_index0 != ~0)
	{
	  t0 = pool_elt_at_index (vcm->tables, table_index0);

	  vnet_buffer (b0)->l2_classify.hash = hash0 =
	    vnet_classify_hash_packet (t0, (u8 *) h0);
	  vnet_classify_prefetch_bucket (t0, hash0);
	}

      vnet_buffer (b1)->l2_classify.table_index =
	table_index1 =
	rt->l2cm->classify_table_index_by_sw_if_index
	[type_index1][sw_if_index1];

      if (table_index1 != ~0)
	{
	  t1 = pool_elt_at_index (vcm->tables, table_index1);

	  vnet_buffer (b1)->l2_classify.hash = hash1 =
	    vnet_classify_hash_packet (t1, (u8 *) h1);
	  vnet_classify_prefetch_bucket (t1, hash1);
	}

      from += 2;
      n_left_from -= 2;
    }

  while (n_left_from > 0)
    {
      vlib_buffer_t *b0;
      u32 bi0;
      ethernet_header_t *h0;
      u32 sw_if_index0;
      u16 type0;
      u32 type_index0;
      vnet_classify_table_t *t0;
      u32 table_index0;
      u64 hash0;

      bi0 = from[0];
      b0 = vlib_get_buffer (vm, bi0);
      h0 = vlib_buffer_get_current (b0);

      sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
      vnet_buffer (b0)->l2_classify.table_index = ~0;

      /* Select classifier table based on ethertype */
      type0 = clib_net_to_host_u16 (h0->type);

      type_index0 = (type0 == ETHERNET_TYPE_IP4)
	? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER;
      type_index0 = (type0 == ETHERNET_TYPE_IP6)
	? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0;

      vnet_buffer (b0)->l2_classify.table_index =
	table_index0 = rt->l2cm->classify_table_index_by_sw_if_index
	[type_index0][sw_if_index0];

      if (table_index0 != ~0)
	{
	  t0 = pool_elt_at_index (vcm->tables, table_index0);

	  vnet_buffer (b0)->l2_classify.hash = hash0 =
	    vnet_classify_hash_packet (t0, (u8 *) h0);
	  vnet_classify_prefetch_bucket (t0, hash0);
	}
      from++;
      n_left_from--;
    }

  next_index = node->cached_next_index;
  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

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

      /* Not enough load/store slots to dual loop... */
      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  u32 next0 = ~0;	/* next l2 input feature, please... */
	  ethernet_header_t *h0;
	  u32 table_index0;
	  u64 hash0;
	  vnet_classify_table_t *t0;
	  vnet_classify_entry_t *e0;

	  if (PREDICT_TRUE (n_left_from > 2))
	    {
	      vlib_buffer_t *p2 = vlib_get_buffer (vm, from[2]);
	      u64 phash2;
	      u32 table_index2;
	      vnet_classify_table_t *tp2;

	      /*
	       * Prefetch table entry two ahead. Buffer / data
	       * were prefetched above...
	       */
	      table_index2 = vnet_buffer (p2)->l2_classify.table_index;

	      if (PREDICT_TRUE (table_index2 != ~0))
		{
		  tp2 = pool_elt_at_index (vcm->tables, table_index2);
		  phash2 = vnet_buffer (p2)->l2_classify.hash;
		  vnet_classify_prefetch_entry (tp2, phash2);
		}
	    }

	  /* speculatively enqueue b0 to the current next frame */
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);
	  h0 = vlib_buffer_get_current (b0);
	  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
	  e0 = 0;
	  vnet_buffer (b0)->l2_classify.opaque_index = ~0;

	  if (PREDICT_TRUE (table_index0 != ~0))
	    {
	      hash0 = vnet_buffer (b0)->l2_classify.hash;
	      t0 = pool_elt_at_index (vcm->tables, table_index0);

	      e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
	      if (e0)
		{
		  vnet_buffer (b0)->l2_classify.opaque_index
		    = e0->opaque_index;
		  vlib_buffer_advance (b0, e0->advance);
		  next0 = (e0->next_index < n_next_nodes) ?
		    e0->next_index : next0;
		  hits++;
		}
	      else
		{
		  while (1)
		    {
		      if (t0->next_table_index != ~0)
			t0 = pool_elt_at_index (vcm->tables,
						t0->next_table_index);
		      else
			{
			  next0 = (t0->miss_next_index < n_next_nodes) ?
			    t0->miss_next_index : next0;
			  misses++;
			  break;
			}

		      hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
		      e0 =
			vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
		      if (e0)
			{
			  vnet_buffer (b0)->l2_classify.opaque_index
			    = e0->opaque_index;
			  vlib_buffer_advance (b0, e0->advance);
			  next0 = (e0->next_index < n_next_nodes) ?
			    e0->next_index : next0;
			  hits++;
			  chain_hits++;
			  break;
			}
		    }
		}
	    }

	  if (PREDICT_FALSE (next0 == 0))
	    b0->error = node->errors[L2_INPUT_CLASSIFY_ERROR_DROP];

	  /* Determine the next node and remove ourself from bitmap */
	  if (PREDICT_TRUE (next0 == ~0))
	    next0 = vnet_l2_feature_next (b0, cm->l2_inp_feat_next,
					  L2INPUT_FEAT_INPUT_CLASSIFY);
	  else
	    vnet_buffer (b0)->l2.feature_bitmap &=
	      ~L2INPUT_FEAT_INPUT_CLASSIFY;

	  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
			     && (b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      l2_input_classify_trace_t *t =
		vlib_add_trace (vm, node, b0, sizeof (*t));
	      t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	      t->table_index = table_index0;
	      t->next_index = next0;
	      t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0;
	    }

	  /* verify speculative enqueue, maybe switch current next frame */
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next,
					   bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  vlib_node_increment_counter (vm, node->node_index,
			       L2_INPUT_CLASSIFY_ERROR_MISS, misses);
  vlib_node_increment_counter (vm, node->node_index,
			       L2_INPUT_CLASSIFY_ERROR_HIT, hits);
  vlib_node_increment_counter (vm, node->node_index,
			       L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
  return frame->n_vectors;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (l2_input_classify_node) = {
  .name = "l2-input-classify",
  .vector_size = sizeof (u32),
  .format_trace = format_l2_input_classify_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

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

  .runtime_data_bytes = sizeof (l2_input_classify_runtime_t),

  .n_next_nodes = L2_INPUT_CLASSIFY_N_NEXT,

  /* edit / add dispositions here */
  .next_nodes = {
    [L2_INPUT_CLASSIFY_NEXT_DROP]  = "error-drop",
    [L2_INPUT_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
    [L2_INPUT_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
    [L2_INPUT_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
    [L2_INPUT_CLASSIFY_NEXT_LI] = "li-hit",
  },
};
/* *INDENT-ON* */

#ifndef CLIB_MARCH_VARIANT
/** l2 input classsifier feature initialization. */
clib_error_t *
l2_input_classify_init (vlib_main_t * vm)
{
  l2_input_classify_main_t *cm = &l2_input_classify_main;
  l2_input_classify_runtime_t *rt;

  rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index);

  cm->vlib_main = vm;
  cm->vnet_main = vnet_get_main ();
  cm->vnet_classify_main = &vnet_classify_main;

  /* Initialize the feature next-node indexes */
  feat_bitmap_init_next_nodes (vm,
			       l2_input_classify_node.index,
			       L2INPUT_N_FEAT,
			       l2input_get_feat_names (),
			       cm->l2_inp_feat_next);
  rt->l2cm = cm;
  rt->vcm = cm->vnet_classify_main;

  return 0;
}

VLIB_INIT_FUNCTION (l2_input_classify_init);

clib_error_t *
l2_input_classify_worker_init (vlib_main_t * vm)
{
  l2_input_classify_main_t *cm = &l2_input_classify_main;
  l2_input_classify_runtime_t *rt;

  rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index);

  rt->l2cm = cm;
  rt->vcm = cm->vnet_classify_main;

  return 0;
}

VLIB_WORKER_INIT_FUNCTION (l2_input_classify_worker_init);

/** Enable/disable l2 input classification on a specific interface. */
void
vnet_l2_input_classify_enable_disable (u32 sw_if_index, int enable_disable)
{
  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_INPUT_CLASSIFY,
			      (u32) enable_disable);
}

/** @brief Set l2 per-protocol, per-interface input classification tables.
 *
 *  @param sw_if_index  interface handle
 *  @param ip4_table_index  ip4 classification table index, or ~0
 *  @param ip6_table_index  ip6 classification table index, or ~0
 *  @param other_table_index  non-ip4, non-ip6 classification table index,
 *         or ~0
 *  @returns 0 on success, VNET_API_ERROR_NO_SUCH_TABLE, TABLE2, TABLE3
 *  if the indicated (non-~0) table does not exist.
 */

int
vnet_l2_input_classify_set_tables (u32 sw_if_index,
				   u32 ip4_table_index,
				   u32 ip6_table_index, u32 other_table_index)
{
  l2_input_classify_main_t *cm = &l2_input_classify_main;
  vnet_classify_main_t *vcm = cm->vnet_classify_main;

  /* Assume that we've validated sw_if_index in the API layer */

  if (ip4_table_index != ~0 &&
      pool_is_free_index (vcm->tables, ip4_table_index))
    return VNET_API_ERROR_NO_SUCH_TABLE;

  if (ip6_table_index != ~0 &&
      pool_is_free_index (vcm->tables, ip6_table_index))
    return VNET_API_ERROR_NO_SUCH_TABLE2;

  if (other_table_index != ~0 &&
      pool_is_free_index (vcm->tables, other_table_index))
    return VNET_API_ERROR_NO_SUCH_TABLE3;

  vec_validate
    (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4],
     sw_if_index);

  vec_validate
    (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6],
     sw_if_index);

  vec_validate
    (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER],
     sw_if_index);

  cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4]
    [sw_if_index] = ip4_table_index;

  cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6]
    [sw_if_index] = ip6_table_index;

  cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER]
    [sw_if_index] = other_table_index;

  return 0;
}
#endif /* CLIB_MARCH_VARIANT */

static clib_error_t *
int_l2_input_classify_command_fn (vlib_main_t * vm,
				  unformat_input_t * input,
				  vlib_cli_command_t * cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 sw_if_index = ~0;
  u32 ip4_table_index = ~0;
  u32 ip6_table_index = ~0;
  u32 other_table_index = ~0;
  int rv;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
		    vnm, &sw_if_index))
	;
      else if (unformat (input, "ip4-table %d", &ip4_table_index))
	;
      else if (unformat (input, "ip6-table %d", &ip6_table_index))
	;
      else if (unformat (input, "other-table %d", &other_table_index))
	;
      else
	break;
    }

  if (sw_if_index == ~0)
    return clib_error_return (0, "interface must be specified");


  if (ip4_table_index == ~0 && ip6_table_index == ~0
      && other_table_index == ~0)
    {
      vlib_cli_output (vm, "L2 classification disabled");
      vnet_l2_input_classify_enable_disable (sw_if_index, 0 /* enable */ );
      return 0;
    }

  rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
					  ip6_table_index, other_table_index);
  switch (rv)
    {
    case 0:
      vnet_l2_input_classify_enable_disable (sw_if_index, 1 /* enable */ );
      break;

    default:
      return clib_error_return (0, "vnet_l2_input_classify_set_tables: %d",
				rv);
      break;
    }

  return 0;
}

/*?
 * Configure l2 input classification.
 *
 * @cliexpar
 * @cliexstart{set interface l2 input classify intfc <interface-name> [ip4-table <index>] [ip6-table <index>] [other-table <index>]}
 * @cliexend
 * @todo This is incomplete. This needs a detailed description and a
 * practical example.
 ?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (int_l2_input_classify_cli, static) = {
  .path = "set interface l2 input classify",
  .short_help =
  "set interface l2 input classify intfc <interface-name> [ip4-table <n>]\n"
  "  [ip6-table <n>] [other-table <n>]",
  .function = int_l2_input_classify_command_fn,
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
> to_next = vlib_frame_vector_args (f); to_next[f->n_vectors] = bi; f->n_vectors += 1; if (flush || f->n_vectors == VLIB_FRAME_SIZE) { vlib_put_frame_to_node (vm, next_index, f); tm->tx_frames[!is_ip4][thread_index] = 0; } } always_inline void sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4) { sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1); } always_inline void sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4, u32 fib_index, u8 flush) { sctp_main_t *tm = vnet_get_sctp_main (); u32 thread_index = vlib_get_thread_index (); u32 *to_next, next_index; vlib_frame_t *f; b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; b->error = 0; vnet_buffer (b)->sw_if_index[VLIB_TX] = fib_index; vnet_buffer (b)->sw_if_index[VLIB_RX] = 0; /* Send to IP lookup */ next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index; if (VLIB_BUFFER_TRACE_TRAJECTORY > 0) { b->pre_data[0] = 2; b->pre_data[1] = next_index; } f = tm->ip_lookup_tx_frames[!is_ip4][thread_index]; if (!f) { f = vlib_get_frame_to_node (vm, next_index); ASSERT (f); tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f; } to_next = vlib_frame_vector_args (f); to_next[f->n_vectors] = bi; f->n_vectors += 1; if (flush || f->n_vectors == VLIB_FRAME_SIZE) { vlib_put_frame_to_node (vm, next_index, f); tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0; } } always_inline void sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi, u8 is_ip4, u32 fib_index) { sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, fib_index, 0); if (vm->thread_index == 0 && vlib_num_workers ()) session_flush_frames_main_thread (vm); } /** * Convert buffer to INIT */ void sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { u32 random_seed = random_default_seed (); u16 alloc_bytes = sizeof (sctp_init_chunk_t); sctp_sub_connection_t *sub_conn = &sctp_conn->sub_conn[idx]; sctp_ipv4_addr_param_t *ip4_param = 0; sctp_ipv6_addr_param_t *ip6_param = 0; if (sub_conn->c_is_ip4) alloc_bytes += sizeof (sctp_ipv4_addr_param_t); else alloc_bytes += sizeof (sctp_ipv6_addr_param_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes); u16 pointer_offset = sizeof (init_chunk); if (sub_conn->c_is_ip4) { ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset; ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32; pointer_offset += sizeof (sctp_ipv4_addr_param_t); } else { ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset; ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0]; ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1]; pointer_offset += sizeof (sctp_ipv6_addr_param_t); } init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */ init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */ init_chunk->sctp_hdr.checksum = 0; /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */ init_chunk->sctp_hdr.verification_tag = 0x0; vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT); vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len); vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr); sctp_init_cwnd (sctp_conn); init_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd); init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed)); init_chunk->inboud_streams_count = clib_host_to_net_u16 (INBOUND_STREAMS_COUNT); init_chunk->outbound_streams_count = clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT); init_chunk->initial_tsn = clib_host_to_net_u32 (sctp_conn->local_initial_tsn); SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u", sctp_conn->local_initial_tsn); sctp_conn->local_tag = init_chunk->initiate_tag; vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index; vnet_buffer (b)->sctp.subconn_idx = idx; SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), " "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u", sub_conn->connection.c_index, sctp_conn->state, sctp_state_to_string (sctp_conn->state), sctp_chunk_to_string (INIT), init_chunk->sctp_hdr.src_port, init_chunk->sctp_hdr.dst_port); } void sctp_compute_mac (sctp_connection_t * sctp_conn, sctp_state_cookie_param_t * state_cookie) { #if OPENSSL_VERSION_NUMBER >= 0x10100000L HMAC_CTX *ctx; #else HMAC_CTX ctx; #endif unsigned int len = 0; const EVP_MD *md = EVP_sha1 (); #if OPENSSL_VERSION_NUMBER >= 0x10100000L ctx = HMAC_CTX_new (); HMAC_Init_ex (ctx, &state_cookie->creation_time, sizeof (state_cookie->creation_time), md, NULL); HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn)); HMAC_Final (ctx, state_cookie->mac, &len); #else HMAC_CTX_init (&ctx); HMAC_Init_ex (&ctx, &state_cookie->creation_time, sizeof (state_cookie->creation_time), md, NULL); HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn)); HMAC_Final (&ctx, state_cookie->mac, &len); HMAC_CTX_cleanup (&ctx); #endif ENDIANESS_SWAP (state_cookie->mac); } void sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { vlib_main_t *vm = vlib_get_main (); sctp_reuse_buffer (vm, b); u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_cookie_ack_chunk_t *cookie_ack_chunk = vlib_buffer_push_uninit (b, alloc_bytes); cookie_ack_chunk->sctp_hdr.checksum = 0; cookie_ack_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; cookie_ack_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK); vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } void sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b, u8 reuse_buffer) { vlib_main_t *vm = vlib_get_main (); if (reuse_buffer) sctp_reuse_buffer (vm, b); /* The minimum size of the message is given by the sctp_init_ack_chunk_t */ u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_cookie_echo_chunk_t *cookie_echo_chunk = vlib_buffer_push_uninit (b, alloc_bytes); cookie_echo_chunk->sctp_hdr.checksum = 0; cookie_echo_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; cookie_echo_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO); vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len); clib_memcpy_fast (&(cookie_echo_chunk->cookie), &sctp_conn->cookie_param, sizeof (sctp_state_cookie_param_t)); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /* * Send COOKIE_ECHO */ void sctp_send_cookie_echo (sctp_connection_t * sctp_conn) { vlib_buffer_t *b; u32 bi; sctp_main_t *tm = vnet_get_sctp_main (); vlib_main_t *vm = vlib_get_main (); if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS)) { clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection."); session_stream_connect_notify (&sctp_conn->sub_conn [SCTP_PRIMARY_PATH_IDX].connection, 1); sctp_connection_timers_reset (sctp_conn); sctp_connection_cleanup (sctp_conn); } if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi))) return; b = vlib_get_buffer (vm, bi); u8 idx = SCTP_PRIMARY_PATH_IDX; sctp_init_buffer (vm, b); sctp_prepare_cookie_echo_chunk (sctp_conn, idx, b, 0); sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4); /* Start the T1_INIT timer */ sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT, sctp_conn->sub_conn[idx].RTO); /* Change state to COOKIE_WAIT */ sctp_conn->state = SCTP_STATE_COOKIE_WAIT; /* Measure RTT with this */ sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now (); } /** * Convert buffer to ERROR */ void sctp_prepare_operation_error (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b, u8 err_cause) { vlib_main_t *vm = vlib_get_main (); sctp_reuse_buffer (vm, b); /* The minimum size of the message is given by the sctp_operation_error_t */ u16 alloc_bytes = sizeof (sctp_operation_error_t) + sizeof (sctp_err_cause_param_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_operation_error_t *err_chunk = vlib_buffer_push_uninit (b, alloc_bytes); /* src_port & dst_port are already in network byte-order */ err_chunk->sctp_hdr.checksum = 0; err_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; err_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */ err_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag; err_chunk->err_causes[0].param_hdr.length = clib_host_to_net_u16 (sizeof (err_chunk->err_causes[0].param_hdr.type) + sizeof (err_chunk->err_causes[0].param_hdr.length)); err_chunk->err_causes[0].param_hdr.type = clib_host_to_net_u16 (err_cause); vnet_sctp_set_chunk_type (&err_chunk->chunk_hdr, OPERATION_ERROR); vnet_sctp_set_chunk_length (&err_chunk->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /** * Convert buffer to ABORT */ void sctp_prepare_abort_for_collision (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b, ip4_address_t * ip4_addr, ip6_address_t * ip6_addr) { vlib_main_t *vm = vlib_get_main (); sctp_reuse_buffer (vm, b); /* The minimum size of the message is given by the sctp_abort_chunk_t */ u16 alloc_bytes = sizeof (sctp_abort_chunk_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_abort_chunk_t *abort_chunk = vlib_buffer_push_uninit (b, alloc_bytes); /* src_port & dst_port are already in network byte-order */ abort_chunk->sctp_hdr.checksum = 0; abort_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; abort_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */ abort_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag; vnet_sctp_set_chunk_type (&abort_chunk->chunk_hdr, ABORT); vnet_sctp_set_chunk_length (&abort_chunk->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /** * Convert buffer to INIT-ACK */ void sctp_prepare_initack_chunk_for_collision (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b, ip4_address_t * ip4_addr, ip6_address_t * ip6_addr) { vlib_main_t *vm = vlib_get_main (); sctp_ipv4_addr_param_t *ip4_param = 0; sctp_ipv6_addr_param_t *ip6_param = 0; sctp_reuse_buffer (vm, b); /* The minimum size of the message is given by the sctp_init_ack_chunk_t */ u16 alloc_bytes = sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t); if (PREDICT_TRUE (ip4_addr != NULL)) { /* Create room for variable-length fields in the INIT_ACK chunk */ alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH; } if (PREDICT_TRUE (ip6_addr != NULL)) { /* Create room for variable-length fields in the INIT_ACK chunk */ alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH; } if (sctp_conn->sub_conn[idx].connection.is_ip4) alloc_bytes += sizeof (sctp_ipv4_addr_param_t); else alloc_bytes += sizeof (sctp_ipv6_addr_param_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_init_ack_chunk_t *init_ack_chunk = vlib_buffer_push_uninit (b, alloc_bytes); u16 pointer_offset = sizeof (sctp_init_ack_chunk_t); /* Create State Cookie parameter */ sctp_state_cookie_param_t *state_cookie_param = (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset); state_cookie_param->param_hdr.type = clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE); state_cookie_param->param_hdr.length = clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t)); state_cookie_param->creation_time = clib_host_to_net_u64 (sctp_time_now ()); state_cookie_param->cookie_lifespan = clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE); sctp_compute_mac (sctp_conn, state_cookie_param); pointer_offset += sizeof (sctp_state_cookie_param_t); if (PREDICT_TRUE (ip4_addr != NULL)) { sctp_ipv4_addr_param_t *ipv4_addr = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset; ipv4_addr->param_hdr.type = clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE); ipv4_addr->param_hdr.length = clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH); ipv4_addr->address.as_u32 = ip4_addr->as_u32; pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH; } if (PREDICT_TRUE (ip6_addr != NULL)) { sctp_ipv6_addr_param_t *ipv6_addr = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset; ipv6_addr->param_hdr.type = clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE); ipv6_addr->param_hdr.length = clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH); ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0]; ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1]; pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH; } if (sctp_conn->sub_conn[idx].connection.is_ip4) { ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset; ip4_param->address.as_u32 = sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32; pointer_offset += sizeof (sctp_ipv4_addr_param_t); } else { ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset; ip6_param->address.as_u64[0] = sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0]; ip6_param->address.as_u64[1] = sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1]; pointer_offset += sizeof (sctp_ipv6_addr_param_t); } /* src_port & dst_port are already in network byte-order */ init_ack_chunk->sctp_hdr.checksum = 0; init_ack_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; init_ack_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */ init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; init_ack_chunk->initial_tsn = clib_host_to_net_u32 (sctp_conn->local_initial_tsn); SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u", init_ack_chunk->initial_tsn); vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK); vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len); init_ack_chunk->initiate_tag = sctp_conn->local_tag; init_ack_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd); init_ack_chunk->inboud_streams_count = clib_host_to_net_u16 (INBOUND_STREAMS_COUNT); init_ack_chunk->outbound_streams_count = clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /** * Convert buffer to INIT-ACK */ void sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b, ip4_address_t * ip4_addr, u8 add_ip4, ip6_address_t * ip6_addr, u8 add_ip6) { vlib_main_t *vm = vlib_get_main (); sctp_ipv4_addr_param_t *ip4_param = 0; sctp_ipv6_addr_param_t *ip6_param = 0; u32 random_seed = random_default_seed (); sctp_reuse_buffer (vm, b); /* The minimum size of the message is given by the sctp_init_ack_chunk_t */ u16 alloc_bytes = sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t); if (PREDICT_FALSE (add_ip4 == 1)) { /* Create room for variable-length fields in the INIT_ACK chunk */ alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH; } if (PREDICT_FALSE (add_ip6 == 1)) { /* Create room for variable-length fields in the INIT_ACK chunk */ alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH; } if (sctp_conn->sub_conn[idx].connection.is_ip4) alloc_bytes += sizeof (sctp_ipv4_addr_param_t); else alloc_bytes += sizeof (sctp_ipv6_addr_param_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_init_ack_chunk_t *init_ack_chunk = vlib_buffer_push_uninit (b, alloc_bytes); u16 pointer_offset = sizeof (sctp_init_ack_chunk_t); /* Create State Cookie parameter */ sctp_state_cookie_param_t *state_cookie_param = (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset); state_cookie_param->param_hdr.type = clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE); state_cookie_param->param_hdr.length = clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t)); state_cookie_param->creation_time = clib_host_to_net_u64 (sctp_time_now ()); state_cookie_param->cookie_lifespan = clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE); sctp_compute_mac (sctp_conn, state_cookie_param); pointer_offset += sizeof (sctp_state_cookie_param_t); if (PREDICT_TRUE (ip4_addr != NULL)) { sctp_ipv4_addr_param_t *ipv4_addr = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset; ipv4_addr->param_hdr.type = clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE); ipv4_addr->param_hdr.length = clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH); ipv4_addr->address.as_u32 = ip4_addr->as_u32; pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH; } if (PREDICT_TRUE (ip6_addr != NULL)) { sctp_ipv6_addr_param_t *ipv6_addr = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset; ipv6_addr->param_hdr.type = clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE); ipv6_addr->param_hdr.length = clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH); ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0]; ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1]; pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH; } if (sctp_conn->sub_conn[idx].connection.is_ip4) { ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset; ip4_param->address.as_u32 = sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32; pointer_offset += sizeof (sctp_ipv4_addr_param_t); } else { ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset; ip6_param->address.as_u64[0] = sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0]; ip6_param->address.as_u64[1] = sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1]; pointer_offset += sizeof (sctp_ipv6_addr_param_t); } /* src_port & dst_port are already in network byte-order */ init_ack_chunk->sctp_hdr.checksum = 0; init_ack_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; init_ack_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */ init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; init_ack_chunk->initial_tsn = clib_host_to_net_u32 (sctp_conn->local_initial_tsn); SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u", init_ack_chunk->initial_tsn); vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK); vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len); init_ack_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed)); init_ack_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd); init_ack_chunk->inboud_streams_count = clib_host_to_net_u16 (INBOUND_STREAMS_COUNT); init_ack_chunk->outbound_streams_count = clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT); sctp_conn->local_tag = init_ack_chunk->initiate_tag; vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /** * Convert buffer to SHUTDOWN */ void sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_shutdown_association_chunk_t *shutdown_chunk = vlib_buffer_push_uninit (b, alloc_bytes); shutdown_chunk->sctp_hdr.checksum = 0; /* No need of host_to_net conversion, already in net-byte order */ shutdown_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; shutdown_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN); vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len); shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn; vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /* * Send SHUTDOWN */ void sctp_send_shutdown (sctp_connection_t * sctp_conn) { vlib_buffer_t *b; u32 bi; sctp_main_t *tm = vnet_get_sctp_main (); vlib_main_t *vm = vlib_get_main (); if (sctp_check_outstanding_data_chunks (sctp_conn) > 0) return; if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi))) return; u8 idx = SCTP_PRIMARY_PATH_IDX; b = vlib_get_buffer (vm, bi); sctp_init_buffer (vm, b); sctp_prepare_shutdown_chunk (sctp_conn, idx, b); sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[idx].connection.is_ip4); } /** * Convert buffer to SHUTDOWN_ACK */ void sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); sctp_shutdown_ack_chunk_t *shutdown_ack_chunk = vlib_buffer_push_uninit (b, alloc_bytes); shutdown_ack_chunk->sctp_hdr.checksum = 0; /* No need of host_to_net conversion, already in net-byte order */ shutdown_ack_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; shutdown_ack_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK); vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /* * Send SHUTDOWN_ACK */ void sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { vlib_main_t *vm = vlib_get_main (); if (sctp_check_outstanding_data_chunks (sctp_conn) > 0) return; sctp_reuse_buffer (vm, b); sctp_prepare_shutdown_ack_chunk (sctp_conn, idx, b); } /** * Convert buffer to SACK */ void sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { vlib_main_t *vm = vlib_get_main (); sctp_reuse_buffer (vm, b); u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes); sack->sctp_hdr.checksum = 0; sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; sack->sctp_hdr.verification_tag = sctp_conn->remote_tag; vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK); vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len); sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected; sctp_conn->ack_state = 0; vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /** * Convert buffer to HEARTBEAT_ACK */ void sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { vlib_main_t *vm = vlib_get_main (); u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t); sctp_reuse_buffer (vm, b); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes); hb_ack->sctp_hdr.checksum = 0; /* No need of host_to_net conversion, already in net-byte order */ hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag; hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1); hb_ack->hb_info.param_hdr.length = clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info)); vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK); vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } /** * Convert buffer to HEARTBEAT */ void sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t); /* As per RFC 4960 the chunk_length value does NOT contemplate * the size of the first header (see sctp_header_t) and any padding */ u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes); hb_req->sctp_hdr.checksum = 0; /* No need of host_to_net conversion, already in net-byte order */ hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag; hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1); hb_req->hb_info.param_hdr.length = clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info)); vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT); vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } void sctp_send_heartbeat (sctp_connection_t * sctp_conn) { vlib_buffer_t *b; u32 bi; sctp_main_t *tm = vnet_get_sctp_main (); vlib_main_t *vm = vlib_get_main (); u8 i; u64 now = sctp_time_now (); for (i = 0; i < MAX_SCTP_CONNECTIONS; i++) { if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN) continue; if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL)) { if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi))) return; b = vlib_get_buffer (vm, bi); sctp_init_buffer (vm, b); sctp_prepare_heartbeat_chunk (sctp_conn, i, b); sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[i]. connection.is_ip4); sctp_conn->sub_conn[i].unacknowledged_hb += 1; } } } /** * Convert buffer to SHUTDOWN_COMPLETE */ void sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b) { u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t); alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes); u16 chunk_len = alloc_bytes - sizeof (sctp_header_t); sctp_shutdown_complete_chunk_t *shutdown_complete = vlib_buffer_push_uninit (b, alloc_bytes); shutdown_complete->sctp_hdr.checksum = 0; /* No need of host_to_net conversion, already in net-byte order */ shutdown_complete->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; shutdown_complete->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag; vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE); vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } void sctp_send_shutdown_complete (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b0) { vlib_main_t *vm = vlib_get_main (); if (sctp_check_outstanding_data_chunks (sctp_conn) > 0) return; sctp_reuse_buffer (vm, b0); sctp_prepare_shutdown_complete_chunk (sctp_conn, idx, b0); } /* * Send INIT */ void sctp_send_init (sctp_connection_t * sctp_conn) { vlib_buffer_t *b; u32 bi; sctp_main_t *tm = vnet_get_sctp_main (); vlib_main_t *vm = vlib_get_main (); if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS)) { clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection."); session_stream_connect_notify (&sctp_conn->sub_conn [SCTP_PRIMARY_PATH_IDX].connection, 1); sctp_connection_timers_reset (sctp_conn); sctp_connection_cleanup (sctp_conn); return; } if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi))) return; b = vlib_get_buffer (vm, bi); u8 idx = SCTP_PRIMARY_PATH_IDX; sctp_init_buffer (vm, b); sctp_prepare_init_chunk (sctp_conn, idx, b); sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b); sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4, sctp_conn->sub_conn[idx].c_fib_index); /* Start the T1_INIT timer */ sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT, sctp_conn->sub_conn[idx].RTO); /* Change state to COOKIE_WAIT */ sctp_conn->state = SCTP_STATE_COOKIE_WAIT; /* Measure RTT with this */ sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now (); } /** * Push SCTP header and update connection variables */ static void sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b, sctp_state_t next_state) { u16 data_len = b->current_length; if (b->flags & VLIB_BUFFER_TOTAL_LENGTH_VALID) data_len += b->total_length_not_including_first_buffer; ASSERT (!b->total_length_not_including_first_buffer || (b->flags & VLIB_BUFFER_NEXT_PRESENT)); SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, " "b->current_data = %p " "data_len = %u", b->current_length, b->current_data, data_len); u16 data_padding = vnet_sctp_calculate_padding (b->current_length); if (data_padding > 0) { u8 *p_tail = vlib_buffer_put_uninit (b, data_padding); clib_memset_u8 (p_tail, 0, data_padding); } u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t); u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t); sctp_payload_data_chunk_t *data_chunk = vlib_buffer_push_uninit (b, bytes_to_add); u8 idx = sctp_data_subconn_select (sctp_conn); SCTP_DBG_OUTPUT ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u", sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index, sctp_conn->sub_conn[idx].connection.c_index, sctp_conn->sub_conn[idx].connection.lcl_port, sctp_conn->sub_conn[idx].connection.rmt_port); data_chunk->sctp_hdr.checksum = 0; data_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port; data_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port; data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag; data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn); data_chunk->stream_id = clib_host_to_net_u16 (0); data_chunk->stream_seq = clib_host_to_net_u16 (0); vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA); vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length); vnet_sctp_set_bbit (&data_chunk->chunk_hdr); vnet_sctp_set_ebit (&data_chunk->chunk_hdr); SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u", b->data, b->current_data); if (sctp_conn->sub_conn[idx].state != SCTP_SUBCONN_AWAITING_SACK) { sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_AWAITING_SACK; sctp_conn->last_unacked_tsn = sctp_conn->next_tsn; } sctp_conn->next_tsn += data_len; u32 inflight = sctp_conn->next_tsn - sctp_conn->last_unacked_tsn; /* Section 7.2.2; point (3) */ if (sctp_conn->sub_conn[idx].partially_acked_bytes >= sctp_conn->sub_conn[idx].cwnd && inflight >= sctp_conn->sub_conn[idx].cwnd) { sctp_conn->sub_conn[idx].cwnd += sctp_conn->sub_conn[idx].PMTU; sctp_conn->sub_conn[idx].partially_acked_bytes -= sctp_conn->sub_conn[idx].cwnd; } sctp_conn->sub_conn[idx].last_data_ts = sctp_time_now (); vnet_buffer (b)->sctp.connection_index = sctp_conn->sub_conn[idx].connection.c_index; vnet_buffer (b)->sctp.subconn_idx = idx; } u32 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b) { sctp_connection_t *sctp_conn = sctp_get_connection_from_transport (trans_conn); SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, " "S_INDEX = %u, C_INDEX = %u," "trans_conn->LCL_PORT = %u, trans_conn->RMT_PORT = %u", trans_conn, sctp_conn, trans_conn->s_index, trans_conn->c_index, trans_conn->lcl_port, trans_conn->rmt_port); sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED); sctp_trajectory_add_start (b, 3); return 0; } u32 sctp_prepare_data_retransmit (sctp_connection_t * sctp_conn, u8 idx, u32 offset, u32 max_deq_bytes, vlib_buffer_t ** b) { sctp_main_t *tm = vnet_get_sctp_main (); vlib_main_t *vm = vlib_get_main (); int n_bytes = 0; u32 bi, available_bytes, seg_size; u8 *data; ASSERT (sctp_conn->state >= SCTP_STATE_ESTABLISHED); ASSERT (max_deq_bytes != 0); /* * Make sure we can retransmit something */ available_bytes = transport_max_tx_dequeue (&sctp_conn->sub_conn[idx].connection); ASSERT (available_bytes >= offset); available_bytes -= offset; if (!available_bytes) return 0; max_deq_bytes = clib_min (sctp_conn->sub_conn[idx].cwnd, max_deq_bytes); max_deq_bytes = clib_min (available_bytes, max_deq_bytes); seg_size = max_deq_bytes; /* * Allocate and fill in buffer(s) */ if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi))) return 0; *b = vlib_get_buffer (vm, bi); data = sctp_init_buffer (vm, *b); /* Easy case, buffer size greater than mss */ if (PREDICT_TRUE (seg_size <= tm->bytes_per_buffer)) { n_bytes = session_tx_fifo_peek_bytes (&sctp_conn->sub_conn[idx].connection, data, offset, max_deq_bytes); ASSERT (n_bytes == max_deq_bytes); b[0]->current_length = n_bytes; sctp_push_hdr_i (sctp_conn, *b, sctp_conn->state); } return n_bytes; } void sctp_data_retransmit (sctp_connection_t * sctp_conn) { vlib_main_t *vm = vlib_get_main (); vlib_buffer_t *b = 0; u32 bi, n_bytes = 0; u8 idx = sctp_data_subconn_select (sctp_conn); SCTP_DBG_OUTPUT ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u", sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index, sctp_conn->sub_conn[idx].connection.c_index, sctp_conn->sub_conn[idx].connection.lcl_port, sctp_conn->sub_conn[idx].connection.rmt_port); if (sctp_conn->state >= SCTP_STATE_ESTABLISHED) { return; } n_bytes = sctp_prepare_data_retransmit (sctp_conn, idx, 0, sctp_conn->sub_conn[idx].cwnd, &b); if (n_bytes > 0) SCTP_DBG_OUTPUT ("We have data (%u bytes) to retransmit", n_bytes); bi = vlib_get_buffer_index (vm, b); sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[idx].connection.is_ip4); return; } #if SCTP_DEBUG_STATE_MACHINE always_inline u8 sctp_validate_output_state_machine (sctp_connection_t * sctp_conn, u8 chunk_type) { u8 result = 0; switch (sctp_conn->state) { case SCTP_STATE_CLOSED: if (chunk_type != INIT && chunk_type != INIT_ACK) result = 1; break; case SCTP_STATE_ESTABLISHED: if (chunk_type != DATA && chunk_type != HEARTBEAT && chunk_type != HEARTBEAT_ACK && chunk_type != SACK && chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN) result = 1; break; case SCTP_STATE_COOKIE_WAIT: if (chunk_type != COOKIE_ECHO) result = 1; break; case SCTP_STATE_SHUTDOWN_SENT: if (chunk_type != SHUTDOWN_COMPLETE) result = 1; break; case SCTP_STATE_SHUTDOWN_RECEIVED: if (chunk_type != SHUTDOWN_ACK) result = 1; break; } return result; } #endif /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */