summaryrefslogtreecommitdiffstats
path: root/src/vnet/map/map_api.c
blob: 7febeb3d7a3ee115994e72b8341bddab1981f13f (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
/*
 *------------------------------------------------------------------
 * map_api.c - vnet map api
 *
 * 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.
 *------------------------------------------------------------------
 */

#include <vnet/vnet.h>
#include <vlibmemory/api.h>

#include "map.h"
#include <vnet/api_errno.h>
#include <vnet/ip/ip.h>
#include <vnet/fib/fib_table.h>
#include <vnet/vnet_msg_enum.h>

#define vl_typedefs		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_typedefs

#define vl_endianfun		/* define message structures */
#include <vnet/vnet_all_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include <vnet/vnet_all_api_h.h>
#undef vl_printfun

#include <vlibapi/api_helper_macros.h>

#define foreach_vpe_api_msg			\
_(MAP_ADD_DOMAIN, map_add_domain)		\
_(MAP_DEL_DOMAIN, map_del_domain)		\
_(MAP_ADD_DEL_RULE, map_add_del_rule)		\
_(MAP_DOMAIN_DUMP, map_domain_dump)		\
_(MAP_RULE_DUMP, map_rule_dump)			\
_(MAP_SUMMARY_STATS, map_summary_stats)

static void
vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
{
  vl_api_map_add_domain_reply_t *rmp;
  int rv = 0;
  u32 index;
  u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
  rv =
    map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
		       (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
		       (ip6_address_t *) & mp->ip6_src,
		       mp->ip6_src_prefix_len, mp->ea_bits_len,
		       mp->psid_offset, mp->psid_length, &index,
		       ntohs (mp->mtu), flags);

  /* *INDENT-OFF* */
  REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
  ({
    rmp->index = ntohl(index);
  }));
  /* *INDENT-ON* */
}

static void
vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
{
  vl_api_map_del_domain_reply_t *rmp;
  int rv = 0;

  rv = map_delete_domain (ntohl (mp->index));

  REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
}

static void
vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
{
  vl_api_map_del_domain_reply_t *rmp;
  int rv = 0;

  rv =
    map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
		      (ip6_address_t *) mp->ip6_dst, mp->is_add);

  REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
}

static void
vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
{
  vl_api_map_domain_details_t *rmp;
  map_main_t *mm = &map_main;
  map_domain_t *d;
  unix_shared_memory_queue_t *q;

  if (pool_elts (mm->domains) == 0)
    return;

  q = vl_api_client_index_to_input_queue (mp->client_index);
  if (q == 0)
    {
      return;
    }

  /* *INDENT-OFF* */
  pool_foreach(d, mm->domains,
  ({
    /* Make sure every field is initiated (or don't skip the memset()) */
    rmp = vl_msg_api_alloc (sizeof (*rmp));
    rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
    rmp->domain_index = htonl(d - mm->domains);
    rmp->ea_bits_len = d->ea_bits_len;
    rmp->psid_offset = d->psid_offset;
    rmp->psid_length = d->psid_length;
    clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
    rmp->ip4_prefix_len = d->ip4_prefix_len;
    clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
    rmp->ip6_prefix_len = d->ip6_prefix_len;
    clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
    rmp->ip6_src_len = d->ip6_src_len;
    rmp->mtu = htons(d->mtu);
    rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
    rmp->context = mp->context;

    vl_msg_api_send_shmem (q, (u8 *)&rmp);
  }));
  /* *INDENT-ON* */
}

static void
vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
{
  unix_shared_memory_queue_t *q;
  u16 i;
  ip6_address_t dst;
  vl_api_map_rule_details_t *rmp;
  map_main_t *mm = &map_main;
  u32 domain_index = ntohl (mp->domain_index);
  map_domain_t *d;

  if (pool_elts (mm->domains) == 0)
    return;

  d = pool_elt_at_index (mm->domains, domain_index);
  if (!d || !d->rules)
    {
      return;
    }

  q = vl_api_client_index_to_input_queue (mp->client_index);
  if (q == 0)
    {
      return;
    }

  for (i = 0; i < (0x1 << d->psid_length); i++)
    {
      dst = d->rules[i];
      if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
	{
	  continue;
	}
      rmp = vl_msg_api_alloc (sizeof (*rmp));
      memset (rmp, 0, sizeof (*rmp));
      rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
      rmp->psid = htons (i);
      clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
      rmp->context = mp->context;
      vl_msg_api_send_shmem (q, (u8 *) & rmp);
    }
}

static void
vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
{
  vl_api_map_summary_stats_reply_t *rmp;
  vlib_combined_counter_main_t *cm;
  vlib_counter_t v;
  int i, which;
  u64 total_pkts[VLIB_N_RX_TX];
  u64 total_bytes[VLIB_N_RX_TX];
  map_main_t *mm = &map_main;
  unix_shared_memory_queue_t *q =
    vl_api_client_index_to_input_queue (mp->client_index);

  if (!q)
    return;

  rmp = vl_msg_api_alloc (sizeof (*rmp));
  rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
  rmp->context = mp->context;
  rmp->retval = 0;

  memset (total_pkts, 0, sizeof (total_pkts));
  memset (total_bytes, 0, sizeof (total_bytes));

  map_domain_counter_lock (mm);
  vec_foreach (cm, mm->domain_counters)
  {
    which = cm - mm->domain_counters;

    for (i = 0; i < vec_len (cm->maxi); i++)
      {
	vlib_get_combined_counter (cm, i, &v);
	total_pkts[which] += v.packets;
	total_bytes[which] += v.bytes;
      }
  }

  map_domain_counter_unlock (mm);

  /* Note: in network byte order! */
  rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
    clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
  rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
    clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
  rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
    clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
  rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
    clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
  rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
  rmp->total_ip4_fragments = 0;	// Not yet implemented. Should be a simple counter.
  rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
    clib_host_to_net_u64 (map_error_counter_get
			  (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
  rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
    clib_host_to_net_u64 (map_error_counter_get
			  (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));

  vl_msg_api_send_shmem (q, (u8 *) & rmp);
}

/*
 * vpe_api_hookup
 * Add vpe's API message handlers to the table.
 * vlib has alread mapped shared memory and
 * added the client registration handlers.
 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
 */
#define vl_msg_name_crc_list
#include <vnet/vnet_all_api_h.h>
#undef vl_msg_name_crc_list

static void
setup_message_id_table (api_main_t * am)
{
#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
  foreach_vl_msg_name_crc_map;
#undef _
}

static clib_error_t *
map_api_hookup (vlib_main_t * vm)
{
  api_main_t *am = &api_main;

#define _(N,n)                                                  \
    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
                           vl_api_##n##_t_handler,              \
                           vl_noop_handler,                     \
                           vl_api_##n##_t_endian,               \
                           vl_api_##n##_t_print,                \
                           sizeof(vl_api_##n##_t), 1);
  foreach_vpe_api_msg;
#undef _

  /*
   * Set up the (msg_name, crc, message-id) table
   */
  setup_message_id_table (am);

  return 0;
}

VLIB_API_INIT_FUNCTION (map_api_hookup);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
ss="p">, 1); if (FA_SESSION_BOGUS_INDEX == pw->fa_conn_list_head[list_id]) { pw->fa_conn_list_head[list_id] = sess_id.session_index; /* set the head expiry time because it is the first element */ pw->fa_conn_list_head_expiry_time[list_id] = now + fa_session_get_timeout (am, sess); } } static int acl_fa_conn_list_delete_session (acl_main_t * am, fa_full_session_id_t sess_id, u64 now) { uword thread_index = os_get_thread_index (); acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index]; if (thread_index != sess_id.thread_index) { /* If another thread attempts to delete the session, fail it. */ #ifdef FA_NODE_VERBOSE_DEBUG clib_warning ("thread id in key %d != curr thread index, not deleting"); #endif return 0; } fa_session_t *sess = get_session_ptr (am, sess_id.thread_index, sess_id.session_index); u64 next_expiry_time = ~0ULL; /* we should never try to delete the session with another thread index */ if (sess->thread_index != os_get_thread_index ()) { clib_error ("Attempting to delete session belonging to thread %d by thread %d", sess->thread_index, thread_index); } if (FA_SESSION_BOGUS_INDEX != sess->link_prev_idx) { fa_session_t *prev_sess = get_session_ptr (am, thread_index, sess->link_prev_idx); /* the previous session must be in the same list as this one */ ASSERT (prev_sess->link_list_id == sess->link_list_id); prev_sess->link_next_idx = sess->link_next_idx; } if (FA_SESSION_BOGUS_INDEX != sess->link_next_idx) { fa_session_t *next_sess = get_session_ptr (am, thread_index, sess->link_next_idx); /* The next session must be in the same list as the one we are deleting */ ASSERT (next_sess->link_list_id == sess->link_list_id); next_sess->link_prev_idx = sess->link_prev_idx; next_expiry_time = now + fa_session_get_timeout (am, next_sess); } if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index) { pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx; pw->fa_conn_list_head_expiry_time[sess->link_list_id] = next_expiry_time; } if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index) { pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx; } return 1; } always_inline int acl_fa_restart_timer_for_session (acl_main_t * am, u64 now, fa_full_session_id_t sess_id) { if (acl_fa_conn_list_delete_session (am, sess_id, now)) { acl_fa_conn_list_add_session (am, sess_id, now); return 1; } else { /* * Our thread does not own this connection, so we can not requeue * The session. So we post the signal to the owner. */ aclp_post_session_change_request (am, sess_id.thread_index, sess_id.session_index, ACL_FA_REQ_SESS_RESCHEDULE); return 0; } } always_inline int is_ip6_5tuple (fa_5tuple_t * p5t) { return (p5t->l3_zero_pad[0] | p5t-> l3_zero_pad[1] | p5t->l3_zero_pad[2] | p5t->l3_zero_pad[3] | p5t-> l3_zero_pad[4] | p5t->l3_zero_pad[5]) != 0; } always_inline u8 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now, fa_session_t * sess, fa_5tuple_t * pkt_5tuple, u32 pkt_len) { sess->last_active_time = now; u8 old_flags = sess->tcp_flags_seen.as_u8[is_input]; u8 new_flags = old_flags | pkt_5tuple->pkt.tcp_flags; int flags_need_update = pkt_5tuple->pkt.tcp_flags_valid && (old_flags != new_flags); if (PREDICT_FALSE (flags_need_update)) { sess->tcp_flags_seen.as_u8[is_input] = new_flags; } return 3; } always_inline u64 reverse_l4_u64_fastpath (u64 l4, int is_ip6) { fa_session_l4_key_t l4i = {.as_u64 = l4 }; fa_session_l4_key_t l4o; l4o.port[1] = l4i.port[0]; l4o.port[0] = l4i.port[1]; l4o.non_port_l4_data = l4i.non_port_l4_data; l4o.l4_flags = l4i.l4_flags ^ FA_SK_L4_FLAG_IS_INPUT; return l4o.as_u64; } always_inline int reverse_l4_u64_slowpath_valid (u64 l4, int is_ip6, u64 * out) { fa_session_l4_key_t l4i = {.as_u64 = l4 }; fa_session_l4_key_t l4o; if (l4i.proto == icmp_protos[is_ip6]) { static const u8 *icmp_invmap[] = { icmp4_invmap, icmp6_invmap }; static const u8 *icmp_valid_new[] = { icmp4_valid_new, icmp6_valid_new }; static const u8 icmp_invmap_size[] = { sizeof (icmp4_invmap), sizeof (icmp6_invmap) }; static const u8 icmp_valid_new_size[] = { sizeof (icmp4_valid_new), sizeof (icmp6_valid_new) }; int type = is_ip6 ? l4i.port[0] - 128 : l4i.port[0]; l4o.non_port_l4_data = l4i.non_port_l4_data; l4o.port[0] = l4i.port[0]; l4o.port[1] = l4i.port[1]; /* * ONLY ICMP messages defined in icmp4_valid_new/icmp6_valid_new table * are allowed to create stateful ACL. * The other messages will be forwarded without creating a reverse session. */ int valid_reverse_sess = (type >= 0 && (type <= icmp_valid_new_size[is_ip6]) && (icmp_valid_new[is_ip6][type]) && (type <= icmp_invmap_size[is_ip6]) && icmp_invmap[is_ip6][type]); if (valid_reverse_sess) { l4o.l4_flags = l4i.l4_flags ^ FA_SK_L4_FLAG_IS_INPUT; l4o.port[0] = icmp_invmap[is_ip6][type] - 1; } *out = l4o.as_u64; return valid_reverse_sess; } else *out = reverse_l4_u64_fastpath (l4, is_ip6); return 1; } always_inline void reverse_session_add_del_ip6 (acl_main_t * am, clib_bihash_kv_40_8_t * pkv, int is_add) { clib_bihash_kv_40_8_t kv2; kv2.key[0] = pkv->key[2]; kv2.key[1] = pkv->key[3]; kv2.key[2] = pkv->key[0]; kv2.key[3] = pkv->key[1]; /* the last u64 needs special treatment (ports, etc.) so we do it last */ kv2.value = pkv->value; if (PREDICT_FALSE (is_session_l4_key_u64_slowpath (pkv->key[4]))) { if (reverse_l4_u64_slowpath_valid (pkv->key[4], 1, &kv2.key[4])) clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash, &kv2, is_add); } else { kv2.key[4] = reverse_l4_u64_fastpath (pkv->key[4], 1); clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash, &kv2, is_add); } } always_inline void reverse_session_add_del_ip4 (acl_main_t * am, clib_bihash_kv_16_8_t * pkv, int is_add) { clib_bihash_kv_16_8_t kv2; kv2.key[0] = ((pkv->key[0] & 0xffffffff) << 32) | ((pkv->key[0] >> 32) & 0xffffffff); /* the last u64 needs special treatment (ports, etc.) so we do it last */ kv2.value = pkv->value; if (PREDICT_FALSE (is_session_l4_key_u64_slowpath (pkv->key[1]))) { if (reverse_l4_u64_slowpath_valid (pkv->key[1], 0, &kv2.key[1])) clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash, &kv2, is_add); } else { kv2.key[1] = reverse_l4_u64_fastpath (pkv->key[1], 0); clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash, &kv2, is_add); } } always_inline void acl_fa_deactivate_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id) { fa_session_t *sess = get_session_ptr (am, sess_id.thread_index, sess_id.session_index); ASSERT (sess->thread_index == os_get_thread_index ()); void *oldheap = clib_mem_set_heap (am->acl_mheap); if (sess->is_ip6) { clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash, &sess->info.kv_40_8, 0); reverse_session_add_del_ip6 (am, &sess->info.kv_40_8, 0); } else { clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash, &sess->info.kv_16_8, 0); reverse_session_add_del_ip4 (am, &sess->info.kv_16_8, 0); } sess->deleted = 1; clib_atomic_fetch_add (&am->fa_session_total_deactivations, 1); clib_mem_set_heap (oldheap); } always_inline void acl_fa_put_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id) { if (sess_id.thread_index != os_get_thread_index ()) { clib_error ("Attempting to delete session belonging to thread %d by thread %d", sess_id.thread_index, os_get_thread_index ()); } void *oldheap = clib_mem_set_heap (am->acl_mheap); acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index]; pool_put_index (pw->fa_sessions_pool, sess_id.session_index); /* Deleting from timer structures not needed, as the caller must have dealt with the timers. */ vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index); clib_mem_set_heap (oldheap); pw->fa_session_dels_by_sw_if_index[sw_if_index]++; clib_atomic_fetch_add (&am->fa_session_total_dels, 1); } always_inline int acl_fa_two_stage_delete_session (acl_main_t * am, u32 sw_if_index, fa_full_session_id_t sess_id, u64 now) { fa_session_t *sess = get_session_ptr (am, sess_id.thread_index, sess_id.session_index); if (sess->deleted) { acl_fa_put_session (am, sw_if_index, sess_id); return 1; } else { acl_fa_deactivate_session (am, sw_if_index, sess_id); acl_fa_conn_list_add_session (am, sess_id, now); return 0; } } always_inline int acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index) { u64 curr_sess_count; curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels; return (curr_sess_count + vec_len (vlib_mains) < am->fa_conn_table_max_entries); } always_inline void acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index, u32 sw_if_index, u64 now) { /* try to recycle a TCP transient session */ acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index]; fa_full_session_id_t volatile sess_id; int n_recycled = 0; /* clean up sessions from purgatory, if we can */ sess_id.session_index = pw->fa_conn_list_head[ACL_TIMEOUT_PURGATORY]; while ((FA_SESSION_BOGUS_INDEX != sess_id.session_index) && n_recycled < am->fa_max_deleted_sessions_per_interval) { sess_id.thread_index = thread_index; fa_session_t *sess = get_session_ptr (am, sess_id.thread_index, sess_id.session_index); if (sess->link_enqueue_time + fa_session_get_timeout (am, sess) < now) { acl_fa_conn_list_delete_session (am, sess_id, now); /* interface that needs the sessions may not be the interface of the session. */ acl_fa_put_session (am, sess->sw_if_index, sess_id); n_recycled++; } else break; /* too early to try to recycle from here, bail out */ sess_id.session_index = pw->fa_conn_list_head[ACL_TIMEOUT_PURGATORY]; } sess_id.session_index = pw->fa_conn_list_head[ACL_TIMEOUT_TCP_TRANSIENT]; if (FA_SESSION_BOGUS_INDEX != sess_id.session_index) { sess_id.thread_index = thread_index; acl_fa_conn_list_delete_session (am, sess_id, now); acl_fa_deactivate_session (am, sw_if_index, sess_id); /* this goes to purgatory list */ acl_fa_conn_list_add_session (am, sess_id, now); } } always_inline fa_full_session_id_t acl_fa_add_session (acl_main_t * am, int is_input, int is_ip6, u32 sw_if_index, u64 now, fa_5tuple_t * p5tuple, u16 current_policy_epoch) { fa_full_session_id_t f_sess_id; uword thread_index = os_get_thread_index (); void *oldheap = clib_mem_set_heap (am->acl_mheap); acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index]; f_sess_id.thread_index = thread_index; fa_session_t *sess; if (f_sess_id.as_u64 == ~0) { clib_error ("Adding session with invalid value"); } pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES); f_sess_id.session_index = sess - pw->fa_sessions_pool; f_sess_id.intf_policy_epoch = current_policy_epoch; if (is_ip6) { sess->info.kv_40_8.key[0] = p5tuple->kv_40_8.key[0]; sess->info.kv_40_8.key[1] = p5tuple->kv_40_8.key[1]; sess->info.kv_40_8.key[2] = p5tuple->kv_40_8.key[2]; sess->info.kv_40_8.key[3] = p5tuple->kv_40_8.key[3]; sess->info.kv_40_8.key[4] = p5tuple->kv_40_8.key[4]; sess->info.kv_40_8.value = f_sess_id.as_u64; } else { sess->info.kv_16_8.key[0] = p5tuple->kv_16_8.key[0]; sess->info.kv_16_8.key[1] = p5tuple->kv_16_8.key[1]; sess->info.kv_16_8.value = f_sess_id.as_u64; } sess->last_active_time = now; sess->sw_if_index = sw_if_index; sess->tcp_flags_seen.as_u16 = 0; sess->thread_index = thread_index; sess->link_list_id = ACL_TIMEOUT_UNUSED; sess->link_prev_idx = FA_SESSION_BOGUS_INDEX; sess->link_next_idx = FA_SESSION_BOGUS_INDEX; sess->deleted = 0; sess->is_ip6 = is_ip6; acl_fa_conn_list_add_session (am, f_sess_id, now); ASSERT (am->fa_sessions_hash_is_initialized == 1); if (is_ip6) { reverse_session_add_del_ip6 (am, &sess->info.kv_40_8, 1); clib_bihash_add_del_40_8 (&am->fa_ip6_sessions_hash, &sess->info.kv_40_8, 1); } else { reverse_session_add_del_ip4 (am, &sess->info.kv_16_8, 1); clib_bihash_add_del_16_8 (&am->fa_ip4_sessions_hash, &sess->info.kv_16_8, 1); } vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index); clib_mem_set_heap (oldheap); pw->fa_session_adds_by_sw_if_index[sw_if_index]++; clib_atomic_fetch_add (&am->fa_session_total_adds, 1); return f_sess_id; } always_inline int acl_fa_find_session (acl_main_t * am, int is_ip6, u32 sw_if_index0, fa_5tuple_t * p5tuple, u64 * pvalue_sess) { int res = 0; if (is_ip6) { clib_bihash_kv_40_8_t kv_result; res = (clib_bihash_search_inline_2_40_8 (&am->fa_ip6_sessions_hash, &p5tuple->kv_40_8, &kv_result) == 0); *pvalue_sess = kv_result.value; } else { clib_bihash_kv_16_8_t kv_result; res = (clib_bihash_search_inline_2_16_8 (&am->fa_ip4_sessions_hash, &p5tuple->kv_16_8, &kv_result) == 0); *pvalue_sess = kv_result.value; } return res; } always_inline u64 acl_fa_make_session_hash (acl_main_t * am, int is_ip6, u32 sw_if_index0, fa_5tuple_t * p5tuple) { if (is_ip6) return clib_bihash_hash_40_8 (&p5tuple->kv_40_8); else return clib_bihash_hash_16_8 (&p5tuple->kv_16_8); } always_inline void acl_fa_prefetch_session_bucket_for_hash (acl_main_t * am, int is_ip6, u64 hash) { if (is_ip6) clib_bihash_prefetch_bucket_40_8 (&am->fa_ip6_sessions_hash, hash); else clib_bihash_prefetch_bucket_16_8 (&am->fa_ip4_sessions_hash, hash); } always_inline void acl_fa_prefetch_session_data_for_hash (acl_main_t * am, int is_ip6, u64 hash) { if (is_ip6) clib_bihash_prefetch_data_40_8 (&am->fa_ip6_sessions_hash, hash); else clib_bihash_prefetch_data_16_8 (&am->fa_ip4_sessions_hash, hash); } always_inline int acl_fa_find_session_with_hash (acl_main_t * am, int is_ip6, u32 sw_if_index0, u64 hash, fa_5tuple_t * p5tuple, u64 * pvalue_sess) { int res = 0; if (is_ip6) { clib_bihash_kv_40_8_t kv_result; kv_result.value = ~0ULL; res = (clib_bihash_search_inline_2_with_hash_40_8 (&am->fa_ip6_sessions_hash, hash, &p5tuple->kv_40_8, &kv_result) == 0); *pvalue_sess = kv_result.value; } else { clib_bihash_kv_16_8_t kv_result; kv_result.value = ~0ULL; res = (clib_bihash_search_inline_2_with_hash_16_8 (&am->fa_ip4_sessions_hash, hash, &p5tuple->kv_16_8, &kv_result) == 0); *pvalue_sess = kv_result.value; } return res; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */