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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <plugins/gbp/gbp.h>
17 #include <plugins/gbp/gbp_fwd_dpo.h>
19 #include <vnet/ethernet/ethernet.h>
22 * The 'DB' of GBP FWD DPOs.
23 * There is one per-proto
25 static index_t gbp_fwd_dpo_db[DPO_PROTO_NUM] = { INDEX_INVALID };
28 * DPO type registered for these GBP FWD
30 static dpo_type_t gbp_fwd_dpo_type;
33 * @brief pool of all interface DPOs
35 gbp_fwd_dpo_t *gbp_fwd_dpo_pool;
37 static gbp_fwd_dpo_t *
38 gbp_fwd_dpo_alloc (void)
42 pool_get (gbp_fwd_dpo_pool, gfd);
47 static inline gbp_fwd_dpo_t *
48 gbp_fwd_dpo_get_from_dpo (const dpo_id_t * dpo)
50 ASSERT (gbp_fwd_dpo_type == dpo->dpoi_type);
52 return (gbp_fwd_dpo_get (dpo->dpoi_index));
56 gbp_fwd_dpo_get_index (gbp_fwd_dpo_t * gfd)
58 return (gfd - gbp_fwd_dpo_pool);
62 gbp_fwd_dpo_lock (dpo_id_t * dpo)
66 gfd = gbp_fwd_dpo_get_from_dpo (dpo);
71 gbp_fwd_dpo_unlock (dpo_id_t * dpo)
75 gfd = gbp_fwd_dpo_get_from_dpo (dpo);
78 if (0 == gfd->gfd_locks)
80 gbp_fwd_dpo_db[gfd->gfd_proto] = INDEX_INVALID;
81 pool_put (gbp_fwd_dpo_pool, gfd);
86 gbp_fwd_dpo_add_or_lock (dpo_proto_t dproto, dpo_id_t * dpo)
90 if (INDEX_INVALID == gbp_fwd_dpo_db[dproto])
92 gfd = gbp_fwd_dpo_alloc ();
94 gfd->gfd_proto = dproto;
96 gbp_fwd_dpo_db[dproto] = gbp_fwd_dpo_get_index (gfd);
100 gfd = gbp_fwd_dpo_get (gbp_fwd_dpo_db[dproto]);
103 dpo_set (dpo, gbp_fwd_dpo_type, dproto, gbp_fwd_dpo_get_index (gfd));
107 format_gbp_fwd_dpo (u8 * s, va_list * ap)
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);
113 return (format (s, "gbp-fwd-dpo: %U", format_dpo_proto, gfd->gfd_proto));
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,
123 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
126 * this means that these graph nodes are ones from which a glean is the
127 * parent object in the DPO-graph.
129 const static char *const gbp_fwd_dpo_ip4_nodes[] = {
134 const static char *const gbp_fwd_dpo_ip6_nodes[] = {
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,
145 gbp_fwd_dpo_get_type (void)
147 return (gbp_fwd_dpo_type);
150 static clib_error_t *
151 gbp_fwd_dpo_module_init (vlib_main_t * vm)
155 FOR_EACH_DPO_PROTO (dproto)
157 gbp_fwd_dpo_db[dproto] = INDEX_INVALID;
160 gbp_fwd_dpo_type = dpo_register_new_type (&gbp_fwd_dpo_vft,
166 VLIB_INIT_FUNCTION (gbp_fwd_dpo_module_init);
168 typedef struct gbp_fwd_dpo_trace_t_
172 } gbp_fwd_dpo_trace_t;
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)
186 u32 n_left_from, next_index, *from, *to_next;
188 from = vlib_frame_vector_args (from_frame);
189 n_left_from = from_frame->n_vectors;
191 next_index = node->cached_next_index;
193 while (n_left_from > 0)
197 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
199 while (n_left_from > 0 && n_left_to_next > 0)
201 const dpo_id_t *next_dpo0;
213 b0 = vlib_get_buffer (vm, bi0);
215 src_epg0 = vnet_buffer2 (b0)->gbp.src_epg;
216 next_dpo0 = gbp_epg_dpo_lookup (src_epg0, fproto);
218 if (PREDICT_TRUE (NULL != next_dpo0))
220 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = next_dpo0->dpoi_index;
225 next0 = GBP_FWD_DROP;
228 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
230 gbp_fwd_dpo_trace_t *tr;
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);
238 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
239 n_left_to_next, bi0, next0);
241 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
243 return from_frame->n_vectors;
247 format_gbp_fwd_dpo_trace (u8 * s, va_list * args)
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 *);
253 s = format (s, " epg:%d dpo:%d", t->src_epg, t->dpo_index);
259 ip4_gbp_fwd_dpo (vlib_main_t * vm,
260 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
262 return (gbp_fwd_dpo_inline (vm, node, from_frame, FIB_PROTOCOL_IP4));
266 ip6_gbp_fwd_dpo (vlib_main_t * vm,
267 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
269 return (gbp_fwd_dpo_inline (vm, node, from_frame, FIB_PROTOCOL_IP6));
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,
281 [GBP_FWD_DROP] = "ip4-drop",
282 [GBP_FWD_FWD] = "ip4-dvr-dpo",
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,
293 [GBP_FWD_DROP] = "ip6-drop",
294 [GBP_FWD_FWD] = "ip6-dvr-dpo",
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)
303 * fd.io coding-style-patch-verification: ON
306 * eval: (c-set-style "gnu")