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