Tests: Example duplicate code refactoring.
[vpp.git] / src / plugins / gbp / gbp_sclass_node.c
1 /*
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:
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 <plugins/gbp/gbp_sclass.h>
17 #include <vnet/l2/l2_input.h>
18 #include <vnet/l2/l2_output.h>
19
20 #define foreach_gbp_sclass                      \
21   _(DROP,    "drop")
22
23 typedef enum
24 {
25 #define _(sym,str) GBP_SCLASS_NEXT_##sym,
26   foreach_gbp_sclass
27 #undef _
28     GBP_SCLASS_N_NEXT,
29 } gbp_sclass_next_t;
30
31 typedef struct gbp_sclass_trace_t_
32 {
33   /* per-pkt trace data */
34   u32 epg;
35   u32 sclass;
36 } gbp_sclass_trace_t;
37
38 static_always_inline uword
39 gbp_sclass_inline (vlib_main_t * vm,
40                    vlib_node_runtime_t * node,
41                    vlib_frame_t * frame, int is_id_2_sclass, int is_l2)
42 {
43   u32 n_left_from, *from, *to_next, next_index;
44   gbp_sclass_main_t *glm;
45
46   glm = &gbp_sclass_main;
47   next_index = 0;
48   n_left_from = frame->n_vectors;
49   from = vlib_frame_vector_args (frame);
50
51   while (n_left_from > 0)
52     {
53       u32 n_left_to_next;
54
55       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
56
57       while (n_left_from > 0 && n_left_to_next > 0)
58         {
59           gbp_sclass_next_t next0;
60           vlib_buffer_t *b0;
61           epg_id_t epg0;
62           u16 sclass0;
63           u32 bi0;
64
65           next0 = GBP_SCLASS_NEXT_DROP;
66           bi0 = from[0];
67           to_next[0] = bi0;
68           from += 1;
69           to_next += 1;
70           n_left_from -= 1;
71           n_left_to_next -= 1;
72
73           b0 = vlib_get_buffer (vm, bi0);
74
75           if (is_id_2_sclass)
76             {
77               // output direction - convert from the SRC-EPD to the sclass
78               gbp_endpoint_group_t *gg;
79
80               epg0 = vnet_buffer2 (b0)->gbp.src_epg;
81               gg = gbp_epg_get (epg0);
82
83               if (NULL != gg)
84                 {
85                   sclass0 = vnet_buffer2 (b0)->gbp.sclass = gg->gg_sclass;
86                   if (is_l2)
87                     next0 =
88                       vnet_l2_feature_next (b0, glm->gel_l2_output_feat_next,
89                                             L2OUTPUT_FEAT_GBP_ID_2_SCLASS);
90                   else
91                     vnet_feature_next (&next0, b0);
92                 }
93               else
94                 sclass0 = 0;
95             }
96           else
97             {
98               /* input direction - convert from the sclass to the SRC-EGD */
99               sclass0 = vnet_buffer2 (b0)->gbp.sclass;
100               vnet_buffer2 (b0)->gbp.src_epg =
101                 gbp_epg_sclass_2_id (vnet_buffer2 (b0)->gbp.sclass);
102               epg0 = vnet_buffer2 (b0)->gbp.src_epg;
103
104               if (EPG_INVALID != epg0)
105                 {
106                   if (is_l2)
107                     next0 =
108                       vnet_l2_feature_next (b0, glm->gel_l2_input_feat_next,
109                                             L2INPUT_FEAT_GBP_SCLASS_2_ID);
110                   else
111                     vnet_feature_next (&next0, b0);
112                 }
113             }
114
115           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
116             {
117               gbp_sclass_trace_t *t =
118                 vlib_add_trace (vm, node, b0, sizeof (*t));
119               t->epg = epg0;
120               t->sclass = sclass0;
121             }
122
123           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
124                                            to_next, n_left_to_next,
125                                            bi0, next0);
126         }
127
128       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
129     }
130
131   return frame->n_vectors;
132 }
133
134 VLIB_NODE_FN (l2_gbp_id_2_sclass_node) (vlib_main_t * vm,
135                                         vlib_node_runtime_t * node,
136                                         vlib_frame_t * frame)
137 {
138   return (gbp_sclass_inline (vm, node, frame, 1, 1));
139 }
140
141 VLIB_NODE_FN (l2_gbp_sclass_2_id_node) (vlib_main_t * vm,
142                                         vlib_node_runtime_t * node,
143                                         vlib_frame_t * frame)
144 {
145   return (gbp_sclass_inline (vm, node, frame, 0, 1));
146 }
147
148 VLIB_NODE_FN (ip4_gbp_id_2_sclass_node) (vlib_main_t * vm,
149                                          vlib_node_runtime_t * node,
150                                          vlib_frame_t * frame)
151 {
152   return (gbp_sclass_inline (vm, node, frame, 1, 0));
153 }
154
155 VLIB_NODE_FN (ip4_gbp_sclass_2_id_node) (vlib_main_t * vm,
156                                          vlib_node_runtime_t * node,
157                                          vlib_frame_t * frame)
158 {
159   return (gbp_sclass_inline (vm, node, frame, 0, 0));
160 }
161
162 VLIB_NODE_FN (ip6_gbp_id_2_sclass_node) (vlib_main_t * vm,
163                                          vlib_node_runtime_t * node,
164                                          vlib_frame_t * frame)
165 {
166   return (gbp_sclass_inline (vm, node, frame, 1, 0));
167 }
168
169 VLIB_NODE_FN (ip6_gbp_sclass_2_id_node) (vlib_main_t * vm,
170                                          vlib_node_runtime_t * node,
171                                          vlib_frame_t * frame)
172 {
173   return (gbp_sclass_inline (vm, node, frame, 0, 0));
174 }
175
176 /* packet trace format function */
177 static u8 *
178 format_gbp_sclass_trace (u8 * s, va_list * args)
179 {
180   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
181   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
182   gbp_sclass_trace_t *t = va_arg (*args, gbp_sclass_trace_t *);
183
184   s = format (s, "epg:%d sclass:%d", t->epg, t->sclass);
185
186   return s;
187 }
188
189 /* *INDENT-OFF* */
190 VLIB_REGISTER_NODE (l2_gbp_id_2_sclass_node) = {
191   .name = "l2-gbp-id-2-sclass",
192   .vector_size = sizeof (u32),
193   .format_trace = format_gbp_sclass_trace,
194   .type = VLIB_NODE_TYPE_INTERNAL,
195
196   .n_next_nodes = GBP_SCLASS_N_NEXT,
197
198   .next_nodes = {
199     [GBP_SCLASS_NEXT_DROP] = "error-drop",
200   },
201 };
202 VLIB_REGISTER_NODE (l2_gbp_sclass_2_id_node) = {
203   .name = "l2-gbp-sclass-2-id",
204   .vector_size = sizeof (u32),
205   .format_trace = format_gbp_sclass_trace,
206   .type = VLIB_NODE_TYPE_INTERNAL,
207
208   .n_next_nodes = GBP_SCLASS_N_NEXT,
209
210   .next_nodes = {
211     [GBP_SCLASS_NEXT_DROP] = "error-drop",
212   },
213 };
214
215 VLIB_REGISTER_NODE (ip4_gbp_id_2_sclass_node) = {
216   .name = "ip4-gbp-id-2-sclass",
217   .vector_size = sizeof (u32),
218   .format_trace = format_gbp_sclass_trace,
219   .type = VLIB_NODE_TYPE_INTERNAL,
220
221   .n_next_nodes = GBP_SCLASS_N_NEXT,
222
223   .next_nodes = {
224     [GBP_SCLASS_NEXT_DROP] = "error-drop",
225   },
226 };
227 VLIB_REGISTER_NODE (ip4_gbp_sclass_2_id_node) = {
228   .name = "ip4-gbp-sclass-2-id",
229   .vector_size = sizeof (u32),
230   .format_trace = format_gbp_sclass_trace,
231   .type = VLIB_NODE_TYPE_INTERNAL,
232
233   .n_next_nodes = GBP_SCLASS_N_NEXT,
234
235   .next_nodes = {
236     [GBP_SCLASS_NEXT_DROP] = "error-drop",
237   },
238 };
239
240 VLIB_REGISTER_NODE (ip6_gbp_id_2_sclass_node) = {
241   .name = "ip6-gbp-id-2-sclass",
242   .vector_size = sizeof (u32),
243   .format_trace = format_gbp_sclass_trace,
244   .type = VLIB_NODE_TYPE_INTERNAL,
245
246   .n_next_nodes = GBP_SCLASS_N_NEXT,
247
248   .next_nodes = {
249     [GBP_SCLASS_NEXT_DROP] = "error-drop",
250   },
251 };
252 VLIB_REGISTER_NODE (ip6_gbp_sclass_2_id_node) = {
253   .name = "ip6-gbp-sclass-2-id",
254   .vector_size = sizeof (u32),
255   .format_trace = format_gbp_sclass_trace,
256   .type = VLIB_NODE_TYPE_INTERNAL,
257
258   .n_next_nodes = GBP_SCLASS_N_NEXT,
259
260   .next_nodes = {
261     [GBP_SCLASS_NEXT_DROP] = "error-drop",
262   },
263 };
264
265
266 VNET_FEATURE_INIT (ip4_gbp_sclass_2_id_feat, static) =
267 {
268   .arc_name = "ip4-unicast",
269   .node_name = "ip4-gbp-sclass-2-id",
270   .runs_before = VNET_FEATURES ("gbp-learn-ip4"),
271 };
272 VNET_FEATURE_INIT (ip6_gbp_sclass_2_id_feat, static) =
273 {
274   .arc_name = "ip6-unicast",
275   .node_name = "ip6-gbp-sclass-2-id",
276   .runs_before = VNET_FEATURES ("gbp-learn-ip6"),
277 };
278 VNET_FEATURE_INIT (ip4_gbp_id_2_sclass_feat, static) =
279 {
280   .arc_name = "ip4-output",
281   .node_name = "ip4-gbp-id-2-sclass",
282 };
283 VNET_FEATURE_INIT (ip6_gbp_id_2_sclass_feat, static) =
284 {
285   .arc_name = "ip6-output",
286   .node_name = "ip6-gbp-id-2-sclass",
287 };
288 /* *INDENT-ON* */
289
290 /*
291  * fd.io coding-style-patch-verification: ON
292  *
293  * Local Variables:
294  * eval: (c-set-style "gnu")
295  * End:
296  */