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.
19 * This code supports the following sixrd modes:
21 * 32 EA bits (Complete IPv4 address is embedded):
23 * IPv4 suffix is embedded:
25 * No embedded address bits (1:1 mode):
30 sixrd_create_domain (ip6_address_t *ip6_prefix,
32 ip4_address_t *ip4_prefix,
34 ip4_address_t *ip4_src,
35 u32 *sixrd_domain_index,
38 sixrd_main_t *mm = &sixrd_main;
39 ip4_main_t *im4 = &ip4_main;
40 ip6_main_t *im6 = &ip6_main;
43 ip4_add_del_route_args_t args4;
44 ip6_add_del_route_args_t args6;
47 /* Get domain index */
48 pool_get_aligned(mm->domains, d, CLIB_CACHE_LINE_BYTES);
49 memset(d, 0, sizeof (*d));
50 *sixrd_domain_index = d - mm->domains;
52 /* Init domain struct */
53 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
54 d->ip4_prefix_len = ip4_prefix_len;
55 d->ip6_prefix = *ip6_prefix;
56 d->ip6_prefix_len = ip6_prefix_len;
57 d->ip4_src = *ip4_src;
60 if (ip4_prefix_len < 32)
61 d->shift = 64 - ip6_prefix_len + (32 - ip4_prefix_len);
63 /* Init IP adjacency */
64 memset(&adj, 0, sizeof(adj));
65 adj.explicit_fib_index = ~0;
66 adj.lookup_next_index = IP_LOOKUP_NEXT_SIXRD;
67 p = (u32 *)&adj.rewrite_data[0];
68 *p = (u32) (*sixrd_domain_index);
70 /* Create ip6 adjacency */
71 memset(&args6, 0, sizeof(args6));
72 args6.table_index_or_table_id = 0;
73 args6.flags = IP6_ROUTE_FLAG_ADD;
74 args6.dst_address.as_u64[0] = ip6_prefix->as_u64[0];
75 args6.dst_address.as_u64[1] = ip6_prefix->as_u64[1];
76 args6.dst_address_length = ip6_prefix_len;
80 ip6_add_del_route(im6, &args6);
82 /* Multiple SIXRD domains may share same source IPv4 TEP */
83 uword *q = ip4_get_route(im4, 0, 0, (u8 *)ip4_src, 32);
86 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
87 ip_adjacency_t *adj4 = ip_get_adjacency(lm4, ai);
88 if (adj4->lookup_next_index != IP_LOOKUP_NEXT_SIXRD) {
89 clib_warning("BR source address already assigned: %U", format_ip4_address, ip4_src);
90 pool_put(mm->domains, d);
94 p = (u32 *)&adj4->rewrite_data[0];
97 /* Add refcount, so we don't accidentially delete the route underneath someone */
100 /* Create ip4 adjacency. */
101 memset(&args4, 0, sizeof(args4));
102 args4.table_index_or_table_id = 0;
103 args4.flags = IP4_ROUTE_FLAG_ADD;
104 args4.dst_address.as_u32 = ip4_src->as_u32;
105 args4.dst_address_length = 32;
106 args4.adj_index = ~0;
107 args4.add_adj = &adj;
109 ip4_add_del_route(im4, &args4);
116 * sixrd_delete_domain
119 sixrd_delete_domain (u32 sixrd_domain_index)
121 sixrd_main_t *mm = &sixrd_main;
122 ip4_main_t *im4 = &ip4_main;
123 ip6_main_t *im6 = &ip6_main;
126 ip4_add_del_route_args_t args4;
127 ip6_add_del_route_args_t args6;
129 if (pool_is_free_index(mm->domains, sixrd_domain_index)) {
130 clib_warning("SIXRD domain delete: domain does not exist: %d", sixrd_domain_index);
134 d = pool_elt_at_index(mm->domains, sixrd_domain_index);
136 memset(&adj, 0, sizeof(adj));
137 adj.explicit_fib_index = ~0;
138 adj.lookup_next_index = IP_LOOKUP_NEXT_SIXRD;
140 /* Delete ip6 adjacency */
141 memset(&args6, 0, sizeof (args6));
142 args6.table_index_or_table_id = 0;
143 args6.flags = IP6_ROUTE_FLAG_DEL;
144 args6.dst_address.as_u64[0] = d->ip6_prefix.as_u64[0];
145 args6.dst_address.as_u64[1] = d->ip6_prefix.as_u64[1];
146 args6.dst_address_length = d->ip6_prefix_len;
148 args6.add_adj = &adj;
150 ip6_add_del_route(im6, &args6);
152 /* Delete ip4 adjacency */
153 uword *q = ip4_get_route(im4, 0, 0, (u8 *)&d->ip4_src, 32);
156 ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
157 ip_adjacency_t *adj4 = ip_get_adjacency(lm4, ai);
159 u32 *p = (u32 *)&adj4->rewrite_data[0];
160 /* Delete route when no other domains use this source */
162 memset(&args4, 0, sizeof(args4));
163 args4.table_index_or_table_id = 0;
164 args4.flags = IP4_ROUTE_FLAG_DEL;
165 args4.dst_address.as_u32 = d->ip4_prefix.as_u32;
166 args4.dst_address_length = d->ip4_prefix_len;
168 args4.add_adj = &adj;
170 ip4_add_del_route(im4, &args4);
175 pool_put(mm->domains, d);
180 static clib_error_t *
181 sixrd_add_domain_command_fn (vlib_main_t *vm,
182 unformat_input_t *input,
183 vlib_cli_command_t *cmd)
185 unformat_input_t _line_input, *line_input = &_line_input;
186 ip4_address_t ip4_prefix;
187 ip6_address_t ip6_prefix;
188 ip4_address_t ip4_src;
189 u32 ip6_prefix_len, ip4_prefix_len, sixrd_domain_index;
191 /* Optional arguments */
194 /* Get a line of input. */
195 if (!unformat_user(input, unformat_line_input, line_input))
197 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
198 if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix, &ip6_prefix_len))
200 else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
202 else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
204 else if (unformat(line_input, "mtu %d", &mtu))
207 return clib_error_return(0, "unknown input `%U'",
208 format_unformat_error, input);
210 unformat_free(line_input);
213 return clib_error_return(0, "mandatory argument(s) missing");
215 sixrd_create_domain(&ip6_prefix, ip6_prefix_len, &ip4_prefix, ip4_prefix_len,
216 &ip4_src, &sixrd_domain_index, mtu);
221 static clib_error_t *
222 sixrd_del_domain_command_fn (vlib_main_t *vm,
223 unformat_input_t *input,
224 vlib_cli_command_t *cmd)
226 unformat_input_t _line_input, *line_input = &_line_input;
228 u32 sixrd_domain_index;
230 /* Get a line of input. */
231 if (! unformat_user(input, unformat_line_input, line_input))
234 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
235 if (unformat(line_input, "index %d", &sixrd_domain_index))
238 return clib_error_return(0, "unknown input `%U'",
239 format_unformat_error, input);
241 unformat_free(line_input);
244 return clib_error_return(0, "mandatory argument(s) missing");
246 sixrd_delete_domain(sixrd_domain_index);
252 format_sixrd_domain (u8 *s, va_list *args)
254 sixrd_domain_t *d = va_arg(*args, sixrd_domain_t *);
255 sixrd_main_t *mm = &sixrd_main;
258 "[%d] ip6-pfx %U/%d ip4-pfx %U/%d ip4-src %U mtu %d",
260 format_ip6_address, &d->ip6_prefix, d->ip6_prefix_len,
261 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
262 format_ip4_address, &d->ip4_src, d->mtu);
267 static clib_error_t *
268 show_sixrd_domain_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
270 sixrd_main_t *mm = &sixrd_main;
273 if (pool_elts(mm->domains) == 0)
274 vlib_cli_output(vm, "No SIXRD domains are configured...");
276 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_sixrd_domain, d);}));
282 static clib_error_t *
283 show_sixrd_stats_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
285 sixrd_main_t *mm = &sixrd_main;
287 int domains = 0, domaincount = 0;
288 if (pool_elts (mm->domains) == 0)
289 vlib_cli_output (vm, "No SIXRD domains are configured...");
291 pool_foreach(d, mm->domains, ({
292 domains += sizeof(*d);
296 vlib_cli_output(vm, "SIXRD domains structure: %d\n", sizeof (sixrd_domain_t));
297 vlib_cli_output(vm, "SIXRD domains: %d (%d bytes)\n", domaincount, domains);
303 * packet trace format function
306 format_sixrd_trace (u8 *s, va_list *args)
308 CLIB_UNUSED(vlib_main_t *vm) = va_arg (*args, vlib_main_t *);
309 CLIB_UNUSED(vlib_node_t *node) = va_arg (*args, vlib_node_t *);
310 sixrd_trace_t *t = va_arg (*args, sixrd_trace_t *);
311 u32 sixrd_domain_index = t->sixrd_domain_index;
313 s = format(s, "SIXRD domain index: %d", sixrd_domain_index);
318 VLIB_CLI_COMMAND(sixrd_add_domain_command, static) = {
319 .path = "sixrd add domain",
321 "sixrd add domain ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> ip4-src <ip4-addr>",
322 .function = sixrd_add_domain_command_fn,
325 VLIB_CLI_COMMAND(sixrd_del_command, static) = {
326 .path = "sixrd del domain",
328 "sixrd del domain index <domain>",
329 .function = sixrd_del_domain_command_fn,
332 VLIB_CLI_COMMAND(show_sixrd_domain_command, static) = {
333 .path = "show sixrd domain",
334 .function = show_sixrd_domain_command_fn,
337 VLIB_CLI_COMMAND(show_sixrd_stats_command, static) = {
338 .path = "show sixrd stats",
339 .function = show_sixrd_stats_command_fn,
345 clib_error_t *sixrd_init (vlib_main_t *vm)
347 sixrd_main_t *mm = &sixrd_main;
349 mm->vnet_main = vnet_get_main();
355 VLIB_INIT_FUNCTION(sixrd_init);