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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <cnat/cnat_types.h>
18 cnat_main_t cnat_main;
19 fib_source_t cnat_fib_source;
20 cnat_timestamp_t *cnat_timestamps;
21 throttle_t cnat_throttle;
23 char *cnat_error_strings[] = {
24 #define cnat_error(n,s) s,
25 #include <cnat/cnat_error.def>
30 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
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)
39 ip6_address_t *ip6 = 0;
40 ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
43 ip_address_set (addr, ip6, AF_IP6);
51 ip4_address_t *ip4 = 0;
52 ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
55 ip_address_set (addr, ip4, AF_IP4);
64 cnat_resolve_ep (cnat_endpoint_t * ep)
67 rv = cnat_resolve_addr (ep->ce_sw_if_index, ep->ce_ip.version, &ep->ce_ip);
69 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
74 unformat_cnat_ep (unformat_input_t * input, va_list * args)
76 cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
77 vnet_main_t *vnm = vnet_get_main ();
80 clib_memset (a, 0, sizeof (*a));
81 a->ce_sw_if_index = INDEX_INVALID;
82 if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
84 else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
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;
95 else if (unformat_user (input, unformat_vnet_sw_interface,
96 vnm, &a->ce_sw_if_index))
97 a->ce_ip.version = AF_IP4;
98 else if (unformat (input, "%d", &port))
102 a->ce_port = (u16) port;
107 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
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))
113 else if (unformat (input, "->%U", unformat_cnat_ep, &a->dst_ep))
115 else if (unformat (input, "%U->", unformat_cnat_ep, &a->src_ep))
123 format_cnat_endpoint (u8 * s, va_list * args)
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);
131 if (cep->ce_flags & CNAT_EP_FLAG_RESOLVED)
132 s = format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
133 cep->ce_sw_if_index, format_ip_address, &cep->ce_ip,
137 format (s, "%U (%U);%d", format_vnet_sw_if_index_name, vnm,
138 cep->ce_sw_if_index, format_ip_address_family,
139 cep->ce_ip.version, cep->ce_port);
144 static clib_error_t *
145 cnat_types_init (vlib_main_t * vm)
147 vlib_thread_main_t *tm = &vlib_thread_main;
148 u32 n_vlib_mains = tm->n_vlib_mains;
149 cnat_fib_source = fib_source_allocate ("cnat",
150 CNAT_FIB_SOURCE_PRIORITY,
151 FIB_SOURCE_BH_SIMPLE);
154 clib_rwlock_init (&cnat_main.ts_lock);
155 throttle_init (&cnat_throttle, n_vlib_mains, 1e-3);
161 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
163 vlib_main_t *vm = vlib_get_main ();
164 vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
170 cnat_main_t *cm = &cnat_main;
171 if (cm->lazy_init_done)
173 cnat_enable_disable_scanner (cm->default_scanner_state);
174 cm->lazy_init_done = 1;
177 static clib_error_t *
178 cnat_config (vlib_main_t * vm, unformat_input_t * input)
180 cnat_main_t *cm = &cnat_main;
182 cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
183 cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
184 cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
185 cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
186 cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
187 cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
188 cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
189 cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
190 cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
191 cm->default_scanner_state = CNAT_SCANNER_ON;
192 cm->lazy_init_done = 0;
194 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
197 (input, "session-db-buckets %u", &cm->session_hash_buckets))
199 else if (unformat (input, "session-db-memory %U",
200 unformat_memory_size, &cm->session_hash_memory))
202 else if (unformat (input, "translation-db-buckets %u",
203 &cm->translation_hash_buckets))
205 else if (unformat (input, "translation-db-memory %U",
206 unformat_memory_size, &cm->translation_hash_memory))
208 else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
210 else if (unformat (input, "snat-db-memory %U",
211 unformat_memory_size, &cm->snat_hash_memory))
213 else if (unformat (input, "session-cleanup-timeout %f",
214 &cm->scanner_timeout))
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))
222 else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
225 return clib_error_return (0, "unknown input '%U'",
226 format_unformat_error, input);
238 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
239 VLIB_INIT_FUNCTION (cnat_types_init);
242 * fd.io coding-style-patch-verification: ON
245 * eval: (c-set-style "gnu")