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;
22 char *cnat_error_strings[] = {
23 #define cnat_error(n,s) s,
24 #include <cnat/cnat_error.def>
29 cnat_resolve_addr (u32 sw_if_index, ip_address_family_t af,
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)
38 ip6_address_t *ip6 = 0;
39 ip6 = ip6_interface_first_address (&ip6_main, sw_if_index);
42 ip_address_set (addr, ip6, AF_IP6);
50 ip4_address_t *ip4 = 0;
51 ip4 = ip4_interface_first_address (&ip4_main, sw_if_index, 0);
54 ip_address_set (addr, ip4, AF_IP4);
63 cnat_resolve_ep (cnat_endpoint_t * ep)
66 rv = cnat_resolve_addr (ep->ce_sw_if_index, ep->ce_ip.version, &ep->ce_ip);
68 ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
73 unformat_cnat_ep (unformat_input_t * input, va_list * args)
75 cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
76 vnet_main_t *vnm = vnet_get_main ();
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))
83 else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
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))
101 a->ce_port = (u16) port;
106 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
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))
112 else if (unformat (input, "->%U", unformat_cnat_ep, &a->dst_ep))
114 else if (unformat (input, "%U->", unformat_cnat_ep, &a->src_ep))
122 format_cnat_endpoint (u8 * s, va_list * args)
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);
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,
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);
143 static clib_error_t *
144 cnat_types_init (vlib_main_t * vm)
146 cnat_fib_source = fib_source_allocate ("cnat",
147 CNAT_FIB_SOURCE_PRIORITY,
148 FIB_SOURCE_BH_SIMPLE);
151 clib_rwlock_init (&cnat_main.ts_lock);
157 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
159 vlib_main_t *vm = vlib_get_main ();
160 vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
166 cnat_main_t *cm = &cnat_main;
167 if (cm->lazy_init_done)
169 cnat_enable_disable_scanner (cm->default_scanner_state);
170 cm->lazy_init_done = 1;
173 static clib_error_t *
174 cnat_config (vlib_main_t * vm, unformat_input_t * input)
176 cnat_main_t *cm = &cnat_main;
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->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
185 cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
186 cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
187 cm->default_scanner_state = CNAT_SCANNER_ON;
188 cm->maglev_len = CNAT_DEFAULT_MAGLEV_LEN;
189 cm->lazy_init_done = 0;
191 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
194 (input, "session-db-buckets %u", &cm->session_hash_buckets))
196 else if (unformat (input, "session-db-memory %U",
197 unformat_memory_size, &cm->session_hash_memory))
199 else if (unformat (input, "translation-db-buckets %u",
200 &cm->translation_hash_buckets))
202 else if (unformat (input, "translation-db-memory %U",
203 unformat_memory_size, &cm->translation_hash_memory))
205 else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
207 else if (unformat (input, "snat-db-memory %U",
208 unformat_memory_size, &cm->snat_hash_memory))
210 else if (unformat (input, "session-cleanup-timeout %f",
211 &cm->scanner_timeout))
213 else if (unformat (input, "scanner off"))
214 cm->default_scanner_state = CNAT_SCANNER_OFF;
215 else if (unformat (input, "scanner on"))
216 cm->default_scanner_state = CNAT_SCANNER_ON;
217 else if (unformat (input, "session-max-age %u", &cm->session_max_age))
219 else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
221 else if (unformat (input, "maglev-len %u", &cm->maglev_len))
224 return clib_error_return (0, "unknown input '%U'",
225 format_unformat_error, input);
237 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
238 VLIB_INIT_FUNCTION (cnat_types_init);
241 * fd.io coding-style-patch-verification: ON
244 * eval: (c-set-style "gnu")