Trivial: Clean up some typos.
[vpp.git] / src / plugins / abf / abf_policy.c
1 /*
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:
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 <plugins/abf/abf_policy.h>
17
18 #include <vlib/vlib.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/fib/fib_path_list.h>
21 #include <vnet/fib/fib_walk.h>
22
23 /**
24  * FIB node type the attachment is registered
25  */
26 fib_node_type_t abf_policy_fib_node_type;
27
28 /**
29  * Pool of ABF objects
30  */
31 static abf_policy_t *abf_policy_pool;
32
33 /**
34  * DB of ABF policy objects
35  *  - policy ID to index conversion.
36  */
37 static uword *abf_policy_db;
38
39
40 abf_policy_t *
41 abf_policy_get (u32 index)
42 {
43   return (pool_elt_at_index (abf_policy_pool, index));
44 }
45
46 static u32
47 abf_policy_get_index (const abf_policy_t * abf)
48 {
49   return (abf - abf_policy_pool);
50 }
51
52 static abf_policy_t *
53 abf_policy_find_i (u32 policy_id)
54 {
55   u32 api;
56
57   api = abf_policy_find (policy_id);
58
59   if (INDEX_INVALID != api)
60     return (abf_policy_get (api));
61
62   return (NULL);
63 }
64
65 u32
66 abf_policy_find (u32 policy_id)
67 {
68   uword *p;
69
70   p = hash_get (abf_policy_db, policy_id);
71
72   if (NULL != p)
73     return (p[0]);
74
75   return (INDEX_INVALID);
76 }
77
78
79 void
80 abf_policy_update (u32 policy_id,
81                    u32 acl_index, const fib_route_path_t * rpaths)
82 {
83   abf_policy_t *ap;
84   u32 api;
85
86   api = abf_policy_find (policy_id);
87
88   if (INDEX_INVALID == api)
89     {
90       /*
91        * create a new policy
92        */
93       pool_get (abf_policy_pool, ap);
94
95       api = ap - abf_policy_pool;
96       fib_node_init (&ap->ap_node, abf_policy_fib_node_type);
97       ap->ap_acl = acl_index;
98       ap->ap_id = policy_id;
99       ap->ap_pl = fib_path_list_create ((FIB_PATH_LIST_FLAG_SHARED |
100                                          FIB_PATH_LIST_FLAG_NO_URPF), rpaths);
101
102       /*
103        * become a child of the path list so we get poked when
104        * the forwarding changes.
105        */
106       ap->ap_sibling = fib_path_list_child_add (ap->ap_pl,
107                                                 abf_policy_fib_node_type,
108                                                 api);
109
110       /*
111        * add this new policy to the DB
112        */
113       hash_set (abf_policy_db, policy_id, api);
114
115       /*
116        * take a lock on behalf of the CLI/API creation
117        */
118       fib_node_lock (&ap->ap_node);
119     }
120   else
121     {
122       /*
123        * update an existing policy.
124        * - add the path to the path-list and swap our ancestry
125        * - backwalk to poke all attachments to update
126        */
127       fib_node_index_t old_pl;
128
129       ap = abf_policy_get (api);
130       old_pl = ap->ap_pl;
131
132       if (FIB_NODE_INDEX_INVALID != old_pl)
133         {
134           ap->ap_pl = fib_path_list_copy_and_path_add (old_pl,
135                                                        (FIB_PATH_LIST_FLAG_SHARED
136                                                         |
137                                                         FIB_PATH_LIST_FLAG_NO_URPF),
138                                                        rpaths);
139           fib_path_list_child_remove (old_pl, ap->ap_sibling);
140         }
141       else
142         {
143           ap->ap_pl = fib_path_list_create ((FIB_PATH_LIST_FLAG_SHARED |
144                                              FIB_PATH_LIST_FLAG_NO_URPF),
145                                             rpaths);
146         }
147
148       ap->ap_sibling = fib_path_list_child_add (ap->ap_pl,
149                                                 abf_policy_fib_node_type,
150                                                 api);
151
152       fib_node_back_walk_ctx_t ctx = {
153         .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
154       };
155
156       fib_walk_sync (abf_policy_fib_node_type, api, &ctx);
157     }
158 }
159
160 static void
161 abf_policy_destroy (abf_policy_t * ap)
162 {
163   /*
164    * this ABF should not be a sibling on the path list, since
165    * that was removed when the API config went
166    */
167   ASSERT (ap->ap_sibling == ~0);
168   ASSERT (ap->ap_pl == FIB_NODE_INDEX_INVALID);
169
170   hash_unset (abf_policy_db, ap->ap_id);
171   pool_put (abf_policy_pool, ap);
172 }
173
174 int
175 abf_policy_delete (u32 policy_id, const fib_route_path_t * rpaths)
176 {
177   abf_policy_t *ap;
178   u32 api;
179
180   api = abf_policy_find (policy_id);
181
182   if (INDEX_INVALID == api)
183     {
184       /*
185        * no such policy
186        */
187       return (-1);
188     }
189   else
190     {
191       /*
192        * update an existing policy.
193        * - add the path to the path-list and swap our ancestry
194        * - backwalk to poke all attachments to update
195        */
196       fib_node_index_t old_pl;
197
198       ap = abf_policy_get (api);
199       old_pl = ap->ap_pl;
200
201       ap->ap_pl =
202         fib_path_list_copy_and_path_remove (ap->ap_pl,
203                                             (FIB_PATH_LIST_FLAG_SHARED |
204                                              FIB_PATH_LIST_FLAG_NO_URPF),
205                                             rpaths);
206
207       fib_path_list_child_remove (old_pl, ap->ap_sibling);
208       ap->ap_sibling = ~0;
209
210       if (FIB_NODE_INDEX_INVALID == ap->ap_pl)
211         {
212           /*
213            * no more paths on this policy. It's toast
214            * remove the CLI/API's lock
215            */
216           fib_node_unlock (&ap->ap_node);
217         }
218       else
219         {
220           ap->ap_sibling = fib_path_list_child_add (ap->ap_pl,
221                                                     abf_policy_fib_node_type,
222                                                     api);
223
224           fib_node_back_walk_ctx_t ctx = {
225             .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
226           };
227
228           fib_walk_sync (abf_policy_fib_node_type, api, &ctx);
229         }
230     }
231
232   return (0);
233 }
234
235 static clib_error_t *
236 abf_policy_cmd (vlib_main_t * vm,
237                 unformat_input_t * main_input, vlib_cli_command_t * cmd)
238 {
239   unformat_input_t _line_input, *line_input = &_line_input;
240   u32 acl_index, policy_id;
241   fib_route_path_t *rpaths = NULL, rpath;
242   u32 is_del;
243
244   is_del = 0;
245   acl_index = INDEX_INVALID;
246   policy_id = INDEX_INVALID;
247
248   /* Get a line of input. */
249   if (!unformat_user (main_input, unformat_line_input, line_input))
250     return 0;
251
252   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
253     {
254       if (unformat (line_input, "acl %d", &acl_index))
255         ;
256       else if (unformat (line_input, "id %d", &policy_id))
257         ;
258       else if (unformat (line_input, "del"))
259         is_del = 1;
260       else if (unformat (line_input, "add"))
261         is_del = 0;
262       else if (unformat (line_input, "via %U",
263                          unformat_fib_route_path, &rpath))
264         vec_add1 (rpaths, rpath);
265       else
266         return (clib_error_return (0, "unknown input '%U'",
267                                    format_unformat_error, line_input));
268     }
269
270   if (INDEX_INVALID == policy_id)
271     {
272       vlib_cli_output (vm, "Specify a Policy ID");
273       return 0;
274     }
275
276   if (!is_del)
277     {
278       if (INDEX_INVALID == acl_index)
279         {
280           vlib_cli_output (vm, "ACL index must be set");
281           return 0;
282         }
283
284       abf_policy_update (policy_id, acl_index, rpaths);
285     }
286   else
287     {
288       abf_policy_delete (policy_id, rpaths);
289     }
290
291   unformat_free (line_input);
292   return (NULL);
293 }
294
295 /* *INDENT-OFF* */
296 /**
297  * Create an ABF policy.
298  */
299 VLIB_CLI_COMMAND (abf_policy_cmd_node, static) = {
300   .path = "abf policy",
301   .function = abf_policy_cmd,
302   .short_help = "abf policy [add|del] id <index> acl <index> via ...",
303   .is_mp_safe = 1,
304 };
305 /* *INDENT-ON* */
306
307 static u8 *
308 format_abf (u8 * s, va_list * args)
309 {
310   abf_policy_t *ap = va_arg (*args, abf_policy_t *);
311
312   s = format (s, "abf:[%d]: policy:%d acl:%d",
313               ap - abf_policy_pool, ap->ap_id, ap->ap_acl);
314   s = format (s, "\n ");
315   if (FIB_NODE_INDEX_INVALID == ap->ap_pl)
316     {
317       s = format (s, "no forwarding");
318     }
319   else
320     {
321       s = fib_path_list_format (ap->ap_pl, s);
322     }
323
324   return (s);
325 }
326
327 void
328 abf_policy_walk (abf_policy_walk_cb_t cb, void *ctx)
329 {
330   u32 api;
331
332   /* *INDENT-OFF* */
333   pool_foreach_index(api, abf_policy_pool,
334   ({
335     if (!cb(api, ctx))
336       break;
337   }));
338   /* *INDENT-ON* */
339 }
340
341 static clib_error_t *
342 abf_show_policy_cmd (vlib_main_t * vm,
343                      unformat_input_t * input, vlib_cli_command_t * cmd)
344 {
345   u32 policy_id;
346   abf_policy_t *ap;
347
348   policy_id = INDEX_INVALID;
349
350   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
351     {
352       if (unformat (input, "%d", &policy_id))
353         ;
354       else
355         return (clib_error_return (0, "unknown input '%U'",
356                                    format_unformat_error, input));
357     }
358
359   if (INDEX_INVALID == policy_id)
360     {
361       /* *INDENT-OFF* */
362       pool_foreach(ap, abf_policy_pool,
363       ({
364         vlib_cli_output(vm, "%U", format_abf, ap);
365       }));
366       /* *INDENT-ON* */
367     }
368   else
369     {
370       ap = abf_policy_find_i (policy_id);
371
372       if (NULL != ap)
373         vlib_cli_output (vm, "%U", format_abf, ap);
374       else
375         vlib_cli_output (vm, "Invalid policy ID:%d", policy_id);
376     }
377
378   return (NULL);
379 }
380
381 /* *INDENT-OFF* */
382 VLIB_CLI_COMMAND (abf_policy_show_policy_cmd_node, static) = {
383   .path = "show abf policy",
384   .function = abf_show_policy_cmd,
385   .short_help = "show abf policy <value>",
386   .is_mp_safe = 1,
387 };
388 /* *INDENT-ON* */
389
390 static fib_node_t *
391 abf_policy_get_node (fib_node_index_t index)
392 {
393   abf_policy_t *ap = abf_policy_get (index);
394   return (&(ap->ap_node));
395 }
396
397 static abf_policy_t *
398 abf_policy_get_from_node (fib_node_t * node)
399 {
400   return ((abf_policy_t *) (((char *) node) -
401                             STRUCT_OFFSET_OF (abf_policy_t, ap_node)));
402 }
403
404 static void
405 abf_policy_last_lock_gone (fib_node_t * node)
406 {
407   abf_policy_destroy (abf_policy_get_from_node (node));
408 }
409
410 /*
411  * A back walk has reached this ABF policy
412  */
413 static fib_node_back_walk_rc_t
414 abf_policy_back_walk_notify (fib_node_t * node,
415                              fib_node_back_walk_ctx_t * ctx)
416 {
417   /*
418    * re-stack the fmask on the n-eos of the via
419    */
420   abf_policy_t *abf = abf_policy_get_from_node (node);
421
422   /*
423    * propagate further up the graph.
424    * we can do this synchronously since the fan out is small.
425    */
426   fib_walk_sync (abf_policy_fib_node_type, abf_policy_get_index (abf), ctx);
427
428   return (FIB_NODE_BACK_WALK_CONTINUE);
429 }
430
431 /*
432  * The BIER fmask's graph node virtual function table
433  */
434 static const fib_node_vft_t abf_policy_vft = {
435   .fnv_get = abf_policy_get_node,
436   .fnv_last_lock = abf_policy_last_lock_gone,
437   .fnv_back_walk = abf_policy_back_walk_notify,
438 };
439
440 static clib_error_t *
441 abf_policy_init (vlib_main_t * vm)
442 {
443   abf_policy_fib_node_type = fib_node_register_new_type (&abf_policy_vft);
444
445   return (NULL);
446 }
447
448 VLIB_INIT_FUNCTION (abf_policy_init);
449
450 /*
451  * fd.io coding-style-patch-verification: ON
452  *
453  * Local Variables:
454  * eval: (c-set-style "gnu")
455  * End:
456  */