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

#include <vnet/fib/fib_entry.h>
#include <vnet/fib/fib_table.h>
#include <vnet/fib/fib_walk.h>
#include <vnet/fib/fib_path_list.h>

#include <vnet/bier/bier_table.h>
#include <vnet/bier/bier_fmask.h>
#include <vnet/bier/bier_bit_string.h>
#include <vnet/bier/bier_disp_table.h>

#include <vnet/mpls/mpls.h>
#include <vnet/dpo/drop_dpo.h>
#include <vnet/dpo/load_balance.h>

/*
 * attributes names for formatting
 */
static const char *const bier_fmask_attr_names[] = BIER_FMASK_ATTR_NAMES;

/*
 * pool of BIER fmask objects
 */
bier_fmask_t *bier_fmask_pool;

/**
 * Stats for each BIER fmask object
 */
vlib_combined_counter_main_t bier_fmask_counters;

static inline index_t
bier_fmask_get_index (const bier_fmask_t *bfm)
{
    return (bfm - bier_fmask_pool);
}

static void
bier_fmask_bits_init (bier_fmask_bits_t *bits,
                      bier_hdr_len_id_t hlid)
{
    bits->bfmb_refs = clib_mem_alloc(sizeof(bits->bfmb_refs[0]) *
                                     bier_hdr_len_id_to_num_bits(hlid));
    memset(bits->bfmb_refs,
           0,
           (sizeof(bits->bfmb_refs[0]) *
            bier_hdr_len_id_to_num_bits(hlid)));

    bits->bfmb_input_reset_string.bbs_len =
        bier_hdr_len_id_to_num_buckets(hlid);

    /*
     * The buckets are accessed in the switch path
     */
    bits->bfmb_input_reset_string.bbs_buckets =
        clib_mem_alloc_aligned(
            sizeof(bits->bfmb_input_reset_string.bbs_buckets[0]) *
            bier_hdr_len_id_to_num_buckets(hlid),
            CLIB_CACHE_LINE_BYTES);
    memset(bits->bfmb_input_reset_string.bbs_buckets,
           0,
           sizeof(bits->bfmb_input_reset_string.bbs_buckets[0]) *
           bier_hdr_len_id_to_num_buckets(hlid));
}

static void
bier_fmask_stack (bier_fmask_t *bfm)
{
    dpo_id_t via_dpo = DPO_INVALID;
    fib_forward_chain_type_t fct;

    if (bfm->bfm_flags & BIER_FMASK_FLAG_MPLS)
    {
        fct = FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS;
    }
    else
    {
        fct = FIB_FORW_CHAIN_TYPE_BIER;
    }

    fib_path_list_contribute_forwarding(bfm->bfm_pl, fct,
                                        FIB_PATH_LIST_FWD_FLAG_COLLAPSE,
                                        &via_dpo);

    /*
     * If the via PL entry provides no forwarding (i.e. a drop)
     * then neither does this fmask. That way children consider this fmask
     * unresolved and other ECMP options are used instead.
     */
    if (dpo_is_drop(&via_dpo))
    {
        bfm->bfm_flags &= ~BIER_FMASK_FLAG_FORWARDING;
    }
    else
    {
        bfm->bfm_flags |= BIER_FMASK_FLAG_FORWARDING;
    }

    dpo_stack(DPO_BIER_FMASK,
              DPO_PROTO_BIER,
              &bfm->bfm_dpo,
              &via_dpo);
    dpo_reset(&via_dpo);
}

void
bier_fmask_contribute_forwarding (index_t bfmi,
                                  dpo_id_t *dpo)
{
    bier_fmask_t *bfm;

    bfm = bier_fmask_get(bfmi);

    if (bfm->bfm_flags & BIER_FMASK_FLAG_FORWARDING)
    {
        dpo_set(dpo,
                DPO_BIER_FMASK,
                DPO_PROTO_BIER,
                bfmi);
    }
    else
    {
        dpo_copy(dpo, drop_dpo_get(DPO_PROTO_BIER));
    }
}

u32
bier_fmask_child_add (fib_node_index_t bfmi,
                     fib_node_type_t child_type,
                     fib_node_index_t child_index)
{
    return (fib_node_child_add(FIB_NODE_TYPE_BIER_FMASK,
                               bfmi,
                               child_type,
                               child_index));
};

void
bier_fmask_child_remove (fib_node_index_t bfmi,
                         u32 sibling_index)
{
    if (INDEX_INVALID == bfmi)
    {
        return;
    }

    fib_node_child_remove(FIB_NODE_TYPE_BIER_FMASK,
                          bfmi,
                          sibling_index);
}

static void
bier_fmask_init (bier_fmask_t *bfm,
                 const bier_fmask_id_t *fmid,
                 const fib_route_path_t *rpath)
{
    const bier_table_id_t *btid;
    fib_route_path_t *rpaths;
    mpls_label_t olabel;

    memset(bfm, 0, sizeof(*bfm));
    
    bfm->bfm_id = clib_mem_alloc(sizeof(*bfm->bfm_id));

    fib_node_init(&bfm->bfm_node, FIB_NODE_TYPE_BIER_FMASK);
    *bfm->bfm_id = *fmid;
    dpo_reset(&bfm->bfm_dpo);
    btid = bier_table_get_id(bfm->bfm_id->bfmi_bti);
    bier_fmask_bits_init(&bfm->bfm_bits, btid->bti_hdr_len);

    if (rpath->frp_flags & FIB_ROUTE_PATH_UDP_ENCAP)
    {
        bfm->bfm_id->bfmi_nh_type = BIER_NH_UDP;
    }
    else if (ip46_address_is_zero(&(bfm->bfm_id->bfmi_nh)))
    {
        bfm->bfm_flags |= BIER_FMASK_FLAG_DISP;
    }

    if (!(bfm->bfm_flags & BIER_FMASK_FLAG_DISP))
    {
        if (NULL != rpath->frp_label_stack)
        {
            olabel = rpath->frp_label_stack[0].fml_value;
            vnet_mpls_uc_set_label(&bfm->bfm_label, olabel);
            vnet_mpls_uc_set_exp(&bfm->bfm_label, 0);
            vnet_mpls_uc_set_s(&bfm->bfm_label, 1);
            vnet_mpls_uc_set_ttl(&bfm->bfm_label, 64);
            bfm->bfm_flags |= BIER_FMASK_FLAG_MPLS;
        }
        else
        {
            bier_bift_id_t id;

            /*
             * not an MPLS label
             */
            bfm->bfm_flags &= ~BIER_FMASK_FLAG_MPLS;

            /*
             * use a label as encoded for BIFT value
             */
            id = bier_bift_id_encode(btid->bti_set,
                                     btid->bti_sub_domain,
                                     btid->bti_hdr_len);
            vnet_mpls_uc_set_label(&bfm->bfm_label, id);
            vnet_mpls_uc_set_s(&bfm->bfm_label, 1);
            vnet_mpls_uc_set_exp(&bfm->bfm_label, 0);
            vnet_mpls_uc_set_ttl(&bfm->bfm_label, 64);
        }
        bfm->bfm_label = clib_host_to_net_u32(bfm->bfm_label);
    }

    rpaths = NULL;
    vec_add1(rpaths, *rpath);
    bfm->bfm_pl = fib_path_list_create((FIB_PATH_LIST_FLAG_SHARED |
                                        FIB_PATH_LIST_FLAG_NO_URPF),
                                       rpaths);
    bfm->bfm_sibling = fib_path_list_child_add(bfm->bfm_pl,
                                               FIB_NODE_TYPE_BIER_FMASK,
                                               bier_fmask_get_index(bfm));
    vec_free(rpaths);
    bier_fmask_stack(bfm);
}

static void
bier_fmask_destroy (bier_fmask_t *bfm)
{
    clib_mem_free(bfm->bfm_bits.bfmb_refs);
    clib_mem_free(bfm->bfm_bits.bfmb_input_reset_string.bbs_buckets);

    bier_fmask_db_remove(bfm->bfm_id);
    fib_path_list_child_remove(bfm->bfm_pl,
                               bfm->bfm_sibling);
    dpo_reset(&bfm->bfm_dpo);
    clib_mem_free(bfm->bfm_id);
    pool_put(bier_fmask_pool, bfm);
}

void
bier_fmask_unlock (index_t bfmi)
{
    bier_fmask_t *bfm;

    if (INDEX_INVALID == bfmi)
    {
        return;
    }

    bfm = bier_fmask_get(bfmi);

    fib_node_unlock(&bfm->bfm_node);
}

void
bier_fmask_lock (index_t bfmi)
{
    bier_fmask_t *bfm;

    if (INDEX_INVALID == bfmi)
    {
        return;
    }

    bfm = bier_fmask_get(bfmi);

    fib_node_lock(&bfm->bfm_node);
}

index_t
bier_fmask_create_and_lock (const bier_fmask_id_t *fmid,
                            const fib_route_path_t *rpath)
{
    bier_fmask_t *bfm;
    index_t bfmi;

    pool_get_aligned(bier_fmask_pool, bfm, CLIB_CACHE_LINE_BYTES);
    bfmi = bier_fmask_get_index(bfm);

    vlib_validate_combined_counter (&(bier_fmask_counters), bfmi);
    vlib_zero_combined_counter (&(bier_fmask_counters), bfmi);

    bier_fmask_init(bfm, fmid, rpath);

    bier_fmask_lock(bfmi);

    return (bfmi);
}

void
bier_fmask_link (index_t bfmi,
                 bier_bp_t bp)
{
    bier_fmask_t *bfm;

    bfm = bier_fmask_get(bfmi);

    if (0 == bfm->bfm_bits.bfmb_refs[BIER_BP_TO_INDEX(bp)])
    {
        /*
         * 0 -> 1 transistion - set the bit in the string
         */
        bier_bit_string_set_bit(&bfm->bfm_bits.bfmb_input_reset_string, bp);
    }

    ++bfm->bfm_bits.bfmb_refs[BIER_BP_TO_INDEX(bp)];
    ++bfm->bfm_bits.bfmb_count;
}

void
bier_fmask_unlink (index_t bfmi,
                   bier_bp_t bp)
{
    bier_fmask_t *bfm;

    bfm = bier_fmask_get(bfmi);

    --bfm->bfm_bits.bfmb_refs[BIER_BP_TO_INDEX(bp)];
    --bfm->bfm_bits.bfmb_count;

    if (0 == bfm->bfm_bits.bfmb_refs[BIER_BP_TO_INDEX(bp)])
    {
        /*
         * 1 -> 0 transistion - clear the bit in the string
         */
        bier_bit_string_clear_bit(&bfm->bfm_bits.bfmb_input_reset_string, bp);
    }
}

u8*
format_bier_fmask (u8 *s, va_list *ap)
{
    index_t bfmi = va_arg(*ap, index_t);
    u32 indent = va_arg(*ap, u32);
    bier_fmask_attributes_t attr;
    bier_fmask_t *bfm;
    vlib_counter_t to;

    if (pool_is_free_index(bier_fmask_pool, bfmi))
    {
        return (format(s, "No BIER f-mask %d", bfmi));
    }

    bfm = bier_fmask_get(bfmi);

    s = format(s, "fmask: nh:%U bs:%U locks:%d ",
               format_ip46_address, &bfm->bfm_id->bfmi_nh, IP46_TYPE_ANY,
               format_bier_bit_string, &bfm->bfm_bits.bfmb_input_reset_string,
               bfm->bfm_node.fn_locks);
    s = format(s, "flags:");
    FOR_EACH_BIER_FMASK_ATTR(attr) {
        if ((1<<attr) & bfm->bfm_flags) {
            s = format (s, "%s,", bier_fmask_attr_names[attr]);
        }
    }
    vlib_get_combined_counter (&(bier_fmask_counters), bfmi, &to);
    s = format (s, " to:[%Ld:%Ld]]", to.packets, to.bytes);
    s = format(s, "\n");
    s = fib_path_list_format(bfm->bfm_pl, s);

    if (bfm->bfm_flags & BIER_FMASK_FLAG_MPLS)
    {
        s = format(s, "  output-label:%U",
                   format_mpls_unicast_label,
                   vnet_mpls_uc_get_label(clib_net_to_host_u32(bfm->bfm_label)));
    }
    else
    {
        s = format(s, "  output-bfit:[%U]",
                   format_bier_bift_id,
                   vnet_mpls_uc_get_label(clib_net_to_host_u32(bfm->bfm_label)));
    }
    s = format(s, "\n %U%U",
               format_white_space, indent,
               format_dpo_id, &bfm->bfm_dpo, indent+2);

    return (s);
}

void
bier_fmask_get_stats (index_t bfmi, u64 * packets, u64 * bytes)
{
  vlib_counter_t to;

  vlib_get_combined_counter (&(bier_fmask_counters), bfmi, &to);

  *packets = to.packets;
  *bytes = to.bytes;
}

void
bier_fmask_encode (index_t bfmi,
                   bier_table_id_t *btid,
                   fib_route_path_encode_t *rpath)
{
    bier_fmask_t *bfm;

    bfm = bier_fmask_get(bfmi);
    *btid = *bier_table_get_id(bfm->bfm_id->bfmi_bti);

    memset(rpath, 0, sizeof(*rpath));

    rpath->rpath.frp_sw_if_index = ~0;

    switch (bfm->bfm_id->bfmi_nh_type)
    {
    case BIER_NH_UDP:
        rpath->rpath.frp_flags = FIB_ROUTE_PATH_UDP_ENCAP;
        rpath->rpath.frp_udp_encap_id = bfm->bfm_id->bfmi_id;
        break;
    case BIER_NH_IP:
        memcpy(&rpath->rpath.frp_addr, &bfm->bfm_id->bfmi_nh,
               sizeof(rpath->rpath.frp_addr));
        break;
    }
}

static fib_node_t *
bier_fmask_get_node (fib_node_index_t index)
{
    bier_fmask_t *bfm = bier_fmask_get(index);
    return (&(bfm->bfm_node));
}

static bier_fmask_t*
bier_fmask_get_from_node (fib_node_t *node)
{
    return ((bier_fmask_t*)(((char*)node) -
                            STRUCT_OFFSET_OF(bier_fmask_t,
                                             bfm_node)));
}

/*
 * bier_fmask_last_lock_gone
 */
static void
bier_fmask_last_lock_gone (fib_node_t *node)
{
    bier_fmask_destroy(bier_fmask_get_from_node(node));
}

/*
 * bier_fmask_back_walk_notify
 *
 * A back walk has reached this BIER fmask
 */
static fib_node_back_walk_rc_t
bier_fmask_back_walk_notify (fib_node_t *node,
                             fib_node_back_walk_ctx_t *ctx)
{
    /*
     * re-stack the fmask on the n-eos of the via
     */
    bier_fmask_t *bfm = bier_fmask_get_from_node(node);

    bier_fmask_stack(bfm);

    /*
     * propagate further up the graph.
     * we can do this synchronously since the fan out is small.
     */
    fib_walk_sync(FIB_NODE_TYPE_BIER_FMASK, bier_fmask_get_index(bfm), ctx);

    return (FIB_NODE_BACK_WALK_CONTINUE);
}

/*
 * The BIER fmask's graph node virtual function table
 */
static const fib_node_vft_t bier_fmask_vft = {
    .fnv_get = bier_fmask_get_node,
    .fnv_last_lock = bier_fmask_last_lock_gone,
    .fnv_back_walk = bier_fmask_back_walk_notify,
};

static void
bier_fmask_dpo_lock (dpo_id_t *dpo)
{
}

static void
bier_fmask_dpo_unlock (dpo_id_t *dpo)
{
}

static void
bier_fmask_dpo_mem_show (void)
{
    fib_show_memory_usage("BIER-fmask",
                          pool_elts(bier_fmask_pool),
                          pool_len(bier_fmask_pool),
                          sizeof(bier_fmask_t));
}

const static dpo_vft_t bier_fmask_dpo_vft = {
    .dv_lock = bier_fmask_dpo_lock,
    .dv_unlock = bier_fmask_dpo_unlock,
    .dv_mem_show = bier_fmask_dpo_mem_show,
    .dv_format = format_bier_fmask,
};

const static char *const bier_fmask_mpls_nodes[] =
{
    "bier-output",
    NULL
};
const static char * const * const bier_fmask_nodes[DPO_PROTO_NUM] =
{
    [DPO_PROTO_BIER] = bier_fmask_mpls_nodes,
    [DPO_PROTO_MPLS] = bier_fmask_mpls_nodes,
};

clib_error_t *
bier_fmask_module_init (vlib_main_t * vm)
{
    fib_node_register_type (FIB_NODE_TYPE_BIER_FMASK, &bier_fmask_vft);
    dpo_register(DPO_BIER_FMASK, &bier_fmask_dpo_vft, bier_fmask_nodes);

    return (NULL);
}

VLIB_INIT_FUNCTION (bier_fmask_module_init);

static clib_error_t *
bier_fmask_show (vlib_main_t * vm,
                 unformat_input_t * input,
                 vlib_cli_command_t * cmd)
{
    bier_fmask_t *bfm;
    index_t bfmi;

    bfmi = INDEX_INVALID;

    while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
        if (unformat (input, "%d", &bfmi))
        {
            ;
        } else
        {
            break;
        }
    }

    if (INDEX_INVALID == bfmi)
    {
        pool_foreach(bfm, bier_fmask_pool,
        ({
            vlib_cli_output (vm, "[@%d] %U",
                             bier_fmask_get_index(bfm),
                             format_bier_fmask, bier_fmask_get_index(bfm), 0);
        }));
    }
    else
    {
        vlib_cli_output (vm, "%U", format_bier_fmask, bfmi, 0);
    }

    return (NULL);
}

VLIB_CLI_COMMAND (show_bier_fmask, static) = {
    .path = "show bier fmask",
    .short_help = "show bier fmask",
    .function = bier_fmask_show,
};
*/ #define vlib_set_next_frame(vm,node,next_index,v) \ ({ \ uword _n_left; \ vlib_get_next_frame ((vm), (node), (next_index), (v), _n_left); \ ASSERT (_n_left > 0); \ vlib_put_next_frame ((vm), (node), (next_index), _n_left - 1); \ (v); \ }) always_inline void vlib_set_next_frame_buffer (vlib_main_t * vm, vlib_node_runtime_t * node, u32 next_index, u32 buffer_index) { u32 *p; p = vlib_set_next_frame (vm, node, next_index, p); p[0] = buffer_index; } vlib_frame_t *vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index); void vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index, vlib_frame_t * f); always_inline uword vlib_in_process_context (vlib_main_t * vm) { return vm->node_main.current_process_index != ~0; } always_inline vlib_process_t * vlib_get_current_process (vlib_main_t * vm) { vlib_node_main_t *nm = &vm->node_main; if (vlib_in_process_context (vm)) return vec_elt (nm->processes, nm->current_process_index); return 0; } always_inline uword vlib_current_process (vlib_main_t * vm) { return vlib_get_current_process (vm)->node_runtime.node_index; } /** Returns TRUE if a process suspend time is less than 10us @param dt - remaining poll time in seconds @returns 1 if dt < 10e-6, 0 otherwise */ always_inline uword vlib_process_suspend_time_is_zero (f64 dt) { return dt < 10e-6; } /** Suspend a vlib cooperative multi-tasking thread for a period of time @param vm - vlib_main_t * @param dt - suspend interval in seconds @returns VLIB_PROCESS_RESUME_LONGJMP_RESUME, routinely ignored */ always_inline uword vlib_process_suspend (vlib_main_t * vm, f64 dt) { uword r; vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p = vec_elt (nm->processes, nm->current_process_index); if (vlib_process_suspend_time_is_zero (dt)) return VLIB_PROCESS_RESUME_LONGJMP_RESUME; p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK; r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND); if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND) { /* expiration time in 10us ticks */ p->resume_clock_interval = dt * 1e5; clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND); } return r; } always_inline void vlib_process_free_event_type (vlib_process_t * p, uword t, uword is_one_time_event) { ASSERT (!pool_is_free_index (p->event_type_pool, t)); pool_put_index (p->event_type_pool, t); if (is_one_time_event) p->one_time_event_type_bitmap = clib_bitmap_andnoti (p->one_time_event_type_bitmap, t); } always_inline void vlib_process_maybe_free_event_type (vlib_process_t * p, uword t) { ASSERT (!pool_is_free_index (p->event_type_pool, t)); if (clib_bitmap_get (p->one_time_event_type_bitmap, t)) vlib_process_free_event_type (p, t, /* is_one_time_event */ 1); } always_inline void * vlib_process_get_event_data (vlib_main_t * vm, uword * return_event_type_opaque) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; vlib_process_event_type_t *et; uword t; void *event_data_vector; p = vec_elt (nm->processes, nm->current_process_index); /* Find first type with events ready. Return invalid type when there's nothing there. */ t = clib_bitmap_first_set (p->non_empty_event_type_bitmap); if (t == ~0) return 0; p->non_empty_event_type_bitmap = clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t); ASSERT (_vec_len (p->pending_event_data_by_type_index[t]) > 0); event_data_vector = p->pending_event_data_by_type_index[t]; p->pending_event_data_by_type_index[t] = 0; et = pool_elt_at_index (p->event_type_pool, t); /* Return user's opaque value and possibly index. */ *return_event_type_opaque = et->opaque; vlib_process_maybe_free_event_type (p, t); return event_data_vector; } /* Return event data vector for later reuse. We reuse event data to avoid repeatedly allocating event vectors in cases where we care about speed. */ always_inline void vlib_process_put_event_data (vlib_main_t * vm, void *event_data) { vlib_node_main_t *nm = &vm->node_main; vec_add1 (nm->recycled_event_data_vectors, event_data); } /** Return the first event type which has occurred and a vector of per-event data of that type, or a timeout indication @param vm - vlib_main_t pointer @param data_vector - pointer to a (uword *) vector to receive event data @returns either an event type and a vector of per-event instance data, or ~0 to indicate a timeout. */ always_inline uword vlib_process_get_events (vlib_main_t * vm, uword ** data_vector) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; vlib_process_event_type_t *et; uword r, t, l; p = vec_elt (nm->processes, nm->current_process_index); /* Find first type with events ready. Return invalid type when there's nothing there. */ t = clib_bitmap_first_set (p->non_empty_event_type_bitmap); if (t == ~0) return t; p->non_empty_event_type_bitmap = clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t); l = _vec_len (p->pending_event_data_by_type_index[t]); if (data_vector) vec_add (*data_vector, p->pending_event_data_by_type_index[t], l); _vec_len (p->pending_event_data_by_type_index[t]) = 0; et = pool_elt_at_index (p->event_type_pool, t); /* Return user's opaque value. */ r = et->opaque; vlib_process_maybe_free_event_type (p, t); return r; } always_inline uword vlib_process_get_events_helper (vlib_process_t * p, uword t, uword ** data_vector) { uword l; p->non_empty_event_type_bitmap = clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t); l = _vec_len (p->pending_event_data_by_type_index[t]); if (data_vector) vec_add (*data_vector, p->pending_event_data_by_type_index[t], l); _vec_len (p->pending_event_data_by_type_index[t]) = 0; vlib_process_maybe_free_event_type (p, t); return l; } /* As above but query as specified type of event. Returns number of events found. */ always_inline uword vlib_process_get_events_with_type (vlib_main_t * vm, uword ** data_vector, uword with_type_opaque) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; uword t, *h; p = vec_elt (nm->processes, nm->current_process_index); h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque); if (!h) /* This can happen when an event has not yet been signaled with given opaque type. */ return 0; t = h[0]; if (!clib_bitmap_get (p->non_empty_event_type_bitmap, t)) return 0; return vlib_process_get_events_helper (p, t, data_vector); } always_inline uword * vlib_process_wait_for_event (vlib_main_t * vm) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; uword r; p = vec_elt (nm->processes, nm->current_process_index); if (clib_bitmap_is_zero (p->non_empty_event_type_bitmap)) { p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT; r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND); if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND) clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND); } return p->non_empty_event_type_bitmap; } always_inline uword vlib_process_wait_for_one_time_event (vlib_main_t * vm, uword ** data_vector, uword with_type_index) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; uword r; p = vec_elt (nm->processes, nm->current_process_index); ASSERT (!pool_is_free_index (p->event_type_pool, with_type_index)); while (!clib_bitmap_get (p->non_empty_event_type_bitmap, with_type_index)) { p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT; r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND); if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND) clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND); } return vlib_process_get_events_helper (p, with_type_index, data_vector); } always_inline uword vlib_process_wait_for_event_with_type (vlib_main_t * vm, uword ** data_vector, uword with_type_opaque) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; uword r, *h; p = vec_elt (nm->processes, nm->current_process_index); h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque); while (!h || !clib_bitmap_get (p->non_empty_event_type_bitmap, h[0])) { p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT; r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND); if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND) clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND); /* See if unknown event type has been signaled now. */ if (!h) h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque); } return vlib_process_get_events_helper (p, h[0], data_vector); } /** Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds to elapse @param vm - vlib_main_t pointer @param dt - timeout, in seconds. @returns the remaining time interval */ always_inline f64 vlib_process_wait_for_event_or_clock (vlib_main_t * vm, f64 dt) { vlib_node_main_t *nm = &vm->node_main; vlib_process_t *p; f64 wakeup_time; uword r; p = vec_elt (nm->processes, nm->current_process_index); if (vlib_process_suspend_time_is_zero (dt) || !clib_bitmap_is_zero (p->non_empty_event_type_bitmap)) return dt; wakeup_time = vlib_time_now (vm) + dt; /* Suspend waiting for both clock and event to occur. */ p->flags |= (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK); r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND); if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND) { p->resume_clock_interval = dt * 1e5; clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND); } /* Return amount of time still left to sleep. If <= 0 then we've been waken up by the clock (and not an event). */ return wakeup_time - vlib_time_now (vm); } always_inline vlib_process_event_type_t * vlib_process_new_event_type (vlib_process_t * p, uword with_type_opaque) { vlib_process_event_type_t *et; pool_get (p->event_type_pool, et); et->opaque = with_type_opaque; return et; } always_inline uword vlib_process_create_one_time_event (vlib_main_t * vm, uword node_index, uword with_type_opaque) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); vlib_process_event_type_t *et; uword t; et = vlib_process_new_event_type (p, with_type_opaque); t = et - p->event_type_pool; p->one_time_event_type_bitmap = clib_bitmap_ori (p->one_time_event_type_bitmap, t); return t; } always_inline void vlib_process_delete_one_time_event (vlib_main_t * vm, uword node_index, uword t) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); ASSERT (clib_bitmap_get (p->one_time_event_type_bitmap, t)); vlib_process_free_event_type (p, t, /* is_one_time_event */ 1); } always_inline void * vlib_process_signal_event_helper (vlib_node_main_t * nm, vlib_node_t * n, vlib_process_t * p, uword t, uword n_data_elts, uword n_data_elt_bytes) { uword p_flags, add_to_pending, delete_from_wheel; void *data_to_be_written_by_caller; ASSERT (n->type == VLIB_NODE_TYPE_PROCESS); ASSERT (!pool_is_free_index (p->event_type_pool, t)); vec_validate (p->pending_event_data_by_type_index, t); /* Resize data vector and return caller's data to be written. */ { void *data_vec = p->pending_event_data_by_type_index[t]; uword l; if (!data_vec && vec_len (nm->recycled_event_data_vectors)) { data_vec = vec_pop (nm->recycled_event_data_vectors); _vec_len (data_vec) = 0; } l = vec_len (data_vec); data_vec = _vec_resize (data_vec, /* length_increment */ n_data_elts, /* total size after increment */ (l + n_data_elts) * n_data_elt_bytes, /* header_bytes */ 0, /* data_align */ 0); p->pending_event_data_by_type_index[t] = data_vec; data_to_be_written_by_caller = data_vec + l * n_data_elt_bytes; } p->non_empty_event_type_bitmap = clib_bitmap_ori (p->non_empty_event_type_bitmap, t); p_flags = p->flags; /* Event was already signalled? */ add_to_pending = (p_flags & VLIB_PROCESS_RESUME_PENDING) == 0; /* Process will resume when suspend time elapses? */ delete_from_wheel = 0; if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK) { /* Waiting for both event and clock? */ if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT) { if (!TW (tw_timer_handle_is_free) ((TWT (tw_timer_wheel) *) nm->timing_wheel, p->stop_timer_handle)) delete_from_wheel = 1; else /* timer just popped so process should already be on the list */ add_to_pending = 0; } else /* Waiting only for clock. Event will be queue and may be handled when timer expires. */ add_to_pending = 0; } /* Never add current process to pending vector since current process is already running. */ add_to_pending &= nm->current_process_index != n->runtime_index; if (add_to_pending) { u32 x = vlib_timing_wheel_data_set_suspended_process (n->runtime_index); p->flags = p_flags | VLIB_PROCESS_RESUME_PENDING; vec_add1 (nm->data_from_advancing_timing_wheel, x); if (delete_from_wheel) TW (tw_timer_stop) ((TWT (tw_timer_wheel) *) nm->timing_wheel, p->stop_timer_handle); } return data_to_be_written_by_caller; } always_inline void * vlib_process_signal_event_data (vlib_main_t * vm, uword node_index, uword type_opaque, uword n_data_elts, uword n_data_elt_bytes) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); uword *h, t; /* Must be in main thread */ ASSERT (vlib_get_thread_index () == 0); h = hash_get (p->event_type_index_by_type_opaque, type_opaque); if (!h) { vlib_process_event_type_t *et = vlib_process_new_event_type (p, type_opaque); t = et - p->event_type_pool; hash_set (p->event_type_index_by_type_opaque, type_opaque, t); } else t = h[0]; return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts, n_data_elt_bytes); } always_inline void * vlib_process_signal_event_at_time (vlib_main_t * vm, f64 dt, uword node_index, uword type_opaque, uword n_data_elts, uword n_data_elt_bytes) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); uword *h, t; h = hash_get (p->event_type_index_by_type_opaque, type_opaque); if (!h) { vlib_process_event_type_t *et = vlib_process_new_event_type (p, type_opaque); t = et - p->event_type_pool; hash_set (p->event_type_index_by_type_opaque, type_opaque, t); } else t = h[0]; if (vlib_process_suspend_time_is_zero (dt)) return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts, n_data_elt_bytes); else { vlib_signal_timed_event_data_t *te; pool_get_aligned (nm->signal_timed_event_data_pool, te, sizeof (te[0])); te->n_data_elts = n_data_elts; te->n_data_elt_bytes = n_data_elt_bytes; te->n_data_bytes = n_data_elts * n_data_elt_bytes; /* Assert that structure fields are big enough. */ ASSERT (te->n_data_elts == n_data_elts); ASSERT (te->n_data_elt_bytes == n_data_elt_bytes); ASSERT (te->n_data_bytes == n_data_elts * n_data_elt_bytes); te->process_node_index = n->runtime_index; te->event_type_index = t; p->stop_timer_handle = TW (tw_timer_start) ((TWT (tw_timer_wheel) *) nm->timing_wheel, vlib_timing_wheel_data_set_timed_event (te - nm->signal_timed_event_data_pool), 0 /* timer_id */ , (vlib_time_now (vm) + dt) * 1e5); /* Inline data big enough to hold event? */ if (te->n_data_bytes < sizeof (te->inline_event_data)) return te->inline_event_data; else { te->event_data_as_vector = 0; vec_resize (te->event_data_as_vector, te->n_data_bytes); return te->event_data_as_vector; } } } always_inline void * vlib_process_signal_one_time_event_data (vlib_main_t * vm, uword node_index, uword type_index, uword n_data_elts, uword n_data_elt_bytes) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); return vlib_process_signal_event_helper (nm, n, p, type_index, n_data_elts, n_data_elt_bytes); } always_inline void vlib_process_signal_event (vlib_main_t * vm, uword node_index, uword type_opaque, uword data) { uword *d = vlib_process_signal_event_data (vm, node_index, type_opaque, 1 /* elts */ , sizeof (uword)); d[0] = data; } always_inline void vlib_process_signal_event_pointer (vlib_main_t * vm, uword node_index, uword type_opaque, void *data) { void **d = vlib_process_signal_event_data (vm, node_index, type_opaque, 1 /* elts */ , sizeof (data)); d[0] = data; } /** * Signal event to process from any thread. * * When in doubt, use this. */ always_inline void vlib_process_signal_event_mt (vlib_main_t * vm, uword node_index, uword type_opaque, uword data) { if (vlib_get_thread_index () != 0) { vlib_process_signal_event_mt_args_t args = { .node_index = node_index, .type_opaque = type_opaque, .data = data, }; vlib_rpc_call_main_thread (vlib_process_signal_event_mt_helper, (u8 *) & args, sizeof (args)); } else vlib_process_signal_event (vm, node_index, type_opaque, data); } always_inline void vlib_process_signal_one_time_event (vlib_main_t * vm, uword node_index, uword type_index, uword data) { uword *d = vlib_process_signal_one_time_event_data (vm, node_index, type_index, 1 /* elts */ , sizeof (uword)); d[0] = data; } always_inline void vlib_signal_one_time_waiting_process (vlib_main_t * vm, vlib_one_time_waiting_process_t * p) { vlib_process_signal_one_time_event (vm, p->node_index, p->one_time_event, /* data */ ~0); clib_memset (p, ~0, sizeof (p[0])); } always_inline void vlib_signal_one_time_waiting_process_vector (vlib_main_t * vm, vlib_one_time_waiting_process_t ** wps) { vlib_one_time_waiting_process_t *wp; vec_foreach (wp, *wps) vlib_signal_one_time_waiting_process (vm, wp); vec_free (*wps); } always_inline void vlib_current_process_wait_for_one_time_event (vlib_main_t * vm, vlib_one_time_waiting_process_t * p) { p->node_index = vlib_current_process (vm); p->one_time_event = vlib_process_create_one_time_event (vm, p->node_index, /* type opaque */ ~0); vlib_process_wait_for_one_time_event (vm, /* don't care about data */ 0, p->one_time_event); } always_inline void vlib_current_process_wait_for_one_time_event_vector (vlib_main_t * vm, vlib_one_time_waiting_process_t ** wps) { vlib_one_time_waiting_process_t *wp; vec_add2 (*wps, wp, 1); vlib_current_process_wait_for_one_time_event (vm, wp); } always_inline u32 vlib_node_runtime_update_main_loop_vector_stats (vlib_main_t * vm, vlib_node_runtime_t * node, uword n_vectors) { u32 i, d, vi0, vi1; u32 i0, i1; ASSERT (is_pow2 (ARRAY_LEN (node->main_loop_vector_stats))); i = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE) & (ARRAY_LEN (node->main_loop_vector_stats) - 1)); i0 = i ^ 0; i1 = i ^ 1; d = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE) - (node->main_loop_count_last_dispatch >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)); vi0 = node->main_loop_vector_stats[i0]; vi1 = node->main_loop_vector_stats[i1]; vi0 = d == 0 ? vi0 : 0; vi1 = d <= 1 ? vi1 : 0; vi0 += n_vectors; node->main_loop_vector_stats[i0] = vi0; node->main_loop_vector_stats[i1] = vi1; node->main_loop_count_last_dispatch = vm->main_loop_count; /* Return previous counter. */ return node->main_loop_vector_stats[i1]; } always_inline f64 vlib_node_vectors_per_main_loop_as_float (vlib_main_t * vm, u32 node_index) { vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index); u32 v; v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt, /* n_vectors */ 0); return (f64) v / (1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE); } always_inline u32 vlib_node_vectors_per_main_loop_as_integer (vlib_main_t * vm, u32 node_index) { vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index); u32 v; v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt, /* n_vectors */ 0); return v >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE; } void vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f); /* Return the edge index if present, ~0 otherwise */ uword vlib_node_get_next (vlib_main_t * vm, uword node, uword next_node); /* Add next node to given node in given slot. */ uword vlib_node_add_next_with_slot (vlib_main_t * vm, uword node, uword next_node, uword slot); /* As above but adds to end of node's next vector. */ always_inline uword vlib_node_add_next (vlib_main_t * vm, uword node, uword next_node) { return vlib_node_add_next_with_slot (vm, node, next_node, ~0); } /* Add next node to given node in given slot. */ uword vlib_node_add_named_next_with_slot (vlib_main_t * vm, uword node, char *next_name, uword slot); /* As above but adds to end of node's next vector. */ always_inline uword vlib_node_add_named_next (vlib_main_t * vm, uword node, char *name) { return vlib_node_add_named_next_with_slot (vm, node, name, ~0); } /** * Get list of nodes */ void vlib_node_get_nodes (vlib_main_t * vm, u32 max_threads, int include_stats, int barrier_sync, vlib_node_t **** node_dupsp, vlib_main_t *** stat_vmsp); /* Query node given name. */ vlib_node_t *vlib_get_node_by_name (vlib_main_t * vm, u8 * name); /* Rename a node. */ void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...); /* Register new packet processing node. Nodes can be registered dynamically via this call or statically via the VLIB_REGISTER_NODE macro. */ u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r); /* Register all static nodes registered via VLIB_REGISTER_NODE. */ void vlib_register_all_static_nodes (vlib_main_t * vm); /* Start a process. */ void vlib_start_process (vlib_main_t * vm, uword process_index); /* Sync up runtime and main node stats. */ void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n); /* Node graph initialization function. */ clib_error_t *vlib_node_main_init (vlib_main_t * vm); format_function_t format_vlib_node_graph; format_function_t format_vlib_node_name; format_function_t format_vlib_next_node_name; format_function_t format_vlib_node_and_next; format_function_t format_vlib_cpu_time; format_function_t format_vlib_time; /* Parse node name -> node index. */ unformat_function_t unformat_vlib_node; always_inline void vlib_node_increment_counter (vlib_main_t * vm, u32 node_index, u32 counter_index, u64 increment) { vlib_node_t *n = vlib_get_node (vm, node_index); vlib_error_main_t *em = &vm->error_main; u32 node_counter_base_index = n->error_heap_index; em->counters[node_counter_base_index + counter_index] += increment; } #endif /* included_vlib_node_funcs_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */