MPLS Unifom mode
[vpp.git] / src / vnet / dpo / dpo.h
1 /*
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  */
41 typedef 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  */
60 typedef enum dpo_proto_t_
61 {
62     DPO_PROTO_IP4 = 0,
63     DPO_PROTO_IP6,
64     DPO_PROTO_MPLS,
65     DPO_PROTO_ETHERNET,
66     DPO_PROTO_BIER,
67     DPO_PROTO_NSH,
68 } __attribute__((packed)) dpo_proto_t;
69
70 #define DPO_PROTO_NUM ((dpo_proto_t)(DPO_PROTO_NSH+1))
71 #define DPO_PROTO_NONE ((dpo_proto_t)(DPO_PROTO_NUM+1))
72
73 #define DPO_PROTOS {            \
74     [DPO_PROTO_IP4]  = "ip4",   \
75     [DPO_PROTO_IP6]  = "ip6",   \
76     [DPO_PROTO_ETHERNET]  = "ethernet", \
77     [DPO_PROTO_MPLS] = "mpls",  \
78     [DPO_PROTO_NSH] = "nsh",    \
79     [DPO_PROTO_BIER] = "bier",  \
80 }
81
82 #define FOR_EACH_DPO_PROTO(_proto)    \
83     for (_proto = DPO_PROTO_IP4;      \
84          _proto <= DPO_PROTO_NSH;    \
85          _proto++)
86
87 /**
88  * @brief Common types of data-path objects
89  * New types can be dynamically added using dpo_register_new_type()
90  */
91 typedef enum dpo_type_t_ {
92     /**
93      * A non-zero value first so we can spot unitialisation errors
94      */
95     DPO_FIRST,
96     DPO_DROP,
97     DPO_IP_NULL,
98     DPO_PUNT,
99     /**
100      * @brief load-balancing over a choice of [un]equal cost paths
101      */
102     DPO_LOAD_BALANCE,
103     DPO_REPLICATE,
104     DPO_ADJACENCY,
105     DPO_ADJACENCY_INCOMPLETE,
106     DPO_ADJACENCY_MIDCHAIN,
107     DPO_ADJACENCY_GLEAN,
108     DPO_ADJACENCY_MCAST,
109     DPO_ADJACENCY_MCAST_MIDCHAIN,
110     DPO_RECEIVE,
111     DPO_LOOKUP,
112     DPO_LISP_CP,
113     DPO_CLASSIFY,
114     DPO_MPLS_DISPOSITION_PIPE,
115     DPO_MPLS_DISPOSITION_UNIFORM,
116     DPO_MFIB_ENTRY,
117     DPO_INTERFACE_RX,
118     DPO_INTERFACE_TX,
119     DPO_DVR,
120     DPO_L3_PROXY,
121     DPO_BIER_TABLE,
122     DPO_BIER_FMASK,
123     DPO_BIER_IMP,
124     DPO_BIER_DISP_TABLE,
125     DPO_BIER_DISP_ENTRY,
126     DPO_IP6_LL,
127     DPO_LAST,
128 } __attribute__((packed)) dpo_type_t;
129
130 #define DPO_TYPE_NUM DPO_LAST
131
132 #define DPO_TYPES {                     \
133     [DPO_FIRST] = "dpo-invalid",        \
134     [DPO_DROP] = "dpo-drop",    \
135     [DPO_IP_NULL] = "dpo-ip-null",              \
136     [DPO_PUNT] = "dpo-punt",    \
137     [DPO_ADJACENCY] = "dpo-adjacency",  \
138     [DPO_ADJACENCY_INCOMPLETE] = "dpo-adjacency-incomplete",    \
139     [DPO_ADJACENCY_MIDCHAIN] = "dpo-adjacency-midcahin",        \
140     [DPO_ADJACENCY_GLEAN] = "dpo-glean",        \
141     [DPO_ADJACENCY_MCAST] = "dpo-adj-mcast",    \
142     [DPO_ADJACENCY_MCAST_MIDCHAIN] = "dpo-adj-mcast-midchain",  \
143     [DPO_RECEIVE] = "dpo-receive",      \
144     [DPO_LOOKUP] = "dpo-lookup",        \
145     [DPO_LOAD_BALANCE] = "dpo-load-balance",    \
146     [DPO_REPLICATE] = "dpo-replicate",  \
147     [DPO_LISP_CP] = "dpo-lisp-cp",      \
148     [DPO_CLASSIFY] = "dpo-classify",    \
149     [DPO_MPLS_DISPOSITION_PIPE] = "dpo-mpls-diposition-pipe", \
150     [DPO_MPLS_DISPOSITION_UNIFORM] = "dpo-mpls-diposition-uniform", \
151     [DPO_MFIB_ENTRY] = "dpo-mfib-entry", \
152     [DPO_INTERFACE_RX] = "dpo-interface-rx",    \
153     [DPO_INTERFACE_TX] = "dpo-interface-tx",    \
154     [DPO_DVR] = "dpo-dvr",      \
155     [DPO_L3_PROXY] = "dpo-l3-proxy",    \
156     [DPO_BIER_TABLE] = "bier-table",    \
157     [DPO_BIER_FMASK] = "bier-fmask",    \
158     [DPO_BIER_IMP] = "bier-imposition", \
159     [DPO_BIER_DISP_ENTRY] = "bier-disp-entry",  \
160     [DPO_BIER_DISP_TABLE] = "bier-disp-table",  \
161     [DPO_IP6_LL] = "ip6-link-local",    \
162 }
163
164 /**
165  * @brief The identity of a DPO is a combination of its type and its
166  * instance number/index of objects of that type
167  */
168 typedef struct dpo_id_t_ {
169     /**
170      * the type
171      */
172     dpo_type_t dpoi_type;
173     /**
174      * the data-path protocol of the type.
175      */
176     dpo_proto_t dpoi_proto;
177     /**
178      * The next VLIB node to follow.
179      */
180     u16 dpoi_next_node;
181     /**
182      * the index of objects of that type
183      */
184     index_t dpoi_index;
185 } __attribute__ ((aligned(sizeof(u64)))) dpo_id_t;
186
187 STATIC_ASSERT(sizeof(dpo_id_t) <= sizeof(u64),
188               "DPO ID is greater than sizeof u64 "
189               "atomic updates need to be revisited");
190
191 /**
192  * @brief An initialiser for DPOs declared on the stack.
193  * Thenext node is set to 0 since VLIB graph nodes should set 0 index to drop.
194  */
195 #define DPO_INVALID                \
196 {                                  \
197     .dpoi_type = DPO_FIRST,        \
198     .dpoi_proto = DPO_PROTO_NONE,  \
199     .dpoi_index = INDEX_INVALID,   \
200     .dpoi_next_node = 0,           \
201 }
202
203 /**
204  * @brief Return true if the DPO object is valid, i.e. has been initialised.
205  */
206 static inline int
207 dpo_id_is_valid (const dpo_id_t *dpoi)
208 {
209     return (dpoi->dpoi_type != DPO_FIRST &&
210             dpoi->dpoi_index != INDEX_INVALID);
211 }
212
213 extern dpo_proto_t vnet_link_to_dpo_proto(vnet_link_t linkt);
214
215 /**
216  * @brief
217  *  Take a reference counting lock on the DPO
218  */
219 extern void dpo_lock(dpo_id_t *dpo);
220
221 /**
222  * @brief
223  *  Release a reference counting lock on the DPO
224  */
225 extern void dpo_unlock(dpo_id_t *dpo);
226
227 /**
228  * @brief Set/create a DPO ID
229  * The DPO will be locked.
230  *
231  * @param dpo
232  *  The DPO object to configure
233  *
234  * @param type
235  *  The dpo_type_t of the DPO
236  *
237  * @param proto
238  *  The dpo_proto_t of the DPO
239  *
240  * @param index
241  *  The type specific index of the DPO
242  */
243 extern void dpo_set(dpo_id_t *dpo,
244                     dpo_type_t type,
245                     dpo_proto_t proto,
246                     index_t index);
247
248 /**
249  * @brief reset a DPO ID
250  * The DPO will be unlocked.
251  *
252  * @param dpo
253  *  The DPO object to reset
254  */
255 extern void dpo_reset(dpo_id_t *dpo);
256
257 /**
258  * @brief compare two DPOs for equality
259  */
260 extern int dpo_cmp(const dpo_id_t *dpo1,
261                    const dpo_id_t *dpo2);
262
263 /**
264  * @brief
265  *  atomic copy a data-plane object.
266  * This is safe to use when the dst DPO is currently switching packets
267  */
268 extern void dpo_copy(dpo_id_t *dst,
269                      const dpo_id_t *src);
270
271 /**
272  * @brief Return TRUE is the DPO is any type of adjacency
273  */
274 extern int dpo_is_adj(const dpo_id_t *dpo);
275
276 /**
277  * @biref Format a DPO_id_t oject
278  */
279 extern u8 *format_dpo_id(u8 * s, va_list * args);
280
281 /**
282  * @biref format a DPO type
283  */
284 extern u8 *format_dpo_type(u8 * s, va_list * args);
285
286 /**
287  * @brief format a DPO protocol
288  */
289 extern u8 *format_dpo_proto(u8 * s, va_list * args);
290
291 /**
292  * @brief format a DPO protocol
293  */
294 extern vnet_link_t dpo_proto_to_link(dpo_proto_t dp);
295
296 /**
297  * @brief
298  *  Set and stack a DPO.
299  *  The DPO passed is set to the parent DPO and the necessary
300  *  VLIB graph arcs are created. The child_type and child_proto
301  * are used to get the VLID nodes from which the arcs are added.
302  *
303  * @param child_type
304  *  Child DPO type.
305  *
306  * @param child_proto
307  *  Child DPO proto
308  *
309  * @parem dpo
310  *  This is the DPO to stack and set.
311  *
312  * @paren parent_dpo
313  *  The parent DPO to stack onto.
314  */
315 extern void dpo_stack(dpo_type_t child_type,
316                       dpo_proto_t child_proto,
317                       dpo_id_t *dpo,
318                       const dpo_id_t *parent_dpo);
319
320 /**
321  * @brief 
322  *  Set and stack a DPO.
323  *  The DPO passed is set to the parent DPO and the necessary
324  *  VLIB graph arcs are created, from the child_node passed.
325  *
326  * @param child_node
327  *  The VLIB grpah node index to create an arc from to the parent
328  *
329  * @param dpo
330  *  This is the DPO to stack and set.
331  *
332  * @param parent_dpo
333  *  The parent DPO to stack onto.
334  */ 
335 extern void dpo_stack_from_node(u32 child_node,
336                                 dpo_id_t *dpo,
337                                 const dpo_id_t *parent);
338
339 /**
340  * Get a uRPF interface for the DPO
341  *
342  * @param dpo
343  *  The DPO from which to get the uRPF interface
344  *
345  * @return valid SW interface index or ~0
346  */
347 extern u32 dpo_get_urpf(const dpo_id_t *dpo);
348
349 /**
350  * @brief  A lock function registered for a DPO type
351  */
352 typedef void (*dpo_lock_fn_t)(dpo_id_t *dpo);
353
354 /**
355  * @brief An unlock function registered for a DPO type
356  */
357 typedef void (*dpo_unlock_fn_t)(dpo_id_t *dpo);
358
359 /**
360  * @brief An memory usage show command
361  */
362 typedef void (*dpo_mem_show_t)(void);
363
364 /**
365  * @brief Given a DPO instance return a vector of node indices that
366  * the type/instance will use.
367  */
368 typedef u32* (*dpo_get_next_node_t)(const dpo_id_t *dpo);
369
370 /**
371  * @brief Given a DPO instance return an interface that can
372  * be used in an uRPF check
373  */
374 typedef u32 (*dpo_get_urpf_t)(const dpo_id_t *dpo);
375
376 /**
377  * @brief A virtual function table regisitered for a DPO type
378  */
379 typedef struct dpo_vft_t_
380 {
381     /**
382      * A reference counting lock function
383      */
384     dpo_lock_fn_t dv_lock;
385     /**
386      * A reference counting unlock function
387      */
388     dpo_lock_fn_t dv_unlock;
389     /**
390      * A format function
391      */
392     format_function_t *dv_format;
393     /**
394      * A show memory usage function
395      */
396     dpo_mem_show_t dv_mem_show;
397     /**
398      * A function to get the next VLIB node given an instance
399      * of the DPO. If this is null, then the node's name MUST be
400      * retreiveable from the nodes names array passed in the register
401      * function
402      */
403     dpo_get_next_node_t dv_get_next_node;
404     /**
405      * Get uRPF interface
406      */
407     dpo_get_urpf_t dv_get_urpf;
408 } dpo_vft_t;
409
410
411 /**
412  * @brief For a given DPO type Register:
413  *   - a virtual function table
414  *   - a NULL terminated array of graph nodes from which that object type
415  *     will originate packets, i.e. the nodes in which the object type will be
416  *     the parent DPO in the DP graph. The ndoes are per-data-path protocol
417  *     (see above).
418  *
419  * @param type
420  *  The type being registered. 
421  *
422  * @param vft
423  *  The virtual function table to register for the type.
424  *
425  * @param nodes
426  *  The string description of the per-protocol VLIB graph nodes.
427  */
428 extern void dpo_register(dpo_type_t type,
429                          const dpo_vft_t *vft,
430                          const char * const * const * nodes);
431
432 /**
433  * @brief Create and register a new DPO type.
434  *
435  * This can be used by plugins to create new DPO types that are not listed
436  * in dpo_type_t enum
437  *
438  * @param vft
439  *  The virtual function table to register for the type.
440  *
441  * @param nodes
442  *  The string description of the per-protocol VLIB graph nodes.
443  *
444  * @return The new dpo_type_t
445  */
446 extern dpo_type_t dpo_register_new_type(const dpo_vft_t *vft,
447                                         const char * const * const * nodes);
448
449 #endif