vlib: refactor node function variants
[vpp.git] / src / vlib / node_init.c
1 /*
2  * Copyright (c) 2020 Intel 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  * node_init.c: node march variant startup initialization
17  *
18  * Copyright (c) 2020 Intel Corporation
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <vlib/vlib.h>
43
44 static clib_error_t *
45 vlib_node_config (vlib_main_t *vm, unformat_input_t *input)
46 {
47   clib_error_t *error = 0;
48   unformat_input_t sub_input;
49   u32 *march_variant_by_node = 0;
50   clib_march_variant_type_t march_variant;
51   u32 node_index;
52   int i;
53
54   /* specify prioritization defaults for all graph nodes */
55   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
56     {
57       if (unformat (input, "default %U", unformat_vlib_cli_sub_input,
58                     &sub_input))
59         {
60           while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
61             {
62               if (!unformat (&sub_input, "variant %U",
63                              unformat_vlib_node_variant, &march_variant))
64                 return clib_error_return (0,
65                                           "please specify a valid node variant");
66
67               vec_validate_init_empty (march_variant_by_node,
68                                        vec_len (vm->node_main.nodes) - 1, ~0);
69               vec_foreach_index (i, march_variant_by_node)
70                 march_variant_by_node[i] = march_variant;
71               vm->node_main.node_fn_default_march_variant = march_variant;
72               unformat_free (&sub_input);
73             }
74         }
75       else /* specify prioritization for an individual graph node */
76         if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
77         {
78           if (unformat (input, "%U", unformat_vlib_cli_sub_input, &sub_input))
79             {
80               while (unformat_check_input (&sub_input) !=
81                      UNFORMAT_END_OF_INPUT)
82                 {
83                   if (!unformat (&sub_input, "variant %U",
84                                  unformat_vlib_node_variant, &march_variant))
85                     return clib_error_return (0,
86                                               "please specify a valid node variant");
87                   vec_validate_init_empty (march_variant_by_node, node_index,
88                                            ~0);
89                   march_variant_by_node[node_index] = march_variant;
90                   unformat_free (&sub_input);
91                 }
92             }
93         }
94       else
95         {
96           break;
97         }
98     }
99
100   if (march_variant_by_node)
101     {
102       vec_foreach_index (i, march_variant_by_node)
103         if (march_variant_by_node[i] != ~0)
104           vlib_node_set_march_variant (vm, i, march_variant_by_node[i]);
105       vec_free (march_variant_by_node);
106     }
107   unformat_free (input);
108
109   return error;
110 }
111
112 VLIB_CONFIG_FUNCTION (vlib_node_config, "node");
113
114 /*
115  * fd.io coding-style-patch-verification: ON
116  *
117  * Local Variables:
118  * eval: (c-set-style "gnu")
119  * End:
120  */