Reorganize source tree to use single autotools instance
[vpp.git] / src / vnet / vxlan / vxlan_api.c
1 /*
2  *------------------------------------------------------------------
3  * vxlan_api.c - vxlan 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/feature/feature.h>
26 #include <vnet/vxlan/vxlan.h>
27 #include <vnet/fib/fib_table.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 _(SW_INTERFACE_SET_VXLAN_BYPASS, sw_interface_set_vxlan_bypass)         \
49 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
50 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)
51
52 static void
53   vl_api_sw_interface_set_vxlan_bypass_t_handler
54   (vl_api_sw_interface_set_vxlan_bypass_t * mp)
55 {
56   vl_api_sw_interface_set_vxlan_bypass_reply_t *rmp;
57   int rv = 0;
58   u32 sw_if_index = ntohl (mp->sw_if_index);
59
60   VALIDATE_SW_IF_INDEX (mp);
61
62   if (mp->is_ipv6)
63     {
64       /* not yet implemented */
65     }
66   else
67     vnet_feature_enable_disable ("ip4-unicast", "ip4-vxlan-bypass",
68                                  sw_if_index, mp->enable, 0, 0);
69
70   BAD_SW_IF_INDEX_LABEL;
71
72   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VXLAN_BYPASS_REPLY);
73 }
74
75 static void vl_api_vxlan_add_del_tunnel_t_handler
76   (vl_api_vxlan_add_del_tunnel_t * mp)
77 {
78   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
79   int rv = 0;
80   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
81   u32 encap_fib_index;
82   uword *p;
83   ip4_main_t *im = &ip4_main;
84   vnet_main_t *vnm = vnet_get_main ();
85   u32 sw_if_index = ~0;
86
87   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
88   if (!p)
89     {
90       rv = VNET_API_ERROR_NO_SUCH_FIB;
91       goto out;
92     }
93   encap_fib_index = p[0];
94   memset (a, 0, sizeof (*a));
95
96   a->is_add = mp->is_add;
97   a->is_ip6 = mp->is_ipv6;
98
99   /* ip addresses sent in network byte order */
100   ip46_from_addr_buf (mp->is_ipv6, mp->dst_address, &a->dst);
101   ip46_from_addr_buf (mp->is_ipv6, mp->src_address, &a->src);
102
103   /* Check src & dst are different */
104   if (ip46_address_cmp (&a->dst, &a->src) == 0)
105     {
106       rv = VNET_API_ERROR_SAME_SRC_DST;
107       goto out;
108     }
109   a->mcast_sw_if_index = ntohl (mp->mcast_sw_if_index);
110   if (ip46_address_is_multicast (&a->dst) &&
111       pool_is_free_index (vnm->interface_main.sw_interfaces,
112                           a->mcast_sw_if_index))
113     {
114       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
115       goto out;
116     }
117   a->encap_fib_index = encap_fib_index;
118   a->decap_next_index = ntohl (mp->decap_next_index);
119   a->vni = ntohl (mp->vni);
120   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
121
122 out:
123   /* *INDENT-OFF* */
124   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
125   ({
126     rmp->sw_if_index = ntohl (sw_if_index);
127   }));
128   /* *INDENT-ON* */
129 }
130
131 static void send_vxlan_tunnel_details
132   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
133 {
134   vl_api_vxlan_tunnel_details_t *rmp;
135   ip4_main_t *im4 = &ip4_main;
136   ip6_main_t *im6 = &ip6_main;
137   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
138
139   rmp = vl_msg_api_alloc (sizeof (*rmp));
140   memset (rmp, 0, sizeof (*rmp));
141   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
142   if (is_ipv6)
143     {
144       memcpy (rmp->src_address, t->src.ip6.as_u8, 16);
145       memcpy (rmp->dst_address, t->dst.ip6.as_u8, 16);
146       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
147     }
148   else
149     {
150       memcpy (rmp->src_address, t->src.ip4.as_u8, 4);
151       memcpy (rmp->dst_address, t->dst.ip4.as_u8, 4);
152       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
153     }
154   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
155   rmp->vni = htonl (t->vni);
156   rmp->decap_next_index = htonl (t->decap_next_index);
157   rmp->sw_if_index = htonl (t->sw_if_index);
158   rmp->is_ipv6 = is_ipv6;
159   rmp->context = context;
160
161   vl_msg_api_send_shmem (q, (u8 *) & rmp);
162 }
163
164 static void vl_api_vxlan_tunnel_dump_t_handler
165   (vl_api_vxlan_tunnel_dump_t * mp)
166 {
167   unix_shared_memory_queue_t *q;
168   vxlan_main_t *vxm = &vxlan_main;
169   vxlan_tunnel_t *t;
170   u32 sw_if_index;
171
172   q = vl_api_client_index_to_input_queue (mp->client_index);
173   if (q == 0)
174     {
175       return;
176     }
177
178   sw_if_index = ntohl (mp->sw_if_index);
179
180   if (~0 == sw_if_index)
181     {
182       /* *INDENT-OFF* */
183       pool_foreach (t, vxm->tunnels,
184       ({
185         send_vxlan_tunnel_details(t, q, mp->context);
186       }));
187       /* *INDENT-ON* */
188     }
189   else
190     {
191       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
192           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
193         {
194           return;
195         }
196       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
197       send_vxlan_tunnel_details (t, q, mp->context);
198     }
199 }
200
201 /*
202  * vpe_api_hookup
203  * Add vpe's API message handlers to the table.
204  * vlib has alread mapped shared memory and
205  * added the client registration handlers.
206  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
207  */
208 #define vl_msg_name_crc_list
209 #include <vnet/vnet_all_api_h.h>
210 #undef vl_msg_name_crc_list
211
212 static void
213 setup_message_id_table (api_main_t * am)
214 {
215 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
216   foreach_vl_msg_name_crc_vxlan;
217 #undef _
218 }
219
220 static clib_error_t *
221 vxlan_api_hookup (vlib_main_t * vm)
222 {
223   api_main_t *am = &api_main;
224
225 #define _(N,n)                                                  \
226     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
227                            vl_api_##n##_t_handler,              \
228                            vl_noop_handler,                     \
229                            vl_api_##n##_t_endian,               \
230                            vl_api_##n##_t_print,                \
231                            sizeof(vl_api_##n##_t), 1);
232   foreach_vpe_api_msg;
233 #undef _
234
235   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
236
237   /*
238    * Set up the (msg_name, crc, message-id) table
239    */
240   setup_message_id_table (am);
241
242   return 0;
243 }
244
245 VLIB_API_INIT_FUNCTION (vxlan_api_hookup);
246
247 /*
248  * fd.io coding-style-patch-verification: ON
249  *
250  * Local Variables:
251  * eval: (c-set-style "gnu")
252  * End:
253  */