GBP V2
[vpp.git] / src / plugins / gbp / gbp_fwd.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_fwd_main_t_
22 {
23   /**
24    * Next nodes for L2 output features
25    */
26   u32 l2_input_feat_next[32];
27 } gbp_fwd_main_t;
28
29 static gbp_fwd_main_t gbp_fwd_main;
30
31 #define foreach_gbp_fwd                      \
32   _(DROP,    "drop")                         \
33   _(OUTPUT,  "output")
34
35 typedef enum
36 {
37 #define _(sym,str) GBP_FWD_ERROR_##sym,
38   foreach_gbp_fwd
39 #undef _
40     GBP_FWD_N_ERROR,
41 } gbp_fwd_error_t;
42
43 static char *gbp_fwd_error_strings[] = {
44 #define _(sym,string) string,
45   foreach_gbp_fwd
46 #undef _
47 };
48
49 typedef enum
50 {
51 #define _(sym,str) GBP_FWD_NEXT_##sym,
52   foreach_gbp_fwd
53 #undef _
54     GBP_FWD_N_NEXT,
55 } gbp_fwd_next_t;
56
57 /**
58  * per-packet trace data
59  */
60 typedef struct gbp_fwd_trace_t_
61 {
62   /* per-pkt trace data */
63   epg_id_t src_epg;
64   u32 sw_if_index;
65 } gbp_fwd_trace_t;
66
67 static uword
68 gbp_fwd (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
69 {
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 bi0, sw_if_index0, src_epg;
86           gbp_fwd_next_t next0;
87           vlib_buffer_t *b0;
88
89           next0 = GBP_FWD_NEXT_DROP;
90           bi0 = from[0];
91           to_next[0] = bi0;
92           from += 1;
93           to_next += 1;
94           n_left_from -= 1;
95           n_left_to_next -= 1;
96
97           b0 = vlib_get_buffer (vm, bi0);
98
99           /*
100            * lookup the uplink based on src EPG
101            */
102           src_epg = vnet_buffer2 (b0)->gbp.src_epg;
103
104           sw_if_index0 = gbp_epg_itf_lookup (src_epg);
105
106           if (~0 != sw_if_index0)
107             {
108               vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
109
110               next0 = GBP_FWD_NEXT_OUTPUT;
111             }
112           /*
113            * else
114            *  don't know the uplink interface for this EPG => drop
115            */
116
117           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
118             {
119               gbp_fwd_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
120               t->src_epg = src_epg;
121               t->sw_if_index = sw_if_index0;
122             }
123
124           /* verify speculative enqueue, maybe switch current next frame */
125           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
126                                            to_next, n_left_to_next,
127                                            bi0, next0);
128         }
129
130       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
131     }
132
133   return frame->n_vectors;
134 }
135
136 /* packet trace format function */
137 static u8 *
138 format_gbp_fwd_trace (u8 * s, va_list * args)
139 {
140   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
141   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
142   gbp_fwd_trace_t *t = va_arg (*args, gbp_fwd_trace_t *);
143
144   s = format (s, "src-epg:%d", t->src_epg);
145
146   return s;
147 }
148
149 /* *INDENT-OFF* */
150 VLIB_REGISTER_NODE (gbp_fwd_node) = {
151   .function = gbp_fwd,
152   .name = "gbp-fwd",
153   .vector_size = sizeof (u32),
154   .format_trace = format_gbp_fwd_trace,
155   .type = VLIB_NODE_TYPE_INTERNAL,
156
157   .n_errors = ARRAY_LEN(gbp_fwd_error_strings),
158   .error_strings = gbp_fwd_error_strings,
159
160   .n_next_nodes = GBP_FWD_N_NEXT,
161
162   .next_nodes = {
163     [GBP_FWD_NEXT_DROP] = "error-drop",
164     [GBP_FWD_NEXT_OUTPUT] = "l2-output",
165   },
166 };
167
168 VLIB_NODE_FUNCTION_MULTIARCH (gbp_fwd_node, gbp_fwd);
169
170 /* *INDENT-ON* */
171
172 static clib_error_t *
173 gbp_fwd_init (vlib_main_t * vm)
174 {
175   gbp_fwd_main_t *gpm = &gbp_fwd_main;
176
177   /* Initialize the feature next-node indices */
178   feat_bitmap_init_next_nodes (vm,
179                                gbp_fwd_node.index,
180                                L2INPUT_N_FEAT,
181                                l2input_get_feat_names (),
182                                gpm->l2_input_feat_next);
183
184   return 0;
185 }
186
187 VLIB_INIT_FUNCTION (gbp_fwd_init);
188
189 /*
190  * fd.io coding-style-patch-verification: ON
191  *
192  * Local Variables:
193  * eval: (c-set-style "gnu")
194  * End:
195  */