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)
39 vnet_feature_enable_disable ("interface-output", "vlan-qos-mark",
40 sw_if_index, enable, NULL, 0);
44 qos_mark_mpls_enable_disable (u32 sw_if_index, u8 enable)
46 vnet_feature_enable_disable ("mpls-output", "mpls-qos-mark",
47 sw_if_index, enable, NULL, 0);
51 qos_egress_map_feature_config (u32 sw_if_index, qos_source_t qs, u8 enable)
59 qos_mark_vlan_enable_disable (sw_if_index, enable);
62 qos_mark_mpls_enable_disable (sw_if_index, enable);
65 qos_mark_ip_enable_disable (sw_if_index, enable);
70 always_inline qos_egress_map_t *
71 qos_egress_map_interface (u32 sw_if_index, qos_source_t output_source)
73 ASSERT (vec_len (qos_mark_configs[output_source]) > sw_if_index);
75 return pool_elt_at_index (qem_pool,
76 qos_mark_configs[output_source][sw_if_index]);
80 * per-packet trace data
82 typedef struct qos_mark_trace_t_
84 /* per-pkt trace data */
91 qos_mark_inline (vlib_main_t * vm,
92 vlib_node_runtime_t * node,
93 vlib_frame_t * frame, qos_source_t output_source, int is_ip6)
95 u32 n_left_from, *from, *to_next, next_index;
98 n_left_from = frame->n_vectors;
99 from = vlib_frame_vector_args (frame);
101 while (n_left_from > 0)
105 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
107 while (n_left_from > 0 && n_left_to_next > 0)
109 qos_source_t input_source0;
110 ethernet_vlan_header_t *vlan0;
111 u32 sw_if_index0, next0, bi0;
112 qos_egress_map_t *qem0;
128 b0 = vlib_get_buffer (vm, bi0);
129 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
130 input_source0 = vnet_buffer2 (b0)->qos.source;
132 qem0 = qos_egress_map_interface (sw_if_index0, output_source);
133 qos0 = qem0->qem_output[input_source0][vnet_buffer2 (b0)->qos.bits];
135 if (PREDICT_TRUE (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID))
137 /* there is a source of QoS recording for this packet */
138 if (QOS_SOURCE_IP == output_source)
142 ip6_0 = (vlib_buffer_get_current (b0) +
143 vnet_buffer (b0)->ip.save_rewrite_length);
145 ip6_set_traffic_class_network_order (ip6_0, qos0);
149 ip4_0 = (vlib_buffer_get_current (b0) +
150 vnet_buffer (b0)->ip.save_rewrite_length);
151 if (PREDICT_FALSE (qos0 != ip4_0->tos))
154 ip4_0->checksum = ip4_header_checksum (ip4_0);
158 else if (QOS_SOURCE_MPLS == output_source)
160 mpls_bytes_0 = (vlib_buffer_get_current (b0) +
161 vnet_buffer (b0)->mpls.save_rewrite_length);
163 /* apply to all the labels in the stack */
166 /* clear out the old COS bts */
167 mpls_bytes_0[2] &= 0xf1;
168 /* OR in 3 bits of the mapped value */
169 mpls_bytes_0[2] |= (qos0 & 0x7) << 1;
170 eos0 = mpls_bytes_0[2] & 0x1;
175 else if (QOS_SOURCE_VLAN == output_source)
177 vlan0 = (vlib_buffer_get_current (b0) +
178 sizeof (ethernet_header_t));
180 ethernet_vlan_header_set_priority_net_order (vlan0, qos0);
183 vnet_feature_next (sw_if_index0, &next0, b0);
185 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
187 qos_mark_trace_t *t =
188 vlib_add_trace (vm, node, b0, sizeof (*t));
190 t->input = input_source0;
191 t->used = (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID);
194 /* verify speculative enqueue, maybe switch current next frame */
195 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
196 to_next, n_left_to_next,
200 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
203 return frame->n_vectors;
206 /* packet trace format function */
208 format_qos_mark_trace (u8 * s, va_list * args)
210 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
211 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
212 qos_mark_trace_t *t = va_arg (*args, qos_mark_trace_t *);
214 s = format (s, "source:%U qos:%d used:%s",
215 format_qos_source, t->input, t->bits, (t->used ? "yes" : "no"));
221 ip4_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
222 vlib_frame_t * frame)
224 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 0));
228 ip6_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
229 vlib_frame_t * frame)
231 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 1));
235 mpls_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
236 vlib_frame_t * frame)
238 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_MPLS, 0));
242 vlan_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
243 vlib_frame_t * frame)
245 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
249 VLIB_REGISTER_NODE (ip4_qos_mark_node) = {
250 .function = ip4_qos_mark,
251 .name = "ip4-qos-mark",
252 .vector_size = sizeof (u32),
253 .format_trace = format_qos_mark_trace,
254 .type = VLIB_NODE_TYPE_INTERNAL,
264 VLIB_NODE_FUNCTION_MULTIARCH (ip4_qos_mark_node, ip4_qos_mark);
266 VNET_FEATURE_INIT (ip4_qos_mark_node, static) = {
267 .arc_name = "ip4-output",
268 .node_name = "ip4-qos-mark",
271 VLIB_REGISTER_NODE (ip6_qos_mark_node) = {
272 .function = ip6_qos_mark,
273 .name = "ip6-qos-mark",
274 .vector_size = sizeof (u32),
275 .format_trace = format_qos_mark_trace,
276 .type = VLIB_NODE_TYPE_INTERNAL,
286 VLIB_NODE_FUNCTION_MULTIARCH (ip6_qos_mark_node, ip6_qos_mark);
288 VNET_FEATURE_INIT (ip6_qos_mark_node, static) = {
289 .arc_name = "ip6-output",
290 .node_name = "ip6-qos-mark",
293 VLIB_REGISTER_NODE (mpls_qos_mark_node) = {
294 .function = mpls_qos_mark,
295 .name = "mpls-qos-mark",
296 .vector_size = sizeof (u32),
297 .format_trace = format_qos_mark_trace,
298 .type = VLIB_NODE_TYPE_INTERNAL,
308 VLIB_NODE_FUNCTION_MULTIARCH (mpls_qos_mark_node, mpls_qos_mark);
310 VNET_FEATURE_INIT (mpls_qos_mark_node, static) = {
311 .arc_name = "mpls-output",
312 .node_name = "mpls-qos-mark",
314 VLIB_REGISTER_NODE (vlan_qos_mark_node) = {
315 .function = vlan_qos_mark,
316 .name = "vlan-qos-mark",
317 .vector_size = sizeof (u32),
318 .format_trace = format_qos_mark_trace,
319 .type = VLIB_NODE_TYPE_INTERNAL,
329 VLIB_NODE_FUNCTION_MULTIARCH (vlan_qos_mark_node, vlan_qos_mark);
331 VNET_FEATURE_INIT (vlan_qos_mark_node, static) = {
332 .arc_name = "interface-output",
333 .node_name = "vlan-qos-mark",
338 qos_mark_enable (u32 sw_if_index,
339 qos_source_t output_source, qos_egress_map_id_t mid)
343 vec_validate_init_empty (qos_mark_configs[output_source],
344 sw_if_index, INDEX_INVALID);
346 qemi = qos_egress_map_find (mid);
348 if (INDEX_INVALID == qemi)
349 return VNET_API_ERROR_NO_SUCH_TABLE;
351 if (INDEX_INVALID == qos_mark_configs[output_source][sw_if_index])
353 qos_egress_map_feature_config (sw_if_index, output_source, 1);
356 qos_mark_configs[output_source][sw_if_index] = qemi;
362 qos_mark_disable (u32 sw_if_index, qos_source_t output_source)
364 if (vec_len (qos_mark_configs[output_source]) < sw_if_index)
365 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
366 if (INDEX_INVALID == qos_mark_configs[output_source][sw_if_index])
367 return VNET_API_ERROR_VALUE_EXIST;
369 if (INDEX_INVALID != qos_mark_configs[output_source][sw_if_index])
371 qos_egress_map_feature_config (sw_if_index, output_source, 0);
374 qos_mark_configs[output_source][sw_if_index] = INDEX_INVALID;
379 static clib_error_t *
380 qos_mark_cli (vlib_main_t * vm,
381 unformat_input_t * input, vlib_cli_command_t * cmd)
383 qos_egress_map_id_t map_id;
388 vnm = vnet_get_main ();
393 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
395 if (unformat (input, "id %d", &map_id))
397 else if (unformat (input, "disable"))
399 else if (unformat (input, "%U", unformat_qos_source, &qs))
401 else if (unformat (input, "%U",
402 unformat_vnet_sw_interface, vnm, &sw_if_index))
408 if (~0 == sw_if_index)
409 return clib_error_return (0, "interface must be specified");
411 return clib_error_return (0, "output location must be specified");
414 rv = qos_mark_enable (sw_if_index, qs, map_id);
416 rv = qos_mark_disable (sw_if_index, qs);
421 return clib_error_return (0, "Failed to map interface");
425 * Apply a QoS egress mapping table to an interface for QoS marking packets
426 * at the given output protocol.
429 * @cliexcmd{qos egress interface GigEthernet0/9/0 id 0 output ip}
432 VLIB_CLI_COMMAND (qos_egress_map_interface_command, static) = {
434 .short_help = "qos mark <SOURCE> <INTERFACE> id <MAP>",
435 .function = qos_mark_cli,
441 * fd.io coding-style-patch-verification: ON
444 * eval: (c-set-style "gnu")