tunnel: Common types for IP tunnels
[vpp.git] / src / vnet / gre / gre_api.c
1 /*
2  *------------------------------------------------------------------
3  * gre_api.c - gre 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
26 #include <vnet/gre/gre.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/tunnel/tunnel_types_api.h>
29 #include <vnet/ip/ip_types_api.h>
30
31 #include <vnet/vnet_msg_enum.h>
32
33 #define vl_typedefs             /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_typedefs
36
37 #define vl_endianfun            /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define vl_printfun
44 #include <vnet/vnet_all_api_h.h>
45 #undef vl_printfun
46
47 #include <vlibapi/api_helper_macros.h>
48
49 #define foreach_vpe_api_msg                             \
50 _(GRE_TUNNEL_ADD_DEL, gre_tunnel_add_del)               \
51 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)
52
53 static int
54 gre_tunnel_type_decode (vl_api_gre_tunnel_type_t in, gre_tunnel_type_t * out)
55 {
56   switch (in)
57     {
58 #define _(n, v)                                           \
59       case GRE_API_TUNNEL_TYPE_##n:                       \
60         *out = GRE_TUNNEL_TYPE_##n;                       \
61         return (0);
62       foreach_gre_tunnel_type
63 #undef _
64     }
65
66   return (VNET_API_ERROR_INVALID_VALUE);
67 }
68
69 static vl_api_gre_tunnel_type_t
70 gre_tunnel_type_encode (gre_tunnel_type_t in)
71 {
72   vl_api_gre_tunnel_type_t out = GRE_API_TUNNEL_TYPE_L3;
73
74   switch (in)
75     {
76 #define _(n, v)                                           \
77       case GRE_TUNNEL_TYPE_##n:                           \
78         out = GRE_API_TUNNEL_TYPE_##n;                    \
79         break;
80       foreach_gre_tunnel_type
81 #undef _
82     }
83
84   return (out);
85 }
86
87 static void vl_api_gre_tunnel_add_del_t_handler
88   (vl_api_gre_tunnel_add_del_t * mp)
89 {
90   vnet_gre_tunnel_add_del_args_t _a = { }, *a = &_a;
91   vl_api_gre_tunnel_add_del_reply_t *rmp;
92   u32 sw_if_index = ~0;
93   ip46_type_t itype[2];
94   int rv = 0;
95
96   itype[0] = ip_address_decode (&mp->tunnel.src, &a->src);
97   itype[1] = ip_address_decode (&mp->tunnel.dst, &a->dst);
98
99   if (itype[0] != itype[1])
100     {
101       rv = VNET_API_ERROR_INVALID_PROTOCOL;
102       goto out;
103     }
104
105   if (ip46_address_is_equal (&a->src, &a->dst))
106     {
107       rv = VNET_API_ERROR_SAME_SRC_DST;
108       goto out;
109     }
110
111   rv = gre_tunnel_type_decode (mp->tunnel.type, &a->type);
112
113   if (rv)
114     goto out;
115
116   rv = tunnel_mode_decode (mp->tunnel.mode, &a->mode);
117
118   if (rv)
119     goto out;
120
121   a->is_add = mp->is_add;
122   a->is_ipv6 = (itype[0] == IP46_TYPE_IP6);
123   a->instance = ntohl (mp->tunnel.instance);
124   a->session_id = ntohs (mp->tunnel.session_id);
125   a->outer_table_id = ntohl (mp->tunnel.outer_table_id);
126
127   rv = vnet_gre_tunnel_add_del (a, &sw_if_index);
128
129 out:
130   /* *INDENT-OFF* */
131   REPLY_MACRO2(VL_API_GRE_TUNNEL_ADD_DEL_REPLY,
132   ({
133     rmp->sw_if_index = ntohl (sw_if_index);
134   }));
135   /* *INDENT-ON* */
136 }
137
138 static void send_gre_tunnel_details
139   (gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
140 {
141   vl_api_gre_tunnel_details_t *rmp;
142
143   rmp = vl_msg_api_alloc (sizeof (*rmp));
144   clib_memset (rmp, 0, sizeof (*rmp));
145   rmp->_vl_msg_id = htons (VL_API_GRE_TUNNEL_DETAILS);
146
147   ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
148   ip_address_encode (&t->tunnel_dst.fp_addr, IP46_TYPE_ANY, &rmp->tunnel.dst);
149
150   rmp->tunnel.outer_table_id =
151     htonl (fib_table_get_table_id
152            (t->outer_fib_index, t->tunnel_dst.fp_proto));
153
154   rmp->tunnel.type = gre_tunnel_type_encode (t->type);
155   rmp->tunnel.mode = tunnel_mode_encode (t->mode);
156   rmp->tunnel.instance = htonl (t->user_instance);
157   rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
158   rmp->tunnel.session_id = htons (t->session_id);
159
160   rmp->context = context;
161
162   vl_api_send_msg (reg, (u8 *) rmp);
163 }
164
165 static void
166 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
167 {
168   vl_api_registration_t *reg;
169   gre_main_t *gm = &gre_main;
170   gre_tunnel_t *t;
171   u32 sw_if_index;
172
173   reg = vl_api_client_index_to_registration (mp->client_index);
174   if (!reg)
175     return;
176
177   sw_if_index = ntohl (mp->sw_if_index);
178
179   if (~0 == sw_if_index)
180     {
181       /* *INDENT-OFF* */
182       pool_foreach (t, gm->tunnels,
183       ({
184         send_gre_tunnel_details(t, reg, mp->context);
185       }));
186       /* *INDENT-ON* */
187     }
188   else
189     {
190       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
191           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
192         {
193           return;
194         }
195       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
196       send_gre_tunnel_details (t, reg, mp->context);
197     }
198 }
199
200 /*
201  * gre_api_hookup
202  * Add vpe's API message handlers to the table.
203  * vlib has already mapped shared memory and
204  * added the client registration handlers.
205  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
206  */
207 #define vl_msg_name_crc_list
208 #include <vnet/vnet_all_api_h.h>
209 #undef vl_msg_name_crc_list
210
211 static void
212 setup_message_id_table (api_main_t * am)
213 {
214 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
215   foreach_vl_msg_name_crc_gre;
216 #undef _
217 }
218
219 static clib_error_t *
220 gre_api_hookup (vlib_main_t * vm)
221 {
222   api_main_t *am = vlibapi_get_main ();
223
224 #define _(N,n)                                                  \
225     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
226                            vl_api_##n##_t_handler,              \
227                            vl_noop_handler,                     \
228                            vl_api_##n##_t_endian,               \
229                            vl_api_##n##_t_print,                \
230                            sizeof(vl_api_##n##_t), 1);
231   foreach_vpe_api_msg;
232 #undef _
233
234   /*
235    * Set up the (msg_name, crc, message-id) table
236    */
237   setup_message_id_table (am);
238
239   return 0;
240 }
241
242 VLIB_API_INIT_FUNCTION (gre_api_hookup);
243
244 /*
245  * fd.io coding-style-patch-verification: ON
246  *
247  * Local Variables:
248  * eval: (c-set-style "gnu")
249  * End:
250  */