L2 BVI/FIB: Update L2 FIB table when BVI's MAC changes
[vpp.git] / src / plugins / gbp / gbp_classify.c
1 /*
2  * gbp.h : Group Based Policy
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <plugins/gbp/gbp.h>
19 #include <vnet/l2/l2_input.h>
20 #include <vnet/l2/feat_bitmap.h>
21
22 typedef enum gbp_src_classify_type_t_
23 {
24   GBP_SRC_CLASSIFY_NULL,
25   GBP_SRC_CLASSIFY_PORT,
26 } gbp_src_classify_type_t;
27
28 #define GBP_SRC_N_CLASSIFY (GBP_SRC_CLASSIFY_PORT + 1)
29
30 /**
31  * Grouping of global data for the GBP source EPG classification feature
32  */
33 typedef struct gbp_src_classify_main_t_
34 {
35   /**
36    * Next nodes for L2 output features
37    */
38   u32 l2_input_feat_next[GBP_SRC_N_CLASSIFY][32];
39 } gbp_src_classify_main_t;
40
41 static gbp_src_classify_main_t gbp_src_classify_main;
42
43 /**
44  * per-packet trace data
45  */
46 typedef struct gbp_classify_trace_t_
47 {
48   /* per-pkt trace data */
49   epg_id_t src_epg;
50 } gbp_classify_trace_t;
51
52 /*
53  * determine the SRC EPG form the input port
54  */
55 always_inline uword
56 gbp_classify_inline (vlib_main_t * vm,
57                      vlib_node_runtime_t * node,
58                      vlib_frame_t * frame,
59                      gbp_src_classify_type_t type, u8 is_l3)
60 {
61   gbp_src_classify_main_t *gscm = &gbp_src_classify_main;
62   u32 n_left_from, *from, *to_next;
63   u32 next_index;
64
65   next_index = 0;
66   n_left_from = frame->n_vectors;
67   from = vlib_frame_vector_args (frame);
68
69   while (n_left_from > 0)
70     {
71       u32 n_left_to_next;
72
73       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
74
75       while (n_left_from > 0 && n_left_to_next > 0)
76         {
77           u32 next0, bi0, src_epg, sw_if_index0;
78           vlib_buffer_t *b0;
79
80           bi0 = from[0];
81           to_next[0] = bi0;
82           from += 1;
83           to_next += 1;
84           n_left_from -= 1;
85           n_left_to_next -= 1;
86
87           b0 = vlib_get_buffer (vm, bi0);
88
89           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
90
91           if (GBP_SRC_CLASSIFY_NULL == type)
92             {
93               src_epg = ~0;
94               next0 =
95                 vnet_l2_feature_next (b0, gscm->l2_input_feat_next[type],
96                                       L2INPUT_FEAT_GBP_NULL_CLASSIFY);
97             }
98           else
99             {
100               src_epg = gbp_port_to_epg (sw_if_index0);
101               if (is_l3)
102                 {
103                   /*
104                    * Go straight to looukp, do not pass go, do not collect $200
105                    */
106                   next0 = 0;
107                 }
108               else
109                 {
110                   next0 =
111                     vnet_l2_feature_next (b0, gscm->l2_input_feat_next[type],
112                                           L2INPUT_FEAT_GBP_SRC_CLASSIFY);
113                 }
114             }
115
116           vnet_buffer2 (b0)->gbp.src_epg = src_epg;
117
118           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
119             {
120               gbp_classify_trace_t *t =
121                 vlib_add_trace (vm, node, b0, sizeof (*t));
122               t->src_epg = src_epg;
123             }
124
125           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
126                                            to_next, n_left_to_next,
127                                            bi0, next0);
128         }
129
130       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
131     }
132
133   return frame->n_vectors;
134 }
135
136 static uword
137 gbp_src_classify (vlib_main_t * vm,
138                   vlib_node_runtime_t * node, vlib_frame_t * frame)
139 {
140   return (gbp_classify_inline (vm, node, frame, GBP_SRC_CLASSIFY_PORT, 0));
141 }
142
143 static uword
144 gbp_null_classify (vlib_main_t * vm,
145                    vlib_node_runtime_t * node, vlib_frame_t * frame)
146 {
147   return (gbp_classify_inline (vm, node, frame, GBP_SRC_CLASSIFY_NULL, 0));
148 }
149
150 static uword
151 gbp_ip4_src_classify (vlib_main_t * vm,
152                       vlib_node_runtime_t * node, vlib_frame_t * frame)
153 {
154   return (gbp_classify_inline (vm, node, frame, 0, 1));
155 }
156
157 static uword
158 gbp_ip6_src_classify (vlib_main_t * vm,
159                       vlib_node_runtime_t * node, vlib_frame_t * frame)
160 {
161   return (gbp_classify_inline (vm, node, frame, 0, 1));
162 }
163
164
165 /* packet trace format function */
166 static u8 *
167 format_gbp_classify_trace (u8 * s, va_list * args)
168 {
169   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
170   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
171   gbp_classify_trace_t *t = va_arg (*args, gbp_classify_trace_t *);
172
173   s = format (s, "src-epg:%d", t->src_epg);
174
175   return s;
176 }
177
178 /* *INDENT-OFF* */
179 VLIB_REGISTER_NODE (gbp_null_classify_node) = {
180   .function = gbp_null_classify,
181   .name = "gbp-null-classify",
182   .vector_size = sizeof (u32),
183   .format_trace = format_gbp_classify_trace,
184   .type = VLIB_NODE_TYPE_INTERNAL,
185
186   .n_errors = 0,
187   .n_next_nodes = 0,
188 };
189
190 VLIB_NODE_FUNCTION_MULTIARCH (gbp_null_classify_node, gbp_null_classify);
191
192 VLIB_REGISTER_NODE (gbp_src_classify_node) = {
193   .function = gbp_src_classify,
194   .name = "gbp-src-classify",
195   .vector_size = sizeof (u32),
196   .format_trace = format_gbp_classify_trace,
197   .type = VLIB_NODE_TYPE_INTERNAL,
198
199   .n_errors = 0,
200   .n_next_nodes = 0,
201 };
202
203 VLIB_NODE_FUNCTION_MULTIARCH (gbp_src_classify_node, gbp_src_classify);
204
205 VLIB_REGISTER_NODE (gbp_ip4_src_classify_node) = {
206   .function = gbp_ip4_src_classify,
207   .name = "ip4-gbp-src-classify",
208   .vector_size = sizeof (u32),
209   .format_trace = format_gbp_classify_trace,
210   .type = VLIB_NODE_TYPE_INTERNAL,
211
212   .n_errors = 0,
213   .n_next_nodes = 1,
214   .next_nodes = {
215     [0] = "ip4-lookup"
216   },
217 };
218
219 VLIB_NODE_FUNCTION_MULTIARCH (gbp_ip4_src_classify_node, gbp_ip4_src_classify);
220
221 VLIB_REGISTER_NODE (gbp_ip6_src_classify_node) = {
222   .function = gbp_ip6_src_classify,
223   .name = "ip6-gbp-src-classify",
224   .vector_size = sizeof (u32),
225   .format_trace = format_gbp_classify_trace,
226   .type = VLIB_NODE_TYPE_INTERNAL,
227
228   .n_errors = 0,
229   .n_next_nodes = 1,
230   .next_nodes = {
231     [0] = "ip6-lookup"
232   },
233 };
234
235 VLIB_NODE_FUNCTION_MULTIARCH (gbp_ip6_src_classify_node, gbp_ip6_src_classify);
236
237 VNET_FEATURE_INIT (gbp_ip4_src_classify_feat_node, static) =
238 {
239   .arc_name = "ip4-unicast",
240   .node_name = "ip4-gbp-src-classify",
241   .runs_before = VNET_FEATURES ("nat44-out2in"),
242 };
243 VNET_FEATURE_INIT (gbp_ip6_src_classify_feat_node, static) =
244 {
245   .arc_name = "ip6-unicast",
246   .node_name = "ip6-gbp-src-classify",
247   .runs_before = VNET_FEATURES ("nat66-out2in"),
248 };
249
250 static clib_error_t *
251 gbp_src_classify_init (vlib_main_t * vm)
252 {
253   gbp_src_classify_main_t *em = &gbp_src_classify_main;
254
255   /* Initialize the feature next-node indexes */
256   feat_bitmap_init_next_nodes (vm,
257                                gbp_src_classify_node.index,
258                                L2INPUT_N_FEAT,
259                                l2input_get_feat_names (),
260                                em->l2_input_feat_next[GBP_SRC_CLASSIFY_NULL]);
261   feat_bitmap_init_next_nodes (vm,
262                                gbp_null_classify_node.index,
263                                L2INPUT_N_FEAT,
264                                l2input_get_feat_names (),
265                                em->l2_input_feat_next[GBP_SRC_CLASSIFY_PORT]);
266
267   return 0;
268 }
269
270 VLIB_INIT_FUNCTION (gbp_src_classify_init);
271
272 /*
273  * fd.io coding-style-patch-verification: ON
274  *
275  * Local Variables:
276  * eval: (c-set-style "gnu")
277  * End:
278  */