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);
78 always_inline qos_egress_map_t *
79 qos_egress_map_interface (u32 sw_if_index, qos_source_t output_source)
81 ASSERT (vec_len (qos_mark_configs[output_source]) > sw_if_index);
83 return pool_elt_at_index (qem_pool,
84 qos_mark_configs[output_source][sw_if_index]);
88 * per-packet trace data
90 typedef struct qos_mark_trace_t_
92 /* per-pkt trace data */
99 qos_mark_inline (vlib_main_t * vm,
100 vlib_node_runtime_t * node,
101 vlib_frame_t * frame, qos_source_t output_source, int is_ip6)
103 u32 n_left_from, *from, *to_next, next_index;
106 n_left_from = frame->n_vectors;
107 from = vlib_frame_vector_args (frame);
109 while (n_left_from > 0)
113 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
115 while (n_left_from > 0 && n_left_to_next > 0)
117 qos_source_t input_source0;
118 ethernet_vlan_header_t *vlan0;
119 u32 sw_if_index0, next0, bi0;
120 qos_egress_map_t *qem0;
136 b0 = vlib_get_buffer (vm, bi0);
137 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
138 input_source0 = vnet_buffer2 (b0)->qos.source;
140 qem0 = qos_egress_map_interface (sw_if_index0, output_source);
141 qos0 = qem0->qem_output[input_source0][vnet_buffer2 (b0)->qos.bits];
143 if (PREDICT_TRUE (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID))
145 /* there is a source of QoS recording for this packet */
146 if (QOS_SOURCE_IP == output_source)
150 ip6_0 = (vlib_buffer_get_current (b0) +
151 vnet_buffer (b0)->ip.save_rewrite_length);
153 ip6_set_traffic_class_network_order (ip6_0, qos0);
157 ip4_0 = (vlib_buffer_get_current (b0) +
158 vnet_buffer (b0)->ip.save_rewrite_length);
159 if (PREDICT_FALSE (qos0 != ip4_0->tos))
162 ip4_0->checksum = ip4_header_checksum (ip4_0);
166 else if (QOS_SOURCE_MPLS == output_source)
168 mpls_bytes_0 = (vlib_buffer_get_current (b0) +
169 vnet_buffer (b0)->mpls.save_rewrite_length);
171 /* apply to all the labels in the stack */
174 /* clear out the old COS bts */
175 mpls_bytes_0[2] &= 0xf1;
176 /* OR in 3 bits of the mapped value */
177 mpls_bytes_0[2] |= (qos0 & 0x7) << 1;
178 eos0 = mpls_bytes_0[2] & 0x1;
183 else if (QOS_SOURCE_VLAN == output_source)
185 vlan0 = (vlib_buffer_get_current (b0) +
186 sizeof (ethernet_header_t));
188 ethernet_vlan_header_set_priority_net_order (vlan0, qos0);
191 vnet_feature_next (&next0, b0);
193 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
195 qos_mark_trace_t *t =
196 vlib_add_trace (vm, node, b0, sizeof (*t));
198 t->input = input_source0;
199 t->used = (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID);
202 /* verify speculative enqueue, maybe switch current next frame */
203 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
204 to_next, n_left_to_next,
208 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
211 return frame->n_vectors;
214 /* packet trace format function */
216 format_qos_mark_trace (u8 * s, va_list * args)
218 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
219 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
220 qos_mark_trace_t *t = va_arg (*args, qos_mark_trace_t *);
222 s = format (s, "source:%U qos:%d used:%s",
223 format_qos_source, t->input, t->bits, (t->used ? "yes" : "no"));
229 ip4_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
230 vlib_frame_t * frame)
232 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 0));
236 ip6_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
237 vlib_frame_t * frame)
239 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 1));
243 mpls_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
244 vlib_frame_t * frame)
246 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_MPLS, 0));
250 vlan_mpls_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
251 vlib_frame_t * frame)
253 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
257 vlan_ip4_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
258 vlib_frame_t * frame)
260 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
264 vlan_ip6_qos_mark (vlib_main_t * vm, vlib_node_runtime_t * node,
265 vlib_frame_t * frame)
267 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
271 VLIB_REGISTER_NODE (ip4_qos_mark_node) = {
272 .function = ip4_qos_mark,
273 .name = "ip4-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 (ip4_qos_mark_node, ip4_qos_mark);
288 VNET_FEATURE_INIT (ip4_qos_mark_node, static) = {
289 .arc_name = "ip4-output",
290 .node_name = "ip4-qos-mark",
293 VLIB_REGISTER_NODE (ip6_qos_mark_node) = {
294 .function = ip6_qos_mark,
295 .name = "ip6-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 (ip6_qos_mark_node, ip6_qos_mark);
310 VNET_FEATURE_INIT (ip6_qos_mark_node, static) = {
311 .arc_name = "ip6-output",
312 .node_name = "ip6-qos-mark",
315 VLIB_REGISTER_NODE (mpls_qos_mark_node) = {
316 .function = mpls_qos_mark,
317 .name = "mpls-qos-mark",
318 .vector_size = sizeof (u32),
319 .format_trace = format_qos_mark_trace,
320 .type = VLIB_NODE_TYPE_INTERNAL,
330 VLIB_NODE_FUNCTION_MULTIARCH (mpls_qos_mark_node, mpls_qos_mark);
332 VNET_FEATURE_INIT (mpls_qos_mark_node, static) = {
333 .arc_name = "mpls-output",
334 .node_name = "mpls-qos-mark",
337 VLIB_REGISTER_NODE (vlan_ip4_qos_mark_node) = {
338 .function = vlan_ip4_qos_mark,
339 .name = "vlan-ip4-qos-mark",
340 .vector_size = sizeof (u32),
341 .format_trace = format_qos_mark_trace,
342 .type = VLIB_NODE_TYPE_INTERNAL,
352 VLIB_NODE_FUNCTION_MULTIARCH (vlan_ip4_qos_mark_node, vlan_ip4_qos_mark);
354 VNET_FEATURE_INIT (vlan_ip4_qos_mark_node, static) = {
355 .arc_name = "ip4-output",
356 .node_name = "vlan-ip4-qos-mark",
357 .runs_after = VNET_FEATURES ("ip4-qos-mark"),
360 VLIB_REGISTER_NODE (vlan_ip6_qos_mark_node) = {
361 .function = vlan_ip6_qos_mark,
362 .name = "vlan-ip6-qos-mark",
363 .vector_size = sizeof (u32),
364 .format_trace = format_qos_mark_trace,
365 .type = VLIB_NODE_TYPE_INTERNAL,
375 VLIB_NODE_FUNCTION_MULTIARCH (vlan_ip6_qos_mark_node, vlan_ip6_qos_mark);
377 VNET_FEATURE_INIT (vlan_ip6_qos_mark_node, static) = {
378 .arc_name = "ip6-output",
379 .node_name = "vlan-ip6-qos-mark",
380 .runs_after = VNET_FEATURES ("ip6-qos-mark"),
383 VLIB_REGISTER_NODE (vlan_mpls_qos_mark_node) = {
384 .function = vlan_mpls_qos_mark,
385 .name = "vlan-mpls-qos-mark",
386 .vector_size = sizeof (u32),
387 .format_trace = format_qos_mark_trace,
388 .type = VLIB_NODE_TYPE_INTERNAL,
398 VLIB_NODE_FUNCTION_MULTIARCH (vlan_mpls_qos_mark_node, vlan_mpls_qos_mark);
400 VNET_FEATURE_INIT (vlan_mpls_qos_mark_node, static) = {
401 .arc_name = "mpls-output",
402 .node_name = "vlan-mpls-qos-mark",
403 .runs_after = VNET_FEATURES ("mpls-qos-mark"),
408 qos_mark_enable (u32 sw_if_index,
409 qos_source_t output_source, qos_egress_map_id_t mid)
413 vec_validate_init_empty (qos_mark_configs[output_source],
414 sw_if_index, INDEX_INVALID);
416 qemi = qos_egress_map_find (mid);
418 if (INDEX_INVALID == qemi)
419 return VNET_API_ERROR_NO_SUCH_TABLE;
421 if (INDEX_INVALID == qos_mark_configs[output_source][sw_if_index])
423 qos_egress_map_feature_config (sw_if_index, output_source, 1);
426 qos_mark_configs[output_source][sw_if_index] = qemi;
432 qos_mark_disable (u32 sw_if_index, qos_source_t output_source)
434 if (vec_len (qos_mark_configs[output_source]) <= sw_if_index)
435 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
436 if (INDEX_INVALID == qos_mark_configs[output_source][sw_if_index])
437 return VNET_API_ERROR_VALUE_EXIST;
439 if (INDEX_INVALID != qos_mark_configs[output_source][sw_if_index])
441 qos_egress_map_feature_config (sw_if_index, output_source, 0);
444 qos_mark_configs[output_source][sw_if_index] = INDEX_INVALID;
449 static clib_error_t *
450 qos_mark_cli (vlib_main_t * vm,
451 unformat_input_t * input, vlib_cli_command_t * cmd)
453 qos_egress_map_id_t map_id;
458 vnm = vnet_get_main ();
463 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
465 if (unformat (input, "id %d", &map_id))
467 else if (unformat (input, "disable"))
469 else if (unformat (input, "%U", unformat_qos_source, &qs))
471 else if (unformat (input, "%U",
472 unformat_vnet_sw_interface, vnm, &sw_if_index))
478 if (~0 == sw_if_index)
479 return clib_error_return (0, "interface must be specified");
481 return clib_error_return (0, "output location must be specified");
484 rv = qos_mark_enable (sw_if_index, qs, map_id);
486 rv = qos_mark_disable (sw_if_index, qs);
491 return clib_error_return (0, "Failed to map interface");
495 * Apply a QoS egress mapping table to an interface for QoS marking packets
496 * at the given output protocol.
499 * @cliexcmd{qos egress interface GigEthernet0/9/0 id 0 output ip}
502 VLIB_CLI_COMMAND (qos_egress_map_interface_command, static) = {
504 .short_help = "qos mark <SOURCE> <INTERFACE> id <MAP>",
505 .function = qos_mark_cli,
511 * fd.io coding-style-patch-verification: ON
514 * eval: (c-set-style "gnu")