summaryrefslogtreecommitdiffstats
path: root/src/vnet/bier/bier_types.c
blob: 0d27524aeb1fdd36771136867eb4996f77e0caee (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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight
/*
 * 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 <vppinfra/types.h>
#include <vnet/bier/bier_types.h>
#include <vnet/bier/bier_hdr_inlines.h>

/*
 * enum to string conversions
 */
const static char* const bier_hdr_len_id_names[] = BIER_HDR_LEN_IDS;
const static char* const bier_hdr_proto_names[] = BIER_HDR_PROTO_ID_NAMES;

const static u16 bier_hdr_len_num_buckets[] = {
    [BIER_HDR_LEN_INVALID] = 0,
    [BIER_HDR_LEN_64] = 8,
    [BIER_HDR_LEN_128] = 16,
    [BIER_HDR_LEN_256] = 32,
    [BIER_HDR_LEN_512] = 64,
    [BIER_HDR_LEN_1024] = 128,
    [BIER_HDR_LEN_2048] = 256,
    [BIER_HDR_LEN_4096] = 512,
};

const static u16 bier_hdr_len_num_bits[] = {
    [BIER_HDR_LEN_INVALID] = 0,
    [BIER_HDR_LEN_64] = 64,
    [BIER_HDR_LEN_128] = 128,
    [BIER_HDR_LEN_256] = 256,
    [BIER_HDR_LEN_512] = 512,
    [BIER_HDR_LEN_1024] = 1024,
    [BIER_HDR_LEN_2048] = 2048,
    [BIER_HDR_LEN_4096] = 4096,
};

const static u16 bier_hdr_len_prefix_len[] = {
    [BIER_HDR_LEN_INVALID] = 0,
    [BIER_HDR_LEN_64] = 7,
    [BIER_HDR_LEN_128] = 8,
    [BIER_HDR_LEN_256] = 9,
    [BIER_HDR_LEN_512] = 10,
    [BIER_HDR_LEN_1024] = 11,
    [BIER_HDR_LEN_2048] = 12,
    [BIER_HDR_LEN_4096] = 13,
};

uint32_t
bier_hdr_len_id_to_num_buckets (bier_hdr_len_id_t id)
{
    return (bier_hdr_len_num_buckets[id]);
}

uint32_t
bier_hdr_len_id_to_num_bytes (bier_hdr_len_id_t id)
{
    return (bier_hdr_len_id_to_num_buckets(id));
}

uint32_t
bier_hdr_len_id_to_max_bucket (bier_hdr_len_id_t id)
{
    return (bier_hdr_len_id_to_num_buckets(id) - 1);
}

uint32_t
bier_hdr_len_id_to_num_bits (bier_hdr_len_id_t id)
{
    return (bier_hdr_len_num_bits[id]);
}

uint32_t
bier_hdr_len_id_to_max_bit (bier_hdr_len_id_t id)
{
    return (bier_hdr_len_id_to_num_bits(id));
}

uint32_t
bier_hdr_len_id_to_prefix_len (bier_hdr_len_id_t id)
{
    return (bier_hdr_len_prefix_len[id]);
}

u8 *
format_bier_hdr_len_id (u8 *s, va_list *ap)
{
    bier_hdr_len_id_t hli = va_arg(*ap, int); // int promotion of bier_hdr_len_id_t

    return (format(s, "%s", bier_hdr_len_id_names[hli]));
}

u8 *
format_bier_hdr_proto (u8 *s, va_list *ap)
{
    bier_hdr_proto_id_t pi = va_arg(*ap, int);

    return (format(s, "%s", bier_hdr_proto_names[pi]));
}

int
bier_table_id_cmp (const bier_table_id_t *btid1,
                   const bier_table_id_t *btid2)
{
    int res;

    res = (btid1->bti_set - btid2->bti_set);

    if (0 == res)
    {
        res  = (btid1->bti_sub_domain - btid2->bti_sub_domain);
    }
    if (0 == res)
    {
        res = (btid1->bti_ecmp - btid2->bti_ecmp);
    }
    if (0 == res)
    {
        res = (btid1->bti_hdr_len - btid2->bti_hdr_len);
    }
    if (0 == res)
    {
        res = (btid1->bti_type - btid2->bti_type);
    }
    return (res);
}

dpo_proto_t
bier_hdr_proto_to_dpo (bier_hdr_proto_id_t bproto)
{
    switch (bproto)
    {
    case BIER_HDR_PROTO_INVALID:
    case BIER_HDR_PROTO_CTRL:
    case BIER_HDR_PROTO_OAM:
        ASSERT(0);
        break;
    case BIER_HDR_PROTO_MPLS_DOWN_STREAM:
    case BIER_HDR_PROTO_MPLS_UP_STREAM:
        return (DPO_PROTO_MPLS);
    case BIER_HDR_PROTO_ETHERNET:
    case BIER_HDR_PROTO_VXLAN:
        return (DPO_PROTO_ETHERNET);
    case BIER_HDR_PROTO_IPV4:
        return (DPO_PROTO_IP4);
    case BIER_HDR_PROTO_IPV6:
        return (DPO_PROTO_IP4);
    }

    return (DPO_PROTO_NUM);
}

u8 *
format_bier_table_id (u8 *s, va_list *ap)
{
    bier_table_id_t *btid = va_arg(*ap, bier_table_id_t *);

    return (format(s, "sub-domain:%d set:%d ecmp:%d bsl:%U",
                   btid->bti_sub_domain,
                   btid->bti_set,
                   btid->bti_ecmp,
                   format_bier_hdr_len_id, btid->bti_hdr_len));
}

u8 *
format_bier_hdr (u8 *s, va_list *ap)
{
    bier_hdr_t *bh = va_arg(*ap, bier_hdr_t *);
    bier_hdr_t copy = *bh;

    bier_hdr_ntoh(&copy);

    return (format(s, "nibble:%d version:%d hdr-len:%U entropy:%d proto:%U src:%d",
                   bier_hdr_get_1st_nibble(&copy),
                   bier_hdr_get_version(&copy),
                   format_bier_hdr_len_id, bier_hdr_get_len_id(&copy),
                   bier_hdr_get_entropy(&copy),
                   format_bier_hdr_proto, bier_hdr_get_proto_id(&copy),
                   bier_hdr_get_src_id(&copy)));
}