FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
nat66.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 NAT66 implementation
18  */
19 
20 #include <nat/nat66.h>
21 #include <vnet/fib/fib_table.h>
23 
25 
26 /* *INDENT-OFF* */
27 
28 /* Hook up input features */
29 VNET_FEATURE_INIT (nat66_in2out, static) = {
30  .arc_name = "ip6-unicast",
31  .node_name = "nat66-in2out",
32  .runs_before = VNET_FEATURES ("ip6-lookup"),
33  .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
34 };
35 VNET_FEATURE_INIT (nat66_out2in, static) = {
36  .arc_name = "ip6-unicast",
37  .node_name = "nat66-out2in",
38  .runs_before = VNET_FEATURES ("ip6-lookup"),
39  .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
40 };
41 
42 /* *INDENT-ON* */
43 
44 
45 void
47 {
48  nat66_main_t *nm = &nat66_main;
50  u32 static_mapping_buckets = 1024;
51  uword static_mapping_memory_size = 64 << 20;
52 
53  node = vlib_get_node_by_name (vm, (u8 *) "nat66-in2out");
54  nm->in2out_node_index = node->index;
55 
56  node = vlib_get_node_by_name (vm, (u8 *) "nat66-out2in");
57  nm->out2in_node_index = node->index;
58 
59  clib_bihash_init_24_8 (&nm->sm_l, "nat66-static-map-by-local",
60  static_mapping_buckets, static_mapping_memory_size);
61  clib_bihash_init_24_8 (&nm->sm_e, "nat66-static-map-by-external",
62  static_mapping_buckets, static_mapping_memory_size);
63 
64  nm->session_counters.name = "session counters";
65 }
66 
67 int
69 {
70  nat66_main_t *nm = &nat66_main;
71  snat_interface_t *interface = 0, *i;
72  const char *feature_name;
73 
74  /* *INDENT-OFF* */
76  ({
77  if (i->sw_if_index == sw_if_index)
78  {
79  interface = i;
80  break;
81  }
82  }));
83  /* *INDENT-ON* */
84 
85  if (is_add)
86  {
87  if (interface)
88  return VNET_API_ERROR_VALUE_EXIST;
89 
90  pool_get (nm->interfaces, interface);
91  interface->sw_if_index = sw_if_index;
92  interface->flags =
93  is_inside ? NAT_INTERFACE_FLAG_IS_INSIDE :
95  }
96  else
97  {
98  if (!interface)
99  return VNET_API_ERROR_NO_SUCH_ENTRY;
100 
101  pool_put (nm->interfaces, interface);
102  }
103 
104  feature_name = is_inside ? "nat66-in2out" : "nat66-out2in";
106  if (rv)
107  return rv;
108  return vnet_feature_enable_disable ("ip6-unicast", feature_name,
109  sw_if_index, is_add, 0, 0);
110 }
111 
112 void
114 {
115  nat66_main_t *nm = &nat66_main;
116  snat_interface_t *i = 0;
117 
118  /* *INDENT-OFF* */
119  pool_foreach (i, nm->interfaces,
120  ({
121  if (fn (i, ctx))
122  break;
123  }));
124  /* *INDENT-ON* */
125 }
126 
129 {
130  nat66_main_t *nm = &nat66_main;
131  nat66_static_mapping_t *sm = 0;
132  nat66_sm_key_t sm_key;
134 
135  sm_key.addr.as_u64[0] = addr->as_u64[0];
136  sm_key.addr.as_u64[1] = addr->as_u64[1];
137  sm_key.fib_index = fib_index;
138  sm_key.rsvd = 0;
139 
140  kv.key[0] = sm_key.as_u64[0];
141  kv.key[1] = sm_key.as_u64[1];
142  kv.key[2] = sm_key.as_u64[2];
143 
144  if (!clib_bihash_search_24_8
145  (is_local ? &nm->sm_l : &nm->sm_e, &kv, &value))
146  sm = pool_elt_at_index (nm->sm, value.value);
147 
148  return sm;
149 }
150 
151 int
153  u32 vrf_id, u8 is_add)
154 {
155  nat66_main_t *nm = &nat66_main;
156  int rv = 0;
157  nat66_static_mapping_t *sm = 0;
158  nat66_sm_key_t sm_key;
160  u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, vrf_id);
161 
162  sm_key.addr.as_u64[0] = l_addr->as_u64[0];
163  sm_key.addr.as_u64[1] = l_addr->as_u64[1];
164  sm_key.fib_index = fib_index;
165  sm_key.rsvd = 0;
166  kv.key[0] = sm_key.as_u64[0];
167  kv.key[1] = sm_key.as_u64[1];
168  kv.key[2] = sm_key.as_u64[2];
169 
170  if (!clib_bihash_search_24_8 (&nm->sm_l, &kv, &value))
171  sm = pool_elt_at_index (nm->sm, value.value);
172 
173  if (is_add)
174  {
175  if (sm)
176  return VNET_API_ERROR_VALUE_EXIST;
177 
180  pool_get (nm->sm, sm);
181  clib_memset (sm, 0, sizeof (*sm));
182  sm->l_addr.as_u64[0] = l_addr->as_u64[0];
183  sm->l_addr.as_u64[1] = l_addr->as_u64[1];
184  sm->e_addr.as_u64[0] = e_addr->as_u64[0];
185  sm->e_addr.as_u64[1] = e_addr->as_u64[1];
186  sm->fib_index = fib_index;
187 
188  sm_key.fib_index = fib_index;
189  kv.key[0] = sm_key.as_u64[0];
190  kv.key[1] = sm_key.as_u64[1];
191  kv.key[2] = sm_key.as_u64[2];
192  kv.value = sm - nm->sm;
193  if (clib_bihash_add_del_24_8 (&nm->sm_l, &kv, 1))
194  nat_elog_warn ("nat66-static-map-by-local add key failed");
195  sm_key.addr.as_u64[0] = e_addr->as_u64[0];
196  sm_key.addr.as_u64[1] = e_addr->as_u64[1];
197  sm_key.fib_index = 0;
198  kv.key[0] = sm_key.as_u64[0];
199  kv.key[1] = sm_key.as_u64[1];
200  kv.key[2] = sm_key.as_u64[2];
201  if (clib_bihash_add_del_24_8 (&nm->sm_e, &kv, 1))
202  nat_elog_warn ("nat66-static-map-by-external add key failed");
203 
206  }
207  else
208  {
209  if (!sm)
210  return VNET_API_ERROR_NO_SUCH_ENTRY;
211 
212  kv.value = sm - nm->sm;
213  if (clib_bihash_add_del_24_8 (&nm->sm_l, &kv, 0))
214  nat_elog_warn ("nat66-static-map-by-local delete key failed");
215  sm_key.addr.as_u64[0] = e_addr->as_u64[0];
216  sm_key.addr.as_u64[1] = e_addr->as_u64[1];
217  sm_key.fib_index = 0;
218  kv.key[0] = sm_key.as_u64[0];
219  kv.key[1] = sm_key.as_u64[1];
220  kv.key[2] = sm_key.as_u64[2];
221  if (clib_bihash_add_del_24_8 (&nm->sm_e, &kv, 0))
222  nat_elog_warn ("nat66-static-map-by-external delete key failed");
224  pool_put (nm->sm, sm);
225  }
226 
227  return rv;
228 }
229 
230 void
232 {
233  nat66_main_t *nm = &nat66_main;
234  nat66_static_mapping_t *sm = 0;
235 
236  /* *INDENT-OFF* */
237  pool_foreach (sm, nm->sm,
238  ({
239  if (fn (sm, ctx))
240  break;
241  }));
242  /* *INDENT-ON* */
243 }
244 
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "gnu")
250  * End:
251  */
u32 rsvd
Definition: nat66.h:40
void vlib_validate_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
validate a combined counter
Definition: counter.c:94
u64 as_u64[2]
Definition: ip6_packet.h:51
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:280
u32 fib_index
Definition: nat66.h:39
#define NAT_INTERFACE_FLAG_IS_OUTSIDE
Definition: nat.h:292
int i
#define nat_elog_warn(nat_elog_str)
Definition: nat.h:1025
u32 out2in_node_index
Definition: nat66.h:60
clib_bihash_24_8_t sm_e
Static mapping by external address lookup table.
Definition: nat66.h:55
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:237
vhost_vring_addr_t addr
Definition: vhost_user.h:147
unsigned char u8
Definition: types.h:56
nat66_main_t nat66_main
Definition: nat66.c:24
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
unsigned int u32
Definition: types.h:88
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1097
void nat66_init(vlib_main_t *vm)
Definition: nat66.c:46
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread counters.
Definition: counter.h:285
void nat66_static_mappings_walk(nat66_static_mapping_walk_fn_t fn, void *ctx)
Definition: nat66.c:231
u64 as_u64[3]
Definition: nat66.h:42
IPv6 shallow virtual reassembly.
long ctx[MAX_CONNS]
Definition: main.c:144
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:287
int(* nat66_static_mapping_walk_fn_t)(nat66_static_mapping_t *sm, void *ctx)
Definition: nat66.h:74
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
Definition: fib_table.c:1291
vlib_main_t * vm
Definition: in2out_ed.c:1810
int nat66_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Definition: nat66.c:68
ip6_address_t e_addr
Definition: nat66.h:28
snat_interface_t * interfaces
Interface pool.
Definition: nat66.h:49
clib_bihash_24_8_t sm_l
Static mapping by local address lookup table.
Definition: nat66.h:53
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
fib_source_t nat_fib_src_hi
Definition: nat.c:41
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
VNET_FEATURE_INIT(nat66_in2out, static)
u8 value
Definition: qos.api:54
#define NAT_INTERFACE_FLAG_IS_INSIDE
Definition: nat.h:291
u32 fib_table_find_or_create_and_lock(fib_protocol_t proto, u32 table_id, fib_source_t src)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1156
int ip6_sv_reass_enable_disable_with_refcnt(u32 sw_if_index, int is_enable)
#define VNET_FEATURES(...)
Definition: feature.h:442
ip6_address_t l_addr
Definition: nat66.h:27
vlib_combined_counter_main_t session_counters
Session counters.
Definition: nat66.h:57
int(* nat66_interface_walk_fn_t)(snat_interface_t *i, void *ctx)
Definition: nat66.h:71
void nat66_interfaces_walk(nat66_interface_walk_fn_t fn, void *ctx)
Definition: nat66.c:113
u64 uword
Definition: types.h:112
char * name
The counter collection&#39;s name.
Definition: counter.h:193
int nat66_static_mapping_add_del(ip6_address_t *l_addr, ip6_address_t *e_addr, u32 vrf_id, u8 is_add)
Definition: nat66.c:152
ip6_address_t addr
Definition: nat66.h:38
nat66_static_mapping_t * sm
Static mapping pool.
Definition: nat66.h:51
u32 vrf_id
Definition: nat.api:821
u32 in2out_node_index
node index
Definition: nat66.h:59
NAT66 global declarations.
nat66_static_mapping_t * nat66_static_mapping_get(ip6_address_t *addr, u32 fib_index, u8 is_local)
Definition: nat66.c:128
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:304