aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_l2xc.py
blob: 2ec4af9288ee348e5c3ce588f5a9bfdf7b020c6c (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
#!/usr/bin/env python

import unittest
import random

from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP

from framework import VppTestCase, VppTestRunner
from util import Host, ppp


class TestL2xc(VppTestCase):
    """ L2XC Test Case """

    @classmethod
    def setUpClass(cls):
        """
        Perform standard class setup (defined by class method setUpClass in
        class VppTestCase) before running the test case, set test case related
        variables and configure VPP.

        :var int hosts_nr: Number of hosts to be created.
        :var int dl_pkts_per_burst: Number of packets in burst for dual-loop
            test.
        :var int sl_pkts_per_burst: Number of packets in burst for single-loop
            test.
        """
        super(TestL2xc, cls).setUpClass()

        # Test variables
        cls.hosts_nr = 10
        cls.dl_pkts_per_burst = 257
        cls.sl_pkts_per_burst = 2

        try:
            # create 4 pg interfaces
            cls.create_pg_interfaces(range(4))

            # packet flows mapping pg0 -> pg1, pg2 -> pg3, etc.
            cls.flows = dict()
            cls.flows[cls.pg0] = [cls.pg1]
            cls.flows[cls.pg1] = [cls.pg0]
            cls.flows[cls.pg2] = [cls.pg3]
            cls.flows[cls.pg3] = [cls.pg2]

            # packet sizes
            cls.pg_if_packet_sizes = [64, 512, 1518, 9018]

            cls.interfaces = list(cls.pg_interfaces)

            # Create bi-directional cross-connects between pg0 and pg1
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg0.sw_if_index, cls.pg1.sw_if_index, enable=1)
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg1.sw_if_index, cls.pg0.sw_if_index, enable=1)

            # Create bi-directional cross-connects between pg2 and pg3
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg2.sw_if_index, cls.pg3.sw_if_index, enable=1)
            cls.vapi.sw_interface_set_l2_xconnect(
                cls.pg3.sw_if_index, cls.pg2.sw_if_index, enable=1)

            # mapping between packet-generator index and lists of test hosts
            cls.hosts_by_pg_idx = dict()

            # Create host MAC and IPv4 lists
            cls.create_host_lists(cls.hosts_nr)

            # setup all interfaces
            for i in cls.interfaces:
                i.admin_up()

        except Exception:
            super(TestL2xc, cls).tearDownClass()
            raise

    def setUp(self):
        super(TestL2xc, self).setUp()
        self.reset_packet_infos()

    def tearDown(self):
        """
        Show various debug prints after each test.
        """
        super(TestL2xc, self).tearDown()
        if not self.vpp_dead:
            self.logger.info(self.vapi.ppcli("show l2patch"))

    @classmethod
    def create_host_lists(cls, count):
        """
        Method to create required number of MAC and IPv4 addresses.
        Create required number of host MAC addresses and distribute them among
        interfaces. Create host IPv4 address for every host MAC address too.

        :param count: Number of hosts to create MAC and IPv4 addresses for.
        """
        for pg_if in cls.pg_interfaces:
            cls.hosts_by_pg_idx[pg_if.sw_if_index] = []
            hosts = cls.hosts_by_pg_idx[pg_if.sw_if_index]
            for j in range(0, count):
                host = Host(
                    "00:00:00:ff:%02x:%02x" % (pg_if.sw_if_index, j),
                    "172.17.1%02x.%u" % (pg_if.sw_if_index, j))
                hosts.append(host)

    def create_stream(self, src_if, packet_sizes, packets_per_burst):
        """
        Create input packet stream for defined interface.

        :param object src_if: Interface to create packet stream for.
        :param list packet_sizes: List of required packet sizes.
        :param int packets_per_burst: Number of packets in burst.
        :return: Stream of packets.
        """
        pkts = []
        for i in range(0, packets_per_burst):
            dst_if = self.flows[src_if][0]
            dst_host = random.choice(self.hosts_by_pg_idx[dst_if.sw_if_index])
            src_host = random.choice(self.hosts_by_pg_idx[src_if.sw_if_index])
            pkt_info = self.create_packet_info(src_if, dst_if)
            payload = self.info_to_payload(pkt_info)
            p = (Ether(dst=dst_host.mac, src=src_host.mac) /
                 IP(src=src_host.ip4, dst=dst_host.ip4) /
                 UDP(sport=1234, dport=1234) /
                 Raw(payload))
            pkt_info.data = p.copy()
            size = random.choice(packet_sizes)
            self.extend_packet(p, size)
            pkts.append(p)
        return pkts

    def verify_capture(self, pg_if, capture):
        """
        Verify captured input packet stream for defined interface.

        :param object pg_if: Interface to verify captured packet stream for.
        :param list capture: Captured packet stream.
        """
        last_info = dict()
        for i in self.interfaces:
            last_info[i.sw_if_index] = None
        dst_sw_if_index = pg_if.sw_if_index
        for packet in capture:
            try:
                ip = packet[IP]
                udp = packet[UDP]
                payload_info = self.payload_to_info(str(packet[Raw]))
                packet_index = payload_info.index
                self.assertEqual(payload_info.dst, dst_sw_if_index)
                self.logger.debug("Got packet on port %s: src=%u (id=%u)" %
                                  (pg_if.name, payload_info.src, packet_index))
                next_info = self.get_next_packet_info_for_interface2(
                    payload_info.src, dst_sw_if_index,
                    last_info[payload_info.src])
                last_info[payload_info.src] = next_info
                self.assertTrue(next_info is not None)
                self.assertEqual(packet_index, next_info.index)
                saved_packet = next_info.data
                # Check standard fields
                self.assertEqual(ip.src, saved_packet[IP].src)
                self.assertEqual(ip.dst, saved_packet[IP].dst)
                self.assertEqual(udp.sport, saved_packet[UDP].sport)
                self.assertEqual(udp.dport, saved_packet[UDP].dport)
            except:
                self.logger.error(ppp("Unexpected or invalid packet:", packet))
                raise
        for i in self.interfaces:
            remaining_packet = self.get_next_packet_info_for_interface2(
                i, dst_sw_if_index, last_info[i.sw_if_index])
            self.assertTrue(remaining_packet is None,
                            "Port %u: Packet expected from source %u didn't"
                            " arrive" % (dst_sw_if_index, i.sw_if_index))

    def run_l2xc_test(self, pkts_per_burst):
        """ L2XC test """

        # Create incoming packet streams for packet-generator interfaces
        for i in self.interfaces:
            pkts = self.create_stream(i, self.pg_if_packet_sizes,
                                      pkts_per_burst)
            i.add_stream(pkts)

        # Enable packet capturing and start packet sending
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        # Verify outgoing packet streams per packet-generator interface
        for i in self.pg_interfaces:
            capture = i.get_capture()
            self.logger.info("Verifying capture on interface %s" % i.name)
            self.verify_capture(i, capture)

    def test_l2xc_sl(self):
        """ L2XC single-loop test

        Test scenario:
            1. config
                2 pairs of 2 interfaces, l2xconnected

            2. sending l2 eth packets between 4 interfaces
                64B, 512B, 1518B, 9018B (ether_size)
                burst of 2 packets per interface
        """

        self.run_l2xc_test(self.sl_pkts_per_burst)

    def test_l2xc_dl(self):
        """ L2XC dual-loop test

        Test scenario:
            1. config
                2 pairs of 2 interfaces, l2xconnected

            2. sending l2 eth packets between 4 interfaces
                64B, 512B, 1518B, 9018B (ether_size)
                burst of 257 packets per interface
        """

        self.run_l2xc_test(self.dl_pkts_per_burst)


if __name__ == '__main__':
    unittest.main(testRunner=VppTestRunner)
pan>free_indices) == 0) { /* Out of free overflow buckets. Resize. */ u32 j, *p; i = vec_len (ob->search_buckets); vec_resize_aligned (ob->search_buckets, sizeof (b[0]) / sizeof (u32x4) + n_key_u32s, CLIB_CACHE_LINE_BYTES); vec_add2 (ob->free_indices, p, 4); for (j = 0; j < 4; j++) p[j] = 4 * i + j; } i = vec_pop (ob->free_indices); i_set = i & 3; b = ((vhash_overflow_search_bucket_t *) vec_elt_at_index (ob->search_buckets, i / 4)); /* Insert result. */ set_overflow_result (b, i_set, new_result, key_hash); /* Insert key. */ for (i = 0; i < n_key_u32s; i++) b->key[i].as_u32[i_set] = vhash_get_key_word (h, i, vi); ob->n_overflow++; h->n_elts++; return /* old result was invalid */ 0; } u32 vhash_unset_overflow (vhash_t * h, u32 key_hash, u32 vi, u32 n_key_u32s) { vhash_overflow_buckets_t *ob = vhash_get_overflow_buckets (h, key_hash); vhash_overflow_search_bucket_t *b; u32 i_set, i, old_result; foreach_vhash_overflow_bucket (b, ob, n_key_u32s) { u32x4 r; r = b->result.as_u32x4; for (i = 0; i < n_key_u32s; i++) r &= vhash_bucket_compare (h, &b->key[0], i, vi); old_result = vhash_merge_results (r); if (old_result) { i_set = vhash_non_empty_result_index (r); /* Invalidate result and invert key hash so that this will never match since all keys in this overflow bucket have matching key hashs. */ set_overflow_result (b, i_set, 0, ~key_hash); free_overflow_bucket (ob, b, i_set); ASSERT (ob->n_overflow > 0); ob->n_overflow--; h->n_elts--; return old_result; } } /* Could not find key. */ return 0; } void vhash_unset_refill_from_overflow (vhash_t * h, vhash_search_bucket_t * sb, u32 key_hash, u32 n_key_u32s) { vhash_overflow_buckets_t *obs = vhash_get_overflow_buckets (h, key_hash); vhash_overflow_search_bucket_t *ob; u32 i, j, i_refill, bucket_mask = h->bucket_mask.as_u32[0]; /* Find overflow element with matching key hash. */ foreach_vhash_overflow_bucket (ob, obs, n_key_u32s) { for (i = 0; i < 4; i++) { if (!ob->result.as_u32[i]) continue; if ((ob->key_hash.as_u32[i] & bucket_mask) != (key_hash & bucket_mask)) continue; i_refill = vhash_empty_result_index (sb->result.as_u32x4); sb->result.as_u32[i_refill] = ob->result.as_u32[i]; for (j = 0; j < n_key_u32s; j++) sb->key[j].as_u32[i_refill] = ob->key[j].as_u32[i]; set_overflow_result (ob, i, 0, ~key_hash); free_overflow_bucket (obs, ob, i); return; } } } void vhash_init (vhash_t * h, u32 log2_n_keys, u32 n_key_u32, u32 * hash_seeds) { uword i, j, m; vhash_search_bucket_t *b; memset (h, 0, sizeof (h[0])); /* Must have at least 4 keys (e.g. one search bucket). */ log2_n_keys = clib_max (log2_n_keys, 2); h->log2_n_keys = log2_n_keys; h->n_key_u32 = n_key_u32; m = pow2_mask (h->log2_n_keys) & ~3; for (i = 0; i < VECTOR_WORD_TYPE_LEN (u32); i++) h->bucket_mask.as_u32[i] = m; /* Allocate and zero search buckets. */ i = (sizeof (b[0]) / sizeof (u32x4) + n_key_u32) << (log2_n_keys - 2); vec_validate_aligned (h->search_buckets, i - 1, CLIB_CACHE_LINE_BYTES); for (i = 0; i < ARRAY_LEN (h->find_first_zero_table); i++) h->find_first_zero_table[i] = min_log2 (first_set (~i)); for (i = 0; i < ARRAY_LEN (h->hash_seeds); i++) for (j = 0; j < VECTOR_WORD_TYPE_LEN (u32); j++) h->hash_seeds[i].as_u32[j] = hash_seeds[i]; } static_always_inline u32 vhash_main_key_gather (void *_vm, u32 vi, u32 wi, u32 n_key_u32) { vhash_main_t *vm = _vm; return vec_elt (vm->keys, vi * n_key_u32 + wi); } static_always_inline u32x4 vhash_main_4key_gather (void *_vm, u32 vi, u32 wi, u32 n_key_u32s) { vhash_main_t *vm = _vm; u32x4_union_t x; ASSERT (n_key_u32s == vm->n_key_u32); ASSERT (wi < n_key_u32s); x.as_u32[0] = vec_elt (vm->keys, (vi + 0) * n_key_u32s + wi); x.as_u32[1] = vec_elt (vm->keys, (vi + 1) * n_key_u32s + wi); x.as_u32[2] = vec_elt (vm->keys, (vi + 2) * n_key_u32s + wi); x.as_u32[3] = vec_elt (vm->keys, (vi + 3) * n_key_u32s + wi); return x.as_u32x4; } static_always_inline u32 vhash_main_set_result (void *_vm, u32 vi, u32 old_result, u32 n_key_u32) { vhash_main_t *vm = _vm; u32 *p = vec_elt_at_index (vm->results, vi); u32 new_result = p[0]; p[0] = old_result; return new_result; } static_always_inline u32 vhash_main_get_result (void *_vm, u32 vi, u32 old_result, u32 n_key_u32) { vhash_main_t *vm = _vm; vec_elt (vm->results, vi) = old_result; return old_result; } static_always_inline u32x4 vhash_main_get_4result (void *_vm, u32 vi, u32x4 old_result, u32 n_key_u32) { vhash_main_t *vm = _vm; u32x4 *p = (u32x4 *) vec_elt_at_index (vm->results, vi); p[0] = old_result; return old_result; } #define _(N_KEY_U32) \ static_always_inline u32 \ vhash_main_key_gather_##N_KEY_U32 (void * _vm, u32 vi, u32 i) \ { return vhash_main_key_gather (_vm, vi, i, N_KEY_U32); } \ \ static_always_inline u32x4 \ vhash_main_4key_gather_##N_KEY_U32 (void * _vm, u32 vi, u32 i) \ { return vhash_main_4key_gather (_vm, vi, i, N_KEY_U32); } \ \ clib_pipeline_stage_static \ (vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_gather_4key_stage \ (vm->vhash, \ /* vector_index */ i, \ vhash_main_4key_gather_##N_KEY_U32, \ vm, \ N_KEY_U32); \ }) \ \ clib_pipeline_stage_no_inline \ (vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_gather_key_stage \ (vm->vhash, \ /* vector_index */ vm->n_vectors_div_4, \ /* n_vectors */ vm->n_vectors_mod_4, \ vhash_main_key_gather_##N_KEY_U32, \ vm, \ N_KEY_U32); \ }) \ \ clib_pipeline_stage \ (vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_finalize_stage (vm->vhash, i, N_KEY_U32); \ }) \ \ clib_pipeline_stage_no_inline \ (vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_finalize_stage (vm->vhash, vm->n_vectors_div_4, N_KEY_U32); \ }) \ \ clib_pipeline_stage_static \ (vhash_main_get_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_get_4_stage (vm->vhash, \ /* vector_index */ i, \ vhash_main_get_4result, \ vm, N_KEY_U32); \ }) \ \ clib_pipeline_stage_no_inline \ (vhash_main_get_mod_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_get_stage (vm->vhash, \ /* vector_index */ vm->n_vectors_div_4, \ /* n_vectors */ vm->n_vectors_mod_4, \ vhash_main_get_result, \ vm, N_KEY_U32); \ }) \ \ clib_pipeline_stage_static \ (vhash_main_set_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_set_stage (vm->vhash, \ /* vector_index */ i, \ /* n_vectors */ VECTOR_WORD_TYPE_LEN (u32), \ vhash_main_set_result, \ vm, N_KEY_U32); \ }) \ \ clib_pipeline_stage_no_inline \ (vhash_main_set_mod_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_set_stage (vm->vhash, \ /* vector_index */ vm->n_vectors_div_4, \ /* n_vectors */ vm->n_vectors_mod_4, \ vhash_main_set_result, \ vm, N_KEY_U32); \ }) \ \ clib_pipeline_stage_static \ (vhash_main_unset_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_unset_stage (vm->vhash, \ /* vector_index */ i, \ /* n_vectors */ VECTOR_WORD_TYPE_LEN (u32), \ vhash_main_get_result, \ vm, N_KEY_U32); \ }) \ \ clib_pipeline_stage_no_inline \ (vhash_main_unset_mod_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_unset_stage (vm->vhash, \ /* vector_index */ vm->n_vectors_div_4, \ /* n_vectors */ vm->n_vectors_mod_4, \ vhash_main_get_result, \ vm, N_KEY_U32); \ }) _(1); _(2); _(3); _(4); _(5); _(6); #undef _ #define _(N_KEY_U32) \ clib_pipeline_stage \ (vhash_main_hash_mix_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_mix_stage (vm->vhash, i, N_KEY_U32); \ }) \ \ clib_pipeline_stage_no_inline \ (vhash_main_hash_mix_mod_stage_##N_KEY_U32, \ vhash_main_t *, vm, i, \ { \ vhash_mix_stage (vm->vhash, vm->n_vectors_div_4, N_KEY_U32); \ }) _(4); _(5); _(6); #undef _ typedef enum { GET, SET, UNSET, } vhash_main_op_t; static void vhash_main_op (vhash_main_t * vm, vhash_main_op_t op) { u32 n_keys = vec_len (vm->results); vm->n_key_u32 = vm->vhash->n_key_u32; vhash_validate_sizes (vm->vhash, vm->n_key_u32, n_keys); vm->n_vectors_div_4 = n_keys / 4; vm->n_vectors_mod_4 = n_keys % 4; if (vm->n_vectors_div_4 > 0) { switch (vm->n_key_u32) { default: ASSERT (0); break; #define _(N_KEY_U32) \ case N_KEY_U32: \ if (op == GET) \ clib_pipeline_run_3_stage \ (vm->n_vectors_div_4, \ vm, \ vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_get_stage_##N_KEY_U32); \ else if (op == SET) \ clib_pipeline_run_3_stage \ (vm->n_vectors_div_4, \ vm, \ vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_set_stage_##N_KEY_U32); \ else \ clib_pipeline_run_3_stage \ (vm->n_vectors_div_4, \ vm, \ vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_unset_stage_##N_KEY_U32); \ break; _(1); _(2); _(3); #undef _ #define _(N_KEY_U32) \ case N_KEY_U32: \ if (op == GET) \ clib_pipeline_run_4_stage \ (vm->n_vectors_div_4, \ vm, \ vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_hash_mix_stage_##N_KEY_U32, \ vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_get_stage_##N_KEY_U32); \ else if (op == SET) \ clib_pipeline_run_4_stage \ (vm->n_vectors_div_4, \ vm, \ vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_hash_mix_stage_##N_KEY_U32, \ vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_set_stage_##N_KEY_U32); \ else \ clib_pipeline_run_4_stage \ (vm->n_vectors_div_4, \ vm, \ vhash_main_gather_keys_stage_##N_KEY_U32, \ vhash_main_hash_mix_stage_##N_KEY_U32, \ vhash_main_hash_finalize_stage_##N_KEY_U32, \ vhash_main_unset_stage_##N_KEY_U32); \ break; _(4); _(5); _(6); #undef _ } } if (vm->n_vectors_mod_4 > 0) { switch (vm->n_key_u32) { default: ASSERT (0); break; #define _(N_KEY_U32) \ case N_KEY_U32: \ if (op == GET) \ clib_pipeline_run_3_stage \ (1, \ vm, \ vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_get_mod_stage_##N_KEY_U32); \ else if (op == SET) \ clib_pipeline_run_3_stage \ (1, \ vm, \ vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_set_mod_stage_##N_KEY_U32); \ else \ clib_pipeline_run_3_stage \ (1, \ vm, \ vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_unset_mod_stage_##N_KEY_U32); \ break; _(1); _(2); _(3); #undef _ #define _(N_KEY_U32) \ case N_KEY_U32: \ if (op == GET) \ clib_pipeline_run_4_stage \ (1, \ vm, \ vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_hash_mix_mod_stage_##N_KEY_U32, \ vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_get_mod_stage_##N_KEY_U32); \ else if (op == SET) \ clib_pipeline_run_4_stage \ (1, \ vm, \ vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_hash_mix_mod_stage_##N_KEY_U32, \ vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_set_mod_stage_##N_KEY_U32); \ else \ clib_pipeline_run_4_stage \ (1, \ vm, \ vhash_main_gather_keys_mod_stage_##N_KEY_U32, \ vhash_main_hash_mix_mod_stage_##N_KEY_U32, \ vhash_main_hash_finalize_mod_stage_##N_KEY_U32, \ vhash_main_unset_mod_stage_##N_KEY_U32); \ break; _(4); _(5); _(6); #undef _ } } } void vhash_main_get (vhash_main_t * vm) { vhash_main_op (vm, GET); } void vhash_main_set (vhash_main_t * vm) { vhash_main_op (vm, SET); } void vhash_main_unset (vhash_main_t * vm) { vhash_main_op (vm, UNSET); } u32 vhash_resize_incremental (vhash_resize_t * vr, u32 vector_index, u32 n_keys_this_call) { vhash_t *old = vr->old; vhash_main_t *vm = &vr->new; vhash_t *new = vm->vhash; uword i, j, n_key_u32; n_key_u32 = old->n_key_u32; if (vector_index == 0) { u32 hash_seeds[3]; hash_seeds[0] = old->hash_seeds[0].as_u32[0]; hash_seeds[1] = old->hash_seeds[1].as_u32[0]; hash_seeds[2] = old->hash_seeds[2].as_u32[0]; vhash_init (new, old->log2_n_keys + 1, n_key_u32, hash_seeds); } vec_reset_length (vm->keys); vec_reset_length (vm->results); if (0 == (vector_index >> old->log2_n_keys)) { for (i = vector_index; 0 == (i >> (old->log2_n_keys - 2)); i++) { vhash_search_bucket_t *b = vhash_get_search_bucket_with_index (old, 4 * i, n_key_u32); u32 r, *k; #define _(I) \ if ((r = b->result.as_u32[I]) != 0) \ { \ vec_add1 (vm->results, r - 1); \ vec_add2 (vm->keys, k, n_key_u32); \ for (j = 0; j < n_key_u32; j++) \ k[j] = b->key[j].as_u32[I]; \ } _(0); _(1); _(2); _(3); #undef _ if (vec_len (vm->results) >= n_keys_this_call) { vhash_main_op (vm, SET); return i; } } } /* Add overflow buckets. */ { vhash_overflow_buckets_t *ob; vhash_overflow_search_bucket_t *b; for (ob = old->overflow_buckets; ob < old->overflow_buckets + ARRAY_LEN (old->overflow_buckets); ob++) { foreach_vhash_overflow_bucket (b, ob, old->n_key_u32) { u32 r, *k; #define _(I) \ if ((r = b->result.as_u32[I]) != 0) \ { \ vec_add1 (vm->results, r - 1); \ vec_add2 (vm->keys, k, n_key_u32); \ for (j = 0; j < n_key_u32; j++) \ k[j] = b->key[j].as_u32[I]; \ } _(0); _(1); _(2); _(3); #undef _ } } } vhash_main_op (vm, SET); /* Let caller know we are done. */ return ~0; } void vhash_resize (vhash_t * old, u32 log2_n_keys) { static vhash_resize_t vr; vhash_t new; u32 i = 0; vr.old = old; vr.new.vhash = &new; while (1) { i = vhash_resize_incremental (&vr, i, 1024); if (i == ~0) break; } vhash_free (old); *old = new; } #endif /* CLIB_HAVE_VEC128 */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */