cnat: Disable default scanner process
[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 throttle_t cnat_throttle;
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 uword
30 unformat_cnat_ep (unformat_input_t * input, va_list * args)
31 {
32   cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
33   int port = 0;
34
35   clib_memset (a, 0, sizeof (*a));
36   if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
37     ;
38   else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
39     ;
40   else if (unformat (input, "%d", &port))
41     ;
42   else
43     return 0;
44   a->ce_port = (u16) port;
45   return 1;
46 }
47
48 uword
49 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
50 {
51   cnat_endpoint_tuple_t *a = va_arg (*args, cnat_endpoint_tuple_t *);
52   if (unformat (input, "%U->%U", unformat_cnat_ep, &a->src_ep,
53                 unformat_cnat_ep, &a->dst_ep))
54     ;
55   else if (unformat (input, "->%U", unformat_cnat_ep, &a->dst_ep))
56     ;
57   else if (unformat (input, "%U->", unformat_cnat_ep, &a->src_ep))
58     ;
59   else
60     return 0;
61   return 1;
62 }
63
64 u8 *
65 format_cnat_endpoint (u8 * s, va_list * args)
66 {
67   cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
68
69   s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
70
71   return (s);
72 }
73
74 static clib_error_t *
75 cnat_types_init (vlib_main_t * vm)
76 {
77   vlib_thread_main_t *tm = &vlib_thread_main;
78   u32 n_vlib_mains = tm->n_vlib_mains;
79   cnat_fib_source = fib_source_allocate ("cnat",
80                                          CNAT_FIB_SOURCE_PRIORITY,
81                                          FIB_SOURCE_BH_SIMPLE);
82
83   clib_rwlock_init (&cnat_main.ts_lock);
84   clib_spinlock_init (&cnat_main.src_ports_lock);
85   clib_bitmap_validate (cnat_main.src_ports, UINT16_MAX);
86   throttle_init (&cnat_throttle, n_vlib_mains, 1e-3);
87
88   return (NULL);
89 }
90
91 void
92 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
93 {
94   vlib_main_t *vm = vlib_get_main ();
95   vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
96 }
97
98 void
99 cnat_lazy_init ()
100 {
101   cnat_main_t *cm = &cnat_main;
102   if (cm->lazy_init_done)
103     return;
104   cnat_enable_disable_scanner (cm->default_scanner_state);
105   cm->lazy_init_done = 1;
106 }
107
108 static clib_error_t *
109 cnat_config (vlib_main_t * vm, unformat_input_t * input)
110 {
111   cnat_main_t *cm = &cnat_main;
112
113   cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
114   cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
115   cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
116   cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
117   cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
118   cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
119   cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
120   cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
121   cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
122   cm->default_scanner_state = CNAT_SCANNER_ON;
123   cm->lazy_init_done = 0;
124
125   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
126     {
127       if (unformat
128           (input, "session-db-buckets %u", &cm->session_hash_buckets))
129         ;
130       else if (unformat (input, "session-db-memory %U",
131                          unformat_memory_size, &cm->session_hash_memory))
132         ;
133       else if (unformat (input, "translation-db-buckets %u",
134                          &cm->translation_hash_buckets))
135         ;
136       else if (unformat (input, "translation-db-memory %U",
137                          unformat_memory_size, &cm->translation_hash_memory))
138         ;
139       else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
140         ;
141       else if (unformat (input, "snat-db-memory %U",
142                          unformat_memory_size, &cm->snat_hash_memory))
143         ;
144       else if (unformat (input, "session-cleanup-timeout %f",
145                          &cm->scanner_timeout))
146         ;
147       else if (unformat (input, "scanner off"))
148         cm->default_scanner_state = CNAT_SCANNER_OFF;
149       else if (unformat (input, "scanner on"))
150         cm->default_scanner_state = CNAT_SCANNER_ON;
151       else if (unformat (input, "session-max-age %u", &cm->session_max_age))
152         ;
153       else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
154         ;
155       else
156         return clib_error_return (0, "unknown input '%U'",
157                                   format_unformat_error, input);
158     }
159
160   return 0;
161 }
162
163 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
164 VLIB_INIT_FUNCTION (cnat_types_init);
165
166 /*
167  * fd.io coding-style-patch-verification: ON
168  *
169  * Local Variables:
170  * eval: (c-set-style "gnu")
171  * End:
172  */