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 <vnet/plugin/plugin.h>
16 #include <nat/dslite/dslite.h>
17 #include <nat/dslite/dslite_dpo.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vpp/app/version.h>
21 dslite_main_t dslite_main;
22 fib_source_t nat_fib_src_hi;
24 clib_error_t *dslite_api_hookup (vlib_main_t * vm);
27 add_del_dslite_pool_addr_cb (ip4_address_t addr, u8 is_add, void *opaque);
30 dslite_init (vlib_main_t * vm)
32 dslite_main_t *dm = &dslite_main;
33 vlib_thread_registration_t *tr;
34 vlib_thread_main_t *tm = vlib_get_thread_main ();
38 node = vlib_get_node_by_name (vm, (u8 *) "dslite-in2out");
39 dm->dslite_in2out_node_index = node->index;
41 node = vlib_get_node_by_name (vm, (u8 *) "dslite-in2out-slowpath");
42 dm->dslite_in2out_slowpath_node_index = node->index;
44 node = vlib_get_node_by_name (vm, (u8 *) "dslite-out2in");
45 dm->dslite_out2in_node_index = node->index;
47 dm->first_worker_index = 0;
50 // init nat address pool
51 dm->pool.add_del_pool_addr_cb = add_del_dslite_pool_addr_cb;
52 dm->pool.alloc_addr_and_port_cb = nat_alloc_ip4_addr_and_port_cb_default;
54 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
57 tr = (vlib_thread_registration_t *) p[0];
60 dm->num_workers = tr->count;
61 dm->first_worker_index = tr->first_index;
66 dm->port_per_thread = (0xffff - 1024) / dm->num_workers;
68 dm->port_per_thread = 0xffff - 1024;
70 vec_validate (dm->per_thread_data, tm->n_vlib_mains - 1);
76 dm->total_b4s.name = "total-b4s";
77 dm->total_b4s.stat_segment_name = "/dslite/total-b4s";
78 vlib_validate_simple_counter (&dm->total_b4s, 0);
79 vlib_zero_simple_counter (&dm->total_b4s, 0);
80 dm->total_sessions.name = "total-sessions";
81 dm->total_sessions.stat_segment_name = "/dslite/total-sessions";
82 vlib_validate_simple_counter (&dm->total_sessions, 0);
83 vlib_zero_simple_counter (&dm->total_sessions, 0);
85 dslite_dpo_module_init ();
87 nat_fib_src_hi = fib_source_allocate ("dslite-hi",
88 FIB_SOURCE_PRIORITY_HI,
89 FIB_SOURCE_BH_SIMPLE);
91 return dslite_api_hookup (vm);
95 dslite_init_datastructures (void)
97 dslite_main_t *dm = &dslite_main;
98 dslite_per_thread_data_t *td;
99 u32 translation_buckets = 1024;
100 u32 translation_memory_size = 128 << 20;
101 u32 b4_buckets = 128;
102 u32 b4_memory_size = 64 << 20;
104 vec_foreach (td, dm->per_thread_data)
106 clib_bihash_init_24_8 (&td->in2out, "dslite in2out", translation_buckets,
107 translation_memory_size);
109 clib_bihash_init_8_8 (&td->out2in, "dslite out2in", translation_buckets,
110 translation_memory_size);
112 clib_bihash_init_16_8 (&td->b4_hash, "dslite b4s", b4_buckets, b4_memory_size);
118 dslite_set_ce (dslite_main_t * dm, u8 set)
120 dm->is_ce = (set != 0);
123 static clib_error_t *
124 dslite_config (vlib_main_t * vm, unformat_input_t * input)
126 dslite_main_t *dm = &dslite_main;
128 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
130 if (unformat (input, "ce"))
131 dslite_set_ce (dm, 1);
136 VLIB_CONFIG_FUNCTION (dslite_config, "dslite");
139 dslite_set_aftr_ip6_addr (dslite_main_t * dm, ip6_address_t * addr)
141 dpo_id_t dpo = DPO_INVALID;
144 dslite_init_datastructures ();
148 dslite_ce_dpo_create (DPO_PROTO_IP4, 0, &dpo);
150 .fp_proto = FIB_PROTOCOL_IP4,
152 .fp_addr.ip4.as_u32 = 0,
154 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
155 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
159 dslite_dpo_create (DPO_PROTO_IP6, 0, &dpo);
161 .fp_proto = FIB_PROTOCOL_IP6,
163 .fp_addr.ip6.as_u64[0] = addr->as_u64[0],
164 .fp_addr.ip6.as_u64[1] = addr->as_u64[1],
166 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
167 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
172 dm->aftr_ip6_addr.as_u64[0] = addr->as_u64[0];
173 dm->aftr_ip6_addr.as_u64[1] = addr->as_u64[1];
178 dslite_set_aftr_ip4_addr (dslite_main_t * dm, ip4_address_t * addr)
180 dm->aftr_ip4_addr.as_u32 = addr->as_u32;
185 dslite_set_b4_ip6_addr (dslite_main_t * dm, ip6_address_t * addr)
188 dslite_init_datastructures ();
192 dpo_id_t dpo = DPO_INVALID;
194 dslite_ce_dpo_create (DPO_PROTO_IP6, 0, &dpo);
196 .fp_proto = FIB_PROTOCOL_IP6,
198 .fp_addr.ip6.as_u64[0] = addr->as_u64[0],
199 .fp_addr.ip6.as_u64[1] = addr->as_u64[1],
201 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
202 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
206 dm->b4_ip6_addr.as_u64[0] = addr->as_u64[0];
207 dm->b4_ip6_addr.as_u64[1] = addr->as_u64[1];
211 return VNET_API_ERROR_FEATURE_DISABLED;
218 dslite_set_b4_ip4_addr (dslite_main_t * dm, ip4_address_t * addr)
222 dm->b4_ip4_addr.as_u32 = addr->as_u32;
226 return VNET_API_ERROR_FEATURE_DISABLED;
233 add_del_dslite_pool_addr_cb (ip4_address_t addr, u8 is_add, void *opaque)
235 dpo_id_t dpo_v4 = DPO_INVALID;
237 .fp_proto = FIB_PROTOCOL_IP4,
239 .fp_addr.ip4.as_u32 = addr.as_u32,
244 dslite_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
245 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
246 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
251 fib_table_entry_special_remove (0, &pfx, nat_fib_src_hi);
256 format_dslite_trace (u8 * s, va_list * args)
258 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
259 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
260 dslite_trace_t *t = va_arg (*args, dslite_trace_t *);
263 format (s, "next index %d, session %d", t->next_index, t->session_index);
269 format_dslite_ce_trace (u8 * s, va_list * args)
271 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
272 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
273 dslite_ce_trace_t *t = va_arg (*args, dslite_ce_trace_t *);
275 s = format (s, "next index %d", t->next_index);
280 VLIB_INIT_FUNCTION (dslite_init);
282 VLIB_PLUGIN_REGISTER () =
284 .version = VPP_BUILD_VER,
285 .description = "Dual-Stack Lite",
289 * fd.io coding-style-patch-verification: ON
292 * eval: (c-set-style "gnu")