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>
23 * Per-interface, per-protocol vector of feature on/off configurations
25 static u8 *qos_record_configs[QOS_N_SOURCES];
28 qos_record_feature_config (u32 sw_if_index,
29 qos_source_t input_source, u8 enable)
34 vnet_feature_enable_disable ("ip6-unicast", "ip6-qos-record",
35 sw_if_index, enable, NULL, 0);
36 vnet_feature_enable_disable ("ip6-multicast", "ip6-qos-record",
37 sw_if_index, enable, NULL, 0);
38 vnet_feature_enable_disable ("ip4-unicast", "ip4-qos-record",
39 sw_if_index, enable, NULL, 0);
40 vnet_feature_enable_disable ("ip4-multicast", "ip4-qos-record",
41 sw_if_index, enable, NULL, 0);
46 // not implemented yet
52 qos_record_enable (u32 sw_if_index, qos_source_t input_source)
54 vec_validate (qos_record_configs[input_source], sw_if_index);
56 if (0 == qos_record_configs[input_source][sw_if_index])
58 qos_record_feature_config (sw_if_index, input_source, 1);
61 qos_record_configs[input_source][sw_if_index]++;
66 qos_record_disable (u32 sw_if_index, qos_source_t input_source)
68 if (vec_len (qos_record_configs[input_source]) < sw_if_index)
69 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
71 if (0 == qos_record_configs[input_source][sw_if_index])
72 return VNET_API_ERROR_VALUE_EXIST;
74 qos_record_configs[input_source][sw_if_index]--;
76 if (0 == qos_record_configs[input_source][sw_if_index])
78 qos_record_feature_config (sw_if_index, input_source, 0);
85 * Disable recording feautre for all protocols when the interface
89 qos_record_ip_interface_add_del (vnet_main_t * vnm,
90 u32 sw_if_index, u32 is_add)
96 FOR_EACH_QOS_SOURCE (qs)
98 qos_record_disable (sw_if_index, qs);
105 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_record_ip_interface_add_del);
108 * per-packet trace data
110 typedef struct qos_record_trace_t_
112 /* per-pkt trace data */
114 } qos_record_trace_t;
117 qos_record_inline (vlib_main_t * vm,
118 vlib_node_runtime_t * node,
119 vlib_frame_t * frame, int is_ip6)
121 u32 n_left_from, *from, *to_next, next_index;
124 n_left_from = frame->n_vectors;
125 from = vlib_frame_vector_args (frame);
127 while (n_left_from > 0)
131 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
133 while (n_left_from > 0 && n_left_to_next > 0)
138 u32 sw_if_index0, next0, bi0;
149 b0 = vlib_get_buffer (vm, bi0);
152 ip6_0 = vlib_buffer_get_current (b0);
153 qos0 = ip6_traffic_class_network_order (ip6_0);
157 ip4_0 = vlib_buffer_get_current (b0);
160 vnet_buffer2 (b0)->qos.bits = qos0;
161 vnet_buffer2 (b0)->qos.source = QOS_SOURCE_IP;
162 b0->flags |= VNET_BUFFER_F_QOS_DATA_VALID;
163 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
165 vnet_feature_next (sw_if_index0, &next0, b0);
167 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
168 (b0->flags & VLIB_BUFFER_IS_TRACED)))
170 qos_record_trace_t *t =
171 vlib_add_trace (vm, node, b0, sizeof (*t));
175 /* verify speculative enqueue, maybe switch current next frame */
176 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
177 to_next, n_left_to_next,
181 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
184 return frame->n_vectors;
187 /* packet trace format function */
189 format_qos_record_trace (u8 * s, va_list * args)
191 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
192 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
193 qos_record_trace_t *t = va_arg (*args, qos_record_trace_t *);
195 s = format (s, "qos:%d", t->bits);
201 ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
202 vlib_frame_t * frame)
204 return (qos_record_inline (vm, node, frame, 0));
208 ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
209 vlib_frame_t * frame)
211 return (qos_record_inline (vm, node, frame, 1));
215 VLIB_REGISTER_NODE (ip4_qos_record_node) = {
216 .function = ip4_qos_record,
217 .name = "ip4-qos-record",
218 .vector_size = sizeof (u32),
219 .format_trace = format_qos_record_trace,
220 .type = VLIB_NODE_TYPE_INTERNAL,
230 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_record_node, ip4_qos_record);
232 VNET_FEATURE_INIT (ip4_qos_record_node, static) = {
233 .arc_name = "ip4-unicast",
234 .node_name = "ip4-qos-record",
237 VLIB_REGISTER_NODE (ip6_qos_record_node) = {
238 .function = ip6_qos_record,
239 .name = "ip6-qos-record",
240 .vector_size = sizeof (u32),
241 .format_trace = format_qos_record_trace,
242 .type = VLIB_NODE_TYPE_INTERNAL,
252 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_record_node, ip6_qos_record);
254 VNET_FEATURE_INIT (ip6_qos_record_node, static) = {
255 .arc_name = "ip6-unicast",
256 .node_name = "ip6-qos-record",
261 static clib_error_t *
262 qos_record_cli (vlib_main_t * vm,
263 unformat_input_t * input, vlib_cli_command_t * cmd)
265 vnet_main_t *vnm = vnet_get_main ();
273 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
275 if (unformat (input, "%U", unformat_vnet_sw_interface,
278 else if (unformat (input, "%U", unformat_qos_source, &qs))
280 else if (unformat (input, "enable"))
282 else if (unformat (input, "disable"))
288 if (~0 == sw_if_index)
289 return clib_error_return (0, "interface must be specified");
291 return clib_error_return (0, "input location must be specified");
294 qos_record_enable (sw_if_index, qs);
296 qos_record_disable (sw_if_index, qs);
302 * Enable QoS bit recording on an interface using the packet's input DSCP bits
303 * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
304 * one protocol is chosen (which is foolish) the higer layers override the
308 * @cliexcmd{qos record ip GigEthernet0/1/0}
311 VLIB_CLI_COMMAND (qos_record_command, static) = {
312 .path = "qos record",
313 .short_help = "qos record <record-source> <INTERFACE> [disable]",
314 .function = qos_record_cli,
321 * fd.io coding-style-patch-verification: ON
324 * eval: (c-set-style "gnu")