2 *------------------------------------------------------------------
3 * lisp_gpe_api.c - lisp_gpe api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/lisp-gpe/lisp_gpe.h>
26 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
27 #include <vnet/lisp-gpe/lisp_gpe_tunnel.h>
28 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
29 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
31 #include <vnet/vnet_msg_enum.h>
33 #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
38 #define vl_typedefs /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 #define vl_endianfun /* define message structures */
43 #include <vnet/vnet_all_api_h.h>
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49 #include <vnet/vnet_all_api_h.h>
52 #include <vlibapi/api_helper_macros.h>
54 #define foreach_vpe_api_msg \
55 _(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) \
60 _(GPE_SET_ENCAP_MODE, gpe_set_encap_mode) \
61 _(GPE_GET_ENCAP_MODE, gpe_get_encap_mode)
63 static locator_pair_t *
64 unformat_gpe_loc_pairs (void *locs, u32 rloc_num)
67 locator_pair_t *pairs = 0, pair, *p;
68 vl_api_gpe_locator_t *r;
70 for (i = 0; i < rloc_num; i++)
73 r = &((vl_api_gpe_locator_t *) locs)[i];
74 memset (&pair, 0, sizeof (pair));
75 ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
77 pair.weight = r->weight;
78 vec_add1 (pairs, pair);
81 for (i = rloc_num; i < rloc_num * 2; i++)
84 r = &((vl_api_gpe_locator_t *) locs)[i];
85 p = &pairs[i - rloc_num];
86 ip_address_set (&p->rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
92 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
98 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
99 gid_address_ip_set (dst, src, IP4);
100 gid_address_ippref_len (dst) = len;
101 ip_prefix_normalize (&gid_address_ippref (dst));
104 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
105 gid_address_ip_set (dst, src, IP6);
106 gid_address_ippref_len (dst) = len;
107 ip_prefix_normalize (&gid_address_ippref (dst));
110 gid_address_type (dst) = GID_ADDR_MAC;
111 clib_memcpy (&gid_address_mac (dst), src, 6);
115 return VNET_API_ERROR_INVALID_VALUE;
118 gid_address_vni (dst) = vni;
124 gpe_fwd_entry_path_dump_t_net_to_host
125 (vl_api_gpe_fwd_entry_path_dump_t * mp)
127 mp->fwd_entry_index = clib_net_to_host_u32 (mp->fwd_entry_index);
131 lisp_api_set_locator (vl_api_gpe_locator_t * loc,
132 const ip_address_t * addr, u8 weight)
134 loc->weight = weight;
135 if (IP4 == ip_addr_version (addr))
138 memcpy (loc->addr, addr, 4);
143 memcpy (loc->addr, addr, 16);
148 vl_api_gpe_fwd_entry_path_dump_t_handler
149 (vl_api_gpe_fwd_entry_path_dump_t * mp)
151 lisp_fwd_path_t *path;
152 vl_api_gpe_fwd_entry_path_details_t *rmp = NULL;
153 lisp_gpe_main_t *lgm = &lisp_gpe_main;
154 unix_shared_memory_queue_t *q = NULL;
155 lisp_gpe_fwd_entry_t *lfe;
157 gpe_fwd_entry_path_dump_t_net_to_host (mp);
159 q = vl_api_client_index_to_input_queue (mp->client_index);
163 if (pool_is_free_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index))
166 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index);
168 if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
171 vec_foreach (path, lfe->paths)
173 rmp = vl_msg_api_alloc (sizeof (*rmp));
174 memset (rmp, 0, sizeof (*rmp));
175 const lisp_gpe_tunnel_t *lgt;
178 clib_host_to_net_u16 (VL_API_GPE_FWD_ENTRY_PATH_DETAILS);
180 const lisp_gpe_adjacency_t *ladj =
181 lisp_gpe_adjacency_get (path->lisp_adj);
182 lisp_api_set_locator (&rmp->rmt_loc, &ladj->remote_rloc, path->weight);
183 lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
184 lisp_api_set_locator (&rmp->lcl_loc, &lgt->key->lcl, path->weight);
186 rmp->context = mp->context;
187 vl_msg_api_send_shmem (q, (u8 *) & rmp);
192 gpe_fwd_entries_copy (vl_api_gpe_fwd_entry_t * dst,
193 lisp_api_gpe_fwd_entry_t * src)
195 lisp_api_gpe_fwd_entry_t *e;
200 memset (dst, 0, sizeof (*dst));
201 dst[i].dp_table = src->dp_table;
202 dst[i].fwd_entry_index = src->fwd_entry_index;
203 switch (fid_addr_type (&e->leid))
205 case FID_ADDR_IP_PREF:
206 if (IP4 == ip_prefix_version (&fid_addr_ippref (&e->leid)))
208 memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 4);
209 memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 4);
214 memcpy (&dst[i].leid, &fid_addr_ippref (&e->leid), 16);
215 memcpy (&dst[i].reid, &fid_addr_ippref (&e->reid), 16);
218 dst[i].leid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->leid));
219 dst[i].reid_prefix_len = ip_prefix_len (&fid_addr_ippref (&e->reid));
222 memcpy (&dst[i].leid, fid_addr_mac (&e->leid), 6);
223 memcpy (&dst[i].reid, fid_addr_mac (&e->reid), 6);
227 clib_warning ("unknown fid type %d!", fid_addr_type (&e->leid));
235 gpe_fwd_entries_get_t_net_to_host (vl_api_gpe_fwd_entries_get_t * mp)
237 mp->vni = clib_net_to_host_u32 (mp->vni);
241 gpe_entry_t_host_to_net (vl_api_gpe_fwd_entry_t * e)
243 e->fwd_entry_index = clib_host_to_net_u32 (e->fwd_entry_index);
244 e->dp_table = clib_host_to_net_u32 (e->dp_table);
248 gpe_fwd_entries_get_reply_t_host_to_net
249 (vl_api_gpe_fwd_entries_get_reply_t * mp)
252 vl_api_gpe_fwd_entry_t *e;
254 for (i = 0; i < mp->count; i++)
257 gpe_entry_t_host_to_net (e);
259 mp->count = clib_host_to_net_u32 (mp->count);
263 vl_api_gpe_fwd_entries_get_t_handler (vl_api_gpe_fwd_entries_get_t * mp)
265 lisp_api_gpe_fwd_entry_t *e;
266 vl_api_gpe_fwd_entries_get_reply_t *rmp = 0;
270 gpe_fwd_entries_get_t_net_to_host (mp);
272 e = vnet_lisp_gpe_fwd_entries_get_by_vni (mp->vni);
273 size = vec_len (e) * sizeof (vl_api_gpe_fwd_entry_t);
276 REPLY_MACRO4 (VL_API_GPE_FWD_ENTRIES_GET_REPLY, size,
278 rmp->count = vec_len (e);
279 gpe_fwd_entries_copy (rmp->entries, e);
280 gpe_fwd_entries_get_reply_t_host_to_net (rmp);
288 gpe_add_del_fwd_entry_t_net_to_host (vl_api_gpe_add_del_fwd_entry_t * mp)
290 mp->vni = clib_net_to_host_u32 (mp->vni);
291 mp->dp_table = clib_net_to_host_u32 (mp->dp_table);
292 mp->loc_num = clib_net_to_host_u32 (mp->loc_num);
296 vl_api_gpe_add_del_fwd_entry_t_handler (vl_api_gpe_add_del_fwd_entry_t * mp)
298 vl_api_gpe_add_del_fwd_entry_reply_t *rmp;
299 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
300 locator_pair_t *pairs = 0;
303 gpe_add_del_fwd_entry_t_net_to_host (mp);
304 memset (a, 0, sizeof (a[0]));
306 rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
307 mp->rmt_eid, mp->rmt_len);
308 rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
309 mp->lcl_eid, mp->lcl_len);
311 if (mp->loc_num % 2 != 0)
316 pairs = unformat_gpe_loc_pairs (mp->locs, mp->loc_num / 2);
318 if (rv || 0 == pairs)
321 a->is_add = mp->is_add;
322 a->locator_pairs = pairs;
323 a->dp_table = mp->dp_table;
325 a->action = mp->action;
327 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
330 REPLY_MACRO (VL_API_GPE_ADD_DEL_FWD_ENTRY_REPLY);
334 vl_api_gpe_enable_disable_t_handler (vl_api_gpe_enable_disable_t * mp)
336 vl_api_gpe_enable_disable_reply_t *rmp;
338 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
340 a->is_en = mp->is_en;
341 vnet_lisp_gpe_enable_disable (a);
343 REPLY_MACRO (VL_API_GPE_ENABLE_DISABLE_REPLY);
347 vl_api_gpe_add_del_iface_t_handler (vl_api_gpe_add_del_iface_t * mp)
349 vl_api_gpe_add_del_iface_reply_t *rmp;
357 lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
361 lisp_gpe_tenant_l2_iface_unlock (mp->vni);
368 lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
372 lisp_gpe_tenant_l3_iface_unlock (mp->vni);
375 REPLY_MACRO (VL_API_GPE_ADD_DEL_IFACE_REPLY);
379 vl_api_gpe_set_encap_mode_t_handler (vl_api_gpe_set_encap_mode_t * mp)
381 vl_api_gpe_set_encap_mode_reply_t *rmp;
384 rv = vnet_gpe_set_encap_mode (mp->mode);
385 REPLY_MACRO (VL_API_GPE_SET_ENCAP_MODE_REPLY);
389 vl_api_gpe_get_encap_mode_t_handler (vl_api_gpe_get_encap_mode_t * mp)
391 vl_api_gpe_get_encap_mode_reply_t *rmp;
395 REPLY_MACRO2 (VL_API_GPE_GET_ENCAP_MODE_REPLY,
397 rmp->encap_mode = vnet_gpe_get_encap_mode ();
404 * Add vpe's API message handlers to the table.
405 * vlib has alread mapped shared memory and
406 * added the client registration handlers.
407 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
409 #define vl_msg_name_crc_list
410 #include <vnet/vnet_all_api_h.h>
411 #undef vl_msg_name_crc_list
414 setup_message_id_table (api_main_t * am)
416 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
417 foreach_vl_msg_name_crc_lisp_gpe;
421 static clib_error_t *
422 gpe_api_hookup (vlib_main_t * vm)
424 api_main_t *am = &api_main;
427 vl_msg_api_set_handlers(VL_API_##N, #n, \
428 vl_api_##n##_t_handler, \
430 vl_api_##n##_t_endian, \
431 vl_api_##n##_t_print, \
432 sizeof(vl_api_##n##_t), 1);
437 * Set up the (msg_name, crc, message-id) table
439 setup_message_id_table (am);
444 VLIB_API_INIT_FUNCTION (gpe_api_hookup);
447 * fd.io coding-style-patch-verification: ON
450 * eval: (c-set-style "gnu")