/* * 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-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 #include #include #include /* for clib_arch_is_big_endian */ always_inline void zero_pair (hash_t * h, hash_pair_t * p) { memset (p, 0, hash_pair_bytes (h)); } always_inline void init_pair (hash_t * h, hash_pair_t * p) { memset (p->value, ~0, hash_value_bytes (h)); } always_inline hash_pair_union_t * get_pair (void *v, uword i) { hash_t *h = hash_header (v); hash_pair_t *p; ASSERT (i < vec_len (v)); p = v; p += i << h->log2_pair_size; return (hash_pair_union_t *) p; } always_inline void set_is_user (void *v, uword i, uword is_user) { hash_t *h = hash_header (v); uword i0 = i / BITS (h->is_user[0]); uword i1 = (uword) 1 << (i % BITS (h->is_user[0])); if (is_user) h->is_user[i0] |= i1; else h->is_user[i0] &= ~i1; } static u8 *hash_format_pair_default (u8 * s, va_list * args); #if uword_bits == 64 static inline u64 zap64 (u64 x, word n) { #define _(n) (((u64) 1 << (u64) (8*(n))) - (u64) 1) static u64 masks_little_endian[] = { 0, _(1), _(2), _(3), _(4), _(5), _(6), _(7), }; static u64 masks_big_endian[] = { 0, ~_(7), ~_(6), ~_(5), ~_(4), ~_(3), ~_(2), ~_(1), }; #undef _ if (clib_arch_is_big_endian) return x & masks_big_endian[n]; else return x & masks_little_endian[n]; } static inline u64 hash_memory64 (void *p, word n_bytes, u64 state) { u64 *q = p; u64 a, b, c, n; a = b = 0x9e3779b97f4a7c13LL; c = state; n = n_bytes; while (n >= 3 * sizeof (u64)) { a += clib_mem_unaligned (q + 0, u64); b += clib_mem_unaligned (q + 1, u64); c += clib_mem_unaligned (q + 2, u64); hash_mix64 (a, b, c); n -= 3 * sizeof (u64); q += 3; } c += n_bytes; switch (n / sizeof (u64)) { case 2: a += clib_mem_unaligned (q + 0, u64); b += clib_mem_unaligned (q + 1, u64); if (n % sizeof (u64)) c += zap64 (clib_mem_unaligned (q + 2, u64), n % sizeof (u64)) << 8; break; case 1: a += clib_mem_unaligned (q + 0, u64); if (n % sizeof (u64)) b += zap64 (clib_mem_unaligned (q + 1, u64), n % sizeof (u64)); break; case 0: if (n % sizeof (u64)) a += zap64 (clib_mem_unaligned (q + 0, u64), n % sizeof (u64)); break; } hash_mix64 (a, b, c); return c; } #else /* if uword_bits == 64 */ static inline u32 zap32 (u32 x, word n) { #define _(n) (((u32) 1 << (u32) (8*(n))) - (u32) 1) static u32 masks_little_endian[] = { 0, _(1), _(2), _(3), }; static u32 masks_big_endian[] = { 0, ~_(3), ~_(2), ~_(1), }; #undef _ if (clib_arch_is_big_endian) return x & masks_big_endian[n]; else return x & masks_little_endian[n]; } static inline u32 hash_memory32 (void *p, word n_bytes, u32 state) { u32 *q = p; u32 a, b, c, n; a = b = 0x9e3779b9; c = state; n = n_bytes; while (n >= 3 * sizeof (u32)) { a += clib_mem_unaligned (q + 0, u32); b += clib_mem_unaligned (q + 1, u32); c += clib_mem_unaligned (q + 2, u32); hash_mix32 (a, b, c); n -= 3 * sizeof (u32); q += 3; } c += n_bytes; switch (n / sizeof (u32)) { case 2: a += clib_mem_unaligned (q + 0, u32); b += clib_mem_unaligned (q + 1, u32); if (n % sizeof (u32)) c += zap32 (clib_mem_unaligned (q + 2, u32), n % sizeof (u32)) << 8; break; case 1: a += clib_mem_unaligned (q + 0, u32); if (n % sizeof (u32)) b += zap32 (clib_mem_unaligned (q + 1, u32), n % sizeof (u32)); break; case 0: if (n % sizeof (u32)) a += zap32 (clib_mem_unaligned (q + 0, u32), n % sizeof (u32)); break; } hash_mix32 (a, b, c); return c; } #endif uword hash_memory (void *p, word n_bytes, uword state) { uword *q = p; #if uword_bits == 64 return hash_memory64 (q, n_bytes, state); #else return hash_memory32 (q, n_bytes, state); #endif } #if uword_bits == 64 always_inline uword hash_uword (uword x) { u64 a, b, c; a = b = 0x9e3779b97f4a7c13LL; c = 0; a += x; hash_mix64 (a, b, c); return c; } #else always_inline uword hash_uword (uword x) { u32 a, b, c; a = b = 0x9e3779b9; c = 0; a += x; hash_mix32 (a, b, c); return c; } #endif /* Call sum function. Hash code will be sum function value modulo the prime length of the hash table. */ always_inline uword key_sum (hash_t * h, uword key) { uword sum; switch (pointer_to_uword ((void *) h->key_sum)) { case KEY_FUNC_NONE: sum = hash_uword (key); break; case KEY_FUNC_POINTER_UWORD: sum = hash_uword (*uword_to_pointer (key, uword *)); break; case KEY_FUNC_POINTER_U32: sum = hash_uword (*uword_to_pointer (key, u32 *)); break; case KEY_FUNC_STRING: sum = string_key_sum (h, key); break; default: sum = h->key_sum (h, key); break; } return sum; } always_inline uword key_equal1 (hash_t * h, uword key1, uword key2, uword e) { switch (pointer_to_uword ((void *) h->key_equal)) { case KEY_FUNC_NONE: break; case KEY_FUNC_POINTER_UWORD: e = *uword_to_pointer (key1, uword *) == *uword_to_pointer (key2, uword *); break; case KEY_FUNC_POINTER_U32: e = *uword_to_pointer (key1, u32 *) == *uword_to_pointer (key2, u32 *); break; case KEY_FUNC_STRING: e = string_key_equal (h, key1, key2); break; default: e = h->key_equal (h, key1, key2); break; } return e; } /* Compares two keys: returns 1 if equal, 0 if not. */ always_inline uword key_equal (hash_t * h, uword key1, uword key2) { uword e = key1 == key2; if (CLIB_DEBUG > 0 && key1 == key2) ASSERT (key_equal1 (h, key1, key2, e)); if (!e) e = key_equal1 (h, key1, key2, e); return e; } static hash_pair_union_t * get_indirect (void *v, hash_pair_indirect_t * pi, uword key) { hash_t *h = hash_header (v); hash_pair_t *p0, *p1; p0 = p1 = pi->pairs;
/*
 * Copyright (c) 2016 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.
 */

#ifndef __LISP_GPE_TENANT_H__
#define __LISP_GPE_TENANT_H__

#include <vnet/lisp-gpe/lisp_gpe.h>

/**
 * Refernece counting lock types on the tenant.
 * When all of these counters drop to zero, we no longer need the tenant.
 */
typedef enum lisp_gpe_tenant_lock_t_
{
  LISP_GPE_TENANT_LOCK_L2_IFACE,
  LISP_GPE_TENANT_LOCK_L3_IFACE,
  LISP_GPE_TENANT_LOCK_NUM,
} lisp_gpe_tenant_lock_t;

/**
 * @brief Representation of the data associated with a LISP overlay tenant
 *
 * This object exists to manage the shared resources of the L2 and L3 interface
 * of a given tenant.
 */
typedef struct lisp_gpe_tenant_t_
{
  /**
   * The VNI is the identifier of the tenant
   */
  u32 lt_vni;

  /**
   * The tenant can have both L2 and L3 services enabled.
   */
  u32 lt_table_id;
  u32 lt_bd_id;

  /**
   * The number of locks on the tenant's L3 interface.
   */
  u32 lt_locks[LISP_GPE_TENANT_LOCK_NUM];

  /**
   * The L3 SW interface index
   */
  u32 lt_l3_sw_if_index;

  /**
   * The L2 SW interface index
   */
  u32 lt_l2_sw_if_index;

} lisp_gpe_tenant_t;

extern u32 lisp_gpe_tenant_find_or_create (u32 vni);

extern u32 lisp_gpe_tenant_l3_iface_add_or_lock (u32 vni, u32 vrf,
						 u8 with_default_route);
extern void lisp_gpe_tenant_l3_iface_unlock (u32 vni);

extern u32 lisp_gpe_tenant_l2_iface_add_or_lock (u32 vni, u32 vrf);
extern void lisp_gpe_tenant_l2_iface_unlock (u32 vni);

extern const lisp_gpe_tenant_t *lisp_gpe_tenant_get (u32 index);

extern void lisp_gpe_tenant_flush (void);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */

#endif
); done: return error; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */