summaryrefslogtreecommitdiffstats
path: root/test/ipfix.py
blob: bac6dfd4c2a6b9623be245988a9bc34fc47a7600 (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
#!/usr/bin/env python3
# IPFIX support for Scapy (RFC7011)

from scapy.all import bind_layers, FieldLenField, IntField, Packet, \
    PacketListField, ShortEnumField, ShortField, StrLenField
from scapy.layers.inet import UDP


# IPFIX Information Elements http://www.iana.org/assignments/ipfix/ipfix.xhtml
information_elements = {
    1:   "octetDeltaCount",
    2:   "packetDeltaCount",
    3:   "deltaFlowCount",
    4:   "protocolIdentifier",
    5:   "ipClassOfService",
    6:   "tcpControlBits",
    7:   "sourceTransportPort",
    8:   "sourceIPv4Address",
    9:   "sourceIPv4PrefixLength",
    10:  "ingressInterface",
    11:  "destinationTransportPort",
    12:  "destinationIPv4Address",
    13:  "destinationIPv4PrefixLength",
    14:  "egressInterface",
    15:  "ipNextHopIPv4Address",
    16:  "bgpSourceAsNumber",
    17:  "bgpDestinationAsNumber",
    18:  "bgpNextHopIPv4Address",
    19:  "postMCastPacketDeltaCount",
    20:  "postMCastOctetDeltaCount",
    21:  "flowEndSysUpTime",
    22:  "flowStartSysUpTime",
    23:  "postOctetDeltaCount",
    24:  "postPacketDeltaCount",
    25:  "minimumIpTotalLength",
    26:  "maximumIpTotalLength",
    27:  "sourceIPv6Address",
    28:  "destinationIPv6Address",
    29:  "sourceIPv6PrefixLength",
    30:  "destinationIPv6PrefixLength",
    31:  "flowLabelIPv6",
    32:  "icmpTypeCodeIPv4",
    33:  "igmpType",
    34:  "samplingInterval",
    35:  "samplingAlgorithm",
    36:  "flowActiveTimeout",
    37:  "flowIdleTimeout",
    38:  "engineType",
    39:  "engineId",
    40:  "exportedOctetTotalCount",
    41:  "exportedMessageTotalCount",
    42:  "exportedFlowRecordTotalCount",
    43:  "ipv4RouterSc",
    44:  "sourceIPv4Prefix",
    45:  "destinationIPv4Prefix",
    46:  "mplsTopLabelType",
    47:  "mplsTopLabelIPv4Address",
    48:  "samplerId",
    49:  "samplerMode",
    50:  "samplerRandomInterval",
    51:  "classId",
    52:  "minimumTTL",
    53:  "maximumTTL",
    54:  "fragmentIdentification",
    55:  "postIpClassOfService",
    56:  "sourceMacAddress",
    57:  "postDestinationMacAddress",
    58:  "vlanId",
    59:  "postVlanId",
    60:  "ipVersion",
    61:  "flowDirection",
    62:  "ipNextHopIPv6Address",
    63:  "bgpNextHopIPv6Address",
    64:  "ipv6ExtensionHeaders",
    70:  "mplsTopLabelStackSection",
    71:  "mplsLabelStackSection2",
    72:  "mplsLabelStackSection3",
    73:  "mplsLabelStackSection4",
    74:  "mplsLabelStackSection5",
    75:  "mplsLabelStackSection6",
    76:  "mplsLabelStackSection7",
    77:  "mplsLabelStackSection8",
    78:  "mplsLabelStackSection9",
    79:  "mplsLabelStackSection10",
    80:  "destinationMacAddress",
    81:  "postSourceMacAddress",
    82:  "interfaceName",
    83:  "interfaceDescription",
    84:  "samplerName",
    85:  "octetTotalCount",
    86:  "packetTotalCount",
    87:  "flagsAndSamplerId",
    88:  "fragmentOffset",
    89:  "forwardingStatus",
    90:  "mplsVpnRouteDistinguisher",
    91:  "mplsTopLabelPrefixLength",
    92:  "srcTrafficIndex",
    93:  "dstTrafficIndex",
    94:  "applicationDescription",
    95:  "applicationId",
    96:  "applicationName",
    98:  "postIpDiffServCodePoint",
    99:  "multicastReplicationFactor",
    100: "className",
    101: "classificationEngineId",
    102: "layer2packetSectionOffset",
    103: "layer2packetSectionSize",
    104: "layer2packetSectionData",
    128: "bgpNextAdjacentAsNumber",
    129: "bgpPrevAdjacentAsNumber",
    130: "exporterIPv4Address",
    131: "exporterIPv6Address",
    132: "droppedOctetDeltaCount",
    133: "droppedPacketDeltaCount",
    134: "droppedOctetTotalCount",
    135: "droppedPacketTotalCount",
    136: "flowEndReason",
    137: "commonPropertiesId",
    138: "observationPointId",
    139: "icmpTypeCodeIPv6",
    140: "mplsTopLabelIPv6Address",
    141: "lineCardId",
    142: "portId",
    143: "meteringProcessId",
    144: "exportingProcessId",
    145: "templateId",
    146: "wlanChannelId",
    147: "wlanSSID",
    148: "flowId",
    149: "observationDomainId",
    150: "flowStartSeconds",
    151: "flowEndSeconds",
    152: "flowStartMilliseconds",
    153: "flowEndMilliseconds",
    154: "flowStartMicroseconds",
    155: "flowEndMicroseconds",
    156: "flowStartNanoseconds",
    157: "flowEndNanoseconds",
    158: "flowStartDeltaMicroseconds",
    159: "flowEndDeltaMicroseconds",
    160: "systemInitTimeMilliseconds",
    161: "flowDurationMilliseconds",
    162: "flowDurationMicroseconds",
    163: "observedFlowTotalCount",
    164: "ignoredPacketTotalCount",
    165: "ignoredOctetTotalCount",
    166: "notSentFlowTotalCount",
    167: "notSentPacketTotalCount",
    168: "notSentOctetTotalCount",
    169: "destinationIPv6Prefix",
    170: "sourceIPv6Prefix",
    171: "postOctetTotalCount",
    172: "postPacketTotalCount",
    173: "flowKeyIndicator",
    174: "postMCastPacketTotalCount",
    175: "postMCastOctetTotalCount",
    176: "icmpTypeIPv4",
    177: "icmpCodeIPv4",
    178: "icmpTypeIPv6",
    179: "icmpCodeIPv6",
    180: "udpSourcePort",
    181: "udpDestinationPort",
    182: "tcpSourcePort",
    183: "tcpDestinationPort",
    184
/*
 * 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 <vlib/vlib.h>

#include <vnet/mfib/mfib_entry.h>
#include <vnet/fib/fib_path_list.h>

#include <vnet/dpo/drop_dpo.h>
#include <vnet/dpo/replicate_dpo.h>

/**
 * Debug macro
 */
#ifdef MFIB_DEBUG
#DEFIne MFIB_ENTRY_DBG(_e, _fmt, _args...)		\
{                                                       \
    u8*__tmp = NULL;					\
    __tmp = format(__tmp, "e:[%d:%U",                   \
                   mfib_entry_get_index(_e),		\
                   format_ip46_address,			\
                   &_e->mfe_prefix.fp_grp_addr,		\
                   IP46_TYPE_ANY);			\
    __tmp = format(__tmp, "/%d,",			\
                   _e->mfe_prefix.fp_len);		\
    __tmp = format(__tmp, "%U]",                        \
                   mfib_entry_get_index(_e),		\
                   format_ip46_address,			\
                   &_e->mfe_prefix.fp_src_addr,		\
                   IP46_TYPE_ANY);			\
    __tmp = format(__tmp, _fmt, ##_args);		\
    clib_warning("%s", __tmp);				\
    vec_free(__tmp);					\
}
#else
#define MFIB_ENTRY_DBG(_e, _fmt, _args...)
#endif

/**
 * The source of an MFIB entry
 */
typedef struct mfib_entry_src_t_
{
    /**
     * Which source this is
     */
    mfib_source_t mfes_src;

    /**
     * The path-list of forwarding interfaces
     */
    fib_node_index_t mfes_pl;

    /**
     * Route flags
     */
    mfib_entry_flags_t mfes_flags;

    /**
     * The hash table of all interfaces
     */
    mfib_itf_t *mfes_itfs;
} mfib_entry_src_t;

/**
 * String names for each source
 */
static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;

/*
 * Pool for all fib_entries
 */
mfib_entry_t *mfib_entry_pool;

static fib_node_t *
mfib_entry_get_node (fib_node_index_t index)
{
    return ((fib_node_t*)mfib_entry_get(index));
}

static fib_protocol_t
mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
{
    return (mfib_entry->mfe_prefix.fp_proto);
}

fib_forward_chain_type_t
mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
{
    switch (mfib_entry->mfe_prefix.fp_proto)
    {
    case FIB_PROTOCOL_IP4:
        return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
    case FIB_PROTOCOL_IP6:
        return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
    case FIB_PROTOCOL_MPLS:
        ASSERT(0);
        break;
    }
    return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
}

static u8 *
format_mfib_entry_dpo (u8 * s, va_list * args)
{
    index_t fei = va_arg(*args, index_t);
    CLIB_UNUSED(u32 indent) = va_arg(*args, u32);

    return (format(s, "%U",
                   format_mfib_entry, fei,
                   MFIB_ENTRY_FORMAT_BRIEF));
}

u8 *
format_mfib_entry (u8 * s, va_list * args)
{
    fib_node_index_t fei, mfi;
    mfib_entry_t *mfib_entry;
    mfib_entry_src_t *msrc;
    u32 sw_if_index;
    int level;

    fei = va_arg (*args, fib_node_index_t);
    level = va_arg (*args, int);
    mfib_entry = mfib_entry_get(fei);

    s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
    s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);

    if (level >= MFIB_ENTRY_FORMAT_DETAIL)
    {
        s = format (s, "\n");
        s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
        s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
        s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
        vec_foreach(msrc, mfib_entry->mfe_srcs)
        {
            s = format (s, "  src:%s", mfib_source_names[msrc->mfes_src]);
            s = format (s, ": %U\n", format_mfib_entry_flags, msrc->mfes_flags);
            if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
            {
                s = fib_path_list_format(msrc->mfes_pl, s);
            }
            hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
            ({
                s = format(s, "    %U\n", format_mfib_itf, mfi);
            }));
        }
    }

    s = format(s, "\n  Interfaces:");
    hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
    ({
        s = format(s, "\n  %U", format_mfib_itf, mfi);
    }));

    s = format(s, "\n  %U-chain\n  %U",
               format_fib_forw_chain_type,
               mfib_entry_get_default_chain_type(mfib_entry),
               format_dpo_id,
               &mfib_entry->mfe_rep,
               2);
    s = format(s, "\n");

    if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
    {
        s = format(s, "\nchildren:");
        s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
    }

    return (s);
}

static mfib_entry_t*
mfib_entry_from_fib_node (fib_node_t *node)
{
#if CLIB_DEBUG > 0
    ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
#endif
    return ((mfib_entry_t*)node);
}

static int
mfib_entry_src_cmp_for_sort (void * v1,
                             void * v2)
{
    mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;

    return (esrc1->mfes_src - esrc2->mfes_src);
}

static void
mfib_entry_src_init (mfib_entry_t *mfib_entry,
                     mfib_source_t source)

{
    mfib_entry_src_t esrc = {
        .mfes_pl = FIB_NODE_INDEX_INVALID,
        .mfes_flags = MFIB_ENTRY_FLAG_NONE,
        .mfes_src = source,
    };

    vec_add1(mfib_entry->mfe_srcs, esrc);
    vec_sort_with_function(mfib_entry->mfe_srcs,
                           mfib_entry_src_cmp_for_sort);
}

static mfib_entry_src_t *
mfib_entry_src_find (const mfib_entry_t *mfib_entry,
                    mfib_source_t source,
                    u32 *index)

{
    mfib_entry_src_t *esrc;
    int ii;

    ii = 0;
    vec_foreach(esrc, mfib_entry->mfe_srcs)
    {
        if (esrc->mfes_src == source)
        {
            if (NULL != index)
            {
                *index = ii;
            }
            return (esrc);
        }
        else
        {
            ii++;
        }
    }

    return (NULL);
}

static mfib_entry_src_t *
mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
                              mfib_source_t source)
{
    mfib_entry_src_t *esrc;

    esrc = mfib_entry_src_find(mfib_entry, source, NULL);

    if (NULL == esrc)
    {
        mfib_entry_src_init(mfib_entry, source);
    }

    return (mfib_entry_src_find(mfib_entry, source, NULL));
}

static mfib_entry_src_t*
mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
{
    mfib_entry_src_t *bsrc;

    /*
     * the enum of sources is deliberately arranged in priority order
     */
    if (0 == vec_len(mfib_entry->mfe_srcs))
    {
        bsrc = NULL;
    }
    else
    {
        bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
    }

    return (bsrc);
}

static void
mfib_entry_src_flush (mfib_entry_src_t *msrc)
{
    u32 sw_if_index;
    index_t mfii;

    hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
    ({
        mfib_itf_delete(mfib_itf_get(mfii));
    }));
    fib_path_list_unlock(msrc->mfes_pl);
}

static void
mfib_entry_src_remove (mfib_entry_t *mfib_entry,
                       mfib_source_t source)

{
    mfib_entry_src_t *msrc;
    u32 index = ~0;

    msrc = mfib_entry_src_find(mfib_entry, source, &index);

    if (NULL != msrc)
    {
        mfib_entry_src_flush(msrc);
        vec_del1(mfib_entry->mfe_srcs, index);
    }
}

static int
mfib_entry_src_n_itfs (const mfib_entry_src_t *msrc)
{
    return (hash_elts(msrc->mfes_itfs));
}


static void
mfib_entry_last_lock_gone (fib_node_t *node)
{
    mfib_entry_t *mfib_entry;
    mfib_entry_src_t *msrc;

    mfib_entry = mfib_entry_from_fib_node(node);

    dpo_reset(&mfib_entry->mfe_rep);

    MFIB_ENTRY_DBG(mfib_entry, "last-lock");

    vec_foreach(msrc, mfib_entry->mfe_srcs)
    {
        mfib_entry_src_flush(msrc);
    }

    fib_path_list_unlock(mfib_entry->mfe_parent);
    vec_free(mfib_entry->mfe_srcs);

    fib_node_deinit(&mfib_entry->mfe_node);
    pool_put(mfib_entry_pool, mfib_entry);
}

/*
 * mfib_entry_back_walk_notify
 *
 * A back walk has reach this entry.
 */
static fib_node_back_walk_rc_t
mfib_entry_back_walk_notify (fib_node_t *node,
                            fib_node_back_walk_ctx_t *ctx)
{
    // FIXME - re-evalute

    return (FIB_NODE_BACK_WALK_CONTINUE);
}

static void
mfib_entry_show_memory (void)
{
    fib_show_memory_usage("multicast-Entry",
                          pool_elts(mfib_entry_pool),
                          pool_len(mfib_entry_pool),
                          sizeof(mfib_entry_t));
}

/*
 * The MFIB entry's graph node virtual function table
 */
static const fib_node_vft_t mfib_entry_vft = {
    .fnv_get = mfib_entry_get_node,
    .fnv_last_lock = mfib_entry_last_lock_gone,
    .fnv_back_walk = mfib_entry_back_walk_notify,
    .fnv_mem_show = mfib_entry_show_memory,
};

u32
mfib_entry_child_add (fib_node_index_t mfib_entry_index,
                      fib_node_type_t child_type,
                      fib_node_index_t child_index)
{
    return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
                               mfib_entry_index,
                               child_type,
                               child_index));
};

void
mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
                         u32 sibling_index)
{
    fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
                          mfib_entry_index,
                          sibling_index);
}

static mfib_entry_t *
mfib_entry_alloc (u32 fib_index,
                  const mfib_prefix_t *prefix,
                  fib_node_index_t *mfib_entry_index)
{
    mfib_entry_t *mfib_entry;

    pool_get(mfib_entry_pool, mfib_entry);
    memset(mfib_entry, 0, sizeof(*mfib_entry));

    fib_node_init(&mfib_entry->mfe_node,
                  FIB_NODE_TYPE_MFIB_ENTRY);

    mfib_entry->mfe_fib_index = fib_index;
    mfib_entry->mfe_prefix = *prefix;
    mfib_entry->mfe_parent = FIB_NODE_INDEX_INVALID;

    dpo_reset(&mfib_entry->mfe_rep);

    *mfib_entry_index = mfib_entry_get_index(mfib_entry);

    MFIB_ENTRY_DBG(mfib_entry, "alloc");

    return (mfib_entry);
}

typedef struct mfib_entry_collect_forwarding_ctx_t_
{
    load_balance_path_t * next_hops;
    fib_forward_chain_type_t fct;
} mfib_entry_collect_forwarding_ctx_t;

static int
mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
                                   fib_node_index_t path_index,
                                   void *arg)
{
    mfib_entry_collect_forwarding_ctx_t *ctx;
    load_balance_path_t *nh;

    ctx = arg;

    /*
     * if the path is not resolved, don't include it.
     */
    if (!fib_path_is_resolved(path_index))
    {
        return (!0);
    }

    switch (ctx->fct)
    {
    case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
    case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
        /*
         * EOS traffic with no label to stack, we need the IP Adj
         */
        vec_add2(ctx->next_hops, nh, 1);

        nh->path_index = path_index;
        nh->path_weight = fib_path_get_weight(path_index);
        fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
        break;

    case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
    case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
    case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
    case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
    case FIB_FORW_CHAIN_TYPE_ETHERNET:
    case FIB_FORW_CHAIN_TYPE_NSH:
        ASSERT(0);
        break;
    }

    return (!0);
}

static void
mfib_entry_stack (mfib_entry_t *mfib_entry)
{
    dpo_proto_t dp;

    dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));

    if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_parent)
    {
        mfib_entry_collect_forwarding_ctx_t ctx = {
            .next_hops = NULL,
            .fct = mfib_entry_get_default_chain_type(mfib_entry),
        };

        fib_path_list_walk(mfib_entry->mfe_parent,
                           mfib_entry_src_collect_forwarding,
                           &ctx);

        if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
        {
            /*
             * each path contirbutes a next-hop. form a replicate
             * from those choices.
             */
            if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
                dpo_is_drop(&mfib_entry->mfe_rep))
            {
                dpo_id_t tmp_dpo = DPO_INVALID;

                dpo_set(&tmp_dpo,
                        DPO_REPLICATE, dp,
                        replicate_create(0, dp));

                dpo_stack(DPO_MFIB_ENTRY, dp,
                          &mfib_entry->mfe_rep,
                          &tmp_dpo);

                dpo_reset(&tmp_dpo);
            }
            replicate_multipath_update(&mfib_entry->mfe_rep,
                                       ctx.next_hops);
        }
        else
        {
            /*
             * for exclusive routes the source provided a replicate DPO
             * we we stashed inthe special path list with one path
             * so we can stack directly on that.
             */
            ASSERT(1 == vec_len(ctx.next_hops));

            dpo_stack(DPO_MFIB_ENTRY, dp,
                      &mfib_entry->mfe_rep,
                      &ctx.next_hops[0].path_dpo);
            dpo_reset(&ctx.next_hops[0].path_dpo);
            vec_free(ctx.
o">*msrc, const fib_route_path_t *rpath) { fib_node_index_t old_pl_index; fib_route_path_t *rpaths; ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags)); /* * path-lists require a vector of paths */ rpaths = NULL; vec_add1(rpaths, rpath[0]); old_pl_index = msrc->mfes_pl; if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl) { msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF, rpaths); } else { msrc->mfes_pl = fib_path_list_copy_and_path_add(msrc->mfes_pl, FIB_PATH_LIST_FLAG_NO_URPF, rpaths); } fib_path_list_lock(msrc->mfes_pl); fib_path_list_unlock(old_pl_index); vec_free(rpaths); } static int mfib_entry_forwarding_path_remove (mfib_entry_src_t *msrc, const fib_route_path_t *rpath) { fib_node_index_t old_pl_index; fib_route_path_t *rpaths; ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags)); /* * path-lists require a vector of paths */ rpaths = NULL; vec_add1(rpaths, rpath[0]); old_pl_index = msrc->mfes_pl; msrc->mfes_pl = fib_path_list_copy_and_path_remove(msrc->mfes_pl, FIB_PATH_LIST_FLAG_NONE, rpaths); fib_path_list_lock(msrc->mfes_pl); fib_path_list_unlock(old_pl_index); vec_free(rpaths); return (FIB_NODE_INDEX_INVALID != msrc->mfes_pl); } static void mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry) { fib_node_index_t old_pl_index; mfib_entry_src_t *bsrc; old_pl_index = mfib_entry->mfe_parent; /* * copy the forwarding data from the bast source */ bsrc = mfib_entry_get_best_src(mfib_entry); if (NULL == bsrc) { mfib_entry->mfe_parent = FIB_NODE_INDEX_INVALID; } else { mfib_entry->mfe_parent = bsrc->mfes_pl; mfib_entry->mfe_flags = bsrc->mfes_flags; mfib_entry->mfe_itfs = bsrc->mfes_itfs; } /* * re-stack the entry on the best forwarding info. */ if (old_pl_index != mfib_entry->mfe_parent || FIB_NODE_INDEX_INVALID == old_pl_index) { mfib_entry_stack(mfib_entry); fib_path_list_lock(mfib_entry->mfe_parent); fib_path_list_unlock(old_pl_index); } } fib_node_index_t mfib_entry_create (u32 fib_index, mfib_source_t source, const mfib_prefix_t *prefix, mfib_entry_flags_t entry_flags) { fib_node_index_t mfib_entry_index; mfib_entry_t *mfib_entry; mfib_entry_src_t *msrc; mfib_entry = mfib_entry_alloc(fib_index, prefix, &mfib_entry_index); msrc = mfib_entry_src_find_or_create(mfib_entry, source); msrc->mfes_flags = entry_flags; mfib_entry_recalculate_forwarding(mfib_entry); return (mfib_entry_index); } static int mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry) { return (0 == vec_len(mfib_entry->mfe_srcs)); } static int mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc) { return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags && 0 == mfib_entry_src_n_itfs(msrc))); } int mfib_entry_update (fib_node_index_t mfib_entry_index, mfib_source_t source, mfib_entry_flags_t entry_flags, index_t repi) { mfib_entry_t *mfib_entry; mfib_entry_src_t *msrc; mfib_entry = mfib_entry_get(mfib_entry_index); msrc = mfib_entry_src_find_or_create(mfib_entry, source); msrc->mfes_flags = entry_flags; if (INDEX_INVALID != repi) { /* * The source is providing its own replicate DPO. * Create a sepcial path-list to manage it, that way * this entry and the source are equivalent to a normal * entry */ fib_node_index_t old_pl_index; fib_protocol_t fp; dpo_id_t dpo = DPO_INVALID; fp = mfib_entry_get_proto(mfib_entry); old_pl_index = msrc->mfes_pl; dpo_set(&dpo, DPO_REPLICATE, fib_proto_to_dpo(fp), repi); msrc->mfes_pl = fib_path_list_create_special(fp, FIB_PATH_LIST_FLAG_EXCLUSIVE, &dpo); dpo_reset(&dpo); fib_path_list_lock(msrc->mfes_pl); fib_path_list_unlock(old_pl_index); } if (mfib_entry_src_ok_for_delete(msrc)) { /* * this source has no interfaces and no flags. * it has nothing left to give - remove it */ mfib_entry_src_remove(mfib_entry, source); } mfib_entry_recalculate_forwarding(mfib_entry); return (mfib_entry_ok_for_delete(mfib_entry)); } static void mfib_entry_itf_add (mfib_entry_src_t *msrc, u32 sw_if_index, index_t mi) { hash_set(msrc->mfes_itfs, sw_if_index, mi); } static void mfib_entry_itf_remove (mfib_entry_src_t *msrc, u32 sw_if_index) { mfib_itf_t *mfi; mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index); mfib_itf_delete(mfi); hash_unset(msrc->mfes_itfs, sw_if_index); } void mfib_entry_path_update (fib_node_index_t mfib_entry_index, mfib_source_t source, const fib_route_path_t *rpath, mfib_itf_flags_t itf_flags) { mfib_entry_t *mfib_entry; mfib_entry_src_t *msrc; mfib_itf_t *mfib_itf; mfib_entry = mfib_entry_get(mfib_entry_index); ASSERT(NULL != mfib_entry); msrc = mfib_entry_src_find_or_create(mfib_entry, source); /* * search for the interface in the current set */ mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs, rpath[0].frp_sw_if_index); if (NULL == mfib_itf) { /* * this is a path we do not yet have. If it is forwarding then we * add it to the replication set */ if (itf_flags & MFIB_ITF_FLAG_FORWARD) { mfib_entry_forwarding_path_add(msrc, rpath); } /* * construct a new ITF for this entry's list */ mfib_entry_itf_add(msrc, rpath[0].frp_sw_if_index, mfib_itf_create(rpath[0].frp_sw_if_index, itf_flags)); } else { int was_forwarding = !!(mfib_itf->mfi_flags & MFIB_ITF_FLAG_FORWARD); int is_forwarding = !!(itf_flags & MFIB_ITF_FLAG_FORWARD); if (!was_forwarding && is_forwarding) { mfib_entry_forwarding_path_add(msrc, rpath); } else if (was_forwarding && !is_forwarding) { mfib_entry_forwarding_path_remove(msrc, rpath); } /* * packets in flight see these updates. */ mfib_itf->mfi_flags = itf_flags; } mfib_entry_recalculate_forwarding(mfib_entry); } /* * mfib_entry_path_remove * * remove a path from the entry. * return the mfib_entry's index if it is still present, INVALID otherwise. */ int mfib_entry_path_remove (fib_node_index_t mfib_entry_index, mfib_source_t source, const fib_route_path_t *rpath) { mfib_entry_t *mfib_entry; mfib_entry_src_t *msrc; mfib_itf_t *mfib_itf; mfib_entry = mfib_entry_get(mfib_entry_index); ASSERT(NULL != mfib_entry); msrc = mfib_entry_src_find(mfib_entry, source, NULL); if (NULL == msrc) { /* * there are no paths left for this source */ return (mfib_entry_ok_for_delete(mfib_entry)); } /* * search for the interface in the current set */ mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs, rpath[0].frp_sw_if_index); if (NULL == mfib_itf) { /* * removing a path that does not exist */ return (mfib_entry_ok_for_delete(mfib_entry)); } /* * we have this path. If it is forwarding then we * remove it to the replication set */ if (mfib_itf->mfi_flags & MFIB_ITF_FLAG_FORWARD) { mfib_entry_forwarding_path_remove(msrc, rpath); } /* * remove the interface/path from this entry's list */ mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index); if (mfib_entry_src_ok_for_delete(msrc)) { /* * this source has no interfaces and no flags. * it has nothing left to give - remove it */ mfib_entry_src_remove(mfib_entry, source); } mfib_entry_recalculate_forwarding(mfib_entry); return (mfib_entry_ok_for_delete(mfib_entry)); } /** * mfib_entry_delete * * The source is withdrawing all the paths it provided */ int mfib_entry_delete (fib_node_index_t mfib_entry_index, mfib_source_t source) { mfib_entry_t *mfib_entry; mfib_entry = mfib_entry_get(mfib_entry_index); mfib_entry_src_remove(mfib_entry, source); mfib_entry_recalculate_forwarding(mfib_entry); return (mfib_entry_ok_for_delete(mfib_entry)); } static int fib_ip4_address_compare (ip4_address_t * a1, ip4_address_t * a2) { /* * IP addresses are unsiged ints. the return value here needs to be signed * a simple subtraction won't cut it. * If the addresses are the same, the sort order is undefiend, so phoey. */ return ((clib_net_to_host_u32(a1->data_u32) > clib_net_to_host_u32(a2->data_u32) ) ? 1 : -1); } static int fib_ip6_address_compare (ip6_address_t * a1, ip6_address_t * a2) { int i; for (i = 0; i < ARRAY_LEN (a1->as_u16); i++) { int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) - clib_net_to_host_u16 (a2->as_u16[i])); if (cmp != 0) return cmp; } return 0; } static int mfib_entry_cmp (fib_node_index_t mfib_entry_index1, fib_node_index_t mfib_entry_index2) { mfib_entry_t *mfib_entry1, *mfib_entry2; int cmp = 0; mfib_entry1 = mfib_entry_get(mfib_entry_index1); mfib_entry2 = mfib_entry_get(mfib_entry_index2); switch (mfib_entry1->mfe_prefix.fp_proto) { case FIB_PROTOCOL_IP4: cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4, &mfib_entry2->mfe_prefix.fp_grp_addr.ip4); if (0 == cmp) { cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4, &mfib_entry2->mfe_prefix.fp_src_addr.ip4); } break; case FIB_PROTOCOL_IP6: cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6, &mfib_entry2->mfe_prefix.fp_grp_addr.ip6); if (0 == cmp) { cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6, &mfib_entry2->mfe_prefix.fp_src_addr.ip6); } break; case FIB_PROTOCOL_MPLS: ASSERT(0); cmp = 0; break; } if (0 == cmp) { cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len); } return (cmp); } int mfib_entry_cmp_for_sort (void *i1, void *i2) { fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2; return (mfib_entry_cmp(*mfib_entry_index1, *mfib_entry_index2)); } void mfib_entry_lock (fib_node_index_t mfib_entry_index) { mfib_entry_t *mfib_entry; mfib_entry = mfib_entry_get(mfib_entry_index); fib_node_lock(&mfib_entry->mfe_node); } void mfib_entry_unlock (fib_node_index_t mfib_entry_index) { mfib_entry_t *mfib_entry; mfib_entry = mfib_entry_get(mfib_entry_index); fib_node_unlock(&mfib_entry->mfe_node); } static void mfib_entry_dpo_lock (dpo_id_t *dpo) { } static void mfib_entry_dpo_unlock (dpo_id_t *dpo) { } const static dpo_vft_t mfib_entry_dpo_vft = { .dv_lock = mfib_entry_dpo_lock, .dv_unlock = mfib_entry_dpo_unlock, .dv_format = format_mfib_entry_dpo, .dv_mem_show = mfib_entry_show_memory, }; const static char* const mfib_entry_ip4_nodes[] = { "ip4-mfib-forward-rpf", NULL, }; const static char* const mfib_entry_ip6_nodes[] = { "ip6-mfib-forward-rpf", NULL, }; const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] = { [DPO_PROTO_IP4] = mfib_entry_ip4_nodes, [DPO_PROTO_IP6] = mfib_entry_ip6_nodes, }; void mfib_entry_module_init (void) { fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft); dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes); } void mfib_entry_encode (fib_node_index_t mfib_entry_index, fib_route_path_encode_t **api_rpaths) { mfib_entry_t *mfib_entry; mfib_entry = mfib_entry_get(mfib_entry_index); if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_parent) { fib_path_list_walk(mfib_entry->mfe_parent, fib_path_encode, api_rpaths); } } void mfib_entry_get_prefix (fib_node_index_t mfib_entry_index, mfib_prefix_t *pfx) { mfib_entry_t *mfib_entry; mfib_entry = mfib_entry_get(mfib_entry_index); *pfx = mfib_entry->mfe_prefix; } u32 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index) { mfib_entry_t *mfib_entry; mfib_entry = mfib_entry_get(mfib_entry_index); return (mfib_entry->mfe_fib_index); } void mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index, fib_forward_chain_type_t type, dpo_id_t *dpo) { /* * An IP mFIB entry can only provide a forwarding chain that * is the same IP proto as the prefix. * No use-cases (i know of) for other combinations. */ mfib_entry_t *mfib_entry; dpo_proto_t dp; mfib_entry = mfib_entry_get(mfib_entry_index); dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto); if (type == fib_forw_chain_type_from_dpo_proto(dp)) { dpo_copy(dpo, &mfib_entry->mfe_rep); } else { dpo_copy(dpo, drop_dpo_get(dp)); } } u32 mfib_entry_pool_size (void) { return (pool_elts(mfib_entry_pool)); } static clib_error_t * show_mfib_entry_command (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { fib_node_index_t fei; if (unformat (input, "%d", &fei)) { /* * show one in detail */ if (!pool_is_free_index(mfib_entry_pool, fei)) { vlib_cli_output (vm, "%d@%U", fei, format_mfib_entry, fei, MFIB_ENTRY_FORMAT_DETAIL2); } else { vlib_cli_output (vm, "entry %d invalid", fei); } } else { /* * show all */ vlib_cli_output (vm, "FIB Entries:"); pool_foreach_index(fei, mfib_entry_pool, ({ vlib_cli_output (vm, "%d@%U", fei, format_mfib_entry, fei, MFIB_ENTRY_FORMAT_BRIEF); })); } return (NULL); } /*? * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique * numerical indentifier. ?*/ VLIB_CLI_COMMAND (show_mfib_entry, static) = { .path = "show mfib entry", .function = show_mfib_entry_command, .short_help = "show mfib entry", };