blob: 3356296827296b6ecc768647a339a6d49363bec4 [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{
Neale Ranns0bfe5d82016-08-25 15:29:12 +010062 DPO_PROTO_IP4 = 0,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063 DPO_PROTO_IP6,
64 DPO_PROTO_MPLS,
Neale Rannsda78f952017-05-24 09:15:43 -070065 DPO_PROTO_ETHERNET,
Florin Corasce1b4c72017-01-26 14:25:34 -080066 DPO_PROTO_NSH,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010067} __attribute__((packed)) dpo_proto_t;
68
Florin Corasce1b4c72017-01-26 14:25:34 -080069#define DPO_PROTO_NUM ((dpo_proto_t)(DPO_PROTO_NSH+1))
Neale Ranns450cd302016-11-09 17:49:42 +000070#define DPO_PROTO_NONE ((dpo_proto_t)(DPO_PROTO_NUM+1))
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071
72#define DPO_PROTOS { \
73 [DPO_PROTO_IP4] = "ip4", \
74 [DPO_PROTO_IP6] = "ip6", \
Neale Ranns5e575b12016-10-03 09:40:25 +010075 [DPO_PROTO_ETHERNET] = "ethernet", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +010076 [DPO_PROTO_MPLS] = "mpls", \
Florin Corasce1b4c72017-01-26 14:25:34 -080077 [DPO_PROTO_NSH] = "nsh", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +010078}
79
Neale Ranns5e575b12016-10-03 09:40:25 +010080#define FOR_EACH_DPO_PROTO(_proto) \
81 for (_proto = DPO_PROTO_IP4; \
Florin Corasce1b4c72017-01-26 14:25:34 -080082 _proto <= DPO_PROTO_NSH; \
Neale Ranns5e575b12016-10-03 09:40:25 +010083 _proto++)
84
Neale Ranns0bfe5d82016-08-25 15:29:12 +010085/**
86 * @brief Common types of data-path objects
87 * New types can be dynamically added using dpo_register_new_type()
88 */
89typedef enum dpo_type_t_ {
90 /**
91 * A non-zero value first so we can spot unitialisation errors
92 */
93 DPO_FIRST,
94 DPO_DROP,
Neale Ranns948e00f2016-10-20 13:39:34 +010095 DPO_IP_NULL,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010096 DPO_PUNT,
97 /**
98 * @brief load-balancing over a choice of [un]equal cost paths
99 */
100 DPO_LOAD_BALANCE,
Neale Ranns32e1c012016-11-22 17:07:28 +0000101 DPO_REPLICATE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100102 DPO_ADJACENCY,
103 DPO_ADJACENCY_INCOMPLETE,
104 DPO_ADJACENCY_MIDCHAIN,
105 DPO_ADJACENCY_GLEAN,
Neale Ranns32e1c012016-11-22 17:07:28 +0000106 DPO_ADJACENCY_MCAST,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800107 DPO_ADJACENCY_MCAST_MIDCHAIN,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108 DPO_RECEIVE,
109 DPO_LOOKUP,
110 DPO_LISP_CP,
111 DPO_CLASSIFY,
112 DPO_MPLS_LABEL,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800113 DPO_MPLS_DISPOSITION,
Neale Ranns32e1c012016-11-22 17:07:28 +0000114 DPO_MFIB_ENTRY,
Neale Ranns43161a82017-08-12 02:12:00 -0700115 DPO_INTERFACE_RX,
116 DPO_INTERFACE_TX,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100117 DPO_LAST,
118} __attribute__((packed)) dpo_type_t;
119
120#define DPO_TYPE_NUM DPO_LAST
121
122#define DPO_TYPES { \
123 [DPO_FIRST] = "dpo-invalid", \
124 [DPO_DROP] = "dpo-drop", \
Neale Ranns948e00f2016-10-20 13:39:34 +0100125 [DPO_IP_NULL] = "dpo-ip-null", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126 [DPO_PUNT] = "dpo-punt", \
127 [DPO_ADJACENCY] = "dpo-adjacency", \
128 [DPO_ADJACENCY_INCOMPLETE] = "dpo-adjacency-incomplete", \
129 [DPO_ADJACENCY_MIDCHAIN] = "dpo-adjacency-midcahin", \
130 [DPO_ADJACENCY_GLEAN] = "dpo-glean", \
Neale Ranns32e1c012016-11-22 17:07:28 +0000131 [DPO_ADJACENCY_MCAST] = "dpo-adj-mcast", \
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800132 [DPO_ADJACENCY_MCAST_MIDCHAIN] = "dpo-adj-mcast-midchain", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133 [DPO_RECEIVE] = "dpo-receive", \
134 [DPO_LOOKUP] = "dpo-lookup", \
135 [DPO_LOAD_BALANCE] = "dpo-load-balance", \
Neale Ranns32e1c012016-11-22 17:07:28 +0000136 [DPO_REPLICATE] = "dpo-replicate", \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100137 [DPO_LISP_CP] = "dpo-lisp-cp", \
138 [DPO_CLASSIFY] = "dpo-classify", \
Neale Ranns32e1c012016-11-22 17:07:28 +0000139 [DPO_MPLS_LABEL] = "dpo-mpls-label", \
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800140 [DPO_MPLS_DISPOSITION] = "dpo-mpls-diposition", \
141 [DPO_MFIB_ENTRY] = "dpo-mfib_entry", \
Neale Ranns43161a82017-08-12 02:12:00 -0700142 [DPO_INTERFACE_RX] = "dpo-interface-rx", \
143 [DPO_INTERFACE_TX] = "dpo-interface-tx" \
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100144}
145
146/**
147 * @brief The identity of a DPO is a combination of its type and its
148 * instance number/index of objects of that type
149 */
150typedef struct dpo_id_t_ {
151 /**
152 * the type
153 */
154 dpo_type_t dpoi_type;
155 /**
156 * the data-path protocol of the type.
157 */
158 dpo_proto_t dpoi_proto;
159 /**
160 * The next VLIB node to follow.
161 */
162 u16 dpoi_next_node;
163 /**
164 * the index of objects of that type
165 */
166 index_t dpoi_index;
167} __attribute__ ((aligned(sizeof(u64)))) dpo_id_t;
168
Damjan Marioncf478942016-11-07 14:57:50 +0100169STATIC_ASSERT(sizeof(dpo_id_t) <= sizeof(u64),
170 "DPO ID is greater than sizeof u64 "
171 "atomic updates need to be revisited");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100172
173/**
Neale Rannsad95b5d2016-11-10 20:35:14 +0000174 * @brief An initialiser for DPOs declared on the stack.
175 * Thenext node is set to 0 since VLIB graph nodes should set 0 index to drop.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100176 */
Neale Rannsad95b5d2016-11-10 20:35:14 +0000177#define DPO_INVALID \
178{ \
179 .dpoi_type = DPO_FIRST, \
180 .dpoi_proto = DPO_PROTO_NONE, \
181 .dpoi_index = INDEX_INVALID, \
182 .dpoi_next_node = 0, \
183}
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100184
185/**
186 * @brief Return true if the DPO object is valid, i.e. has been initialised.
187 */
188static inline int
189dpo_id_is_valid (const dpo_id_t *dpoi)
190{
191 return (dpoi->dpoi_type != DPO_FIRST &&
192 dpoi->dpoi_index != INDEX_INVALID);
193}
194
Neale Rannsad95b5d2016-11-10 20:35:14 +0000195extern dpo_proto_t vnet_link_to_dpo_proto(vnet_link_t linkt);
196
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100197/**
198 * @brief
199 * Take a reference counting lock on the DPO
200 */
201extern void dpo_lock(dpo_id_t *dpo);
202
203/**
204 * @brief
205 * Release a reference counting lock on the DPO
206 */
207extern void dpo_unlock(dpo_id_t *dpo);
208
209/**
210 * @brief Set/create a DPO ID
211 * The DPO will be locked.
212 *
213 * @param dpo
214 * The DPO object to configure
215 *
216 * @param type
217 * The dpo_type_t of the DPO
218 *
219 * @param proto
220 * The dpo_proto_t of the DPO
221 *
222 * @param index
223 * The type specific index of the DPO
224 */
225extern void dpo_set(dpo_id_t *dpo,
226 dpo_type_t type,
227 dpo_proto_t proto,
228 index_t index);
229
230/**
231 * @brief reset a DPO ID
232 * The DPO will be unlocked.
233 *
234 * @param dpo
235 * The DPO object to reset
236 */
237extern void dpo_reset(dpo_id_t *dpo);
238
239/**
240 * @brief compare two DPOs for equality
241 */
242extern int dpo_cmp(const dpo_id_t *dpo1,
243 const dpo_id_t *dpo2);
244
245/**
246 * @brief
247 * atomic copy a data-plane object.
248 * This is safe to use when the dst DPO is currently switching packets
249 */
250extern void dpo_copy(dpo_id_t *dst,
251 const dpo_id_t *src);
252
253/**
254 * @brief Return TRUE is the DPO is any type of adjacency
255 */
256extern int dpo_is_adj(const dpo_id_t *dpo);
257
258/**
259 * @biref Format a DPO_id_t oject
260 */
261extern u8 *format_dpo_id(u8 * s, va_list * args);
262
263/**
264 * @biref format a DPO type
265 */
266extern u8 *format_dpo_type(u8 * s, va_list * args);
267
268/**
269 * @brief format a DPO protocol
270 */
271extern u8 *format_dpo_proto(u8 * s, va_list * args);
272
273/**
Neale Rannsda78f952017-05-24 09:15:43 -0700274 * @brief format a DPO protocol
275 */
276extern vnet_link_t dpo_proto_to_link(dpo_proto_t dp);
277
278/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279 * @brief
280 * Set and stack a DPO.
281 * The DPO passed is set to the parent DPO and the necessary
282 * VLIB graph arcs are created. The child_type and child_proto
283 * are used to get the VLID nodes from which the arcs are added.
284 *
285 * @param child_type
286 * Child DPO type.
287 *
288 * @param child_proto
289 * Child DPO proto
290 *
291 * @parem dpo
292 * This is the DPO to stack and set.
293 *
294 * @paren parent_dpo
295 * The parent DPO to stack onto.
296 */
297extern void dpo_stack(dpo_type_t child_type,
298 dpo_proto_t child_proto,
299 dpo_id_t *dpo,
300 const dpo_id_t *parent_dpo);
301
302/**
303 * @brief
304 * Set and stack a DPO.
305 * The DPO passed is set to the parent DPO and the necessary
306 * VLIB graph arcs are created, from the child_node passed.
307 *
308 * @param child_node
309 * The VLIB grpah node index to create an arc from to the parent
310 *
311 * @parem dpo
312 * This is the DPO to stack and set.
313 *
314 * @paren parent_dpo
315 * The parent DPO to stack onto.
316 */
317extern void dpo_stack_from_node(u32 child_node,
318 dpo_id_t *dpo,
319 const dpo_id_t *parent);
320
321/**
322 * @brief A lock function registered for a DPO type
323 */
324typedef void (*dpo_lock_fn_t)(dpo_id_t *dpo);
325
326/**
327 * @brief An unlock function registered for a DPO type
328 */
329typedef void (*dpo_unlock_fn_t)(dpo_id_t *dpo);
330
331/**
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100332 * @brief An memory usage show command
333 */
334typedef void (*dpo_mem_show_t)(void);
335
336/**
Neale Ranns43161a82017-08-12 02:12:00 -0700337 * @brief Given a DPO instance return a vector of node indices that
338 * the type/instance will use.
339 */
340typedef u32* (*dpo_get_next_node_t)(const dpo_id_t *dpo);
341
342/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100343 * @brief A virtual function table regisitered for a DPO type
344 */
345typedef struct dpo_vft_t_
346{
347 /**
348 * A reference counting lock function
349 */
350 dpo_lock_fn_t dv_lock;
351 /**
352 * A reference counting unlock function
353 */
354 dpo_lock_fn_t dv_unlock;
355 /**
356 * A format function
357 */
358 format_function_t *dv_format;
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100359 /**
360 * A show memory usage function
361 */
362 dpo_mem_show_t dv_mem_show;
Neale Ranns43161a82017-08-12 02:12:00 -0700363 /**
364 * A function to get the next VLIB node given an instance
365 * of the DPO. If this is null, then the node's name MUST be
366 * retreiveable from the nodes names array passed in the register
367 * function
368 */
369 dpo_get_next_node_t dv_get_next_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100370} dpo_vft_t;
371
372
373/**
374 * @brief For a given DPO type Register:
375 * - a virtual function table
376 * - a NULL terminated array of graph nodes from which that object type
377 * will originate packets, i.e. the nodes in which the object type will be
378 * the parent DPO in the DP graph. The ndoes are per-data-path protocol
379 * (see above).
380 *
381 * @param type
382 * The type being registered.
383 *
384 * @param vft
385 * The virtual function table to register for the type.
386 *
387 * @param nodes
388 * The string description of the per-protocol VLIB graph nodes.
389 */
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100390extern void dpo_register(dpo_type_t type,
391 const dpo_vft_t *vft,
392 const char * const * const * nodes);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100393
394/**
395 * @brief Create and register a new DPO type.
396 *
397 * This can be used by plugins to create new DPO types that are not listed
398 * in dpo_type_t enum
399 *
400 * @param vft
401 * The virtual function table to register for the type.
402 *
403 * @param nodes
404 * The string description of the per-protocol VLIB graph nodes.
405 *
406 * @return The new dpo_type_t
407 */
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100408extern dpo_type_t dpo_register_new_type(const dpo_vft_t *vft,
409 const char * const * const * nodes);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100410
411#endif