f77a6f4059f4af95b6ea5dfa4b0a5efce95bb039
[vpp.git] / src / plugins / lisp / lisp-gpe / lisp_gpe_api.c
1 /*
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 <lisp/lisp-gpe/lisp_gpe.h>
26 #include <lisp/lisp-gpe/lisp_gpe_adjacency.h>
27 #include <lisp/lisp-gpe/lisp_gpe_tunnel.h>
28 #include <lisp/lisp-gpe/lisp_gpe_fwd_entry.h>
29 #include <lisp/lisp-gpe/lisp_gpe_tenant.h>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/ip/ip_types_api.h>
32 #include <vnet/ethernet/ethernet_types_api.h>
33 #include <lisp/lisp-gpe/lisp_types_api.h>
34
35 /* define message IDs */
36 #include <vnet/format_fns.h>
37 #include <lisp/lisp-gpe/lisp_gpe.api_enum.h>
38 #include <lisp/lisp-gpe/lisp_gpe.api_types.h>
39
40 /**
41  * Base message ID fot the plugin
42  */
43 static u32 gpe_base_msg_id;
44 #define REPLY_MSG_ID_BASE gpe_base_msg_id
45
46 #include <vlibapi/api_helper_macros.h>
47
48 static locator_pair_t *
49 unformat_gpe_loc_pairs (void *locs, u32 rloc_num)
50 {
51   u32 i;
52   locator_pair_t *pairs = 0, pair, *p;
53   vl_api_gpe_locator_t *r;
54
55   for (i = 0; i < rloc_num; i++)
56     {
57       /* local locator */
58       r = &((vl_api_gpe_locator_t *) locs)[i];
59       clib_memset (&pair, 0, sizeof (pair));
60       ip_address_decode2 (&r->addr, &pair.lcl_loc);
61
62       pair.weight = r->weight;
63       vec_add1 (pairs, pair);
64     }
65
66   for (i = rloc_num; i < rloc_num * 2; i++)
67     {
68       /* remote locators */
69       r = &((vl_api_gpe_locator_t *) locs)[i];
70       p = &pairs[i - rloc_num];
71       ip_address_decode2 (&r->addr, &p->rmt_loc);
72     }
73   return pairs;
74 }
75
76 static void
77   gpe_fwd_entry_path_dump_t_net_to_host
78   (vl_api_gpe_fwd_entry_path_dump_t * mp)
79 {
80   mp->fwd_entry_index = clib_net_to_host_u32 (mp->fwd_entry_index);
81 }
82
83 static void
84 lisp_api_set_locator (vl_api_gpe_locator_t * loc,
85                       const ip_address_t * addr, u8 weight)
86 {
87   loc->weight = weight;
88   ip_address_encode2 (addr, &loc->addr);
89 }
90
91 static void
92   vl_api_gpe_fwd_entry_path_dump_t_handler
93   (vl_api_gpe_fwd_entry_path_dump_t * mp)
94 {
95   lisp_fwd_path_t *path;
96   vl_api_gpe_fwd_entry_path_details_t *rmp = NULL;
97   lisp_gpe_main_t *lgm = &lisp_gpe_main;
98   vl_api_registration_t *reg;
99   lisp_gpe_fwd_entry_t *lfe;
100
101   gpe_fwd_entry_path_dump_t_net_to_host (mp);
102
103   reg = vl_api_client_index_to_registration (mp->client_index);
104   if (!reg)
105     return;
106
107   if (pool_is_free_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index))
108     return;
109
110   lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, mp->fwd_entry_index);
111
112   if (LISP_GPE_FWD_ENTRY_TYPE_NEGATIVE == lfe->type)
113     return;
114
115   vec_foreach (path, lfe->paths)
116   {
117     rmp = vl_msg_api_alloc (sizeof (*rmp));
118     clib_memset (rmp, 0, sizeof (*rmp));
119     const lisp_gpe_tunnel_t *lgt;
120
121     rmp->_vl_msg_id =
122       clib_host_to_net_u16 (VL_API_GPE_FWD_ENTRY_PATH_DETAILS);
123
124     const lisp_gpe_adjacency_t *ladj =
125       lisp_gpe_adjacency_get (path->lisp_adj);
126     lisp_api_set_locator (&rmp->rmt_loc, &ladj->remote_rloc, path->weight);
127     lgt = lisp_gpe_tunnel_get (ladj->tunnel_index);
128     lisp_api_set_locator (&rmp->lcl_loc, &lgt->key->lcl, path->weight);
129
130     rmp->context = mp->context;
131     vl_api_send_msg (reg, (u8 *) rmp);
132   }
133 }
134
135 static void
136 gpe_fwd_entries_copy (vl_api_gpe_fwd_entry_t * dst,
137                       lisp_api_gpe_fwd_entry_t * src)
138 {
139   lisp_api_gpe_fwd_entry_t *e;
140   u32 i = 0;
141
142   vec_foreach (e, src)
143   {
144     clib_memset (&dst[i], 0, sizeof (*dst));
145     dst[i].dp_table = e->dp_table;
146     dst[i].fwd_entry_index = e->fwd_entry_index;
147     dst[i].vni = e->vni;
148     dst[i].action = e->action;
149     switch (fid_addr_type (&e->leid))
150       {
151       case FID_ADDR_IP_PREF:
152         dst[i].leid.type = EID_TYPE_API_PREFIX;
153         dst[i].reid.type = EID_TYPE_API_PREFIX;
154         ip_prefix_encode2 (&fid_addr_ippref (&e->leid),
155                            &dst[i].leid.address.prefix);
156         ip_prefix_encode2 (&fid_addr_ippref (&e->reid),
157                            &dst[i].reid.address.prefix);
158         break;
159       case FID_ADDR_MAC:
160         mac_address_encode ((mac_address_t *) fid_addr_mac (&e->leid),
161                             dst[i].leid.address.mac);
162         mac_address_encode ((mac_address_t *) fid_addr_mac (&e->reid),
163                             dst[i].reid.address.mac);
164         dst[i].leid.type = EID_TYPE_API_MAC;
165         dst[i].reid.type = EID_TYPE_API_MAC;
166         break;
167       default:
168         clib_warning ("unknown fid type %d!", fid_addr_type (&e->leid));
169         break;
170       }
171     i++;
172   }
173 }
174
175 static void
176 gpe_fwd_entries_get_t_net_to_host (vl_api_gpe_fwd_entries_get_t * mp)
177 {
178   mp->vni = clib_net_to_host_u32 (mp->vni);
179 }
180
181 static void
182 gpe_entry_t_host_to_net (vl_api_gpe_fwd_entry_t * e)
183 {
184   e->fwd_entry_index = clib_host_to_net_u32 (e->fwd_entry_index);
185   e->dp_table = clib_host_to_net_u32 (e->dp_table);
186   e->vni = clib_host_to_net_u32 (e->vni);
187 }
188
189 static void
190   gpe_fwd_entries_get_reply_t_host_to_net
191   (vl_api_gpe_fwd_entries_get_reply_t * mp)
192 {
193   u32 i;
194   vl_api_gpe_fwd_entry_t *e;
195
196   for (i = 0; i < mp->count; i++)
197     {
198       e = &mp->entries[i];
199       gpe_entry_t_host_to_net (e);
200     }
201   mp->count = clib_host_to_net_u32 (mp->count);
202 }
203
204 static void
205 vl_api_gpe_fwd_entry_vnis_get_t_handler (vl_api_gpe_fwd_entry_vnis_get_t * mp)
206 {
207   vl_api_gpe_fwd_entry_vnis_get_reply_t *rmp = 0;
208   hash_pair_t *p;
209   u32 i = 0;
210   int rv = 0;
211
212   u32 *vnis = vnet_lisp_gpe_get_fwd_entry_vnis ();
213   u32 size = hash_elts (vnis) * sizeof (u32);
214
215   REPLY_MACRO4 (VL_API_GPE_FWD_ENTRY_VNIS_GET_REPLY, size,
216   {
217     rmp->count = clib_host_to_net_u32 (hash_elts (vnis));
218     hash_foreach_pair (p, vnis,
219     ({
220       rmp->vnis[i++] = clib_host_to_net_u32 (p->key);
221     }));
222   });
223
224   hash_free (vnis);
225 }
226
227 static void
228 vl_api_gpe_fwd_entries_get_t_handler (vl_api_gpe_fwd_entries_get_t * mp)
229 {
230   lisp_api_gpe_fwd_entry_t *e;
231   vl_api_gpe_fwd_entries_get_reply_t *rmp = 0;
232   u32 size = 0;
233   int rv = 0;
234
235   gpe_fwd_entries_get_t_net_to_host (mp);
236
237   e = vnet_lisp_gpe_fwd_entries_get_by_vni (mp->vni);
238   size = vec_len (e) * sizeof (vl_api_gpe_fwd_entry_t);
239
240   REPLY_MACRO4 (VL_API_GPE_FWD_ENTRIES_GET_REPLY, size,
241   {
242     rmp->count = vec_len (e);
243     gpe_fwd_entries_copy (rmp->entries, e);
244     gpe_fwd_entries_get_reply_t_host_to_net (rmp);
245   });
246
247   vec_free (e);
248 }
249
250 static void
251 gpe_add_del_fwd_entry_t_net_to_host (vl_api_gpe_add_del_fwd_entry_t * mp)
252 {
253   mp->vni = clib_net_to_host_u32 (mp->vni);
254   mp->dp_table = clib_net_to_host_u32 (mp->dp_table);
255   mp->loc_num = clib_net_to_host_u32 (mp->loc_num);
256 }
257
258 static void
259 vl_api_gpe_add_del_fwd_entry_t_handler (vl_api_gpe_add_del_fwd_entry_t * mp)
260 {
261   vl_api_gpe_add_del_fwd_entry_reply_t *rmp;
262   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
263   locator_pair_t *pairs = 0;
264   int rv = 0;
265
266   gpe_add_del_fwd_entry_t_net_to_host (mp);
267   clib_memset (a, 0, sizeof (a[0]));
268
269   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, &mp->rmt_eid);
270   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, &mp->lcl_eid);
271
272   if (mp->loc_num % 2 != 0)
273     {
274       rv = -1;
275       goto send_reply;
276     }
277   pairs = unformat_gpe_loc_pairs (mp->locs, mp->loc_num / 2);
278
279   if (rv)
280     goto send_reply;
281
282   a->is_add = mp->is_add;
283   a->locator_pairs = pairs;
284   a->dp_table = mp->dp_table;
285   a->vni = mp->vni;
286   a->action = mp->action;
287   if (mp->loc_num == 0)
288     a->is_negative = 1;
289
290   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
291   vec_free (pairs);
292 send_reply:
293   REPLY_MACRO2 (VL_API_GPE_ADD_DEL_FWD_ENTRY_REPLY,
294   {
295     rmp->fwd_entry_index = clib_host_to_net_u32 (a->fwd_entry_index);
296   });
297 }
298
299 static void
300 vl_api_gpe_enable_disable_t_handler (vl_api_gpe_enable_disable_t * mp)
301 {
302   vl_api_gpe_enable_disable_reply_t *rmp;
303   int rv = 0;
304   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
305
306   a->is_en = mp->is_enable;
307   vnet_lisp_gpe_enable_disable (a);
308
309   REPLY_MACRO (VL_API_GPE_ENABLE_DISABLE_REPLY);
310 }
311
312 static void
313 vl_api_gpe_add_del_iface_t_handler (vl_api_gpe_add_del_iface_t * mp)
314 {
315   vl_api_gpe_add_del_iface_reply_t *rmp;
316   int rv = 0;
317   u32 vni, dp_table;
318
319   vni = clib_net_to_host_u32 (mp->vni);
320   dp_table = clib_net_to_host_u32 (mp->dp_table);
321
322   if (mp->is_l2)
323     {
324       if (mp->is_add)
325         {
326           if (~0 == lisp_gpe_tenant_l2_iface_add_or_lock (vni, dp_table))
327             rv = 1;
328         }
329       else
330         lisp_gpe_tenant_l2_iface_unlock (vni);
331     }
332   else
333     {
334       if (mp->is_add)
335         {
336           if (~0 == lisp_gpe_tenant_l3_iface_add_or_lock (vni, dp_table, 1))
337             rv = 1;
338         }
339       else
340         lisp_gpe_tenant_l3_iface_unlock (vni);
341     }
342
343   REPLY_MACRO (VL_API_GPE_ADD_DEL_IFACE_REPLY);
344 }
345
346 static void
347 vl_api_gpe_set_encap_mode_t_handler (vl_api_gpe_set_encap_mode_t * mp)
348 {
349   vl_api_gpe_set_encap_mode_reply_t *rmp;
350   int rv = 0;
351
352   rv = vnet_gpe_set_encap_mode (mp->is_vxlan);
353   REPLY_MACRO (VL_API_GPE_SET_ENCAP_MODE_REPLY);
354 }
355
356 static void
357 vl_api_gpe_get_encap_mode_t_handler (vl_api_gpe_get_encap_mode_t * mp)
358 {
359   vl_api_gpe_get_encap_mode_reply_t *rmp;
360   int rv = 0;
361
362   REPLY_MACRO2 (VL_API_GPE_GET_ENCAP_MODE_REPLY,
363   ({
364     rmp->encap_mode = vnet_gpe_get_encap_mode ();
365   }));
366 }
367
368 static void
369   vl_api_gpe_add_del_native_fwd_rpath_t_handler
370   (vl_api_gpe_add_del_native_fwd_rpath_t * mp)
371 {
372   vl_api_gpe_add_del_native_fwd_rpath_reply_t *rmp;
373   vnet_gpe_native_fwd_rpath_args_t _a, *a = &_a;
374   int rv = 0;
375
376   clib_memset (a, 0, sizeof (a[0]));
377
378   if (mp->nh_addr.af)
379     clib_memcpy (&a->rpath.frp_addr.ip6, mp->nh_addr.un.ip6,
380                  sizeof (ip6_address_t));
381   else
382     clib_memcpy (&a->rpath.frp_addr.ip4, mp->nh_addr.un.ip4,
383                  sizeof (ip4_address_t));
384
385   a->is_add = mp->is_add;
386   a->rpath.frp_proto = mp->nh_addr.af ? DPO_PROTO_IP6 : DPO_PROTO_IP4;
387   a->rpath.frp_fib_index =
388     fib_table_find (dpo_proto_to_fib (a->rpath.frp_proto),
389                     clib_net_to_host_u32 (mp->table_id));
390   if (~0 == a->rpath.frp_fib_index)
391     {
392       rv = VNET_API_ERROR_INVALID_VALUE;
393       goto done;
394     }
395
396   a->rpath.frp_sw_if_index = clib_net_to_host_u32 (mp->nh_sw_if_index);
397   a->rpath.frp_weight = 1;
398
399   rv = vnet_gpe_add_del_native_fwd_rpath (a);
400 done:
401   REPLY_MACRO (VL_API_GPE_ADD_DEL_NATIVE_FWD_RPATH_REPLY);
402 }
403
404 static void
405 gpe_native_fwd_rpaths_copy (vl_api_gpe_native_fwd_rpath_t * dst,
406                             fib_route_path_t * src)
407 {
408   fib_route_path_t *e;
409   fib_table_t *table;
410   u32 i = 0;
411
412   vec_foreach (e, src)
413   {
414     clib_memset (&dst[i], 0, sizeof (*dst));
415     table = fib_table_get (e->frp_fib_index, dpo_proto_to_fib (e->frp_proto));
416     dst[i].fib_index = table->ft_table_id;
417     dst[i].nh_sw_if_index = e->frp_sw_if_index;
418     ip_address_encode (&e->frp_addr, IP46_TYPE_ANY, &dst[i].nh_addr);
419     i++;
420   }
421 }
422
423 static void
424 gpe_native_fwd_rpath_t_host_to_net (vl_api_gpe_native_fwd_rpath_t * e)
425 {
426   e->fib_index = clib_host_to_net_u32 (e->fib_index);
427   e->nh_sw_if_index = clib_host_to_net_u32 (e->nh_sw_if_index);
428 }
429
430 static void
431   gpe_native_fwd_rpaths_get_reply_t_host_to_net
432   (vl_api_gpe_native_fwd_rpaths_get_reply_t * mp)
433 {
434   u32 i;
435   vl_api_gpe_native_fwd_rpath_t *e;
436
437   for (i = 0; i < mp->count; i++)
438     {
439       e = &mp->entries[i];
440       gpe_native_fwd_rpath_t_host_to_net (e);
441     }
442   mp->count = clib_host_to_net_u32 (mp->count);
443 }
444
445 static void
446 vl_api_gpe_native_fwd_rpaths_get_t_handler (vl_api_gpe_native_fwd_rpaths_get_t
447                                             * mp)
448 {
449   lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
450   vl_api_gpe_native_fwd_rpaths_get_reply_t *rmp;
451   u32 size = 0;
452   int rv = 0;
453
454   u8 rpath_index = mp->is_ip4 ? 1 : 0;
455
456   size = vec_len (lgm->native_fwd_rpath[rpath_index])
457     * sizeof (vl_api_gpe_native_fwd_rpath_t);
458
459   REPLY_MACRO4 (VL_API_GPE_NATIVE_FWD_RPATHS_GET_REPLY, size,
460   {
461     rmp->count = vec_len (lgm->native_fwd_rpath[rpath_index]);
462     gpe_native_fwd_rpaths_copy (rmp->entries,
463                                 lgm->native_fwd_rpath[rpath_index]);
464     gpe_native_fwd_rpaths_get_reply_t_host_to_net (rmp);
465   });
466 }
467
468 /*
469  * lisp_api_hookup
470  * Add vpe's API message handlers to the table.
471  * vlib has already mapped shared memory and
472  * added the client registration handlers.
473  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
474  */
475 #include <lisp/lisp-gpe/lisp_gpe.api.c>
476
477 static clib_error_t *
478 gpe_api_hookup (vlib_main_t * vm)
479 {
480   /*
481    * Set up the (msg_name, crc, message-id) table
482    */
483   gpe_base_msg_id = setup_message_id_table ();
484
485   return NULL;
486 }
487
488 VLIB_API_INIT_FUNCTION (gpe_api_hookup);
489
490 /*
491  * fd.io coding-style-patch-verification: ON
492  *
493  * Local Variables:
494  * eval: (c-set-style "gnu")
495  * End:
496  */