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
|
/*
* node.c - skeleton vpp engine plug-in dual-loop node skeleton
*
* Copyright (c) <current-year> <your-organization>
* 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 <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/pg/pg.h>
#include <vppinfra/error.h>
#include <nsim/nsim.h>
typedef struct
{
f64 expires;
u32 tx_sw_if_index;
int is_drop;
int is_lost;
} nsim_trace_t;
#ifndef CLIB_MARCH_VARIANT
/* packet trace format function */
static u8 *
format_nsim_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 *);
nsim_trace_t *t = va_arg (*args, nsim_trace_t *);
if (t->is_drop)
s = format (s, "NSIM: dropped, %s", t->is_lost ?
"simulated network loss" : "no space in ring");
else
s = format (s, "NSIM: tx time %.6f sw_if_index %d",
t->expires, t->tx_sw_if_index);
return s;
}
vlib_node_registration_t nsim_node;
#endif /* CLIB_MARCH_VARIANT */
#define foreach_nsim_error \
_(BUFFERED, "Packets buffered") \
_(DROPPED, "Packets dropped due to lack of space") \
_(LOSS, "Network loss simulation drop packets")
typedef enum
{
#define _(sym,str) NSIM_ERROR_##sym,
foreach_nsim_error
#undef _
NSIM_N_ERROR,
} nsim_error_t;
#ifndef CLIB_MARCH_VARIANT
static char *nsim_error_strings[] = {
#define _(sym,string) string,
foreach_nsim_error
#undef _
};
#endif /* CLIB_MARCH_VARIANT */
typedef enum
{
NSIM_NEXT_DROP,
NSIM_N_NEXT,
} nsim_next_t;
always_inline uword
nsim_inline (vlib_main_t * vm,
vlib_node_runtime_t * node, vlib_frame_t * frame, int is_trace,
int is_cross_connect)
{
nsim_main_t *nsm = &nsim_main;
u32 n_left_from, *from;
u32 *to_next, n_left_to_next;
u32 drops[VLIB_FRAME_SIZE], *drop;
vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
u8 is_drop[4];
u16 nexts[VLIB_FRAME_SIZE], *next;
u32 my_thread_index = vm->thread_index;
nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
f64 now = vlib_time_now (vm);
f64 expires = now + nsm->delay;
f64 rnd[4];
u32 no_buffer_error = node->errors[NSIM_ERROR_DROPPED];
u32 loss_error = node->errors[NSIM_ERROR_LOSS];
u32 buffered = 0;
nsim_wheel_entry_t *ep = 0;
ASSERT (wp);
from = vlib_frame_vector_args (frame);
n_left_from = frame->n_vectors;
vlib_get_buffers (vm, from, bufs, n_left_from);
b = bufs;
next = nexts;
drop = drops;
while (n_left_from >= 8)
{
vlib_prefetch_buffer_header (b[4], STORE);
vlib_prefetch_buffer_header (b[5], STORE);
vlib_prefetch_buffer_header (b[6], STORE);
vlib_prefetch_buffer_header (b[7], STORE);
memset (&is_drop, 0, sizeof (is_drop));
next[0] = next[1] = next[2] = next[3] = NSIM_NEXT_DROP;
if (PREDICT_FALSE (wp->cursize + 4 >= wp->wheel_size))
goto slow_path;
if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
{
rnd[0] = random_f64 (&nsm->seed);
rnd[1] = random_f64 (&nsm->seed);
rnd[2] = random_f64 (&nsm->seed);
rnd[3] = random_f64 (&nsm->seed);
if (rnd[0] <= nsm->drop_fraction)
{
b[0]->error = loss_error;
is_drop[0] = 1;
}
if (rnd[1] <= nsm->drop_fraction)
{
b[1]->error = loss_error;
is_drop[1] = 1;
}
if (rnd[2] <= nsm->drop_fraction)
{
b[2]->error = loss_error;
is_drop[2] = 1;
}
if (rnd[3] <= nsm->drop_fraction)
{
b[3]->error = loss_error;
is_drop[3] = 1;
}
}
if (PREDICT_TRUE (is_drop[0] == 0))
{
ep = wp->entries + wp->tail;
wp->tail++;
if (wp->tail == wp->wheel_size)
wp->tail = 0;
wp->cursize++;
ep->tx_time = expires;
ep->rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
if (is_cross_connect)
{
ep->tx_sw_if_index =
(vnet_buffer (b[0])->sw_if_index[VLIB_RX] ==
nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
ep->output_next_index =
(ep->tx_sw_if_index ==
nsm->sw_if_index0) ? nsm->
output_next_index0 : nsm->output_next_index1;
}
else /* output feature, even easier... */
{
ep->tx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
ep->output_next_index =
nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
}
ep->buffer_index = from[0];
buffered++;
}
if (is_trace)
{
if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
{
nsim_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
t->expires = expires;
t->is_drop = is_drop[1];
t->is_lost = b[1]->error == loss_error;
t->tx_sw_if_index = (is_drop[1] == 0) ? ep->tx_sw_if_index : 0;
}
}
if (PREDICT_TRUE (is_drop[1] == 0))
{
ep = wp->entries + wp->tail;
wp->tail++;
if (wp->tail == wp->wheel_size)
wp->tail = 0;
wp->cursize++;
ep->tx_time = expires;
ep->rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
if (is_cross_connect)
{
ep->tx_sw_if_index =
(vnet_buffer (b[1])->sw_if_index[VLIB_RX] ==
nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
ep->output_next_index =
(ep->tx_sw_if_index ==
nsm->sw_if_index0) ? nsm->
output_next_index0 : nsm->output_next_index1;
}
else /* output feature, even easier... */
{
ep->tx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
ep->output_next_index =
nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
}
ep->buffer_index = from[1];
buffered++;
}
if (is_trace)
{
if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
{
nsim_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
t->expires = expires;
t->is_drop = is_drop[2];
t->is_lost = b[2]->error == loss_error;
t->tx_sw_if_index = (is_drop[2] == 0) ? ep->tx_sw_if_index : 0;
}
}
if (PREDICT_TRUE (is_drop[2] == 0))
{
ep = wp->entries + wp->tail;
wp->tail++;
if (wp->tail == wp->wheel_size)
wp->tail = 0;
wp->cursize++;
ep->tx_time = expires;
ep->rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
if (is_cross_connect)
{
ep->tx_sw_if_index =
(vnet_buffer (b[2])->sw_if_index[VLIB_RX] ==
nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
ep->output_next_index =
(ep->tx_sw_if_index ==
nsm->sw_if_index0) ? nsm->
output_next_index0 : nsm->output_next_index1;
}
else /* output feature, even easier... */
{
ep->tx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
ep->output_next_index =
nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
}
ep->buffer_index = from[2];
buffered++;
}
if (is_trace)
{
if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
{
nsim_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
t->expires = expires;
t->is_drop = is_drop[2];
t->is_lost = b[2]->error == loss_error;
t->tx_sw_if_index = (is_drop[2] == 0) ? ep->tx_sw_if_index : 0;
}
}
if (PREDICT_TRUE (is_drop[3] == 0))
{
ep = wp->entries + wp->tail;
wp->tail++;
if (wp->tail == wp->wheel_size)
wp->tail = 0;
wp->cursize++;
ep->tx_time = expires;
ep->rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
if (is_cross_connect)
{
ep->tx_sw_if_index =
(vnet_buffer (b[3])->sw_if_index[VLIB_RX] ==
nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
ep->output_next_index =
(ep->tx_sw_if_index ==
nsm->sw_if_index0) ? nsm->
output_next_index0 : nsm->output_next_index1;
}
else /* output feature, even easier... */
{
ep->tx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
ep->output_next_index =
nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
}
ep->buffer_index = from[3];
buffered++;
}
if (is_trace)
{
if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
{
nsim_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
t->expires = expires;
t->is_drop = is_drop[3];
t->is_lost = b[3]->error == loss_error;
t->tx_sw_if_index = (is_drop[3] == 0) ? ep->tx_sw_if_index : 0;
}
}
if (PREDICT_FALSE (is_drop[0]))
*drop++ = from[0];
if (PREDICT_FALSE (is_drop[1]))
*drop++ = from[1];
if (PREDICT_FALSE (is_drop[2]))
*drop++ = from[2];
if (PREDICT_FALSE (is_drop[3]))
*drop++ = from[3];
b += 4;
next += 4;
from += 4;
n_left_from -= 4;
}
slow_path:
while (n_left_from > 0)
{
next[0] = NSIM_NEXT_DROP;
is_drop[0] = 0;
if (PREDICT_TRUE (wp->cursize < wp->wheel_size))
{
if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
{
/* Get a random number on the closed interval [0,1] */
rnd[0] = random_f64 (&nsm->seed);
/* Drop the pkt? */
if (rnd[0] <= nsm->drop_fraction)
{
b[0]->error = loss_error;
is_drop[0] = 1;
goto do_trace;
}
}
ep = wp->entries + wp->tail;
wp->tail++;
if (wp->tail == wp->wheel_size)
wp->tail = 0;
wp->cursize++;
ep->tx_time = expires;
ep->rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
if (is_cross_connect)
{
ep->tx_sw_if_index =
(vnet_buffer (b[0])->sw_if_index[VLIB_RX] ==
nsm->sw_if_index0) ? nsm->sw_if_index1 : nsm->sw_if_index0;
ep->output_next_index =
(ep->tx_sw_if_index ==
nsm->sw_if_index0) ? nsm->
output_next_index0 : nsm->output_next_index1;
}
else /* output feature, even easier... */
{
ep->tx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
ep->output_next_index =
nsm->output_next_index_by_sw_if_index[ep->tx_sw_if_index];
}
ep->buffer_index = from[0];
buffered++;
}
else /* out of wheel space, drop pkt */
{
b[0]->error = no_buffer_error;
is_drop[0] = 1;
}
do_trace:
if (is_trace)
{
if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
{
nsim_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
t->expires = expires;
t->is_drop = is_drop[0];
t->is_lost = b[0]->error == loss_error;
t->tx_sw_if_index = (is_drop[0] == 0) ? ep->tx_sw_if_index : 0;
}
}
b += 1;
next += 1;
if (PREDICT_FALSE (is_drop[0]))
{
drop[0] = from[0];
drop++;
}
from++;
n_left_from -= 1;
}
if (PREDICT_FALSE (drop > drops))
{
u32 n_left_to_drop = drop - drops;
drop = drops;
while (n_left_to_drop > 0)
{
u32 this_copy_size;
vlib_get_next_frame (vm, node, NSIM_NEXT_DROP, to_next,
n_left_to_next);
this_copy_size = clib_min (n_left_to_drop, n_left_to_next);
clib_memcpy_fast (to_next, drop, this_copy_size * sizeof (u32));
n_left_to_next -= this_copy_size;
vlib_put_next_frame (vm, node, NSIM_NEXT_DROP, n_left_to_next);
drop += this_copy_size;
n_left_to_drop -= this_copy_size;
}
}
vlib_node_increment_counter (vm, node->node_index,
NSIM_ERROR_BUFFERED, buffered);
return frame->n_vectors;
}
VLIB_NODE_FN (nsim_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
vlib_frame_t * frame)
{
if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
return nsim_inline (vm, node, frame,
1 /* is_trace */ , 1 /* is_cross_connect */ );
else
return nsim_inline (vm, node, frame,
0 /* is_trace */ , 1 /* is_cross_connect */ );
}
/* *INDENT-OFF* */
#ifndef CLIB_MARCH_VARIANT
VLIB_REGISTER_NODE (nsim_node) =
{
.name = "nsim",
.vector_size = sizeof (u32),
.format_trace = format_nsim_trace,
.type = VLIB_NODE_TYPE_INTERNAL,
.n_errors = ARRAY_LEN(nsim_error_strings),
.error_strings = nsim_error_strings,
.n_next_nodes = NSIM_N_NEXT,
/* edit / add dispositions here */
.next_nodes = {
[NSIM_NEXT_DROP] = "error-drop",
},
};
#endif /* CLIB_MARCH_VARIANT */
/* *INDENT-ON* */
VLIB_NODE_FN (nsim_feature_node) (vlib_main_t * vm,
vlib_node_runtime_t * node,
vlib_frame_t * frame)
{
if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
return nsim_inline (vm, node, frame,
1 /* is_trace */ , 0 /* is_cross_connect */ );
else
return nsim_inline (vm, node, frame,
0 /* is_trace */ , 0 /* is_cross_connect */ );
}
/* *INDENT-OFF* */
#ifndef CLIB_MARCH_VARIANT
VLIB_REGISTER_NODE (nsim_feature_node) =
{
.name = "nsim-output-feature",
.vector_size = sizeof (u32),
.format_trace = format_nsim_trace,
.type = VLIB_NODE_TYPE_INTERNAL,
.n_errors = ARRAY_LEN(nsim_error_strings),
.error_strings = nsim_error_strings,
.n_next_nodes = NSIM_N_NEXT,
/* edit / add dispositions here */
.next_nodes = {
[NSIM_NEXT_DROP] = "error-drop",
},
};
#endif /* CLIB_MARCH_VARIANT */
/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|