ipsec: IPSec protection for multi-point tunnel interfaces
[vpp.git] / src / vnet / qos / qos_mark_node.c
1 /*
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
21 extern index_t *qos_mark_configs[QOS_N_SOURCES];
22
23 always_inline qos_egress_map_t *
24 qos_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  */
35 typedef 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
43 static inline uword
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)
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 */
160 static u8 *
161 format_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
173 VLIB_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
180 VLIB_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
187 VLIB_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
194 VLIB_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
201 VLIB_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
208 VLIB_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
215 /* *INDENT-OFF* */
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,
221
222   .n_errors = 0,
223   .n_next_nodes = 1,
224
225   .next_nodes = {
226     [0] = "ip4-drop",
227   },
228 };
229
230 VNET_FEATURE_INIT (ip4_qos_mark_node, static) = {
231     .arc_name = "ip4-output",
232     .node_name = "ip4-qos-mark",
233 };
234
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,
240
241   .n_errors = 0,
242   .n_next_nodes = 1,
243
244   .next_nodes = {
245     [0] = "ip6-drop",
246   },
247 };
248
249 VNET_FEATURE_INIT (ip6_qos_mark_node, static) = {
250     .arc_name = "ip6-output",
251     .node_name = "ip6-qos-mark",
252 };
253
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,
259
260   .n_errors = 0,
261   .n_next_nodes = 1,
262
263   .next_nodes = {
264     [0] = "mpls-drop",
265   },
266 };
267
268 VNET_FEATURE_INIT (mpls_qos_mark_node, static) = {
269     .arc_name = "mpls-output",
270     .node_name = "mpls-qos-mark",
271 };
272
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,
278
279   .n_errors = 0,
280   .n_next_nodes = 1,
281
282   .next_nodes = {
283     [0] = "error-drop",
284   },
285 };
286
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"),
291 };
292
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,
298
299   .n_errors = 0,
300   .n_next_nodes = 1,
301
302   .next_nodes = {
303     [0] = "error-drop",
304   },
305 };
306
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"),
311 };
312
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,
318
319   .n_errors = 0,
320   .n_next_nodes = 1,
321
322   .next_nodes = {
323     [0] = "error-drop",
324   },
325 };
326
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"),
331 };
332
333 /* *INDENT-ON* */
334
335 /*
336  * fd.io coding-style-patch-verification: ON
337  *
338  * Local Variables:
339  * eval: (c-set-style "gnu")
340  * End:
341  */