aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/cuckoo_template.h
blob: c3b2bc98aaa1e75f168b0b0b1b10c8f08d723f52 (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
/*
 * srv6_t_m_gtp4_d.c
 *
 * Copyright (c) 2019 Arrcus Inc 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 <vnet/vnet.h>
#include <vnet/adj/adj.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <srv6-mobile/mobile.h>

srv6_t_main_v4_decap_t srv6_t_main_v4_decap;

static void
clb_dpo_lock_srv6_t_m_gtp4_d (dpo_id_t * dpo)
{
}

static void
clb_dpo_unlock_srv6_t_m_gtp4_d (dpo_id_t * dpo)
{
}

static u8 *
clb_dpo_format_srv6_t_m_gtp4_d (u8 * s, va_list * args)
{
  index_t index = va_arg (*args, index_t);
  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);

  return (format (s, "SR: dynamic_proxy_index:[%u]", index));
}

const static dpo_vft_t dpo_vft = {
  .dv_lock = clb_dpo_lock_srv6_t_m_gtp4_d,
  .dv_unlock = clb_dpo_unlock_srv6_t_m_gtp4_d,
  .dv_format = clb_dpo_format_srv6_t_m_gtp4_d,
};

const static char *const srv6_t_m_gtp4_d_nodes[] = {
  "srv6-t-m-gtp4-d",
  NULL,
};

const static char *const srv6_t_m_gtp4_d_v6_nodes[] = {
  "error-drop",
  NULL,
};

const static char *const *const dpo_nodes[DPO_PROTO_NUM] = {
  [DPO_PROTO_IP6] = srv6_t_m_gtp4_d_v6_nodes,
  [DPO_PROTO_IP4] = srv6_t_m_gtp4_d_nodes,
};

static u8 fn_name[] = "SRv6-T.M.GTP4.D-plugin";
static u8 keyword_str[] = "t.m.gtp4.d";
static u8 def_str[] =
  "Transit function with decapsulation for IPv4/GTP tunnel";
static u8 param_str[] =
  "<sr-prefix>/<sr-prefixlen> v6src_prefix <v6src_prefix>/<prefixlen> [nhtype <nhtype>]";

static u8 *
clb_format_srv6_t_m_gtp4_d (u8 * s, va_list * args)
{
  srv6_end_gtp4_param_t *ls_mem = va_arg (*args, void *);

  s = format (s, "SRv6 T.M.GTP4.D\n\t");

  s =
    format (s, "SR Prefix: %U/%d, ", format_ip6_address, &ls_mem->sr_prefix,
	    ls_mem->sr_prefixlen);

  s =
    format (s, "v6src Prefix: %U/%d", format_ip6_address,
	    &ls_mem->v6src_prefix, ls_mem->v6src_prefixlen);

  if (ls_mem->nhtype != SRV6_NHTYPE_NONE)
    {
      if (ls_mem->nhtype == SRV6_NHTYPE_IPV4)
	s = format (s, ", NHType IPv4\n");
      else if (ls_mem->nhtype == SRV6_NHTYPE_IPV6)
	s = format (s, ", NHType IPv6\n");
      else if (ls_mem->nhtype == SRV6_NHTYPE_NON_IP)
	s = format (s, ", NHType Non-IP\n");
      else
	s = format (s, ", NHType Unknow(%d)\n", ls_mem->nhtype);
    }
  else
    s = format (s, "\n");

  return s;
}

static uword
clb_unformat_srv6_t_m_gtp4_d (unformat_input_t * input, va_list * args)
{
  void **plugin_mem_p = va_arg (*args, void **);
  srv6_end_gtp4_param_t *ls_mem;
  ip6_address_t sr_prefix;
  u32 sr_prefixlen;
  ip6_address_t v6src_prefix;
  u32 v6src_prefixlen;
  u8 nhtype;

  if (unformat (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv4",
		unformat_ip6_address, &sr_prefix, &sr_prefixlen,
		unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
    {
      nhtype = SRV6_NHTYPE_IPV4;
    }
  else
    if (unformat
	(input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype ipv6",
	 unformat_ip6_address, &sr_prefix, &sr_prefixlen,
	 unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
    {
      nhtype = SRV6_NHTYPE_IPV6;
    }
  else
    if (unformat
	(input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d nhtype non-ip",
	 unformat_ip6_address, &sr_prefix, &sr_prefixlen,
	 unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
    {
      nhtype = SRV6_NHTYPE_NON_IP;
    }
  else if (unformat (input, "t.m.gtp4.d %U/%d v6src_prefix %U/%d",
		     unformat_ip6_address, &sr_prefix, &sr_prefixlen,
		     unformat_ip6_address, &v6src_prefix, &v6src_prefixlen))
    {
      nhtype = SRV6_NHTYPE_NONE;
    }
  else
    {
      return 0;
    }

  ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
  clib_memset (ls_mem, 0, sizeof *ls_mem);
  *plugin_mem_p = ls_mem;

  ls_mem->sr_prefix = sr_prefix;
  ls_mem->sr_prefixlen = sr_prefixlen;

  ls_mem->v6src_prefix = v6src_prefix;
  ls_mem->v6src_prefixlen = v6src_prefixlen;

  ls_mem->nhtype = nhtype;

  return 1;
}

static int
clb_creation_srv6_t_m_gtp4_d (ip6_sr_policy_t * sr_policy)
{
  return 0;
}

static int
clb_removal_srv6_t_m_gtp4_d (ip6_sr_policy_t * sr_policy)
{
  srv6_end_gtp4_param_t *ls_mem;

  ls_mem = (srv6_end_gtp4_param_t *) sr_policy->plugin_mem;

  clib_mem_free (ls_mem);

  return 0;
}

static clib_error_t *
srv6_t_m_gtp4_d_init (vlib_main_t * vm)
{
  srv6_t_main_v4_decap_t *sm = &srv6_t_main_v4_decap;
  ip6_header_t *ip6;
  dpo_type_t dpo_type;
  vlib_node_t *node;
  int rc;

  sm->vlib_main = vm;
  sm->vnet_main = vnet_get_main ();

  node = vlib_get_node_by_name (vm, (u8 *) "srv6-t-m-gtp4-d");
  sm->t_m_gtp4_d_node_index = node->index;

  node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
  sm->error_node_index = node->index;

  ip6 = &sm->cache_hdr;

  clib_memset_u8 (ip6, 0, sizeof (ip6_header_t));

  // IPv6 header (default)
  ip6->ip_version_traffic_class_and_flow_label = 0x60;
  ip6->hop_limit = 64;
  ip6->protocol = IP_PROTOCOL_IPV6;

  dpo_type = dpo_register_new_type (&dpo_vft, dpo_nodes);

  rc = sr_policy_register_function (vm, fn_name, keyword_str, def_str, param_str, 128,	//prefix len
				    &dpo_type,
				    clb_format_srv6_t_m_gtp4_d,
				    clb_unformat_srv6_t_m_gtp4_d,
				    clb_creation_srv6_t_m_gtp4_d,
				    clb_removal_srv6_t_m_gtp4_d);
  if (rc < 0)
    clib_error_return (0, "SRv6 Transit GTP4.D Policy function"
		       "couldn't be registered");
  return 0;
}

/* *INDENT-OFF* */
VNET_FEATURE_INIT (srv6_t_m_gtp4_d, static) =
{
  .arc_name = "ip4-unicast",
  .node_name = "srv6-t-m-gtp4-d",
  .runs_before = 0,
};

VLIB_INIT_FUNCTION (srv6_t_m_gtp4_d_init);
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
ass="o">* *to_be_freed; /** hash table name */ const char *name; /** pool of cuckoo paths (reused when doing bfd search) */ clib_cuckoo_path_t *paths; /** * vector used as queue when doing cuckoo path searches - holds offsets * in paths pool */ uword *bfs_search_queue; /** * writer lock - whether this lock is taken or not has zero effect on * readers */ clib_spinlock_t writer_lock; /** caller context passed to callback with garbage notification */ void *garbage_ctx; /** * garbage notify function - called when some garbage needs to be collected * in main thread while other threads are stopped */ void (*garbage_callback) (struct CV (clib_cuckoo) * h, void *garbage_ctx); #if CLIB_CUCKOO_DEBUG_COUNTERS u64 steps_exceeded; u64 bfs_queue_emptied; u64 fast_adds; u64 slow_adds; u64 rehashes; #endif } CVT (clib_cuckoo); void CV (clib_cuckoo_init) (CVT (clib_cuckoo) * h, const char *name, uword nbuckets, void (*garbage_callback) (CVT (clib_cuckoo) *, void *), void *garbage_ctx); void CV (clib_cuckoo_garbage_collect) (CVT (clib_cuckoo) * h); void CV (clib_cuckoo_free) (CVT (clib_cuckoo) * h); int CV (clib_cuckoo_add_del) (CVT (clib_cuckoo) * h, CVT (clib_cuckoo_kv) * add_v, int is_add); int CV (clib_cuckoo_search) (CVT (clib_cuckoo) * h, CVT (clib_cuckoo_kv) * search_v, CVT (clib_cuckoo_kv) * return_v); void CV (clib_cuckoo_foreach_key_value_pair) (CVT (clib_cuckoo) * h, void *callback, void *arg); float CV (clib_cuckoo_calc_load) (CVT (clib_cuckoo) * h); format_function_t CV (format_cuckoo); format_function_t CV (format_cuckoo_kvp); always_inline u8 clib_cuckoo_reduce_hash (u64 hash) { u32 v32 = ((u32) hash) ^ ((u32) (hash >> 32)); u16 v16 = ((u16) v32) ^ ((u16) (v32 >> 16)); u8 v8 = ((u8) v16) ^ ((u8) (v16 >> 8)); return v8; } always_inline u64 clib_cuckoo_get_other_bucket (u64 nbuckets, u64 bucket, u8 reduced_hash) { u64 mask = (nbuckets - 1); return (bucket ^ ((reduced_hash + 1) * 0xc6a4a7935bd1e995)) & mask; } always_inline clib_cuckoo_lookup_info_t CV (clib_cuckoo_calc_lookup) (CVT (clib_cuckoo_bucket) * buckets, u64 hash) { clib_cuckoo_lookup_info_t lookup; u64 nbuckets = vec_len (buckets); u64 mask = (nbuckets - 1); lookup.bucket1 = hash & mask; #if CLIB_CUCKOO_OPTIMIZE_PREFETCH CLIB_PREFETCH (vec_elt_at_index (buckets, lookup.bucket1), sizeof (*buckets), LOAD); #endif u8 reduced_hash = clib_cuckoo_reduce_hash (hash); lookup.bucket2 = clib_cuckoo_get_other_bucket (nbuckets, lookup.bucket1, reduced_hash); #if CLIB_CUCKOO_OPTIMIZE_PREFETCH CLIB_PREFETCH (vec_elt_at_index (buckets, lookup.bucket2), sizeof (*buckets), LOAD); #endif lookup.reduced_hash = reduced_hash; ASSERT (lookup.bucket1 < nbuckets); ASSERT (lookup.bucket2 < nbuckets); return lookup; } /** * search for key within bucket */ always_inline int CV (clib_cuckoo_bucket_search) (CVT (clib_cuckoo_bucket) * b, CVT (clib_cuckoo_kv) * kvp, u8 reduced_hash) { clib_cuckoo_bucket_aux_t bucket_aux; u8 writer_flag; do { bucket_aux = b->aux; writer_flag = clib_cuckoo_bucket_aux_get_writer_flag (bucket_aux); } while (PREDICT_FALSE (writer_flag)); /* loop while writer flag is set */ int i; #if CLIB_CUCKOO_OPTIMIZE_USE_COUNT_LIMITS_SEARCH const int use_count = clib_cuckoo_bucket_aux_get_use_count (bucket_aux); #endif /* *INDENT-OFF* */ clib_cuckoo_bucket_foreach_idx_unrolled (i, { #if CLIB_CUCKOO_OPTIMIZE_USE_COUNT_LIMITS_SEARCH if (i > use_count) { break; } #endif if ( #if CLIB_CUCKOO_OPTIMIZE_CMP_REDUCED_HASH reduced_hash == b->reduced_hashes[i] && #endif 0 == memcmp (&kvp->key, &b->elts[i].key, sizeof (kvp->key))) { kvp->value = b->elts[i].value; clib_cuckoo_bucket_aux_t bucket_aux2 = b->aux; if (PREDICT_TRUE (clib_cuckoo_bucket_aux_get_version (bucket_aux) == clib_cuckoo_bucket_aux_get_version (bucket_aux2))) { /* yay, fresh data */ return CLIB_CUCKOO_ERROR_SUCCESS; } else { /* oops, modification detected */ return CLIB_CUCKOO_ERROR_AGAIN; } } }); /* *INDENT-ON* */ return CLIB_CUCKOO_ERROR_NOT_FOUND; } always_inline int CV (clib_cuckoo_search_inline) (CVT (clib_cuckoo) * h, CVT (clib_cuckoo_kv) * kvp) { clib_cuckoo_lookup_info_t lookup; int rv; u64 hash = CV (clib_cuckoo_hash) (kvp); CVT (clib_cuckoo_bucket) * buckets; again: buckets = h->buckets; lookup = CV (clib_cuckoo_calc_lookup) (buckets, hash); do { rv = CV (clib_cuckoo_bucket_search) (vec_elt_at_index (buckets, lookup.bucket1), kvp, lookup.reduced_hash); } while (PREDICT_FALSE (CLIB_CUCKOO_ERROR_AGAIN == rv)); if (CLIB_CUCKOO_ERROR_SUCCESS == rv) { return CLIB_CUCKOO_ERROR_SUCCESS; } rv = CV (clib_cuckoo_bucket_search) (vec_elt_at_index (buckets, lookup.bucket2), kvp, lookup.reduced_hash); if (PREDICT_FALSE (CLIB_CUCKOO_ERROR_AGAIN == rv)) { /* * change to 2nd bucket could bump the item to 1st bucket and the bucket * indexes might not even be valid anymore - restart the search */ goto again; } return rv; } #endif /* __included_cuckoo_template_h__ */ /** @endcond */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */