blob: aff4e1b82cc78062b0a566f2bee8392cc4cb87bd [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * Copyright (c) 2016 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 * @brief
17 * A Data-Path Object is an object that represents actions that are
18 * applied to packets are they are switched through VPP's data-path.
19 *
20 * The DPO can be considered to be like is a base class that is specialised
21 * by other objects to provide concreate actions
22 *
23 * The VLIB graph nodes are graph of DPO types, the DPO graph is a graph of
24 * instances.
25 */
26
27#ifndef __DPO_H__
28#define __DPO_H__
29
30#include <vnet/vnet.h>
31
32/**
33 * @brief An index for adjacencies.
34 * Alas 'C' is not typesafe enough to b0rk when a u32 is used instead of
35 * an index_t. However, for us humans, we can glean much more intent
36 * from the declaration
37 * foo barindex_t t);
38 * than we can from
39 * foo bar(u32 t);
40 */
41typedef u32 index_t;
42
43/**
44 * @brief Invalid index - used when no index is known
45 * blazoned capitals INVALID speak volumes where ~0 does not.
46 */
47#define INDEX_INVALID ((index_t)(~0))
48
49/**
50 * @brief Data path protocol.
51 * Actions performed on packets in the data-plane can be described and represented
52 * by protocol independent objects, i.e. ADJACENCY, but the spceifics actions
53 * required during ADJACENCY processing can be protocol dependent. For example,
54 * the adjacency rewrite node performs a ip4 checksum calculation, ip6 and MPLS
55 * do not, all 3 perform a TTL decrement. The VLIB graph nodes are thus protocol
56 * dependent, and thus each graph edge/arc is too.
57 * When programming a DPO's next node arc from child to parent it is thus required
58 * to know the parent's data-path protocol so the correct arc index can be used.
59 */
60typedef enum dpo_proto_t_
61{
62#if CLIB_DEBUG > 0
63 DPO_PROTO_IP4 = 1,
64#else
65 DPO_PROTO_IP4 = 0,
66#endif
67 DPO_PROTO_IP6,
Neale Ranns5e575b12016-10-03 09:40:25 +010068 DPO_PROTO_ETHERNET,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010069 DPO_PROTO_MPLS,
70} __attribute__((packed)) dpo_proto_t;
71
Neale Ranns450cd302016-11-09 17:49:42 +000072#define DPO_PROTO_NUM ((dpo_proto_t)(DPO_PROTO_MPLS+1))
73#define DPO_PROTO_NONE ((dpo_proto_t)(DPO_PROTO_NUM+1))
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074
75#define DPO_PROTOS { \
76 [DPO_PROTO_IP4] = "ip4", \
77 [DPO_PROTO_IP6] = "ip6", \
Neale Ranns5e575b12016-10-03 09:40:25 +010078 [DPO_PROTO_ETHERNET] = "ethernet", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +010079 [DPO_PROTO_MPLS] = "mpls", \
80}
81
Neale Ranns5e575b12016-10-03 09:40:25 +010082#define FOR_EACH_DPO_PROTO(_proto) \
83 for (_proto = DPO_PROTO_IP4; \
84 _proto <= DPO_PROTO_MPLS; \
85 _proto++)
86
Neale Ranns0bfe5d82016-08-25 15:29:12 +010087/**
88 * @brief Common types of data-path objects
89 * New types can be dynamically added using dpo_register_new_type()
90 */
91typedef enum dpo_type_t_ {
92 /**
93 * A non-zero value first so we can spot unitialisation errors
94 */
95 DPO_FIRST,
96 DPO_DROP,
Neale Ranns948e00f2016-10-20 13:39:34 +010097 DPO_IP_NULL,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010098 DPO_PUNT,
99 /**
100 * @brief load-balancing over a choice of [un]equal cost paths
101 */
102 DPO_LOAD_BALANCE,
Neale Ranns32e1c012016-11-22 17:07:28 +0000103 DPO_REPLICATE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100104 DPO_ADJACENCY,
105 DPO_ADJACENCY_INCOMPLETE,
106 DPO_ADJACENCY_MIDCHAIN,
107 DPO_ADJACENCY_GLEAN,
Neale Ranns32e1c012016-11-22 17:07:28 +0000108 DPO_ADJACENCY_MCAST,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100109 DPO_RECEIVE,
110 DPO_LOOKUP,
111 DPO_LISP_CP,
112 DPO_CLASSIFY,
113 DPO_MPLS_LABEL,
Neale Ranns32e1c012016-11-22 17:07:28 +0000114 DPO_MFIB_ENTRY,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100115 DPO_LAST,
116} __attribute__((packed)) dpo_type_t;
117
118#define DPO_TYPE_NUM DPO_LAST
119
120#define DPO_TYPES { \
121 [DPO_FIRST] = "dpo-invalid", \
122 [DPO_DROP] = "dpo-drop", \
Neale Ranns948e00f2016-10-20 13:39:34 +0100123 [DPO_IP_NULL] = "dpo-ip-null", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100124 [DPO_PUNT] = "dpo-punt", \
125 [DPO_ADJACENCY] = "dpo-adjacency", \
126 [DPO_ADJACENCY_INCOMPLETE] = "dpo-adjacency-incomplete", \
127 [DPO_ADJACENCY_MIDCHAIN] = "dpo-adjacency-midcahin", \
128 [DPO_ADJACENCY_GLEAN] = "dpo-glean", \
Neale Ranns32e1c012016-11-22 17:07:28 +0000129 [DPO_ADJACENCY_MCAST] = "dpo-adj-mcast", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100130 [DPO_RECEIVE] = "dpo-receive", \
131 [DPO_LOOKUP] = "dpo-lookup", \
132 [DPO_LOAD_BALANCE] = "dpo-load-balance", \
Neale Ranns32e1c012016-11-22 17:07:28 +0000133 [DPO_REPLICATE] = "dpo-replicate", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100134 [DPO_LISP_CP] = "dpo-lisp-cp", \
135 [DPO_CLASSIFY] = "dpo-classify", \
Neale Ranns32e1c012016-11-22 17:07:28 +0000136 [DPO_MPLS_LABEL] = "dpo-mpls-label", \
137 [DPO_MFIB_ENTRY] = "dpo-mfib_entry" \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100138}
139
140/**
141 * @brief The identity of a DPO is a combination of its type and its
142 * instance number/index of objects of that type
143 */
144typedef struct dpo_id_t_ {
145 /**
146 * the type
147 */
148 dpo_type_t dpoi_type;
149 /**
150 * the data-path protocol of the type.
151 */
152 dpo_proto_t dpoi_proto;
153 /**
154 * The next VLIB node to follow.
155 */
156 u16 dpoi_next_node;
157 /**
158 * the index of objects of that type
159 */
160 index_t dpoi_index;
161} __attribute__ ((aligned(sizeof(u64)))) dpo_id_t;
162
Damjan Marioncf478942016-11-07 14:57:50 +0100163STATIC_ASSERT(sizeof(dpo_id_t) <= sizeof(u64),
164 "DPO ID is greater than sizeof u64 "
165 "atomic updates need to be revisited");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100166
167/**
Neale Rannsad95b5d2016-11-10 20:35:14 +0000168 * @brief An initialiser for DPOs declared on the stack.
169 * Thenext node is set to 0 since VLIB graph nodes should set 0 index to drop.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170 */
Neale Rannsad95b5d2016-11-10 20:35:14 +0000171#define DPO_INVALID \
172{ \
173 .dpoi_type = DPO_FIRST, \
174 .dpoi_proto = DPO_PROTO_NONE, \
175 .dpoi_index = INDEX_INVALID, \
176 .dpoi_next_node = 0, \
177}
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178
179/**
180 * @brief Return true if the DPO object is valid, i.e. has been initialised.
181 */
182static inline int
183dpo_id_is_valid (const dpo_id_t *dpoi)
184{
185 return (dpoi->dpoi_type != DPO_FIRST &&
186 dpoi->dpoi_index != INDEX_INVALID);
187}
188
Neale Rannsad95b5d2016-11-10 20:35:14 +0000189extern dpo_proto_t vnet_link_to_dpo_proto(vnet_link_t linkt);
190
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191/**
192 * @brief
193 * Take a reference counting lock on the DPO
194 */
195extern void dpo_lock(dpo_id_t *dpo);
196
197/**
198 * @brief
199 * Release a reference counting lock on the DPO
200 */
201extern void dpo_unlock(dpo_id_t *dpo);
202
203/**
204 * @brief Set/create a DPO ID
205 * The DPO will be locked.
206 *
207 * @param dpo
208 * The DPO object to configure
209 *
210 * @param type
211 * The dpo_type_t of the DPO
212 *
213 * @param proto
214 * The dpo_proto_t of the DPO
215 *
216 * @param index
217 * The type specific index of the DPO
218 */
219extern void dpo_set(dpo_id_t *dpo,
220 dpo_type_t type,
221 dpo_proto_t proto,
222 index_t index);
223
224/**
225 * @brief reset a DPO ID
226 * The DPO will be unlocked.
227 *
228 * @param dpo
229 * The DPO object to reset
230 */
231extern void dpo_reset(dpo_id_t *dpo);
232
233/**
234 * @brief compare two DPOs for equality
235 */
236extern int dpo_cmp(const dpo_id_t *dpo1,
237 const dpo_id_t *dpo2);
238
239/**
240 * @brief
241 * atomic copy a data-plane object.
242 * This is safe to use when the dst DPO is currently switching packets
243 */
244extern void dpo_copy(dpo_id_t *dst,
245 const dpo_id_t *src);
246
247/**
248 * @brief Return TRUE is the DPO is any type of adjacency
249 */
250extern int dpo_is_adj(const dpo_id_t *dpo);
251
252/**
253 * @biref Format a DPO_id_t oject
254 */
255extern u8 *format_dpo_id(u8 * s, va_list * args);
256
257/**
258 * @biref format a DPO type
259 */
260extern u8 *format_dpo_type(u8 * s, va_list * args);
261
262/**
263 * @brief format a DPO protocol
264 */
265extern u8 *format_dpo_proto(u8 * s, va_list * args);
266
267/**
268 * @brief
269 * Set and stack a DPO.
270 * The DPO passed is set to the parent DPO and the necessary
271 * VLIB graph arcs are created. The child_type and child_proto
272 * are used to get the VLID nodes from which the arcs are added.
273 *
274 * @param child_type
275 * Child DPO type.
276 *
277 * @param child_proto
278 * Child DPO proto
279 *
280 * @parem dpo
281 * This is the DPO to stack and set.
282 *
283 * @paren parent_dpo
284 * The parent DPO to stack onto.
285 */
286extern void dpo_stack(dpo_type_t child_type,
287 dpo_proto_t child_proto,
288 dpo_id_t *dpo,
289 const dpo_id_t *parent_dpo);
290
291/**
292 * @brief
293 * Set and stack a DPO.
294 * The DPO passed is set to the parent DPO and the necessary
295 * VLIB graph arcs are created, from the child_node passed.
296 *
297 * @param child_node
298 * The VLIB grpah node index to create an arc from to the parent
299 *
300 * @parem dpo
301 * This is the DPO to stack and set.
302 *
303 * @paren parent_dpo
304 * The parent DPO to stack onto.
305 */
306extern void dpo_stack_from_node(u32 child_node,
307 dpo_id_t *dpo,
308 const dpo_id_t *parent);
309
310/**
311 * @brief A lock function registered for a DPO type
312 */
313typedef void (*dpo_lock_fn_t)(dpo_id_t *dpo);
314
315/**
316 * @brief An unlock function registered for a DPO type
317 */
318typedef void (*dpo_unlock_fn_t)(dpo_id_t *dpo);
319
320/**
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100321 * @brief An memory usage show command
322 */
323typedef void (*dpo_mem_show_t)(void);
324
325/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100326 * @brief A virtual function table regisitered for a DPO type
327 */
328typedef struct dpo_vft_t_
329{
330 /**
331 * A reference counting lock function
332 */
333 dpo_lock_fn_t dv_lock;
334 /**
335 * A reference counting unlock function
336 */
337 dpo_lock_fn_t dv_unlock;
338 /**
339 * A format function
340 */
341 format_function_t *dv_format;
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100342 /**
343 * A show memory usage function
344 */
345 dpo_mem_show_t dv_mem_show;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100346} dpo_vft_t;
347
348
349/**
350 * @brief For a given DPO type Register:
351 * - a virtual function table
352 * - a NULL terminated array of graph nodes from which that object type
353 * will originate packets, i.e. the nodes in which the object type will be
354 * the parent DPO in the DP graph. The ndoes are per-data-path protocol
355 * (see above).
356 *
357 * @param type
358 * The type being registered.
359 *
360 * @param vft
361 * The virtual function table to register for the type.
362 *
363 * @param nodes
364 * The string description of the per-protocol VLIB graph nodes.
365 */
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100366extern void dpo_register(dpo_type_t type,
367 const dpo_vft_t *vft,
368 const char * const * const * nodes);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100369
370/**
371 * @brief Create and register a new DPO type.
372 *
373 * This can be used by plugins to create new DPO types that are not listed
374 * in dpo_type_t enum
375 *
376 * @param vft
377 * The virtual function table to register for the type.
378 *
379 * @param nodes
380 * The string description of the per-protocol VLIB graph nodes.
381 *
382 * @return The new dpo_type_t
383 */
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100384extern dpo_type_t dpo_register_new_type(const dpo_vft_t *vft,
385 const char * const * const * nodes);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100386
387#endif