aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/session/session_table.c
blob: d0b576fda7b63d2b34127c000c04dfe4da69e705 (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
/*
 * Copyright (c) 2017-2019 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/session/session_table.h>
#include <vnet/session/session.h>

/**
 * Pool of session tables
 */
static session_table_t *lookup_tables;

session_table_t *
_get_session_tables (void)
{
  return lookup_tables;
}

session_table_t *
session_table_alloc (void)
{
  session_table_t *slt;
  pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES);
  clib_memset (slt, 0, sizeof (*slt));
  return slt;
}

u32
session_table_index (session_table_t * slt)
{
  return (slt - lookup_tables);
}

session_table_t *
session_table_get (u32 table_index)
{
  if (pool_is_free_index (lookup_tables, table_index))
    return 0;
  return pool_elt_at_index (lookup_tables, table_index);
}

#define foreach_hash_table_parameter            \
  _(v4,session,buckets,20000)                   \
  _(v4,session,memory,(64<<20))                 \
  _(v6,session,buckets,20000)                   \
  _(v6,session,memory,(64<<20))                 \
  _(v4,halfopen,buckets,20000)                  \
  _(v4,halfopen,memory,(64<<20))                \
  _(v6,halfopen,buckets,20000)                  \
  _(v6,halfopen,memory,(64<<20))

/**
 * Initialize session table hash tables
 *
 * If vpp configured with set of table parameters it uses them,
 * otherwise it uses defaults above.
 */
void
session_table_init (session_table_t * slt, u8 fib_proto)
{
  u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
  int i;

#define _(af,table,parm,value) 						\
  u32 configured_##af##_##table##_table_##parm = value;
  foreach_hash_table_parameter;
#undef _

#define _(af,table,parm,value)                                          \
  if (session_main.configured_##af##_##table##_table_##parm)    \
    configured_##af##_##table##_table_##parm =                          \
      session_main.configured_##af##_##table##_table_##parm;
  foreach_hash_table_parameter;
#undef _

  if (fib_proto == FIB_PROTOCOL_IP4 || all)
    {
      clib_bihash_init2_args_16_8_t _a, *a = &_a;

      memset (a, 0, sizeof (*a));
      a->h = &slt->v4_session_hash;
      a->name = "v4 session table";
      a->nbuckets = configured_v4_session_table_buckets;
      a->memory_size = configured_v4_session_table_memory;
      a->dont_add_to_all_bihash_list = 1;
      a->instantiate_immediately = 1;
      clib_bihash_init2_16_8 (a);

      memset (a, 0, sizeof (*a));
      a->h = &slt->v4_half_open_hash;
      a->name = "v4 half-open table";
      a->nbuckets = configured_v4_halfopen_table_buckets;
      a->memory_size = configured_v4_halfopen_table_memory;
      a->dont_add_to_all_bihash_list = 1;
      a->instantiate_immediately = 1;
      clib_bihash_init2_16_8 (a);
    }
  if (fib_proto == FIB_PROTOCOL_IP6 || all)
    {
      clib_bihash_init2_args_48_8_t _a, *a = &_a;

      memset (a, 0, sizeof (*a));
      a->h = &slt->v6_session_hash;
      a->name = "v6 session table";
      a->nbuckets = configured_v6_session_table_buckets;
      a->memory_size = configured_v6_session_table_memory;
      a->dont_add_to_all_bihash_list = 1;
      a->instantiate_immediately = 1;
      clib_bihash_init2_48_8 (a);

      memset (a, 0, sizeof (*a));
      a->h = &slt->v6_half_open_hash;
      a->name = "v6 half-open table";
      a->nbuckets = configured_v6_halfopen_table_buckets;
      a->memory_size = configured_v6_halfopen_table_memory;
      a->dont_add_to_all_bihash_list = 1;
      a->instantiate_immediately = 1;
      clib_bihash_init2_48_8 (a);
    }

  vec_validate (slt->session_rules, TRANSPORT_N_PROTOS - 1);
  for (i = 0; i < TRANSPORT_N_PROTOS; i++)
    session_rules_table_init (&slt->session_rules[i]);
}

typedef struct _ip4_session_table_walk_ctx_t
{
  ip4_session_table_walk_fn_t fn;
  void *ctx;
} ip4_session_table_walk_ctx_t;

static int
ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg)
{
  ip4_session_table_walk_ctx_t *ctx = arg;
  ctx->fn (kvp, ctx->ctx);
  return (BIHASH_WALK_CONTINUE);
}

void
ip4_session_table_walk (clib_bihash_16_8_t * hash,
			ip4_session_table_walk_fn_t fn, void *arg)
{
  ip4_session_table_walk_ctx_t ctx = {
    .fn = fn,
    .ctx = arg,
  };
  clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb,
					   &ctx);
}

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