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