NAT: remove worker_by_in lookup hash table (VPP-992)
[vpp.git] / src / plugins / nat / nat.h
1
2 /*
3  * nat.h - NAT plugin definitions
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
31
32
33 #define SNAT_UDP_TIMEOUT 300
34 #define SNAT_UDP_TIMEOUT_MIN 120
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_TCP_INCOMING_SYN 6
38 #define SNAT_ICMP_TIMEOUT 60
39
40 #define SNAT_FLAG_HAIRPINNING (1 << 0)
41
42 /* Key */
43 typedef struct {
44   union
45   {
46     struct
47     {
48       ip4_address_t addr;
49       u16 port;
50       u16 protocol:3,
51         fib_index:13;
52     };
53     u64 as_u64;
54   };
55 } snat_session_key_t;
56
57 typedef struct {
58   union
59   {
60     struct
61     {
62       ip4_address_t l_addr;
63       ip4_address_t r_addr;
64       u32 fib_index;
65       u16 l_port;
66       u8 proto;
67       u8 rsvd;
68     };
69     u64 as_u64[2];
70   };
71 } nat_ed_ses_key_t;
72
73 typedef struct {
74   union
75   {
76     struct
77     {
78       ip4_address_t ext_host_addr;
79       u16 ext_host_port;
80       u16 out_port;
81     };
82     u64 as_u64;
83   };
84 } snat_det_out_key_t;
85
86 typedef struct {
87   union
88   {
89     struct
90     {
91       ip4_address_t addr;
92       u32 fib_index;
93     };
94     u64 as_u64;
95   };
96 } snat_user_key_t;
97
98
99 #define foreach_snat_protocol \
100   _(UDP, 0, udp, "udp")       \
101   _(TCP, 1, tcp, "tcp")       \
102   _(ICMP, 2, icmp, "icmp")
103
104 typedef enum {
105 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
106   foreach_snat_protocol
107 #undef _
108 } snat_protocol_t;
109
110
111 #define foreach_snat_session_state          \
112   _(0, UNKNOWN, "unknown")                 \
113   _(1, UDP_ACTIVE, "udp-active")           \
114   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
115   _(3, TCP_ESTABLISHED, "tcp-established") \
116   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
117   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
118   _(6, TCP_LAST_ACK, "tcp-last-ack")       \
119   _(7, ICMP_ACTIVE, "icmp-active")
120
121 typedef enum {
122 #define _(v, N, s) SNAT_SESSION_##N = v,
123   foreach_snat_session_state
124 #undef _
125 } snat_session_state_t;
126
127
128 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
129 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO  2
130 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
131
132 typedef CLIB_PACKED(struct {
133   snat_session_key_t out2in;    /* 0-15 */
134
135   snat_session_key_t in2out;    /* 16-31 */
136
137   u32 flags;                    /* 32-35 */
138
139   /* per-user translations */
140   u32 per_user_index;           /* 36-39 */
141
142   u32 per_user_list_head_index; /* 40-43 */
143
144   /* Last heard timer */
145   f64 last_heard;               /* 44-51 */
146
147   u64 total_bytes;              /* 52-59 */
148
149   u32 total_pkts;               /* 60-63 */
150
151   /* Outside address */
152   u32 outside_address_index;    /* 64-67 */
153
154   /* External host address */
155   ip4_address_t ext_host_addr;  /* 68-71 */
156
157 }) snat_session_t;
158
159
160 typedef struct {
161   ip4_address_t addr;
162   u32 fib_index;
163   u32 sessions_per_user_list_head_index;
164   u32 nsessions;
165   u32 nstaticsessions;
166 } snat_user_t;
167
168 typedef struct {
169   ip4_address_t addr;
170   u32 fib_index;
171 #define _(N, i, n, s) \
172   u16 busy_##n##_ports; \
173   u16 * busy_##n##_ports_per_thread; \
174   uword * busy_##n##_port_bitmap;
175   foreach_snat_protocol
176 #undef _
177 } snat_address_t;
178
179 typedef struct {
180   u16 in_port;
181   snat_det_out_key_t out;
182   u8 state;
183   u32 expire;
184 } snat_det_session_t;
185
186 typedef struct {
187   ip4_address_t in_addr;
188   u8 in_plen;
189   ip4_address_t out_addr;
190   u8 out_plen;
191   u32 sharing_ratio;
192   u16 ports_per_host;
193   u32 ses_num;
194   /* vector of sessions */
195   snat_det_session_t * sessions;
196 } snat_det_map_t;
197
198 typedef struct {
199   ip4_address_t addr;
200   u16 port;
201   u8 probability;
202   u8 prefix;
203 } nat44_lb_addr_port_t;
204
205 typedef struct {
206   ip4_address_t local_addr;
207   ip4_address_t external_addr;
208   u16 local_port;
209   u16 external_port;
210   u8 addr_only;
211   u32 vrf_id;
212   u32 fib_index;
213   snat_protocol_t proto;
214   u32 worker_index;
215   nat44_lb_addr_port_t *locals;
216 } snat_static_mapping_t;
217
218 typedef struct {
219   u32 sw_if_index;
220   u8 is_inside;
221 } snat_interface_t;
222
223 typedef struct {
224   ip4_address_t l_addr;
225   u16 l_port;
226   u16 e_port;
227   u32 sw_if_index;
228   u32 vrf_id;
229   snat_protocol_t proto;
230   int addr_only;
231   int is_add;
232 } snat_static_map_resolve_t;
233
234 typedef struct {
235   /* Main lookup tables */
236   clib_bihash_8_8_t out2in;
237   clib_bihash_8_8_t in2out;
238
239   /* Find-a-user => src address lookup */
240   clib_bihash_8_8_t user_hash;
241
242   /* User pool */
243   snat_user_t * users;
244
245   /* Session pool */
246   snat_session_t * sessions;
247
248   /* Pool of doubly-linked list elements */
249   dlist_elt_t * list_pool;
250
251   u32 snat_thread_index;
252 } snat_main_per_thread_data_t;
253
254 struct snat_main_s;
255
256 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
257                                         vlib_node_runtime_t *node,
258                                         u32 thread_index,
259                                         vlib_buffer_t *b0,
260                                         u8 *p_proto,
261                                         snat_session_key_t *p_value,
262                                         u8 *p_dont_translate,
263                                         void *d,
264                                         void *e);
265
266 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
267
268 typedef struct snat_main_s {
269   /* Endpoint address dependent sessions lookup tables */
270   clib_bihash_16_8_t out2in_ed;
271   clib_bihash_16_8_t in2out_ed;
272
273   snat_icmp_match_function_t * icmp_match_in2out_cb;
274   snat_icmp_match_function_t * icmp_match_out2in_cb;
275
276   u32 num_workers;
277   u32 first_worker_index;
278   u32 next_worker;
279   u32 * workers;
280   snat_get_worker_function_t * worker_in2out_cb;
281   snat_get_worker_function_t * worker_out2in_cb;
282   u16 port_per_thread;
283   u32 num_snat_thread;
284
285   /* Per thread data */
286   snat_main_per_thread_data_t * per_thread_data;
287
288   /* Find a static mapping by local */
289   clib_bihash_8_8_t static_mapping_by_local;
290
291   /* Find a static mapping by external */
292   clib_bihash_8_8_t static_mapping_by_external;
293
294   /* Static mapping pool */
295   snat_static_mapping_t * static_mappings;
296
297   /* Interface pool */
298   snat_interface_t * interfaces;
299   snat_interface_t * output_feature_interfaces;
300
301   /* Vector of outside addresses */
302   snat_address_t * addresses;
303
304   /* sw_if_indices whose intfc addresses should be auto-added */
305   u32 * auto_add_sw_if_indices;
306
307   /* vector of interface address static mappings to resolve. */
308   snat_static_map_resolve_t *to_resolve;
309
310   /* Randomize port allocation order */
311   u32 random_seed;
312
313   /* Worker handoff index */
314   u32 fq_in2out_index;
315   u32 fq_in2out_output_index;
316   u32 fq_out2in_index;
317
318   /* in2out and out2in node index */
319   u32 in2out_node_index;
320   u32 in2out_output_node_index;
321   u32 out2in_node_index;
322
323   /* Deterministic NAT */
324   snat_det_map_t * det_maps;
325
326   /* Config parameters */
327   u8 static_mapping_only;
328   u8 static_mapping_connection_tracking;
329   u8 deterministic;
330   u32 translation_buckets;
331   u32 translation_memory_size;
332   u32 max_translations;
333   u32 user_buckets;
334   u32 user_memory_size;
335   u32 max_translations_per_user;
336   u32 outside_vrf_id;
337   u32 outside_fib_index;
338   u32 inside_vrf_id;
339   u32 inside_fib_index;
340
341   /* tenant VRF aware address pool activation flag */
342   u8 vrf_mode;
343
344   /* values of various timeouts */
345   u32 udp_timeout;
346   u32 tcp_established_timeout;
347   u32 tcp_transitory_timeout;
348   u32 icmp_timeout;
349
350   /* API message ID base */
351   u16 msg_id_base;
352
353   /* convenience */
354   vlib_main_t * vlib_main;
355   vnet_main_t * vnet_main;
356   ip4_main_t * ip4_main;
357   ip_lookup_main_t * ip4_lookup_main;
358   api_main_t * api_main;
359 } snat_main_t;
360
361 extern snat_main_t snat_main;
362 extern vlib_node_registration_t snat_in2out_node;
363 extern vlib_node_registration_t snat_in2out_output_node;
364 extern vlib_node_registration_t snat_out2in_node;
365 extern vlib_node_registration_t snat_in2out_fast_node;
366 extern vlib_node_registration_t snat_out2in_fast_node;
367 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
368 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
369 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
370 extern vlib_node_registration_t snat_det_in2out_node;
371 extern vlib_node_registration_t snat_det_out2in_node;
372 extern vlib_node_registration_t snat_hairpin_dst_node;
373 extern vlib_node_registration_t snat_hairpin_src_node;
374
375 void snat_free_outside_address_and_port (snat_main_t * sm,
376                                          u32 thread_index,
377                                          snat_session_key_t * k,
378                                          u32 address_index);
379
380 int snat_alloc_outside_address_and_port (snat_main_t * sm,
381                                          u32 fib_index,
382                                          u32 thread_index,
383                                          snat_session_key_t * k,
384                                          u32 * address_indexp);
385
386 int snat_static_mapping_match (snat_main_t * sm,
387                                snat_session_key_t match,
388                                snat_session_key_t * mapping,
389                                u8 by_external,
390                                u8 *is_addr_only);
391
392 void snat_add_del_addr_to_fib (ip4_address_t * addr,
393                                u8 p_len,
394                                u32 sw_if_index,
395                                int is_add);
396
397 format_function_t format_snat_user;
398
399 typedef struct {
400   u32 cached_sw_if_index;
401   u32 cached_ip4_address;
402 } snat_runtime_t;
403
404 /** \brief Check if SNAT session is created from static mapping.
405     @param s SNAT session
406     @return 1 if SNAT session is created from static mapping otherwise 0
407 */
408 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
409
410 /** \brief Check if SNAT session for unknown protocol.
411     @param s SNAT session
412     @return 1 if SNAT session for unknown protocol otherwise 0
413 */
414 #define snat_is_unk_proto_session(s) s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO
415
416 /*
417  * Why is this here? Because we don't need to touch this layer to
418  * simply reply to an icmp. We need to change id to a unique
419  * value to NAT an echo request/reply.
420  */
421
422 typedef struct {
423   u16 identifier;
424   u16 sequence;
425 } icmp_echo_header_t;
426
427 always_inline u32
428 ip_proto_to_snat_proto (u8 ip_proto)
429 {
430   u32 snat_proto = ~0;
431
432   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
433   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
434   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
435   snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
436
437   return snat_proto;
438 }
439
440 always_inline u8
441 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
442 {
443   u8 ip_proto = ~0;
444
445   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
446   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
447   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
448
449   return ip_proto;
450 }
451
452 typedef struct {
453   u16 src_port, dst_port;
454 } tcp_udp_header_t;
455
456 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
457                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
458                            snat_session_key_t *p_value,
459                            u8 *p_dont_translate, void *d, void *e);
460 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
461                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
462                            snat_session_key_t *p_value,
463                            u8 *p_dont_translate, void *d, void *e);
464 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
465                           u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
466                           snat_session_key_t *p_value,
467                           u8 *p_dont_translate, void *d, void *e);
468 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
469                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
470                            snat_session_key_t *p_value,
471                            u8 *p_dont_translate, void *d, void *e);
472 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
473                            u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
474                            snat_session_key_t *p_value,
475                            u8 *p_dont_translate, void *d, void *e);
476 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
477                           u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
478                           snat_session_key_t *p_value,
479                           u8 *p_dont_translate, void *d, void *e);
480 void increment_v4_address(ip4_address_t * a);
481 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id);
482 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
483 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
484                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
485                             u32 sw_if_index, snat_protocol_t proto, int is_add);
486 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
487 int snat_set_workers (uword * bitmap);
488 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
489 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
490                                           int is_del);
491 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
492 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
493 u8 * format_snat_protocol(u8 * s, va_list * args);
494 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
495                                      snat_protocol_t proto, u32 vrf_id,
496                                      nat44_lb_addr_port_t *locals, u8 is_add);
497
498 static_always_inline u8
499 icmp_is_error_message (icmp46_header_t * icmp)
500 {
501   switch(icmp->type)
502     {
503     case ICMP4_destination_unreachable:
504     case ICMP4_time_exceeded:
505     case ICMP4_parameter_problem:
506     case ICMP4_source_quench:
507     case ICMP4_redirect:
508     case ICMP4_alternate_host_address:
509       return 1;
510     }
511   return 0;
512 }
513
514 static_always_inline u8
515 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
516                   u32 ip4_addr)
517 {
518   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
519   ip4_address_t * first_int_addr;
520
521   if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
522     {
523       first_int_addr =
524         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
525                                      0 /* just want the address */);
526       rt->cached_sw_if_index = sw_if_index0;
527       if (first_int_addr)
528         rt->cached_ip4_address = first_int_addr->as_u32;
529       else
530         rt->cached_ip4_address = 0;
531     }
532
533   if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
534     return 1;
535   else
536     return 0;
537 }
538
539 always_inline u8
540 maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
541 {
542   if (pool_elts (sm->per_thread_data[thread_index].sessions) >= sm->max_translations)
543     return 1;
544
545   return 0;
546 }
547
548 #endif /* __included_nat_h__ */