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