summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/bihash_12_4.h
blob: 3fdf1847861c9161cfea6507f4c96222dc8f320f (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */
#undef BIHASH_TYPE
#undef BIHASH_KVP_PER_PAGE
#undef BIHASH_32_64_SVM
#undef BIHASH_ENABLE_STATS
#undef BIHASH_KVP_AT_BUCKET_LEVEL
#undef BIHASH_LAZY_INSTANTIATE
#undef BIHASH_BUCKET_PREFETCH_CACHE_LINES
#undef BIHASH_USE_HEAP

#define BIHASH_TYPE			   _12_4
#define BIHASH_KVP_PER_PAGE		   4
#define BIHASH_KVP_AT_BUCKET_LEVEL	   0
#define BIHASH_LAZY_INSTANTIATE		   1
#define BIHASH_BUCKET_PREFETCH_CACHE_LINES 1
#define BIHASH_USE_HEAP			   1

#ifndef __included_bihash_12_4_h__
#define __included_bihash_12_4_h__

#include <vppinfra/crc32.h>
#include <vppinfra/heap.h>
#include <vppinfra/format.h>
#include <vppinfra/pool.h>
#include <vppinfra/xxhash.h>

typedef union
{
  struct
  {
    u32 key[3];
    u32 value;
  };
  u64 as_u64[2];
} clib_bihash_kv_12_4_t;

static inline void
clib_bihash_mark_free_12_4 (clib_bihash_kv_12_4_t *v)
{
  v->value = 0xFEEDFACE;
}

static inline int
clib_bihash_is_free_12_4 (const clib_bihash_kv_12_4_t *v)
{
  if (v->value == 0xFEEDFACE)
    return 1;
  return 0;
}

static inline u64
clib_bihash_hash_12_4 (const clib_bihash_kv_12_4_t *v)
{
#ifdef clib_crc32c_uses_intrinsics
  return clib_crc32c ((u8 *) v->key, 12);
#else
  u64 tmp = v->as_u64[0] ^ v->key[2];
  return clib_xxhash (tmp);
#endif
}

static inline u8 *
format_bihash_kvp_12_4 (u8 *s, va_list *args)
{
  clib_bihash_kv_12_4_t *v = va_arg (*args, clib_bihash_kv_12_4_t *);

  s = format (s, "key %u %u %u value %u", v->key[0], v->key[1], v->key[2],
	      v->value);
  return s;
}

static inline int
clib_bihash_key_compare_12_4 (u32 *a, u32 *b)
{
#if defined(CLIB_HAVE_VEC128)
  u32x4 v = (*(u32x4u *) a) ^ (*(u32x4u *) b);
  v[3] = 0;
  return u32x4_is_all_zero (v);
#else
  return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
#endif
}

#undef __included_bihash_template_h__
#include <vppinfra/bihash_template.h>

#endif /* __included_bihash_12_4_h__ */
/span> = &p2p_main; p2p_key_t p2pe_key; uword *p; create_p2pe_key (&p2pe_key, parent_if_index, client_mac); p = hash_get_mem (p2pm->p2p_ethernet_by_key, &p2pe_key); if (p) return p[0]; return ~0; } int p2p_ethernet_add_del (vlib_main_t * vm, u32 parent_if_index, u8 * client_mac, u32 p2pe_subif_id, int is_add, u32 * p2pe_if_index) { vnet_main_t *vnm = vnet_get_main (); p2p_ethernet_main_t *p2pm = &p2p_main; vnet_interface_main_t *im = &vnm->interface_main; u32 p2pe_sw_if_index = ~0; p2pe_sw_if_index = p2p_ethernet_lookup (parent_if_index, client_mac); if (p2pe_if_index) *p2pe_if_index = ~0; if (is_add) { if (p2pe_sw_if_index == ~0) { vnet_hw_interface_t *hi; hi = vnet_get_hw_interface (vnm, parent_if_index); if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) return VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED; u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id; uword *p; p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key); if (p) { if (CLIB_DEBUG > 0) clib_warning ("p2p ethernet sub-interface on sw_if_index %d with sub id %d already exists\n", hi->sw_if_index, p2pe_subif_id); return VNET_API_ERROR_SUBIF_ALREADY_EXISTS; } vnet_sw_interface_t template = { .type = VNET_SW_INTERFACE_TYPE_P2P, .flood_class = VNET_FLOOD_CLASS_NORMAL, .sup_sw_if_index = hi->sw_if_index, .sub.id = p2pe_subif_id }; clib_memcpy (template.p2p.client_mac, client_mac, sizeof (template.p2p.client_mac)); if (vnet_create_sw_interface (vnm, &template, &p2pe_sw_if_index)) return VNET_API_ERROR_SUBIF_CREATE_FAILED; /* Allocate counters for this interface. */ { u32 i; vnet_interface_counter_lock (im); for (i = 0; i < vec_len (im->sw_if_counters); i++) { vlib_validate_simple_counter (&im->sw_if_counters[i], p2pe_sw_if_index); vlib_zero_simple_counter (&im->sw_if_counters[i], p2pe_sw_if_index); } for (i = 0; i < vec_len (im->combined_sw_if_counters); i++) { vlib_validate_combined_counter (&im->combined_sw_if_counters [i], p2pe_sw_if_index); vlib_zero_combined_counter (&im->combined_sw_if_counters[i], p2pe_sw_if_index); } vnet_interface_counter_unlock (im); } vnet_interface_main_t *im = &vnm->interface_main; sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id; u64 *kp = clib_mem_alloc (sizeof (*kp)); *kp = sup_and_sub_key; hash_set (hi->sub_interface_sw_if_index_by_id, p2pe_subif_id, p2pe_sw_if_index); hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, p2pe_sw_if_index); p2p_key_t *p_p2pe_key; p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key)); create_p2pe_key (p_p2pe_key, parent_if_index, client_mac); hash_set_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key, p2pe_sw_if_index); if (p2pe_if_index) *p2pe_if_index = p2pe_sw_if_index; vec_validate (p2pm->p2p_ethernet_by_sw_if_index, parent_if_index); if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 0) { vnet_feature_enable_disable ("device-input", "p2p-ethernet-input", parent_if_index, 1, 0, 0); /* Set promiscuous mode on the l2 interface */ ethernet_set_flags (vnm, parent_if_index, ETHERNET_INTERFACE_FLAG_ACCEPT_ALL); } p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]++; /* set the interface mode */ set_int_l2_mode (vm, vnm, MODE_L3, p2pe_subif_id, 0, L2_BD_PORT_TYPE_NORMAL, 0, 0); return 0; } return VNET_API_ERROR_SUBIF_ALREADY_EXISTS; } else { if (p2pe_sw_if_index == ~0) return VNET_API_ERROR_SUBIF_DOESNT_EXIST; else { int rv = 0; rv = vnet_delete_sub_interface (p2pe_sw_if_index); if (!rv) { vec_validate (p2pm->p2p_ethernet_by_sw_if_index, parent_if_index); if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 1) { vnet_feature_enable_disable ("device-input", "p2p-ethernet-input", parent_if_index, 0, 0, 0); /* Disable promiscuous mode on the l2 interface */ ethernet_set_flags (vnm, parent_if_index, 0); } p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]--; /* Remove p2p_ethernet from hash map */ p2p_key_t *p_p2pe_key; p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key)); create_p2pe_key (p_p2pe_key, parent_if_index, client_mac); hash_unset_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key); } return rv; } } } static clib_error_t * vnet_p2p_ethernet_add_del (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { vnet_main_t *vnm = vnet_get_main (); int is_add = 1; int remote_mac = 0; u32 hw_if_index = ~0; u32 sub_id = ~0; u8 client_mac[6]; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) ; else if (unformat (input, "%U", unformat_ethernet_address, &client_mac)) remote_mac = 1; else if (unformat (input, "sub-id %d", &sub_id)) ; else if (unformat (input, "del")) is_add = 0; else break; } if (hw_if_index == ~0) return clib_error_return (0, "Please specify parent interface ..."); if (!remote_mac) return clib_error_return (0, "Please specify client MAC address ..."); if (sub_id == ~0 && is_add) return clib_error_return (0, "Please specify sub-interface id ..."); u32 rv; rv = p2p_ethernet_add_del (vm, hw_if_index, client_mac, sub_id, is_add, 0); switch (rv) { case VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED: return clib_error_return (0, "not allowed as parent interface belongs to a BondEthernet interface"); case -1: return clib_error_return (0, "p2p ethernet for given parent interface and client mac already exists"); case -2: return clib_error_return (0, "couldn't create p2p ethernet subinterface"); case -3: return clib_error_return (0, "p2p ethernet for given parent interface and client mac doesn't exist"); default: break; } return 0; } VLIB_CLI_COMMAND (p2p_ethernet_add_del_command, static) = { .path = "p2p_ethernet ",.function = vnet_p2p_ethernet_add_del,.short_help = "p2p_ethernet <intfc> <mac-address> [sub-id <id> | del]",}; static clib_error_t * p2p_ethernet_init (vlib_main_t * vm) { p2p_ethernet_main_t *p2pm = &p2p_main; p2pm->vlib_main = vm; p2pm->vnet_main = vnet_get_main (); p2pm->p2p_ethernet_by_key = hash_create_mem (0, sizeof (p2p_key_t), sizeof (uword)); return 0; } VLIB_INIT_FUNCTION (p2p_ethernet_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */