Plugin infrastructure improvements
[vpp.git] / src / plugins / sixrd / 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 #include <vnet/plugin/plugin.h>
18
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/ip6_fib.h>
21 #include <vnet/adj/adj.h>
22 #include <vpp/app/version.h>
23
24 /*
25  * This code supports the following sixrd modes:
26  * 
27  * 32 EA bits (Complete IPv4 address is embedded):
28  *   ea_bits_len = 32
29  * IPv4 suffix is embedded:
30  *   ea_bits_len = < 32
31  * No embedded address bits (1:1 mode):
32  *   ea_bits_len = 0
33  */
34
35 int
36 sixrd_create_domain (ip6_address_t *ip6_prefix,
37                      u8 ip6_prefix_len,
38                      ip4_address_t *ip4_prefix,
39                      u8 ip4_prefix_len,
40                      ip4_address_t *ip4_src,
41                      u32 *sixrd_domain_index,
42                      u16 mtu)
43 {
44   dpo_id_t dpo_v6 = DPO_INVALID, dpo_v4 = DPO_INVALID;
45   sixrd_main_t *mm = &sixrd_main;
46   fib_node_index_t fei;
47   sixrd_domain_t *d;
48
49   /* Get domain index */
50   pool_get_aligned(mm->domains, d, CLIB_CACHE_LINE_BYTES);
51   memset(d, 0, sizeof (*d));
52   *sixrd_domain_index = d - mm->domains;
53
54   /* Init domain struct */
55   d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
56   d->ip4_prefix_len = ip4_prefix_len;
57   d->ip6_prefix = *ip6_prefix;
58   d->ip6_prefix_len = ip6_prefix_len;
59   d->ip4_src = *ip4_src;
60   d->mtu = mtu;
61
62   if (ip4_prefix_len < 32)
63     d->shift = 64 - ip6_prefix_len + (32 - ip4_prefix_len);
64     
65   /* Create IPv6 route/adjacency */
66   fib_prefix_t pfx6 = {
67       .fp_proto = FIB_PROTOCOL_IP6,
68       .fp_len = d->ip6_prefix_len,
69       .fp_addr = {
70           .ip6 = d->ip6_prefix,
71       },
72   };
73   sixrd_dpo_create(DPO_PROTO_IP6,
74                    *sixrd_domain_index,
75                    &dpo_v6);
76   fib_table_entry_special_dpo_add(0, &pfx6,
77                                   FIB_SOURCE_SIXRD,
78                                   FIB_ENTRY_FLAG_EXCLUSIVE,
79                                   &dpo_v6);
80   dpo_reset (&dpo_v6);
81
82   /*
83    * Multiple SIXRD domains may share same source IPv4 TEP
84    * In this case the route will exist and be SixRD sourced.
85    * Find the adj (if any) already contributed and modify it
86    */
87   fib_prefix_t pfx4 = {
88       .fp_proto = FIB_PROTOCOL_IP4,
89       .fp_len = 32,
90       .fp_addr = {
91           .ip4 = d->ip4_src,
92       },
93   };
94   fei = fib_table_lookup_exact_match(0, &pfx4);
95
96   if (FIB_NODE_INDEX_INVALID != fei)
97   {
98       dpo_id_t dpo = DPO_INVALID;
99
100       if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SIXRD, &dpo))
101       {
102           /*
103            * modify the existing adj to indicate it's shared
104            * skip to route add.
105            * It is locked to pair with the unlock below.
106            */
107           const dpo_id_t *sd_dpo;
108           sixrd_dpo_t *sd;
109
110           ASSERT(DPO_LOAD_BALANCE == dpo.dpoi_type);
111
112           sd_dpo = load_balance_get_bucket(dpo.dpoi_index, 0);
113           sd = sixrd_dpo_get (sd_dpo->dpoi_index);
114
115           sd->sd_domain = ~0;
116           dpo_copy (&dpo_v4, sd_dpo);
117           dpo_reset (&dpo);
118
119           goto route_add;
120       }
121   }
122   /* first time addition of the route */
123   sixrd_dpo_create(DPO_PROTO_IP4,
124                    *sixrd_domain_index,
125                    &dpo_v4);
126
127 route_add:
128   /*
129    * Create ip4 route. This is a reference counted add. If the prefix
130    * already exists and is SixRD sourced, it is now SixRD source n+1 times
131    * and will need to be removed n+1 times.
132    */
133   fib_table_entry_special_dpo_add(0, &pfx4,
134                                   FIB_SOURCE_SIXRD,
135                                   FIB_ENTRY_FLAG_EXCLUSIVE,
136                                   &dpo_v4);
137   dpo_reset (&dpo_v4);
138
139   return 0;
140 }
141
142 /*
143  * sixrd_delete_domain
144  */
145 int
146 sixrd_delete_domain (u32 sixrd_domain_index)
147 {
148   sixrd_main_t *mm = &sixrd_main;
149   sixrd_domain_t *d;
150
151   if (pool_is_free_index(mm->domains, sixrd_domain_index)) {
152     clib_warning("SIXRD domain delete: domain does not exist: %d",
153                  sixrd_domain_index);
154     return -1;
155   }
156
157   d = pool_elt_at_index(mm->domains, sixrd_domain_index);
158
159   fib_prefix_t pfx = {
160       .fp_proto = FIB_PROTOCOL_IP4,
161       .fp_len = 32,
162       .fp_addr = {
163           .ip4 = d->ip4_src,
164       },
165   };
166   fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_SIXRD);
167
168   fib_prefix_t pfx6 = {
169       .fp_proto = FIB_PROTOCOL_IP6,
170       .fp_len = d->ip6_prefix_len,
171       .fp_addr = {
172           .ip6 = d->ip6_prefix,
173       },
174   };
175   fib_table_entry_special_remove(0, &pfx6, FIB_SOURCE_SIXRD);
176
177   pool_put(mm->domains, d);
178
179   return 0;
180 }
181
182 static clib_error_t *
183 sixrd_add_domain_command_fn (vlib_main_t *vm,
184                            unformat_input_t *input,
185                            vlib_cli_command_t *cmd)
186 {
187   unformat_input_t _line_input, *line_input = &_line_input;
188   ip4_address_t ip4_prefix;
189   ip6_address_t ip6_prefix;
190   ip4_address_t ip4_src;
191   u32 ip6_prefix_len=0, ip4_prefix_len=0, sixrd_domain_index;
192   u32 num_m_args = 0;
193   /* Optional arguments */
194   u32 mtu = 0;
195
196   /* Get a line of input. */
197   if (!unformat_user(input, unformat_line_input, line_input))
198     return 0;
199   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
200     if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix, &ip6_prefix_len))
201       num_m_args++;
202     else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
203       num_m_args++;
204     else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
205       num_m_args++;
206     else if (unformat(line_input, "mtu %d", &mtu))
207       num_m_args++;
208     else
209       return clib_error_return(0, "unknown input `%U'",
210                                format_unformat_error, input);
211   }
212   unformat_free(line_input);
213
214   if (num_m_args < 3)
215     return clib_error_return(0, "mandatory argument(s) missing");
216
217   sixrd_create_domain(&ip6_prefix, ip6_prefix_len, &ip4_prefix, ip4_prefix_len,
218                       &ip4_src, &sixrd_domain_index, mtu);
219
220   return 0;
221 }
222
223 static clib_error_t *
224 sixrd_del_domain_command_fn (vlib_main_t *vm,
225                            unformat_input_t *input,
226                            vlib_cli_command_t *cmd)
227 {
228   unformat_input_t _line_input, *line_input = &_line_input;
229   u32 num_m_args = 0;
230   u32 sixrd_domain_index;
231
232   /* Get a line of input. */
233   if (! unformat_user(input, unformat_line_input, line_input))
234     return 0;
235  
236   while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
237     if (unformat(line_input, "index %d", &sixrd_domain_index))
238       num_m_args++;
239     else
240       return clib_error_return(0, "unknown input `%U'",
241                                 format_unformat_error, input);
242   }
243   unformat_free(line_input);
244
245   if (num_m_args != 1)
246     return clib_error_return(0, "mandatory argument(s) missing");
247
248   sixrd_delete_domain(sixrd_domain_index);
249
250   return 0;
251 }
252
253 static u8 *
254 format_sixrd_domain (u8 *s, va_list *args)
255 {
256   sixrd_domain_t *d = va_arg(*args, sixrd_domain_t *);
257   sixrd_main_t *mm = &sixrd_main;
258
259   s = format(s,
260              "[%d] ip6-pfx %U/%d ip4-pfx %U/%d ip4-src %U mtu %d",
261              d - mm->domains,
262              format_ip6_address, &d->ip6_prefix, d->ip6_prefix_len,
263              format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
264              format_ip4_address, &d->ip4_src, d->mtu);
265
266   return s;
267 }
268
269 static clib_error_t *
270 show_sixrd_domain_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
271 {
272   sixrd_main_t *mm = &sixrd_main;
273   sixrd_domain_t *d;
274
275   if (pool_elts(mm->domains) == 0)
276     vlib_cli_output(vm, "No SIXRD domains are configured...");
277
278   pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_sixrd_domain, d);}));
279
280   return 0;
281
282 }
283
284 static clib_error_t *
285 show_sixrd_stats_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
286 {
287   sixrd_main_t *mm = &sixrd_main;
288   sixrd_domain_t *d;
289   int domains = 0, domaincount = 0;
290   if (pool_elts (mm->domains) == 0)
291     vlib_cli_output (vm, "No SIXRD domains are configured...");
292
293   pool_foreach(d, mm->domains, ({
294     domains += sizeof(*d);
295     domaincount++;
296   }));
297
298   vlib_cli_output(vm, "SIXRD domains structure: %d\n", sizeof (sixrd_domain_t));
299   vlib_cli_output(vm, "SIXRD domains: %d (%d bytes)\n", domaincount, domains);
300
301   return 0;
302 }
303
304 /*
305  * packet trace format function
306  */
307 u8 *
308 format_sixrd_trace (u8 *s, va_list *args)
309 {
310   CLIB_UNUSED(vlib_main_t *vm) = va_arg (*args, vlib_main_t *);
311   CLIB_UNUSED(vlib_node_t *node) = va_arg (*args, vlib_node_t *);
312   sixrd_trace_t *t = va_arg (*args, sixrd_trace_t *);
313   u32 sixrd_domain_index = t->sixrd_domain_index;
314
315   s = format(s, "SIXRD domain index: %d", sixrd_domain_index);
316
317   return s;
318 }
319
320 VLIB_CLI_COMMAND(sixrd_add_domain_command, static) = {
321   .path = "sixrd add domain",
322   .short_help = 
323   "sixrd add domain ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> ip4-src <ip4-addr>",
324   .function = sixrd_add_domain_command_fn,
325 };
326
327 VLIB_CLI_COMMAND(sixrd_del_command, static) = {
328   .path = "sixrd del domain",
329   .short_help = 
330   "sixrd del domain index <domain>",
331   .function = sixrd_del_domain_command_fn,
332 };
333
334 VLIB_CLI_COMMAND(show_sixrd_domain_command, static) = {
335   .path = "show sixrd domain",
336   .function = show_sixrd_domain_command_fn,
337 };
338
339 VLIB_CLI_COMMAND(show_sixrd_stats_command, static) = {
340   .path = "show sixrd stats",
341   .function = show_sixrd_stats_command_fn,
342 };
343
344 /* *INDENT-OFF* */
345 VLIB_PLUGIN_REGISTER () = {
346     .version = VPP_BUILD_VER,
347 };
348 /* *INDENT-ON* */
349
350 static clib_error_t * sixrd_init (vlib_main_t * vm)
351 {
352   sixrd_main_t *mm = &sixrd_main;
353
354   mm->vnet_main = vnet_get_main();
355   mm->vlib_main = vm;
356
357   sixrd_dpo_module_init ();
358
359   return (NULL);
360 }
361
362 VLIB_INIT_FUNCTION (sixrd_init);