aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/memif/private.h
blob: ad6295e4bb80b168fe7e5e7713ba9c8d74caec7e (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
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 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/lock.h>

#define MEMIF_DEFAULT_SOCKET_FILENAME  "memif.sock"
#define MEMIF_DEFAULT_RING_SIZE 1024
#define MEMIF_DEFAULT_RX_QUEUES 1
#define MEMIF_DEFAULT_TX_QUEUES 1
#define MEMIF_DEFAULT_BUFFER_SIZE 2048

#define MEMIF_MAX_M2S_RING		(vec_len (vlib_mains) - 1)
#define MEMIF_MAX_S2M_RING		(vec_len (vlib_mains) - 1)
#define MEMIF_MAX_REGION		255
#define MEMIF_MAX_LOG2_RING_SIZE	14

#define MEMIF_DEBUG 0

#if MEMIF_DEBUG == 1
#define DBG(...) clib_warning(__VA_ARGS__)
#define DBG_UNIX_LOG(...) clib_unix_warning(__VA_ARGS__)
#else
#define DBG(...)
#define DBG_UNIX_LOG(...)
#endif

#if MEMIF_DEBUG == 1
#define memif_file_add(a, b) do {					\
  *a = clib_file_add (&file_main, b);					\
  clib_warning ("clib_file_add fd %d private_data %u idx %u",		\
		(b)->file_descriptor, (b)->private_data, *a);		\
} while (0)

#define memif_file_del(a) do {						\
  clib_warning ("clib_file_del idx %u",a - file_main.file_pool);	\
  clib_file_del (&file_main, a);					\
} while (0)

#define memif_file_del_by_index(a) do {					\
  clib_warning ("clib_file_del idx %u", a);				\
  clib_file_del_by_index (&file_main, a);				\
} while (0)
#else
#define memif_file_add(a, b) do {					\
  *a = clib_file_add (&file_main, b);					\
} while (0)
#define memif_file_del(a) clib_file_del(&file_main, a)
#define memif_file_del_by_index(a) clib_file_del_by_index(&file_main, a)
#endif

typedef struct
{
  u8 *filename;
  u32 socket_id;
  clib_socket_t *sock;
  clib_socket_t **pending_clients;
  int ref_cnt;
  int is_listener;

  /* hash of all registered id */
  mhash_t dev_instance_by_id;

  /* hash of all registered fds */
  uword *dev_instance_by_fd;
} memif_socket_file_t;

typedef struct
{
  void *shm;
  memif_region_size_t region_size;
  int fd;
  u8 is_external;
} memif_region_t;

typedef struct
{
  memif_msg_t msg;
  int fd;
} memif_msg_fifo_elt_t;

typedef struct
{
  /* ring data */
  memif_ring_t *ring;
  memif_log2_ring_size_t log2_ring_size;
  memif_region_index_t region;
  memif_region_offset_t offset;

  u16 last_head;
  u16 last_tail;
  u32 *buffers;

  /* interrupts */
  int int_fd;
  uword int_clib_file_index;
  u64 int_count;

  /* queue type */
  memif_ring_type_t type;
} memif_queue_t;

#define foreach_memif_if_flag \
  _(0, ADMIN_UP, "admin-up")		\
  _(1, IS_SLAVE, "slave")		\
  _(2, CONNECTING, "connecting")	\
  _(3, CONNECTED, "connected")		\
  _(4, DELETING, "deleting")		\
  _(5, ZERO_COPY, "zero-copy")		\
  _(6, ERROR, "error")

typedef enum
{
#define _(a, b, c) MEMIF_IF_FLAG_##b = (1 << a),
  foreach_memif_if_flag
#undef _
} memif_if_flag_t;

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  clib_spinlock_t lockp;
  u32 flags;
  memif_interface_id_t id;
  u32 hw_if_index;
  u32 sw_if_index;
  uword dev_instance;
  memif_interface_mode_t mode:8;

  u32 per_interface_next_index;

  /* socket connection */
  clib_socket_t *sock;
  uword socket_file_index;
  memif_msg_fifo_elt_t *msg_queue;
  u8 *secret;

  memif_region_t *regions;

  memif_queue_t *rx_queues;
  memif_queue_t *tx_queues;

  /* remote info */
  u8 *remote_name;
  u8 *remote_if_name;

  struct
  {
    memif_log2_ring_size_t log2_ring_size;
    u8 num_s2m_rings;
    u8 num_m2s_rings;
    u16 buffer_size;
  } cfg;

  struct
  {
    memif_log2_ring_size_t log2_ring_size;
    u8 num_s2m_rings;
    u8 num_m2s_rings;
    u16 buffer_size;
  } run;

  /* disconnect strings */
  u8 *local_disc_string;
  u8 *remote_disc_string;
} memif_if_t;

typedef struct
{
  u32 packet_len;
  u16 first_buffer_vec_index;
} memif_packet_op_t;

typedef struct
{
  void *data;
  u32 data_len;
  i16 buffer_offset;
  u16 buffer_vec_index;
} memif_copy_op_t;

#define MEMIF_RX_VECTOR_SZ VLIB_FRAME_SIZE

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);

  /* copy vector */
  memif_packet_op_t packet_ops[MEMIF_RX_VECTOR_SZ];
  memif_copy_op_t *copy_ops;
  u32 *buffers;

  /* buffer template */
  vlib_buffer_t buffer_template;
  memif_desc_t desc_template;
} memif_per_thread_data_t;

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);

  /** API message ID base */
  u16 msg_id_base;

  /* pool of all memory interfaces */
  memif_if_t *interfaces;

  /* pool of all unix socket files */
  memif_socket_file_t *socket_files;
  uword *socket_file_index_by_sock_id;	/* map user socket id to pool idx */

  /* per thread data */
  memif_per_thread_data_t *per_thread_data;

} memif_main_t;

extern memif_main_t memif_main;
extern vnet_device_class_t memif_device_class;
extern vlib_node_registration_t memif_input_node;

enum
{
  MEMIF_PROCESS_EVENT_START = 1,
  MEMIF_PROCESS_EVENT_STOP = 2,
} memif_process_event_t;

typedef struct
{
  memif_interface_id_t id;
  u32 socket_id;
  u8 *secret;
  u8 is_master;
  u8 is_zero_copy;
  memif_interface_mode_t mode:8;
  memif_log2_ring_size_t log2_ring_size;
  u16 buffer_size;
  u8 hw_addr_set;
  u8 hw_addr[6];
  u8 rx_queues;
  u8 tx_queues;

  /* return */
  u32 sw_if_index;
} memif_create_if_args_t;

int memif_socket_filename_add_del (u8 is_add, u32 sock_id,
				   u8 * sock_filename);
int memif_create_if (vlib_main_t * vm, memif_create_if_args_t * args);
int memif_delete_if (vlib_main_t * vm, memif_if_t * mif);
clib_error_t *memif_plugin_api_hookup (vlib_main_t * vm);

static_always_inline void *
memif_get_buffer (memif_if_t * mif, memif_ring_t * ring, u16 slot)
{
  u16 region = ring->desc[slot].region;
  return mif->regions[region].shm + ring->desc[slot].offset;
}

/* memif.c */
clib_error_t *memif_init_regions_and_queues (memif_if_t * mif);
clib_error_t *memif_connect (memif_if_t * mif);
void memif_disconnect (memif_if_t * mif, clib_error_t * err);

/* socket.c */
void memif_socket_close (clib_socket_t ** sock);
clib_error_t *memif_conn_fd_accept_ready (clib_file_t * uf);
clib_error_t *memif_master_conn_fd_read_ready (clib_file_t * uf);
clib_error_t *memif_slave_conn_fd_read_ready (clib_file_t * uf);
clib_error_t *memif_master_conn_fd_write_ready (clib_file_t * uf);
clib_error_t *memif_slave_conn_fd_write_ready (clib_file_t * uf);
clib_error_t *memif_master_conn_fd_error (clib_file_t * uf);
clib_error_t *memif_slave_conn_fd_error (clib_file_t * uf);
clib_error_t *memif_msg_send_disconnect (memif_if_t * mif,
					 clib_error_t * err);
u8 *format_memif_device_name (u8 * s, va_list * args);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
pan class="p">(void *v) { hash_t *h = hash_header (v); return v ? h->elts : 0; } /* Number of elements the hash table can hold */ always_inline uword hash_capacity (void *v) { return vec_len (v); } /* Returns 1 if the hash pair contains user data */ always_inline uword hash_is_user (void *v, uword i) { hash_t *h = hash_header (v); uword i0 = i / BITS (h->is_user[0]); uword i1 = i % BITS (h->is_user[0]); return (h->is_user[i0] & ((uword) 1 << i1)) != 0; } /* Set the format function and format argument for a hash table */ always_inline void hash_set_pair_format (void *v, format_function_t * format_pair, void *format_pair_arg) { hash_t *h = hash_header (v); h->format_pair = format_pair; h->format_pair_arg = format_pair_arg; } /* Set hash table flags */ always_inline void hash_set_flags (void *v, uword flags) { hash_header (v)->flags |= flags; } /* Key value pairs. */ typedef struct { /* The Key */ uword key; /* The Value. Length is 2^log2_pair_size - 1. */ uword value[0]; } hash_pair_t; /* The indirect pair structure If log2_pair_size > 0 we overload hash pairs with indirect pairs for buckets with more than one pair. */ typedef struct { /* pair vector */ hash_pair_t *pairs; /* padding */ u8 pad[sizeof (uword) - sizeof (hash_pair_t *)]; /* allocated length */ uword alloc_len; } hash_pair_indirect_t; /* Direct / Indirect pair union */ typedef union { hash_pair_t direct; hash_pair_indirect_t indirect; } hash_pair_union_t; #define LOG2_ALLOC_BITS (5) #define PAIR_BITS (BITS (uword) - LOG2_ALLOC_BITS) /* Log2 number of bytes allocated in pairs array. */ always_inline uword indirect_pair_get_log2_bytes (hash_pair_indirect_t * p) { return p->alloc_len >> PAIR_BITS; } /* Get the length of an indirect pair */ always_inline uword indirect_pair_get_len (hash_pair_indirect_t * p) { if (!p->pairs) return 0; else return p->alloc_len & (((uword) 1 << PAIR_BITS) - 1); } /* Set the length of an indirect pair */ always_inline void indirect_pair_set (hash_pair_indirect_t * p, uword log2_alloc, uword len) { ASSERT (len < ((uword) 1 << PAIR_BITS)); ASSERT (log2_alloc < ((uword) 1 << LOG2_ALLOC_BITS)); p->alloc_len = (log2_alloc << PAIR_BITS) | len; } /* internal routine to fetch value for given key */ uword *_hash_get (void *v, uword key); /* internal routine to fetch value (key, value) pair for given key */ hash_pair_t *_hash_get_pair (void *v, uword key); /* internal routine to unset a (key, value) pair */ void *_hash_unset (void *v, uword key, void *old_value); /* internal routine to set a (key, value) pair, return the old value */ void *_hash_set3 (void *v, uword key, void *value, void *old_value); /* Resize a hash table */ void *hash_resize (void *old, uword new_size); /* duplicate a hash table */ void *hash_dup (void *old); /* Returns the number of bytes used by a hash table */ uword hash_bytes (void *v); /* Public macro to set a (key, value) pair, return the old value */ #define hash_set3(h,key,value,old_value) \ ({ \ uword _v = (uword) (value); \ (h) = _hash_set3 ((h), (uword) (key), (void *) &_v, (old_value)); \ }) /* Public macro to fetch value for given key */ #define hash_get(h,key) _hash_get ((h), (uword) (key)) /* Public macro to fetch value (key, value) pair for given key */ #define hash_get_pair(h,key) _hash_get_pair ((h), (uword) (key)) /* Public macro to set a (key, value) pair */ #define hash_set(h,key,value) hash_set3(h,key,value,0) /* Public macro to set (key, 0) pair */ #define hash_set1(h,key) (h) = _hash_set3(h,(uword) (key),0,0) /* Public macro to unset a (key, value) pair */ #define hash_unset(h,key) ((h) = _hash_unset ((h), (uword) (key),0)) /* Public macro to unset a (key, value) pair, return the old value */ #define hash_unset3(h,key,old_value) ((h) = _hash_unset ((h), (uword) (key), (void *) (old_value))) /* get/set/unset for pointer keys. */ /* Public macro to fetch value for given pointer key */ #define hash_get_mem(h,key) _hash_get ((h), pointer_to_uword (key)) /* Public macro to fetch (key, value) for given pointer key */ #define hash_get_pair_mem(h,key) _hash_get_pair ((h), pointer_to_uword (key)) /* Public macro to set (key, value) for pointer key */ #define hash_set_mem(h,key,value) hash_set3 (h, pointer_to_uword (key), (value), 0) /* Public inline function allocate and copy key to use in hash for pointer key */ always_inline void hash_set_mem_alloc (uword ** h, void *key, uword v) { size_t ksz = hash_header (*h)->user; void *copy = clib_mem_alloc (ksz); clib_memcpy_fast (copy, key, ksz); hash_set_mem (*h, copy, v); } /* Public macro to set (key, 0) for pointer key */ #define hash_set1_mem(h,key) hash_set3 ((h), pointer_to_uword (key), 0, 0) /* Public macro to unset (key, value) for pointer key */ #define hash_unset_mem(h,key) ((h) = _hash_unset ((h), pointer_to_uword (key),0)) /* Public inline function to unset pointer key and then free the key memory */ always_inline void hash_unset_mem_free (uword ** h, void *key) { hash_pair_t *hp = hash_get_pair_mem (*h, key); if (PREDICT_TRUE (hp != NULL)) { key = uword_to_pointer (hp->key, void *); hash_unset_mem (*h, key); clib_mem_free (key); } } /* internal routine to free a hash table */ extern void *_hash_free (void *v); /* Public macro to free a hash table */ #define hash_free(h) (h) = _hash_free ((h)) clib_error_t *hash_validate (void *v); /* Public inline function to get the number of value bytes for a hash table */ always_inline uword hash_value_bytes (hash_t * h) { hash_pair_t *p; return (sizeof (p->value[0]) << h->log2_pair_size) - sizeof (p->key); } /* Public inline function to get log2(size of a (key,value) pair) */ always_inline uword hash_pair_log2_bytes (hash_t * h) { uword log2_bytes = h->log2_pair_size; ASSERT (BITS (hash_pair_t) == 32 || BITS (hash_pair_t) == 64); if (BITS (hash_pair_t) == 32) log2_bytes += 2; else if (BITS (hash_pair_t) == 64) log2_bytes += 3; return log2_bytes; } /* Public inline function to get size of a (key,value) pair */ always_inline uword hash_pair_bytes (hash_t * h) { return (uword) 1 << hash_pair_log2_bytes (h); } /* Public inline function to advance a pointer past one (key,value) pair */ always_inline void * hash_forward1 (hash_t * h, void *v) { return (u8 *) v + hash_pair_bytes (h); } /* Public inline function to advance a pointer past N (key,value) pairs */ always_inline void * hash_forward (hash_t * h, void *v, uword n) { return (u8 *) v + ((n * sizeof (hash_pair_t)) << h->log2_pair_size); } /** Iterate over hash pairs. @param p The current (key,value) pair. This should be of type <code>(hash_pair_t *)</code>. @param v The hash table to iterate. @param body The operation to perform on each (key,value) pair. Executes the expression or code block @c body with each active hash pair. */ /* A previous version of this macro made use of the hash_pair_union_t * structure; this version does not since that approach mightily upset * the static analysis tool. In the rare chance someone is reading this * code, pretend that _p below is of type hash_pair_union_t and that when * used as an rvalue it's really using one of the union members as the * rvalue. If you were confused before you might be marginally less * confused after. */ #define hash_foreach_pair(p,v,body) \ do { \ __label__ _hash_foreach_done; \ hash_t * _h = hash_header (v); \ void * _p; \ hash_pair_t * _q, * _q_end; \ uword _i, _i1, _id, _pair_increment; \ \ _p = (v); \ _i = 0; \ _pair_increment = 1; \ if ((v)) \ _pair_increment = 1 << _h->log2_pair_size; \ while (_i < hash_capacity (v)) \ { \ _id = _h->is_user[_i / BITS (_h->is_user[0])]; \ _i1 = _i + BITS (_h->is_user[0]); \ \ do { \ if (_id & 1) \ { \ _q = _p; \ _q_end = _q + _pair_increment; \ } \ else \ { \ hash_pair_indirect_t * _pi = _p; \ _q = _pi->pairs; \ if (_h->log2_pair_size > 0) \ _q_end = hash_forward (_h, _q, indirect_pair_get_len (_pi)); \ else \ _q_end = vec_end (_q); \ } \ \ /* Loop through all elements in bucket. \ Bucket may have 0 1 or more (indirect case) pairs. */ \ while (_q < _q_end) \ { \ uword _break_in_body = 1; \ (p) = _q; \ do { \ body; \ _break_in_body = 0; \ } while (0); \ if (_break_in_body) \ goto _hash_foreach_done; \ _q += _pair_increment; \ } \ \ _p = (hash_pair_t *)_p + _pair_increment; \ _id = _id / 2; \ _i++; \ } while (_i < _i1); \ } \ _hash_foreach_done: \ /* Be silent Mr. Compiler-Warning. */ \ ; \ } while (0) /* Iterate over key/value pairs @param key_var the current key @param value_var the current value @param h the hash table to iterate across @param body the operation to perform on each (key_var,value_var) pair. calls body with each active hash pair */ /* Iterate over key/value pairs. */ #define hash_foreach(key_var,value_var,h,body) \ do { \ hash_pair_t * _r; \ hash_foreach_pair (_r, (h), { \ (key_var) = (__typeof__ (key_var)) _r->key; \ (value_var) = (__typeof__ (value_var)) _r->value[0]; \ do { body; } while (0); \ }); \ } while (0) /* Iterate over key/value pairs for pointer key hash tables @param key_var the current key @param value_var the current value @param h the hash table to iterate across @param body the operation to perform on each (key_var,value_var) pair. calls body with each active hash pair */ #define hash_foreach_mem(key_var,value_var,h,body) \ do { \ hash_pair_t * _r; \ hash_foreach_pair (_r, (h), { \ (key_var) = (__typeof__ (key_var)) uword_to_pointer (_r->key, void *); \ (value_var) = (__typeof__ (value_var)) _r->value[0]; \ do { body; } while (0); \ }); \ } while (0) /* Support for iteration through hash table. */ /* This struct saves iteration state for hash_next. None of these fields are meant to be visible to the user. Hence, the cryptic short-hand names. */ typedef struct { uword i, j, f; } hash_next_t; hash_pair_t *hash_next (void *v, hash_next_t * hn); void *_hash_create (uword elts, hash_t * h); always_inline void hash_set_value_bytes (hash_t * h, uword value_bytes) { hash_pair_t *p; h->log2_pair_size = max_log2 ((sizeof (p->key) + value_bytes + sizeof (p->key) - 1) / sizeof (p->key)); } #define hash_create2(_elts,_user,_value_bytes, \ _key_sum,_key_equal, \ _format_pair,_format_pair_arg) \ ({ \ hash_t _h; \ clib_memset (&_h, 0, sizeof (_h)); \ _h.user = (_user); \ _h.key_sum = (hash_key_sum_function_t *) (_key_sum); \ _h.key_equal = (_key_equal); \ hash_set_value_bytes (&_h, (_value_bytes)); \ _h.format_pair = (format_function_t *) (_format_pair); \ _h.format_pair_arg = (_format_pair_arg); \ _hash_create ((_elts), &_h); \ }) /* Hash function based on that of Bob Jenkins (bob_jenkins@compuserve.com). Public domain per: http://www.burtleburtle.net/bob/hash/doobs.html Thanks, Bob. */ #define hash_mix_step(a,b,c,s0,s1,s2) \ do { \ (a) -= (b) + (c); (a) ^= (c) >> (s0); \ (b) -= (c) + (a); (b) ^= (a) << (s1); \ (c) -= (a) + (b); (c) ^= (b) >> (s2); \ } while (0) #define hash_mix32_step_1(a,b,c) hash_mix_step(a,b,c,13,8,13) #define hash_mix32_step_2(a,b,c) hash_mix_step(a,b,c,12,16,5) #define hash_mix32_step_3(a,b,c) hash_mix_step(a,b,c,3,10,15) #define hash_mix64_step_1(a,b,c) hash_mix_step(a,b,c,43,9,8) #define hash_mix64_step_2(a,b,c) hash_mix_step(a,b,c,38,23,5) #define hash_mix64_step_3(a,b,c) hash_mix_step(a,b,c,35,49,11) #define hash_mix64_step_4(a,b,c) hash_mix_step(a,b,c,12,18,22) /* Hash function based on that of Bob Jenkins (bob_jenkins@compuserve.com). Thanks, Bob. */ #define hash_mix64(a0,b0,c0) \ do { \ hash_mix64_step_1 (a0, b0, c0); \ hash_mix64_step_2 (a0, b0, c0); \ hash_mix64_step_3 (a0, b0, c0); \ hash_mix64_step_4 (a0, b0, c0); \ } while (0) \ #define hash_mix32(a0,b0,c0) \ do { \ hash_mix32_step_1 (a0, b0, c0); \ hash_mix32_step_2 (a0, b0, c0); \ hash_mix32_step_3 (a0, b0, c0); \ } while (0) \ /* Finalize from Bob Jenkins lookup3.c */ always_inline uword hash32_rotate_left (u32 x, u32 i) { return (x << i) | (x >> (BITS (i) - i)); } #define hash_v3_mix32(a,b,c) \ do { \ (a) -= (c); (a) ^= hash32_rotate_left ((c), 4); (c) += (b); \ (b) -= (a); (b) ^= hash32_rotate_left ((a), 6); (a) += (c); \ (c) -= (b); (c) ^= hash32_rotate_left ((b), 8); (b) += (a); \ (a) -= (c); (a) ^= hash32_rotate_left ((c),16); (c) += (b); \ (b) -= (a); (b) ^= hash32_rotate_left ((a),19); (a) += (c); \ (c) -= (b); (c) ^= hash32_rotate_left ((b), 4); (b) += (a); \ } while (0) #define hash_v3_finalize32(a,b,c) \ do { \ (c) ^= (b); (c) -= hash32_rotate_left ((b), 14); \ (a) ^= (c); (a) -= hash32_rotate_left ((c), 11); \ (b) ^= (a); (b) -= hash32_rotate_left ((a), 25); \ (c) ^= (b); (c) -= hash32_rotate_left ((b), 16); \ (a) ^= (c); (a) -= hash32_rotate_left ((c), 4); \ (b) ^= (a); (b) -= hash32_rotate_left ((a), 14); \ (c) ^= (b); (c) -= hash32_rotate_left ((b), 24); \ } while (0) /* 32 bit mixing/finalize in steps. */ #define hash_v3_mix32_step1(a,b,c) \ do { \ (a) -= (c); (a) ^= hash32_rotate_left ((c), 4); (c) += (b); \ (b) -= (a); (b) ^= hash32_rotate_left ((a), 6); (a) += (c); \ } while (0) #define hash_v3_mix32_step2(a,b,c) \ do { \ (c) -= (b); (c) ^= hash32_rotate_left ((b), 8); (b) += (a); \ (a) -= (c); (a) ^= hash32_rotate_left ((c),16); (c) += (b); \ } while (0) #define hash_v3_mix32_step3(a,b,c) \ do { \ (b) -= (a); (b) ^= hash32_rotate_left ((a),19); (a) += (c); \ (c) -= (b); (c) ^= hash32_rotate_left ((b), 4); (b) += (a); \ } while (0) #define hash_v3_finalize32_step1(a,b,c) \ do { \ (c) ^= (b); (c) -= hash32_rotate_left ((b), 14); \ (a) ^= (c); (a) -= hash32_rotate_left ((c), 11); \ } while (0) #define hash_v3_finalize32_step2(a,b,c) \ do { \ (b) ^= (a); (b) -= hash32_rotate_left ((a), 25); \ (c) ^= (b); (c) -= hash32_rotate_left ((b), 16); \ } while (0) #define hash_v3_finalize32_step3(a,b,c) \ do { \ (a) ^= (c); (a) -= hash32_rotate_left ((c), 4); \ (b) ^= (a); (b) -= hash32_rotate_left ((a), 14); \ (c) ^= (b); (c) -= hash32_rotate_left ((b), 24); \ } while (0) /* Vector v3 mixing/finalize. */ #define hash_v3_mix_step_1_u32x(a,b,c) \ do { \ (a) -= (c); (a) ^= u32x_irotate_left ((c), 4); (c) += (b); \ (b) -= (a); (b) ^= u32x_irotate_left ((a), 6); (a) += (c); \ (c) -= (b); (c) ^= u32x_irotate_left ((b), 8); (b) += (a); \ } while (0) #define hash_v3_mix_step_2_u32x(a,b,c) \ do { \ (a) -= (c); (a) ^= u32x_irotate_left ((c),16); (c) += (b); \ (b) -= (a); (b) ^= u32x_irotate_left ((a),19); (a) += (c); \ (c) -= (b); (c) ^= u32x_irotate_left ((b), 4); (b) += (a); \ } while (0) #define hash_v3_finalize_step_1_u32x(a,b,c) \ do { \ (c) ^= (b); (c) -= u32x_irotate_left ((b), 14); \ (a) ^= (c); (a) -= u32x_irotate_left ((c), 11); \ (b) ^= (a); (b) -= u32x_irotate_left ((a), 25); \ } while (0) #define hash_v3_finalize_step_2_u32x(a,b,c) \ do { \ (c) ^= (b); (c) -= u32x_irotate_left ((b), 16); \ (a) ^= (c); (a) -= u32x_irotate_left ((c), 4); \ (b) ^= (a); (b) -= u32x_irotate_left ((a), 14); \ (c) ^= (b); (c) -= u32x_irotate_left ((b), 24); \ } while (0) #define hash_v3_mix_u32x(a,b,c) \ do { \ hash_v3_mix_step_1_u32x(a,b,c); \ hash_v3_mix_step_2_u32x(a,b,c); \ } while (0) #define hash_v3_finalize_u32x(a,b,c) \ do { \ hash_v3_finalize_step_1_u32x(a,b,c); \ hash_v3_finalize_step_2_u32x(a,b,c); \ } while (0) extern uword hash_memory (void *p, word n_bytes, uword state); extern uword mem_key_sum (hash_t * h, uword key); extern uword mem_key_equal (hash_t * h, uword key1, uword key2); #define hash_create_mem(elts,key_bytes,value_bytes) \ hash_create2((elts),(key_bytes),(value_bytes),mem_key_sum,mem_key_equal,0,0) extern uword vec_key_sum (hash_t * h, uword key); extern uword vec_key_equal (hash_t * h, uword key1, uword key2); extern u8 *vec_key_format_pair (u8 * s, va_list * args); #define hash_create_vec(elts,key_bytes,value_bytes) \ hash_create2((elts),(key_bytes),(value_bytes),\ vec_key_sum,vec_key_equal,vec_key_format_pair,0) extern uword string_key_sum (hash_t * h, uword key); extern uword string_key_equal (hash_t * h, uword key1, uword key2); extern u8 *string_key_format_pair (u8 * s, va_list * args); /* * Note: if you plan to put a hash into shared memory, * the key sum and key equal functions MUST be set to magic constants! * PIC means that whichever process sets up the hash won't have * the actual key sum functions at the same place, leading to * very hard-to-find bugs... */ #define hash_create_shmem(elts,key_bytes,value_bytes) \ hash_create2((elts),(key_bytes),(value_bytes), \ (hash_key_sum_function_t *) KEY_FUNC_MEM, \ (hash_key_equal_function_t *)KEY_FUNC_MEM, \ 0, 0) #define hash_create_string(elts,value_bytes) \ hash_create2((elts),0,(value_bytes), \ (hash_key_sum_function_t *) KEY_FUNC_STRING, \ (hash_key_equal_function_t *)KEY_FUNC_STRING, \ 0, 0) #define hash_create(elts,value_bytes) \ hash_create2((elts),0,(value_bytes), \ (hash_key_sum_function_t *) KEY_FUNC_NONE, \ (hash_key_equal_function_t *) KEY_FUNC_NONE, \ 0,0) #define hash_create_uword(elts,value_bytes) \ hash_create2((elts),0,(value_bytes), \ (hash_key_sum_function_t *) KEY_FUNC_POINTER_UWORD, \ (hash_key_equal_function_t *) KEY_FUNC_POINTER_UWORD, \ 0,0) #define hash_create_u32(elts,value_bytes) \ hash_create2((elts),0,(value_bytes), \ (hash_key_sum_function_t *) KEY_FUNC_POINTER_U32, \ (hash_key_equal_function_t *) KEY_FUNC_POINTER_U32, \ 0,0) u8 *format_hash (u8 * s, va_list * va); /* Looks up input in hash table indexed by either vec string or c string (null terminated). */ unformat_function_t unformat_hash_vec_string; unformat_function_t unformat_hash_string; /* Main test routine. */ int test_hash_main (unformat_input_t * input); #endif /* included_hash_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */