summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/sparse_vec.h
blob: 54a92ce7a84e8995577e54b2c7eb3ede25720274 (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
/*
 * Copyright (c) 2015 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.
 */
/*
  Copyright (c) 2005 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef included_sparse_vec_h
#define included_sparse_vec_h

#include <vppinfra/vec.h>
#include <vppinfra/bitops.h>

/* Sparsely indexed vectors.  Basic idea taken from Hacker's delight.
   Eliot added ranges. */
typedef struct
{
  /* Bitmap one for each sparse index. */
  uword *is_member_bitmap;

  /* member_counts[i] = total number of members with j < i. */
  u16 *member_counts;

#define SPARSE_VEC_IS_RANGE (1 << 0)
#define SPARSE_VEC_IS_VALID_RANGE (1 << 1)
  u8 *range_flags;
} sparse_vec_header_t;

always_inline sparse_vec_header_t *
sparse_vec_header (void *v)
{
  return vec_header (v, sizeof (sparse_vec_header_t));
}

/* Index 0 is always used to mark indices that are not valid in
   sparse vector.  For example, you look up V[0x1234] and 0x1234 is not
   known you'll get 0 back as an index. */
#define SPARSE_VEC_INVALID_INDEX (0)

always_inline void *
sparse_vec_new (uword elt_bytes, uword sparse_index_bits)
{
  void *v;
  sparse_vec_header_t *h;
  word n;

  ASSERT (sparse_index_bits <= 16);

  v = _vec_resize ((void *) 0,
		   /* length increment */ 8,
		   /* data bytes */ 8 * elt_bytes,
		   /* header bytes */ sizeof (h[0]),
		   /* data align */ 0);

  /* Make space for invalid entry (entry 0). */
  _vec_len (v) = 1;

  h = sparse_vec_header (v);

  n = sparse_index_bits - min_log2 (BITS (uword));
  if (n < 0)
    n = 0;
  n = 1ULL << n;
  vec_resize (h->is_member_bitmap, n);
  vec_resize (h->member_counts, n);

  return v;
}

always_inline uword
sparse_vec_index_internal (void *v,
			   uword sparse_index,
			   uword maybe_range, u32 * insert)
{
  sparse_vec_header_t *h;
  uword i, b, d, w;
  u8 is_member;

  h = sparse_vec_header (v);
  i = sparse_index / BITS (h->is_member_bitmap[0]);
  b = sparse_index % BITS (h->is_member_bitmap[0]);

  ASSERT (i < vec_len (h->is_member_bitmap));
  ASSERT (i < vec_len (h->member_counts));

  w = h->is_member_bitmap[i];

  /* count_trailing_zeros(0) == 0, take care of that case */
  if (PREDICT_FALSE (maybe_range == 0 && insert == 0 && w == 0))
    return 0;

  if (PREDICT_TRUE (maybe_range == 0 && insert == 0 &&
		    count_trailing_zeros (w) == b))
    return h->member_counts[i] + 1;

  d = h->member_counts[i] + count_set_bits (w & ((1ULL << b) - 1));
  is_member = (w & (1ULL << b)) != 0;

  if (maybe_range)
    {
      u8 r = h->range_flags[d];
      u8 is_range, is_valid_range;

      is_range = maybe_range & (r & SPARSE_VEC_IS_RANGE);
      is_valid_range = (r & SPARSE_VEC_IS_VALID_RANGE) != 0;

      is_member = is_range ? is_valid_range : is_member;
    }

  if (insert)
    {
      *insert = !is_member;
      if (!is_member)
	{
	  uword j;
	  w |= 1ULL << b;
	  h->is_member_bitmap[i] = w;
	  for (j = i + 1; j < vec_len (h->member_counts); j++)
	    h->member_counts[j] += 1;
	}

      return 1 + d;
    }

  d = is_member ? d : 0;

  return is_member + d;
}

always_inline uword
sparse_vec_index (void *v, uword sparse_index)
{
  return sparse_vec_index_internal (v, sparse_index,
				    /* maybe range */ 0,
				    /* insert? */ 0);
}

always_inline void
sparse_vec_index2 (void *v,
		   u32 si0, u32 si1, u32 * i0_return, u32 * i1_return)
{
  sparse_vec_header_t *h;
  uword b0, b1, w0, w1, v0, v1;
  u32 i0, i1, d0, d1;
  u8 is_member0, is_member1;

  h = sparse_vec_header (v);

  i0 = si0 / BITS (h->is_member_bitmap[0]);
  i1 = si1 / BITS (h->is_member_bitmap[0]);

  b0 = si0 % BITS (h->is_member_bitmap[0]);
  b1 = si1 % BITS (h->is_member_bitmap[0]);

  ASSERT (i0 < vec_len (h->is_member_bitmap));
  ASSERT (i1 < vec_len (h->is_member_bitmap));

  ASSERT (i0 < vec_len (h->member_counts));
  ASSERT (i1 < vec_len (h->member_counts));

  w0 = h->is_member_bitmap[i0];
  w1 = h->is_member_bitmap[i1];

  if (PREDICT_TRUE ((count_trailing_zeros (w0) == b0) +
		    (count_trailing_zeros (w1) == b1) == 2))
    {
      *i0_return = h->member_counts[i0] + 1;
      *i1_return = h->member_counts[i1] + 1;
      return;
    }

  v0 = w0 & ((1ULL << b0) - 1);
  v1 = w1 & ((1ULL << b1) - 1);

  /* Speculate that masks will have zero or one bits set. */
  d0 = h->member_counts[i0] + (v0 != 0);
  d1 = h->member_counts[i1] + (v1 != 0);

  /* Validate speculation. */
  if (PREDICT_FALSE (!is_pow2 (v0) || !is_pow2 (v1)))
    {
      d0 += count_set_bits (v0) - (v0 != 0);
      d1 += count_set_bits (v1) - (v1 != 0);
    }

  is_member0 = (w0 & (1ULL << b0)) != 0;
  is_member1 = (w1 & (1ULL << b1)) != 0;

  d0 = is_member0 ? d0 : 0;
  d1 = is_member1 ? d1 : 0;

  *i0_return = is_member0 + d0;
  *i1_return = is_member1 + d1;
}

#define sparse_vec_free(v) vec_free(v)

#define sparse_vec_elt_at_index(v,i) \
  vec_elt_at_index ((v), sparse_vec_index ((v), (i)))

#define sparse_vec_validate(v,i)					\
({									\
  uword _i;								\
  u32 _insert;								\
									\
  if (! (v))								\
    (v) = sparse_vec_new (sizeof ((v)[0]), BITS (u16));			\
									\
  _i = sparse_vec_index_internal ((v), (i),				\
				  /* maybe range */ 0,			\
				  /* insert? */ &_insert);		\
  if (_insert)								\
    vec_insert_ha ((v), 1, _i,						\
		   /* header size */ sizeof (sparse_vec_header_t),	\
		   /* align */ 0);					\
									\
  /* Invalid index is 0. */						\
  ASSERT (_i > 0);							\
									\
  (v) + _i;								\
})

#endif /* included_sparse_vec_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
e to live @param authoritative - authoritative @param key_id HMAC_NO_KEY 0 HMAC_SHA_1_96 1 HMAC_SHA_256_128 2 @param key - secret key */ define lisp_eid_table_details { u32 context; u32 locator_set_index; u8 action; u8 is_local; u8 eid_type; u8 is_src_dst; u32 vni; u8 eid[16]; u8 eid_prefix_len; u8 seid[16]; u8 seid_prefix_len; u32 ttl; u8 authoritative; u16 key_id; u8 key[64]; }; /** \brief Request for eid table summary status @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @param eid_set - if non-zero request info about specific mapping @param vni - virtual network instance; valid only if eid_set != 0 @param prefix_length - prefix length if EID is IP address; valid only if eid_set != 0 @param eid_type - EID type; valid only if eid_set != 0 Supported values: 0: EID is IPv4 1: EID is IPv6 2: EID is ethernet address @param eid - endpoint identifier @param filter - filter type; Support values: 0: all eid 1: local eid 2: remote eid */ define lisp_eid_table_dump { u32 client_index; u32 context; u8 eid_set; u8 prefix_length; u32 vni; u8 eid_type; u8 eid[16]; u8 filter; }; /** \brief LISP adjacency @param eid_type - 0 : ipv4 1 : ipv6 2 : mac @param reid - remote EID @param leid - local EID @param reid_prefix_len - remote EID IP prefix length @param leid_prefix_len - local EID IP prefix length */ typeonly manual_print manual_endian define lisp_adjacency { u8 eid_type; u8 reid[16]; u8 leid[16]; u8 reid_prefix_len; u8 leid_prefix_len; }; /** \brief LISP adjacency reply @param count - number of adjacencies @param adjacencies - array of adjacencies */ manual_endian manual_print define lisp_adjacencies_get_reply { u32 context; i32 retval; u32 count; vl_api_lisp_adjacency_t adjacencies[count]; }; /** \brief Request for LISP adjacencies @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @param vni - filter adjacencies by VNI */ define lisp_adjacencies_get { u32 client_index; u32 context; u32 vni; }; /** \brief Shows relationship between vni and vrf/bd @param dp_table - VRF index or bridge domain index @param vni - vitual network instance */ define lisp_eid_table_map_details { u32 context; u32 vni; u32 dp_table; }; /** \brief Request for lisp_eid_table_map_details @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @param is_l2 - if set dump vni/bd mappings else vni/vrf */ define lisp_eid_table_map_dump { u32 client_index; u32 context; u8 is_l2; }; /** \brief Dumps all VNIs used in mappings @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request */ define lisp_eid_table_vni_dump { u32 client_index; u32 context; }; /** \brief reply to lisp_eid_table_vni_dump @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @param vni - virtual network instance */ define lisp_eid_table_vni_details { u32 client_index; u32 context; u32 vni; }; /** \brief LISP map resolver status @param is_ipv6 - if non-zero the address is ipv6, else ipv4 @param ip_address - array of address bytes */ define lisp_map_resolver_details { u32 context; u8 is_ipv6; u8 ip_address[16]; }; /** \brief Request for map resolver summary status @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request */ define lisp_map_resolver_dump { u32 client_index; u32 context; }; /** \brief LISP map server details @param is_ipv6 - if non-zero the address is ipv6, else ipv4 @param ip_address - array of address bytes */ define lisp_map_server_details { u32 context; u8 is_ipv6; u8 ip_address[16]; }; /** \brief Request for map server summary status @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request */ define lisp_map_server_dump { u32 client_index; u32 context; }; /** \brief Request for lisp-gpe protocol status @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request */ define show_lisp_status { u32 client_index; u32 context; }; /** \brief Status of lisp, enable or disable @param context - sender context, to match reply w/ request @param feature_status - lisp enable if non-zero, else disable @param gpe_status - lisp enable if non-zero, else disable */ define show_lisp_status_reply { u32 context; i32 retval; u8 feature_status; u8 gpe_status; }; /** \brief Get LISP map request itr rlocs status @param context - sender context, to match reply w/ request @param locator_set_name - name of the locator_set */ define lisp_get_map_request_itr_rlocs { u32 client_index; u32 context; }; /** \brief Request for map request itr rlocs summary status */ define lisp_get_map_request_itr_rlocs_reply { u32 context; i32 retval; u8 locator_set_name[64]; }; /** \brief Request for lisp pitr status @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request */ define show_lisp_pitr { u32 client_index; u32 context; }; /** \brief Status of lisp pitr, enable or disable @param context - sender context, to match reply w/ request @param status - lisp pitr enable if non-zero, else disable @param locator_set_name - name of the locator_set */ define show_lisp_pitr_reply { u32 context; i32 retval; u8 status; u8 locator_set_name[64]; }; /* * Local Variables: * eval: (c-set-style "gnu") * End: */