FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
gbp_fwd_dpo.c
Go to the documentation of this file.
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>
18 
19 #include <vnet/ethernet/ethernet.h>
20 
21 /**
22  * The 'DB' of GBP FWD DPOs.
23  * There is one per-proto
24  */
26 
27 /**
28  * DPO type registered for these GBP FWD
29  */
31 
32 /**
33  * @brief pool of all interface DPOs
34  */
36 
37 static gbp_fwd_dpo_t *
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 *
49 {
51 
52  return (gbp_fwd_dpo_get (dpo->dpoi_index));
53 }
54 
55 static inline index_t
57 {
58  return (gfd - gbp_fwd_dpo_pool);
59 }
60 
61 static void
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
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  {
81  pool_put (gbp_fwd_dpo_pool, gfd);
82  }
83 }
84 
85 void
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 = {
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] = {
142 };
143 
146 {
147  return (gbp_fwd_dpo_type);
148 }
149 
150 static clib_error_t *
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,
162 
163  return (NULL);
164 }
165 
167 
168 typedef struct gbp_fwd_dpo_trace_t_
169 {
173 
174 typedef enum
175 {
180 
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  {
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
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
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* */
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 };
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 
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  */
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:404
u16 epg_id_t
Definition: gbp_types.h:21
#define CLIB_UNUSED(x)
Definition: clib.h:82
A virtual function table regisitered for a DPO type.
Definition: dpo.h:399
gbp_fwd_next_t
Definition: gbp_fwd_dpo.c:174
static index_t gbp_fwd_dpo_db[DPO_PROTO_NUM]
The &#39;DB&#39; of GBP FWD DPOs.
Definition: gbp_fwd_dpo.c:25
#define vnet_buffer2(b)
Definition: buffer.h:413
#define PREDICT_TRUE(x)
Definition: clib.h:112
#define NULL
Definition: clib.h:58
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
static clib_error_t * gbp_fwd_dpo_module_init(vlib_main_t *vm)
Definition: gbp_fwd_dpo.c:151
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static uword gbp_fwd_dpo_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame, fib_protocol_t fproto)
Definition: gbp_fwd_dpo.c:182
u16 gfd_locks
number of locks.
Definition: gbp_fwd_dpo.h:36
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
static gbp_fwd_dpo_t * gbp_fwd_dpo_alloc(void)
Definition: gbp_fwd_dpo.c:38
vlib_node_registration_t ip6_gbp_fwd_dpo_node
(constructor) VLIB_REGISTER_NODE (ip6_gbp_fwd_dpo_node)
Definition: gbp_fwd_dpo.c:285
enum dpo_type_t_ dpo_type_t
Common types of data-path objects New types can be dynamically added using dpo_register_new_type() ...
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define always_inline
Definition: clib.h:98
static const char *const gbp_fwd_dpo_ip4_nodes[]
The per-protocol VLIB graph nodes that are assigned to a glean object.
Definition: gbp_fwd_dpo.c:129
unsigned int u32
Definition: types.h:88
enum dpo_proto_t_ dpo_proto_t
Data path protocol.
dpo_type_t dpo_register_new_type(const dpo_vft_t *vft, const char *const *const *nodes)
Create and register a new DPO type.
Definition: dpo.c:341
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:223
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:168
dpo_type_t dpoi_type
the type
Definition: dpo.h:172
static const dpo_id_t * gbp_epg_dpo_lookup(epg_id_t epg, fib_protocol_t fproto)
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
struct gbp_fwd_dpo_trace_t_ gbp_fwd_dpo_trace_t
#define PREDICT_FALSE(x)
Definition: clib.h:111
static gbp_fwd_dpo_t * gbp_fwd_dpo_get_from_dpo(const dpo_id_t *dpo)
Definition: gbp_fwd_dpo.c:48
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
static u8 * format_gbp_fwd_dpo_trace(u8 *s, va_list *args)
Definition: gbp_fwd_dpo.c:247
static uword ip4_gbp_fwd_dpo(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: gbp_fwd_dpo.c:259
static gbp_fwd_dpo_t * gbp_fwd_dpo_get(index_t index)
Definition: gbp_fwd_dpo.h:49
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
dpo_type_t gbp_fwd_dpo_get_type(void)
Definition: gbp_fwd_dpo.c:145
u16 n_vectors
Definition: node.h:420
u8 * format_gbp_fwd_dpo(u8 *s, va_list *ap)
Definition: gbp_fwd_dpo.c:107
vlib_main_t * vm
Definition: buffer.c:301
vlib_node_registration_t ip4_gbp_fwd_dpo_node
(constructor) VLIB_REGISTER_NODE (ip4_gbp_fwd_dpo_node)
Definition: gbp_fwd_dpo.c:273
void gbp_fwd_dpo_add_or_lock(dpo_proto_t dproto, dpo_id_t *dpo)
Definition: gbp_fwd_dpo.c:86
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:452
void dpo_set(dpo_id_t *dpo, dpo_type_t type, dpo_proto_t proto, index_t index)
Set/create a DPO ID The DPO will be locked.
Definition: dpo.c:185
static index_t gbp_fwd_dpo_get_index(gbp_fwd_dpo_t *gfd)
Definition: gbp_fwd_dpo.c:56
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:537
#define ASSERT(truth)
static void gbp_fwd_dpo_unlock(dpo_id_t *dpo)
Definition: gbp_fwd_dpo.c:71
static dpo_type_t gbp_fwd_dpo_type
DPO type registered for these GBP FWD.
Definition: gbp_fwd_dpo.c:30
gbp_fwd_dpo_t * gbp_fwd_dpo_pool
pool of all interface DPOs
Definition: gbp_fwd_dpo.c:35
static void gbp_fwd_dpo_lock(dpo_id_t *dpo)
Definition: gbp_fwd_dpo.c:62
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
static const char *const gbp_fwd_dpo_ip6_nodes[]
Definition: gbp_fwd_dpo.c:134
Definition: defs.h:47
#define DPO_PROTO_NUM
Definition: dpo.h:70
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:184
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
u8 * format_dpo_proto(u8 *s, va_list *args)
format a DPO protocol
Definition: dpo.c:177
The GBP FWD DPO.
Definition: gbp_fwd_dpo.h:26
#define vnet_buffer(b)
Definition: buffer.h:368
static const char *const *const gbp_fwd_dpo_nodes[DPO_PROTO_NUM]
Definition: gbp_fwd_dpo.c:139
static uword ip6_gbp_fwd_dpo(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: gbp_fwd_dpo.c:266
#define FOR_EACH_DPO_PROTO(_proto)
Definition: dpo.h:82
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:117
dpo_proto_t gfd_proto
The protocol of packets using this DPO.
Definition: gbp_fwd_dpo.h:31
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62