dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / vnet / 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_typedefs             /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_typedefs
34
35 #define vl_endianfun            /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_printfun
44
45 #include <vlibapi/api_helper_macros.h>
46
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)
52
53 /** Used for transferring locators via VPP API */
54 /* *INDENT-OFF* */
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 */
60 }) rloc_t;
61 /* *INDENT-ON* */
62
63 static locator_pair_t *
64 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
65 {
66   u32 i;
67   locator_pair_t *pairs = 0, pair;
68   rloc_t *r;
69
70   for (i = 0; i < rloc_num; i++)
71     {
72       /* local locator */
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);
76
77       /* remote locators */
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);
81
82       pair.priority = r->priority;
83       pair.weight = r->weight;
84
85       vec_add1 (pairs, pair);
86     }
87   return pairs;
88 }
89
90 static int
91 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
92                        u8 len)
93 {
94   switch (type)
95     {
96     case 0:                     /* ipv4 */
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));
101       break;
102     case 1:                     /* ipv6 */
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));
107       break;
108     case 2:                     /* l2 mac */
109       gid_address_type (dst) = GID_ADDR_MAC;
110       clib_memcpy (&gid_address_mac (dst), src, 6);
111       break;
112     default:
113       /* unknown type */
114       return VNET_API_ERROR_INVALID_VALUE;
115     }
116
117   gid_address_vni (dst) = vni;
118
119   return 0;
120 }
121
122 static void
123   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
124   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
125 {
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;
129   int rv = 0;
130
131   memset (a, 0, sizeof (a[0]));
132
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);
137
138   pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
139
140   if (rv || 0 == pairs)
141     goto send_reply;
142
143   a->is_add = mp->is_add;
144   a->locator_pairs = pairs;
145   a->dp_table = mp->dp_table;
146   a->vni = mp->vni;
147   a->action = mp->action;
148
149   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
150   vec_free (pairs);
151 send_reply:
152   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
153 }
154
155 static void
156 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
157                                           mp)
158 {
159   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
160   int rv = 0;
161   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
162
163   a->is_en = mp->is_en;
164   vnet_lisp_gpe_enable_disable (a);
165
166   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
167 }
168
169 static void
170 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
171 {
172   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
173   int rv = 0;
174
175   if (mp->is_l2)
176     {
177       if (mp->is_add)
178         {
179           if (~0 ==
180               lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
181             rv = 1;
182         }
183       else
184         lisp_gpe_tenant_l2_iface_unlock (mp->vni);
185     }
186   else
187     {
188       if (mp->is_add)
189         {
190           if (~0 ==
191               lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
192             rv = 1;
193         }
194       else
195         lisp_gpe_tenant_l3_iface_unlock (mp->vni);
196     }
197
198   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
199 }
200
201 static void
202 send_lisp_gpe_fwd_entry_details (lisp_gpe_fwd_entry_t * lfe,
203                                  unix_shared_memory_queue_t * q, u32 context)
204 {
205   vl_api_lisp_gpe_tunnel_details_t *rmp;
206   lisp_gpe_main_t *lgm = &lisp_gpe_main;
207
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);
211
212   rmp->tunnels = lfe - lgm->lisp_fwd_entry_pool;
213
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)));
219
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;
224
225   vl_msg_api_send_shmem (q, (u8 *) & rmp);
226 }
227
228 static void
229 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
230 {
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;
234
235   if (pool_elts (lgm->lisp_fwd_entry_pool) == 0)
236     {
237       return;
238     }
239
240   q = vl_api_client_index_to_input_queue (mp->client_index);
241   if (q == 0)
242     {
243       return;
244     }
245
246   /* *INDENT-OFF* */
247   pool_foreach(lfe, lgm->lisp_fwd_entry_pool,
248   ({
249     send_lisp_gpe_fwd_entry_details(lfe, q, mp->context);
250   }));
251   /* *INDENT-ON* */
252 }
253
254 /*
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()
260  */
261 #define vl_msg_name_crc_list
262 #include <vnet/vnet_all_api_h.h>
263 #undef vl_msg_name_crc_list
264
265 static void
266 setup_message_id_table (api_main_t * am)
267 {
268 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
269   foreach_vl_msg_name_crc_lisp_gpe;
270 #undef _
271 }
272
273 static clib_error_t *
274 lisp_gpe_api_hookup (vlib_main_t * vm)
275 {
276   api_main_t *am = &api_main;
277
278 #define _(N,n)                                                  \
279     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
280                            vl_api_##n##_t_handler,              \
281                            vl_noop_handler,                     \
282                            vl_api_##n##_t_endian,               \
283                            vl_api_##n##_t_print,                \
284                            sizeof(vl_api_##n##_t), 1);
285   foreach_vpe_api_msg;
286 #undef _
287
288   /*
289    * Set up the (msg_name, crc, message-id) table
290    */
291   setup_message_id_table (am);
292
293   return 0;
294 }
295
296 VLIB_API_INIT_FUNCTION (lisp_gpe_api_hookup);
297
298 /*
299  * fd.io coding-style-patch-verification: ON
300  *
301  * Local Variables:
302  * eval: (c-set-style "gnu")
303  * End:
304  */