LISP: Fix gpe API
[vpp.git] / src / vnet / 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 <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>
28
29 #include <vnet/vnet_msg_enum.h>
30
31 #define vl_api_lisp_gpe_locator_pair_t_endian vl_noop_handler
32 #define vl_api_lisp_gpe_locator_pair_t_print vl_noop_handler
33 #define vl_api_lisp_gpe_add_del_fwd_entry_t_endian vl_noop_handler
34 #define vl_api_lisp_gpe_add_del_fwd_entry_t_print vl_noop_handler
35
36 #define vl_typedefs             /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_typedefs
39
40 #define vl_endianfun            /* define message structures */
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #define vl_printfun
47 #include <vnet/vnet_all_api_h.h>
48 #undef vl_printfun
49
50 #include <vlibapi/api_helper_macros.h>
51
52 #define foreach_vpe_api_msg                             \
53 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
54 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
55 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)
56
57 static locator_pair_t *
58 unformat_lisp_loc_pairs (void *locs, u32 rloc_num)
59 {
60   u32 i;
61   locator_pair_t *pairs = 0, pair, *p;
62   vl_api_lisp_gpe_locator_t *r;
63
64   for (i = 0; i < rloc_num; i++)
65     {
66       /* local locator */
67       r = &((vl_api_lisp_gpe_locator_t *) locs)[i];
68       memset (&pair, 0, sizeof (pair));
69       ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
70
71       pair.weight = r->weight;
72       vec_add1 (pairs, pair);
73     }
74
75   for (i = rloc_num; i < rloc_num * 2; i++)
76     {
77       /* remote locators */
78       r = &((vl_api_lisp_gpe_locator_t *) locs)[i];
79       p = &pairs[i - rloc_num];
80       ip_address_set (&p->rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
81     }
82   return pairs;
83 }
84
85 static int
86 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
87                        u8 len)
88 {
89   switch (type)
90     {
91     case 0:                     /* ipv4 */
92       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
93       gid_address_ip_set (dst, src, IP4);
94       gid_address_ippref_len (dst) = len;
95       ip_prefix_normalize (&gid_address_ippref (dst));
96       break;
97     case 1:                     /* ipv6 */
98       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
99       gid_address_ip_set (dst, src, IP6);
100       gid_address_ippref_len (dst) = len;
101       ip_prefix_normalize (&gid_address_ippref (dst));
102       break;
103     case 2:                     /* l2 mac */
104       gid_address_type (dst) = GID_ADDR_MAC;
105       clib_memcpy (&gid_address_mac (dst), src, 6);
106       break;
107     default:
108       /* unknown type */
109       return VNET_API_ERROR_INVALID_VALUE;
110     }
111
112   gid_address_vni (dst) = vni;
113
114   return 0;
115 }
116
117 static void
118   lisp_gpe_add_del_fwd_entry_t_net_to_host
119   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
120 {
121   mp->vni = clib_net_to_host_u32 (mp->vni);
122   mp->dp_table = clib_net_to_host_u32 (mp->dp_table);
123   mp->loc_num = clib_net_to_host_u32 (mp->loc_num);
124 }
125
126 static void
127   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
128   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
129 {
130   vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
131   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
132   locator_pair_t *pairs = 0;
133   int rv = 0;
134
135   lisp_gpe_add_del_fwd_entry_t_net_to_host (mp);
136   memset (a, 0, sizeof (a[0]));
137
138   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
139                               mp->rmt_eid, mp->rmt_len);
140   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
141                                mp->lcl_eid, mp->lcl_len);
142
143   if (mp->loc_num % 2 != 0)
144     {
145       rv = -1;
146       goto send_reply;
147     }
148   pairs = unformat_lisp_loc_pairs (mp->locs, mp->loc_num / 2);
149
150   if (rv || 0 == pairs)
151     goto send_reply;
152
153   a->is_add = mp->is_add;
154   a->locator_pairs = pairs;
155   a->dp_table = mp->dp_table;
156   a->vni = mp->vni;
157   a->action = mp->action;
158
159   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
160   vec_free (pairs);
161 send_reply:
162   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
163 }
164
165 static void
166 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
167                                           mp)
168 {
169   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
170   int rv = 0;
171   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
172
173   a->is_en = mp->is_en;
174   vnet_lisp_gpe_enable_disable (a);
175
176   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
177 }
178
179 static void
180 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
181 {
182   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
183   int rv = 0;
184
185   if (mp->is_l2)
186     {
187       if (mp->is_add)
188         {
189           if (~0 ==
190               lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
191             rv = 1;
192         }
193       else
194         lisp_gpe_tenant_l2_iface_unlock (mp->vni);
195     }
196   else
197     {
198       if (mp->is_add)
199         {
200           if (~0 ==
201               lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
202             rv = 1;
203         }
204       else
205         lisp_gpe_tenant_l3_iface_unlock (mp->vni);
206     }
207
208   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
209 }
210
211 /*
212  * lisp_gpe_api_hookup
213  * Add vpe's API message handlers to the table.
214  * vlib has alread mapped shared memory and
215  * added the client registration handlers.
216  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
217  */
218 #define vl_msg_name_crc_list
219 #include <vnet/vnet_all_api_h.h>
220 #undef vl_msg_name_crc_list
221
222 static void
223 setup_message_id_table (api_main_t * am)
224 {
225 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
226   foreach_vl_msg_name_crc_lisp_gpe;
227 #undef _
228 }
229
230 static clib_error_t *
231 lisp_gpe_api_hookup (vlib_main_t * vm)
232 {
233   api_main_t *am = &api_main;
234
235 #define _(N,n)                                                  \
236     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
237                            vl_api_##n##_t_handler,              \
238                            vl_noop_handler,                     \
239                            vl_api_##n##_t_endian,               \
240                            vl_api_##n##_t_print,                \
241                            sizeof(vl_api_##n##_t), 1);
242   foreach_vpe_api_msg;
243 #undef _
244
245   /*
246    * Set up the (msg_name, crc, message-id) table
247    */
248   setup_message_id_table (am);
249
250   return 0;
251 }
252
253 VLIB_API_INIT_FUNCTION (lisp_gpe_api_hookup);
254
255 /*
256  * fd.io coding-style-patch-verification: ON
257  *
258  * Local Variables:
259  * eval: (c-set-style "gnu")
260  * End:
261  */