b80ba0b844f62a0ca406d62695e38050933c48b4
[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 typedef enum
61 {
62   /* NOTE: ip addresses are left out on purpose. Use max masked ip-prefixes
63    * instead */
64   GID_ADDR_IP_PREFIX,
65   GID_ADDR_LCAF,
66   GID_ADDR_MAC,
67   GID_ADDR_NO_ADDRESS,
68   GID_ADDR_TYPES
69 } gid_address_type_t;
70
71 typedef enum
72 {
73   /* make sure that values corresponds with RFC */
74   LCAF_NULL_BODY = 0,
75   LCAF_AFI_LIST_TYPE,
76   LCAF_INSTANCE_ID,
77   LCAF_TYPES
78 } lcaf_type_t;
79
80 struct _gid_address_t;
81
82 typedef struct
83 {
84   u8 vni_mask_len;
85   u32 vni;
86   struct _gid_address_t *gid_addr;
87 } vni_t;
88
89 #define vni_vni(_a) (_a)->vni
90 #define vni_mask_len(_a) (_a)->vni_mask_len
91 #define vni_gid(_a) (_a)->gid_addr
92
93 typedef struct
94 {
95   u8 src_len;
96   u8 dst_len;
97   struct _gid_address_t *src;
98   struct _gid_address_t *dst;
99 } source_dest_t;
100
101 typedef struct
102 {
103   /* the union needs to be at the beginning! */
104   union
105   {
106     source_dest_t sd;
107     vni_t uni;
108   };
109   u8 type;
110 } lcaf_t;
111
112 #define lcaf_type(_a) (_a)->type
113 #define lcaf_vni(_a) vni_vni(& (_a)->uni)
114 #define lcaf_vni_len(_a) vni_mask_len(& (_a)->uni)
115 #define lcaf_gid (_a) vni_gid(& (_a)->uni)
116
117 /* might want to expand this in the future :) */
118 typedef struct _gid_address_t
119 {
120   union
121   {
122     ip_prefix_t ippref;
123     lcaf_t lcaf;
124     u8 mac[6];
125   };
126   u8 type;
127 } gid_address_t;
128
129 u8 * format_ip_address (u8 * s, va_list * args);
130 uword unformat_ip_address (unformat_input_t * input, va_list * args);
131 u8 * format_ip_prefix (u8 * s, va_list * args);
132 uword unformat_ip_prefix (unformat_input_t * input, va_list * args);
133
134 u16 ip4_address_size_to_put ();
135 u16 ip6_address_size_to_put ();
136 u32 ip4_address_put (u8 * b, ip4_address_t * a);
137 u32 ip6_address_put (u8 * b, ip6_address_t * a);
138
139 u16 ip_address_size_to_write (ip_address_t * a);
140 u16 ip_address_iana_afi(ip_address_t *a);
141 u8 ip_address_max_len (u8 ver);
142 u32 ip_address_put (u8 * b, ip_address_t * a);
143
144 /* LISP AFI codes  */
145 typedef enum {
146     LISP_AFI_NO_ADDR,
147     LISP_AFI_IP,
148     LISP_AFI_IP6,
149     LISP_AFI_MAC = 6,
150     LISP_AFI_LCAF = 16387
151 } lisp_afi_e;
152
153 u8 *format_gid_address (u8 * s, va_list * args);
154 uword unformat_gid_address (unformat_input_t * input, va_list * args);
155 int gid_address_cmp (gid_address_t * a1, gid_address_t * a2);
156 void gid_address_free (gid_address_t *a);
157
158 u16 gid_address_size_to_put (gid_address_t * a);
159 u16 gid_address_put (u8 * b, gid_address_t * gid);
160 u8 gid_address_len (gid_address_t *a);
161 void * gid_address_cast (gid_address_t * gid, gid_address_type_t type);
162 void gid_address_copy(gid_address_t * dst, gid_address_t * src);
163 u32 gid_address_parse (u8 * offset, gid_address_t *a);
164
165 #define gid_address_type(_a) (_a)->type
166 #define gid_address_ippref(_a) (_a)->ippref
167 #define gid_address_ippref_len(_a) (_a)->ippref.len
168 #define gid_address_ip(_a) ip_prefix_addr(&gid_address_ippref(_a))
169 #define gid_address_ip_version(_a) ip_addr_version(&gid_address_ip(_a))
170 #define gid_address_lcaf(_a) (_a)->lcaf
171 #define gid_address_mac(_a) (_a)->mac
172 #define gid_address_vni(_a) ( (GID_ADDR_LCAF == gid_address_type(_a)) ? \
173                               lcaf_vni(&gid_address_lcaf(_a)) : 0)
174 /* setter for vni  */
175 #define gid_address_set_vni(_a, _val) \
176   (lcaf_vni(&gid_address_lcaf(_a)) = (_val))
177
178 /* 'sub'address functions */
179 #define foreach_gid_address_type_fcns  \
180   _(ip_prefix)                    \
181   _(lcaf)                         \
182   _(mac)
183
184 #define _(_n)                                 \
185 u16    _n ## _size_to_write (void * pref);    \
186 u16    _n ## _write (u8 * p, void * pref);    \
187 u8     _n ## _length (void *a);               \
188 void * _n ## _cast (gid_address_t * a);       \
189 void   _n ## _copy (void * dst , void * src);
190
191 foreach_gid_address_type_fcns
192 #undef _
193
194 typedef struct
195 {
196   /* mark locator as local as opposed to remote */
197   u8 local;
198   u8 state;
199   union {
200     u32 sw_if_index;
201     gid_address_t address;
202   };
203   u8 priority;
204   u8 weight;
205   u8 mpriority;
206   u8 mweight;
207 } locator_t;
208
209 u32 locator_parse (void * ptr, locator_t * loc);
210 void locator_copy (locator_t * dst, locator_t * src);
211 u32 locator_cmp (locator_t * l1, locator_t * l2);
212 void locator_free (locator_t * l);
213
214 typedef struct
215 {
216   /* locator-set name */
217   u8 * name;
218
219   /* vector of locator indices */
220   u32 * locator_indices;
221   u8 local;
222 } locator_set_t;
223
224 typedef struct
225 {
226   gid_address_t eid;
227
228   /* index of local locator set */
229   u32 locator_set_index;
230
231   u32 ttl;
232   u8 action;
233   u8 authoritative;
234
235   u8 local;
236 } mapping_t;
237
238 #endif /* VNET_LISP_GPE_LISP_TYPES_H_ */