aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/xxhash.h
blob: ea1e21bf1440d0ce5a00aa74ea8a6aa58a87c1d6 (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
/*
 * 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.
 */
/*
  Original license for the code used to construct
  clib_xxhash(...).

  xxHash - Fast Hash algorithm
  Copyright (C) 2012-2014, Yann Collet.
  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following disclaimer
  in the documentation and/or other materials provided with the
  distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __included_xxhash_h__
#define __included_xxhash_h__

#define PRIME64_1 11400714785074694791ULL
#define PRIME64_2 14029467366897019727ULL
#define PRIME64_3  1609587929392839161ULL
#define PRIME64_4  9650029242287828579ULL
#define PRIME64_5  2870177450012600261ULL
#define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))

static inline u64
clib_xxhash (u64 key)
{
  u64 k1, h64;

  k1 = key;
  h64 = 0x9e3779b97f4a7c13LL + PRIME64_5 + 8;
  k1 *= PRIME64_2;
  k1 = XXH_rotl64 (k1, 31);
  k1 *= PRIME64_1;
  h64 ^= k1;
  h64 = XXH_rotl64 (h64, 27) * PRIME64_1 + PRIME64_4;

  h64 ^= h64 >> 33;
  h64 *= PRIME64_2;
  h64 ^= h64 >> 29;
  h64 *= PRIME64_3;
  h64 ^= h64 >> 32;
  return h64;
}

#endif /* __included_xxhash_h__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
&sep->ip.ip4, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 1; return 1; } else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, &transport_proto, &sep->hostname, unformat_ip4_address, &sep->ip.ip4, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 1; return 1; } else if (unformat (input, "%U://%U/%d", unformat_transport_proto, &transport_proto, unformat_ip6_address, &sep->ip.ip6, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 0; return 1; } else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto, &transport_proto, &sep->hostname, unformat_ip6_address, &sep->ip.ip6, &port)) { sep->transport_proto = transport_proto; sep->port = clib_host_to_net_u16 (port); sep->is_ip4 = 0; return 1; } else if (unformat (input, "%U://session/%lu", unformat_transport_proto, &transport_proto, &sep->parent_handle)) { sep->transport_proto = transport_proto; sep->ip.ip4.as_u32 = 1; /* ip need to be non zero in vnet */ return 1; } return 0; } static u8 *cache_uri; static session_endpoint_cfg_t *cache_sep; int parse_uri (char *uri, session_endpoint_cfg_t * sep) { unformat_input_t _input, *input = &_input; if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri))) { *sep = *cache_sep; return 0; } /* Make sure */ uri = (char *) format (0, "%s%c", uri, 0); /* Parse uri */ unformat_init_string (input, uri, strlen (uri)); if (!unformat (input, "%U", unformat_vnet_uri, sep)) { unformat_free (input); return VNET_API_ERROR_INVALID_VALUE; } unformat_free (input); vec_free (cache_uri); cache_uri = (u8 *) uri; if (cache_sep) clib_mem_free (cache_sep); cache_sep = clib_mem_alloc (sizeof (*sep)); *cache_sep = *sep; return 0; } int vnet_bind_uri (vnet_listen_args_t * a) { session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; int rv; rv = parse_uri (a->uri, &sep); if (rv) return rv; sep.app_wrk_index = 0; clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); return vnet_listen (a); } int vnet_unbind_uri (vnet_unlisten_args_t * a) { session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; application_t *app; session_t *listener; u32 table_index; int rv; if ((rv = parse_uri (a->uri, &sep))) return rv; app = application_get (a->app_index); if (!app) return VNET_API_ERROR_INVALID_VALUE; table_index = application_session_table (app, fib_ip_proto (!sep.is_ip4)); listener = session_lookup_listener (table_index, (session_endpoint_t *) & sep); if (!listener) return VNET_API_ERROR_ADDRESS_NOT_IN_USE; a->handle = listen_session_get_handle (listener); return vnet_unlisten (a); } int vnet_connect_uri (vnet_connect_args_t * a) { session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL; int rv; if ((rv = parse_uri (a->uri, &sep))) return rv; clib_memcpy (&a->sep_ext, &sep, sizeof (sep)); if ((rv = vnet_connect (a))) return rv; return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */