cea9ed71c8c07781d35f0a61eb0925d46a2030eb
[vpp.git] / src / plugins / nat / lib / lib.h
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  * @file
17  * @brief NAT port/address allocation lib
18  */
19 #ifndef included_nat_lib_h__
20 #define included_nat_lib_h__
21
22 #include <vlibapi/api.h>
23
24 /* NAT API Configuration flags */
25 #define foreach_nat_config_flag \
26   _(0x01, IS_TWICE_NAT)         \
27   _(0x02, IS_SELF_TWICE_NAT)    \
28   _(0x04, IS_OUT2IN_ONLY)       \
29   _(0x08, IS_ADDR_ONLY)         \
30   _(0x10, IS_OUTSIDE)           \
31   _(0x20, IS_INSIDE)            \
32   _(0x40, IS_STATIC)            \
33   _(0x80, IS_EXT_HOST_VALID)
34
35 typedef enum nat_config_flags_t_
36 {
37 #define _(n,f) NAT_API_##f = n,
38   foreach_nat_config_flag
39 #undef _
40 } nat_config_flags_t;
41
42 #define foreach_nat_counter _ (tcp) _ (udp) _ (icmp) _ (other) _ (drops)
43
44 #define foreach_nat_error                      \
45   _ (VALUE_EXIST, -1, "Value already exists")  \
46   _ (NO_SUCH_ENTRY, -2, "No such entry")       \
47   _ (UNKNOWN_PROTOCOL, -3, "Unknown protocol") \
48   _ (OUT_OF_TRANSLATIONS, -4, "Out of translations")
49
50 typedef enum
51 {
52 #define _(N, i, s) NAT_ERROR_##N = i,
53   foreach_nat_error
54 #undef _
55 } nat_error_t;
56
57 /* default protocol timeouts */
58 #define NAT_UDP_TIMEOUT 300
59 #define NAT_TCP_TRANSITORY_TIMEOUT 240
60 #define NAT_TCP_ESTABLISHED_TIMEOUT 7440
61 #define NAT_ICMP_TIMEOUT 60
62
63 typedef struct
64 {
65   struct
66   {
67     u32 established;
68     u32 transitory;
69   } tcp;
70
71   u32 udp;
72   u32 icmp;
73
74 } nat_timeouts_t;
75
76 static_always_inline void
77 nat_reset_timeouts (nat_timeouts_t * timeouts)
78 {
79   timeouts->udp = NAT_UDP_TIMEOUT;
80   timeouts->tcp.established = NAT_TCP_ESTABLISHED_TIMEOUT;
81   timeouts->tcp.transitory = NAT_TCP_TRANSITORY_TIMEOUT;
82   timeouts->icmp = NAT_ICMP_TIMEOUT;
83 }
84
85 static_always_inline u32
86 nat_calc_bihash_buckets (u32 n_elts)
87 {
88   n_elts = n_elts / 2.5;
89   u64 lower_pow2 = 1;
90   while (lower_pow2 * 2 < n_elts)
91     {
92       lower_pow2 = 2 * lower_pow2;
93     }
94   u64 upper_pow2 = 2 * lower_pow2;
95   if ((upper_pow2 - n_elts) < (n_elts - lower_pow2))
96     {
97       if (upper_pow2 <= UINT32_MAX)
98         {
99           return upper_pow2;
100         }
101     }
102   return lower_pow2;
103 }
104
105 #endif /* included_nat_lib_h__ */
106 /*
107  * fd.io coding-style-patch-verification: ON
108  *
109  * Local Variables:
110  * eval: (c-set-style "gnu")
111  * End:
112  */