blob: 16a487aede83a1fd3a5cf6cf20fffd596e7587ad [file] [log] [blame]
Filip Tehlar0bddf7e2019-03-04 08:14:07 -08001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
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>
20
21extern index_t *qos_mark_configs[QOS_N_SOURCES];
22
23always_inline qos_egress_map_t *
24qos_egress_map_interface (u32 sw_if_index, qos_source_t output_source)
25{
26 ASSERT (vec_len (qos_mark_configs[output_source]) > sw_if_index);
27
28 return pool_elt_at_index (qem_pool,
29 qos_mark_configs[output_source][sw_if_index]);
30}
31
32/**
33 * per-packet trace data
34 */
35typedef struct qos_mark_trace_t_
36{
37 /* per-pkt trace data */
38 qos_bits_t bits;
39 qos_source_t input;
40 u32 used;
41} qos_mark_trace_t;
42
43static inline uword
44qos_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)
47{
48 u32 n_left_from, *from, *to_next, next_index;
49
50 next_index = 0;
51 n_left_from = frame->n_vectors;
52 from = vlib_frame_vector_args (frame);
53
54 while (n_left_from > 0)
55 {
56 u32 n_left_to_next;
57
58 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
59
60 while (n_left_from > 0 && n_left_to_next > 0)
61 {
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;
66 ip4_header_t *ip4_0;
67 ip6_header_t *ip6_0;
68 vlib_buffer_t *b0;
69 qos_bits_t qos0;
70 u8 *mpls_bytes_0;
71 u8 eos0;
72
73 next0 = 0;
74 bi0 = from[0];
75 to_next[0] = bi0;
76 from += 1;
77 to_next += 1;
78 n_left_from -= 1;
79 n_left_to_next -= 1;
80
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;
84
85 qem0 = qos_egress_map_interface (sw_if_index0, output_source);
86 qos0 = qem0->qem_output[input_source0][vnet_buffer2 (b0)->qos.bits];
87
88 if (PREDICT_TRUE (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID))
89 {
90 /* there is a source of QoS recording for this packet */
91 if (QOS_SOURCE_IP == output_source)
92 {
93 if (is_ip6)
94 {
95 ip6_0 = (vlib_buffer_get_current (b0) +
96 vnet_buffer (b0)->ip.save_rewrite_length);
97
98 ip6_set_traffic_class_network_order (ip6_0, qos0);
99 }
100 else
101 {
102 ip4_0 = (vlib_buffer_get_current (b0) +
103 vnet_buffer (b0)->ip.save_rewrite_length);
104 if (PREDICT_FALSE (qos0 != ip4_0->tos))
105 {
106 ip4_0->tos = qos0;
107 ip4_0->checksum = ip4_header_checksum (ip4_0);
108 }
109 }
110 }
111 else if (QOS_SOURCE_MPLS == output_source)
112 {
113 mpls_bytes_0 = (vlib_buffer_get_current (b0) +
114 vnet_buffer (b0)->mpls.save_rewrite_length);
115
116 /* apply to all the labels in the stack */
117 do
118 {
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;
124 mpls_bytes_0 += 4;
125 }
126 while (!eos0);
127 }
128 else if (QOS_SOURCE_VLAN == output_source)
129 {
130 vlan0 = (vlib_buffer_get_current (b0) +
131 sizeof (ethernet_header_t));
132
133 ethernet_vlan_header_set_priority_net_order (vlan0, qos0);
134 }
135 }
136 vnet_feature_next (&next0, b0);
137
138 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
139 {
140 qos_mark_trace_t *t =
141 vlib_add_trace (vm, node, b0, sizeof (*t));
142 t->bits = qos0;
143 t->input = input_source0;
144 t->used = (b0->flags & VNET_BUFFER_F_QOS_DATA_VALID);
145 }
146
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,
150 bi0, next0);
151 }
152
153 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
154 }
155
156 return frame->n_vectors;
157}
158
159/* packet trace format function */
160static u8 *
161format_qos_mark_trace (u8 * s, va_list * args)
162{
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 *);
166
167 s = format (s, "source:%U qos:%d used:%s",
168 format_qos_source, t->input, t->bits, (t->used ? "yes" : "no"));
169
170 return s;
171}
172
173VLIB_NODE_FN (ip4_qos_mark_node) (vlib_main_t * vm,
174 vlib_node_runtime_t * node,
175 vlib_frame_t * frame)
176{
177 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 0));
178}
179
180VLIB_NODE_FN (ip6_qos_mark_node) (vlib_main_t * vm,
181 vlib_node_runtime_t * node,
182 vlib_frame_t * frame)
183{
184 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_IP, 1));
185}
186
187VLIB_NODE_FN (mpls_qos_mark_node) (vlib_main_t * vm,
188 vlib_node_runtime_t * node,
189 vlib_frame_t * frame)
190{
191 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_MPLS, 0));
192}
193
194VLIB_NODE_FN (vlan_mpls_qos_mark_node) (vlib_main_t * vm,
195 vlib_node_runtime_t * node,
196 vlib_frame_t * frame)
197{
198 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
199}
200
201VLIB_NODE_FN (vlan_ip4_qos_mark_node) (vlib_main_t * vm,
202 vlib_node_runtime_t * node,
203 vlib_frame_t * frame)
204{
205 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
206}
207
208VLIB_NODE_FN (vlan_ip6_qos_mark_node) (vlib_main_t * vm,
209 vlib_node_runtime_t * node,
210 vlib_frame_t * frame)
211{
212 return (qos_mark_inline (vm, node, frame, QOS_SOURCE_VLAN, 0));
213}
214
Filip Tehlar0bddf7e2019-03-04 08:14:07 -0800215VLIB_REGISTER_NODE (ip4_qos_mark_node) = {
216 .name = "ip4-qos-mark",
217 .vector_size = sizeof (u32),
218 .format_trace = format_qos_mark_trace,
219 .type = VLIB_NODE_TYPE_INTERNAL,
220
221 .n_errors = 0,
222 .n_next_nodes = 1,
223
224 .next_nodes = {
225 [0] = "ip4-drop",
226 },
227};
228
229VNET_FEATURE_INIT (ip4_qos_mark_node, static) = {
230 .arc_name = "ip4-output",
231 .node_name = "ip4-qos-mark",
232};
233
234VLIB_REGISTER_NODE (ip6_qos_mark_node) = {
235 .name = "ip6-qos-mark",
236 .vector_size = sizeof (u32),
237 .format_trace = format_qos_mark_trace,
238 .type = VLIB_NODE_TYPE_INTERNAL,
239
240 .n_errors = 0,
241 .n_next_nodes = 1,
242
243 .next_nodes = {
244 [0] = "ip6-drop",
245 },
246};
247
248VNET_FEATURE_INIT (ip6_qos_mark_node, static) = {
249 .arc_name = "ip6-output",
250 .node_name = "ip6-qos-mark",
251};
252
253VLIB_REGISTER_NODE (mpls_qos_mark_node) = {
254 .name = "mpls-qos-mark",
255 .vector_size = sizeof (u32),
256 .format_trace = format_qos_mark_trace,
257 .type = VLIB_NODE_TYPE_INTERNAL,
258
259 .n_errors = 0,
260 .n_next_nodes = 1,
261
262 .next_nodes = {
263 [0] = "mpls-drop",
264 },
265};
266
267VNET_FEATURE_INIT (mpls_qos_mark_node, static) = {
268 .arc_name = "mpls-output",
269 .node_name = "mpls-qos-mark",
270};
271
272VLIB_REGISTER_NODE (vlan_ip4_qos_mark_node) = {
273 .name = "vlan-ip4-qos-mark",
274 .vector_size = sizeof (u32),
275 .format_trace = format_qos_mark_trace,
276 .type = VLIB_NODE_TYPE_INTERNAL,
277
278 .n_errors = 0,
279 .n_next_nodes = 1,
280
281 .next_nodes = {
282 [0] = "error-drop",
283 },
284};
285
286VNET_FEATURE_INIT (vlan_ip4_qos_mark_node, static) = {
287 .arc_name = "ip4-output",
288 .node_name = "vlan-ip4-qos-mark",
289 .runs_after = VNET_FEATURES ("ip4-qos-mark"),
290};
291
292VLIB_REGISTER_NODE (vlan_ip6_qos_mark_node) = {
293 .name = "vlan-ip6-qos-mark",
294 .vector_size = sizeof (u32),
295 .format_trace = format_qos_mark_trace,
296 .type = VLIB_NODE_TYPE_INTERNAL,
297
298 .n_errors = 0,
299 .n_next_nodes = 1,
300
301 .next_nodes = {
302 [0] = "error-drop",
303 },
304};
305
306VNET_FEATURE_INIT (vlan_ip6_qos_mark_node, static) = {
307 .arc_name = "ip6-output",
308 .node_name = "vlan-ip6-qos-mark",
309 .runs_after = VNET_FEATURES ("ip6-qos-mark"),
310};
311
312VLIB_REGISTER_NODE (vlan_mpls_qos_mark_node) = {
313 .name = "vlan-mpls-qos-mark",
314 .vector_size = sizeof (u32),
315 .format_trace = format_qos_mark_trace,
316 .type = VLIB_NODE_TYPE_INTERNAL,
317
318 .n_errors = 0,
319 .n_next_nodes = 1,
320
321 .next_nodes = {
322 [0] = "error-drop",
323 },
324};
325
326VNET_FEATURE_INIT (vlan_mpls_qos_mark_node, static) = {
327 .arc_name = "mpls-output",
328 .node_name = "vlan-mpls-qos-mark",
329 .runs_after = VNET_FEATURES ("mpls-qos-mark"),
330};
331
Filip Tehlar0bddf7e2019-03-04 08:14:07 -0800332
333/*
334 * fd.io coding-style-patch-verification: ON
335 *
336 * Local Variables:
337 * eval: (c-set-style "gnu")
338 * End:
339 */