Remove unused argument to vlib_feature_next
[vpp.git] / src / vnet / qos / qos_record.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_record.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/ip/ip6_to_ip4.h>
19 #include <vnet/feature/feature.h>
20 #include <vnet/qos/qos_types.h>
21
22 /**
23  * Per-interface, per-protocol vector of feature on/off configurations
24  */
25 static u8 *qos_record_configs[QOS_N_SOURCES];
26 static u32 l2_qos_input_next[QOS_N_SOURCES][32];
27
28 static void
29 qos_record_feature_config (u32 sw_if_index,
30                            qos_source_t input_source, u8 enable)
31 {
32   switch (input_source)
33     {
34     case QOS_SOURCE_IP:
35       vnet_feature_enable_disable ("ip6-unicast", "ip6-qos-record",
36                                    sw_if_index, enable, NULL, 0);
37       vnet_feature_enable_disable ("ip6-multicast", "ip6-qos-record",
38                                    sw_if_index, enable, NULL, 0);
39       vnet_feature_enable_disable ("ip4-unicast", "ip4-qos-record",
40                                    sw_if_index, enable, NULL, 0);
41       vnet_feature_enable_disable ("ip4-multicast", "ip4-qos-record",
42                                    sw_if_index, enable, NULL, 0);
43       l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_L2_IP_QOS_RECORD,
44                                   enable);
45       break;
46     case QOS_SOURCE_MPLS:
47     case QOS_SOURCE_VLAN:
48     case QOS_SOURCE_EXT:
49       // not implemented yet
50       break;
51     }
52 }
53
54 int
55 qos_record_enable (u32 sw_if_index, qos_source_t input_source)
56 {
57   vec_validate (qos_record_configs[input_source], sw_if_index);
58
59   if (0 == qos_record_configs[input_source][sw_if_index])
60     {
61       qos_record_feature_config (sw_if_index, input_source, 1);
62     }
63
64   qos_record_configs[input_source][sw_if_index]++;
65   return (0);
66 }
67
68 int
69 qos_record_disable (u32 sw_if_index, qos_source_t input_source)
70 {
71   if (vec_len (qos_record_configs[input_source]) <= sw_if_index)
72     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
73
74   if (0 == qos_record_configs[input_source][sw_if_index])
75     return VNET_API_ERROR_VALUE_EXIST;
76
77   qos_record_configs[input_source][sw_if_index]--;
78
79   if (0 == qos_record_configs[input_source][sw_if_index])
80     {
81       qos_record_feature_config (sw_if_index, input_source, 0);
82     }
83
84   return (0);
85 }
86
87 /*
88  * Disable recording feautre for all protocols when the interface
89  * is deleted
90  */
91 static clib_error_t *
92 qos_record_ip_interface_add_del (vnet_main_t * vnm,
93                                  u32 sw_if_index, u32 is_add)
94 {
95   if (!is_add)
96     {
97       qos_source_t qs;
98
99       FOR_EACH_QOS_SOURCE (qs)
100       {
101         while (qos_record_disable (sw_if_index, qs) == 0);
102       }
103     }
104
105   return (NULL);
106 }
107
108 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_record_ip_interface_add_del);
109
110 /**
111  * per-packet trace data
112  */
113 typedef struct qos_record_trace_t_
114 {
115   /* per-pkt trace data */
116   qos_bits_t bits;
117 } qos_record_trace_t;
118
119 static inline uword
120 qos_record_inline (vlib_main_t * vm,
121                    vlib_node_runtime_t * node,
122                    vlib_frame_t * frame, int is_ip6, int is_l2)
123 {
124   u32 n_left_from, *from, *to_next, next_index;
125
126   next_index = 0;
127   n_left_from = frame->n_vectors;
128   from = vlib_frame_vector_args (frame);
129
130   while (n_left_from > 0)
131     {
132       u32 n_left_to_next;
133
134       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
135
136       while (n_left_from > 0 && n_left_to_next > 0)
137         {
138           ip4_header_t *ip4_0;
139           ip6_header_t *ip6_0;
140           vlib_buffer_t *b0;
141           u32 next0, bi0;
142           qos_bits_t qos0;
143           u8 l2_len;
144
145           next0 = 0;
146           bi0 = from[0];
147           to_next[0] = bi0;
148           from += 1;
149           to_next += 1;
150           n_left_from -= 1;
151           n_left_to_next -= 1;
152
153           b0 = vlib_get_buffer (vm, bi0);
154
155           if (is_l2)
156             {
157               l2_len = vnet_buffer (b0)->l2.l2_len;
158               u8 *l3h;
159               u16 ethertype;
160
161               vlib_buffer_advance (b0, l2_len);
162
163               l3h = vlib_buffer_get_current (b0);
164               ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));
165
166               if (ethertype == ETHERNET_TYPE_IP4)
167                 is_ip6 = 0;
168               else if (ethertype == ETHERNET_TYPE_IP6)
169                 is_ip6 = 1;
170               else
171                 goto non_ip;
172             }
173
174           if (is_ip6)
175             {
176               ip6_0 = vlib_buffer_get_current (b0);
177               qos0 = ip6_traffic_class_network_order (ip6_0);
178             }
179           else
180             {
181               ip4_0 = vlib_buffer_get_current (b0);
182               qos0 = ip4_0->tos;
183             }
184           vnet_buffer2 (b0)->qos.bits = qos0;
185           vnet_buffer2 (b0)->qos.source = QOS_SOURCE_IP;
186           b0->flags |= VNET_BUFFER_F_QOS_DATA_VALID;
187
188           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
189                              (b0->flags & VLIB_BUFFER_IS_TRACED)))
190             {
191               qos_record_trace_t *t =
192                 vlib_add_trace (vm, node, b0, sizeof (*t));
193               t->bits = qos0;
194             }
195
196         non_ip:
197           if (is_l2)
198             {
199               vlib_buffer_advance (b0, -l2_len);
200               next0 = vnet_l2_feature_next (b0,
201                                             l2_qos_input_next[QOS_SOURCE_IP],
202                                             L2INPUT_FEAT_L2_IP_QOS_RECORD);
203             }
204           else
205             vnet_feature_next (&next0, b0);
206
207           /* verify speculative enqueue, maybe switch current next frame */
208           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
209                                            to_next, n_left_to_next,
210                                            bi0, next0);
211         }
212
213       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
214     }
215
216   return frame->n_vectors;
217 }
218
219 /* packet trace format function */
220 static u8 *
221 format_qos_record_trace (u8 * s, va_list * args)
222 {
223   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
224   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
225   qos_record_trace_t *t = va_arg (*args, qos_record_trace_t *);
226
227   s = format (s, "qos:%d", t->bits);
228
229   return s;
230 }
231
232 static inline uword
233 ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
234                 vlib_frame_t * frame)
235 {
236   return (qos_record_inline (vm, node, frame, 0, 0));
237 }
238
239 static inline uword
240 ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
241                 vlib_frame_t * frame)
242 {
243   return (qos_record_inline (vm, node, frame, 1, 0));
244 }
245
246 static inline uword
247 l2_ip_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
248                   vlib_frame_t * frame)
249 {
250   return (qos_record_inline (vm, node, frame, 0, 1));
251 }
252
253 /* *INDENT-OFF* */
254 VLIB_REGISTER_NODE (ip4_qos_record_node) = {
255   .function = ip4_qos_record,
256   .name = "ip4-qos-record",
257   .vector_size = sizeof (u32),
258   .format_trace = format_qos_record_trace,
259   .type = VLIB_NODE_TYPE_INTERNAL,
260
261   .n_errors = 0,
262   .n_next_nodes = 1,
263
264   .next_nodes = {
265     [0] = "ip4-drop",
266   },
267 };
268
269 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_record_node, ip4_qos_record);
270
271 VNET_FEATURE_INIT (ip4_qos_record_node, static) = {
272     .arc_name = "ip4-unicast",
273     .node_name = "ip4-qos-record",
274 };
275
276 VLIB_REGISTER_NODE (ip6_qos_record_node) = {
277   .function = ip6_qos_record,
278   .name = "ip6-qos-record",
279   .vector_size = sizeof (u32),
280   .format_trace = format_qos_record_trace,
281   .type = VLIB_NODE_TYPE_INTERNAL,
282
283   .n_errors = 0,
284   .n_next_nodes = 1,
285
286   .next_nodes = {
287     [0] = "ip6-drop",
288   },
289 };
290
291 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_record_node, ip6_qos_record);
292
293 VNET_FEATURE_INIT (ip6_qos_record_node, static) = {
294     .arc_name = "ip6-unicast",
295     .node_name = "ip6-qos-record",
296 };
297
298 VLIB_REGISTER_NODE (l2_ip_qos_record_node, static) = {
299   .function = l2_ip_qos_record,
300   .name = "l2-ip-qos-record",
301   .vector_size = sizeof (u32),
302   .format_trace = format_qos_record_trace,
303   .type = VLIB_NODE_TYPE_INTERNAL,
304
305   .n_errors = 0,
306   .n_next_nodes = 1,
307
308   /* Consider adding error "no IP after L2, no recording" */
309   .next_nodes = {
310     [0] = "error-drop",
311   },
312 };
313
314 VLIB_NODE_FUNCTION_MULTIARCH (l2_ip_qos_record_node, l2_ip_qos_record);
315 /* *INDENT-ON* */
316
317 clib_error_t *
318 l2_ip_qos_init (vlib_main_t * vm)
319 {
320   /* Initialize the feature next-node indexes */
321   feat_bitmap_init_next_nodes (vm,
322                                l2_ip_qos_record_node.index,
323                                L2INPUT_N_FEAT,
324                                l2input_get_feat_names (),
325                                l2_qos_input_next[QOS_SOURCE_IP]);
326   return 0;
327 }
328
329 VLIB_INIT_FUNCTION (l2_ip_qos_init);
330
331 static clib_error_t *
332 qos_record_cli (vlib_main_t * vm,
333                 unformat_input_t * input, vlib_cli_command_t * cmd)
334 {
335   vnet_main_t *vnm = vnet_get_main ();
336   u32 sw_if_index, qs;
337   u8 enable;
338
339   qs = 0xff;
340   enable = 1;
341   sw_if_index = ~0;
342
343   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
344     {
345       if (unformat (input, "%U", unformat_vnet_sw_interface,
346                     vnm, &sw_if_index))
347         ;
348       else if (unformat (input, "%U", unformat_qos_source, &qs))
349         ;
350       else if (unformat (input, "enable"))
351         enable = 1;
352       else if (unformat (input, "disable"))
353         enable = 0;
354       else
355         break;
356     }
357
358   if (~0 == sw_if_index)
359     return clib_error_return (0, "interface must be specified");
360   if (0xff == qs)
361     return clib_error_return (0, "input location must be specified");
362
363   if (enable)
364     qos_record_enable (sw_if_index, qs);
365   else
366     qos_record_disable (sw_if_index, qs);
367
368   return (NULL);
369 }
370
371 /*?
372  * Enable QoS bit recording on an interface using the packet's input DSCP bits
373  * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
374  * one protocol is chosen (which is foolish) the higer layers override the
375  * lower.
376  *
377  * @cliexpar
378  * @cliexcmd{qos record ip GigEthernet0/1/0}
379  ?*/
380 /* *INDENT-OFF* */
381 VLIB_CLI_COMMAND (qos_record_command, static) = {
382   .path = "qos record",
383   .short_help = "qos record <record-source> <INTERFACE> [disable]",
384   .function = qos_record_cli,
385   .is_mp_safe = 1,
386 };
387 /* *INDENT-ON* */
388
389
390 /*
391  * fd.io coding-style-patch-verification: ON
392  *
393  * Local Variables:
394  * eval: (c-set-style "gnu")
395  * End:
396  */