aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cnat/cnat_client.h
blob: db6933c3b9512e4d37f8f94725dd09a518f19fbb (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
/*
 * Copyright (c) 2020 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.
 */

#ifndef __CNAT_CLIENT_H__
#define __CNAT_CLIENT_H__

#include <cnat/cnat_types.h>
#include <vppinfra/bihash_16_8.h>

/**
 * A client is a representation of an IP address behind the NAT.
 * A client thus sends packet to a VIP.
 * Clients are learned in the Data-plane when they send packets,
 * but, since they make additions to the FIB they must be programmed
 * in the main thread. They are aged out when they become idle.
 *
 * A client interposes in the FIB graph for the prefix corresponding
 * to the client (e.g. client's-IP/32). As a result this client object
 * is cloned as the interpose DPO. The clones are removed when the lock
 * count drops to zero. The originals are removed when the client ages.
 * At forwarding time the client preforms the reverse translation and
 * then ships the packet to where the FIB would send it.
 */
typedef struct cnat_client_t_
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);

  /**
   * the client's IP address
   */
  ip_address_t cc_ip;

  /**
   * How to send packets to this client post translation
   */
  dpo_id_t cc_parent;

  /**
   * the FIB entry this client sources
   */
  fib_node_index_t cc_fei;

  /**
   * number of DPO locks
   */
  u32 cc_locks;

  /**
   * Translations refcount for cleanup
   */
  u32 tr_refcnt;

  /**
   * Session refcount for cleanup
   */
  u32 session_refcnt;

  /**
   * Parent cnat_client index if cloned via interpose
   * or own index if vanilla client.
   * Used to get translations & update session_refcnt
   */
  index_t parent_cci;

  /**
   * Client flags
   */
  u8 flags;
} cnat_client_t;

extern u8 *format_cnat_client (u8 * s, va_list * args);
extern void cnat_client_free_by_ip (ip46_address_t * addr, u8 af);

extern cnat_client_t *cnat_client_pool;
extern dpo_type_t cnat_client_dpo;

#define CC_INDEX_INVALID ((u32)(~0))

static_always_inline cnat_client_t *
cnat_client_get (index_t i)
{
  return (pool_elt_at_index (cnat_client_pool, i));
}

/**
 * A translation that references this VIP was deleted
 */
extern void cnat_client_translation_deleted (index_t cci);

/**
 * A translation that references this VIP was added
 */
extern void cnat_client_translation_added (index_t cci);
/**
 * Called in the main thread by RPC from the workers to learn a
 * new client
 */
extern void cnat_client_learn (const ip_address_t *addr);

extern index_t cnat_client_add (const ip_address_t * ip, u8 flags);

/**
 * Check all the clients were purged by translation & session purge
 */
extern int cnat_client_purge (void);

/**
 * CNat Client (dpo) flags
 */
typedef enum
{
  /* IP already present in the FIB, need to interpose dpo */
  CNAT_FLAG_EXCLUSIVE = (1 << 1),
} cnat_entry_flag_t;


extern void cnat_client_throttle_pool_process ();

/**
 * DB of clients
 */
typedef struct cnat_client_db_t_
{
  clib_bihash_16_8_t cc_ip_id_hash;
  /* Pool of addresses that have been throttled
     and need to be refcounted before calling
     cnat_client_free_by_ip */
  clib_spinlock_t throttle_lock;
  uword *throttle_mem;
} cnat_client_db_t;

extern cnat_client_db_t cnat_client_db;

/**
 * Find a client from an IP4 address
 */
static_always_inline cnat_client_t *
cnat_client_ip4_find (const ip4_address_t * ip)
{
  clib_bihash_kv_16_8_t bkey, bval;

  bkey.key[0] = ip->as_u32;
  bkey.key[1] = 0;

  if (clib_bihash_search_16_8 (&cnat_client_db.cc_ip_id_hash, &bkey, &bval))
    return (NULL);

  return (pool_elt_at_index (cnat_client_pool, bval.value));
}

/**
 * Find a client from an IP6 address
 */
static_always_inline cnat_client_t *
cnat_client_ip6_find (const ip6_address_t * ip)
{
  clib_bihash_kv_16_8_t bkey, bval;

  bkey.key[0] = ip->as_u64[0];
  bkey.key[1] = ip->as_u64[1];

  if (clib_bihash_search_16_8 (&cnat_client_db.cc_ip_id_hash, &bkey, &bval))
    return (NULL);

  return (pool_elt_at_index (cnat_client_pool, bval.value));
}

/**
 * Add a session refcnt to this client
 */
static_always_inline u32
cnat_client_cnt_session (cnat_client_t * cc)
{
  cnat_client_t *ccp = cnat_client_get (cc->parent_cci);
  return clib_atomic_add_fetch (&ccp->session_refcnt, 1);
}

/**
 * Del a session refcnt to this client
 */
static_always_inline u32
cnat_client_uncnt_session (cnat_client_t * cc)
{
  cnat_client_t *ccp = cnat_client_get (cc->parent_cci);
  return clib_atomic_sub_fetch (&ccp->session_refcnt, 1);
}

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

#endif