2 * Copyright (c) 2017 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.
15 #include <nat/dslite.h>
16 #include <nat/dslite_dpo.h>
17 #include <vnet/fib/fib_table.h>
19 dslite_main_t dslite_main;
22 dslite_init (vlib_main_t * vm)
24 dslite_main_t *dm = &dslite_main;
25 vlib_thread_registration_t *tr;
26 vlib_thread_main_t *tm = vlib_get_thread_main ();
29 dslite_per_thread_data_t *td;
30 u32 translation_buckets = 1024;
31 u32 translation_memory_size = 128 << 20;
33 u32 b4_memory_size = 64 << 20;
35 node = vlib_get_node_by_name (vm, (u8 *) "dslite-in2out");
36 dm->dslite_in2out_node_index = node->index;
38 node = vlib_get_node_by_name (vm, (u8 *) "dslite-in2out-slowpath");
39 dm->dslite_in2out_slowpath_node_index = node->index;
41 node = vlib_get_node_by_name (vm, (u8 *) "dslite-out2in");
42 dm->dslite_out2in_node_index = node->index;
44 dm->first_worker_index = 0;
47 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
50 tr = (vlib_thread_registration_t *) p[0];
53 dm->num_workers = tr->count;
54 dm->first_worker_index = tr->first_index;
59 dm->port_per_thread = (0xffff - 1024) / dm->num_workers;
61 dm->port_per_thread = 0xffff - 1024;
63 vec_validate (dm->per_thread_data, tm->n_vlib_mains - 1);
66 vec_foreach (td, dm->per_thread_data)
68 clib_bihash_init_24_8 (&td->in2out, "in2out", translation_buckets,
69 translation_memory_size);
71 clib_bihash_init_8_8 (&td->out2in, "out2in", translation_buckets,
72 translation_memory_size);
74 clib_bihash_init_16_8 (&td->b4_hash, "b4s", b4_buckets, b4_memory_size);
81 dm->total_b4s.name = "total-b4s";
82 dm->total_b4s.stat_segment_name = "/dslite/total-b4s";
83 vlib_validate_simple_counter (&dm->total_b4s, 0);
84 vlib_zero_simple_counter (&dm->total_b4s, 0);
85 dm->total_sessions.name = "total-sessions";
86 dm->total_sessions.stat_segment_name = "/dslite/total-sessions";
87 vlib_validate_simple_counter (&dm->total_sessions, 0);
88 vlib_zero_simple_counter (&dm->total_sessions, 0);
90 dslite_dpo_module_init ();
94 dslite_set_ce (dslite_main_t * dm, u8 set)
96 dm->is_ce = (set != 0);
100 dslite_set_aftr_ip6_addr (dslite_main_t * dm, ip6_address_t * addr)
102 dpo_id_t dpo = DPO_INVALID;
106 dslite_ce_dpo_create (DPO_PROTO_IP4, 0, &dpo);
108 .fp_proto = FIB_PROTOCOL_IP4,
110 .fp_addr.ip4.as_u32 = 0,
112 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
113 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
117 dslite_dpo_create (DPO_PROTO_IP6, 0, &dpo);
119 .fp_proto = FIB_PROTOCOL_IP6,
121 .fp_addr.ip6.as_u64[0] = addr->as_u64[0],
122 .fp_addr.ip6.as_u64[1] = addr->as_u64[1],
124 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
125 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
130 dm->aftr_ip6_addr.as_u64[0] = addr->as_u64[0];
131 dm->aftr_ip6_addr.as_u64[1] = addr->as_u64[1];
136 dslite_set_aftr_ip4_addr (dslite_main_t * dm, ip4_address_t * addr)
138 dm->aftr_ip4_addr.as_u32 = addr->as_u32;
143 dslite_set_b4_ip6_addr (dslite_main_t * dm, ip6_address_t * addr)
147 dpo_id_t dpo = DPO_INVALID;
149 dslite_ce_dpo_create (DPO_PROTO_IP6, 0, &dpo);
151 .fp_proto = FIB_PROTOCOL_IP6,
153 .fp_addr.ip6.as_u64[0] = addr->as_u64[0],
154 .fp_addr.ip6.as_u64[1] = addr->as_u64[1],
156 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
157 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
161 dm->b4_ip6_addr.as_u64[0] = addr->as_u64[0];
162 dm->b4_ip6_addr.as_u64[1] = addr->as_u64[1];
166 return VNET_API_ERROR_FEATURE_DISABLED;
173 dslite_set_b4_ip4_addr (dslite_main_t * dm, ip4_address_t * addr)
177 dm->b4_ip4_addr.as_u32 = addr->as_u32;
181 return VNET_API_ERROR_FEATURE_DISABLED;
188 dslite_add_del_pool_addr (dslite_main_t * dm, ip4_address_t * addr, u8 is_add)
190 vlib_thread_main_t *tm = vlib_get_thread_main ();
191 snat_address_t *a = 0;
193 dpo_id_t dpo_v4 = DPO_INVALID;
195 .fp_proto = FIB_PROTOCOL_IP4,
197 .fp_addr.ip4.as_u32 = addr->as_u32,
200 for (i = 0; i < vec_len (dm->addr_pool); i++)
202 if (dm->addr_pool[i].addr.as_u32 == addr->as_u32)
204 a = dm->addr_pool + i;
211 return VNET_API_ERROR_VALUE_EXIST;
212 vec_add2 (dm->addr_pool, a, 1);
214 #define _(N, i, n, s) \
215 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
216 a->busy_##n##_ports = 0; \
217 vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
218 foreach_snat_protocol
220 dslite_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
221 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
222 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
228 return VNET_API_ERROR_NO_SUCH_ENTRY;
229 #define _(N, id, n, s) \
230 clib_bitmap_free (a->busy_##n##_port_bitmap); \
231 vec_free (a->busy_##n##_ports_per_thread);
232 foreach_snat_protocol
234 fib_table_entry_special_remove (0, &pfx, nat_fib_src_hi);
235 vec_del1 (dm->addr_pool, i);
241 format_dslite_trace (u8 * s, va_list * args)
243 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
244 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
245 dslite_trace_t *t = va_arg (*args, dslite_trace_t *);
248 format (s, "next index %d, session %d", t->next_index, t->session_index);
254 format_dslite_ce_trace (u8 * s, va_list * args)
256 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
257 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
258 dslite_ce_trace_t *t = va_arg (*args, dslite_ce_trace_t *);
260 s = format (s, "next index %d", t->next_index);
266 * fd.io coding-style-patch-verification: ON
269 * eval: (c-set-style "gnu")