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_fwd_entry.h>
27 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
29 #include <vnet/vnet_msg_enum.h>
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include <vnet/vnet_all_api_h.h>
45 #include <vlibapi/api_helper_macros.h>
47 #define foreach_vpe_api_msg \
48 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry) \
49 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable) \
50 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface) \
51 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)
53 /** Used for transferring locators via VPP API */
55 typedef CLIB_PACKED (struct {
56 u8 is_ip4; /**< is locator an IPv4 address */
57 u8 priority; /**< locator priority */
58 u8 weight; /**< locator weight */
59 u8 addr[16]; /**< IPv4/IPv6 address */
63 static locator_pair_t *
64 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
67 locator_pair_t *pairs = 0, pair;
70 for (i = 0; i < rloc_num; i++)
73 r = &((rloc_t *) lcl_locs)[i];
74 memset (&pair.lcl_loc, 0, sizeof (pair.lcl_loc));
75 ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
78 r = &((rloc_t *) rmt_locs)[i];
79 memset (&pair.rmt_loc, 0, sizeof (pair.rmt_loc));
80 ip_address_set (&pair.rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
82 pair.priority = r->priority;
83 pair.weight = r->weight;
85 vec_add1 (pairs, pair);
91 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
97 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
98 gid_address_ip_set (dst, src, IP4);
99 gid_address_ippref_len (dst) = len;
100 ip_prefix_normalize (&gid_address_ippref (dst));
103 gid_address_type (dst) = GID_ADDR_IP_PREFIX;
104 gid_address_ip_set (dst, src, IP6);
105 gid_address_ippref_len (dst) = len;
106 ip_prefix_normalize (&gid_address_ippref (dst));
109 gid_address_type (dst) = GID_ADDR_MAC;
110 clib_memcpy (&gid_address_mac (dst), src, 6);
114 return VNET_API_ERROR_INVALID_VALUE;
117 gid_address_vni (dst) = vni;
123 vl_api_lisp_gpe_add_del_fwd_entry_t_handler
124 (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
126 vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
127 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
128 locator_pair_t *pairs = 0;
131 memset (a, 0, sizeof (a[0]));
133 rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
134 mp->rmt_eid, mp->rmt_len);
135 rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
136 mp->lcl_eid, mp->lcl_len);
138 pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
140 if (rv || 0 == pairs)
143 a->is_add = mp->is_add;
144 a->locator_pairs = pairs;
145 a->dp_table = mp->dp_table;
147 a->action = mp->action;
149 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
152 REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
156 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
159 vl_api_lisp_gpe_enable_disable_reply_t *rmp;
161 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
163 a->is_en = mp->is_en;
164 vnet_lisp_gpe_enable_disable (a);
166 REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
170 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
172 vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
180 lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
184 lisp_gpe_tenant_l2_iface_unlock (mp->vni);
191 lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
195 lisp_gpe_tenant_l3_iface_unlock (mp->vni);
198 REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
202 send_lisp_gpe_fwd_entry_details (lisp_gpe_fwd_entry_t * lfe,
203 unix_shared_memory_queue_t * q, u32 context)
205 vl_api_lisp_gpe_tunnel_details_t *rmp;
206 lisp_gpe_main_t *lgm = &lisp_gpe_main;
208 rmp = vl_msg_api_alloc (sizeof (*rmp));
209 memset (rmp, 0, sizeof (*rmp));
210 rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
212 rmp->tunnels = lfe - lgm->lisp_fwd_entry_pool;
214 rmp->is_ipv6 = ip_prefix_version (&(lfe->key->rmt.ippref)) == IP6 ? 1 : 0;
215 ip_address_copy_addr (rmp->source_ip,
216 &ip_prefix_addr (&(lfe->key->rmt.ippref)));
217 ip_address_copy_addr (rmp->destination_ip,
218 &ip_prefix_addr (&(lfe->key->rmt.ippref)));
220 rmp->encap_fib_id = htonl (0);
221 rmp->decap_fib_id = htonl (lfe->eid_fib_index);
222 rmp->iid = htonl (lfe->key->vni);
223 rmp->context = context;
225 vl_msg_api_send_shmem (q, (u8 *) & rmp);
229 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
231 unix_shared_memory_queue_t *q = NULL;
232 lisp_gpe_main_t *lgm = &lisp_gpe_main;
233 lisp_gpe_fwd_entry_t *lfe = NULL;
235 if (pool_elts (lgm->lisp_fwd_entry_pool) == 0)
240 q = vl_api_client_index_to_input_queue (mp->client_index);
247 pool_foreach(lfe, lgm->lisp_fwd_entry_pool,
249 send_lisp_gpe_fwd_entry_details(lfe, q, mp->context);
255 * lisp_gpe_api_hookup
256 * Add vpe's API message handlers to the table.
257 * vlib has alread mapped shared memory and
258 * added the client registration handlers.
259 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
261 #define vl_msg_name_crc_list
262 #include <vnet/vnet_all_api_h.h>
263 #undef vl_msg_name_crc_list
266 setup_message_id_table (api_main_t * am)
268 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
269 foreach_vl_msg_name_crc_lisp_gpe;
273 static clib_error_t *
274 lisp_gpe_api_hookup (vlib_main_t * vm)
276 api_main_t *am = &api_main;
279 vl_msg_api_set_handlers(VL_API_##N, #n, \
280 vl_api_##n##_t_handler, \
282 vl_api_##n##_t_endian, \
283 vl_api_##n##_t_print, \
284 sizeof(vl_api_##n##_t), 1);
289 * Set up the (msg_name, crc, message-id) table
291 setup_message_id_table (am);
296 VLIB_API_INIT_FUNCTION (lisp_gpe_api_hookup);
299 * fd.io coding-style-patch-verification: ON
302 * eval: (c-set-style "gnu")