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];
26 static u32 l2_qos_input_next[QOS_N_SOURCES][32];
29 qos_record_feature_config (u32 sw_if_index,
30 qos_source_t input_source, u8 enable)
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,
49 // not implemented yet
55 qos_record_enable (u32 sw_if_index, qos_source_t input_source)
57 vec_validate (qos_record_configs[input_source], sw_if_index);
59 if (0 == qos_record_configs[input_source][sw_if_index])
61 qos_record_feature_config (sw_if_index, input_source, 1);
64 qos_record_configs[input_source][sw_if_index]++;
69 qos_record_disable (u32 sw_if_index, qos_source_t input_source)
71 if (vec_len (qos_record_configs[input_source]) <= sw_if_index)
72 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
74 if (0 == qos_record_configs[input_source][sw_if_index])
75 return VNET_API_ERROR_VALUE_EXIST;
77 qos_record_configs[input_source][sw_if_index]--;
79 if (0 == qos_record_configs[input_source][sw_if_index])
81 qos_record_feature_config (sw_if_index, input_source, 0);
88 * Disable recording feautre for all protocols when the interface
92 qos_record_ip_interface_add_del (vnet_main_t * vnm,
93 u32 sw_if_index, u32 is_add)
99 FOR_EACH_QOS_SOURCE (qs)
101 while (qos_record_disable (sw_if_index, qs) == 0);
108 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_record_ip_interface_add_del);
111 * per-packet trace data
113 typedef struct qos_record_trace_t_
115 /* per-pkt trace data */
117 } qos_record_trace_t;
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)
124 u32 n_left_from, *from, *to_next, next_index;
127 n_left_from = frame->n_vectors;
128 from = vlib_frame_vector_args (frame);
130 while (n_left_from > 0)
134 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
136 while (n_left_from > 0 && n_left_to_next > 0)
153 b0 = vlib_get_buffer (vm, bi0);
157 l2_len = vnet_buffer (b0)->l2.l2_len;
161 vlib_buffer_advance (b0, l2_len);
163 l3h = vlib_buffer_get_current (b0);
164 ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));
166 if (ethertype == ETHERNET_TYPE_IP4)
168 else if (ethertype == ETHERNET_TYPE_IP6)
176 ip6_0 = vlib_buffer_get_current (b0);
177 qos0 = ip6_traffic_class_network_order (ip6_0);
181 ip4_0 = vlib_buffer_get_current (b0);
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;
188 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
189 (b0->flags & VLIB_BUFFER_IS_TRACED)))
191 qos_record_trace_t *t =
192 vlib_add_trace (vm, node, b0, sizeof (*t));
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);
205 vnet_feature_next (&next0, b0);
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,
213 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
216 return frame->n_vectors;
219 /* packet trace format function */
221 format_qos_record_trace (u8 * s, va_list * args)
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 *);
227 s = format (s, "qos:%d", t->bits);
233 ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
234 vlib_frame_t * frame)
236 return (qos_record_inline (vm, node, frame, 0, 0));
240 ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
241 vlib_frame_t * frame)
243 return (qos_record_inline (vm, node, frame, 1, 0));
247 l2_ip_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
248 vlib_frame_t * frame)
250 return (qos_record_inline (vm, node, frame, 0, 1));
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,
269 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_record_node, ip4_qos_record);
271 VNET_FEATURE_INIT (ip4_qos_record_node, static) = {
272 .arc_name = "ip4-unicast",
273 .node_name = "ip4-qos-record",
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,
291 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_record_node, ip6_qos_record);
293 VNET_FEATURE_INIT (ip6_qos_record_node, static) = {
294 .arc_name = "ip6-unicast",
295 .node_name = "ip6-qos-record",
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,
308 /* Consider adding error "no IP after L2, no recording" */
314 VLIB_NODE_FUNCTION_MULTIARCH (l2_ip_qos_record_node, l2_ip_qos_record);
318 l2_ip_qos_init (vlib_main_t * vm)
320 /* Initialize the feature next-node indexes */
321 feat_bitmap_init_next_nodes (vm,
322 l2_ip_qos_record_node.index,
324 l2input_get_feat_names (),
325 l2_qos_input_next[QOS_SOURCE_IP]);
329 VLIB_INIT_FUNCTION (l2_ip_qos_init);
331 static clib_error_t *
332 qos_record_cli (vlib_main_t * vm,
333 unformat_input_t * input, vlib_cli_command_t * cmd)
335 vnet_main_t *vnm = vnet_get_main ();
343 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
345 if (unformat (input, "%U", unformat_vnet_sw_interface,
348 else if (unformat (input, "%U", unformat_qos_source, &qs))
350 else if (unformat (input, "enable"))
352 else if (unformat (input, "disable"))
358 if (~0 == sw_if_index)
359 return clib_error_return (0, "interface must be specified");
361 return clib_error_return (0, "input location must be specified");
364 qos_record_enable (sw_if_index, qs);
366 qos_record_disable (sw_if_index, qs);
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
378 * @cliexcmd{qos record ip GigEthernet0/1/0}
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,
391 * fd.io coding-style-patch-verification: ON
394 * eval: (c-set-style "gnu")