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