qos: Store function
[vpp.git] / src / vnet / qos / qos_store.c
1 /*
2  * Copyright (c) 2018 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/qos/qos_store.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/feature/feature.h>
19
20 /**
21  * QoS Store configuration
22  */
23 typedef struct qos_store_t_
24 {
25   u8 qst_n_cfgs;
26   qos_bits_t qst_value;
27 } qos_store_t;
28
29 /**
30  * Per-interface, per-protocol vector of feature on/off configurations
31  */
32 qos_store_t *qos_store_configs[QOS_N_SOURCES];
33
34 static void
35 qos_store_feature_config (u32 sw_if_index,
36                           qos_source_t input_source,
37                           u8 enable, qos_bits_t value)
38 {
39   switch (input_source)
40     {
41     case QOS_SOURCE_IP:
42       vnet_feature_enable_disable ("ip6-unicast", "ip6-qos-store",
43                                    sw_if_index, enable, &value,
44                                    sizeof (value));
45       vnet_feature_enable_disable ("ip6-multicast", "ip6-qos-store",
46                                    sw_if_index, enable, &value,
47                                    sizeof (value));
48       vnet_feature_enable_disable ("ip4-unicast", "ip4-qos-store",
49                                    sw_if_index, enable, &value,
50                                    sizeof (value));
51       vnet_feature_enable_disable ("ip4-multicast", "ip4-qos-store",
52                                    sw_if_index, enable, &value,
53                                    sizeof (value));
54       break;
55     case QOS_SOURCE_MPLS:
56     case QOS_SOURCE_VLAN:
57     case QOS_SOURCE_EXT:
58       /* not a valid option for storeing */
59       break;
60     }
61 }
62
63 int
64 qos_store_enable (u32 sw_if_index,
65                   qos_source_t input_source, qos_bits_t value)
66 {
67   qos_store_t *qst;
68
69   if (QOS_SOURCE_IP != input_source)
70     return VNET_API_ERROR_UNIMPLEMENTED;
71
72   vec_validate (qos_store_configs[input_source], sw_if_index);
73
74   qst = &qos_store_configs[input_source][sw_if_index];
75
76   if (0 == qst->qst_n_cfgs)
77     {
78       qst->qst_value = value;
79       qos_store_feature_config (sw_if_index, input_source, 1, value);
80     }
81
82   qst->qst_n_cfgs++;
83
84   return (0);
85 }
86
87 int
88 qos_store_disable (u32 sw_if_index, qos_source_t input_source)
89 {
90   qos_store_t *qst;
91
92   if (vec_len (qos_store_configs[input_source]) <= sw_if_index)
93     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
94
95   qst = &qos_store_configs[input_source][sw_if_index];
96
97   if (0 == qst->qst_n_cfgs)
98     return VNET_API_ERROR_VALUE_EXIST;
99
100   qst->qst_n_cfgs--;
101
102   if (0 == qst->qst_n_cfgs)
103     {
104       qos_store_feature_config (sw_if_index, input_source, 0, qst->qst_value);
105     }
106
107   return (0);
108 }
109
110 void
111 qos_store_walk (qos_store_walk_cb_t fn, void *c)
112 {
113   qos_source_t qs;
114
115   FOR_EACH_QOS_SOURCE (qs)
116   {
117     qos_store_t *qst;
118     u32 sw_if_index;
119
120     vec_foreach_index (sw_if_index, qos_store_configs[qs])
121     {
122       qst = &qos_store_configs[qs][sw_if_index];
123       if (0 != qst->qst_n_cfgs)
124         fn (sw_if_index, qs, qst->qst_value, c);
125     }
126   }
127 }
128
129 /*
130  * Disable storeing feature for all protocols when the interface
131  * is deleted
132  */
133 static clib_error_t *
134 qos_store_ip_interface_add_del (vnet_main_t * vnm,
135                                 u32 sw_if_index, u32 is_add)
136 {
137   if (!is_add)
138     {
139       qos_source_t qs;
140
141       FOR_EACH_QOS_SOURCE (qs)
142       {
143         while (qos_store_disable (sw_if_index, qs) == 0);
144       }
145     }
146
147   return (NULL);
148 }
149
150 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_store_ip_interface_add_del);
151
152 clib_error_t *
153 qos_store_init (vlib_main_t * vm)
154 {
155   return 0;
156 }
157
158 VLIB_INIT_FUNCTION (qos_store_init);
159
160 static clib_error_t *
161 qos_store_cli (vlib_main_t * vm,
162                unformat_input_t * input, vlib_cli_command_t * cmd)
163 {
164   vnet_main_t *vnm = vnet_get_main ();
165   u32 sw_if_index, qs, value;
166   u8 enable;
167
168   qs = 0xff;
169   enable = 1;
170   sw_if_index = ~0;
171
172   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
173     {
174       if (unformat (input, "%U", unformat_vnet_sw_interface,
175                     vnm, &sw_if_index))
176         ;
177       else if (unformat (input, "%U", unformat_qos_source, &qs))
178         ;
179       else if (unformat (input, "enable"))
180         enable = 1;
181       else if (unformat (input, "disable"))
182         enable = 0;
183       else if (unformat (input, "value &d", &value))
184         ;
185       else
186         break;
187     }
188
189   if (~0 == sw_if_index)
190     return clib_error_return (0, "interface must be specified");
191   if (0xff == qs)
192     return clib_error_return (0, "input location must be specified");
193
194   if (enable)
195     qos_store_enable (sw_if_index, qs, value);
196   else
197     qos_store_disable (sw_if_index, qs);
198
199   return (NULL);
200 }
201
202 /*?
203  * Enable QoS bit storeing on an interface using the packet's input DSCP bits
204  * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
205  * one protocol is chosen (which is foolish) the higher layers override the
206  * lower.
207  *
208  * @cliexpar
209  * @cliexcmd{qos store ip GigEthernet0/1/0}
210  ?*/
211 /* *INDENT-OFF* */
212 VLIB_CLI_COMMAND (qos_store_command, static) = {
213   .path = "qos store",
214   .short_help = "qos store <store-source> <INTERFACE> [disable]",
215   .function = qos_store_cli,
216   .is_mp_safe = 1,
217 };
218 /* *INDENT-ON* */
219
220 static void
221 qos_store_show_one_interface (vlib_main_t * vm, u32 sw_if_index)
222 {
223   u8 n_cfgs[QOS_N_SOURCES] = { };
224   qos_source_t qs;
225   bool set;
226
227   set = false;
228
229   FOR_EACH_QOS_SOURCE (qs)
230   {
231     if (vec_len (qos_store_configs[qs]) <= sw_if_index)
232       continue;
233     if (0 != (n_cfgs[qs] = qos_store_configs[qs][sw_if_index].qst_n_cfgs))
234       set = true;
235   }
236
237   if (set)
238     {
239       vlib_cli_output (vm, " %U:", format_vnet_sw_if_index_name,
240                        vnet_get_main (), sw_if_index);
241
242       FOR_EACH_QOS_SOURCE (qs)
243       {
244         if (n_cfgs[qs] != 0)
245           vlib_cli_output (vm, "  %U -> %d",
246                            format_qos_source, qs,
247                            qos_store_configs[qs][sw_if_index].qst_value);
248       }
249     }
250 }
251
252 static clib_error_t *
253 qos_store_show (vlib_main_t * vm,
254                 unformat_input_t * input, vlib_cli_command_t * cmd)
255 {
256   vnet_main_t *vnm = vnet_get_main ();
257   qos_source_t qs;
258   u32 sw_if_index;
259
260   sw_if_index = ~0;
261
262   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
263     {
264       if (unformat (input, "%U", unformat_vnet_sw_interface,
265                     vnm, &sw_if_index))
266         ;
267     }
268
269   if (~0 == sw_if_index)
270     {
271       u32 ii, n_ints = 0;
272
273       FOR_EACH_QOS_SOURCE (qs)
274       {
275         n_ints = clib_max (n_ints, vec_len (qos_store_configs[qs]));
276       }
277
278       for (ii = 0; ii < n_ints; ii++)
279         {
280           qos_store_show_one_interface (vm, ii);
281         }
282     }
283   else
284     qos_store_show_one_interface (vm, sw_if_index);
285
286   return (NULL);
287 }
288
289 /*?
290  * Show Egress Qos Maps
291  *
292  * @cliexpar
293  * @cliexcmd{show qos egress map}
294  ?*/
295 /* *INDENT-OFF* */
296 VLIB_CLI_COMMAND (qos_store_show_command, static) = {
297   .path = "show qos store",
298   .short_help = "show qos store [interface]",
299   .function = qos_store_show,
300   .is_mp_safe = 1,
301 };
302 /* *INDENT-ON* */
303
304 /*
305  * fd.io coding-style-patch-verification: ON
306  *
307  * Local Variables:
308  * eval: (c-set-style "gnu")
309  * End:
310  */