GBP: use sclass in the DP for policy
[vpp.git] / src / plugins / gbp / gbp_policy_dpo.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 <vnet/dpo/dvr_dpo.h>
17 #include <vnet/dpo/drop_dpo.h>
18 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
19
20 #include <plugins/gbp/gbp.h>
21 #include <plugins/gbp/gbp_policy_dpo.h>
22 #include <plugins/gbp/gbp_recirc.h>
23
24 #ifndef CLIB_MARCH_VARIANT
25 /**
26  * DPO pool
27  */
28 gbp_policy_dpo_t *gbp_policy_dpo_pool;
29
30 /**
31  * DPO type registered for these GBP FWD
32  */
33 dpo_type_t gbp_policy_dpo_type;
34
35 static gbp_policy_dpo_t *
36 gbp_policy_dpo_alloc (void)
37 {
38   gbp_policy_dpo_t *gpd;
39
40   pool_get_aligned_zero (gbp_policy_dpo_pool, gpd, CLIB_CACHE_LINE_BYTES);
41
42   return (gpd);
43 }
44
45 static inline gbp_policy_dpo_t *
46 gbp_policy_dpo_get_from_dpo (const dpo_id_t * dpo)
47 {
48   ASSERT (gbp_policy_dpo_type == dpo->dpoi_type);
49
50   return (gbp_policy_dpo_get (dpo->dpoi_index));
51 }
52
53 static inline index_t
54 gbp_policy_dpo_get_index (gbp_policy_dpo_t * gpd)
55 {
56   return (gpd - gbp_policy_dpo_pool);
57 }
58
59 static void
60 gbp_policy_dpo_lock (dpo_id_t * dpo)
61 {
62   gbp_policy_dpo_t *gpd;
63
64   gpd = gbp_policy_dpo_get_from_dpo (dpo);
65   gpd->gpd_locks++;
66 }
67
68 static void
69 gbp_policy_dpo_unlock (dpo_id_t * dpo)
70 {
71   gbp_policy_dpo_t *gpd;
72
73   gpd = gbp_policy_dpo_get_from_dpo (dpo);
74   gpd->gpd_locks--;
75
76   if (0 == gpd->gpd_locks)
77     {
78       dpo_reset (&gpd->gpd_dpo);
79       pool_put (gbp_policy_dpo_pool, gpd);
80     }
81 }
82
83 static u32
84 gbp_policy_dpo_get_urpf (const dpo_id_t * dpo)
85 {
86   gbp_policy_dpo_t *gpd;
87
88   gpd = gbp_policy_dpo_get_from_dpo (dpo);
89
90   return (gpd->gpd_sw_if_index);
91 }
92
93 void
94 gbp_policy_dpo_add_or_lock (dpo_proto_t dproto,
95                             sclass_t sclass, u32 sw_if_index, dpo_id_t * dpo)
96 {
97   gbp_policy_dpo_t *gpd;
98   dpo_id_t parent = DPO_INVALID;
99
100   gpd = gbp_policy_dpo_alloc ();
101
102   gpd->gpd_proto = dproto;
103   gpd->gpd_sw_if_index = sw_if_index;
104   gpd->gpd_sclass = sclass;
105
106   if (~0 != sw_if_index)
107     {
108       /*
109        * stack on the DVR DPO for the output interface
110        */
111       dvr_dpo_add_or_lock (sw_if_index, dproto, &parent);
112     }
113   else
114     {
115       dpo_copy (&parent, drop_dpo_get (dproto));
116     }
117
118   dpo_stack (gbp_policy_dpo_type, dproto, &gpd->gpd_dpo, &parent);
119   dpo_set (dpo, gbp_policy_dpo_type, dproto, gbp_policy_dpo_get_index (gpd));
120 }
121
122 u8 *
123 format_gbp_policy_dpo (u8 * s, va_list * ap)
124 {
125   index_t index = va_arg (*ap, index_t);
126   u32 indent = va_arg (*ap, u32);
127   gbp_policy_dpo_t *gpd = gbp_policy_dpo_get (index);
128   vnet_main_t *vnm = vnet_get_main ();
129
130   s = format (s, "gbp-policy-dpo: %U, sclass:%d out:%U",
131               format_dpo_proto, gpd->gpd_proto,
132               (int) gpd->gpd_sclass,
133               format_vnet_sw_if_index_name, vnm, gpd->gpd_sw_if_index);
134   s = format (s, "\n%U", format_white_space, indent + 2);
135   s = format (s, "%U", format_dpo_id, &gpd->gpd_dpo, indent + 4);
136
137   return (s);
138 }
139
140 /**
141  * Interpose a policy DPO
142  */
143 static void
144 gbp_policy_dpo_interpose (const dpo_id_t * original,
145                           const dpo_id_t * parent, dpo_id_t * clone)
146 {
147   gbp_policy_dpo_t *gpd, *gpd_clone;
148
149   gpd_clone = gbp_policy_dpo_alloc ();
150   gpd = gbp_policy_dpo_get (original->dpoi_index);
151
152   gpd_clone->gpd_proto = gpd->gpd_proto;
153   gpd_clone->gpd_sclass = gpd->gpd_sclass;
154   gpd_clone->gpd_sw_if_index = gpd->gpd_sw_if_index;
155
156   dpo_stack (gbp_policy_dpo_type,
157              gpd_clone->gpd_proto, &gpd_clone->gpd_dpo, parent);
158
159   dpo_set (clone,
160            gbp_policy_dpo_type,
161            gpd_clone->gpd_proto, gbp_policy_dpo_get_index (gpd_clone));
162 }
163
164 const static dpo_vft_t gbp_policy_dpo_vft = {
165   .dv_lock = gbp_policy_dpo_lock,
166   .dv_unlock = gbp_policy_dpo_unlock,
167   .dv_format = format_gbp_policy_dpo,
168   .dv_get_urpf = gbp_policy_dpo_get_urpf,
169   .dv_mk_interpose = gbp_policy_dpo_interpose,
170 };
171
172 /**
173  * @brief The per-protocol VLIB graph nodes that are assigned to a glean
174  *        object.
175  *
176  * this means that these graph nodes are ones from which a glean is the
177  * parent object in the DPO-graph.
178  */
179 const static char *const gbp_policy_dpo_ip4_nodes[] = {
180   "ip4-gbp-policy-dpo",
181   NULL,
182 };
183
184 const static char *const gbp_policy_dpo_ip6_nodes[] = {
185   "ip6-gbp-policy-dpo",
186   NULL,
187 };
188
189 const static char *const *const gbp_policy_dpo_nodes[DPO_PROTO_NUM] = {
190   [DPO_PROTO_IP4] = gbp_policy_dpo_ip4_nodes,
191   [DPO_PROTO_IP6] = gbp_policy_dpo_ip6_nodes,
192 };
193
194 dpo_type_t
195 gbp_policy_dpo_get_type (void)
196 {
197   return (gbp_policy_dpo_type);
198 }
199
200 static clib_error_t *
201 gbp_policy_dpo_module_init (vlib_main_t * vm)
202 {
203   gbp_policy_dpo_type = dpo_register_new_type (&gbp_policy_dpo_vft,
204                                                gbp_policy_dpo_nodes);
205
206   return (NULL);
207 }
208
209 VLIB_INIT_FUNCTION (gbp_policy_dpo_module_init);
210 #endif /* CLIB_MARCH_VARIANT */
211
212 typedef struct gbp_policy_dpo_trace_t_
213 {
214   u32 src_epg;
215   u32 dst_epg;
216   u32 acl_index;
217   u32 a_bit;
218 } gbp_policy_dpo_trace_t;
219
220 typedef enum
221 {
222   GBP_POLICY_DROP,
223   GBP_POLICY_N_NEXT,
224 } gbp_policy_next_t;
225
226 always_inline u32
227 gbp_rule_l3_redirect (const gbp_rule_t * gu, vlib_buffer_t * b0, int is_ip6)
228 {
229   gbp_policy_node_t pnode;
230   const dpo_id_t *dpo;
231   dpo_proto_t dproto;
232
233   pnode = (is_ip6 ? GBP_POLICY_NODE_IP6 : GBP_POLICY_NODE_IP4);
234   dproto = (is_ip6 ? DPO_PROTO_IP6 : DPO_PROTO_IP4);
235   dpo = &gu->gu_dpo[pnode][dproto];
236
237   /* The flow hash is still valid as this is a IP packet being switched */
238   vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo->dpoi_index;
239
240   return (dpo->dpoi_next_node);
241 }
242
243 always_inline uword
244 gbp_policy_dpo_inline (vlib_main_t * vm,
245                        vlib_node_runtime_t * node,
246                        vlib_frame_t * from_frame, u8 is_ip6)
247 {
248   gbp_main_t *gm = &gbp_main;
249   u32 n_left_from, next_index, *from, *to_next;
250   gbp_rule_t *gu;
251
252   from = vlib_frame_vector_args (from_frame);
253   n_left_from = from_frame->n_vectors;
254
255   next_index = node->cached_next_index;
256
257   while (n_left_from > 0)
258     {
259       u32 n_left_to_next;
260
261       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
262
263       while (n_left_from > 0 && n_left_to_next > 0)
264         {
265           const gbp_policy_dpo_t *gpd0;
266           u32 bi0, next0;
267           gbp_contract_key_t key0;
268           gbp_contract_t *gc0;
269           vlib_buffer_t *b0;
270           index_t gci0;
271
272           bi0 = from[0];
273           to_next[0] = bi0;
274           from += 1;
275           to_next += 1;
276           n_left_from -= 1;
277           n_left_to_next -= 1;
278           next0 = GBP_POLICY_DROP;
279
280           b0 = vlib_get_buffer (vm, bi0);
281
282           gc0 = NULL;
283           gpd0 = gbp_policy_dpo_get (vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
284           vnet_buffer (b0)->ip.adj_index[VLIB_TX] = gpd0->gpd_dpo.dpoi_index;
285
286           if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A)
287             {
288               next0 = gpd0->gpd_dpo.dpoi_next_node;
289               key0.as_u32 = ~0;
290               goto trace;
291             }
292
293           key0.gck_src = vnet_buffer2 (b0)->gbp.sclass;
294           key0.gck_dst = gpd0->gpd_sclass;
295
296           if (SCLASS_INVALID != key0.gck_src)
297             {
298               if (PREDICT_FALSE (key0.gck_src == key0.gck_dst))
299                 {
300                   /*
301                    * intra-epg allowed
302                    */
303                   next0 = gpd0->gpd_dpo.dpoi_next_node;
304                   vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
305                 }
306               else
307                 {
308                   gci0 = gbp_contract_find (&key0);
309
310                   if (INDEX_INVALID != gci0)
311                     {
312                       fa_5tuple_opaque_t pkt_5tuple0;
313                       u8 action0 = 0;
314                       u32 acl_pos_p0, acl_match_p0;
315                       u32 rule_match_p0, trace_bitmap0;
316                       /*
317                        * tests against the ACL
318                        */
319                       gc0 = gbp_contract_get (gci0);
320                       acl_plugin_fill_5tuple_inline (gm->
321                                                      acl_plugin.p_acl_main,
322                                                      gc0->gc_lc_index, b0,
323                                                      is_ip6,
324                                                      /* is_input */ 1,
325                                                      /* is_l2_path */ 0,
326                                                      &pkt_5tuple0);
327                       acl_plugin_match_5tuple_inline (gm->
328                                                       acl_plugin.p_acl_main,
329                                                       gc0->gc_lc_index,
330                                                       &pkt_5tuple0, is_ip6,
331                                                       &action0, &acl_pos_p0,
332                                                       &acl_match_p0,
333                                                       &rule_match_p0,
334                                                       &trace_bitmap0);
335
336                       if (action0 > 0)
337                         {
338
339                           vnet_buffer2 (b0)->gbp.flags |= VXLAN_GBP_GPFLAGS_A;
340                           gu = gbp_rule_get (gc0->gc_rules[rule_match_p0]);
341
342                           switch (gu->gu_action)
343                             {
344                             case GBP_RULE_PERMIT:
345                               next0 = gpd0->gpd_dpo.dpoi_next_node;
346                               break;
347                             case GBP_RULE_DENY:
348                               next0 = 0;
349                               break;
350                             case GBP_RULE_REDIRECT:
351                               next0 = gbp_rule_l3_redirect (gu, b0, is_ip6);
352                               break;
353                             }
354                         }
355                     }
356                 }
357             }
358           else
359             {
360               /*
361                * the src EPG is not set when the packet arrives on an EPG
362                * uplink interface and we do not need to apply policy
363                */
364               next0 = gpd0->gpd_dpo.dpoi_next_node;
365             }
366         trace:
367           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
368             {
369               gbp_policy_dpo_trace_t *tr;
370
371               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
372               tr->src_epg = key0.gck_src;
373               tr->dst_epg = key0.gck_dst;
374               tr->acl_index = (gc0 ? gc0->gc_acl_index : ~0);
375               tr->a_bit = vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_A;
376             }
377
378           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
379                                            n_left_to_next, bi0, next0);
380         }
381       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
382     }
383   return from_frame->n_vectors;
384 }
385
386 static u8 *
387 format_gbp_policy_dpo_trace (u8 * s, va_list * args)
388 {
389   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
390   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
391   gbp_policy_dpo_trace_t *t = va_arg (*args, gbp_policy_dpo_trace_t *);
392
393   s = format (s, " src-epg:%d dst-epg:%d acl-index:%d a-bit:%d",
394               t->src_epg, t->dst_epg, t->acl_index, t->a_bit);
395
396   return s;
397 }
398
399 VLIB_NODE_FN (ip4_gbp_policy_dpo_node) (vlib_main_t * vm,
400                                         vlib_node_runtime_t * node,
401                                         vlib_frame_t * from_frame)
402 {
403   return (gbp_policy_dpo_inline (vm, node, from_frame, 0));
404 }
405
406 VLIB_NODE_FN (ip6_gbp_policy_dpo_node) (vlib_main_t * vm,
407                                         vlib_node_runtime_t * node,
408                                         vlib_frame_t * from_frame)
409 {
410   return (gbp_policy_dpo_inline (vm, node, from_frame, 1));
411 }
412
413 /* *INDENT-OFF* */
414 VLIB_REGISTER_NODE (ip4_gbp_policy_dpo_node) = {
415     .name = "ip4-gbp-policy-dpo",
416     .vector_size = sizeof (u32),
417     .format_trace = format_gbp_policy_dpo_trace,
418     .n_next_nodes = GBP_POLICY_N_NEXT,
419     .next_nodes =
420     {
421         [GBP_POLICY_DROP] = "ip4-drop",
422     }
423 };
424 VLIB_REGISTER_NODE (ip6_gbp_policy_dpo_node) = {
425     .name = "ip6-gbp-policy-dpo",
426     .vector_size = sizeof (u32),
427     .format_trace = format_gbp_policy_dpo_trace,
428     .n_next_nodes = GBP_POLICY_N_NEXT,
429     .next_nodes =
430     {
431         [GBP_POLICY_DROP] = "ip6-drop",
432     }
433 };
434 /* *INDENT-ON* */
435
436 /*
437  * fd.io coding-style-patch-verification: ON
438  *
439  * Local Variables:
440  * eval: (c-set-style "gnu")
441  * End:
442  */