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