VPP-197: LISP Source/Dest control plane support
[vpp.git] / vnet / vnet / lisp-cp / lisp_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 VNET_LISP_GPE_LISP_TYPES_H_
17 #define VNET_LISP_GPE_LISP_TYPES_H_
18
19 #include <vnet/ip/ip.h>
20 #include <vnet/lisp-cp/lisp_cp_messages.h>
21
22 typedef enum
23 {
24   IP4,
25   IP6
26 } ip_address_type_t;
27
28 typedef CLIB_PACKED(struct ip_address
29 {
30   union
31   {
32     ip4_address_t v4;
33     ip6_address_t v6;
34   } ip;
35   u8 version;
36 }) ip_address_t;
37
38 #define ip_addr_addr(_a) (_a)->ip
39 #define ip_addr_v4(_a) (_a)->ip.v4
40 #define ip_addr_v6(_a) (_a)->ip.v6
41 #define ip_addr_version(_a) (_a)->version
42
43 int ip_address_cmp (ip_address_t * ip1, ip_address_t * ip2);
44 void ip_address_copy (ip_address_t * dst , ip_address_t * src);
45 void ip_address_copy_addr (void * dst , ip_address_t * src);
46 void ip_address_set(ip_address_t * dst, void * src, u8 version);
47
48 typedef CLIB_PACKED(struct ip_prefix
49 {
50   ip_address_t addr;
51   u8 len;
52 }) ip_prefix_t;
53
54 #define ip_prefix_addr(_a) (_a)->addr
55 #define ip_prefix_version(_a) ip_addr_version(&ip_prefix_addr(_a))
56 #define ip_prefix_len(_a) (_a)->len
57 #define ip_prefix_v4(_a) ip_addr_v4(&ip_prefix_addr(_a))
58 #define ip_prefix_v6(_a) ip_addr_v6(&ip_prefix_addr(_a))
59
60 void ip_prefix_normalize(ip_prefix_t * a);
61
62 typedef enum
63 {
64   /* NOTE: ip addresses are left out on purpose. Use max masked ip-prefixes
65    * instead */
66   GID_ADDR_IP_PREFIX,
67   GID_ADDR_LCAF,
68   GID_ADDR_MAC,
69   GID_ADDR_SRC_DST,
70   GID_ADDR_NO_ADDRESS,
71   GID_ADDR_TYPES
72 } gid_address_type_t;
73
74 typedef enum
75 {
76   /* make sure that values corresponds with RFC */
77   LCAF_NULL_BODY = 0,
78   LCAF_AFI_LIST_TYPE,
79   LCAF_INSTANCE_ID,
80   LCAF_SOURCE_DEST = 12,
81   LCAF_TYPES
82 } lcaf_type_t;
83
84 typedef enum
85 {
86   FID_ADDR_IP_PREF,
87   FID_ADDR_MAC
88 } fid_addr_type_t;
89
90 /* flat address type */
91 typedef struct
92 {
93   union
94   {
95     ip_prefix_t ippref;
96     u8 mac[6];
97   };
98   u8 type; /* fid_addr_type_t */
99 } fid_address_t;
100
101 typedef fid_address_t dp_address_t;
102
103 #define fid_addr_ippref(_a) (_a)->ippref
104 #define fid_addr_mac(_a) (_a)->mac
105 #define fid_addr_type(_a) (_a)->type
106
107 typedef struct
108 {
109   fid_address_t src;
110   fid_address_t dst;
111 } source_dest_t;
112
113 #define sd_dst(_a) (_a)->dst
114 #define sd_src(_a) (_a)->src
115 #define sd_src_ippref(_a) fid_addr_ippref(&sd_src(_a))
116 #define sd_dst_ippref(_a) fid_addr_ippref(&sd_dst(_a))
117 #define sd_src_mac(_a) fid_addr_mac(&sd_src(_a))
118 #define sd_dst_mac(_a) fid_addr_mac(&sd_dst(_a))
119
120 typedef struct
121 {
122   u8 vni_mask_len;
123   u32 vni;
124   struct _gid_address_t *gid_addr;
125 } vni_t;
126
127 #define vni_vni(_a) (_a)->vni
128 #define vni_mask_len(_a) (_a)->vni_mask_len
129 #define vni_gid(_a) (_a)->gid_addr
130
131 typedef struct
132 {
133   /* the union needs to be at the beginning! */
134   union
135   {
136     source_dest_t sd;
137     vni_t uni;
138   };
139   u8 type;
140 } lcaf_t;
141
142 #define lcaf_type(_a) (_a)->type
143 #define lcaf_vni(_a) vni_vni(& (_a)->uni)
144 #define lcaf_vni_len(_a) vni_mask_len(& (_a)->uni)
145
146 /* might want to expand this in the future :) */
147 typedef struct _gid_address_t
148 {
149   union
150   {
151     ip_prefix_t ippref;
152     lcaf_t lcaf;
153     u8 mac[6];
154     source_dest_t sd;
155   };
156   u8 type;
157   u32 vni;
158   u8 vni_mask;
159 } gid_address_t;
160
161 u8 * format_ip_address (u8 * s, va_list * args);
162 uword unformat_ip_address (unformat_input_t * input, va_list * args);
163 u8 * format_ip_prefix (u8 * s, va_list * args);
164 uword unformat_ip_prefix (unformat_input_t * input, va_list * args);
165 u8 * format_mac_address (u8 * s, va_list * args);
166 uword unformat_mac_address (unformat_input_t * input, va_list * args);
167
168 u16 ip4_address_size_to_put ();
169 u16 ip6_address_size_to_put ();
170 u32 ip4_address_put (u8 * b, ip4_address_t * a);
171 u32 ip6_address_put (u8 * b, ip6_address_t * a);
172
173 u16 ip_address_size_to_write (ip_address_t * a);
174 u16 ip_address_iana_afi(ip_address_t *a);
175 u8 ip_address_max_len (u8 ver);
176 u32 ip_address_put (u8 * b, ip_address_t * a);
177
178 /* LISP AFI codes  */
179 typedef enum {
180     LISP_AFI_NO_ADDR,
181     LISP_AFI_IP,
182     LISP_AFI_IP6,
183     LISP_AFI_LCAF = 16387,
184     LISP_AFI_MAC = 16389
185 } lisp_afi_e;
186
187 u8 *format_gid_address (u8 * s, va_list * args);
188 uword unformat_gid_address (unformat_input_t * input, va_list * args);
189 int gid_address_cmp (gid_address_t * a1, gid_address_t * a2);
190 void gid_address_free (gid_address_t *a);
191
192 u16 gid_address_size_to_put (gid_address_t * a);
193 u16 gid_address_put (u8 * b, gid_address_t * gid);
194 u8 gid_address_len (gid_address_t *a);
195 void * gid_address_cast (gid_address_t * gid, gid_address_type_t type);
196 void gid_address_copy(gid_address_t * dst, gid_address_t * src);
197 u32 gid_address_parse (u8 * offset, gid_address_t *a);
198 void gid_address_ip_set(gid_address_t * dst, void * src, u8 version);
199
200 #define gid_address_type(_a) (_a)->type
201 #define gid_address_ippref(_a) (_a)->ippref
202 #define gid_address_ippref_len(_a) (_a)->ippref.len
203 #define gid_address_ip(_a) ip_prefix_addr(&gid_address_ippref(_a))
204 #define gid_address_ip_version(_a) ip_addr_version(&gid_address_ip(_a))
205 #define gid_address_lcaf(_a) (_a)->lcaf
206 #define gid_address_mac(_a) (_a)->mac
207 #define gid_address_vni(_a) (_a)->vni
208 #define gid_address_vni_mask(_a) (_a)->vni_mask
209 #define gid_address_sd_dst_ippref(_a) sd_dst_ippref(&(_a)->sd)
210 #define gid_address_sd_src_ippref(_a) sd_src_ippref(&(_a)->sd)
211 #define gid_address_sd_dst_mac(_a) sd_dst_mac(&(_a)->sd)
212 #define gid_address_sd_src_mac(_a) sd_src_mac(&(_a)->sd)
213 #define gid_address_sd(_a) (_a)->sd
214 #define gid_address_sd_src(_a) sd_src(&gid_address_sd(_a))
215 #define gid_address_sd_dst(_a) sd_dst(&gid_address_sd(_a))
216
217 /* 'sub'address functions */
218 #define foreach_gid_address_type_fcns  \
219   _(ip_prefix)                    \
220   _(lcaf)                         \
221   _(mac)                          \
222   _(sd)
223
224 #define _(_n)                                 \
225 u16    _n ## _size_to_write (void * pref);    \
226 u16    _n ## _write (u8 * p, void * pref);    \
227 u8     _n ## _length (void *a);               \
228 void * _n ## _cast (gid_address_t * a);       \
229 void   _n ## _copy (void * dst , void * src);
230
231 foreach_gid_address_type_fcns
232 #undef _
233
234 #define MAC_BIT_MASK (((u64)1 << 48) - 1)
235
236 typedef struct
237 {
238   /* mark locator as local as opposed to remote */
239   u8 local;
240   u8 state;
241   union {
242     u32 sw_if_index;
243     gid_address_t address;
244   };
245   u8 priority;
246   u8 weight;
247   u8 mpriority;
248   u8 mweight;
249 } locator_t;
250
251 u32 locator_parse (void * ptr, locator_t * loc);
252 void locator_copy (locator_t * dst, locator_t * src);
253 u32 locator_cmp (locator_t * l1, locator_t * l2);
254 void locator_free (locator_t * l);
255
256 typedef struct
257 {
258   /* locator-set name */
259   u8 * name;
260
261   /* vector of locator indices */
262   u32 * locator_indices;
263   u8 local;
264 } locator_set_t;
265
266 typedef struct
267 {
268   gid_address_t eid;
269
270   /* index of local locator set */
271   u32 locator_set_index;
272
273   u32 ttl;
274   u8 action;
275   u8 authoritative;
276
277   u8 local;
278 } mapping_t;
279
280 uword
281 unformat_negative_mapping_action (unformat_input_t * input, va_list * args);
282
283 typedef struct locator_pair
284 {
285   /* local and remote locators (underlay attachment points) */
286   ip_address_t lcl_loc;
287   ip_address_t rmt_loc;
288
289   u8 priority;
290   u8 weight;
291 } locator_pair_t;
292
293 #endif /* VNET_LISP_GPE_LISP_TYPES_H_ */