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/ip/ip.h>
17 #include <vnet/feature/feature.h>
18 #include <vnet/qos/qos_egress_map.h>
19 #include <vnet/qos/qos_mark.h>
22 * per-interface vector of which MAP is used by which interface
23 * for each output source
25 index_t *qos_mark_configs[QOS_N_SOURCES];
28 qos_mark_ip_enable_disable (u32 sw_if_index, u8 enable)
30 vnet_feature_enable_disable ("ip6-output", "ip6-qos-mark",
31 sw_if_index, enable, NULL, 0);
32 vnet_feature_enable_disable ("ip4-output", "ip4-qos-mark",
33 sw_if_index, enable, NULL, 0);
37 qos_mark_vlan_enable_disable (u32 sw_if_index, u8 enable)
40 * one cannot run a feature on a sub-interface, so we need
41 * to enable a feature on all the L3 output paths
43 vnet_feature_enable_disable ("ip6-output", "vlan-ip6-qos-mark",
44 sw_if_index, enable, NULL, 0);
45 vnet_feature_enable_disable ("ip4-output", "vlan-ip4-qos-mark",
46 sw_if_index, enable, NULL, 0);
47 vnet_feature_enable_disable ("mpls-output", "vlan-mpls-qos-mark",
48 sw_if_index, enable, NULL, 0);
52 qos_mark_mpls_enable_disable (u32 sw_if_index, u8 enable)
54 vnet_feature_enable_disable ("mpls-output", "mpls-qos-mark",
55 sw_if_index, enable, NULL, 0);
59 qos_egress_map_feature_config (u32 sw_if_index, qos_source_t qs, u8 enable)
67 qos_mark_vlan_enable_disable (sw_if_index, enable);
70 qos_mark_mpls_enable_disable (sw_if_index, enable);
73 qos_mark_ip_enable_disable (sw_if_index, enable);
79 qos_mark_enable (u32 sw_if_index,
80 qos_source_t output_source, qos_egress_map_id_t mid)
84 vec_validate_init_empty (qos_mark_configs[output_source],
85 sw_if_index, INDEX_INVALID);
87 qemi = qos_egress_map_find (mid);
89 if (INDEX_INVALID == qemi)
90 return VNET_API_ERROR_NO_SUCH_TABLE;
92 if (INDEX_INVALID == qos_mark_configs[output_source][sw_if_index])
94 qos_egress_map_feature_config (sw_if_index, output_source, 1);
97 qos_mark_configs[output_source][sw_if_index] = qemi;
103 qos_mark_disable (u32 sw_if_index, qos_source_t output_source)
105 if (vec_len (qos_mark_configs[output_source]) <= sw_if_index)
106 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
107 if (INDEX_INVALID == qos_mark_configs[output_source][sw_if_index])
108 return VNET_API_ERROR_VALUE_EXIST;
110 if (INDEX_INVALID != qos_mark_configs[output_source][sw_if_index])
112 qos_egress_map_feature_config (sw_if_index, output_source, 0);
115 qos_mark_configs[output_source][sw_if_index] = INDEX_INVALID;
121 qos_mark_walk (qos_mark_walk_cb_t fn, void *c)
125 FOR_EACH_QOS_SOURCE (qs)
129 vec_foreach_index (sw_if_index, qos_mark_configs[qs])
131 if (INDEX_INVALID != qos_mark_configs[qs][sw_if_index])
133 qos_egress_map_get_id (qos_mark_configs[qs][sw_if_index]), qs, c);
138 static clib_error_t *
139 qos_mark_cli (vlib_main_t * vm,
140 unformat_input_t * input, vlib_cli_command_t * cmd)
142 qos_egress_map_id_t map_id;
147 vnm = vnet_get_main ();
152 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
154 if (unformat (input, "id %d", &map_id))
156 else if (unformat (input, "disable"))
158 else if (unformat (input, "%U", unformat_qos_source, &qs))
160 else if (unformat (input, "%U",
161 unformat_vnet_sw_interface, vnm, &sw_if_index))
167 if (~0 == sw_if_index)
168 return clib_error_return (0, "interface must be specified");
170 return clib_error_return (0, "output location must be specified");
173 rv = qos_mark_enable (sw_if_index, qs, map_id);
175 rv = qos_mark_disable (sw_if_index, qs);
180 return clib_error_return (0, "Failed to map interface");
184 * Apply a QoS egress mapping table to an interface for QoS marking packets
185 * at the given output protocol.
188 * @cliexcmd{qos egress interface GigEthernet0/9/0 id 0 output ip}
191 VLIB_CLI_COMMAND (qos_egress_map_interface_command, static) = {
193 .short_help = "qos mark <SOURCE> <INTERFACE> id <MAP>",
194 .function = qos_mark_cli,
200 qos_mark_show_one_interface (vlib_main_t * vm, u32 sw_if_index)
202 index_t qemis[QOS_N_SOURCES];
207 clib_memset_u32 (qemis, INDEX_INVALID, QOS_N_SOURCES);
209 FOR_EACH_QOS_SOURCE (qs)
211 if (vec_len (qos_mark_configs[qs]) <= sw_if_index)
213 if (INDEX_INVALID != (qemis[qs] = qos_mark_configs[qs][sw_if_index]))
219 vlib_cli_output (vm, " %U:", format_vnet_sw_if_index_name,
220 vnet_get_main (), sw_if_index);
222 FOR_EACH_QOS_SOURCE (qs)
224 if (qemis[qs] != INDEX_INVALID)
225 vlib_cli_output (vm, " %U: map:%d", format_qos_source, qs,
231 static clib_error_t *
232 qos_mark_show (vlib_main_t * vm,
233 unformat_input_t * input, vlib_cli_command_t * cmd)
235 vnet_main_t *vnm = vnet_get_main ();
241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
243 if (unformat (input, "%U", unformat_vnet_sw_interface,
248 if (~0 == sw_if_index)
252 FOR_EACH_QOS_SOURCE (qs)
254 n_ints = clib_max (n_ints, vec_len (qos_mark_configs[qs]));
257 for (ii = 0; ii < n_ints; ii++)
259 qos_mark_show_one_interface (vm, ii);
263 qos_mark_show_one_interface (vm, sw_if_index);
269 * Show Egress Qos Maps
272 * @cliexcmd{show qos egress map}
275 VLIB_CLI_COMMAND (qos_mark_show_command, static) = {
276 .path = "show qos mark",
277 .short_help = "show qos mark [interface]",
278 .function = qos_mark_show,
284 * fd.io coding-style-patch-verification: ON
287 * eval: (c-set-style "gnu")