2 * Copyright (c) 2019 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>
21 extern index_t *qos_mark_configs[QOS_N_SOURCES];
23 always_inline qos_egress_map_t *
24 qos_egress_map_interface (u32 sw_if_index, qos_source_t output_source)
26 ASSERT (vec_len (qos_mark_configs[output_source]) > sw_if_index);
28 return pool_elt_at_index (qem_pool,
29 qos_mark_configs[output_source][sw_if_index]);
33 * per-packet trace data
35 typedef struct qos_mark_trace_t_
37 /* per-pkt trace data */
44 qos_mark_inline (vlib_main_t * vm,
45 vlib_node_runtime_t * node,
46 vlib_frame_t * frame, qos_source_t output_source, int is_ip6)
48 u32 n_left_from, *from, *to_next, next_index;
51 n_left_from = frame->n_vectors;
52 from = vlib_frame_vector_args (frame);
54 while (n_left_from > 0)
58 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
60 while (n_left_from > 0 && n_left_to_next > 0)
62 qos_source_t input_source0;
63 ethernet_vlan_header_t *vlan0;
64 u32 sw_if_index0, next0, bi0;
65 qos_egress_map_t *qem0;
81 b0 = vlib_get_buffer (vm, bi0);
82 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
83 input_source0 = vnet_buffer2 (b0)->qos.source;
85 qem0 = qos_egress_map_interface (sw_if_index0, output_source);
86 qos0 = qem0->qem_output[input_source0][vnet_buffer2 (b0)->qos.bits];
88 if (PREDICT_TRUE (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID))
90 /* there is a source of QoS recording for this packet */
91 if (QOS_SOURCE_IP == output_source)
95 ip6_0 = (vlib_buffer_get_current (b0) +
96 vnet_buffer (b0)->ip.save_rewrite_length);
98 ip6_set_traffic_class_network_order (ip6_0, qos0);
102 ip4_0 = (vlib_buffer_get_current (b0) +
103 vnet_buffer (b0)->ip.save_rewrite_length);
104 if (PREDICT_FALSE (qos0 != ip4_0->tos))
107 ip4_0->checksum = ip4_header_checksum (ip4_0);
111 else if (QOS_SOURCE_MPLS == output_source)
113 mpls_bytes_0 = (vlib_buffer_get_current (b0) +
114 vnet_buffer (b0)->mpls.save_rewrite_length);
116 /* apply to all the labels in the stack */
119 /* clear out the old COS bts */
120 mpls_bytes_0[2] &= 0xf1;
121 /* OR in 3 bits of the mapped value */
122 mpls_bytes_0[2] |= (qos0 & 0x7) << 1;
123 eos0 = mpls_bytes_0[2] & 0x1;
128 else if (QOS_SOURCE_VLAN == output_source)
130 vlan0 = (vlib_buffer_get_current (b0) +
131 sizeof (ethernet_header_t));
133 ethernet_vlan_header_set_priority_net_order (vlan0, qos0);
136 vnet_feature_next (&next0, b0);
138 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
140 qos_mark_trace_t *t =
141 vlib_add_trace (vm, node, b0, sizeof (*t));
143 t->input = input_source0;
144 t->used = (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID);
147 /* verify speculative enqueue, maybe switch current next frame */
148 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
149 to_next, n_left_to_next,
153 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
156 return frame->n_vectors;
159 /* packet trace format function */
161 format_qos_mark_trace (u8 * s, va_list * args)
163 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
164 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
165 qos_mark_trace_t *t = va_arg (*args, qos_mark_trace_t *);
167 s = format (s, "source:%U qos:%d used:%s",
168 format_qos_source, t->input, t->bits, (t->used ? "yes" : "no"));
173 VLIB_NODE_FN (ip4_qos_mark_node) (vlib_main_t * vm,
174 vlib_node_runtime_t * node,
175 vlib_frame_t * frame)
177 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 0));
180 VLIB_NODE_FN (ip6_qos_mark_node) (vlib_main_t * vm,
181 vlib_node_runtime_t * node,
182 vlib_frame_t * frame)
184 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 1));
187 VLIB_NODE_FN (mpls_qos_mark_node) (vlib_main_t * vm,
188 vlib_node_runtime_t * node,
189 vlib_frame_t * frame)
191 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_MPLS, 0));
194 VLIB_NODE_FN (vlan_mpls_qos_mark_node) (vlib_main_t * vm,
195 vlib_node_runtime_t * node,
196 vlib_frame_t * frame)
198 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
201 VLIB_NODE_FN (vlan_ip4_qos_mark_node) (vlib_main_t * vm,
202 vlib_node_runtime_t * node,
203 vlib_frame_t * frame)
205 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
208 VLIB_NODE_FN (vlan_ip6_qos_mark_node) (vlib_main_t * vm,
209 vlib_node_runtime_t * node,
210 vlib_frame_t * frame)
212 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
216 VLIB_REGISTER_NODE (ip4_qos_mark_node) = {
217 .name = "ip4-qos-mark",
218 .vector_size = sizeof (u32),
219 .format_trace = format_qos_mark_trace,
220 .type = VLIB_NODE_TYPE_INTERNAL,
230 VNET_FEATURE_INIT (ip4_qos_mark_node, static) = {
231 .arc_name = "ip4-output",
232 .node_name = "ip4-qos-mark",
235 VLIB_REGISTER_NODE (ip6_qos_mark_node) = {
236 .name = "ip6-qos-mark",
237 .vector_size = sizeof (u32),
238 .format_trace = format_qos_mark_trace,
239 .type = VLIB_NODE_TYPE_INTERNAL,
249 VNET_FEATURE_INIT (ip6_qos_mark_node, static) = {
250 .arc_name = "ip6-output",
251 .node_name = "ip6-qos-mark",
254 VLIB_REGISTER_NODE (mpls_qos_mark_node) = {
255 .name = "mpls-qos-mark",
256 .vector_size = sizeof (u32),
257 .format_trace = format_qos_mark_trace,
258 .type = VLIB_NODE_TYPE_INTERNAL,
268 VNET_FEATURE_INIT (mpls_qos_mark_node, static) = {
269 .arc_name = "mpls-output",
270 .node_name = "mpls-qos-mark",
273 VLIB_REGISTER_NODE (vlan_ip4_qos_mark_node) = {
274 .name = "vlan-ip4-qos-mark",
275 .vector_size = sizeof (u32),
276 .format_trace = format_qos_mark_trace,
277 .type = VLIB_NODE_TYPE_INTERNAL,
287 VNET_FEATURE_INIT (vlan_ip4_qos_mark_node, static) = {
288 .arc_name = "ip4-output",
289 .node_name = "vlan-ip4-qos-mark",
290 .runs_after = VNET_FEATURES ("ip4-qos-mark"),
293 VLIB_REGISTER_NODE (vlan_ip6_qos_mark_node) = {
294 .name = "vlan-ip6-qos-mark",
295 .vector_size = sizeof (u32),
296 .format_trace = format_qos_mark_trace,
297 .type = VLIB_NODE_TYPE_INTERNAL,
307 VNET_FEATURE_INIT (vlan_ip6_qos_mark_node, static) = {
308 .arc_name = "ip6-output",
309 .node_name = "vlan-ip6-qos-mark",
310 .runs_after = VNET_FEATURES ("ip6-qos-mark"),
313 VLIB_REGISTER_NODE (vlan_mpls_qos_mark_node) = {
314 .name = "vlan-mpls-qos-mark",
315 .vector_size = sizeof (u32),
316 .format_trace = format_qos_mark_trace,
317 .type = VLIB_NODE_TYPE_INTERNAL,
327 VNET_FEATURE_INIT (vlan_mpls_qos_mark_node, static) = {
328 .arc_name = "mpls-output",
329 .node_name = "vlan-mpls-qos-mark",
330 .runs_after = VNET_FEATURES ("mpls-qos-mark"),
336 * fd.io coding-style-patch-verification: ON
339 * eval: (c-set-style "gnu")