misc: Move l2tp to plugin
[vpp.git] / src / vnet / geneve / geneve_api.c
1 /*
2  * Copyright (c) 2017 SUSE LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *------------------------------------------------------------------
15  */
16
17 #include <vnet/vnet.h>
18 #include <vlibmemory/api.h>
19
20 #include <vnet/interface.h>
21 #include <vnet/api_errno.h>
22 #include <vnet/feature/feature.h>
23 #include <vnet/geneve/geneve.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/ip/ip_types_api.h>
26
27 #include <vnet/geneve/geneve.api_enum.h>
28 #include <vnet/geneve/geneve.api_types.h>
29
30 #define REPLY_MSG_ID_BASE gm->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
32
33 static void
34   vl_api_sw_interface_set_geneve_bypass_t_handler
35   (vl_api_sw_interface_set_geneve_bypass_t * mp)
36 {
37   geneve_main_t *gm = &geneve_main;
38   vl_api_sw_interface_set_geneve_bypass_reply_t *rmp;
39   int rv = 0;
40   u32 sw_if_index = ntohl (mp->sw_if_index);
41
42   VALIDATE_SW_IF_INDEX (mp);
43
44   vnet_int_geneve_bypass_mode (sw_if_index, mp->is_ipv6, mp->enable);
45   BAD_SW_IF_INDEX_LABEL;
46
47   REPLY_MACRO (VL_API_SW_INTERFACE_SET_GENEVE_BYPASS_REPLY);
48 }
49
50 static void vl_api_geneve_add_del_tunnel_t_handler
51   (vl_api_geneve_add_del_tunnel_t * mp)
52 {
53   geneve_main_t *gm = &geneve_main;
54   vl_api_geneve_add_del_tunnel_reply_t *rmp;
55   int rv = 0;
56   ip4_main_t *im = &ip4_main;
57
58   uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
59   if (!p)
60     {
61       rv = VNET_API_ERROR_NO_SUCH_FIB;
62       goto out;
63     }
64
65   vnet_geneve_add_del_tunnel_args_t a = {
66     .is_add = mp->is_add,
67     .is_ip6 = mp->remote_address.af,
68     .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
69     .encap_fib_index = p[0],
70     .decap_next_index = ntohl (mp->decap_next_index),
71     .vni = ntohl (mp->vni),
72   };
73
74   ip_address_decode (&mp->remote_address, &a.remote);
75   ip_address_decode (&mp->local_address, &a.local);
76
77   /* Check src & dst are different */
78   if (ip46_address_cmp (&a.remote, &a.local) == 0)
79     {
80       rv = VNET_API_ERROR_SAME_SRC_DST;
81       goto out;
82     }
83   if (ip46_address_is_multicast (&a.remote) &&
84       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
85     {
86       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
87       goto out;
88     }
89
90   u32 sw_if_index = ~0;
91   rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);
92
93 out:
94   /* *INDENT-OFF* */
95   REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL_REPLY,
96   ({
97     rmp->sw_if_index = ntohl (sw_if_index);
98   }));
99   /* *INDENT-ON* */
100 }
101
102 static void vl_api_geneve_add_del_tunnel2_t_handler
103   (vl_api_geneve_add_del_tunnel2_t * mp)
104 {
105   geneve_main_t *gm = &geneve_main;
106   vl_api_geneve_add_del_tunnel2_reply_t *rmp;
107   int rv = 0;
108   ip4_main_t *im = &ip4_main;
109
110   uword *p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
111   if (!p)
112     {
113       rv = VNET_API_ERROR_NO_SUCH_FIB;
114       goto out;
115     }
116
117   vnet_geneve_add_del_tunnel_args_t a = {
118     .is_add = mp->is_add,
119     .is_ip6 = mp->remote_address.af,
120     .mcast_sw_if_index = ntohl (mp->mcast_sw_if_index),
121     .encap_fib_index = p[0],
122     .decap_next_index = ntohl (mp->decap_next_index),
123     .vni = ntohl (mp->vni),
124     .l3_mode = mp->l3_mode,
125   };
126
127   ip_address_decode (&mp->remote_address, &a.remote);
128   ip_address_decode (&mp->local_address, &a.local);
129
130   /* Check src & dst are different */
131   if (ip46_address_cmp (&a.remote, &a.local) == 0)
132     {
133       rv = VNET_API_ERROR_SAME_SRC_DST;
134       goto out;
135     }
136   if (ip46_address_is_multicast (&a.remote) &&
137       !vnet_sw_if_index_is_api_valid (a.mcast_sw_if_index))
138     {
139       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
140       goto out;
141     }
142
143   u32 sw_if_index = ~0;
144   rv = vnet_geneve_add_del_tunnel (&a, &sw_if_index);
145
146 out:
147   /* *INDENT-OFF* */
148   REPLY_MACRO2(VL_API_GENEVE_ADD_DEL_TUNNEL2_REPLY,
149   ({
150     rmp->sw_if_index = ntohl (sw_if_index);
151   }));
152   /* *INDENT-ON* */
153 }
154
155 static void send_geneve_tunnel_details
156   (geneve_tunnel_t * t, vl_api_registration_t * reg, u32 context)
157 {
158   geneve_main_t *gm = &geneve_main;
159   vl_api_geneve_tunnel_details_t *rmp;
160   ip4_main_t *im4 = &ip4_main;
161   ip6_main_t *im6 = &ip6_main;
162   u8 is_ipv6 = !ip46_address_is_ip4 (&t->remote);
163
164   rmp = vl_msg_api_alloc (sizeof (*rmp));
165   clib_memset (rmp, 0, sizeof (*rmp));
166   rmp->_vl_msg_id = ntohs (VL_API_GENEVE_TUNNEL_DETAILS + gm->msg_id_base);
167   ip_address_encode (&t->local, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
168                      &rmp->src_address);
169   ip_address_encode (&t->remote, is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
170                      &rmp->dst_address);
171   rmp->encap_vrf_id =
172     htonl (is_ipv6 ? im6->fibs[t->encap_fib_index].
173            ft_table_id : im4->fibs[t->encap_fib_index].ft_table_id);
174
175   rmp->mcast_sw_if_index = htonl (t->mcast_sw_if_index);
176   rmp->vni = htonl (t->vni);
177   rmp->decap_next_index = htonl (t->decap_next_index);
178   rmp->sw_if_index = htonl (t->sw_if_index);
179   rmp->context = context;
180
181   vl_api_send_msg (reg, (u8 *) rmp);
182 }
183
184 static void vl_api_geneve_tunnel_dump_t_handler
185   (vl_api_geneve_tunnel_dump_t * mp)
186 {
187   vl_api_registration_t *reg;
188   geneve_main_t *vxm = &geneve_main;
189   geneve_tunnel_t *t;
190   u32 sw_if_index;
191
192   reg = vl_api_client_index_to_registration (mp->client_index);
193   if (!reg)
194     return;
195
196   sw_if_index = ntohl (mp->sw_if_index);
197
198   if (~0 == sw_if_index)
199     {
200       /* *INDENT-OFF* */
201       pool_foreach (t, vxm->tunnels,
202       ({
203         send_geneve_tunnel_details(t, reg, mp->context);
204       }));
205       /* *INDENT-ON* */
206     }
207   else
208     {
209       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
210           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
211         {
212           return;
213         }
214       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
215       send_geneve_tunnel_details (t, reg, mp->context);
216     }
217 }
218
219 /*
220  * geneve_api_hookup
221  * Add geneve's API message handlers to the table.
222  */
223 /* API definitions */
224 #include <vnet/format_fns.h>
225 #include <vnet/geneve/geneve.api.c>
226
227 static clib_error_t *
228 geneve_api_hookup (vlib_main_t * vm)
229 {
230   geneve_main_t *gm = &geneve_main;
231
232   /*
233    * Set up the (msg_name, crc, message-id) table
234    */
235   gm->msg_id_base = setup_message_id_table ();
236
237   return 0;
238 }
239
240 VLIB_API_INIT_FUNCTION (geneve_api_hookup);
241
242 /*
243  * fd.io coding-style-patch-verification: ON
244  *
245  * Local Variables:
246  * eval: (c-set-style "gnu")
247  * End:
248  */