summaryrefslogtreecommitdiffstats
path: root/src/vnet/bier/bier_disp_lookup_node.c
blob: 255f0453bf3a9b845bf95e370ed9fe5f1eabd3ad (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
/*
 * 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/bier/bier_disp_table.h>
#include <vnet/bier/bier_hdr_inlines.h>

/**
 * @brief A struct to hold tracing information for the MPLS label imposition
 * node.
 */
typedef struct bier_disp_lookup_trace_t_
{
    /**
     * BIER source BP used in the lookup - host order
     */
    bier_bp_t bp;
    /**
     * BIER disp table
     */
    index_t bdti;
} bier_disp_lookup_trace_t;

/**
 * Next nodes from BIER disposition lookup
 */
typedef enum bier_disp_lookup_next_t_
{
    BIER_DISP_LOOKUP_NEXT_DROP,
    BIER_DISP_LOOKUP_NEXT_DISPATCH,
} bier_disp_lookup_next_t;
#define BIER_DISP_LOOKUP_N_NEXT (BIER_DISP_LOOKUP_NEXT_DISPATCH+1)

always_inline uword
bier_disp_lookup_inline (vlib_main_t * vm,
                         vlib_node_runtime_t * node,
                         vlib_frame_t * from_frame)
{
    u32 n_left_from, next_index, * from, * to_next;

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

    next_index = node->cached_next_index;

    while (n_left_from > 0)
    {
        u32 n_left_to_next;

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

        while (n_left_from > 0 && n_left_to_next > 0)
        {
            const bier_hdr_t *hdr0;
            bier_hdr_src_id_t src0;
            vlib_buffer_t * b0;
            u32 bdei0, bdti0;
            u32 next0, bi0;

            bi0 = from[0];
            to_next[0] = bi0;
            from += 1;
            to_next += 1;
            n_left_from -= 1;
            n_left_to_next -= 1;

            b0 = vlib_get_buffer (vm, bi0);

            bdti0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
            hdr0 = vlib_buffer_get_current(b0);

            /*
             * lookup - source is in network order.
             */
            src0 = bier_hdr_get_src_id(hdr0);
            next0 = BIER_DISP_LOOKUP_NEXT_DISPATCH;

            bdei0 = bier_disp_table_lookup(bdti0, src0);

            if (PREDICT_FALSE(INDEX_INVALID == bdei0))
            {
                /*
                 * if a specific match misses, try the default
                 */
                bdei0 = bier_disp_table_lookup(bdti0, 0);

                if (PREDICT_FALSE(INDEX_INVALID == bdei0))
                {
                    next0 = BIER_DISP_LOOKUP_NEXT_DROP;
                }
            }

            vnet_buffer(b0)->ip.adj_index[VLIB_TX] = bdei0;

            if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
            {
                bier_disp_lookup_trace_t *tr =
                    vlib_add_trace (vm, node, b0, sizeof (*tr));
                tr->bp = clib_net_to_host_u16(bier_hdr_get_src_id(hdr0));
                tr->bdti = bdti0;
            }

            vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
                                            n_left_to_next, bi0, next0);
        }
        vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
    return from_frame->n_vectors;
}

static u8 *
format_bier_disp_lookup_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 *);
    bier_disp_lookup_trace_t * t;

    t = va_arg (*args, bier_disp_lookup_trace_t *);
    s = format (s, "tbl:%d src:%d", t->bdti, t->bp);

    return (s);
}

VLIB_NODE_FN (bier_disp_lookup_node) (vlib_main_t * vm,
                  vlib_node_runtime_t * node,
                  vlib_frame_t * frame)
{
    return (bier_disp_lookup_inline(vm, node, frame));
}

VLIB_REGISTER_NODE (bier_disp_lookup_node) = {
    .name = "bier-disp-lookup",
    .vector_size = sizeof (u32),

    .format_trace = format_bier_disp_lookup_trace,
    .n_next_nodes = BIER_DISP_LOOKUP_N_NEXT,
    .next_nodes = {
        [BIER_DISP_LOOKUP_NEXT_DROP] = "bier-drop",
        [BIER_DISP_LOOKUP_NEXT_DISPATCH] = "bier-disp-dispatch",
    }
};
pan class="k">def test_adding_new_message_object(self): """ Add new message object """ p = json.loads(TestPAPIMessageParsing.show_version_msg) msglist = VPP(testmode=json) msgdef = msglist.add_message(p[0], p[1:]) # Verify that message can be retrieved self.assertTrue(msglist['show_version']) self.assertFalse(msglist['foobar']) # Test duplicate self.assertRaises(ValueError, msglist.add_message, p[0], p[1:]) # Look at return tuple self.assertTrue(msglist.ret_tup('show_version')) def test_adding_new_message_object_with_array(self): """ New message with array """ p = json.loads(TestPAPIMessageParsing.ip_address_details_msg) msglist = VPP(testmode=True) msglist.add_message(p[0], p[1:]) self.assertTrue(msglist['ip_address_details']) def test_message_to_bytes(self): """ Message to byte encoding """ msglist = VPP(testmode=True) p = json.loads(TestPAPIMessageParsing.show_version_msg) msgdef = msglist.add_message(p[0], p[1:]) # Give me a byte string for given message and given arguments b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123}) self.assertEqual(10, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(rv._0, 50) self.assertEqual(rv.context, 123) p = json.loads(TestPAPIMessageParsing.ip_address_details_msg) msgdef = msglist.add_message(p[0], p[1:]) # Give me a byte string for given message and given arguments b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123, 'ip': b'\xf0\xf1\xf2', 'prefix_length': 12}) self.assertEqual(27, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(rv.context, 123) self.assertEqual(rv.ip, b'\xf0\xf1\xf2\x00\x00\x00' + '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') self.assertEqual(rv.prefix_length, 12) p = json.loads(TestPAPIMessageParsing.cli_inband_msg) msgdef = msglist.add_message(p[0], p[1:]) # Give me a byte string for given message and given arguments b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123, 'length': 20, 'cmd': 'show version verbose'}) self.assertEqual(34, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(rv._0, 50) self.assertEqual(rv.context, 123) self.assertEqual(rv.cmd.decode('ascii'), 'show version verbose') variable_array_16_msg = '''["variable_array_16", ["u32", "length"], ["u16", "list", 0, "length"] ]''' p = json.loads(variable_array_16_msg) msgdef = msglist.add_message(p[0], p[1:]) # Give me a byte string for given message and given arguments b = msglist.encode(msgdef, {'list': [1, 2], 'length': 2}) self.assertEqual(8, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(2, rv.length) self.assertEqual([1, 2], rv.list) def test_add_new_types(self): """ Add new types """ counter_type = '''["ip4_fib_counter", ["u32", "address"], ["u8", "address_length"], ["u64", "packets"], ["u64", "bytes"], {"crc" : "0xb2739495"} ]''' with_type_msg = '''["with_type_msg", ["u32", "length"], ["u16", "list", 0, "length"], ["vl_api_ip4_fib_counter_t", "counter"] ]''' # Add new type msglist = VPP(testmode=True) p = json.loads(counter_type) msglist.add_type(p[0], p[1:]) p = json.loads(with_type_msg) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2], 'counter': {'address': 4, 'address_length': 12, 'packets': 1235, 'bytes': 5678}}) self.assertEqual(29, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(2, rv.length) self.assertEqual(5678, rv.counter.bytes) def test_add_two_new_types(self): """ Add new types 2 """ mock_r1 = '''["mock_r1", ["u32", "a1"], {"crc" : "0xb2739495"} ]''' mock_r2 = '''["mock_r2", ["u32", "a1"], {"crc" : "0xb2739495"} ]''' mock_msg = '''["mock_msg", ["u32", "context"], ["i32", "retval"], ["vl_api_mock_r1_t", "r1"], ["vl_api_mock_r2_t", "r2"], {"crc" : "0xb2739495"} ]''' # Add new type msglist = VPP(testmode=True) p = json.loads(mock_r1) msglist.add_type(p[0], p[1:]) p = json.loads(mock_r2) msglist.add_type(p[0], p[1:]) p = json.loads(mock_msg) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'context': 2, 'retval': 0, 'r1': {'a1': 4}, 'r2': {'a1': 12}}) self.assertEqual(16, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(4, rv.r1.a1) def test_nested_array_type(self): """ Nested array type """ bier_type = '''["bier_table_id", ["u8", "bt_set"], ["u8", "bt_sub_domain"], ["u8", "bt_hdr_len_id"], {"crc" : "0xb2739495"} ]''' fib_path3 = '''["fib_path3", ["u32", "sw_if_index"], ["u8", "n_labels"], ["u32", "label_stack", 0, "n_labels"], {"crc" : "0xb2739495"} ]''' bier_route_details = '''["bier_route_details", ["u32", "client_index"], ["vl_api_bier_table_id_t", "br_tbl_id"], ["u32", "br_n_paths"], ["vl_api_fib_path3_t", "br_paths", 0, "br_n_paths"], {"crc" : "0xb2739495"} ]''' # Add new type msglist = VPP(testmode=True) p = json.loads(bier_type) msglist.add_type(p[0], p[1:]) p = json.loads(fib_path3) msglist.add_type(p[0], p[1:]) p = json.loads(bier_route_details) msgdef = msglist.add_message(p[0], p[1:]) bt_tbl_id = {'bt_set': 1, 'bt_sub_domain': 2, 'bt_hdr_len_id': 3} fib_path = {'sw_if_index': 1, 'n_labels': 2, 'label_stack': [123, 456]} b = msglist.encode(msgdef, {'client_index': 2, 'br_tbl_id': bt_tbl_id, 'br_n_paths': 2, 'br_paths': [fib_path, fib_path]}) self.assertEqual(37, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual([123, 456], rv.br_paths[1].label_stack) self.assertEqual(bt_tbl_id['bt_set'], rv.br_tbl_id.bt_set) def test_add_new_compound_type_with_array(self): """ New compound type with array """ counter_type = '''["ip4_fib_counter", ["u32", "address"], ["u8", "address_length"], ["u64", "packets"], ["u64", "bytes"], {"crc" : "0xb2739495"} ]''' with_type_msg = '''["with_type_msg", ["u32", "length"], ["u16", "list", 0, "length"], ["vl_api_ip4_fib_counter_t", "counter", 2] ]''' # Add new type msglist = VPP(testmode=True) p = json.loads(counter_type) msglist.add_type(p[0], p[1:]) p = json.loads(with_type_msg) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2], 'counter': [{'address': 4, 'address_length': 12, 'packets': 1235, 'bytes': 5678}, {'address': 111, 'address_length': 222, 'packets': 333, 'bytes': 444}]}) self.assertEqual(50, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual([1, 2], rv.list) self.assertEqual(1235, rv.counter[0].packets) with_type_variable_msg = '''["with_type_variable_msg", ["u32", "length"], ["vl_api_ip4_fib_counter_t", "counter", 0, "length"] ]''' p = json.loads(with_type_variable_msg) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 2, 'counter': [{'address': 4, 'address_length': 12, 'packets': 1235, 'bytes': 5678}, {'address': 111, 'address_length': 222, 'packets': 333, 'bytes': 444}]}) self.assertEqual(46, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(2, rv.length) self.assertEqual(1235, rv.counter[0].packets) self.assertEqual(333, rv.counter[1].packets) def test_simple_array(self): """ Simple array """ msglist = VPP(testmode=True) simple_byte_array = '''["simple_byte_array", ["u32", "length"], ["u8", "namecommand", 64] ]''' p = json.loads(simple_byte_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 2, 'namecommand': 'foobar'}) self.assertEqual(68, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(2, rv.length) simple_array = '''["simple_array", ["u32", "length"], ["u32", "list", 2] ]''' p = json.loads(simple_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2]}) self.assertEqual(12, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(2, rv.length) self.assertEqual([1, 2], rv.list) simple_variable_array = '''["simple_variable_array", ["u32", "length"], ["u32", "list", 0, "length"] ]''' p = json.loads(simple_variable_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2]}) self.assertEqual(12, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(2, rv.length) self.assertEqual([1, 2], rv.list) simple_variable_byte_array = '''["simple_variable_byte_array", ["u32", "length"], ["u8", "list", 0, "length"] ]''' p = json.loads(simple_variable_byte_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 6, 'list': 'foobar'}) self.assertEqual(10, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(6, rv.length) self.assertEqual('foobar', rv.list) def test_old_vla_array(self): """ Old style VLA array """ msglist = VPP(testmode=True) # VLA vla_byte_array = '''["vla_byte_array", ["u32", "foobar"], ["u32", "list", 2], ["u32", "propercount"], ["u8", "propermask", 0, "propercount"], ["u8", "oldmask", 0], {"crc" : "0xb2739495"} ]''' p = json.loads(vla_byte_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'list': [123, 456], 'oldmask': b'foobar', 'propercount': 2, 'propermask': [8, 9]}) self.assertEqual(24, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(b'foobar', rv.oldmask) def test_old_vla_array_not_last_member(self): """ Old VLA array arbitrary placement """ msglist = VPP(testmode=True) # VLA vla_byte_array = '''["vla_byte_array", ["u8", "oldmask", 0], ["u32", "foobar"], {"crc" : "0xb2739495"} ]''' p = json.loads(vla_byte_array) self.assertRaises(ValueError, msglist.add_message, p[0], p[1:]) def test_old_vla_array_u32(self): """ Old VLA u32 """ msglist = VPP(testmode=True) # VLA vla_byte_array = '''["vla_byte_array", ["u32", "foobar"], ["u32", "oldmask", 0], {"crc" : "0xb2739495"} ]''' p = json.loads(vla_byte_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'foobar': 123, 'oldmask': [123, 456, 789]}) self.assertEqual(16, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual([123, 456, 789], rv.oldmask) def test_old_vla_array_compound(self): """ Old VLA compound type """ msglist = VPP(testmode=True) # VLA counter_type = '''["ip4_fib_counter", ["u32", "address"], ["u8", "address_length"], ["u64", "packets"], ["u64", "bytes"], {"crc" : "0xb2739495"} ]''' vla_byte_array = '''["vla_byte_array", ["vl_api_ip4_fib_counter_t", "counter", 0], {"crc" : "0xb2739495"} ]''' p = json.loads(counter_type) msglist.add_type(p[0], p[1:]) p = json.loads(vla_byte_array) with self.assertRaises(NotImplementedError): msgdef = msglist.add_message(p[0], p[1:]) def test_array_count_not_previous(self): """ VLA with aribtrary length field placement """ msglist = VPP(testmode=True) # VLA vla_byte_array = '''["vla_byte_array", ["u32", "count"], ["u32", "filler"], ["u32", "lst", 0, "count"], {"crc" : "0xb2739495"} ]''' p = json.loads(vla_byte_array) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'count': 3, 'lst': [1, 2, 3], 'filler': 1}) rv = msglist.decode(msgdef, b) self.assertEqual(rv.lst, [1, 2, 3]) def test_argument_name(self): """ Argument name """ msglist = VPP(testmode=True) simple_name = '''["simple_name", ["u32", "length"], ["u8", "name"] ]''' p = json.loads(simple_name) msgdef = msglist.add_message(p[0], p[1:]) b = msglist.encode(msgdef, {'length': 6, 'name': 1}) self.assertEqual(5, len(b)) rv = msglist.decode(msgdef, b) self.assertEqual(6, rv.length) self.assertEqual(1, rv.name)