Improve feature arc order constraint specification
[vpp.git] / src / vnet / feature / feature.c
1 /*
2  * Copyright (c) 2016 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 <vnet/feature/feature.h>
17 #include <vnet/adj/adj.h>
18
19 vnet_feature_main_t feature_main;
20
21 static clib_error_t *
22 vnet_feature_init (vlib_main_t * vm)
23 {
24   vnet_feature_main_t *fm = &feature_main;
25   vnet_feature_registration_t *freg;
26   vnet_feature_arc_registration_t *areg;
27   vnet_feature_constraint_registration_t *creg;
28   u32 arc_index = 0;
29
30   fm->arc_index_by_name = hash_create_string (0, sizeof (uword));
31   areg = fm->next_arc;
32
33   /* process feature arc registrations */
34   while (areg)
35     {
36       char *s;
37       int i = 0;
38       areg->feature_arc_index = arc_index;
39       if (areg->arc_index_ptr)
40         *areg->arc_index_ptr = arc_index;
41       hash_set_mem (fm->arc_index_by_name, areg->arc_name,
42                     pointer_to_uword (areg));
43
44       /* process start nodes */
45       while ((s = areg->start_nodes[i]))
46         {
47           i++;
48         }
49       areg->n_start_nodes = i;
50
51       /* next */
52       areg = areg->next;
53       arc_index++;
54     }
55
56   vec_validate (fm->next_feature_by_arc, arc_index - 1);
57   vec_validate (fm->feature_nodes, arc_index - 1);
58   vec_validate (fm->feature_config_mains, arc_index - 1);
59   vec_validate (fm->next_feature_by_name, arc_index - 1);
60   vec_validate (fm->sw_if_index_has_features, arc_index - 1);
61   vec_validate (fm->feature_count_by_sw_if_index, arc_index - 1);
62   vec_validate (fm->next_constraint_by_arc, arc_index - 1);
63
64   freg = fm->next_feature;
65   while (freg)
66     {
67       vnet_feature_registration_t *next;
68       uword *p = hash_get_mem (fm->arc_index_by_name, freg->arc_name);
69       if (p == 0)
70         {
71           /* Don't start vpp with broken features arcs */
72           clib_warning ("Unknown feature arc '%s'", freg->arc_name);
73           os_exit (1);
74         }
75
76       areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
77       arc_index = areg->feature_arc_index;
78
79       next = freg->next;
80       freg->next_in_arc = fm->next_feature_by_arc[arc_index];
81       fm->next_feature_by_arc[arc_index] = freg;
82
83       /* next */
84       freg = next;
85     }
86
87   /* Move bulk constraints to the constraint by arc lists */
88   creg = fm->next_constraint;
89   while (creg)
90     {
91       vnet_feature_constraint_registration_t *next;
92       uword *p = hash_get_mem (fm->arc_index_by_name, creg->arc_name);
93       if (p == 0)
94         {
95           /* Don't start vpp with broken features arcs */
96           clib_warning ("Unknown feature arc '%s'", creg->arc_name);
97           os_exit (1);
98         }
99
100       areg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
101       arc_index = areg->feature_arc_index;
102
103       next = creg->next;
104       creg->next_in_arc = fm->next_constraint_by_arc[arc_index];
105       fm->next_constraint_by_arc[arc_index] = creg;
106
107       /* next */
108       creg = next;
109     }
110
111
112   areg = fm->next_arc;
113   while (areg)
114     {
115       clib_error_t *error;
116       vnet_feature_config_main_t *cm;
117       vnet_config_main_t *vcm;
118
119       arc_index = areg->feature_arc_index;
120       cm = &fm->feature_config_mains[arc_index];
121       vcm = &cm->config_main;
122       if ((error = vnet_feature_arc_init
123            (vm, vcm, areg->start_nodes, areg->n_start_nodes,
124             fm->next_feature_by_arc[arc_index],
125             fm->next_constraint_by_arc[arc_index],
126             &fm->feature_nodes[arc_index])))
127         {
128           clib_error_report (error);
129           os_exit (1);
130         }
131
132       fm->next_feature_by_name[arc_index] =
133         hash_create_string (0, sizeof (uword));
134       freg = fm->next_feature_by_arc[arc_index];
135
136       while (freg)
137         {
138           hash_set_mem (fm->next_feature_by_name[arc_index],
139                         freg->node_name, pointer_to_uword (freg));
140           freg = freg->next_in_arc;
141         }
142
143       /* next */
144       areg = areg->next;
145       arc_index++;
146     }
147
148   return 0;
149 }
150
151 VLIB_INIT_FUNCTION (vnet_feature_init);
152
153 u8
154 vnet_get_feature_arc_index (const char *s)
155 {
156   vnet_feature_main_t *fm = &feature_main;
157   vnet_feature_arc_registration_t *reg;
158   uword *p;
159
160   p = hash_get_mem (fm->arc_index_by_name, s);
161   if (p == 0)
162     return ~0;
163
164   reg = uword_to_pointer (p[0], vnet_feature_arc_registration_t *);
165   return reg->feature_arc_index;
166 }
167
168 vnet_feature_registration_t *
169 vnet_get_feature_reg (const char *arc_name, const char *node_name)
170 {
171   u8 arc_index;
172
173   arc_index = vnet_get_feature_arc_index (arc_name);
174   if (arc_index == (u8) ~ 0)
175     return 0;
176
177   vnet_feature_main_t *fm = &feature_main;
178   vnet_feature_registration_t *reg;
179   uword *p;
180
181   p = hash_get_mem (fm->next_feature_by_name[arc_index], node_name);
182   if (p == 0)
183     return 0;
184
185   reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
186   return reg;
187 }
188
189 u32
190 vnet_get_feature_index (u8 arc, const char *s)
191 {
192   vnet_feature_main_t *fm = &feature_main;
193   vnet_feature_registration_t *reg;
194   uword *p;
195
196   if (s == 0)
197     return ~0;
198
199   p = hash_get_mem (fm->next_feature_by_name[arc], s);
200   if (p == 0)
201     return ~0;
202
203   reg = uword_to_pointer (p[0], vnet_feature_registration_t *);
204   return reg->feature_index;
205 }
206
207 int
208 vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
209                                         u32 sw_if_index, int enable_disable,
210                                         void *feature_config,
211                                         u32 n_feature_config_bytes)
212 {
213   vnet_feature_main_t *fm = &feature_main;
214   vnet_feature_config_main_t *cm;
215   i16 feature_count;
216   u32 ci;
217
218   if (arc_index == (u8) ~ 0)
219     return VNET_API_ERROR_INVALID_VALUE;
220
221   if (feature_index == ~0)
222     return VNET_API_ERROR_INVALID_VALUE_2;
223
224   cm = &fm->feature_config_mains[arc_index];
225   vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
226   ci = cm->config_index_by_sw_if_index[sw_if_index];
227
228   vec_validate (fm->feature_count_by_sw_if_index[arc_index], sw_if_index);
229   feature_count = fm->feature_count_by_sw_if_index[arc_index][sw_if_index];
230
231   if (!enable_disable && feature_count < 1)
232     return 0;
233
234   ci = (enable_disable
235         ? vnet_config_add_feature
236         : vnet_config_del_feature)
237     (vlib_get_main (), &cm->config_main, ci, feature_index, feature_config,
238      n_feature_config_bytes);
239   if (ci == ~0)
240     {
241       return 0;
242     }
243   cm->config_index_by_sw_if_index[sw_if_index] = ci;
244
245   /* update feature count */
246   enable_disable = (enable_disable > 0);
247   feature_count += enable_disable ? 1 : -1;
248   ASSERT (feature_count >= 0);
249
250   fm->sw_if_index_has_features[arc_index] =
251     clib_bitmap_set (fm->sw_if_index_has_features[arc_index], sw_if_index,
252                      (feature_count > 0));
253   adj_feature_update (sw_if_index, arc_index, (feature_count > 0));
254
255   fm->feature_count_by_sw_if_index[arc_index][sw_if_index] = feature_count;
256   return 0;
257 }
258
259 int
260 vnet_feature_enable_disable (const char *arc_name, const char *node_name,
261                              u32 sw_if_index, int enable_disable,
262                              void *feature_config, u32 n_feature_config_bytes)
263 {
264   u32 feature_index;
265   u8 arc_index;
266
267   arc_index = vnet_get_feature_arc_index (arc_name);
268
269   if (arc_index == (u8) ~ 0)
270     return VNET_API_ERROR_INVALID_VALUE;
271
272   feature_index = vnet_get_feature_index (arc_index, node_name);
273
274   return vnet_feature_enable_disable_with_index (arc_index, feature_index,
275                                                  sw_if_index, enable_disable,
276                                                  feature_config,
277                                                  n_feature_config_bytes);
278 }
279
280 static int
281 feature_cmp (void *a1, void *a2)
282 {
283   vnet_feature_registration_t *reg1 = a1;
284   vnet_feature_registration_t *reg2 = a2;
285
286   return (int) reg1->feature_index - reg2->feature_index;
287 }
288
289 /** Display the set of available driver features.
290     Useful for verifying that expected features are present
291 */
292
293 static clib_error_t *
294 show_features_command_fn (vlib_main_t * vm,
295                           unformat_input_t * input, vlib_cli_command_t * cmd)
296 {
297   vnet_feature_main_t *fm = &feature_main;
298   vnet_feature_arc_registration_t *areg;
299   vnet_feature_registration_t *freg;
300   vnet_feature_registration_t *feature_regs = 0;
301   int verbose = 0;
302
303   if (unformat (input, "verbose"))
304     verbose = 1;
305
306   vlib_cli_output (vm, "Available feature paths");
307
308   areg = fm->next_arc;
309   while (areg)
310     {
311       if (verbose)
312         vlib_cli_output (vm, "[%2d] %s:", areg->feature_arc_index,
313                          areg->arc_name);
314       else
315         vlib_cli_output (vm, "%s:", areg->arc_name);
316
317       freg = fm->next_feature_by_arc[areg->feature_arc_index];
318       while (freg)
319         {
320           vec_add1 (feature_regs, freg[0]);
321           freg = freg->next_in_arc;
322         }
323
324       vec_sort_with_function (feature_regs, feature_cmp);
325
326       vec_foreach (freg, feature_regs)
327       {
328         if (verbose)
329           vlib_cli_output (vm, "  [%2d]: %s\n", freg->feature_index,
330                            freg->node_name);
331         else
332           vlib_cli_output (vm, "  %s\n", freg->node_name);
333       }
334       vec_reset_length (feature_regs);
335       /* next */
336       areg = areg->next;
337     }
338   vec_free (feature_regs);
339
340   return 0;
341 }
342
343 /*?
344  * Display the set of available driver features
345  *
346  * @cliexpar
347  * Example:
348  * @cliexcmd{show features [verbose]}
349  * @cliexend
350  * @endparblock
351 ?*/
352 /* *INDENT-OFF* */
353 VLIB_CLI_COMMAND (show_features_command, static) = {
354   .path = "show features",
355   .short_help = "show features",
356   .function = show_features_command_fn,
357 };
358 /* *INDENT-ON* */
359
360 /** Display the set of driver features configured on a specific interface
361   * Called by "show interface" handler
362  */
363
364 void
365 vnet_interface_features_show (vlib_main_t * vm, u32 sw_if_index, int verbose)
366 {
367   vnet_feature_main_t *fm = &feature_main;
368   u32 node_index, current_config_index;
369   u16 feature_arc;
370   vnet_feature_config_main_t *cm = fm->feature_config_mains;
371   vnet_feature_arc_registration_t *areg;
372   vnet_config_main_t *vcm;
373   vnet_config_t *cfg;
374   u32 cfg_index;
375   vnet_config_feature_t *feat;
376   vlib_node_t *n;
377   int i;
378
379   vlib_cli_output (vm, "Feature paths configured on %U...",
380                    format_vnet_sw_if_index_name,
381                    vnet_get_main (), sw_if_index);
382
383   areg = fm->next_arc;
384   while (areg)
385     {
386       feature_arc = areg->feature_arc_index;
387       vcm = &(cm[feature_arc].config_main);
388
389       vlib_cli_output (vm, "\n%s:", areg->arc_name);
390       areg = areg->next;
391
392       if (NULL == cm[feature_arc].config_index_by_sw_if_index ||
393           vec_len (cm[feature_arc].config_index_by_sw_if_index) <=
394           sw_if_index)
395         {
396           vlib_cli_output (vm, "  none configured");
397           continue;
398         }
399
400       current_config_index =
401         vec_elt (cm[feature_arc].config_index_by_sw_if_index, sw_if_index);
402
403       if (current_config_index == ~0)
404         {
405           vlib_cli_output (vm, "  none configured");
406           continue;
407         }
408
409       ASSERT (current_config_index
410               < vec_len (vcm->config_pool_index_by_user_index));
411
412       cfg_index = vcm->config_pool_index_by_user_index[current_config_index];
413       cfg = pool_elt_at_index (vcm->config_pool, cfg_index);
414
415       for (i = 0; i < vec_len (cfg->features); i++)
416         {
417           feat = cfg->features + i;
418           node_index = feat->node_index;
419           n = vlib_get_node (vm, node_index);
420           if (verbose)
421             vlib_cli_output (vm, "  [%2d] %v", feat->feature_index, n->name);
422           else
423             vlib_cli_output (vm, "  %v", n->name);
424         }
425     }
426 }
427
428 static clib_error_t *
429 set_interface_features_command_fn (vlib_main_t * vm,
430                                    unformat_input_t * input,
431                                    vlib_cli_command_t * cmd)
432 {
433   vnet_main_t *vnm = vnet_get_main ();
434   unformat_input_t _line_input, *line_input = &_line_input;
435   clib_error_t *error = 0;
436
437   u8 *arc_name = 0;
438   u8 *feature_name = 0;
439   u32 sw_if_index = ~0;
440   u8 enable = 1;
441
442   /* Get a line of input. */
443   if (!unformat_user (input, unformat_line_input, line_input))
444     return 0;
445
446   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
447     {
448       if (unformat
449           (line_input, "%U %v", unformat_vnet_sw_interface, vnm, &sw_if_index,
450            &feature_name))
451         ;
452       else if (unformat (line_input, "arc %v", &arc_name))
453         ;
454       else if (unformat (line_input, "disable"))
455         enable = 0;
456       else
457         {
458           if (feature_name && arc_name)
459             break;
460           error = unformat_parse_error (line_input);
461           goto done;
462         }
463     }
464
465   if (sw_if_index == ~0)
466     {
467       error = clib_error_return (0, "Interface not specified...");
468       goto done;
469     }
470
471   vec_add1 (arc_name, 0);
472   vec_add1 (feature_name, 0);
473
474   vnet_feature_registration_t *reg;
475   reg =
476     vnet_get_feature_reg ((const char *) arc_name,
477                           (const char *) feature_name);
478   if (reg == 0)
479     {
480       error = clib_error_return (0, "Unknown feature...");
481       goto done;
482     }
483   if (reg->enable_disable_cb)
484     error = reg->enable_disable_cb (sw_if_index, enable);
485   if (!error)
486     vnet_feature_enable_disable ((const char *) arc_name,
487                                  (const char *) feature_name, sw_if_index,
488                                  enable, 0, 0);
489
490 done:
491   vec_free (feature_name);
492   vec_free (arc_name);
493   unformat_free (line_input);
494   return error;
495 }
496
497 /*?
498  * Set feature for given interface
499  *
500  * @cliexpar
501  * Example:
502  * @cliexcmd{set interface feature GigabitEthernet2/0/0 ip4_flow_classify arc ip4_unicast}
503  * @cliexend
504  * @endparblock
505 ?*/
506 /* *INDENT-OFF* */
507 VLIB_CLI_COMMAND (set_interface_feature_command, static) = {
508   .path = "set interface feature",
509   .short_help = "set interface feature <intfc> <feature_name> arc <arc_name> "
510       "[disable]",
511   .function = set_interface_features_command_fn,
512 };
513 /* *INDENT-ON* */
514
515 /*
516  * fd.io coding-style-patch-verification: ON
517  *
518  * Local Variables:
519  * eval: (c-set-style "gnu")
520  * End:
521  */