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 unformat_cnat_ep (unformat_input_t * input, va_list * args)
32 cnat_endpoint_t *a = va_arg (*args, cnat_endpoint_t *);
35 clib_memset (a, 0, sizeof (*a));
36 if (unformat (input, "%U %d", unformat_ip_address, &a->ce_ip, &port))
38 else if (unformat_user (input, unformat_ip_address, &a->ce_ip))
40 else if (unformat (input, "%d", &port))
44 a->ce_port = (u16) port;
49 unformat_cnat_ep_tuple (unformat_input_t * input, va_list * args)
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))
55 else if (unformat (input, "->%U", unformat_cnat_ep, &a->dst_ep))
57 else if (unformat (input, "%U->", unformat_cnat_ep, &a->src_ep))
65 format_cnat_endpoint (u8 * s, va_list * args)
67 cnat_endpoint_t *cep = va_arg (*args, cnat_endpoint_t *);
69 s = format (s, "%U;%d", format_ip_address, &cep->ce_ip, cep->ce_port);
75 cnat_types_init (vlib_main_t * vm)
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);
84 clib_rwlock_init (&cnat_main.ts_lock);
85 vec_validate (cnat_main.src_ports, CNAT_N_SPORT_PROTO);
86 for (int i = 0; i < CNAT_N_SPORT_PROTO; i++)
88 clib_spinlock_init (&cnat_main.src_ports[i].lock);
89 clib_bitmap_validate (cnat_main.src_ports[i].bmap, UINT16_MAX);
91 throttle_init (&cnat_throttle, n_vlib_mains, 1e-3);
97 cnat_enable_disable_scanner (cnat_scanner_cmd_t event_type)
99 vlib_main_t *vm = vlib_get_main ();
100 vlib_process_signal_event (vm, cnat_main.scanner_node_index, event_type, 0);
106 cnat_main_t *cm = &cnat_main;
107 if (cm->lazy_init_done)
109 cnat_enable_disable_scanner (cm->default_scanner_state);
110 cm->lazy_init_done = 1;
113 static clib_error_t *
114 cnat_config (vlib_main_t * vm, unformat_input_t * input)
116 cnat_main_t *cm = &cnat_main;
118 cm->session_hash_memory = CNAT_DEFAULT_SESSION_MEMORY;
119 cm->session_hash_buckets = CNAT_DEFAULT_SESSION_BUCKETS;
120 cm->translation_hash_memory = CNAT_DEFAULT_TRANSLATION_MEMORY;
121 cm->translation_hash_buckets = CNAT_DEFAULT_TRANSLATION_BUCKETS;
122 cm->snat_hash_memory = CNAT_DEFAULT_SNAT_MEMORY;
123 cm->snat_hash_buckets = CNAT_DEFAULT_SNAT_BUCKETS;
124 cm->scanner_timeout = CNAT_DEFAULT_SCANNER_TIMEOUT;
125 cm->session_max_age = CNAT_DEFAULT_SESSION_MAX_AGE;
126 cm->tcp_max_age = CNAT_DEFAULT_TCP_MAX_AGE;
127 cm->default_scanner_state = CNAT_SCANNER_ON;
128 cm->lazy_init_done = 0;
130 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
133 (input, "session-db-buckets %u", &cm->session_hash_buckets))
135 else if (unformat (input, "session-db-memory %U",
136 unformat_memory_size, &cm->session_hash_memory))
138 else if (unformat (input, "translation-db-buckets %u",
139 &cm->translation_hash_buckets))
141 else if (unformat (input, "translation-db-memory %U",
142 unformat_memory_size, &cm->translation_hash_memory))
144 else if (unformat (input, "snat-db-buckets %u", &cm->snat_hash_buckets))
146 else if (unformat (input, "snat-db-memory %U",
147 unformat_memory_size, &cm->snat_hash_memory))
149 else if (unformat (input, "session-cleanup-timeout %f",
150 &cm->scanner_timeout))
152 else if (unformat (input, "scanner off"))
153 cm->default_scanner_state = CNAT_SCANNER_OFF;
154 else if (unformat (input, "scanner on"))
155 cm->default_scanner_state = CNAT_SCANNER_ON;
156 else if (unformat (input, "session-max-age %u", &cm->session_max_age))
158 else if (unformat (input, "tcp-max-age %u", &cm->tcp_max_age))
161 return clib_error_return (0, "unknown input '%U'",
162 format_unformat_error, input);
168 VLIB_EARLY_CONFIG_FUNCTION (cnat_config, "cnat");
169 VLIB_INIT_FUNCTION (cnat_types_init);
172 * fd.io coding-style-patch-verification: ON
175 * eval: (c-set-style "gnu")