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)
141 u32 sw_if_index0, next0, bi0;
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;
187 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
189 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
190 (b0->flags & VLIB_BUFFER_IS_TRACED)))
192 qos_record_trace_t *t =
193 vlib_add_trace (vm, node, b0, sizeof (*t));
200 vlib_buffer_advance (b0, -l2_len);
201 next0 = vnet_l2_feature_next (b0,
202 l2_qos_input_next[QOS_SOURCE_IP],
203 L2INPUT_FEAT_L2_IP_QOS_RECORD);
206 vnet_feature_next (sw_if_index0, &next0, b0);
208 /* verify speculative enqueue, maybe switch current next frame */
209 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
210 to_next, n_left_to_next,
214 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
217 return frame->n_vectors;
220 /* packet trace format function */
222 format_qos_record_trace (u8 * s, va_list * args)
224 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
225 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
226 qos_record_trace_t *t = va_arg (*args, qos_record_trace_t *);
228 s = format (s, "qos:%d", t->bits);
234 ip4_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
235 vlib_frame_t * frame)
237 return (qos_record_inline (vm, node, frame, 0, 0));
241 ip6_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
242 vlib_frame_t * frame)
244 return (qos_record_inline (vm, node, frame, 1, 0));
248 l2_ip_qos_record (vlib_main_t * vm, vlib_node_runtime_t * node,
249 vlib_frame_t * frame)
251 return (qos_record_inline (vm, node, frame, 0, 1));
255 VLIB_REGISTER_NODE (ip4_qos_record_node) = {
256 .function = ip4_qos_record,
257 .name = "ip4-qos-record",
258 .vector_size = sizeof (u32),
259 .format_trace = format_qos_record_trace,
260 .type = VLIB_NODE_TYPE_INTERNAL,
270 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_record_node, ip4_qos_record);
272 VNET_FEATURE_INIT (ip4_qos_record_node, static) = {
273 .arc_name = "ip4-unicast",
274 .node_name = "ip4-qos-record",
277 VLIB_REGISTER_NODE (ip6_qos_record_node) = {
278 .function = ip6_qos_record,
279 .name = "ip6-qos-record",
280 .vector_size = sizeof (u32),
281 .format_trace = format_qos_record_trace,
282 .type = VLIB_NODE_TYPE_INTERNAL,
292 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_record_node, ip6_qos_record);
294 VNET_FEATURE_INIT (ip6_qos_record_node, static) = {
295 .arc_name = "ip6-unicast",
296 .node_name = "ip6-qos-record",
299 VLIB_REGISTER_NODE (l2_ip_qos_record_node, static) = {
300 .function = l2_ip_qos_record,
301 .name = "l2-ip-qos-record",
302 .vector_size = sizeof (u32),
303 .format_trace = format_qos_record_trace,
304 .type = VLIB_NODE_TYPE_INTERNAL,
309 /* Consider adding error "no IP after L2, no recording" */
315 VLIB_NODE_FUNCTION_MULTIARCH (l2_ip_qos_record_node, l2_ip_qos_record);
319 l2_ip_qos_init (vlib_main_t * vm)
321 /* Initialize the feature next-node indexes */
322 feat_bitmap_init_next_nodes (vm,
323 l2_ip_qos_record_node.index,
325 l2input_get_feat_names (),
326 l2_qos_input_next[QOS_SOURCE_IP]);
330 VLIB_INIT_FUNCTION (l2_ip_qos_init);
332 static clib_error_t *
333 qos_record_cli (vlib_main_t * vm,
334 unformat_input_t * input, vlib_cli_command_t * cmd)
336 vnet_main_t *vnm = vnet_get_main ();
344 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
346 if (unformat (input, "%U", unformat_vnet_sw_interface,
349 else if (unformat (input, "%U", unformat_qos_source, &qs))
351 else if (unformat (input, "enable"))
353 else if (unformat (input, "disable"))
359 if (~0 == sw_if_index)
360 return clib_error_return (0, "interface must be specified");
362 return clib_error_return (0, "input location must be specified");
365 qos_record_enable (sw_if_index, qs);
367 qos_record_disable (sw_if_index, qs);
373 * Enable QoS bit recording on an interface using the packet's input DSCP bits
374 * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
375 * one protocol is chosen (which is foolish) the higer layers override the
379 * @cliexcmd{qos record ip GigEthernet0/1/0}
382 VLIB_CLI_COMMAND (qos_record_command, static) = {
383 .path = "qos record",
384 .short_help = "qos record <record-source> <INTERFACE> [disable]",
385 .function = qos_record_cli,
392 * fd.io coding-style-patch-verification: ON
395 * eval: (c-set-style "gnu")