GBP V2
[vpp.git] / src / plugins / gbp / gbp_policy.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.h>
17
18 /**
19  * Grouping of global data for the GBP source EPG classification feature
20  */
21 typedef struct gbp_policy_main_t_
22 {
23   /**
24    * Next nodes for L2 output features
25    */
26   u32 l2_output_feat_next[32];
27 } gbp_policy_main_t;
28
29 static gbp_policy_main_t gbp_policy_main;
30
31 #define foreach_gbp_policy                      \
32   _(DENY,    "deny")
33
34 typedef enum
35 {
36 #define _(sym,str) GBP_ERROR_##sym,
37   foreach_gbp_policy
38 #undef _
39     GBP_POLICY_N_ERROR,
40 } gbp_policy_error_t;
41
42 static char *gbp_policy_error_strings[] = {
43 #define _(sym,string) string,
44   foreach_gbp_policy
45 #undef _
46 };
47
48 typedef enum
49 {
50 #define _(sym,str) GBP_POLICY_NEXT_##sym,
51   foreach_gbp_policy
52 #undef _
53     GBP_POLICY_N_NEXT,
54 } gbp_policy_next_t;
55
56 /**
57  * per-packet trace data
58  */
59 typedef struct gbp_policy_trace_t_
60 {
61   /* per-pkt trace data */
62   epg_id_t src_epg;
63   epg_id_t dst_epg;
64   u32 acl_index;
65 } gbp_policy_trace_t;
66
67 static uword
68 gbp_policy (vlib_main_t * vm,
69             vlib_node_runtime_t * node, vlib_frame_t * frame)
70 {
71   gbp_policy_main_t *gpm = &gbp_policy_main;
72   u32 n_left_from, *from, *to_next;
73   u32 next_index;
74
75   next_index = 0;
76   n_left_from = frame->n_vectors;
77   from = vlib_frame_vector_args (frame);
78
79   while (n_left_from > 0)
80     {
81       u32 n_left_to_next;
82
83       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
84
85       while (n_left_from > 0 && n_left_to_next > 0)
86         {
87           gbp_policy_next_t next0;
88           gbp_contract_key_t key0;
89           u32 bi0, sw_if_index0;
90           vlib_buffer_t *b0;
91           u32 acl_index0;
92
93           next0 = GBP_POLICY_NEXT_DENY;
94           bi0 = from[0];
95           to_next[0] = bi0;
96           from += 1;
97           to_next += 1;
98           n_left_from -= 1;
99           n_left_to_next -= 1;
100
101           b0 = vlib_get_buffer (vm, bi0);
102
103           /*
104            * determine the src and dst EPG
105            */
106           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
107           key0.gck_dst = gbp_port_to_epg (sw_if_index0);
108           key0.gck_src = vnet_buffer2 (b0)->gbp.src_epg;
109           acl_index0 = ~0;
110
111           if (~0 != key0.gck_src)
112             {
113               if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
114                 {
115                   /*
116                    * intra-epg allowed
117                    */
118                   next0 = vnet_l2_feature_next (b0, gpm->l2_output_feat_next,
119                                                 L2OUTPUT_FEAT_GBP_POLICY);
120                 }
121               else
122                 {
123                   acl_index0 = gbp_acl_lookup (&key0);
124
125                   if (~0 != acl_index0)
126                     {
127                       /*
128                        * TODO tests against the ACL
129                        */
130                       /*
131                        * ACL tables are not available outside of ACL plugin
132                        * until then bypass the ACL to next node
133                        */
134                       next0 =
135                         vnet_l2_feature_next (b0, gpm->l2_output_feat_next,
136                                               L2OUTPUT_FEAT_GBP_POLICY);
137                     }
138                 }
139             }
140           else
141             {
142               /*
143                * the src EPG is not set when the packet arrives on an EPG
144                * uplink interface and we do not need to apply policy
145                */
146               next0 = vnet_l2_feature_next (b0, gpm->l2_output_feat_next,
147                                             L2OUTPUT_FEAT_GBP_POLICY);
148             }
149
150           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
151             {
152               gbp_policy_trace_t *t =
153                 vlib_add_trace (vm, node, b0, sizeof (*t));
154               t->src_epg = key0.gck_src;
155               t->dst_epg = key0.gck_dst;
156               t->acl_index = acl_index0;
157             }
158
159           /* verify speculative enqueue, maybe switch current next frame */
160           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
161                                            to_next, n_left_to_next,
162                                            bi0, next0);
163         }
164
165       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
166     }
167
168   return frame->n_vectors;
169 }
170
171 /* packet trace format function */
172 static u8 *
173 format_gbp_policy_trace (u8 * s, va_list * args)
174 {
175   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
176   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
177   gbp_policy_trace_t *t = va_arg (*args, gbp_policy_trace_t *);
178
179   s =
180     format (s, "src:%d, dst:%d, acl:%d", t->src_epg, t->dst_epg,
181             t->acl_index);
182
183   return s;
184 }
185
186 /* *INDENT-OFF* */
187 VLIB_REGISTER_NODE (gbp_policy_node) = {
188   .function = gbp_policy,
189   .name = "gbp-policy",
190   .vector_size = sizeof (u32),
191   .format_trace = format_gbp_policy_trace,
192   .type = VLIB_NODE_TYPE_INTERNAL,
193
194   .n_errors = ARRAY_LEN(gbp_policy_error_strings),
195   .error_strings = gbp_policy_error_strings,
196
197   .n_next_nodes = GBP_POLICY_N_NEXT,
198
199   .next_nodes = {
200     [GBP_POLICY_NEXT_DENY] = "error-drop",
201   },
202 };
203
204 VLIB_NODE_FUNCTION_MULTIARCH (gbp_policy_node, gbp_policy);
205
206 /* *INDENT-ON* */
207
208 static clib_error_t *
209 gbp_policy_init (vlib_main_t * vm)
210 {
211   gbp_policy_main_t *gpm = &gbp_policy_main;
212
213   /* Initialize the feature next-node indexes */
214   feat_bitmap_init_next_nodes (vm,
215                                gbp_policy_node.index,
216                                L2OUTPUT_N_FEAT,
217                                l2output_get_feat_names (),
218                                gpm->l2_output_feat_next);
219
220   return 0;
221 }
222
223 VLIB_INIT_FUNCTION (gbp_policy_init);
224
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "gnu")
230  * End:
231  */