summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/socket.c
blob: 99b353fcd52deebfd729e0fb2cb498d3b384f371 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * 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) 2001, 2002, 2003, 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.
*/

#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>		/* strchr */

#include <vppinfra/mem.h>
#include <vppinfra/vec.h>
#include <vppinfra/socket.h>
#include <vppinfra/format.h>
#include <vppinfra/error.h>

void
clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...)
{
  va_list va;
  va_start (va, fmt);
  clib_socket_tx_add_va_formatted (s, fmt, &va);
  va_end (va);
}

/* Return and bind to an unused port. */
static word
find_free_port (word sock)
{
  word port;

  for (port = IPPORT_USERRESERVED; port < 1 << 16; port++)
    {
      struct sockaddr_in a;

      memset (&a, 0, sizeof (a));	/* Warnings be gone */

      a.sin_family = PF_INET;
      a.sin_addr.s_addr = INADDR_ANY;
      a.sin_port = htons (port);

      if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0)
	break;
    }

  return port < 1 << 16 ? port : -1;
}

/* Convert a config string to a struct sockaddr and length for use
   with bind or connect. */
static clib_error_t *
socket_config (char *config,
	       void *addr, socklen_t * addr_len, u32 ip4_default_address)
{
  clib_error_t *error = 0;

  if (!config)
    config = "";

  /* Anything that begins with a / is a local PF_LOCAL socket. */
  if (config[0] == '/')
    {
      struct sockaddr_un *su = addr;
      su->sun_family = PF_LOCAL;
      clib_memcpy (&su->sun_path, config,
		   clib_min (sizeof (su->sun_path), 1 + strlen (config)));
      *addr_len = sizeof (su[0]);
    }

  /* Hostname or hostname:port or port. */
  else
    {
      char *host_name;
      int port = -1;
      struct sockaddr_in *sa = addr;

      host_name = 0;
      port = -1;
      if (config[0] != 0)
	{
	  unformat_input_t i;

	  unformat_init_string (&i, config, strlen (config));
	  if (unformat (&i, "%s:%d", &host_name, &port)
	      || unformat (&i, "%s:0x%x", &host_name, &port))
	    ;
	  else if (unformat (&i, "%s", &host_name))
	    ;
	  else
	    error = clib_error_return (0, "unknown input `%U'",
				       format_unformat_error, &i);
	  unformat_free (&i);

	  if (error)
	    goto done;
	}

      sa->sin_family = PF_INET;
      *addr_len = sizeof (sa[0]);
      if (port != -1)
	sa->sin_port = htons (port);
      else
	sa->sin_port = 0;

      if (host_name)
	{
	  struct in_addr host_addr;

	  /* Recognize localhost to avoid host lookup in most common cast. */
	  if (!strcmp (host_name, "localhost"))
	    sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);

	  else if (inet_aton (host_name, &host_addr))
	    sa->sin_addr = host_addr;

	  else if (host_name && strlen (host_name) > 0)
	    {
	      struct hostent *host = gethostbyname (host_name);
	      if (!host)
		error = clib_error_return (0, "unknown host `%s'", config);
	      else
		clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0],
			     host->h_length);
	    }

	  else
	    sa->sin_addr.s_addr = htonl (ip4_default_address);

	  vec_free (host_name);
	  if (error)
	    goto done;
	}
    }

done:
  return error;
}

static clib_error_t *
default_socket_write (clib_socket_t * s)
{
  clib_error_t *err = 0;
  word written = 0;
  word fd = 0;
  word tx_len;

  fd = s->fd;

  /* Map standard input to standard output.
     Typically, fd is a socket for which read/write both work. */
  if (fd == 0)
    fd = 1;

  tx_len = vec_len (s->tx_buffer);
  written = write (fd, s->tx_buffer, tx_len);

  /* Ignore certain errors. */
  if (written < 0 && !unix_error_is_fatal (errno))
    written = 0;

  /* A "real" error occurred. */
  if (written < 0)
    {
      err = clib_error_return_unix (0, "write %wd bytes", tx_len);
      vec_free (s->tx_buffer);
      goto done;
    }

  /* Reclaim the transmitted part of the tx buffer on successful writes. */
  else if (written > 0)
    {
      if (written == tx_len)
	_vec_len (s->tx_buffer) = 0;
      else
	vec_delete (s->tx_buffer, written, 0);
    }

  /* If a non-fatal error occurred AND
     the buffer is full, then we must free it. */
  else if (written == 0 && tx_len > 64 * 1024)
    {
      vec_free (s->tx_buffer);
    }

done:
  return err;
}

static clib_error_t *
default_socket_read (clib_socket_t * sock, int n_bytes)
{
  word fd, n_read;
  u8 *buf;

  /* RX side of socket is down once end of file is reached. */
  if (sock->flags & SOCKET_RX_END_OF_FILE)
    return 0;

  fd = sock->fd;

  n_bytes = clib_max (n_bytes, 4096);
  vec_add2 (sock->rx_buffer, buf, n_bytes);

  if ((n_read = read (fd, buf, n_bytes)) < 0)
    {
      n_read = 0;

      /* Ignore certain errors. */
      if (!unix_error_is_fatal (errno))
	goto non_fatal;

      return clib_error_return_unix (0, "read %d bytes", n_bytes);
    }

  /* Other side closed the socket. */
  if (n_read == 0)
    sock->flags |= SOCKET_RX_END_OF_FILE;

non_fatal:
  _vec_len (sock->rx_buffer) += n_read - n_bytes;

  return 0;
}

static clib_error_t *
default_socket_close (clib_socket_t * s)
{
  if (close (s->fd) < 0)
    return clib_error_return_unix (0, "close");
  return 0;
}

static void
socket_init_funcs (clib_socket_t * s)
{
  if (!s->write_func)
    s->write_func = default_socket_write;
  if (!s->read_func)
    s->read_func = default_socket_read;
  if (!s->close_func)
    s->close_func = default_socket_close;
}

clib_error_t *
clib_socket_init (clib_socket_t * s)
{
  union
  {
    struct sockaddr sa;
    struct sockaddr_un su;
  } addr;
  socklen_t addr_len = 0;
  clib_error_t *error = 0;
  word port;

  error = socket_config (s->config, &addr.sa, &addr_len,
			 (s->flags & SOCKET_IS_SERVER
			  ? INADDR_LOOPBACK : INADDR_ANY));
  if (error)
    goto done;

  socket_init_funcs (s);

  s->fd = socket (addr.sa.sa_family, SOCK_STREAM, 0);
  if (s->fd < 0)
    {
      error = clib_error_return_unix (0, "socket");
      goto done;
    }

  port = 0;
  if (addr.sa.sa_family == PF_INET)
    port = ((struct sockaddr_in *) &addr)->sin_port;

  if (s->flags & SOCKET_IS_SERVER)
    {
      uword need_bind = 1;

      if (addr.sa.sa_family == PF_INET)
	{
	  if (port == 0)
	    {
	      port = find_free_port (s->fd);
	      if (port < 0)
		{
		  error = clib_error_return (0, "no free port");
		  goto done;
		}
	      need_bind = 0;
	    }
	}
      if (addr.sa.sa_family == PF_LOCAL)
	unlink (((struct sockaddr_un *) &addr)->sun_path);

      /* Make address available for multiple users. */
      {
	int v = 1;
	if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
	  clib_unix_warning ("setsockopt SO_REUSEADDR fails");
      }

      if (need_bind && bind (s->fd, &addr.sa, addr_len) < 0)
	{
	  error = clib_error_return_unix (0, "bind");
	  goto done;
	}

      if (listen (s->fd, 5) < 0)
	{
	  error = clib_error_return_unix (0, "listen");
	  goto done;
	}
    }
  else
    {
      if ((s->flags & SOCKET_NON_BLOCKING_CONNECT)
	  && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
	{
	  error = clib_error_return_unix (0, "fcntl NONBLOCK");
	  goto done;
	}

      if (connect (s->fd, &addr.sa, addr_len) < 0
	  && !((s->flags & SOCKET_NON_BLOCKING_CONNECT) &&
	       errno == EINPROGRESS))
	{
	  error = clib_error_return_unix (0, "connect");
	  goto done;
	}
    }

  return error;

done:
  if (s->fd > 0)
    close (s->fd);
  return error;
}

clib_error_t *
clib_socket_accept (clib_socket_t * server, clib_socket_t * client)
{
  clib_error_t *err = 0;
  socklen_t len = 0;

  memset (client, 0, sizeof (client[0]));

  /* Accept the new socket connection. */
  client->fd = accept (server->fd, 0, 0);
  if (client->fd < 0)
    return clib_error_return_unix (0, "accept");

  /* Set the new socket to be non-blocking. */
  if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
    {
      err = clib_error_return_unix (0, "fcntl O_NONBLOCK");
      goto close_client;
    }

  /* Get peer info. */
  len = sizeof (client->peer);
  if (getpeername (client->fd, (struct sockaddr *) &client->peer, &len) < 0)
    {
      err = clib_error_return_unix (0, "getpeername");
      goto close_client;
    }

  client->flags = SOCKET_IS_CLIENT;

  socket_init_funcs (client);
  return 0;

close_client:
  close (client->fd);
  return err;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
> c0, bi0, valid0; u32 a1, b1, c1, bi1, valid1; uword k0, k1, *h0, *h1; k0 = k[0]; k1 = k[1]; n_left -= 2; k += 2; a0 = a1 = h->hash_seeds[0]; b0 = b1 = h->hash_seeds[1]; c0 = c1 = h->hash_seeds[2]; a0 ^= k0; a1 ^= k1; #if uword_bits == 64 b0 ^= k0 >> 32; b1 ^= k1 >> 32; #endif hash_mix32_step_1 (a0, b0, c0); hash_mix32_step_1 (a1, b1, c1); hash_mix32_step_2 (a0, b0, c0); hash_mix32_step_2 (a1, b1, c1); hash_mix32_step_3 (a0, b0, c0); hash_mix32_step_3 (a1, b1, c1); bi0 = c0 & bucket_mask; bi1 = c1 & bucket_mask; h0 = hash_keys + bi0; h1 = hash_keys + bi1; /* Search two buckets. */ valid0 = qhash_get_valid_elt_mask (h, bi0); valid1 = qhash_get_valid_elt_mask (h, bi1); match_mask = qhash_search_bucket (h0, k0, valid0); match_mask |= (qhash_search_bucket (h1, k1, valid1) << QHASH_KEYS_PER_BUCKET); if (match_mask) { uword bi, is_match1; bi = qhash_min_log2 (match_mask); is_match1 = bi >= QHASH_KEYS_PER_BUCKET; bi += ((is_match1 ? bi1 : bi0) - (is_match1 << QHASH_LOG2_KEYS_PER_BUCKET)); *matching_key = (k - 2 - search_keys) + is_match1; return bi; } /* Full buckets trigger search of overflow hash. */ if (PREDICT_FALSE (valid0 == QHASH_ALL_VALID || valid1 == QHASH_ALL_VALID)) { uword *p = 0; uword ki = k - 2 - search_keys; if (valid0 == QHASH_ALL_VALID) p = hash_get (h->overflow_hash, k0); if (!p && valid1 == QHASH_ALL_VALID) { p = hash_get (h->overflow_hash, k1); ki++; } if (p) { *matching_key = ki; return p[0]; } } } while (n_left >= 1) { u32 a0, b0, c0, bi0, valid0; uword k0, *h0; k0 = k[0]; n_left -= 1; k += 1; a0 = h->hash_seeds[0]; b0 = h->hash_seeds[1]; c0 = h->hash_seeds[2]; a0 ^= k0; #if uword_bits == 64 b0 ^= k0 >> 32; #endif hash_mix32 (a0, b0, c0); bi0 = c0 & bucket_mask; h0 = hash_keys + bi0; /* Search one bucket. */ valid0 = qhash_get_valid_elt_mask (h, bi0); match_mask = qhash_search_bucket (h0, k0, valid0); if (match_mask) { uword bi; bi = bi0 + qhash_min_log2 (match_mask); *matching_key = (k - 1 - search_keys); return bi; } /* Full buckets trigger search of overflow hash. */ if (PREDICT_FALSE (valid0 == QHASH_ALL_VALID)) { uword *p = hash_get (h->overflow_hash, k0); if (p) { *matching_key = (k - 1 - search_keys); return p[0]; } } } return ~0; } static void * qhash_set_overflow (void *v, uword elt_bytes, uword key, uword bi, uword * n_elts, u32 * result) { qhash_t *h = qhash_header (v); uword *p = hash_get (h->overflow_hash, key); uword i; bi /= QHASH_KEYS_PER_BUCKET; if (p) i = p[0]; else { uword l = vec_len (h->overflow_free_indices); if (l > 0) { i = h->overflow_free_indices[l - 1]; _vec_len (h->overflow_free_indices) = l - 1; } else i = (1 << h->log2_hash_size) + hash_elts (h->overflow_hash); hash_set (h->overflow_hash, key, i); vec_validate (h->overflow_counts, bi); h->overflow_counts[bi] += 1; *n_elts += 1; l = vec_len (v); if (i >= l) { uword dl = round_pow2 (1 + i - l, 8); v = _vec_resize (v, dl, (l + dl) * elt_bytes, sizeof (h[0]), /* align */ sizeof (uword)); memset (v + l * elt_bytes, ~0, dl * elt_bytes); } } *result = i; return v; } static uword qhash_unset_overflow (void *v, uword key, uword bi, uword * n_elts) { qhash_t *h = qhash_header (v); uword *p = hash_get (h->overflow_hash, key); uword result; bi /= QHASH_KEYS_PER_BUCKET; if (p) { result = p[0]; hash_unset (h->overflow_hash, key); ASSERT (bi < vec_len (h->overflow_counts)); ASSERT (h->overflow_counts[bi] > 0); ASSERT (*n_elts > 0); vec_add1 (h->overflow_free_indices, result); h->overflow_counts[bi] -= 1; *n_elts -= 1; } else result = ~0; return result; } always_inline uword qhash_find_free (uword i, uword valid_mask) { return first_set (~valid_mask & pow2_mask (QHASH_KEYS_PER_BUCKET)); } void * _qhash_set_multiple (void *v, uword elt_bytes, uword * search_keys, uword n_search_keys, u32 * result_indices) { qhash_t *h = qhash_header (v); uword *k, *hash_keys; uword n_left, n_elts, bucket_mask; u32 *r; if (vec_len (v) < n_search_keys) v = _qhash_resize (v, n_search_keys, elt_bytes); if (qhash_min_log2 (2) != 1) { qhash_min_log2_init (); ASSERT (qhash_min_log2 (2) == 1); } ASSERT (v != 0); bucket_mask = pow2_mask (h->log2_hash_size) & ~(QHASH_KEYS_PER_BUCKET - 1); hash_keys = h->hash_keys; k = search_keys; r = result_indices; n_left = n_search_keys; n_elts = h->n_elts; while (n_left >= 2) { u32 a0, b0, c0, bi0, match0, valid0, free0; u32 a1, b1, c1, bi1, match1, valid1, free1; uword k0, *h0; uword k1, *h1; k0 = k[0]; k1 = k[1]; /* Keys must be unique. */ ASSERT (k0 != k1); n_left -= 2; k += 2; a0 = a1 = h->hash_seeds[0]; b0 = b1 = h->hash_seeds[1]; c0 = c1 = h->hash_seeds[2]; a0 ^= k0; a1 ^= k1; #if uword_bits == 64 b0 ^= k0 >> 32; b1 ^= k1 >> 32; #endif hash_mix32_step_1 (a0, b0, c0); hash_mix32_step_1 (a1, b1, c1); hash_mix32_step_2 (a0, b0, c0); hash_mix32_step_2 (a1, b1, c1); hash_mix32_step_3 (a0, b0, c0); hash_mix32_step_3 (a1, b1, c1); bi0 = c0 & bucket_mask; bi1 = c1 & bucket_mask; h0 = hash_keys + bi0; h1 = hash_keys + bi1; /* Search two buckets. */ valid0 = qhash_get_valid_elt_mask (h, bi0); valid1 = qhash_get_valid_elt_mask (h, bi1); match0 = qhash_search_bucket (h0, k0, valid0); match1 = qhash_search_bucket (h1, k1, valid1); /* Find first free element starting at hash offset into bucket. */ free0 = qhash_find_free (c0 & (QHASH_KEYS_PER_BUCKET - 1), valid0); valid1 = valid1 | (bi0 == bi1 ? free0 : 0); free1 = qhash_find_free (c1 & (QHASH_KEYS_PER_BUCKET - 1), valid1); n_elts += (match0 == 0) + (match1 == 0); match0 = match0 ? match0 : free0; match1 = match1 ? match1 : free1; valid0 |= match0; valid1 |= match1; h0 += qhash_min_log2 (match0); h1 += qhash_min_log2 (match1); if (PREDICT_FALSE (!match0 || !match1)) goto slow_path2; h0[0] = k0; h1[0] = k1; r[0] = h0 - hash_keys; r[1] = h1 - hash_keys; r += 2; qhash_set_valid_elt_mask (h, bi0, valid0); qhash_set_valid_elt_mask (h, bi1, valid1); continue; slow_path2: if (!match0) { n_elts -= 1; v = qhash_set_overflow (v, elt_bytes, k0, bi0, &n_elts, &r[0]); } else { h0[0] = k0; r[0] = h0 - hash_keys; qhash_set_valid_elt_mask (h, bi0, valid0); } if (!match1) { n_elts -= 1; v = qhash_set_overflow (v, elt_bytes, k1, bi1, &n_elts, &r[1]); } else { h1[0] = k1; r[1] = h1 - hash_keys; qhash_set_valid_elt_mask (h, bi1, valid1); } r += 2; } while (n_left >= 1) { u32 a0, b0, c0, bi0, match0, valid0, free0; uword k0, *h0; k0 = k[0]; n_left -= 1; k += 1; a0 = h->hash_seeds[0]; b0 = h->hash_seeds[1]; c0 = h->hash_seeds[2]; a0 ^= k0; #if uword_bits == 64 b0 ^= k0 >> 32; #endif hash_mix32 (a0, b0, c0); bi0 = c0 & bucket_mask; h0 = hash_keys + bi0; valid0 = qhash_get_valid_elt_mask (h, bi0); /* Find first free element starting at hash offset into bucket. */ free0 = qhash_find_free (c0 & (QHASH_KEYS_PER_BUCKET - 1), valid0); match0 = qhash_search_bucket (h0, k0, valid0); n_elts += (match0 == 0); match0 = match0 ? match0 : free0; valid0 |= match0; h0 += qhash_min_log2 (match0); if (PREDICT_FALSE (!match0)) goto slow_path1; h0[0] = k0; r[0] = h0 - hash_keys; r += 1; qhash_set_valid_elt_mask (h, bi0, valid0); continue; slow_path1: n_elts -= 1; v = qhash_set_overflow (v, elt_bytes, k0, bi0, &n_elts, &r[0]); r += 1; } h = qhash_header (v); h->n_elts = n_elts; return v; } static uword unset_slow_path (void *v, uword elt_bytes, uword k0, uword bi0, uword valid0, uword match0, uword * n_elts) { qhash_t *h = qhash_header (v); uword i, j = 0, k, l, t = ~0; hash_pair_t *p, *found; if (!match0) { if (valid0 == QHASH_ALL_VALID) t = qhash_unset_overflow (v, k0, bi0, n_elts); return t; } i = bi0 / QHASH_KEYS_PER_BUCKET; t = bi0 + qhash_min_log2 (match0); if (valid0 == QHASH_ALL_VALID && i < vec_len (h->overflow_counts) && h->overflow_counts[i] > 0) { found = 0; /* *INDENT-OFF* */ hash_foreach_pair (p, h->overflow_hash, ({ j = qhash_hash_mix (h, p->key) / QHASH_KEYS_PER_BUCKET; if (j == i) { found = p; break; } })); /* *INDENT-ON* */ ASSERT (found != 0); ASSERT (j == i); l = found->value[0]; k = found->key; hash_unset3 (h->overflow_hash, k, &j); vec_add1 (h->overflow_free_indices, j); h->overflow_counts[i] -= 1; qhash_set_valid_elt_mask (h, bi0, valid0); h->hash_keys[t] = k; clib_memswap (v + t * elt_bytes, v + l * elt_bytes, elt_bytes); t = l; } else qhash_set_valid_elt_mask (h, bi0, valid0 ^ match0); return t; } void _qhash_unset_multiple (void *v, uword elt_bytes, uword * search_keys, uword n_search_keys, u32 * result_indices) { qhash_t *h = qhash_header (v); uword *k, *hash_keys; uword n_left, n_elts, bucket_mask; u32 *r; if (!v) { uword i; for (i = 0; i < n_search_keys; i++) result_indices[i] = ~0; } bucket_mask = pow2_mask (h->log2_hash_size) & ~(QHASH_KEYS_PER_BUCKET - 1); hash_keys = h->hash_keys; k = search_keys; r = result_indices; n_left = n_search_keys; n_elts = h->n_elts; while (n_left >= 2) { u32 a0, b0, c0, bi0, match0, valid0; u32 a1, b1, c1, bi1, match1, valid1; uword k0, *h0; uword k1, *h1; k0 = k[0]; k1 = k[1]; /* Keys must be unique. */ ASSERT (k0 != k1); n_left -= 2; k += 2; a0 = a1 = h->hash_seeds[0]; b0 = b1 = h->hash_seeds[1]; c0 = c1 = h->hash_seeds[2]; a0 ^= k0; a1 ^= k1; #if uword_bits == 64 b0 ^= k0 >> 32; b1 ^= k1 >> 32; #endif hash_mix32_step_1 (a0, b0, c0); hash_mix32_step_1 (a1, b1, c1); hash_mix32_step_2 (a0, b0, c0); hash_mix32_step_2 (a1, b1, c1); hash_mix32_step_3 (a0, b0, c0); hash_mix32_step_3 (a1, b1, c1); bi0 = c0 & bucket_mask; bi1 = c1 & bucket_mask; h0 = hash_keys + bi0; h1 = hash_keys + bi1; /* Search two buckets. */ valid0 = qhash_get_valid_elt_mask (h, bi0); valid1 = qhash_get_valid_elt_mask (h, bi1); match0 = qhash_search_bucket (h0, k0, valid0); match1 = qhash_search_bucket (h1, k1, valid1); n_elts -= (match0 != 0) + (match1 != 0); if (PREDICT_FALSE (valid0 == QHASH_ALL_VALID || valid1 == QHASH_ALL_VALID)) goto slow_path2; valid0 ^= match0; qhash_set_valid_elt_mask (h, bi0, valid0); valid1 = bi0 == bi1 ? valid0 : valid1; valid1 ^= match1; qhash_set_valid_elt_mask (h, bi1, valid1); r[0] = match0 ? bi0 + qhash_min_log2 (match0) : ~0; r[1] = match1 ? bi1 + qhash_min_log2 (match1) : ~0; r += 2; continue; slow_path2: r[0] = unset_slow_path (v, elt_bytes, k0, bi0, valid0, match0, &n_elts); if (bi0 == bi1) { /* Search again in same bucket to test new overflow element. */ valid1 = qhash_get_valid_elt_mask (h, bi0); if (!match1) { match1 = qhash_search_bucket (h1, k1, valid1); n_elts -= (match1 != 0); } } r[1] = unset_slow_path (v, elt_bytes, k1, bi1, valid1, match1, &n_elts); r += 2; } while (n_left >= 1) { u32 a0, b0, c0, bi0, match0, valid0; uword k0, *h0; k0 = k[0]; n_left -= 1; k += 1; a0 = h->hash_seeds[0]; b0 = h->hash_seeds[1]; c0 = h->hash_seeds[2]; a0 ^= k0; #if uword_bits == 64 b0 ^= k0 >> 32; #endif hash_mix32 (a0, b0, c0); bi0 = c0 & bucket_mask; h0 = hash_keys + bi0; valid0 = qhash_get_valid_elt_mask (h, bi0); match0 = qhash_search_bucket (h0, k0, valid0); n_elts -= (match0 != 0); qhash_set_valid_elt_mask (h, bi0, valid0 ^ match0); r[0] = match0 ? bi0 + qhash_min_log2 (match0) : ~0; r += 1; if (PREDICT_FALSE (valid0 == QHASH_ALL_VALID)) r[-1] = unset_slow_path (v, elt_bytes, k0, bi0, valid0, match0, &n_elts); } h->n_elts = n_elts; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */