FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
lib.h
Go to the documentation of this file.
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,
39 #undef _
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,
54 #undef _
55 } nat_error_t;
56 
57 #define foreach_nat_protocol \
58  _ (OTHER, 0, other, "other") \
59  _ (UDP, 1, udp, "udp") \
60  _ (TCP, 2, tcp, "tcp") \
61  _ (ICMP, 3, icmp, "icmp")
62 
63 typedef enum
64 {
65 #define _(N, i, n, s) NAT_PROTOCOL_##N = i,
67 #undef _
69 
70 /* default protocol timeouts */
71 #define NAT_UDP_TIMEOUT 300
72 #define NAT_TCP_TRANSITORY_TIMEOUT 240
73 #define NAT_TCP_ESTABLISHED_TIMEOUT 7440
74 #define NAT_ICMP_TIMEOUT 60
75 
76 typedef struct
77 {
78  struct
79  {
82  } tcp;
83 
86 
88 
91 {
92  timeouts->udp = NAT_UDP_TIMEOUT;
95  timeouts->icmp = NAT_ICMP_TIMEOUT;
96 }
97 
100  u8 state)
101 {
102  switch (proto)
103  {
104  case NAT_PROTOCOL_ICMP:
105  return timeouts->icmp;
106  case NAT_PROTOCOL_UDP:
107  return timeouts->udp;
108  case NAT_PROTOCOL_TCP:
109  {
110  if (state)
111  return timeouts->tcp.transitory;
112  else
113  return timeouts->tcp.established;
114  }
115  default:
116  return timeouts->udp;
117  }
118  return 0;
119 }
120 
123 {
124  n_elts = n_elts / 2.5;
125  u64 lower_pow2 = 1;
126  while (lower_pow2 * 2 < n_elts)
127  {
128  lower_pow2 = 2 * lower_pow2;
129  }
130  u64 upper_pow2 = 2 * lower_pow2;
131  if ((upper_pow2 - n_elts) < (n_elts - lower_pow2))
132  {
133  if (upper_pow2 <= UINT32_MAX)
134  {
135  return upper_pow2;
136  }
137  }
138  return lower_pow2;
139 }
140 
141 u8 *format_nat_protocol (u8 *s, va_list *args);
142 
143 uword unformat_nat_protocol (unformat_input_t *input, va_list *args);
144 
145 #endif /* included_nat_lib_h__ */
146 /*
147  * fd.io coding-style-patch-verification: ON
148  *
149  * Local Variables:
150  * eval: (c-set-style "gnu")
151  * End:
152  */
nat_timeouts_t::udp
u32 udp
Definition: lib.h:84
NAT_TCP_TRANSITORY_TIMEOUT
#define NAT_TCP_TRANSITORY_TIMEOUT
Definition: lib.h:72
nat_timeouts_t::icmp
u32 icmp
Definition: lib.h:85
nat_timeouts_t::tcp
struct nat_timeouts_t::@732 tcp
nat_timeouts_t
Definition: lib.h:76
nat_protocol_t
nat_protocol_t
Definition: lib.h:63
api.h
state
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
nat_timeouts_t::transitory
u32 transitory
Definition: lib.h:81
unformat_input_t
struct _unformat_input_t unformat_input_t
foreach_nat_config_flag
#define foreach_nat_config_flag
Definition: lib.h:25
nat_config_flags_t
enum nat_config_flags_t_ nat_config_flags_t
nat_error_t
nat_error_t
Definition: lib.h:50
nat_timeouts_t::established
u32 established
Definition: lib.h:80
foreach_nat_error
#define foreach_nat_error
Definition: lib.h:44
unformat_nat_protocol
uword unformat_nat_protocol(unformat_input_t *input, va_list *args)
Definition: nat64.c:1613
static_always_inline
#define static_always_inline
Definition: clib.h:112
nat_calc_bihash_buckets
static_always_inline u32 nat_calc_bihash_buckets(u32 n_elts)
Definition: lib.h:122
uword
u64 uword
Definition: types.h:112
NAT_ICMP_TIMEOUT
#define NAT_ICMP_TIMEOUT
Definition: lib.h:74
nat_config_flags_t_
nat_config_flags_t_
Definition: lib.h:35
u64
unsigned long u64
Definition: types.h:89
nat_reset_timeouts
static_always_inline void nat_reset_timeouts(nat_timeouts_t *timeouts)
Definition: lib.h:90
u32
unsigned int u32
Definition: types.h:88
u8
unsigned char u8
Definition: types.h:56
nat_session_get_timeout
static_always_inline u32 nat_session_get_timeout(nat_timeouts_t *timeouts, nat_protocol_t proto, u8 state)
Definition: lib.h:99
foreach_nat_protocol
#define foreach_nat_protocol
Definition: lib.h:57
format_nat_protocol
u8 * format_nat_protocol(u8 *s, va_list *args)
Definition: nat64.c:1627
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
NAT_TCP_ESTABLISHED_TIMEOUT
#define NAT_TCP_ESTABLISHED_TIMEOUT
Definition: lib.h:73
NAT_UDP_TIMEOUT
#define NAT_UDP_TIMEOUT
Definition: lib.h:71