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 u8 *qos_record_configs[QOS_N_SOURCES];
28 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 ip_feature_enable_disable (AF_IP6, N_SAFI, IP_FEATURE_INPUT,
39 sw_if_index, enable, NULL, 0);
40 ip_feature_enable_disable (AF_IP4, N_SAFI, IP_FEATURE_INPUT,
42 sw_if_index, enable, NULL, 0);
43 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_L2_IP_QOS_RECORD,
47 vnet_feature_enable_disable ("mpls-input", "mpls-qos-record",
48 sw_if_index, enable, NULL, 0);
51 ip_feature_enable_disable (AF_IP6, N_SAFI, IP_FEATURE_INPUT,
52 "vlan-ip6-qos-record",
53 sw_if_index, enable, NULL, 0);
54 ip_feature_enable_disable (AF_IP4, N_SAFI, IP_FEATURE_INPUT,
55 "vlan-ip4-qos-record",
56 sw_if_index, enable, NULL, 0);
57 vnet_feature_enable_disable ("mpls-input", "vlan-mpls-qos-record",
58 sw_if_index, enable, NULL, 0);
61 /* not a valid option for recording */
67 qos_record_enable (u32 sw_if_index, qos_source_t input_source)
69 vec_validate (qos_record_configs[input_source], sw_if_index);
71 if (0 == qos_record_configs[input_source][sw_if_index])
73 qos_record_feature_config (sw_if_index, input_source, 1);
76 qos_record_configs[input_source][sw_if_index]++;
81 qos_record_disable (u32 sw_if_index, qos_source_t input_source)
83 if (vec_len (qos_record_configs[input_source]) <= sw_if_index)
84 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
86 if (0 == qos_record_configs[input_source][sw_if_index])
87 return VNET_API_ERROR_VALUE_EXIST;
89 qos_record_configs[input_source][sw_if_index]--;
91 if (0 == qos_record_configs[input_source][sw_if_index])
93 qos_record_feature_config (sw_if_index, input_source, 0);
100 qos_record_walk (qos_record_walk_cb_t fn, void *c)
104 FOR_EACH_QOS_SOURCE (qs)
108 vec_foreach_index (sw_if_index, qos_record_configs[qs])
110 if (0 != qos_record_configs[qs][sw_if_index])
111 fn (sw_if_index, qs, c);
117 * Disable recording feature for all protocols when the interface
120 static clib_error_t *
121 qos_record_ip_interface_add_del (vnet_main_t * vnm,
122 u32 sw_if_index, u32 is_add)
128 FOR_EACH_QOS_SOURCE (qs)
130 while (qos_record_disable (sw_if_index, qs) == 0);
137 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (qos_record_ip_interface_add_del);
140 qos_record_init (vlib_main_t * vm)
143 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "l2-ip-qos-record");
145 /* Initialize the feature next-node indexes */
146 FOR_EACH_QOS_SOURCE (qs)
147 feat_bitmap_init_next_nodes (vm,
150 l2input_get_feat_names (),
151 l2_qos_input_next[qs]);
155 VLIB_INIT_FUNCTION (qos_record_init);
157 static clib_error_t *
158 qos_record_cli (vlib_main_t * vm,
159 unformat_input_t * input, vlib_cli_command_t * cmd)
161 vnet_main_t *vnm = vnet_get_main ();
169 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
171 if (unformat (input, "%U", unformat_vnet_sw_interface,
174 else if (unformat (input, "%U", unformat_qos_source, &qs))
176 else if (unformat (input, "enable"))
178 else if (unformat (input, "disable"))
184 if (~0 == sw_if_index)
185 return clib_error_return (0, "interface must be specified");
187 return clib_error_return (0, "input location must be specified");
190 qos_record_enable (sw_if_index, qs);
192 qos_record_disable (sw_if_index, qs);
198 * Enable QoS bit recording on an interface using the packet's input DSCP bits
199 * Which input QoS bits to use are either; IP, MPLS or VLAN. If more than
200 * one protocol is chosen (which is foolish) the higher layers override the
204 * @cliexcmd{qos record ip GigEthernet0/1/0}
207 VLIB_CLI_COMMAND (qos_record_command, static) = {
208 .path = "qos record",
209 .short_help = "qos record <record-source> <INTERFACE> [disable]",
210 .function = qos_record_cli,
216 qos_record_show_one_interface (vlib_main_t * vm, u32 sw_if_index)
218 u8 n_cfgs[QOS_N_SOURCES] = { };
224 FOR_EACH_QOS_SOURCE (qs)
226 if (vec_len (qos_record_configs[qs]) <= sw_if_index)
228 if (0 != (n_cfgs[qs] = qos_record_configs[qs][sw_if_index]))
234 vlib_cli_output (vm, " %U:", format_vnet_sw_if_index_name,
235 vnet_get_main (), sw_if_index);
237 FOR_EACH_QOS_SOURCE (qs)
240 vlib_cli_output (vm, " %U", format_qos_source, qs);
245 static clib_error_t *
246 qos_record_show (vlib_main_t * vm,
247 unformat_input_t * input, vlib_cli_command_t * cmd)
249 vnet_main_t *vnm = vnet_get_main ();
255 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
257 if (unformat (input, "%U", unformat_vnet_sw_interface,
262 if (~0 == sw_if_index)
266 FOR_EACH_QOS_SOURCE (qs)
268 n_ints = clib_max (n_ints, vec_len (qos_record_configs[qs]));
271 for (ii = 0; ii < n_ints; ii++)
273 qos_record_show_one_interface (vm, ii);
277 qos_record_show_one_interface (vm, sw_if_index);
283 * Show Egress Qos Maps
286 * @cliexcmd{show qos egress map}
289 VLIB_CLI_COMMAND (qos_record_show_command, static) = {
290 .path = "show qos record",
291 .short_help = "show qos record [interface]",
292 .function = qos_record_show,
298 * fd.io coding-style-patch-verification: ON
301 * eval: (c-set-style "gnu")