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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 #include <vnet/l2/l2_input.h>
22 #include <vnet/l2/feat_bitmap.h>
25 * Per-interface, per-protocol vector of feature on/off configurations
27 static u8 *qos_record_configs[QOS_N_SOURCES];
28 static u32 l2_qos_input_next[QOS_N_SOURCES][32];
31 qos_record_feature_config (u32 sw_if_index,
32 qos_source_t input_source, u8 enable)
37 vnet_feature_enable_disable ("ip6-unicast", "ip6-qos-record",
38 sw_if_index, enable, NULL, 0);
39 vnet_feature_enable_disable ("ip6-multicast", "ip6-qos-record",
40 sw_if_index, enable, NULL, 0);
41 vnet_feature_enable_disable ("ip4-unicast", "ip4-qos-record",
42 sw_if_index, enable, NULL, 0);
43 vnet_feature_enable_disable ("ip4-multicast", "ip4-qos-record",
44 sw_if_index, enable, NULL, 0);
45 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_L2_IP_QOS_RECORD,
49 vnet_feature_enable_disable ("mpls-input", "mpls-qos-record",
50 sw_if_index, enable, NULL, 0);
53 vnet_feature_enable_disable ("ip6-unicast", "vlan-ip6-qos-record",
54 sw_if_index, enable, NULL, 0);
55 vnet_feature_enable_disable ("ip6-multicast", "vlan-ip6-qos-record",
56 sw_if_index, enable, NULL, 0);
57 vnet_feature_enable_disable ("ip4-unicast", "vlan-ip4-qos-record",
58 sw_if_index, enable, NULL, 0);
59 vnet_feature_enable_disable ("ip4-multicast", "vlan-ip4-qos-record",
60 sw_if_index, enable, NULL, 0);
61 vnet_feature_enable_disable ("mpls-input", "vlan-mpls-qos-record",
62 sw_if_index, enable, NULL, 0);
65 /* not a valid option for recording */
71 qos_record_enable (u32 sw_if_index, qos_source_t input_source)
73 vec_validate (qos_record_configs[input_source], sw_if_index);
75 if (0 == qos_record_configs[input_source][sw_if_index])
77 qos_record_feature_config (sw_if_index, input_source, 1);
80 qos_record_configs[input_source][sw_if_index]++;
85 qos_record_disable (u32 sw_if_index, qos_source_t input_source)
87 if (vec_len (qos_record_configs[input_source]) <= sw_if_index)
88 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
90 if (0 == qos_record_configs[input_source][sw_if_index])
91 return VNET_API_ERROR_VALUE_EXIST;
93 qos_record_configs[input_source][sw_if_index]--;
95 if (0 == qos_record_configs[input_source][sw_if_index])
97 qos_record_feature_config (sw_if_index, input_source, 0);
104 * Disable recording feature for all protocols when the interface
107 static clib_error_t *
108 qos_record_ip_interface_add_del (vnet_main_t * vnm,
109 u32 sw_if_index, u32 is_add)
115 FOR_EACH_QOS_SOURCE (qs)
117 while (qos_record_disable (sw_if_index, qs) == 0);
124 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_record_ip_interface_add_del);
127 * per-packet trace data
129 typedef struct qos_record_trace_t_
131 /* per-pkt trace data */
133 } qos_record_trace_t;
136 qos_record_inline (vlib_main_t * vm,
137 vlib_node_runtime_t * node,
138 vlib_frame_t * frame, dpo_proto_t dproto, int is_l2)
140 u32 n_left_from, *from, *to_next, next_index;
143 n_left_from = frame->n_vectors;
144 from = vlib_frame_vector_args (frame);
146 while (n_left_from > 0)
150 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
152 while (n_left_from > 0 && n_left_to_next > 0)
169 b0 = vlib_get_buffer (vm, bi0);
173 l2_len = vnet_buffer (b0)->l2.l2_len;
177 vlib_buffer_advance (b0, l2_len);
179 l3h = vlib_buffer_get_current (b0);
180 ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));
182 if (ethertype == ETHERNET_TYPE_IP4)
183 dproto = DPO_PROTO_IP4;
184 else if (ethertype == ETHERNET_TYPE_IP6)
185 dproto = DPO_PROTO_IP6;
186 else if (ethertype == ETHERNET_TYPE_MPLS)
187 dproto = DPO_PROTO_MPLS;
192 if (DPO_PROTO_IP6 == dproto)
194 ip6_0 = vlib_buffer_get_current (b0);
195 qos0 = ip6_traffic_class_network_order (ip6_0);
197 else if (DPO_PROTO_IP4 == dproto)
199 ip4_0 = vlib_buffer_get_current (b0);
202 else if (DPO_PROTO_ETHERNET == dproto)
204 ethernet_vlan_header_t *vlan0;
206 vlan0 = (vlib_buffer_get_current (b0) -
207 sizeof (ethernet_vlan_header_t));
209 qos0 = ethernet_vlan_header_get_priority_net_order (vlan0);
211 else if (DPO_PROTO_MPLS)
213 mpls_unicast_header_t *mh;
215 mh = vlib_buffer_get_current (b0);
216 qos0 = vnet_mpls_uc_get_exp (mh->label_exp_s_ttl);
219 vnet_buffer2 (b0)->qos.bits = qos0;
220 vnet_buffer2 (b0)->qos.source = QOS_SOURCE_IP;
221 b0->flags |= VNET_BUFFER_F_QOS_DATA_VALID;
223 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
224 (b0->flags & VLIB_BUFFER_IS_TRACED)))
226 qos_record_trace_t *t =
227 vlib_add_trace (vm, node, b0, sizeof (*t));
234 vlib_buffer_advance (b0, -l2_len);
235 next0 = vnet_l2_feature_next (b0,
236 l2_qos_input_next[QOS_SOURCE_IP],
237 L2INPUT_FEAT_L2_IP_QOS_RECORD);
240 vnet_feature_next (&next0, b0);
242 /* verify speculative enqueue, maybe switch current next frame */
243 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
244 to_next, n_left_to_next,
248 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
251 return frame->n_vectors;
254 /* packet trace format function */
256 format_qos_record_trace (u8 * s, va_list * args)
258 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
259 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
260 qos_record_trace_t *t = va_arg (*args, qos_record_trace_t *);
262 s = format (s, "qos:%d", t->bits);
268 ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
269 vlib_frame_t * frame)
271 return (qos_record_inline (vm, node, frame, DPO_PROTO_IP4, 0));
275 ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
276 vlib_frame_t * frame)
278 return (qos_record_inline (vm, node, frame, DPO_PROTO_IP6, 0));
282 mpls_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
283 vlib_frame_t * frame)
285 return (qos_record_inline (vm, node, frame, DPO_PROTO_MPLS, 0));
289 vlan_ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
290 vlib_frame_t * frame)
292 return (qos_record_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
296 vlan_ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
297 vlib_frame_t * frame)
299 return (qos_record_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
303 vlan_mpls_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
304 vlib_frame_t * frame)
306 return (qos_record_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
310 l2_ip_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
311 vlib_frame_t * frame)
313 return (qos_record_inline (vm, node, frame, 0, 1));
317 VLIB_REGISTER_NODE (ip4_qos_record_node) = {
318 .function = ip4_qos_record,
319 .name = "ip4-qos-record",
320 .vector_size = sizeof (u32),
321 .format_trace = format_qos_record_trace,
322 .type = VLIB_NODE_TYPE_INTERNAL,
332 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_record_node, ip4_qos_record);
334 VNET_FEATURE_INIT (ip4_qos_record_node, static) = {
335 .arc_name = "ip4-unicast",
336 .node_name = "ip4-qos-record",
338 VNET_FEATURE_INIT (ip4m_qos_record_node, static) = {
339 .arc_name = "ip4-multicast",
340 .node_name = "ip4-qos-record",
343 VLIB_REGISTER_NODE (ip6_qos_record_node) = {
344 .function = ip6_qos_record,
345 .name = "ip6-qos-record",
346 .vector_size = sizeof (u32),
347 .format_trace = format_qos_record_trace,
348 .type = VLIB_NODE_TYPE_INTERNAL,
358 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_record_node, ip6_qos_record);
360 VNET_FEATURE_INIT (ip6_qos_record_node, static) = {
361 .arc_name = "ip6-unicast",
362 .node_name = "ip6-qos-record",
364 VNET_FEATURE_INIT (ip6m_qos_record_node, static) = {
365 .arc_name = "ip6-multicast",
366 .node_name = "ip6-qos-record",
369 VLIB_REGISTER_NODE (mpls_qos_record_node) = {
370 .function = mpls_qos_record,
371 .name = "mpls-qos-record",
372 .vector_size = sizeof (u32),
373 .format_trace = format_qos_record_trace,
374 .type = VLIB_NODE_TYPE_INTERNAL,
384 VLIB_NODE_FUNCTION_MULTIARCH (mpls_qos_record_node, mpls_qos_record);
386 VNET_FEATURE_INIT (mpls_qos_record_node, static) = {
387 .arc_name = "mpls-input",
388 .node_name = "mpls-qos-record",
391 VLIB_REGISTER_NODE (vlan_mpls_qos_record_node) = {
392 .function = vlan_mpls_qos_record,
393 .name = "vlan-mpls-qos-record",
394 .vector_size = sizeof (u32),
395 .format_trace = format_qos_record_trace,
396 .type = VLIB_NODE_TYPE_INTERNAL,
406 VLIB_NODE_FUNCTION_MULTIARCH (vlan_mpls_qos_record_node, vlan_mpls_qos_record);
408 VNET_FEATURE_INIT (vlan_mpls_qos_record_node, static) = {
409 .arc_name = "mpls-input",
410 .node_name = "vlan-mpls-qos-record",
411 .runs_before = VNET_FEATURES ("mpls-qos-record"),
414 VLIB_REGISTER_NODE (vlan_ip4_qos_record_node) = {
415 .function = vlan_ip4_qos_record,
416 .name = "vlan-ip4-qos-record",
417 .vector_size = sizeof (u32),
418 .format_trace = format_qos_record_trace,
419 .type = VLIB_NODE_TYPE_INTERNAL,
429 VLIB_NODE_FUNCTION_MULTIARCH (vlan_ip4_qos_record_node, vlan_ip4_qos_record);
431 VNET_FEATURE_INIT (vlan_ip4_qos_record_node, static) = {
432 .arc_name = "ip4-unicast",
433 .node_name = "vlan-ip4-qos-record",
434 .runs_before = VNET_FEATURES ("ip4-qos-record"),
436 VNET_FEATURE_INIT (vlan_ip4m_qos_record_node, static) = {
437 .arc_name = "ip4-multicast",
438 .node_name = "vlan-ip4-qos-record",
439 .runs_before = VNET_FEATURES ("ip4-qos-record"),
442 VLIB_REGISTER_NODE (vlan_ip6_qos_record_node) = {
443 .function = vlan_ip6_qos_record,
444 .name = "vlan-ip6-qos-record",
445 .vector_size = sizeof (u32),
446 .format_trace = format_qos_record_trace,
447 .type = VLIB_NODE_TYPE_INTERNAL,
457 VLIB_NODE_FUNCTION_MULTIARCH (vlan_ip6_qos_record_node, vlan_ip6_qos_record);
459 VNET_FEATURE_INIT (vlan_ip6_qos_record_node, static) = {
460 .arc_name = "ip6-unicast",
461 .node_name = "vlan-ip6-qos-record",
462 .runs_before = VNET_FEATURES ("ip6-qos-record"),
464 VNET_FEATURE_INIT (vlan_ip6m_qos_record_node, static) = {
465 .arc_name = "ip6-multicast",
466 .node_name = "vlan-ip6-qos-record",
467 .runs_before = VNET_FEATURES ("ip6-qos-record"),
470 VLIB_REGISTER_NODE (l2_ip_qos_record_node, static) = {
471 .function = l2_ip_qos_record,
472 .name = "l2-ip-qos-record",
473 .vector_size = sizeof (u32),
474 .format_trace = format_qos_record_trace,
475 .type = VLIB_NODE_TYPE_INTERNAL,
480 /* Consider adding error "no IP after L2, no recording" */
486 VLIB_NODE_FUNCTION_MULTIARCH (l2_ip_qos_record_node, l2_ip_qos_record);
491 l2_ip_qos_init (vlib_main_t * vm)
493 /* Initialize the feature next-node indexes */
494 feat_bitmap_init_next_nodes (vm,
495 l2_ip_qos_record_node.index,
497 l2input_get_feat_names (),
498 l2_qos_input_next[QOS_SOURCE_IP]);
502 VLIB_INIT_FUNCTION (l2_ip_qos_init);
504 static clib_error_t *
505 qos_record_cli (vlib_main_t * vm,
506 unformat_input_t * input, vlib_cli_command_t * cmd)
508 vnet_main_t *vnm = vnet_get_main ();
516 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
518 if (unformat (input, "%U", unformat_vnet_sw_interface,
521 else if (unformat (input, "%U", unformat_qos_source, &qs))
523 else if (unformat (input, "enable"))
525 else if (unformat (input, "disable"))
531 if (~0 == sw_if_index)
532 return clib_error_return (0, "interface must be specified");
534 return clib_error_return (0, "input location must be specified");
537 qos_record_enable (sw_if_index, qs);
539 qos_record_disable (sw_if_index, qs);
545 * Enable QoS bit recording on an interface using the packet's input DSCP bits
546 * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
547 * one protocol is chosen (which is foolish) the higher layers override the
551 * @cliexcmd{qos record ip GigEthernet0/1/0}
554 VLIB_CLI_COMMAND (qos_record_command, static) = {
555 .path = "qos record",
556 .short_help = "qos record <record-source> <INTERFACE> [disable]",
557 .function = qos_record_cli,
564 * fd.io coding-style-patch-verification: ON
567 * eval: (c-set-style "gnu")