FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
cnat_types.c
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 #include <cnat/cnat_types.h>
17 
21 
22 char *cnat_error_strings[] = {
23 #define cnat_error(n,s) s,
24 #include <cnat/cnat_error.def>
25 #undef cnat_error
26 };
27 
28 u8
31 {
32  /* Tries to resolve IP from sw_if_index
33  * returns 1 if we need to schedule DHCP */
35  return 0;
36  if (af == AF_IP6)
37  {
38  ip6_address_t *ip6 = 0;
40  if (ip6)
41  {
43  return 0;
44  }
45  else
46  return 1;
47  }
48  else
49  {
50  ip4_address_t *ip4 = 0;
52  if (ip4)
53  {
55  return 0;
56  }
57  else
58  return 1;
59  }
60 }
61 
62 u8
64 {
65  int rv;
67  if (0 == rv)
69  return rv;
70 }
71 
72 uword
73 unformat_cnat_ep (unformat_input_t * input, va_list * args)
74 {
75  cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
76  vnet_main_t *vnm = vnet_get_main ();
77  int port = 0;
78 
79  clib_memset (a, 0, sizeof (*a));
80  a->ce_sw_if_index = INDEX_INVALID;
81  if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
82  ;
83  else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
84  ;
85  else if (unformat (input, "%U v6 %d", unformat_vnet_sw_interface,
86  vnm, &a->ce_sw_if_index, &port))
87  a->ce_ip.version = AF_IP6;
88  else if (unformat (input, "%U v6", unformat_vnet_sw_interface,
89  vnm, &a->ce_sw_if_index))
90  a->ce_ip.version = AF_IP6;
91  else if (unformat (input, "%U %d", unformat_vnet_sw_interface,
92  vnm, &a->ce_sw_if_index, &port))
93  a->ce_ip.version = AF_IP4;
95  vnm, &a->ce_sw_if_index))
96  a->ce_ip.version = AF_IP4;
97  else if (unformat (input, "%d", &port))
98  ;
99  else
100  return 0;
101  a->ce_port = (u16) port;
102  return 1;
103 }
104 
105 uword
107 {
108  int *a = va_arg (*args, int *);
109  if (unformat (input, ":nonat"))
111  return 1;
112 }
113 
114 uword
115 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
116 {
117  cnat_endpoint_tuple_t *a = va_arg (*args, cnat_endpoint_tuple_t *);
118  int flgs = 0;
119  if (unformat (input, "%U->%U%U", unformat_cnat_ep, &a->src_ep,
120  unformat_cnat_ep, &a->dst_ep, unformat_cnat_ep_flags, &flgs))
121  a->ep_flags = flgs;
122  else if (unformat (input, "->%U%U", unformat_cnat_ep, &a->dst_ep,
123  unformat_cnat_ep_flags, &flgs))
124  a->ep_flags = flgs;
125  else if (unformat (input, "%U->%U", unformat_cnat_ep, &a->src_ep,
126  unformat_cnat_ep_flags, &flgs))
127  a->ep_flags = flgs;
128  else
129  return 0;
130  return 1;
131 }
132 
133 u8 *
134 format_cnat_endpoint (u8 * s, va_list * args)
135 {
136  cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
137  vnet_main_t *vnm = vnet_get_main ();
138  if (INDEX_INVALID == cep->ce_sw_if_index)
139  s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
140  else
141  {
142  if (cep->ce_flags & CNAT_EP_FLAG_RESOLVED)
143  s = format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
145  cep->ce_port);
146  else
147  s =
148  format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
150  cep->ce_ip.version, cep->ce_port);
151  }
152  return (s);
153 }
154 
155 static clib_error_t *
157 {
161 
162 
164 
165  return (NULL);
166 }
167 
168 void
170 {
173 }
174 
175 void
177 {
179  if (cm->lazy_init_done)
180  return;
181  cnat_enable_disable_scanner (cm->default_scanner_state);
182  cm->lazy_init_done = 1;
183 }
184 
185 static clib_error_t *
187 {
189 
190  cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
191  cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
192  cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
193  cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
194  cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
195  cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
196  cm->snat_if_map_length = CNAT_DEFAULT_SNAT_IF_MAP_LEN;
197  cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
198  cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
199  cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
200  cm->default_scanner_state = CNAT_SCANNER_ON;
201  cm->maglev_len = CNAT_DEFAULT_MAGLEV_LEN;
202  cm->lazy_init_done = 0;
203 
205  {
206  if (unformat
207  (input, "session-db-buckets %u", &cm->session_hash_buckets))
208  ;
209  else if (unformat (input, "session-db-memory %U",
210  unformat_memory_size, &cm->session_hash_memory))
211  ;
212  else if (unformat (input, "translation-db-buckets %u",
213  &cm->translation_hash_buckets))
214  ;
215  else if (unformat (input, "translation-db-memory %U",
216  unformat_memory_size, &cm->translation_hash_memory))
217  ;
218  else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
219  ;
220  else if (unformat (input, "snat-if-map-len %u", &cm->snat_if_map_length))
221  ;
222  else if (unformat (input, "snat-db-memory %U",
223  unformat_memory_size, &cm->snat_hash_memory))
224  ;
225  else if (unformat (input, "session-cleanup-timeout %f",
226  &cm->scanner_timeout))
227  ;
228  else if (unformat (input, "scanner off"))
229  cm->default_scanner_state = CNAT_SCANNER_OFF;
230  else if (unformat (input, "scanner on"))
231  cm->default_scanner_state = CNAT_SCANNER_ON;
232  else if (unformat (input, "session-max-age %u", &cm->session_max_age))
233  ;
234  else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
235  ;
236  else if (unformat (input, "maglev-len %u", &cm->maglev_len))
237  ;
238  else
239  return clib_error_return (0, "unknown input '%U'",
240  format_unformat_error, input);
241  }
242 
243  return 0;
244 }
245 
246 cnat_main_t *
248 {
249  return &cnat_main;
250 }
251 
254 
255 /*
256  * fd.io coding-style-patch-verification: ON
257  *
258  * Local Variables:
259  * eval: (c-set-style "gnu")
260  * End:
261  */
ip_address
Definition: ip_types.h:79
CNAT_FIB_SOURCE_PRIORITY
#define CNAT_FIB_SOURCE_PRIORITY
Definition: cnat_types.h:51
CNAT_EP_FLAG_RESOLVED
@ CNAT_EP_FLAG_RESOLVED
Definition: cnat_types.h:70
CNAT_DEFAULT_SESSION_BUCKETS
#define CNAT_DEFAULT_SESSION_BUCKETS
Definition: cnat_types.h:37
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
cnat_resolve_ep
u8 cnat_resolve_ep(cnat_endpoint_t *ep)
Resolve endpoint address.
Definition: cnat_types.c:63
ip4
vl_api_ip4_address_t ip4
Definition: one.api:376
ip4_main
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1105
cnat_main
cnat_main_t cnat_main
Definition: cnat_types.c:18
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
CNAT_SCANNER_OFF
@ CNAT_SCANNER_OFF
Definition: cnat_types.h:184
cnat_endpoint_t_::ce_sw_if_index
u32 ce_sw_if_index
Definition: cnat_types.h:76
cnat_error_strings
char * cnat_error_strings[]
Definition: cnat_types.c:22
CNAT_DEFAULT_SNAT_MEMORY
#define CNAT_DEFAULT_SNAT_MEMORY
Definition: cnat_types.h:44
u16
unsigned short u16
Definition: types.h:57
cnat_get_main
cnat_main_t * cnat_get_main()
Definition: cnat_types.c:247
cnat_timestamps
cnat_timestamp_t * cnat_timestamps
Definition: cnat_types.c:20
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
AF_IP4
@ AF_IP4
Definition: ip_types.h:23
port
u16 port
Definition: lb_types.api:73
CNAT_DEFAULT_SESSION_MAX_AGE
#define CNAT_DEFAULT_SESSION_MAX_AGE
Definition: cnat_types.h:30
unformat_input_t
struct _unformat_input_t unformat_input_t
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
vlib_process_signal_event
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1019
cnat_endpoint_t_
Definition: cnat_types.h:73
VLIB_EARLY_CONFIG_FUNCTION
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:220
cnat_types.h
cnat_endpoint_tuple_t_
Definition: cnat_types.h:81
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
CNAT_SCANNER_ON
@ CNAT_SCANNER_ON
Definition: cnat_types.h:185
unformat_ip_address
uword unformat_ip_address(unformat_input_t *input, va_list *args)
Definition: ip_types.c:41
CNAT_DEFAULT_SNAT_IF_MAP_LEN
#define CNAT_DEFAULT_SNAT_IF_MAP_LEN
Definition: cnat_types.h:40
unformat_cnat_ep_tuple
uword unformat_cnat_ep_tuple(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:115
unformat_memory_size
unformat_function_t unformat_memory_size
Definition: format.h:288
format_ip_address_family
u8 * format_ip_address_family(u8 *s, va_list *args)
Definition: ip.c:215
CNAT_TRK_FLAG_NO_NAT
@ CNAT_TRK_FLAG_NO_NAT
Definition: cnat_types.h:64
ip_address::version
ip_address_family_t version
Definition: ip_types.h:82
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
cnat_main_::ts_lock
clib_rwlock_t ts_lock
Definition: cnat_types.h:129
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
vlib_config_function_runtime_t
Definition: init.h:68
CNAT_DEFAULT_TCP_MAX_AGE
#define CNAT_DEFAULT_TCP_MAX_AGE
Definition: cnat_types.h:32
format_ip_address
u8 * format_ip_address(u8 *s, va_list *args)
Definition: ip_types.c:21
uword
u64 uword
Definition: types.h:112
CNAT_DEFAULT_TRANSLATION_MEMORY
#define CNAT_DEFAULT_TRANSLATION_MEMORY
Definition: cnat_types.h:43
CNAT_DEFAULT_TRANSLATION_BUCKETS
#define CNAT_DEFAULT_TRANSLATION_BUCKETS
Definition: cnat_types.h:38
cm
vnet_feature_config_main_t * cm
Definition: nat44_ei_hairpinning.c:591
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
ip_address_set
void ip_address_set(ip_address_t *dst, const void *src, ip_address_family_t af)
Definition: ip_types.c:207
ip4_address_t
Definition: ip4_packet.h:50
fib_source_allocate
fib_source_t fib_source_allocate(const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:118
clib_rwlock_init
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:152
cnat_timestamp_t_
Definition: cnat_types.h:145
ip6_main
ip6_main_t ip6_main
Definition: ip6_forward.c:2787
vnet_main_t
Definition: vnet.h:76
ip6_interface_first_address
ip6_address_t * ip6_interface_first_address(ip6_main_t *im, u32 sw_if_index)
get first IPv6 interface address
Definition: ip6_forward.c:278
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
format
description fragment has unexpected format
Definition: map.api:433
cnat_types_init
static clib_error_t * cnat_types_init(vlib_main_t *vm)
Definition: cnat_types.c:156
cnat_fib_source
fib_source_t cnat_fib_source
Definition: cnat_types.c:19
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
cnat_lazy_init
void cnat_lazy_init()
Lazy initialization when first adding a translation or using snat.
Definition: cnat_types.c:176
unformat_cnat_ep_flags
uword unformat_cnat_ep_flags(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:106
cnat_scanner_cmd_t
enum cnat_scanner_cmd_t_ cnat_scanner_cmd_t
ip6
vl_api_ip6_address_t ip6
Definition: one.api:424
CNAT_DEFAULT_SCANNER_TIMEOUT
#define CNAT_DEFAULT_SCANNER_TIMEOUT
Definition: cnat_types.h:35
cnat_main_::scanner_node_index
uword scanner_node_index
Definition: cnat_types.h:132
AF_IP6
@ AF_IP6
Definition: ip_types.h:24
cnat_enable_disable_scanner
void cnat_enable_disable_scanner(cnat_scanner_cmd_t event_type)
Enable/Disable session cleanup.
Definition: cnat_types.c:169
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
cnat_endpoint_t_::ce_port
u16 ce_port
Definition: cnat_types.h:77
vlib_main_t
Definition: main.h:102
format_cnat_endpoint
u8 * format_cnat_endpoint(u8 *s, va_list *args)
Definition: cnat_types.c:134
cnat_endpoint_t_::ce_ip
ip_address_t ce_ip
Definition: cnat_types.h:75
cnat_config
static clib_error_t * cnat_config(vlib_main_t *vm, unformat_input_t *input)
Definition: cnat_types.c:186
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:544
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
FIB_SOURCE_BH_SIMPLE
@ FIB_SOURCE_BH_SIMPLE
add paths without path extensions
Definition: fib_source.h:210
cnat_resolve_addr
u8 cnat_resolve_addr(u32 sw_if_index, ip_address_family_t af, ip_address_t *addr)
Definition: cnat_types.c:29
fib_source_t
enum fib_source_t_ fib_source_t
The different sources that can create a route.
rv
int __clib_unused rv
Definition: application.c:491
ip4_interface_first_address
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:281
cnat_endpoint_t_::ce_flags
u8 ce_flags
Definition: cnat_types.h:78
INDEX_INVALID
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:49
CNAT_DEFAULT_MAGLEV_LEN
#define CNAT_DEFAULT_MAGLEV_LEN
Definition: cnat_types.h:47
CNAT_DEFAULT_SESSION_MEMORY
#define CNAT_DEFAULT_SESSION_MEMORY
Definition: cnat_types.h:42
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
CNAT_DEFAULT_SNAT_BUCKETS
#define CNAT_DEFAULT_SNAT_BUCKETS
Definition: cnat_types.h:39
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
unformat_cnat_ep
uword unformat_cnat_ep(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:73
cnat_error.def
ip_address_family_t
enum ip_address_family_t_ ip_address_family_t
cnat_main_
Definition: cnat_types.h:94