2 * Copyright (c) 2015 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.
17 #include <vnet/plugin/plugin.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/ip6_fib.h>
21 #include <vnet/adj/adj.h>
24 * This code supports the following sixrd modes:
26 * 32 EA bits (Complete IPv4 address is embedded):
28 * IPv4 suffix is embedded:
30 * No embedded address bits (1:1 mode):
35 sixrd_create_domain (ip6_address_t *ip6_prefix,
37 ip4_address_t *ip4_prefix,
39 ip4_address_t *ip4_src,
40 u32 *sixrd_domain_index,
43 dpo_id_t dpo_v6 = DPO_NULL, dpo_v4 = DPO_NULL;
44 sixrd_main_t *mm = &sixrd_main;
48 /* Get domain index */
49 pool_get_aligned(mm->domains, d, CLIB_CACHE_LINE_BYTES);
50 memset(d, 0, sizeof (*d));
51 *sixrd_domain_index = d - mm->domains;
53 /* Init domain struct */
54 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
55 d->ip4_prefix_len = ip4_prefix_len;
56 d->ip6_prefix = *ip6_prefix;
57 d->ip6_prefix_len = ip6_prefix_len;
58 d->ip4_src = *ip4_src;
61 if (ip4_prefix_len < 32)
62 d->shift = 64 - ip6_prefix_len + (32 - ip4_prefix_len);
64 /* Create IPv6 route/adjacency */
66 .fp_proto = FIB_PROTOCOL_IP6,
67 .fp_len = d->ip6_prefix_len,
72 sixrd_dpo_create(FIB_PROTOCOL_IP6,
75 fib_table_entry_special_dpo_add(0, &pfx6,
77 FIB_ENTRY_FLAG_EXCLUSIVE,
82 * Multiple SIXRD domains may share same source IPv4 TEP
83 * In this case the route will exist and be SixRD sourced.
84 * Find the adj (if any) already contributed and modify it
87 .fp_proto = FIB_PROTOCOL_IP6,
93 fei = fib_table_lookup_exact_match(0, &pfx4);
95 if (FIB_NODE_INDEX_INVALID != fei)
97 dpo_id_t dpo = DPO_NULL;
99 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SIXRD, &dpo))
102 * modify the existing adj to indicate it's shared
104 * It is locked to pair with the unlock below.
106 const dpo_id_t *sd_dpo;
109 ASSERT(DPO_LOAD_BALANCE == dpo.dpoi_type);
111 sd_dpo = load_balance_get_bucket(dpo.dpoi_index, 0);
112 sd = sixrd_dpo_get (sd_dpo->dpoi_index);
115 dpo_copy (&dpo_v4, sd_dpo);
121 /* first time addition of the route */
122 sixrd_dpo_create(FIB_PROTOCOL_IP4,
128 * Create ip4 route. This is a reference counted add. If the prefix
129 * already exists and is SixRD sourced, it is now SixRD source n+1 times
130 * and will need to be removed n+1 times.
132 fib_table_entry_special_dpo_add(0, &pfx4,
134 FIB_ENTRY_FLAG_EXCLUSIVE,
142 * sixrd_delete_domain
145 sixrd_delete_domain (u32 sixrd_domain_index)
147 sixrd_main_t *mm = &sixrd_main;
150 if (pool_is_free_index(mm->domains, sixrd_domain_index)) {
151 clib_warning("SIXRD domain delete: domain does not exist: %d",
156 d = pool_elt_at_index(mm->domains, sixrd_domain_index);
159 .fp_proto = FIB_PROTOCOL_IP4,
165 fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_SIXRD);
167 fib_prefix_t pfx6 = {
168 .fp_proto = FIB_PROTOCOL_IP6,
169 .fp_len = d->ip6_prefix_len,
171 .ip6 = d->ip6_prefix,
174 fib_table_entry_special_remove(0, &pfx6, FIB_SOURCE_SIXRD);
176 pool_put(mm->domains, d);
181 static clib_error_t *
182 sixrd_add_domain_command_fn (vlib_main_t *vm,
183 unformat_input_t *input,
184 vlib_cli_command_t *cmd)
186 unformat_input_t _line_input, *line_input = &_line_input;
187 ip4_address_t ip4_prefix;
188 ip6_address_t ip6_prefix;
189 ip4_address_t ip4_src;
190 u32 ip6_prefix_len=0, ip4_prefix_len=0, sixrd_domain_index;
192 /* Optional arguments */
195 /* Get a line of input. */
196 if (!unformat_user(input, unformat_line_input, line_input))
198 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
199 if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix, &ip6_prefix_len))
201 else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
203 else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
205 else if (unformat(line_input, "mtu %d", &mtu))
208 return clib_error_return(0, "unknown input `%U'",
209 format_unformat_error, input);
211 unformat_free(line_input);
214 return clib_error_return(0, "mandatory argument(s) missing");
216 sixrd_create_domain(&ip6_prefix, ip6_prefix_len, &ip4_prefix, ip4_prefix_len,
217 &ip4_src, &sixrd_domain_index, mtu);
222 static clib_error_t *
223 sixrd_del_domain_command_fn (vlib_main_t *vm,
224 unformat_input_t *input,
225 vlib_cli_command_t *cmd)
227 unformat_input_t _line_input, *line_input = &_line_input;
229 u32 sixrd_domain_index;
231 /* Get a line of input. */
232 if (! unformat_user(input, unformat_line_input, line_input))
235 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
236 if (unformat(line_input, "index %d", &sixrd_domain_index))
239 return clib_error_return(0, "unknown input `%U'",
240 format_unformat_error, input);
242 unformat_free(line_input);
245 return clib_error_return(0, "mandatory argument(s) missing");
247 sixrd_delete_domain(sixrd_domain_index);
253 format_sixrd_domain (u8 *s, va_list *args)
255 sixrd_domain_t *d = va_arg(*args, sixrd_domain_t *);
256 sixrd_main_t *mm = &sixrd_main;
259 "[%d] ip6-pfx %U/%d ip4-pfx %U/%d ip4-src %U mtu %d",
261 format_ip6_address, &d->ip6_prefix, d->ip6_prefix_len,
262 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
263 format_ip4_address, &d->ip4_src, d->mtu);
268 static clib_error_t *
269 show_sixrd_domain_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
271 sixrd_main_t *mm = &sixrd_main;
274 if (pool_elts(mm->domains) == 0)
275 vlib_cli_output(vm, "No SIXRD domains are configured...");
277 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_sixrd_domain, d);}));
283 static clib_error_t *
284 show_sixrd_stats_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
286 sixrd_main_t *mm = &sixrd_main;
288 int domains = 0, domaincount = 0;
289 if (pool_elts (mm->domains) == 0)
290 vlib_cli_output (vm, "No SIXRD domains are configured...");
292 pool_foreach(d, mm->domains, ({
293 domains += sizeof(*d);
297 vlib_cli_output(vm, "SIXRD domains structure: %d\n", sizeof (sixrd_domain_t));
298 vlib_cli_output(vm, "SIXRD domains: %d (%d bytes)\n", domaincount, domains);
304 * packet trace format function
307 format_sixrd_trace (u8 *s, va_list *args)
309 CLIB_UNUSED(vlib_main_t *vm) = va_arg (*args, vlib_main_t *);
310 CLIB_UNUSED(vlib_node_t *node) = va_arg (*args, vlib_node_t *);
311 sixrd_trace_t *t = va_arg (*args, sixrd_trace_t *);
312 u32 sixrd_domain_index = t->sixrd_domain_index;
314 s = format(s, "SIXRD domain index: %d", sixrd_domain_index);
319 VLIB_CLI_COMMAND(sixrd_add_domain_command, static) = {
320 .path = "sixrd add domain",
322 "sixrd add domain ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> ip4-src <ip4-addr>",
323 .function = sixrd_add_domain_command_fn,
326 VLIB_CLI_COMMAND(sixrd_del_command, static) = {
327 .path = "sixrd del domain",
329 "sixrd del domain index <domain>",
330 .function = sixrd_del_domain_command_fn,
333 VLIB_CLI_COMMAND(show_sixrd_domain_command, static) = {
334 .path = "show sixrd domain",
335 .function = show_sixrd_domain_command_fn,
338 VLIB_CLI_COMMAND(show_sixrd_stats_command, static) = {
339 .path = "show sixrd stats",
340 .function = show_sixrd_stats_command_fn,
344 * This routine exists to convince the vlib plugin framework that
345 * we haven't accidentally copied a random .dll into the plugin directory.
347 * Also collects global variable pointers passed from the vpp engine
350 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
353 clib_error_t * error = 0;
354 sixrd_main_t *mm = &sixrd_main;
356 mm->vnet_main = vnet_get_main();
362 static clib_error_t * sixrd_init (vlib_main_t * vm)
364 sixrd_dpo_module_init ();
369 VLIB_INIT_FUNCTION (sixrd_init);