bcf01e2153f1a564526b4cf0b195e563783d4e76
[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
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 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)               \
49 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)
50
51 static void vl_api_gre_add_del_tunnel_t_handler
52   (vl_api_gre_add_del_tunnel_t * mp)
53 {
54   vl_api_gre_add_del_tunnel_reply_t *rmp;
55   int rv = 0;
56   vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
57   u32 sw_if_index = ~0;
58
59   /* Check src & dst are different */
60   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
61       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
62     {
63       rv = VNET_API_ERROR_SAME_SRC_DST;
64       goto out;
65     }
66   memset (a, 0, sizeof (*a));
67
68   a->is_add = mp->is_add;
69   a->teb = mp->teb;
70   a->is_ipv6 = mp->is_ipv6;
71
72   /* ip addresses sent in network byte order */
73   if (!mp->is_ipv6)
74     {
75       clib_memcpy (&(a->src.ip4), mp->src_address, 4);
76       clib_memcpy (&(a->dst.ip4), mp->dst_address, 4);
77     }
78   else
79     {
80       clib_memcpy (&(a->src.ip6), mp->src_address, 16);
81       clib_memcpy (&(a->dst.ip6), mp->dst_address, 16);
82     }
83
84   a->outer_fib_id = ntohl (mp->outer_fib_id);
85   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
86
87 out:
88   /* *INDENT-OFF* */
89   REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
90   ({
91     rmp->sw_if_index = ntohl (sw_if_index);
92   }));
93   /* *INDENT-ON* */
94 }
95
96 static void send_gre_tunnel_details
97   (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
98 {
99   vl_api_gre_tunnel_details_t *rmp;
100   u8 is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
101   fib_table_t *ft;
102
103   rmp = vl_msg_api_alloc (sizeof (*rmp));
104   memset (rmp, 0, sizeof (*rmp));
105   rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
106   if (!is_ipv6)
107     {
108       clib_memcpy (rmp->src_address, &(t->tunnel_src.ip4.as_u8), 4);
109       clib_memcpy (rmp->dst_address, &(t->tunnel_dst.fp_addr.ip4.as_u8), 4);
110       ft = fib_table_get (t->outer_fib_index, FIB_PROTOCOL_IP4);
111       rmp->outer_fib_id = ft->ft_table_id;
112     }
113   else
114     {
115       clib_memcpy (rmp->src_address, &(t->tunnel_src.ip6.as_u8), 16);
116       clib_memcpy (rmp->dst_address, &(t->tunnel_dst.fp_addr.ip6.as_u8), 16);
117       ft = fib_table_get (t->outer_fib_index, FIB_PROTOCOL_IP6);
118       rmp->outer_fib_id = ft->ft_table_id;
119     }
120   rmp->teb = (GRE_TUNNEL_TYPE_TEB == t->type);
121   rmp->sw_if_index = htonl (t->sw_if_index);
122   rmp->context = context;
123   rmp->is_ipv6 = is_ipv6;
124
125   vl_msg_api_send_shmem (q, (u8 *) & rmp);
126 }
127
128 static void
129 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
130 {
131   unix_shared_memory_queue_t *q;
132   gre_main_t *gm = &gre_main;
133   gre_tunnel_t *t;
134   u32 sw_if_index;
135
136   q = vl_api_client_index_to_input_queue (mp->client_index);
137   if (q == 0)
138     {
139       return;
140     }
141
142   sw_if_index = ntohl (mp->sw_if_index);
143
144   if (~0 == sw_if_index)
145     {
146       /* *INDENT-OFF* */
147       pool_foreach (t, gm->tunnels,
148       ({
149         send_gre_tunnel_details(t, q, mp->context);
150       }));
151       /* *INDENT-ON* */
152     }
153   else
154     {
155       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
156           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
157         {
158           return;
159         }
160       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
161       send_gre_tunnel_details (t, q, mp->context);
162     }
163 }
164
165 /*
166  * gre_api_hookup
167  * Add vpe's API message handlers to the table.
168  * vlib has alread mapped shared memory and
169  * added the client registration handlers.
170  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
171  */
172 #define vl_msg_name_crc_list
173 #include <vnet/vnet_all_api_h.h>
174 #undef vl_msg_name_crc_list
175
176 static void
177 setup_message_id_table (api_main_t * am)
178 {
179 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
180   foreach_vl_msg_name_crc_gre;
181 #undef _
182 }
183
184 static clib_error_t *
185 gre_api_hookup (vlib_main_t * vm)
186 {
187   api_main_t *am = &api_main;
188
189 #define _(N,n)                                                  \
190     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
191                            vl_api_##n##_t_handler,              \
192                            vl_noop_handler,                     \
193                            vl_api_##n##_t_endian,               \
194                            vl_api_##n##_t_print,                \
195                            sizeof(vl_api_##n##_t), 1);
196   foreach_vpe_api_msg;
197 #undef _
198
199   /*
200    * Set up the (msg_name, crc, message-id) table
201    */
202   setup_message_id_table (am);
203
204   return 0;
205 }
206
207 VLIB_API_INIT_FUNCTION (gre_api_hookup);
208
209 /*
210  * fd.io coding-style-patch-verification: ON
211  *
212  * Local Variables:
213  * eval: (c-set-style "gnu")
214  * End:
215  */