blob: 8d19f8ce466a7b0d4fffd1460fae3a98e9fde4db [file] [log] [blame]
Pavel Kotucekabea9662016-12-21 15:50:08 +01001/*
2 *------------------------------------------------------------------
3 * lisp_gpe_api.c - lisp_gpe api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/lisp-gpe/lisp_gpe.h>
Filip Tehlar5fae99c2017-01-18 12:57:37 +010026#include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
27#include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
Pavel Kotucekabea9662016-12-21 15:50:08 +010028#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
29#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
30
31#include <vnet/vnet_msg_enum.h>
32
Filip Tehlar82786c42017-02-20 15:20:37 +010033#define vl_api_gpe_locator_pair_t_endian vl_noop_handler
34#define vl_api_gpe_locator_pair_t_print vl_noop_handler
35#define vl_api_gpe_add_del_fwd_entry_t_endian vl_noop_handler
36#define vl_api_gpe_add_del_fwd_entry_t_print vl_noop_handler
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010037
Pavel Kotucekabea9662016-12-21 15:50:08 +010038#define vl_typedefs /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_typedefs
41
42#define vl_endianfun /* define message structures */
43#include <vnet/vnet_all_api_h.h>
44#undef vl_endianfun
45
46/* instantiate all the print functions we know about */
47#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48#define vl_printfun
49#include <vnet/vnet_all_api_h.h>
50#undef vl_printfun
51
52#include <vlibapi/api_helper_macros.h>
53
54#define foreach_vpe_api_msg \
Filip Tehlar82786c42017-02-20 15:20:37 +010055_(GPE_ADD_DEL_FWD_ENTRY, gpe_add_del_fwd_entry) \
56_(GPE_FWD_ENTRIES_GET, gpe_fwd_entries_get) \
57_(GPE_FWD_ENTRY_PATH_DUMP, gpe_fwd_entry_path_dump) \
58_(GPE_ENABLE_DISABLE, gpe_enable_disable) \
59_(GPE_ADD_DEL_IFACE, gpe_add_del_iface)
Pavel Kotucekabea9662016-12-21 15:50:08 +010060
61static locator_pair_t *
Filip Tehlar82786c42017-02-20 15:20:37 +010062unformat_gpe_loc_pairs (void *locs, u32 rloc_num)
Pavel Kotucekabea9662016-12-21 15:50:08 +010063{
64 u32 i;
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010065 locator_pair_t *pairs = 0, pair, *p;
Filip Tehlar82786c42017-02-20 15:20:37 +010066 vl_api_gpe_locator_t *r;
Pavel Kotucekabea9662016-12-21 15:50:08 +010067
68 for (i = 0; i < rloc_num; i++)
69 {
70 /* local locator */
Filip Tehlar82786c42017-02-20 15:20:37 +010071 r = &((vl_api_gpe_locator_t *) locs)[i];
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010072 memset (&pair, 0, sizeof (pair));
Pavel Kotucekabea9662016-12-21 15:50:08 +010073 ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
74
Pavel Kotucekabea9662016-12-21 15:50:08 +010075 pair.weight = r->weight;
Pavel Kotucekabea9662016-12-21 15:50:08 +010076 vec_add1 (pairs, pair);
77 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010078
79 for (i = rloc_num; i < rloc_num * 2; i++)
80 {
81 /* remote locators */
Filip Tehlar82786c42017-02-20 15:20:37 +010082 r = &((vl_api_gpe_locator_t *) locs)[i];
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010083 p = &pairs[i - rloc_num];
84 ip_address_set (&p->rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
85 }
Pavel Kotucekabea9662016-12-21 15:50:08 +010086 return pairs;
87}
88
89static int
90unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
91 u8 len)
92{
93 switch (type)
94 {
95 case 0: /* ipv4 */
96 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
97 gid_address_ip_set (dst, src, IP4);
98 gid_address_ippref_len (dst) = len;
99 ip_prefix_normalize (&gid_address_ippref (dst));
100 break;
101 case 1: /* ipv6 */
102 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
103 gid_address_ip_set (dst, src, IP6);
104 gid_address_ippref_len (dst) = len;
105 ip_prefix_normalize (&gid_address_ippref (dst));
106 break;
107 case 2: /* l2 mac */
108 gid_address_type (dst) = GID_ADDR_MAC;
109 clib_memcpy (&gid_address_mac (dst), src, 6);
110 break;
111 default:
112 /* unknown type */
113 return VNET_API_ERROR_INVALID_VALUE;
114 }
115
116 gid_address_vni (dst) = vni;
117
118 return 0;
119}
120
121static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100122 gpe_fwd_entry_path_dump_t_net_to_host
123 (vl_api_gpe_fwd_entry_path_dump_t * mp)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100124{
125 mp->fwd_entry_index = clib_net_to_host_u32 (mp->fwd_entry_index);
126}
127
128static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100129lisp_api_set_locator (vl_api_gpe_locator_t * loc,
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100130 const ip_address_t * addr, u8 weight)
131{
132 loc->weight = weight;
133 if (IP4 == ip_addr_version (addr))
134 {
135 loc->is_ip4 = 1;
136 memcpy (loc->addr, addr, 4);
137 }
138 else
139 {
140 loc->is_ip4 = 0;
141 memcpy (loc->addr, addr, 16);
142 }
143}
144
145static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100146 vl_api_gpe_fwd_entry_path_dump_t_handler
147 (vl_api_gpe_fwd_entry_path_dump_t * mp)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100148{
149 lisp_fwd_path_t *path;
Filip Tehlar82786c42017-02-20 15:20:37 +0100150 vl_api_gpe_fwd_entry_path_details_t *rmp = NULL;
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100151 lisp_gpe_main_t *lgm = &lisp_gpe_main;
152 unix_shared_memory_queue_t *q = NULL;
153 lisp_gpe_fwd_entry_t *lfe;
154
Filip Tehlar82786c42017-02-20 15:20:37 +0100155 gpe_fwd_entry_path_dump_t_net_to_host (mp);
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100156
157 q = vl_api_client_index_to_input_queue (mp->client_index);
158 if (q == 0)
159 return;
160
161 if (pool_is_free_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index))
162 return;
163
164 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index);
165
166 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
167 return;
168
169 vec_foreach (path, lfe->paths)
170 {
171 rmp = vl_msg_api_alloc (sizeof (*rmp));
172 memset (rmp, 0, sizeof (*rmp));
173 const lisp_gpe_tunnel_t *lgt;
174
175 rmp->_vl_msg_id =
Filip Tehlar82786c42017-02-20 15:20:37 +0100176 clib_host_to_net_u16 (VL_API_GPE_FWD_ENTRY_PATH_DETAILS);
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100177
178 const lisp_gpe_adjacency_t *ladj =
179 lisp_gpe_adjacency_get (path->lisp_adj);
180 lisp_api_set_locator (&rmp->rmt_loc, &ladj->remote_rloc, path->weight);
181 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
182 lisp_api_set_locator (&rmp->lcl_loc, &lgt->key->lcl, path->weight);
183
184 rmp->context = mp->context;
185 vl_msg_api_send_shmem (q, (u8 *) & rmp);
186 }
187}
188
189static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100190gpe_fwd_entries_copy (vl_api_gpe_fwd_entry_t * dst,
191 lisp_api_gpe_fwd_entry_t * src)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100192{
193 lisp_api_gpe_fwd_entry_t *e;
194 u32 i = 0;
195
196 vec_foreach (e, src)
197 {
198 memset (dst, 0, sizeof (*dst));
199 dst[i].dp_table = src->dp_table;
200 dst[i].fwd_entry_index = src->fwd_entry_index;
201 switch (fid_addr_type (&e->leid))
202 {
203 case FID_ADDR_IP_PREF:
204 if (IP4 == ip_prefix_version (&fid_addr_ippref (&e->leid)))
205 {
206 memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 4);
207 memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 4);
208 dst[i].eid_type = 0;
209 }
210 else
211 {
212 memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 16);
213 memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 16);
214 dst[i].eid_type = 1;
215 }
216 dst[i].leid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->leid));
217 dst[i].reid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->reid));
218 break;
219 case FID_ADDR_MAC:
220 memcpy (&dst[i].leid, fid_addr_mac (&e->leid), 6);
221 memcpy (&dst[i].reid, fid_addr_mac (&e->reid), 6);
222 dst[i].eid_type = 2;
223 break;
224 default:
225 clib_warning ("unknown fid type %d!", fid_addr_type (&e->leid));
226 break;
227 }
228 i++;
229 }
230}
231
232static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100233gpe_fwd_entries_get_t_net_to_host (vl_api_gpe_fwd_entries_get_t * mp)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100234{
235 mp->vni = clib_net_to_host_u32 (mp->vni);
236}
237
238static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100239gpe_entry_t_host_to_net (vl_api_gpe_fwd_entry_t * e)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100240{
241 e->fwd_entry_index = clib_host_to_net_u32 (e->fwd_entry_index);
242 e->dp_table = clib_host_to_net_u32 (e->dp_table);
243}
244
245static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100246 gpe_fwd_entries_get_reply_t_host_to_net
247 (vl_api_gpe_fwd_entries_get_reply_t * mp)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100248{
249 u32 i;
Filip Tehlar82786c42017-02-20 15:20:37 +0100250 vl_api_gpe_fwd_entry_t *e;
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100251
252 for (i = 0; i < mp->count; i++)
253 {
254 e = &mp->entries[i];
Filip Tehlar82786c42017-02-20 15:20:37 +0100255 gpe_entry_t_host_to_net (e);
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100256 }
257 mp->count = clib_host_to_net_u32 (mp->count);
258}
259
260static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100261vl_api_gpe_fwd_entries_get_t_handler (vl_api_gpe_fwd_entries_get_t * mp)
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100262{
263 lisp_api_gpe_fwd_entry_t *e;
Filip Tehlar82786c42017-02-20 15:20:37 +0100264 vl_api_gpe_fwd_entries_get_reply_t *rmp = 0;
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100265 u32 size = 0;
266 int rv = 0;
267
Filip Tehlar82786c42017-02-20 15:20:37 +0100268 gpe_fwd_entries_get_t_net_to_host (mp);
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100269
270 e = vnet_lisp_gpe_fwd_entries_get_by_vni (mp->vni);
Filip Tehlar82786c42017-02-20 15:20:37 +0100271 size = vec_len (e) * sizeof (vl_api_gpe_fwd_entry_t);
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100272
273 /* *INDENT-OFF* */
Filip Tehlar82786c42017-02-20 15:20:37 +0100274 REPLY_MACRO4 (VL_API_GPE_FWD_ENTRIES_GET_REPLY, size,
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100275 {
276 rmp->count = vec_len (e);
Filip Tehlar82786c42017-02-20 15:20:37 +0100277 gpe_fwd_entries_copy (rmp->entries, e);
278 gpe_fwd_entries_get_reply_t_host_to_net (rmp);
Filip Tehlar5fae99c2017-01-18 12:57:37 +0100279 });
280 /* *INDENT-ON* */
281
282 vec_free (e);
283}
284
285static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100286gpe_add_del_fwd_entry_t_net_to_host (vl_api_gpe_add_del_fwd_entry_t * mp)
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100287{
288 mp->vni = clib_net_to_host_u32 (mp->vni);
289 mp->dp_table = clib_net_to_host_u32 (mp->dp_table);
290 mp->loc_num = clib_net_to_host_u32 (mp->loc_num);
291}
292
293static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100294vl_api_gpe_add_del_fwd_entry_t_handler (vl_api_gpe_add_del_fwd_entry_t * mp)
Pavel Kotucekabea9662016-12-21 15:50:08 +0100295{
Filip Tehlar82786c42017-02-20 15:20:37 +0100296 vl_api_gpe_add_del_fwd_entry_reply_t *rmp;
Pavel Kotucekabea9662016-12-21 15:50:08 +0100297 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
298 locator_pair_t *pairs = 0;
299 int rv = 0;
300
Filip Tehlar82786c42017-02-20 15:20:37 +0100301 gpe_add_del_fwd_entry_t_net_to_host (mp);
Pavel Kotucekabea9662016-12-21 15:50:08 +0100302 memset (a, 0, sizeof (a[0]));
303
304 rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
305 mp->rmt_eid, mp->rmt_len);
306 rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
307 mp->lcl_eid, mp->lcl_len);
308
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100309 if (mp->loc_num % 2 != 0)
310 {
311 rv = -1;
312 goto send_reply;
313 }
Filip Tehlar82786c42017-02-20 15:20:37 +0100314 pairs = unformat_gpe_loc_pairs (mp->locs, mp->loc_num / 2);
Pavel Kotucekabea9662016-12-21 15:50:08 +0100315
316 if (rv || 0 == pairs)
317 goto send_reply;
318
319 a->is_add = mp->is_add;
320 a->locator_pairs = pairs;
321 a->dp_table = mp->dp_table;
322 a->vni = mp->vni;
323 a->action = mp->action;
324
325 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
326 vec_free (pairs);
327send_reply:
Filip Tehlar82786c42017-02-20 15:20:37 +0100328 REPLY_MACRO (VL_API_GPE_ADD_DEL_FWD_ENTRY_REPLY);
Pavel Kotucekabea9662016-12-21 15:50:08 +0100329}
330
331static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100332vl_api_gpe_enable_disable_t_handler (vl_api_gpe_enable_disable_t * mp)
Pavel Kotucekabea9662016-12-21 15:50:08 +0100333{
Filip Tehlar82786c42017-02-20 15:20:37 +0100334 vl_api_gpe_enable_disable_reply_t *rmp;
Pavel Kotucekabea9662016-12-21 15:50:08 +0100335 int rv = 0;
336 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
337
338 a->is_en = mp->is_en;
339 vnet_lisp_gpe_enable_disable (a);
340
Filip Tehlar82786c42017-02-20 15:20:37 +0100341 REPLY_MACRO (VL_API_GPE_ENABLE_DISABLE_REPLY);
Pavel Kotucekabea9662016-12-21 15:50:08 +0100342}
343
344static void
Filip Tehlar82786c42017-02-20 15:20:37 +0100345vl_api_gpe_add_del_iface_t_handler (vl_api_gpe_add_del_iface_t * mp)
Pavel Kotucekabea9662016-12-21 15:50:08 +0100346{
Filip Tehlar82786c42017-02-20 15:20:37 +0100347 vl_api_gpe_add_del_iface_reply_t *rmp;
Pavel Kotucekabea9662016-12-21 15:50:08 +0100348 int rv = 0;
349
350 if (mp->is_l2)
351 {
352 if (mp->is_add)
353 {
354 if (~0 ==
355 lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
356 rv = 1;
357 }
358 else
359 lisp_gpe_tenant_l2_iface_unlock (mp->vni);
360 }
361 else
362 {
363 if (mp->is_add)
364 {
365 if (~0 ==
366 lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
367 rv = 1;
368 }
369 else
370 lisp_gpe_tenant_l3_iface_unlock (mp->vni);
371 }
372
Filip Tehlar82786c42017-02-20 15:20:37 +0100373 REPLY_MACRO (VL_API_GPE_ADD_DEL_IFACE_REPLY);
Pavel Kotucekabea9662016-12-21 15:50:08 +0100374}
375
Pavel Kotucekabea9662016-12-21 15:50:08 +0100376/*
Filip Tehlar82786c42017-02-20 15:20:37 +0100377 * gpe_api_hookup
Pavel Kotucekabea9662016-12-21 15:50:08 +0100378 * Add vpe's API message handlers to the table.
379 * vlib has alread mapped shared memory and
380 * added the client registration handlers.
381 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
382 */
383#define vl_msg_name_crc_list
384#include <vnet/vnet_all_api_h.h>
385#undef vl_msg_name_crc_list
386
387static void
388setup_message_id_table (api_main_t * am)
389{
390#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
391 foreach_vl_msg_name_crc_lisp_gpe;
392#undef _
393}
394
395static clib_error_t *
Filip Tehlar82786c42017-02-20 15:20:37 +0100396gpe_api_hookup (vlib_main_t * vm)
Pavel Kotucekabea9662016-12-21 15:50:08 +0100397{
398 api_main_t *am = &api_main;
399
400#define _(N,n) \
401 vl_msg_api_set_handlers(VL_API_##N, #n, \
402 vl_api_##n##_t_handler, \
403 vl_noop_handler, \
404 vl_api_##n##_t_endian, \
405 vl_api_##n##_t_print, \
406 sizeof(vl_api_##n##_t), 1);
407 foreach_vpe_api_msg;
408#undef _
409
410 /*
411 * Set up the (msg_name, crc, message-id) table
412 */
413 setup_message_id_table (am);
414
415 return 0;
416}
417
Filip Tehlar82786c42017-02-20 15:20:37 +0100418VLIB_API_INIT_FUNCTION (gpe_api_hookup);
Pavel Kotucekabea9662016-12-21 15:50:08 +0100419
420/*
421 * fd.io coding-style-patch-verification: ON
422 *
423 * Local Variables:
424 * eval: (c-set-style "gnu")
425 * End:
426 */