cnat: Prepare extended snat policies
[vpp.git] / src / plugins / cnat / cnat_types.c
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
18 cnat_main_t cnat_main;
19 fib_source_t cnat_fib_source;
20 cnat_timestamp_t *cnat_timestamps;
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
29 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
30                    ip_address_t * addr)
31 {
32   /* Tries to resolve IP from sw_if_index
33    * returns 1 if we need to schedule DHCP */
34   if (INDEX_INVALID == sw_if_index)
35     return 0;
36   if (af == AF_IP6)
37     {
38       ip6_address_t *ip6 = 0;
39       ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
40       if (ip6)
41         {
42           ip_address_set (addr, ip6, AF_IP6);
43           return 0;
44         }
45       else
46         return 1;
47     }
48   else
49     {
50       ip4_address_t *ip4 = 0;
51       ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
52       if (ip4)
53         {
54           ip_address_set (addr, ip4, AF_IP4);
55           return 0;
56         }
57       else
58         return 1;
59     }
60 }
61
62 u8
63 cnat_resolve_ep (cnat_endpoint_t * ep)
64 {
65   int rv;
66   rv = cnat_resolve_addr (ep->ce_sw_if_index, ep->ce_ip.version, &ep->ce_ip);
67   if (0 == rv)
68     ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
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;
94   else if (unformat_user (input, unformat_vnet_sw_interface,
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
106 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
107 {
108   cnat_endpoint_tuple_t *a = va_arg (*args, cnat_endpoint_tuple_t *);
109   if (unformat (input, "%U->%U", unformat_cnat_ep, &a->src_ep,
110                 unformat_cnat_ep, &a->dst_ep))
111     ;
112   else if (unformat (input, "->%U", unformat_cnat_ep, &a->dst_ep))
113     ;
114   else if (unformat (input, "%U->", unformat_cnat_ep, &a->src_ep))
115     ;
116   else
117     return 0;
118   return 1;
119 }
120
121 u8 *
122 format_cnat_endpoint (u8 * s, va_list * args)
123 {
124   cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
125   vnet_main_t *vnm = vnet_get_main ();
126   if (INDEX_INVALID == cep->ce_sw_if_index)
127     s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
128   else
129     {
130       if (cep->ce_flags & CNAT_EP_FLAG_RESOLVED)
131         s = format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
132                     cep->ce_sw_if_index, format_ip_address, &cep->ce_ip,
133                     cep->ce_port);
134       else
135         s =
136           format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
137                   cep->ce_sw_if_index, format_ip_address_family,
138                   cep->ce_ip.version, cep->ce_port);
139     }
140   return (s);
141 }
142
143 static clib_error_t *
144 cnat_types_init (vlib_main_t * vm)
145 {
146   cnat_fib_source = fib_source_allocate ("cnat",
147                                          CNAT_FIB_SOURCE_PRIORITY,
148                                          FIB_SOURCE_BH_SIMPLE);
149
150
151   clib_rwlock_init (&cnat_main.ts_lock);
152
153   return (NULL);
154 }
155
156 void
157 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
158 {
159   vlib_main_t *vm = vlib_get_main ();
160   vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
161 }
162
163 void
164 cnat_lazy_init ()
165 {
166   cnat_main_t *cm = &cnat_main;
167   if (cm->lazy_init_done)
168     return;
169   cnat_enable_disable_scanner (cm->default_scanner_state);
170   cm->lazy_init_done = 1;
171 }
172
173 static clib_error_t *
174 cnat_config (vlib_main_t * vm, unformat_input_t * input)
175 {
176   cnat_main_t *cm = &cnat_main;
177
178   cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
179   cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
180   cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
181   cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
182   cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
183   cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
184   cm->snat_if_map_length = CNAT_DEFAULT_SNAT_IF_MAP_LEN;
185   cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
186   cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
187   cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
188   cm->default_scanner_state = CNAT_SCANNER_ON;
189   cm->maglev_len = CNAT_DEFAULT_MAGLEV_LEN;
190   cm->lazy_init_done = 0;
191
192   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
193     {
194       if (unformat
195           (input, "session-db-buckets %u", &cm->session_hash_buckets))
196         ;
197       else if (unformat (input, "session-db-memory %U",
198                          unformat_memory_size, &cm->session_hash_memory))
199         ;
200       else if (unformat (input, "translation-db-buckets %u",
201                          &cm->translation_hash_buckets))
202         ;
203       else if (unformat (input, "translation-db-memory %U",
204                          unformat_memory_size, &cm->translation_hash_memory))
205         ;
206       else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
207         ;
208       else if (unformat (input, "snat-if-map-len %u", &cm->snat_if_map_length))
209         ;
210       else if (unformat (input, "snat-db-memory %U",
211                          unformat_memory_size, &cm->snat_hash_memory))
212         ;
213       else if (unformat (input, "session-cleanup-timeout %f",
214                          &cm->scanner_timeout))
215         ;
216       else if (unformat (input, "scanner off"))
217         cm->default_scanner_state = CNAT_SCANNER_OFF;
218       else if (unformat (input, "scanner on"))
219         cm->default_scanner_state = CNAT_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 if (unformat (input, "maglev-len %u", &cm->maglev_len))
225         ;
226       else
227         return clib_error_return (0, "unknown input '%U'",
228                                   format_unformat_error, input);
229     }
230
231   return 0;
232 }
233
234 cnat_main_t *
235 cnat_get_main ()
236 {
237   return &cnat_main;
238 }
239
240 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
241 VLIB_INIT_FUNCTION (cnat_types_init);
242
243 /*
244  * fd.io coding-style-patch-verification: ON
245  *
246  * Local Variables:
247  * eval: (c-set-style "gnu")
248  * End:
249  */