A Protocol Independent Hierarchical FIB (VPP-352)
[vpp.git] / vnet / vnet / fib / fib_types.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 #ifndef __FIB_TYPES_H__
17 #define __FIB_TYPES_H__
18
19 #include <vlib/vlib.h>
20 #include <vnet/ip/ip6_packet.h>
21 #include <vnet/mpls/packet.h>
22 #include <vnet/dpo/dpo.h>
23
24 /**
25  * A typedef of a node index.
26  * we make this typedef so the code becomes easier for a human to parse.
27  */
28 typedef u32 fib_node_index_t;
29 #define FIB_NODE_INDEX_INVALID ((fib_node_index_t)(~0))
30
31 /**
32  * Protocol Type. packed so it consumes a u8 only
33  */
34 typedef enum fib_protocol_t_ {
35 #if CLIB_DEBUG > 0
36     FIB_PROTOCOL_IP4 = 1,
37 #else
38     FIB_PROTOCOL_IP4 = 0,
39 #endif
40     FIB_PROTOCOL_IP6,
41     FIB_PROTOCOL_MPLS,
42 }  __attribute__ ((packed)) fib_protocol_t;
43
44 #define FIB_PROTOCOLS {                 \
45     [FIB_PROTOCOL_IP4] = "ipv4",        \
46     [FIB_PROTOCOL_IP6] = "ipv6",        \
47     [FIB_PROTOCOL_MPLS] = "MPLS",       \
48 }
49
50 /**
51  * Definition outside of enum so it does not need to be included in non-defaulted
52  * switch statements
53  */
54 #define FIB_PROTOCOL_MAX (FIB_PROTOCOL_MPLS + 1)
55
56 /**
57  * Not part of the enum so it does not have to be handled in switch statements
58  */
59 #define FIB_PROTOCOL_NONE (FIB_PROTOCOL_MAX+1)
60
61 /**
62  * Link Type. This maps directly into the ethertype.
63  */
64 typedef enum fib_link_t_ {
65 #if CLIB_DEBUG > 0
66     FIB_LINK_IP4 = 1,
67 #else
68     FIB_LINK_IP4 = 0,
69 #endif
70     FIB_LINK_IP6,
71     FIB_LINK_MPLS,
72 }  __attribute__ ((packed)) fib_link_t;
73
74 /**
75  * Definition outside of enum so it does not need to be included in non-defaulted
76  * switch statements
77  */
78 #define FIB_LINK_NUM (FIB_LINK_MPLS+1)
79
80 #define FIB_LINKS {             \
81     [FIB_LINK_IP4] = "ipv4",    \
82     [FIB_LINK_IP6] = "ipv6",   \
83     [FIB_LINK_MPLS] = "mpls",   \
84 }
85
86 #define FOR_EACH_FIB_LINK(_item)  \
87     for (_item = FIB_LINK_IP4;    \
88          _item <= FIB_LINK_MPLS;  \
89          _item++)
90
91 #define FOR_EACH_FIB_IP_LINK(_item)  \
92     for (_item = FIB_LINK_IP4;       \
93          _item <= FIB_LINK_IP6;      \
94          _item++)
95
96 /**
97  * @brief Convert from a protocol to a link type
98  */
99 fib_link_t fib_proto_to_link (fib_protocol_t proto);
100
101 /**
102  * FIB output chain type. When a child object requests a forwarding contribution
103  * from a parent, it does so for a particular scenario. This enumererates those
104  * sceanrios
105  */
106 typedef enum fib_forward_chain_type_t_ {
107     /**
108      * Contribute an object that is to be used to forward IP4 packets
109      */
110     FIB_FORW_CHAIN_TYPE_UNICAST_IP4,
111     /**
112      * Contribute an object that is to be used to forward IP6 packets
113      */
114     FIB_FORW_CHAIN_TYPE_UNICAST_IP6,
115     /**
116      * Contribute an object that is to be used to forward non-end-of-stack
117      * MPLS packets
118      */
119     FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
120     /**
121      * Contribute an object that is to be used to forward end-of-stack
122      * MPLS packets. This is a convenient ID for clients. A real EOS chain
123      * must be pay-load protocol specific. This
124      * option is converted into one of the other three internally.
125      */
126     FIB_FORW_CHAIN_TYPE_MPLS_EOS,
127 }  __attribute__ ((packed)) fib_forward_chain_type_t;
128
129 #define FIB_FORW_CHAINS {                                       \
130     [FIB_FORW_CHAIN_TYPE_UNICAST_IP4]   = "unicast-ip4",        \
131     [FIB_FORW_CHAIN_TYPE_UNICAST_IP6]   = "unicast-ip6",        \
132     [FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS]  = "mpls-neos",          \
133     [FIB_FORW_CHAIN_TYPE_MPLS_EOS]      = "mpls-eos",           \
134 }
135
136 #define FIB_FORW_CHAIN_NUM (FIB_FORW_CHAIN_TYPE_MPLS_EOS+1)
137
138 #define FOR_EACH_FIB_FORW_CHAIN(_item)                    \
139     for (_item = FIB_FORW_CHAIN_TYPE_UNICAST_IP4;         \
140          _item <= FIB_FORW_CHAIN_TYPE_MPLS_EOS;           \
141          _item++)
142
143 /**
144  * @brief Convert from a chain type to the adjacencies link type
145  */
146 extern fib_link_t fib_forw_chain_type_to_link_type(fib_forward_chain_type_t fct);
147
148 /**
149  * @brief Convert from a payload-protocol to a chain type.
150  */
151 extern fib_forward_chain_type_t fib_proto_to_forw_chain_type(fib_protocol_t proto);
152
153 /**
154  * @brief Convert from a chain type to the DPO proto it will install
155  */
156 extern dpo_proto_t fib_forw_chain_type_to_dpo_proto(fib_forward_chain_type_t fct);
157
158 /**
159  * Aggregrate type for a prefix
160  */
161 typedef struct fib_prefix_t_ {
162     /**
163      * The mask length
164      */
165     u16 fp_len;
166
167     /**
168      * protocol type
169      */
170     fib_protocol_t fp_proto;
171
172     /**
173      * Pad to keep the address 4 byte aligned
174      */
175     u8 ___fp___pad;
176
177     union {
178         /**
179          * The address type is not deriveable from the fp_addr member.
180          * If it's v4, then the first 3 u32s of the address will be 0.
181          * v6 addresses (even v4 mapped ones) have at least 2 u32s assigned
182          * to non-zero values. true. but when it's all zero, one cannot decide.
183          */
184         ip46_address_t fp_addr;
185
186         struct {
187             mpls_label_t fp_label;
188             mpls_eos_bit_t fp_eos;
189             /**
190              * This protocol determines the payload protocol of packets
191              * that will be forwarded by this entry once the label is popped.
192              * For a non-eos entry it will be MPLS.
193              */
194             dpo_proto_t fp_payload_proto;
195         };
196     };
197 } fib_prefix_t;
198
199 _Static_assert(STRUCT_OFFSET_OF(fib_prefix_t, fp_addr) == 4,
200                "FIB Prefix's address is 4 byte aligned.");
201
202 /**
203  * \brief Compare two prefixes for equality
204  */
205 extern int fib_prefix_cmp(const fib_prefix_t *p1,
206                           const fib_prefix_t *p2);
207
208 /**
209  * \brief Compare two prefixes for covering relationship
210  *
211  * \return non-zero if the first prefix is a cover for the second
212  */
213 extern int fib_prefix_is_cover(const fib_prefix_t *p1,
214                                const fib_prefix_t *p2);
215
216 /**
217  * \brief Return true is the prefix is a host prefix
218  */
219 extern int fib_prefix_is_host(const fib_prefix_t *p);
220
221 extern u8 * format_fib_prefix(u8 * s, va_list * args);
222 extern u8 * format_fib_forw_chain_type(u8 * s, va_list * args);
223
224 extern dpo_proto_t fib_proto_to_dpo(fib_protocol_t fib_proto);
225 extern fib_protocol_t dpo_proto_to_fib(dpo_proto_t dpo_proto);
226
227 /**
228  * Enurmeration of special path/entry types
229  */
230 typedef enum fib_special_type_t_ {
231     /**
232      * Marker. Add new types after this one.
233      */
234     FIB_SPECIAL_TYPE_FIRST = 0,
235     /**
236      * Local/for-us paths
237      */
238     FIB_SPECIAL_TYPE_LOCAL = FIB_SPECIAL_TYPE_FIRST,
239     /**
240      * drop paths
241      */
242     FIB_SPECIAL_TYPE_DROP,
243     /**
244      * Marker. Add new types before this one, then update it.
245      */
246     FIB_SPECIAL_TYPE_LAST = FIB_SPECIAL_TYPE_DROP,
247 } __attribute__ ((packed)) fib_special_type_t;
248
249 /**
250  * The maximum number of types
251  */
252 #define FIB_SPEICAL_TYPE_MAX (FIB_SPEICAL_TYPE_LAST + 1)
253
254 #define FOR_EACH_FIB_SPEICAL_TYPE(_item)                \
255     for (_item = FIB_TYPE_SPEICAL_FIRST;                \
256          _item <= FIB_SPEICAL_TYPE_LAST; _item++)
257
258 extern u8 * format_fib_protocol(u8 * s, va_list ap);
259 extern u8 * format_fib_link(u8 *s, va_list ap);
260
261 /**
262  * Path flags from the control plane
263  */
264 typedef enum fib_route_path_flags_t_
265 {
266     FIB_ROUTE_PATH_FLAG_NONE = 0,
267     /**
268      * Recursion constraint of via a host prefix
269      */
270     FIB_ROUTE_PATH_RESOLVE_VIA_HOST = (1 << 0),
271     /**
272      * Recursion constraint of via an attahced prefix
273      */
274     FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED = (1 << 1),
275 } fib_route_path_flags_t;
276
277 /**
278  * @brief 
279  * A representation of a path as described by a route producer.
280  * These paramenters will determine the path 'type', of which there are:
281  * 1) Attached-next-hop:
282  *   a single peer on a link.
283  *   It is 'attached' because it is in the same sub-net as the router, on a link
284  *   directly connected to the route.
285  *   It is 'next=hop' since the next-hop address of the peer is known.
286  * 2) Attached:
287  *  the next-hop is not known. but we can ARP for it.
288  * 3) Recursive.
289  *  The next-hop is known but the interface is not. So to find the adj to use
290  *  we must recursively resolve the next-hop.
291  * 3) deaggregate (deag)
292  *  A further lookup is required.
293  */
294 typedef struct fib_route_path_t_ {
295     /**
296      * The protocol of the address below. We need this since the all
297      * zeros address is ambiguous.
298      */
299     fib_protocol_t frp_proto;
300     /**
301      * The next-hop address.
302      * Will be NULL for attached paths.
303      * Will be all zeros for attached-next-hop paths on a p2p interface
304      * Will be all zeros for a deag path.
305      */
306     ip46_address_t frp_addr;
307     /**
308      * The interface.
309      * Will be invalid for recursive paths.
310      */
311     u32 frp_sw_if_index;
312     /**
313      * The FIB index to lookup the nexthop
314      * Only valid for recursive paths.
315      */
316     u32 frp_fib_index;
317     /**
318      * [un]equal cost path weight
319      */
320     u32 frp_weight;
321     /**
322      * flags on the path
323      */
324     fib_route_path_flags_t frp_flags;
325     /**
326      * The outgoing MPLS label. INVALID implies no label.
327      */
328     mpls_label_t frp_label;
329 } fib_route_path_t;
330
331 #endif