misc: deprecate gbp and its dependents
[vpp.git] / extras / deprecated / plugins / gbp / gbp_fwd_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 <plugins/gbp/gbp.h>
17 #include <plugins/gbp/gbp_fwd_dpo.h>
18
19 #include <vnet/ethernet/ethernet.h>
20
21
22 #ifndef CLIB_MARCH_VARIANT
23 /**
24  * The 'DB' of GBP FWD DPOs.
25  * There is one per-proto
26  */
27 static index_t gbp_fwd_dpo_db[DPO_PROTO_NUM] = { INDEX_INVALID };
28
29 /**
30  * DPO type registered for these GBP FWD
31  */
32 static dpo_type_t gbp_fwd_dpo_type;
33
34 /**
35  * @brief pool of all interface DPOs
36  */
37 gbp_fwd_dpo_t *gbp_fwd_dpo_pool;
38
39 static gbp_fwd_dpo_t *
40 gbp_fwd_dpo_alloc (void)
41 {
42   gbp_fwd_dpo_t *gfd;
43
44   pool_get (gbp_fwd_dpo_pool, gfd);
45
46   return (gfd);
47 }
48
49 static inline gbp_fwd_dpo_t *
50 gbp_fwd_dpo_get_from_dpo (const dpo_id_t * dpo)
51 {
52   ASSERT (gbp_fwd_dpo_type == dpo->dpoi_type);
53
54   return (gbp_fwd_dpo_get (dpo->dpoi_index));
55 }
56
57 static inline index_t
58 gbp_fwd_dpo_get_index (gbp_fwd_dpo_t * gfd)
59 {
60   return (gfd - gbp_fwd_dpo_pool);
61 }
62
63 static void
64 gbp_fwd_dpo_lock (dpo_id_t * dpo)
65 {
66   gbp_fwd_dpo_t *gfd;
67
68   gfd = gbp_fwd_dpo_get_from_dpo (dpo);
69   gfd->gfd_locks++;
70 }
71
72 static void
73 gbp_fwd_dpo_unlock (dpo_id_t * dpo)
74 {
75   gbp_fwd_dpo_t *gfd;
76
77   gfd = gbp_fwd_dpo_get_from_dpo (dpo);
78   gfd->gfd_locks--;
79
80   if (0 == gfd->gfd_locks)
81     {
82       gbp_fwd_dpo_db[gfd->gfd_proto] = INDEX_INVALID;
83       pool_put (gbp_fwd_dpo_pool, gfd);
84     }
85 }
86
87 void
88 gbp_fwd_dpo_add_or_lock (dpo_proto_t dproto, dpo_id_t * dpo)
89 {
90   gbp_fwd_dpo_t *gfd;
91
92   if (INDEX_INVALID == gbp_fwd_dpo_db[dproto])
93     {
94       gfd = gbp_fwd_dpo_alloc ();
95
96       gfd->gfd_proto = dproto;
97
98       gbp_fwd_dpo_db[dproto] = gbp_fwd_dpo_get_index (gfd);
99     }
100   else
101     {
102       gfd = gbp_fwd_dpo_get (gbp_fwd_dpo_db[dproto]);
103     }
104
105   dpo_set (dpo, gbp_fwd_dpo_type, dproto, gbp_fwd_dpo_get_index (gfd));
106 }
107
108 u8 *
109 format_gbp_fwd_dpo (u8 * s, va_list * ap)
110 {
111   index_t index = va_arg (*ap, index_t);
112   CLIB_UNUSED (u32 indent) = va_arg (*ap, u32);
113   gbp_fwd_dpo_t *gfd = gbp_fwd_dpo_get (index);
114
115   return (format (s, "gbp-fwd-dpo: %U", format_dpo_proto, gfd->gfd_proto));
116 }
117
118 const static dpo_vft_t gbp_fwd_dpo_vft = {
119   .dv_lock = gbp_fwd_dpo_lock,
120   .dv_unlock = gbp_fwd_dpo_unlock,
121   .dv_format = format_gbp_fwd_dpo,
122 };
123
124 /**
125  * @brief The per-protocol VLIB graph nodes that are assigned to a glean
126  *        object.
127  *
128  * this means that these graph nodes are ones from which a glean is the
129  * parent object in the DPO-graph.
130  */
131 const static char *const gbp_fwd_dpo_ip4_nodes[] = {
132   "ip4-gbp-fwd-dpo",
133   NULL,
134 };
135
136 const static char *const gbp_fwd_dpo_ip6_nodes[] = {
137   "ip6-gbp-fwd-dpo",
138   NULL,
139 };
140
141 const static char *const *const gbp_fwd_dpo_nodes[DPO_PROTO_NUM] = {
142   [DPO_PROTO_IP4] = gbp_fwd_dpo_ip4_nodes,
143   [DPO_PROTO_IP6] = gbp_fwd_dpo_ip6_nodes,
144 };
145
146 dpo_type_t
147 gbp_fwd_dpo_get_type (void)
148 {
149   return (gbp_fwd_dpo_type);
150 }
151
152 static clib_error_t *
153 gbp_fwd_dpo_module_init (vlib_main_t * vm)
154 {
155   dpo_proto_t dproto;
156
157   FOR_EACH_DPO_PROTO (dproto)
158   {
159     gbp_fwd_dpo_db[dproto] = INDEX_INVALID;
160   }
161
162   gbp_fwd_dpo_type = dpo_register_new_type (&gbp_fwd_dpo_vft,
163                                             gbp_fwd_dpo_nodes);
164
165   return (NULL);
166 }
167
168 VLIB_INIT_FUNCTION (gbp_fwd_dpo_module_init);
169 #endif /* CLIB_MARCH_VARIANT */
170
171 typedef struct gbp_fwd_dpo_trace_t_
172 {
173   u32 sclass;
174   u32 dpo_index;
175 } gbp_fwd_dpo_trace_t;
176
177 typedef enum
178 {
179   GBP_FWD_DROP,
180   GBP_FWD_FWD,
181   GBP_FWD_N_NEXT,
182 } gbp_fwd_next_t;
183
184 always_inline uword
185 gbp_fwd_dpo_inline (vlib_main_t * vm,
186                     vlib_node_runtime_t * node,
187                     vlib_frame_t * from_frame, fib_protocol_t fproto)
188 {
189   u32 n_left_from, next_index, *from, *to_next;
190
191   from = vlib_frame_vector_args (from_frame);
192   n_left_from = from_frame->n_vectors;
193
194   next_index = node->cached_next_index;
195
196   while (n_left_from > 0)
197     {
198       u32 n_left_to_next;
199
200       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
201
202       while (n_left_from > 0 && n_left_to_next > 0)
203         {
204           const dpo_id_t *next_dpo0;
205           vlib_buffer_t *b0;
206           sclass_t sclass0;
207           u32 bi0, next0;
208
209           bi0 = from[0];
210           to_next[0] = bi0;
211           from += 1;
212           to_next += 1;
213           n_left_from -= 1;
214           n_left_to_next -= 1;
215
216           b0 = vlib_get_buffer (vm, bi0);
217
218           sclass0 = vnet_buffer2 (b0)->gbp.sclass;
219           next_dpo0 = gbp_epg_dpo_lookup (sclass0, fproto);
220
221           if (PREDICT_TRUE (NULL != next_dpo0))
222             {
223               vnet_buffer (b0)->ip.adj_index[VLIB_TX] = next_dpo0->dpoi_index;
224               next0 = GBP_FWD_FWD;
225             }
226           else
227             {
228               next0 = GBP_FWD_DROP;
229             }
230
231           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
232             {
233               gbp_fwd_dpo_trace_t *tr;
234
235               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
236               tr->sclass = sclass0;
237               tr->dpo_index = (NULL != next_dpo0 ?
238                                next_dpo0->dpoi_index : ~0);
239             }
240
241           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
242                                            n_left_to_next, bi0, next0);
243         }
244       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
245     }
246   return from_frame->n_vectors;
247 }
248
249 static u8 *
250 format_gbp_fwd_dpo_trace (u8 * s, va_list * args)
251 {
252   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
253   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
254   gbp_fwd_dpo_trace_t *t = va_arg (*args, gbp_fwd_dpo_trace_t *);
255
256   s = format (s, " sclass:%d dpo:%d", t->sclass, t->dpo_index);
257
258   return s;
259 }
260
261 VLIB_NODE_FN (ip4_gbp_fwd_dpo_node) (vlib_main_t * vm,
262                                      vlib_node_runtime_t * node,
263                                      vlib_frame_t * from_frame)
264 {
265   return (gbp_fwd_dpo_inline (vm, node, from_frame, FIB_PROTOCOL_IP4));
266 }
267
268 VLIB_NODE_FN (ip6_gbp_fwd_dpo_node) (vlib_main_t * vm,
269                                      vlib_node_runtime_t * node,
270                                      vlib_frame_t * from_frame)
271 {
272   return (gbp_fwd_dpo_inline (vm, node, from_frame, FIB_PROTOCOL_IP6));
273 }
274
275 /* *INDENT-OFF* */
276 VLIB_REGISTER_NODE (ip4_gbp_fwd_dpo_node) = {
277     .name = "ip4-gbp-fwd-dpo",
278     .vector_size = sizeof (u32),
279     .format_trace = format_gbp_fwd_dpo_trace,
280     .n_next_nodes = GBP_FWD_N_NEXT,
281     .next_nodes =
282     {
283         [GBP_FWD_DROP] = "ip4-drop",
284         [GBP_FWD_FWD] = "ip4-dvr-dpo",
285     }
286 };
287 VLIB_REGISTER_NODE (ip6_gbp_fwd_dpo_node) = {
288     .name = "ip6-gbp-fwd-dpo",
289     .vector_size = sizeof (u32),
290     .format_trace = format_gbp_fwd_dpo_trace,
291     .n_next_nodes = GBP_FWD_N_NEXT,
292     .next_nodes =
293     {
294         [GBP_FWD_DROP] = "ip6-drop",
295         [GBP_FWD_FWD] = "ip6-dvr-dpo",
296     }
297 };
298 /* *INDENT-ON* */
299
300 /*
301  * fd.io coding-style-patch-verification: ON
302  *
303  * Local Variables:
304  * eval: (c-set-style "gnu")
305  * End:
306  */