cnat: fixes & prepare maglev
[vpp.git] / src / plugins / cnat / cnat_snat.c
1 /*
2  * Copyright (c) 2020 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 #include <vnet/ip/ip.h>
17 #include <cnat/cnat_snat.h>
18 #include <cnat/cnat_translation.h>
19
20 static void
21 cnat_compute_prefix_lengths_in_search_order (cnat_snat_pfx_table_t *
22                                              table, ip_address_family_t af)
23 {
24   int i;
25   vec_reset_length (table->meta[af].prefix_lengths_in_search_order);
26   /* Note: bitmap reversed so this is in fact a longest prefix match */
27   clib_bitmap_foreach (i, table->meta[af].non_empty_dst_address_length_bitmap)
28      {
29       int dst_address_length = 128 - i;
30       vec_add1 (table->meta[af].prefix_lengths_in_search_order, dst_address_length);
31     }
32 }
33
34 int
35 cnat_add_snat_prefix (ip_prefix_t * pfx)
36 {
37   /* All packets destined to this prefix won't be source-NAT-ed */
38   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
39   clib_bihash_kv_24_8_t kv;
40   ip6_address_t *mask;
41   u64 af = ip_prefix_version (pfx);;
42
43   mask = &table->ip_masks[pfx->len];
44   if (AF_IP4 == af)
45     {
46       kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
47       kv.key[1] = 0;
48     }
49   else
50     {
51       kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
52       kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
53     }
54   kv.key[2] = ((u64) af << 32) | pfx->len;
55   clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 1 /* is_add */ );
56
57   table->meta[af].dst_address_length_refcounts[pfx->len]++;
58   table->meta[af].non_empty_dst_address_length_bitmap =
59     clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
60                      128 - pfx->len, 1);
61   cnat_compute_prefix_lengths_in_search_order (table, af);
62   return 0;
63 }
64
65 int
66 cnat_del_snat_prefix (ip_prefix_t * pfx)
67 {
68   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
69   clib_bihash_kv_24_8_t kv, val;
70   ip6_address_t *mask;
71   u64 af = ip_prefix_version (pfx);;
72
73   mask = &table->ip_masks[pfx->len];
74   if (AF_IP4 == af)
75     {
76       kv.key[0] = (u64) ip_prefix_v4 (pfx).as_u32 & mask->as_u64[0];
77       kv.key[1] = 0;
78     }
79   else
80     {
81       kv.key[0] = ip_prefix_v6 (pfx).as_u64[0] & mask->as_u64[0];
82       kv.key[1] = ip_prefix_v6 (pfx).as_u64[1] & mask->as_u64[1];
83     }
84   kv.key[2] = ((u64) af << 32) | pfx->len;
85
86   if (clib_bihash_search_24_8 (&table->ip_hash, &kv, &val))
87     {
88       return 1;
89     }
90   clib_bihash_add_del_24_8 (&table->ip_hash, &kv, 0 /* is_add */ );
91   /* refcount accounting */
92   ASSERT (table->meta[af].dst_address_length_refcounts[pfx->len] > 0);
93   if (--table->meta[af].dst_address_length_refcounts[pfx->len] == 0)
94     {
95       table->meta[af].non_empty_dst_address_length_bitmap =
96         clib_bitmap_set (table->meta[af].non_empty_dst_address_length_bitmap,
97                          128 - pfx->len, 0);
98       cnat_compute_prefix_lengths_in_search_order (table, af);
99     }
100   return 0;
101 }
102
103 int
104 cnat_search_snat_prefix (ip46_address_t * addr, ip_address_family_t af)
105 {
106   /* Returns 0 if addr matches any of the listed prefixes */
107   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
108   clib_bihash_kv_24_8_t kv, val;
109   int i, n_p, rv;
110   n_p = vec_len (table->meta[af].prefix_lengths_in_search_order);
111   if (AF_IP4 == af)
112     {
113       kv.key[0] = addr->ip4.as_u32;
114       kv.key[1] = 0;
115     }
116   else
117     {
118       kv.key[0] = addr->as_u64[0];
119       kv.key[1] = addr->as_u64[1];
120     }
121
122   /*
123    * start search from a mask length same length or shorter.
124    * we don't want matches longer than the mask passed
125    */
126   i = 0;
127   for (; i < n_p; i++)
128     {
129       int dst_address_length =
130         table->meta[af].prefix_lengths_in_search_order[i];
131       ip6_address_t *mask = &table->ip_masks[dst_address_length];
132
133       ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
134       /* As lengths are decreasing, masks are increasingly specific. */
135       kv.key[0] &= mask->as_u64[0];
136       kv.key[1] &= mask->as_u64[1];
137       kv.key[2] = ((u64) af << 32) | dst_address_length;
138       rv = clib_bihash_search_inline_2_24_8 (&table->ip_hash, &kv, &val);
139       if (rv == 0)
140         return 0;
141     }
142   return -1;
143 }
144
145 u8 *
146 format_cnat_snat_prefix (u8 * s, va_list * args)
147 {
148   clib_bihash_kv_24_8_t *kv = va_arg (*args, clib_bihash_kv_24_8_t *);
149   CLIB_UNUSED (int verbose) = va_arg (*args, int);
150   u32 af = kv->key[2] >> 32;
151   u32 len = kv->key[2] & 0xffffffff;
152   if (AF_IP4 == af)
153     s = format (s, "%U/%d", format_ip4_address, &kv->key[0], len);
154   else
155     s = format (s, "%U/%d", format_ip6_address, &kv->key[0], len);
156   return (s);
157 }
158
159 void
160 cnat_set_snat (ip4_address_t * ip4, ip6_address_t * ip6, u32 sw_if_index)
161 {
162   cnat_lazy_init ();
163
164   cnat_translation_unwatch_addr (INDEX_INVALID, CNAT_RESOLV_ADDR_SNAT);
165
166   ip_address_set (&cnat_main.snat_ip4.ce_ip, ip4, AF_IP4);
167   ip_address_set (&cnat_main.snat_ip6.ce_ip, ip6, AF_IP6);
168   cnat_main.snat_ip4.ce_sw_if_index = sw_if_index;
169   cnat_main.snat_ip6.ce_sw_if_index = sw_if_index;
170
171   cnat_resolve_ep (&cnat_main.snat_ip4);
172   cnat_resolve_ep (&cnat_main.snat_ip6);
173   cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip4,
174                                CNAT_RESOLV_ADDR_SNAT);
175   cnat_translation_watch_addr (INDEX_INVALID, 0, &cnat_main.snat_ip6,
176                                CNAT_RESOLV_ADDR_SNAT);
177 }
178
179 static clib_error_t *
180 cnat_set_snat_cli (vlib_main_t * vm,
181                    unformat_input_t * input, vlib_cli_command_t * cmd)
182 {
183   unformat_input_t _line_input, *line_input = &_line_input;
184   vnet_main_t *vnm = vnet_get_main ();
185   ip4_address_t ip4 = { {0} };
186   ip6_address_t ip6 = { {0} };
187   clib_error_t *e = 0;
188   u32 sw_if_index = INDEX_INVALID;
189
190   cnat_lazy_init ();
191
192   /* Get a line of input. */
193   if (!unformat_user (input, unformat_line_input, line_input))
194     return 0;
195
196   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
197     {
198       if (unformat_user (line_input, unformat_ip4_address, &ip4))
199         ;
200       else if (unformat_user (line_input, unformat_ip6_address, &ip6))
201         ;
202       else if (unformat_user (line_input, unformat_vnet_sw_interface,
203                               vnm, &sw_if_index))
204         ;
205       else
206         {
207           e = clib_error_return (0, "unknown input '%U'",
208                                  format_unformat_error, input);
209           goto done;
210         }
211     }
212
213   cnat_set_snat (&ip4, &ip6, sw_if_index);
214
215 done:
216   unformat_free (line_input);
217
218   return (e);
219 }
220
221 VLIB_CLI_COMMAND (cnat_set_snat_command, static) =
222 {
223   .path = "cnat snat with",
224   .short_help = "cnat snat with [<ip4-address>][<ip6-address>][sw_if_index]",
225   .function = cnat_set_snat_cli,
226 };
227
228 static clib_error_t *
229 cnat_snat_exclude (vlib_main_t * vm,
230                    unformat_input_t * input, vlib_cli_command_t * cmd)
231 {
232   ip_prefix_t pfx;
233   u8 is_add = 1;
234   int rv;
235
236   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
237     {
238       if (unformat (input, "%U", unformat_ip_prefix, &pfx))
239         ;
240       else if (unformat (input, "del"))
241         is_add = 0;
242       else
243         return (clib_error_return (0, "unknown input '%U'",
244                                    format_unformat_error, input));
245     }
246
247   if (is_add)
248     rv = cnat_add_snat_prefix (&pfx);
249   else
250     rv = cnat_del_snat_prefix (&pfx);
251
252   if (rv)
253     {
254       return (clib_error_return (0, "error %d", rv, input));
255     }
256
257   return (NULL);
258 }
259
260 VLIB_CLI_COMMAND (cnat_snat_exclude_command, static) =
261 {
262   .path = "cnat snat exclude",
263   .short_help = "cnat snat exclude [ip]",
264   .function = cnat_snat_exclude,
265 };
266
267 static clib_error_t *
268 cnat_show_snat (vlib_main_t * vm,
269                 unformat_input_t * input, vlib_cli_command_t * cmd)
270 {
271   cnat_snat_pfx_table_t *table = &cnat_main.snat_pfx_table;
272   vlib_cli_output (vm, "Source NAT\nip4: %U\nip6: %U\n",
273                    format_cnat_endpoint, &cnat_main.snat_ip4,
274                    format_cnat_endpoint, &cnat_main.snat_ip6);
275   vlib_cli_output (vm, "Prefixes:\n%U\n",
276                    format_bihash_24_8, &table->ip_hash, 1);
277   return (NULL);
278 }
279
280 VLIB_CLI_COMMAND (cnat_show_snat_command, static) =
281 {
282   .path = "show cnat snat",
283   .short_help = "show cnat snat",
284   .function = cnat_show_snat,
285 };
286
287 static clib_error_t *
288 cnat_snat_init (vlib_main_t * vm)
289 {
290   cnat_main_t *cm = &cnat_main;
291   cnat_snat_pfx_table_t *table = &cm->snat_pfx_table;
292   int i;
293   for (i = 0; i < ARRAY_LEN (table->ip_masks); i++)
294     {
295       u32 j, i0, i1;
296
297       i0 = i / 32;
298       i1 = i % 32;
299
300       for (j = 0; j < i0; j++)
301         table->ip_masks[i].as_u32[j] = ~0;
302
303       if (i1)
304         table->ip_masks[i].as_u32[i0] =
305           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
306     }
307   clib_bihash_init_24_8 (&table->ip_hash, "snat prefixes",
308                          cm->snat_hash_buckets, cm->snat_hash_memory);
309   clib_bihash_set_kvp_format_fn_24_8 (&table->ip_hash,
310                                       format_cnat_snat_prefix);
311
312   return (NULL);
313 }
314
315 VLIB_INIT_FUNCTION (cnat_snat_init);
316
317
318 /*
319  * fd.io coding-style-patch-verification: ON
320  *
321  * Local Variables:
322  * eval: (c-set-style "gnu")
323  * End:
324  */