GBP: l3-out subnets
[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 <plugins/gbp/gbp_policy_dpo.h>
20 #include <plugins/gbp/gbp_ext_itf.h>
21 #include <vnet/fib/ip4_fib.h>
22 #include <vnet/fib/ip6_fib.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/l2/l2_input.h>
25 #include <vnet/l2/feat_bitmap.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
28
29 typedef enum gbp_src_classify_type_t_
30 {
31   GBP_SRC_CLASSIFY_NULL,
32   GBP_SRC_CLASSIFY_PORT,
33   GBP_SRC_CLASSIFY_LPM,
34 } gbp_src_classify_type_t;
35
36 #define GBP_SRC_N_CLASSIFY (GBP_SRC_CLASSIFY_LPM + 1)
37
38 /**
39  * Grouping of global data for the GBP source EPG classification feature
40  */
41 typedef struct gbp_src_classify_main_t_
42 {
43   /**
44    * Next nodes for L2 output features
45    */
46   u32 l2_input_feat_next[GBP_SRC_N_CLASSIFY][32];
47 } gbp_src_classify_main_t;
48
49 static gbp_src_classify_main_t gbp_src_classify_main;
50
51 /**
52  * per-packet trace data
53  */
54 typedef struct gbp_classify_trace_t_
55 {
56   /* per-pkt trace data */
57   epg_id_t src_epg;
58 } gbp_classify_trace_t;
59
60 /*
61  * determine the SRC EPG form the input port
62  */
63 always_inline uword
64 gbp_classify_inline (vlib_main_t * vm,
65                      vlib_node_runtime_t * node,
66                      vlib_frame_t * frame,
67                      gbp_src_classify_type_t type, dpo_proto_t dproto)
68 {
69   gbp_src_classify_main_t *gscm = &gbp_src_classify_main;
70   u32 n_left_from, *from, *to_next;
71   u32 next_index;
72
73   next_index = 0;
74   n_left_from = frame->n_vectors;
75   from = vlib_frame_vector_args (frame);
76
77   while (n_left_from > 0)
78     {
79       u32 n_left_to_next;
80
81       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
82
83       while (n_left_from > 0 && n_left_to_next > 0)
84         {
85           u32 next0, bi0, src_epg, sw_if_index0;
86           const gbp_endpoint_t *ge0;
87           vlib_buffer_t *b0;
88
89           bi0 = from[0];
90           to_next[0] = bi0;
91           from += 1;
92           to_next += 1;
93           n_left_from -= 1;
94           n_left_to_next -= 1;
95
96           b0 = vlib_get_buffer (vm, bi0);
97
98           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
99           vnet_buffer2 (b0)->gbp.flags = VXLAN_GBP_GPFLAGS_NONE;
100
101           if (GBP_SRC_CLASSIFY_NULL == type)
102             {
103               src_epg = EPG_INVALID;
104               next0 =
105                 vnet_l2_feature_next (b0, gscm->l2_input_feat_next[type],
106                                       L2INPUT_FEAT_GBP_NULL_CLASSIFY);
107             }
108           else
109             {
110               if (DPO_PROTO_ETHERNET == dproto)
111                 {
112                   const ethernet_header_t *h0;
113
114                   h0 = vlib_buffer_get_current (b0);
115                   next0 =
116                     vnet_l2_feature_next (b0, gscm->l2_input_feat_next[type],
117                                           L2INPUT_FEAT_GBP_SRC_CLASSIFY);
118                   ge0 = gbp_endpoint_find_mac (h0->src_address,
119                                                vnet_buffer (b0)->l2.bd_index);
120                 }
121               else if (DPO_PROTO_IP4 == dproto)
122                 {
123                   const ip4_header_t *h0;
124
125                   h0 = vlib_buffer_get_current (b0);
126
127                   ge0 = gbp_endpoint_find_ip4
128                     (&h0->src_address,
129                      fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
130                                                           sw_if_index0));
131
132
133                   /*
134                    * Go straight to looukp, do not pass go, do not collect $200
135                    */
136                   next0 = 0;
137                 }
138               else if (DPO_PROTO_IP6 == dproto)
139                 {
140                   const ip6_header_t *h0;
141
142                   h0 = vlib_buffer_get_current (b0);
143
144                   ge0 = gbp_endpoint_find_ip6
145                     (&h0->src_address,
146                      fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
147                                                           sw_if_index0));
148
149
150                   /*
151                    * Go straight to lookup, do not pass go, do not collect $200
152                    */
153                   next0 = 0;
154                 }
155               else
156                 {
157                   ge0 = NULL;
158                   next0 = 0;
159                   ASSERT (0);
160                 }
161
162               if (PREDICT_TRUE (NULL != ge0))
163                 src_epg = ge0->ge_fwd.gef_epg_id;
164               else
165                 src_epg = EPG_INVALID;
166             }
167
168           vnet_buffer2 (b0)->gbp.src_epg = src_epg;
169
170           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
171             {
172               gbp_classify_trace_t *t =
173                 vlib_add_trace (vm, node, b0, sizeof (*t));
174               t->src_epg = src_epg;
175             }
176
177           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
178                                            to_next, n_left_to_next,
179                                            bi0, next0);
180         }
181
182       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
183     }
184
185   return frame->n_vectors;
186 }
187
188 static uword
189 gbp_src_classify (vlib_main_t * vm,
190                   vlib_node_runtime_t * node, vlib_frame_t * frame)
191 {
192   return (gbp_classify_inline (vm, node, frame,
193                                GBP_SRC_CLASSIFY_PORT, DPO_PROTO_ETHERNET));
194 }
195
196 static uword
197 gbp_null_classify (vlib_main_t * vm,
198                    vlib_node_runtime_t * node, vlib_frame_t * frame)
199 {
200   return (gbp_classify_inline (vm, node, frame,
201                                GBP_SRC_CLASSIFY_NULL, DPO_PROTO_ETHERNET));
202 }
203
204 static uword
205 gbp_ip4_src_classify (vlib_main_t * vm,
206                       vlib_node_runtime_t * node, vlib_frame_t * frame)
207 {
208   return (gbp_classify_inline (vm, node, frame,
209                                GBP_SRC_CLASSIFY_PORT, DPO_PROTO_IP4));
210 }
211
212 static uword
213 gbp_ip6_src_classify (vlib_main_t * vm,
214                       vlib_node_runtime_t * node, vlib_frame_t * frame)
215 {
216   return (gbp_classify_inline (vm, node, frame,
217                                GBP_SRC_CLASSIFY_PORT, DPO_PROTO_IP6));
218 }
219
220
221 /* packet trace format function */
222 static u8 *
223 format_gbp_classify_trace (u8 * s, va_list * args)
224 {
225   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
226   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
227   gbp_classify_trace_t *t = va_arg (*args, gbp_classify_trace_t *);
228
229   s = format (s, "src-epg:%d", t->src_epg);
230
231   return s;
232 }
233
234 /* *INDENT-OFF* */
235 VLIB_REGISTER_NODE (gbp_null_classify_node) = {
236   .function = gbp_null_classify,
237   .name = "gbp-null-classify",
238   .vector_size = sizeof (u32),
239   .format_trace = format_gbp_classify_trace,
240   .type = VLIB_NODE_TYPE_INTERNAL,
241
242   .n_errors = 0,
243   .n_next_nodes = 0,
244 };
245
246 VLIB_NODE_FUNCTION_MULTIARCH (gbp_null_classify_node, gbp_null_classify);
247
248 VLIB_REGISTER_NODE (gbp_src_classify_node) = {
249   .function = gbp_src_classify,
250   .name = "gbp-src-classify",
251   .vector_size = sizeof (u32),
252   .format_trace = format_gbp_classify_trace,
253   .type = VLIB_NODE_TYPE_INTERNAL,
254
255   .n_errors = 0,
256   .n_next_nodes = 0,
257 };
258
259 VLIB_NODE_FUNCTION_MULTIARCH (gbp_src_classify_node, gbp_src_classify);
260
261 VLIB_REGISTER_NODE (gbp_ip4_src_classify_node) = {
262   .function = gbp_ip4_src_classify,
263   .name = "ip4-gbp-src-classify",
264   .vector_size = sizeof (u32),
265   .format_trace = format_gbp_classify_trace,
266   .type = VLIB_NODE_TYPE_INTERNAL,
267
268   .n_errors = 0,
269   .n_next_nodes = 1,
270   .next_nodes = {
271     [0] = "ip4-lookup"
272   },
273 };
274
275 VLIB_NODE_FUNCTION_MULTIARCH (gbp_ip4_src_classify_node, gbp_ip4_src_classify);
276
277 VLIB_REGISTER_NODE (gbp_ip6_src_classify_node) = {
278   .function = gbp_ip6_src_classify,
279   .name = "ip6-gbp-src-classify",
280   .vector_size = sizeof (u32),
281   .format_trace = format_gbp_classify_trace,
282   .type = VLIB_NODE_TYPE_INTERNAL,
283
284   .n_errors = 0,
285   .n_next_nodes = 1,
286   .next_nodes = {
287     [0] = "ip6-lookup"
288   },
289 };
290
291 VLIB_NODE_FUNCTION_MULTIARCH (gbp_ip6_src_classify_node, gbp_ip6_src_classify);
292
293 VNET_FEATURE_INIT (gbp_ip4_src_classify_feat_node, static) =
294 {
295   .arc_name = "ip4-unicast",
296   .node_name = "ip4-gbp-src-classify",
297   .runs_before = VNET_FEATURES ("nat44-out2in"),
298 };
299 VNET_FEATURE_INIT (gbp_ip6_src_classify_feat_node, static) =
300 {
301   .arc_name = "ip6-unicast",
302   .node_name = "ip6-gbp-src-classify",
303   .runs_before = VNET_FEATURES ("nat66-out2in"),
304 };
305
306 /* *INDENT-ON* */
307
308 typedef enum gbp_lpm_classify_next_t_
309 {
310   GPB_LPM_CLASSIFY_DROP,
311 } gbp_lpm_classify_next_t;
312
313 always_inline dpo_proto_t
314 ethertype_to_dpo_proto (const ethernet_header_t * eh0)
315 {
316   u16 etype = clib_net_to_host_u16 (eh0->type);
317
318   switch (etype)
319     {
320     case ETHERNET_TYPE_IP4:
321       return (DPO_PROTO_IP4);
322     case ETHERNET_TYPE_IP6:
323       return (DPO_PROTO_IP6);
324     case ETHERNET_TYPE_VLAN:
325       {
326         ethernet_vlan_header_t *vh0;
327
328         vh0 = (ethernet_vlan_header_t *) (eh0 + 1);
329
330         switch (clib_net_to_host_u16 (vh0->type))
331           {
332           case ETHERNET_TYPE_IP4:
333             return (DPO_PROTO_IP4);
334           case ETHERNET_TYPE_IP6:
335             return (DPO_PROTO_IP6);
336           }
337       }
338     }
339
340   return (DPO_PROTO_NONE);
341 }
342
343 /*
344  * Determine the SRC EPG from a LPM
345  */
346 always_inline uword
347 gbp_lpm_classify_inline (vlib_main_t * vm,
348                          vlib_node_runtime_t * node,
349                          vlib_frame_t * frame,
350                          dpo_proto_t dproto, u8 is_recirc)
351 {
352   gbp_src_classify_main_t *gscm = &gbp_src_classify_main;
353   u32 n_left_from, *from, *to_next;
354   u32 next_index;
355
356   next_index = 0;
357   n_left_from = frame->n_vectors;
358   from = vlib_frame_vector_args (frame);
359
360   while (n_left_from > 0)
361     {
362       u32 n_left_to_next;
363
364       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
365
366       while (n_left_from > 0 && n_left_to_next > 0)
367         {
368           u32 bi0, sw_if_index0, fib_index0, lbi0;
369           gbp_lpm_classify_next_t next0;
370           const gbp_policy_dpo_t *gpd0;
371           const gbp_ext_itf_t *gx0;
372           const gbp_recirc_t *gr0;
373           const dpo_id_t *dpo0;
374           load_balance_t *lb0;
375           ip4_header_t *ip4_0;
376           ip6_header_t *ip6_0;
377           vlib_buffer_t *b0;
378           epg_id_t src_epg0;
379
380           bi0 = from[0];
381           to_next[0] = bi0;
382           from += 1;
383           to_next += 1;
384           n_left_from -= 1;
385           n_left_to_next -= 1;
386           next0 = GPB_LPM_CLASSIFY_DROP;
387
388           b0 = vlib_get_buffer (vm, bi0);
389
390           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
391           vnet_buffer2 (b0)->gbp.flags = VXLAN_GBP_GPFLAGS_NONE;
392
393           if (DPO_PROTO_IP4 == dproto)
394             ip4_0 = vlib_buffer_get_current (b0);
395           else if (DPO_PROTO_IP4 == dproto)
396             ip6_0 = vlib_buffer_get_current (b0);
397           else if (DPO_PROTO_ETHERNET == dproto)
398             {
399               const ethernet_header_t *eh0;
400
401               eh0 = vlib_buffer_get_current (b0);
402
403               dproto = ethertype_to_dpo_proto (eh0);
404
405               switch (dproto)
406                 {
407                 case DPO_PROTO_IP4:
408                   ip4_0 = (vlib_buffer_get_current (b0) +
409                            vnet_buffer (b0)->l2.l2_len);
410                   break;
411                 case DPO_PROTO_IP6:
412                   ip6_0 = (vlib_buffer_get_current (b0) +
413                            vnet_buffer (b0)->l2.l2_len);
414                   break;
415                 default:
416                   /* not IP so no LPM classify possible */
417                   src_epg0 = EPG_INVALID;
418                   goto trace;
419                 }
420             }
421
422           if (is_recirc)
423             {
424               gr0 = gbp_recirc_get (sw_if_index0);
425               fib_index0 = gr0->gr_fib_index[dproto];
426
427               vnet_feature_next (&next0, b0);
428             }
429           else
430             {
431               gx0 = gbp_ext_itf_get (sw_if_index0);
432               fib_index0 = gx0->gx_fib_index[dproto];
433
434               next0 = vnet_l2_feature_next
435                 (b0, gscm->l2_input_feat_next[GBP_SRC_CLASSIFY_LPM],
436                  L2INPUT_FEAT_GBP_LPM_CLASSIFY);
437             }
438
439           if (DPO_PROTO_IP4 == dproto)
440             {
441               lbi0 = ip4_fib_forwarding_lookup (fib_index0,
442                                                 &ip4_0->src_address);
443             }
444           else if (DPO_PROTO_IP4 == dproto)
445             {
446               lbi0 = ip6_fib_table_fwding_lookup (&ip6_main, fib_index0,
447                                                   &ip6_0->src_address);
448             }
449           else
450             {
451               /* not IP so no LPM classify possible */
452               src_epg0 = EPG_INVALID;
453               goto trace;
454             }
455           lb0 = load_balance_get (lbi0);
456           dpo0 = load_balance_get_bucket_i (lb0, 0);
457
458           if (gbp_policy_dpo_type == dpo0->dpoi_type)
459             {
460               gpd0 = gbp_policy_dpo_get (dpo0->dpoi_index);
461               src_epg0 = gpd0->gpd_epg;
462             }
463           else
464             {
465               /* could not classify => drop */
466               src_epg0 = EPG_INVALID;
467               next0 = GPB_LPM_CLASSIFY_DROP;
468             }
469
470         trace:
471           vnet_buffer2 (b0)->gbp.src_epg = src_epg0;
472
473           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
474             {
475               gbp_classify_trace_t *t =
476                 vlib_add_trace (vm, node, b0, sizeof (*t));
477               t->src_epg = src_epg0;
478             }
479
480           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
481                                            to_next, n_left_to_next,
482                                            bi0, next0);
483         }
484
485       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
486     }
487
488   return frame->n_vectors;
489 }
490
491 static uword
492 gbp_ip4_lpm_classify (vlib_main_t * vm,
493                       vlib_node_runtime_t * node, vlib_frame_t * frame)
494 {
495   return (gbp_lpm_classify_inline (vm, node, frame, DPO_PROTO_IP4, 1));
496 }
497
498 static uword
499 gbp_ip6_lpm_classify (vlib_main_t * vm,
500                       vlib_node_runtime_t * node, vlib_frame_t * frame)
501 {
502   return (gbp_lpm_classify_inline (vm, node, frame, DPO_PROTO_IP6, 1));
503 }
504
505 static uword
506 gbp_l2_lpm_classify (vlib_main_t * vm,
507                      vlib_node_runtime_t * node, vlib_frame_t * frame)
508 {
509   return (gbp_lpm_classify_inline (vm, node, frame, DPO_PROTO_ETHERNET, 0));
510 }
511
512 /* *INDENT-OFF* */
513 VLIB_REGISTER_NODE (gbp_ip4_lpm_classify_node) = {
514   .function = gbp_ip4_lpm_classify,
515   .name = "ip4-gbp-lpm-classify",
516   .vector_size = sizeof (u32),
517   .format_trace = format_gbp_classify_trace,
518   .type = VLIB_NODE_TYPE_INTERNAL,
519
520   .n_errors = 0,
521   .n_next_nodes = 1,
522   .next_nodes = {
523     [GPB_LPM_CLASSIFY_DROP] = "ip4-drop"
524   },
525 };
526
527 VLIB_NODE_FUNCTION_MULTIARCH (gbp_ip4_lpm_classify_node, gbp_ip4_lpm_classify);
528
529 VLIB_REGISTER_NODE (gbp_ip6_lpm_classify_node) = {
530   .function = gbp_ip6_lpm_classify,
531   .name = "ip6-gbp-lpm-classify",
532   .vector_size = sizeof (u32),
533   .format_trace = format_gbp_classify_trace,
534   .type = VLIB_NODE_TYPE_INTERNAL,
535
536   .n_errors = 0,
537   .n_next_nodes = 1,
538   .next_nodes = {
539     [GPB_LPM_CLASSIFY_DROP] = "ip6-drop"
540   },
541 };
542
543 VLIB_NODE_FUNCTION_MULTIARCH (gbp_ip6_lpm_classify_node, gbp_ip6_lpm_classify);
544
545 VLIB_REGISTER_NODE (gbp_l2_lpm_classify_node) = {
546   .function = gbp_l2_lpm_classify,
547   .name = "l2-gbp-lpm-classify",
548   .vector_size = sizeof (u32),
549   .format_trace = format_gbp_classify_trace,
550   .type = VLIB_NODE_TYPE_INTERNAL,
551
552   .n_errors = 0,
553   .n_next_nodes = 1,
554   .next_nodes = {
555     [GPB_LPM_CLASSIFY_DROP] = "error-drop"
556   },
557 };
558
559 VLIB_NODE_FUNCTION_MULTIARCH (gbp_l2_lpm_classify_node, gbp_l2_lpm_classify);
560
561 VNET_FEATURE_INIT (gbp_ip4_lpm_classify_feat_node, static) =
562 {
563   .arc_name = "ip4-unicast",
564   .node_name = "ip4-gbp-lpm-classify",
565   .runs_before = VNET_FEATURES ("nat44-out2in"),
566 };
567 VNET_FEATURE_INIT (gbp_ip6_lpm_classify_feat_node, static) =
568 {
569   .arc_name = "ip6-unicast",
570   .node_name = "ip6-gbp-lpm-classify",
571   .runs_before = VNET_FEATURES ("nat66-out2in"),
572 };
573
574 /* *INDENT-ON* */
575
576 static clib_error_t *
577 gbp_src_classify_init (vlib_main_t * vm)
578 {
579   gbp_src_classify_main_t *em = &gbp_src_classify_main;
580
581   /* Initialize the feature next-node indexes */
582   feat_bitmap_init_next_nodes (vm,
583                                gbp_src_classify_node.index,
584                                L2INPUT_N_FEAT,
585                                l2input_get_feat_names (),
586                                em->l2_input_feat_next[GBP_SRC_CLASSIFY_NULL]);
587   feat_bitmap_init_next_nodes (vm,
588                                gbp_null_classify_node.index,
589                                L2INPUT_N_FEAT,
590                                l2input_get_feat_names (),
591                                em->l2_input_feat_next[GBP_SRC_CLASSIFY_PORT]);
592   feat_bitmap_init_next_nodes (vm,
593                                gbp_l2_lpm_classify_node.index,
594                                L2INPUT_N_FEAT,
595                                l2input_get_feat_names (),
596                                em->l2_input_feat_next[GBP_SRC_CLASSIFY_LPM]);
597
598   return 0;
599 }
600
601 VLIB_INIT_FUNCTION (gbp_src_classify_init);
602
603 /*
604  * fd.io coding-style-patch-verification: ON
605  *
606  * Local Variables:
607  * eval: (c-set-style "gnu")
608  * End:
609  */