FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
nat_affinity.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief NAT plugin client-IP based session affinity for load-balancing
18  */
19 
20 #include <nat/nat_affinity.h>
21 #include <nat/nat.h>
22 
24 
25 #define AFFINITY_HASH_BUCKETS 65536
26 #define AFFINITY_HASH_MEMORY (2 << 25)
27 
28 u8 *
29 format_affinity_kvp (u8 * s, va_list * args)
30 {
31  clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
33 
34  k.as_u64[0] = v->key[0];
35  k.as_u64[1] = v->key[1];
36 
37  s = format (s, "client %U backend %U:%d proto %U index %llu",
40  clib_net_to_host_u16 (k.service_port),
42 
43  return s;
44 }
45 
48 {
51  clib_error_t *error = 0;
52 
53  if (tm->n_vlib_mains > 1)
55 
56  clib_bihash_init_16_8 (&nam->affinity_hash, "nat-affinity",
58  clib_bihash_set_kvp_format_fn_16_8 (&nam->affinity_hash,
60 
61  nam->vlib_main = vm;
62 
63  return error;
64 }
65 
68  ip4_address_t service_addr, u8 proto, u16 service_port)
69 {
71 
72  key->client_addr = client_addr;
73  key->service_addr = service_addr;
74  key->proto = proto;
75  key->service_port = service_port;
76 
77  kv->value = ~0ULL;
78 }
79 
80 u32
82 {
84  dlist_elt_t *head_elt;
85 
87 
88  pool_get (nam->list_pool, head_elt);
89  clib_dlist_init (nam->list_pool, head_elt - nam->list_pool);
90 
92 
93  return head_elt - nam->list_pool;
94 }
95 
96 void
97 nat_affinity_flush_service (u32 affinity_per_service_list_head_index)
98 {
100  u32 elt_index;
101  dlist_elt_t *elt;
102  nat_affinity_t *a;
104 
106 
107  while ((elt_index =
109  affinity_per_service_list_head_index)) !=
110  ~0)
111  {
112  elt = pool_elt_at_index (nam->list_pool, elt_index);
113  a = pool_elt_at_index (nam->affinity_pool, elt->value);
114  kv.key[0] = a->key.as_u64[0];
115  kv.key[1] = a->key.as_u64[1];
116  pool_put_index (nam->affinity_pool, elt->value);
117  if (clib_bihash_add_del_16_8 (&nam->affinity_hash, &kv, 0))
118  nat_log_warn ("affinity key del failed");
119  pool_put_index (nam->list_pool, elt_index);
120  }
121  pool_put_index (nam->list_pool, affinity_per_service_list_head_index);
122 
124 }
125 
126 int
128  ip4_address_t service_addr, u8 proto,
129  u16 service_port, u8 * backend_index)
130 {
132  clib_bihash_kv_16_8_t kv, value;
133  nat_affinity_t *a;
134  int rv = 0;
135 
136  make_affinity_kv (&kv, client_addr, service_addr, proto, service_port);
138  if (clib_bihash_search_16_8 (&nam->affinity_hash, &kv, &value))
139  {
140  rv = 1;
141  goto unlock;
142  }
143 
144  a = pool_elt_at_index (nam->affinity_pool, value.value);
145  /* if already expired delete */
146  if (a->ref_cnt == 0)
147  {
148  if (a->expire < vlib_time_now (nam->vlib_main))
149  {
150  clib_dlist_remove (nam->list_pool, a->per_service_index);
151  pool_put_index (nam->list_pool, a->per_service_index);
152  pool_put_index (nam->affinity_pool, value.value);
153  if (clib_bihash_add_del_16_8 (&nam->affinity_hash, &kv, 0))
154  nat_log_warn ("affinity key del failed");
155  rv = 1;
156  goto unlock;
157  }
158  }
159  a->ref_cnt++;
160  *backend_index = a->backend_index;
161 
162 unlock:
164  return rv;
165 }
166 
167 static int
169 {
171  nat_affinity_t *a;
172 
173  a = pool_elt_at_index (nam->affinity_pool, kv->value);
174  if (a->ref_cnt == 0)
175  {
176  if (a->expire < vlib_time_now (nam->vlib_main))
177  {
178  clib_dlist_remove (nam->list_pool, a->per_service_index);
179  pool_put_index (nam->list_pool, a->per_service_index);
180  pool_put_index (nam->affinity_pool, kv->value);
181  if (clib_bihash_add_del_16_8 (&nam->affinity_hash, kv, 0))
182  nat_log_warn ("affinity key del failed");
183  return 1;
184  }
185  }
186 
187  return 0;
188 }
189 
190 int
192  ip4_address_t service_addr, u8 proto,
193  u16 service_port, u8 backend_index,
194  u32 sticky_time,
195  u32 affinity_per_service_list_head_index)
196 {
198  clib_bihash_kv_16_8_t kv, value;
199  nat_affinity_t *a;
200  dlist_elt_t *list_elt;
201  int rv = 0;
202 
203  make_affinity_kv (&kv, client_addr, service_addr, proto, service_port);
205  if (!clib_bihash_search_16_8 (&nam->affinity_hash, &kv, &value))
206  {
207  rv = 1;
208  nat_log_notice ("affinity key already exist");
209  goto unlock;
210  }
211 
212  pool_get (nam->affinity_pool, a);
213  kv.value = a - nam->affinity_pool;
214  rv =
215  clib_bihash_add_or_overwrite_stale_16_8 (&nam->affinity_hash, &kv,
217  if (rv)
218  {
219  nat_log_notice ("affinity key add failed");
220  pool_put (nam->affinity_pool, a);
221  goto unlock;
222  }
223 
224  pool_get (nam->list_pool, list_elt);
225  clib_dlist_init (nam->list_pool, list_elt - nam->list_pool);
226  list_elt->value = a - nam->affinity_pool;
227  a->per_service_index = list_elt - nam->list_pool;
228  a->backend_index = backend_index;
229  a->ref_cnt = 1;
230  a->sticky_time = sticky_time;
231  a->key.as_u64[0] = kv.key[0];
232  a->key.as_u64[1] = kv.key[1];
233  clib_dlist_addtail (nam->list_pool, affinity_per_service_list_head_index,
234  a->per_service_index);
235 
236 unlock:
238  return rv;
239 }
240 
241 void
243  u8 proto, u16 service_port)
244 {
246  clib_bihash_kv_16_8_t kv, value;
247  nat_affinity_t *a;
248 
249  make_affinity_kv (&kv, client_addr, service_addr, proto, service_port);
251  if (clib_bihash_search_16_8 (&nam->affinity_hash, &kv, &value))
252  goto unlock;
253 
254  a = pool_elt_at_index (nam->affinity_pool, value.value);
255  a->ref_cnt--;
256  if (a->ref_cnt == 0)
257  a->expire = (u64) a->sticky_time + vlib_time_now (nam->vlib_main);
258 
259 unlock:
261 }
262 
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "gnu")
268  * End:
269  */
int nat_affinity_create_and_lock(ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port, u8 backend_index, u32 sticky_time, u32 affinity_per_service_list_head_index)
Create affinity record and take reference counting lock.
Definition: nat_affinity.c:191
ip4_address_t client_addr
Definition: nat_affinity.h:34
nat_affinity_main_t nat_affinity_main
Definition: nat_affinity.c:23
a
Definition: bitmap.h:538
static void clib_dlist_init(dlist_elt_t *pool, u32 index)
Definition: dlist.h:36
u8 * format_affinity_kvp(u8 *s, va_list *args)
Definition: nat_affinity.c:29
unsigned long u64
Definition: types.h:89
#define NULL
Definition: clib.h:58
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:98
#define nat_log_warn(...)
Definition: nat.h:720
#define AFFINITY_HASH_BUCKETS
Definition: nat_affinity.c:25
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
dlist_elt_t * list_pool
Definition: nat_affinity.h:61
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
unsigned char u8
Definition: types.h:56
ip4_address_t service_addr
Definition: nat_affinity.h:33
format_function_t format_ip4_address
Definition: format.h:75
#define static_always_inline
Definition: clib.h:99
clib_bihash_16_8_t affinity_hash
Definition: nat_affinity.h:59
vlib_main_t * vlib_main
Definition: nat_affinity.h:62
unsigned int u32
Definition: types.h:88
void nat_affinity_unlock(ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port)
Release a reference counting lock for affinity.
Definition: nat_affinity.c:242
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:57
clib_spinlock_t affinity_lock
Definition: nat_affinity.h:60
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
Definition: dlist.h:43
unsigned short u16
Definition: types.h:57
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
#define nat_log_notice(...)
Definition: nat.h:722
static int affinity_is_expired_cb(clib_bihash_kv_16_8_t *kv, void *arg)
Definition: nat_affinity.c:168
nat_affinity_t * affinity_pool
Definition: nat_affinity.h:58
u32 nat_affinity_get_per_service_list_head_index(void)
Get new affinity per service list head index.
Definition: nat_affinity.c:81
vlib_main_t * vm
Definition: buffer.c:312
int nat_affinity_find_and_lock(ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port, u8 *backend_index)
Find service backend index for client-IP and take a reference counting lock.
Definition: nat_affinity.c:127
static_always_inline void make_affinity_kv(clib_bihash_kv_16_8_t *kv, ip4_address_t client_addr, ip4_address_t service_addr, u8 proto, u16 service_port)
Definition: nat_affinity.c:67
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:311
clib_error_t * nat_affinity_init(vlib_main_t *vm)
Initialize NAT client-IP based affinity.
Definition: nat_affinity.c:47
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
Definition: dlist.h:99
format_function_t format_snat_protocol
Definition: nat.h:621
u32 value
Definition: dlist.h:32
void nat_affinity_flush_service(u32 affinity_per_service_list_head_index)
Flush all service affinity data.
Definition: nat_affinity.c:97
typedef key
Definition: ipsec.api:244
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
NAT plugin client-IP based session affinity for load-balancing.
#define AFFINITY_HASH_MEMORY
Definition: nat_affinity.c:26
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:82
static u32 clib_dlist_remove_head(dlist_elt_t *pool, u32 head_index)
Definition: dlist.h:117