FD.io VPP  v21.01.1
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 
22 
23 char *cnat_error_strings[] = {
24 #define cnat_error(n,s) s,
25 #include <cnat/cnat_error.def>
26 #undef cnat_error
27 };
28 
29 u8
32 {
33  /* Tries to resolve IP from sw_if_index
34  * returns 1 if we need to schedule DHCP */
35  if (INDEX_INVALID == sw_if_index)
36  return 0;
37  if (af == AF_IP6)
38  {
39  ip6_address_t *ip6 = 0;
40  ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
41  if (ip6)
42  {
43  ip_address_set (addr, ip6, AF_IP6);
44  return 0;
45  }
46  else
47  return 1;
48  }
49  else
50  {
51  ip4_address_t *ip4 = 0;
52  ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
53  if (ip4)
54  {
55  ip_address_set (addr, ip4, AF_IP4);
56  return 0;
57  }
58  else
59  return 1;
60  }
61 }
62 
63 u8
65 {
66  int rv;
67  rv = cnat_resolve_addr (ep->ce_sw_if_index, ep->ce_ip.version, &ep->ce_ip);
68  if (0 == rv)
70  return rv;
71 }
72 
73 uword
74 unformat_cnat_ep (unformat_input_t * input, va_list * args)
75 {
76  cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
77  vnet_main_t *vnm = vnet_get_main ();
78  int port = 0;
79 
80  clib_memset (a, 0, sizeof (*a));
82  if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
83  ;
84  else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
85  ;
86  else if (unformat (input, "%U v6 %d", unformat_vnet_sw_interface,
87  vnm, &a->ce_sw_if_index, &port))
88  a->ce_ip.version = AF_IP6;
89  else if (unformat (input, "%U v6", unformat_vnet_sw_interface,
90  vnm, &a->ce_sw_if_index))
91  a->ce_ip.version = AF_IP6;
92  else if (unformat (input, "%U %d", unformat_vnet_sw_interface,
93  vnm, &a->ce_sw_if_index, &port))
94  a->ce_ip.version = AF_IP4;
96  vnm, &a->ce_sw_if_index))
97  a->ce_ip.version = AF_IP4;
98  else if (unformat (input, "%d", &port))
99  ;
100  else
101  return 0;
102  a->ce_port = (u16) port;
103  return 1;
104 }
105 
106 uword
107 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
108 {
109  cnat_endpoint_tuple_t *a = va_arg (*args, cnat_endpoint_tuple_t *);
110  if (unformat (input, "%U->%U", unformat_cnat_ep, &a->src_ep,
111  unformat_cnat_ep, &a->dst_ep))
112  ;
113  else if (unformat (input, "->%U", unformat_cnat_ep, &a->dst_ep))
114  ;
115  else if (unformat (input, "%U->", unformat_cnat_ep, &a->src_ep))
116  ;
117  else
118  return 0;
119  return 1;
120 }
121 
122 u8 *
123 format_cnat_endpoint (u8 * s, va_list * args)
124 {
125  cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
126  vnet_main_t *vnm = vnet_get_main ();
127  if (INDEX_INVALID == cep->ce_sw_if_index)
128  s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
129  else
130  {
131  if (cep->ce_flags & CNAT_EP_FLAG_RESOLVED)
132  s = format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
134  cep->ce_port);
135  else
136  s =
137  format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
139  cep->ce_ip.version, cep->ce_port);
140  }
141  return (s);
142 }
143 
144 static clib_error_t *
146 {
148  u32 n_vlib_mains = tm->n_vlib_mains;
152 
153 
154  clib_rwlock_init (&cnat_main.ts_lock);
155  throttle_init (&cnat_throttle, n_vlib_mains, 1e-3);
156 
157  return (NULL);
158 }
159 
160 void
162 {
164  vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
165 }
166 
167 void
169 {
171  if (cm->lazy_init_done)
172  return;
174  cm->lazy_init_done = 1;
175 }
176 
177 static clib_error_t *
179 {
181 
192  cm->lazy_init_done = 0;
193 
195  {
196  if (unformat
197  (input, "session-db-buckets %u", &cm->session_hash_buckets))
198  ;
199  else if (unformat (input, "session-db-memory %U",
201  ;
202  else if (unformat (input, "translation-db-buckets %u",
204  ;
205  else if (unformat (input, "translation-db-memory %U",
207  ;
208  else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
209  ;
210  else if (unformat (input, "snat-db-memory %U",
212  ;
213  else if (unformat (input, "session-cleanup-timeout %f",
214  &cm->scanner_timeout))
215  ;
216  else if (unformat (input, "scanner off"))
218  else if (unformat (input, "scanner on"))
220  else if (unformat (input, "session-max-age %u", &cm->session_max_age))
221  ;
222  else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
223  ;
224  else
225  return clib_error_return (0, "unknown input '%U'",
226  format_unformat_error, input);
227  }
228 
229  return 0;
230 }
231 
232 cnat_main_t *
234 {
235  return &cnat_main;
236 }
237 
240 
241 /*
242  * fd.io coding-style-patch-verification: ON
243  *
244  * Local Variables:
245  * eval: (c-set-style "gnu")
246  * End:
247  */
enum fib_source_t_ fib_source_t
The different sources that can create a route.
#define CNAT_DEFAULT_TRANSLATION_MEMORY
Definition: cnat_types.h:42
enum cnat_scanner_cmd_t_ cnat_scanner_cmd_t
a
Definition: bitmap.h:544
void ip_address_set(ip_address_t *dst, const void *src, u8 version)
Definition: ip_types.c:208
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
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
u8 default_scanner_state
Definition: cnat_types.h:146
throttle_t cnat_throttle
Definition: cnat_types.c:21
add paths without path extensions
Definition: fib_source.h:205
void throttle_init(throttle_t *t, u32 n_threads, f64 time)
Definition: throttle.c:19
ip6_address_t * ip6_interface_first_address(ip6_main_t *im, u32 sw_if_index)
get first IPv6 interface address
Definition: ip6_forward.c:279
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
vlib_main_t * vm
Definition: in2out_ed.c:1580
u32 session_hash_buckets
Definition: cnat_types.h:103
cnat_endpoint_t src_ep
Definition: cnat_types.h:71
unformat_function_t unformat_vnet_sw_interface
vhost_vring_addr_t addr
Definition: vhost_user.h:111
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
u8 lazy_init_done
Definition: cnat_types.h:143
uword unformat_cnat_ep(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:74
cnat_endpoint_t dst_ep
Definition: cnat_types.h:70
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vl_api_ip6_address_t ip6
Definition: one.api:424
description fragment has unexpected format
Definition: map.api:433
cnat_main_t * cnat_get_main()
Definition: cnat_types.c:233
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
#define CNAT_DEFAULT_SESSION_MAX_AGE
Definition: cnat_types.h:30
fib_source_t fib_source_allocate(const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:118
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
static void clib_rwlock_init(clib_rwlock_t *p)
Definition: lock.h:152
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1015
fib_source_t cnat_fib_source
Definition: cnat_types.c:19
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
cnat_timestamp_t * cnat_timestamps
Definition: cnat_types.c:20
u8 * format_cnat_endpoint(u8 *s, va_list *args)
Definition: cnat_types.c:123
#define CNAT_FIB_SOURCE_PRIORITY
Definition: cnat_types.h:47
vl_api_ip4_address_t ip4
Definition: one.api:376
ip6_main_t ip6_main
Definition: ip6_forward.c:2785
static clib_error_t * cnat_config(vlib_main_t *vm, unformat_input_t *input)
Definition: cnat_types.c:178
uword unformat_ip_address(unformat_input_t *input, va_list *args)
Definition: ip_types.c:41
u8 * format_ip_address_family(u8 *s, va_list *args)
Definition: ip.c:192
A throttle Used in the data plane to decide if a given hash should be throttled, i.e.
Definition: throttle.h:28
vlib_thread_main_t vlib_thread_main
Definition: threads.c:35
void cnat_enable_disable_scanner(cnat_scanner_cmd_t event_type)
Enable/Disable session cleanup.
Definition: cnat_types.c:161
f64 scanner_timeout
Definition: cnat_types.h:125
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:226
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
#define CNAT_DEFAULT_TCP_MAX_AGE
Definition: cnat_types.h:32
u8 * format_ip_address(u8 *s, va_list *args)
Definition: ip_types.c:21
#define CNAT_DEFAULT_SNAT_MEMORY
Definition: cnat_types.h:43
#define CNAT_DEFAULT_SNAT_BUCKETS
Definition: cnat_types.h:39
ip_address_family_t version
Definition: ip_types.h:82
uword session_hash_memory
Definition: cnat_types.h:100
u8 cnat_resolve_addr(u32 sw_if_index, ip_address_family_t af, ip_address_t *addr)
Definition: cnat_types.c:30
clib_rwlock_t ts_lock
Definition: cnat_types.h:128
#define CNAT_DEFAULT_SESSION_BUCKETS
Definition: cnat_types.h:37
u32 session_max_age
Definition: cnat_types.h:118
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
enum ip_address_family_t_ ip_address_family_t
u32 translation_hash_buckets
Definition: cnat_types.h:109
#define CNAT_DEFAULT_TRANSLATION_BUCKETS
Definition: cnat_types.h:38
#define CNAT_DEFAULT_SESSION_MEMORY
Definition: cnat_types.h:41
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
uword unformat_cnat_ep_tuple(unformat_input_t *input, va_list *args)
Definition: cnat_types.c:107
u64 uword
Definition: types.h:112
cnat_main_t cnat_main
Definition: cnat_types.c:18
uword translation_hash_memory
Definition: cnat_types.h:106
u16 port
Definition: lb_types.api:73
unformat_function_t unformat_memory_size
Definition: format.h:295
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1105
u32 tcp_max_age
Definition: cnat_types.h:122
uword scanner_node_index
Definition: cnat_types.h:140
ip_address_t ce_ip
Definition: cnat_types.h:62
static clib_error_t * cnat_types_init(vlib_main_t *vm)
Definition: cnat_types.c:145
char * cnat_error_strings[]
Definition: cnat_types.c:23
u8 cnat_resolve_ep(cnat_endpoint_t *ep)
Resolve endpoint address.
Definition: cnat_types.c:64
u32 snat_hash_buckets
Definition: cnat_types.h:115
void cnat_lazy_init()
Lazy initialization when first adding a translation or using snat.
Definition: cnat_types.c:168
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
uword snat_hash_memory
Definition: cnat_types.h:112
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
#define CNAT_DEFAULT_SCANNER_TIMEOUT
Definition: cnat_types.h:35