26b4eea9a86e4e45fec004a3f6601ef3385238f5
[vpp.git] / vnet / vnet / map / sixrd.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include "sixrd.h"
17
18 /*
19  * This code supports the following sixrd modes:
20  * 
21  * 32 EA bits (Complete IPv4 address is embedded):
22  *   ea_bits_len = 32
23  * IPv4 suffix is embedded:
24  *   ea_bits_len = < 32
25  * No embedded address bits (1:1 mode):
26  *   ea_bits_len = 0
27  */
28
29 int
30 sixrd_create_domain (ip6_address_t *ip6_prefix,
31                    u8 ip6_prefix_len,
32                    ip4_address_t *ip4_prefix,
33                    u8 ip4_prefix_len,
34                    ip4_address_t *ip4_src,
35                    u32 *sixrd_domain_index,
36                      u16 mtu)
37 {
38   sixrd_main_t *mm = &sixrd_main;
39   ip4_main_t *im4 = &ip4_main;
40   ip6_main_t *im6 = &ip6_main;
41   sixrd_domain_t *d;
42   ip_adjacency_t adj;
43   ip4_add_del_route_args_t args4;
44   ip6_add_del_route_args_t args6;
45   u32 *p;
46
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;
51
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;
58   d->mtu = mtu;
59
60   if (ip4_prefix_len < 32)
61     d->shift = 64 - ip6_prefix_len + (32 - ip4_prefix_len);
62     
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);
69
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;
77   args6.adj_index = ~0;
78   args6.add_adj = &adj;
79   args6.n_add_adj = 1;
80   ip6_add_del_route(im6, &args6);
81
82   /* Multiple SIXRD domains may share same source IPv4 TEP */
83   uword *q = ip4_get_route(im4, 0, 0, (u8 *)ip4_src, 32);
84   if (q) {
85     u32 ai = q[0];
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);
91       return -1;
92     }
93     /* Shared source */
94     p = (u32 *)&adj4->rewrite_data[0];
95     p[0] = ~0;
96
97     /* Add refcount, so we don't accidentially delete the route underneath someone */
98     p[1]++;
99   } else {
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;
108     args4.n_add_adj = 1;
109     ip4_add_del_route(im4, &args4);
110   }
111
112   return 0;
113 }
114
115 /*
116  * sixrd_delete_domain
117  */
118 int
119 sixrd_delete_domain (u32 sixrd_domain_index)
120 {
121   sixrd_main_t *mm = &sixrd_main;
122   ip4_main_t *im4 = &ip4_main;
123   ip6_main_t *im6 = &ip6_main;
124   sixrd_domain_t *d;
125   ip_adjacency_t adj;
126   ip4_add_del_route_args_t args4;
127   ip6_add_del_route_args_t args6;
128
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);
131     return -1;
132   }
133
134   d = pool_elt_at_index(mm->domains, sixrd_domain_index);
135
136   memset(&adj, 0, sizeof(adj));
137   adj.explicit_fib_index = ~0;
138   adj.lookup_next_index = IP_LOOKUP_NEXT_SIXRD;
139
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;
147   args6.adj_index = 0;
148   args6.add_adj = &adj;
149   args6.n_add_adj = 0;
150   ip6_add_del_route(im6, &args6);
151
152   /* Delete ip4 adjacency */
153   uword *q = ip4_get_route(im4, 0, 0, (u8 *)&d->ip4_src, 32);
154   if (q) {
155     u32 ai = q[0];
156     ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
157     ip_adjacency_t *adj4 = ip_get_adjacency(lm4, ai);
158
159     u32 *p = (u32 *)&adj4->rewrite_data[0];
160     /* Delete route when no other domains use this source */
161     if (p[1] == 0) {
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;
167       args4.adj_index = 0;
168       args4.add_adj = &adj;
169       args4.n_add_adj = 0;
170       ip4_add_del_route(im4, &args4);
171     }
172     p[1]--;
173   }
174
175   pool_put(mm->domains, d);
176
177   return 0;
178 }
179
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)
184 {
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;
190   u32 num_m_args = 0;
191   /* Optional arguments */
192   u32 mtu = 0;
193
194   /* Get a line of input. */
195   if (!unformat_user(input, unformat_line_input, line_input))
196     return 0;
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))
199       num_m_args++;
200     else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
201       num_m_args++;
202     else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
203       num_m_args++;
204     else if (unformat(line_input, "mtu %d", &mtu))
205       num_m_args++;
206     else
207       return clib_error_return(0, "unknown input `%U'",
208                                format_unformat_error, input);
209   }
210   unformat_free(line_input);
211
212   if (num_m_args < 3)
213     return clib_error_return(0, "mandatory argument(s) missing");
214
215   sixrd_create_domain(&ip6_prefix, ip6_prefix_len, &ip4_prefix, ip4_prefix_len,
216                       &ip4_src, &sixrd_domain_index, mtu);
217
218   return 0;
219 }
220
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)
225 {
226   unformat_input_t _line_input, *line_input = &_line_input;
227   u32 num_m_args = 0;
228   u32 sixrd_domain_index;
229
230   /* Get a line of input. */
231   if (! unformat_user(input, unformat_line_input, line_input))
232     return 0;
233  
234   while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
235     if (unformat(line_input, "index %d", &sixrd_domain_index))
236       num_m_args++;
237     else
238       return clib_error_return(0, "unknown input `%U'",
239                                 format_unformat_error, input);
240   }
241   unformat_free(line_input);
242
243   if (num_m_args != 1)
244     return clib_error_return(0, "mandatory argument(s) missing");
245
246   sixrd_delete_domain(sixrd_domain_index);
247
248   return 0;
249 }
250
251 static u8 *
252 format_sixrd_domain (u8 *s, va_list *args)
253 {
254   sixrd_domain_t *d = va_arg(*args, sixrd_domain_t *);
255   sixrd_main_t *mm = &sixrd_main;
256
257   s = format(s,
258              "[%d] ip6-pfx %U/%d ip4-pfx %U/%d ip4-src %U mtu %d",
259              d - mm->domains,
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);
263
264   return s;
265 }
266
267 static clib_error_t *
268 show_sixrd_domain_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
269 {
270   sixrd_main_t *mm = &sixrd_main;
271   sixrd_domain_t *d;
272
273   if (pool_elts(mm->domains) == 0)
274     vlib_cli_output(vm, "No SIXRD domains are configured...");
275
276   pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_sixrd_domain, d);}));
277
278   return 0;
279
280 }
281
282 static clib_error_t *
283 show_sixrd_stats_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
284 {
285   sixrd_main_t *mm = &sixrd_main;
286   sixrd_domain_t *d;
287   int domains = 0, domaincount = 0;
288   if (pool_elts (mm->domains) == 0)
289     vlib_cli_output (vm, "No SIXRD domains are configured...");
290
291   pool_foreach(d, mm->domains, ({
292     domains += sizeof(*d);
293     domaincount++;
294   }));
295
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);
298
299   return 0;
300 }
301
302 /*
303  * packet trace format function
304  */
305 u8 *
306 format_sixrd_trace (u8 *s, va_list *args)
307 {
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;
312
313   s = format(s, "SIXRD domain index: %d", sixrd_domain_index);
314
315   return s;
316 }
317
318 VLIB_CLI_COMMAND(sixrd_add_domain_command, static) = {
319   .path = "sixrd add domain",
320   .short_help = 
321   "sixrd add domain ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> ip4-src <ip4-addr>",
322   .function = sixrd_add_domain_command_fn,
323 };
324
325 VLIB_CLI_COMMAND(sixrd_del_command, static) = {
326   .path = "sixrd del domain",
327   .short_help = 
328   "sixrd del domain index <domain>",
329   .function = sixrd_del_domain_command_fn,
330 };
331
332 VLIB_CLI_COMMAND(show_sixrd_domain_command, static) = {
333   .path = "show sixrd domain",
334   .function = show_sixrd_domain_command_fn,
335 };
336
337 VLIB_CLI_COMMAND(show_sixrd_stats_command, static) = {
338   .path = "show sixrd stats",
339   .function = show_sixrd_stats_command_fn,
340 };
341
342 /*
343  * sixrd_init
344  */
345 clib_error_t *sixrd_init (vlib_main_t *vm)
346 {
347   sixrd_main_t *mm = &sixrd_main;
348
349   mm->vnet_main = vnet_get_main();
350   mm->vlib_main = vm;
351
352   return 0;
353 }
354
355 VLIB_INIT_FUNCTION(sixrd_init);