1433a02527b42eccbbafcaec2f10a9091cdfd9f9
[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       vnet_feature_enable_disable ("mpls-input", "mpls-qos-record",
48                                    sw_if_index, enable, NULL, 0);
49       break;
50     case QOS_SOURCE_VLAN:
51       vnet_feature_enable_disable ("ip6-unicast", "vlan-ip6-qos-record",
52                                    sw_if_index, enable, NULL, 0);
53       vnet_feature_enable_disable ("ip6-multicast", "vlan-ip6-qos-record",
54                                    sw_if_index, enable, NULL, 0);
55       vnet_feature_enable_disable ("ip4-unicast", "vlan-ip4-qos-record",
56                                    sw_if_index, enable, NULL, 0);
57       vnet_feature_enable_disable ("ip4-multicast", "vlan-ip4-qos-record",
58                                    sw_if_index, enable, NULL, 0);
59       vnet_feature_enable_disable ("mpls-input", "vlan-mpls-qos-record",
60                                    sw_if_index, enable, NULL, 0);
61       break;
62     case QOS_SOURCE_EXT:
63       /* not a valid option for recording */
64       break;
65     }
66 }
67
68 int
69 qos_record_enable (u32 sw_if_index, qos_source_t input_source)
70 {
71   vec_validate (qos_record_configs[input_source], sw_if_index);
72
73   if (0 == qos_record_configs[input_source][sw_if_index])
74     {
75       qos_record_feature_config (sw_if_index, input_source, 1);
76     }
77
78   qos_record_configs[input_source][sw_if_index]++;
79   return (0);
80 }
81
82 int
83 qos_record_disable (u32 sw_if_index, qos_source_t input_source)
84 {
85   if (vec_len (qos_record_configs[input_source]) <= sw_if_index)
86     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
87
88   if (0 == qos_record_configs[input_source][sw_if_index])
89     return VNET_API_ERROR_VALUE_EXIST;
90
91   qos_record_configs[input_source][sw_if_index]--;
92
93   if (0 == qos_record_configs[input_source][sw_if_index])
94     {
95       qos_record_feature_config (sw_if_index, input_source, 0);
96     }
97
98   return (0);
99 }
100
101 /*
102  * Disable recording feautre for all protocols when the interface
103  * is deleted
104  */
105 static clib_error_t *
106 qos_record_ip_interface_add_del (vnet_main_t * vnm,
107                                  u32 sw_if_index, u32 is_add)
108 {
109   if (!is_add)
110     {
111       qos_source_t qs;
112
113       FOR_EACH_QOS_SOURCE (qs)
114       {
115         while (qos_record_disable (sw_if_index, qs) == 0);
116       }
117     }
118
119   return (NULL);
120 }
121
122 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_record_ip_interface_add_del);
123
124 /**
125  * per-packet trace data
126  */
127 typedef struct qos_record_trace_t_
128 {
129   /* per-pkt trace data */
130   qos_bits_t bits;
131 } qos_record_trace_t;
132
133 static inline uword
134 qos_record_inline (vlib_main_t * vm,
135                    vlib_node_runtime_t * node,
136                    vlib_frame_t * frame, dpo_proto_t dproto, int is_l2)
137 {
138   u32 n_left_from, *from, *to_next, next_index;
139
140   next_index = 0;
141   n_left_from = frame->n_vectors;
142   from = vlib_frame_vector_args (frame);
143
144   while (n_left_from > 0)
145     {
146       u32 n_left_to_next;
147
148       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
149
150       while (n_left_from > 0 && n_left_to_next > 0)
151         {
152           ip4_header_t *ip4_0;
153           ip6_header_t *ip6_0;
154           vlib_buffer_t *b0;
155           u32 next0, bi0;
156           qos_bits_t qos0;
157           u8 l2_len;
158
159           next0 = 0;
160           bi0 = from[0];
161           to_next[0] = bi0;
162           from += 1;
163           to_next += 1;
164           n_left_from -= 1;
165           n_left_to_next -= 1;
166
167           b0 = vlib_get_buffer (vm, bi0);
168
169           if (is_l2)
170             {
171               l2_len = vnet_buffer (b0)->l2.l2_len;
172               u8 *l3h;
173               u16 ethertype;
174
175               vlib_buffer_advance (b0, l2_len);
176
177               l3h = vlib_buffer_get_current (b0);
178               ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));
179
180               if (ethertype == ETHERNET_TYPE_IP4)
181                 dproto = DPO_PROTO_IP4;
182               else if (ethertype == ETHERNET_TYPE_IP6)
183                 dproto = DPO_PROTO_IP6;
184               else if (ethertype == ETHERNET_TYPE_MPLS)
185                 dproto = DPO_PROTO_MPLS;
186               else
187                 goto non_ip;
188             }
189
190           if (DPO_PROTO_IP6 == dproto)
191             {
192               ip6_0 = vlib_buffer_get_current (b0);
193               qos0 = ip6_traffic_class_network_order (ip6_0);
194             }
195           else if (DPO_PROTO_IP4 == dproto)
196             {
197               ip4_0 = vlib_buffer_get_current (b0);
198               qos0 = ip4_0->tos;
199             }
200           else if (DPO_PROTO_ETHERNET == dproto)
201             {
202               ethernet_vlan_header_t *vlan0;
203
204               vlan0 = (vlib_buffer_get_current (b0) -
205                        sizeof (ethernet_vlan_header_t));
206
207               qos0 = ethernet_vlan_header_get_priority_net_order (vlan0);
208             }
209           else if (DPO_PROTO_MPLS)
210             {
211               mpls_unicast_header_t *mh;
212
213               mh = vlib_buffer_get_current (b0);
214               qos0 = vnet_mpls_uc_get_exp (mh->label_exp_s_ttl);
215             }
216
217           vnet_buffer2 (b0)->qos.bits = qos0;
218           vnet_buffer2 (b0)->qos.source = QOS_SOURCE_IP;
219           b0->flags |= VNET_BUFFER_F_QOS_DATA_VALID;
220
221           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
222                              (b0->flags & VLIB_BUFFER_IS_TRACED)))
223             {
224               qos_record_trace_t *t =
225                 vlib_add_trace (vm, node, b0, sizeof (*t));
226               t->bits = qos0;
227             }
228
229         non_ip:
230           if (is_l2)
231             {
232               vlib_buffer_advance (b0, -l2_len);
233               next0 = vnet_l2_feature_next (b0,
234                                             l2_qos_input_next[QOS_SOURCE_IP],
235                                             L2INPUT_FEAT_L2_IP_QOS_RECORD);
236             }
237           else
238             vnet_feature_next (&next0, b0);
239
240           /* verify speculative enqueue, maybe switch current next frame */
241           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
242                                            to_next, n_left_to_next,
243                                            bi0, next0);
244         }
245
246       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
247     }
248
249   return frame->n_vectors;
250 }
251
252 /* packet trace format function */
253 static u8 *
254 format_qos_record_trace (u8 * s, va_list * args)
255 {
256   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
257   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
258   qos_record_trace_t *t = va_arg (*args, qos_record_trace_t *);
259
260   s = format (s, "qos:%d", t->bits);
261
262   return s;
263 }
264
265 static inline uword
266 ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
267                 vlib_frame_t * frame)
268 {
269   return (qos_record_inline (vm, node, frame, DPO_PROTO_IP4, 0));
270 }
271
272 static inline uword
273 ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
274                 vlib_frame_t * frame)
275 {
276   return (qos_record_inline (vm, node, frame, DPO_PROTO_IP6, 0));
277 }
278
279 static inline uword
280 mpls_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
281                  vlib_frame_t * frame)
282 {
283   return (qos_record_inline (vm, node, frame, DPO_PROTO_MPLS, 0));
284 }
285
286 static inline uword
287 vlan_ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
288                      vlib_frame_t * frame)
289 {
290   return (qos_record_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
291 }
292
293 static inline uword
294 vlan_ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
295                      vlib_frame_t * frame)
296 {
297   return (qos_record_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
298 }
299
300 static inline uword
301 vlan_mpls_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
302                       vlib_frame_t * frame)
303 {
304   return (qos_record_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
305 }
306
307 static inline uword
308 l2_ip_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
309                   vlib_frame_t * frame)
310 {
311   return (qos_record_inline (vm, node, frame, 0, 1));
312 }
313
314 /* *INDENT-OFF* */
315 VLIB_REGISTER_NODE (ip4_qos_record_node) = {
316   .function = ip4_qos_record,
317   .name = "ip4-qos-record",
318   .vector_size = sizeof (u32),
319   .format_trace = format_qos_record_trace,
320   .type = VLIB_NODE_TYPE_INTERNAL,
321
322   .n_errors = 0,
323   .n_next_nodes = 1,
324
325   .next_nodes = {
326     [0] = "ip4-drop",
327   },
328 };
329
330 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_record_node, ip4_qos_record);
331
332 VNET_FEATURE_INIT (ip4_qos_record_node, static) = {
333     .arc_name = "ip4-unicast",
334     .node_name = "ip4-qos-record",
335 };
336 VNET_FEATURE_INIT (ip4m_qos_record_node, static) = {
337     .arc_name = "ip4-multicast",
338     .node_name = "ip4-qos-record",
339 };
340
341 VLIB_REGISTER_NODE (ip6_qos_record_node) = {
342   .function = ip6_qos_record,
343   .name = "ip6-qos-record",
344   .vector_size = sizeof (u32),
345   .format_trace = format_qos_record_trace,
346   .type = VLIB_NODE_TYPE_INTERNAL,
347
348   .n_errors = 0,
349   .n_next_nodes = 1,
350
351   .next_nodes = {
352     [0] = "ip6-drop",
353   },
354 };
355
356 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_record_node, ip6_qos_record);
357
358 VNET_FEATURE_INIT (ip6_qos_record_node, static) = {
359     .arc_name = "ip6-unicast",
360     .node_name = "ip6-qos-record",
361 };
362 VNET_FEATURE_INIT (ip6m_qos_record_node, static) = {
363     .arc_name = "ip6-multicast",
364     .node_name = "ip6-qos-record",
365 };
366
367 VLIB_REGISTER_NODE (mpls_qos_record_node) = {
368   .function = mpls_qos_record,
369   .name = "mpls-qos-record",
370   .vector_size = sizeof (u32),
371   .format_trace = format_qos_record_trace,
372   .type = VLIB_NODE_TYPE_INTERNAL,
373
374   .n_errors = 0,
375   .n_next_nodes = 1,
376
377   .next_nodes = {
378     [0] = "mpls-drop",
379   },
380 };
381
382 VLIB_NODE_FUNCTION_MULTIARCH (mpls_qos_record_node, mpls_qos_record);
383
384 VNET_FEATURE_INIT (mpls_qos_record_node, static) = {
385     .arc_name = "mpls-input",
386     .node_name = "mpls-qos-record",
387 };
388
389 VLIB_REGISTER_NODE (vlan_mpls_qos_record_node) = {
390   .function = vlan_mpls_qos_record,
391   .name = "vlan-mpls-qos-record",
392   .vector_size = sizeof (u32),
393   .format_trace = format_qos_record_trace,
394   .type = VLIB_NODE_TYPE_INTERNAL,
395
396   .n_errors = 0,
397   .n_next_nodes = 1,
398
399   .next_nodes = {
400     [0] = "mpls-drop",
401   },
402 };
403
404 VLIB_NODE_FUNCTION_MULTIARCH (vlan_mpls_qos_record_node, vlan_mpls_qos_record);
405
406 VNET_FEATURE_INIT (vlan_mpls_qos_record_node, static) = {
407     .arc_name = "mpls-input",
408     .node_name = "vlan-mpls-qos-record",
409     .runs_before = VNET_FEATURES ("mpls-qos-record"),
410 };
411
412 VLIB_REGISTER_NODE (vlan_ip4_qos_record_node) = {
413   .function = vlan_ip4_qos_record,
414   .name = "vlan-ip4-qos-record",
415   .vector_size = sizeof (u32),
416   .format_trace = format_qos_record_trace,
417   .type = VLIB_NODE_TYPE_INTERNAL,
418
419   .n_errors = 0,
420   .n_next_nodes = 1,
421
422   .next_nodes = {
423     [0] = "ip4-drop",
424   },
425 };
426
427 VLIB_NODE_FUNCTION_MULTIARCH (vlan_ip4_qos_record_node, vlan_ip4_qos_record);
428
429 VNET_FEATURE_INIT (vlan_ip4_qos_record_node, static) = {
430     .arc_name = "ip4-unicast",
431     .node_name = "vlan-ip4-qos-record",
432     .runs_before = VNET_FEATURES ("ip4-qos-record"),
433 };
434 VNET_FEATURE_INIT (vlan_ip4m_qos_record_node, static) = {
435     .arc_name = "ip4-multicast",
436     .node_name = "vlan-ip4-qos-record",
437     .runs_before = VNET_FEATURES ("ip4-qos-record"),
438 };
439
440 VLIB_REGISTER_NODE (vlan_ip6_qos_record_node) = {
441   .function = vlan_ip6_qos_record,
442   .name = "vlan-ip6-qos-record",
443   .vector_size = sizeof (u32),
444   .format_trace = format_qos_record_trace,
445   .type = VLIB_NODE_TYPE_INTERNAL,
446
447   .n_errors = 0,
448   .n_next_nodes = 1,
449
450   .next_nodes = {
451     [0] = "ip6-drop",
452   },
453 };
454
455 VLIB_NODE_FUNCTION_MULTIARCH (vlan_ip6_qos_record_node, vlan_ip6_qos_record);
456
457 VNET_FEATURE_INIT (vlan_ip6_qos_record_node, static) = {
458     .arc_name = "ip6-unicast",
459     .node_name = "vlan-ip6-qos-record",
460     .runs_before = VNET_FEATURES ("ip6-qos-record"),
461 };
462 VNET_FEATURE_INIT (vlan_ip6m_qos_record_node, static) = {
463     .arc_name = "ip6-multicast",
464     .node_name = "vlan-ip6-qos-record",
465     .runs_before = VNET_FEATURES ("ip6-qos-record"),
466 };
467
468 VLIB_REGISTER_NODE (l2_ip_qos_record_node, static) = {
469   .function = l2_ip_qos_record,
470   .name = "l2-ip-qos-record",
471   .vector_size = sizeof (u32),
472   .format_trace = format_qos_record_trace,
473   .type = VLIB_NODE_TYPE_INTERNAL,
474
475   .n_errors = 0,
476   .n_next_nodes = 1,
477
478   /* Consider adding error "no IP after L2, no recording" */
479   .next_nodes = {
480     [0] = "error-drop",
481   },
482 };
483
484 VLIB_NODE_FUNCTION_MULTIARCH (l2_ip_qos_record_node, l2_ip_qos_record);
485
486 /* *INDENT-ON* */
487
488 clib_error_t *
489 l2_ip_qos_init (vlib_main_t * vm)
490 {
491   /* Initialize the feature next-node indexes */
492   feat_bitmap_init_next_nodes (vm,
493                                l2_ip_qos_record_node.index,
494                                L2INPUT_N_FEAT,
495                                l2input_get_feat_names (),
496                                l2_qos_input_next[QOS_SOURCE_IP]);
497   return 0;
498 }
499
500 VLIB_INIT_FUNCTION (l2_ip_qos_init);
501
502 static clib_error_t *
503 qos_record_cli (vlib_main_t * vm,
504                 unformat_input_t * input, vlib_cli_command_t * cmd)
505 {
506   vnet_main_t *vnm = vnet_get_main ();
507   u32 sw_if_index, qs;
508   u8 enable;
509
510   qs = 0xff;
511   enable = 1;
512   sw_if_index = ~0;
513
514   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
515     {
516       if (unformat (input, "%U", unformat_vnet_sw_interface,
517                     vnm, &sw_if_index))
518         ;
519       else if (unformat (input, "%U", unformat_qos_source, &qs))
520         ;
521       else if (unformat (input, "enable"))
522         enable = 1;
523       else if (unformat (input, "disable"))
524         enable = 0;
525       else
526         break;
527     }
528
529   if (~0 == sw_if_index)
530     return clib_error_return (0, "interface must be specified");
531   if (0xff == qs)
532     return clib_error_return (0, "input location must be specified");
533
534   if (enable)
535     qos_record_enable (sw_if_index, qs);
536   else
537     qos_record_disable (sw_if_index, qs);
538
539   return (NULL);
540 }
541
542 /*?
543  * Enable QoS bit recording on an interface using the packet's input DSCP bits
544  * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
545  * one protocol is chosen (which is foolish) the higer layers override the
546  * lower.
547  *
548  * @cliexpar
549  * @cliexcmd{qos record ip GigEthernet0/1/0}
550  ?*/
551 /* *INDENT-OFF* */
552 VLIB_CLI_COMMAND (qos_record_command, static) = {
553   .path = "qos record",
554   .short_help = "qos record <record-source> <INTERFACE> [disable]",
555   .function = qos_record_cli,
556   .is_mp_safe = 1,
557 };
558 /* *INDENT-ON* */
559
560
561 /*
562  * fd.io coding-style-patch-verification: ON
563  *
564  * Local Variables:
565  * eval: (c-set-style "gnu")
566  * End:
567  */