API refactoring : l2, mpls, sr
[vpp.git] / src / vnet / sr / sr_api.c
1 /*
2  *------------------------------------------------------------------
3  * sr_api.c - ipv6 segment routing 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
27 #include <vnet/vnet_msg_enum.h>
28
29 #define vl_typedefs             /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_typedefs
32
33 #define vl_endianfun            /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_endianfun
36
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #define vl_printfun
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_printfun
42
43 #include <vlibapi/api_helper_macros.h>
44
45 #define foreach_vpe_api_msg                             \
46 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)
47
48 static void vl_api_sr_tunnel_add_del_t_handler
49   (vl_api_sr_tunnel_add_del_t * mp)
50 {
51 #if IP6SR == 0
52   clib_warning ("unimplemented");
53 #else
54   ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
55   int rv = 0;
56   vl_api_sr_tunnel_add_del_reply_t *rmp;
57   ip6_address_t *segments = 0, *seg;
58   ip6_address_t *tags = 0, *tag;
59   ip6_address_t *this_address;
60   int i;
61
62   if (mp->n_segments == 0)
63     {
64       rv = -11;
65       goto out;
66     }
67
68   memset (a, 0, sizeof (*a));
69   a->src_address = (ip6_address_t *) & mp->src_address;
70   a->dst_address = (ip6_address_t *) & mp->dst_address;
71   a->dst_mask_width = mp->dst_mask_width;
72   a->flags_net_byte_order = mp->flags_net_byte_order;
73   a->is_del = (mp->is_add == 0);
74   a->rx_table_id = ntohl (mp->outer_vrf_id);
75   a->tx_table_id = ntohl (mp->inner_vrf_id);
76
77   a->name = format (0, "%s", mp->name);
78   if (!(vec_len (a->name)))
79     a->name = 0;
80
81   a->policy_name = format (0, "%s", mp->policy_name);
82   if (!(vec_len (a->policy_name)))
83     a->policy_name = 0;
84
85   /* Yank segments and tags out of the API message */
86   this_address = (ip6_address_t *) mp->segs_and_tags;
87   for (i = 0; i < mp->n_segments; i++)
88     {
89       vec_add2 (segments, seg, 1);
90       clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
91       this_address++;
92     }
93   for (i = 0; i < mp->n_tags; i++)
94     {
95       vec_add2 (tags, tag, 1);
96       clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
97       this_address++;
98     }
99
100   a->segments = segments;
101   a->tags = tags;
102
103   rv = ip6_sr_add_del_tunnel (a);
104
105 out:
106
107   REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
108 #endif
109 }
110
111 static void vl_api_sr_policy_add_del_t_handler
112   (vl_api_sr_policy_add_del_t * mp)
113 {
114 #if IP6SR == 0
115   clib_warning ("unimplemented");
116 #else
117   ip6_sr_add_del_policy_args_t _a, *a = &_a;
118   int rv = 0;
119   vl_api_sr_policy_add_del_reply_t *rmp;
120   int i;
121
122   memset (a, 0, sizeof (*a));
123   a->is_del = (mp->is_add == 0);
124
125   a->name = format (0, "%s", mp->name);
126   if (!(vec_len (a->name)))
127     {
128       rv = VNET_API_ERROR_NO_SUCH_NODE2;
129       goto out;
130     }
131
132   if (!(mp->tunnel_names[0]))
133     {
134       rv = VNET_API_ERROR_NO_SUCH_NODE2;
135       goto out;
136     }
137
138   // start deserializing tunnel_names
139   int num_tunnels = mp->tunnel_names[0];        //number of tunnels
140   u8 *deser_tun_names = mp->tunnel_names;
141   deser_tun_names += 1;         //moving along
142
143   u8 *tun_name = 0;
144   int tun_name_len = 0;
145
146   for (i = 0; i < num_tunnels; i++)
147     {
148       tun_name_len = *deser_tun_names;
149       deser_tun_names += 1;
150       vec_resize (tun_name, tun_name_len);
151       memcpy (tun_name, deser_tun_names, tun_name_len);
152       vec_add1 (a->tunnel_names, tun_name);
153       deser_tun_names += tun_name_len;
154       tun_name = 0;
155     }
156
157   rv = ip6_sr_add_del_policy (a);
158
159 out:
160
161   REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
162 #endif
163 }
164
165 static void vl_api_sr_multicast_map_add_del_t_handler
166   (vl_api_sr_multicast_map_add_del_t * mp)
167 {
168 #if IP6SR == 0
169   clib_warning ("unimplemented");
170 #else
171   ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
172   int rv = 0;
173   vl_api_sr_multicast_map_add_del_reply_t *rmp;
174
175   memset (a, 0, sizeof (*a));
176   a->is_del = (mp->is_add == 0);
177
178   a->multicast_address = (ip6_address_t *) & mp->multicast_address;
179   a->policy_name = format (0, "%s", mp->policy_name);
180
181   if (a->multicast_address == 0)
182     {
183       rv = -1;
184       goto out;
185     }
186
187   if (!(a->policy_name))
188     {
189       rv = -2;
190       goto out;
191     }
192
193 #if DPDK > 0                    /* Cannot call replicate without DPDK */
194   rv = ip6_sr_add_del_multicastmap (a);
195 #else
196   clib_warning ("multicast replication without DPDK not implemented");
197   rv = VNET_API_ERROR_UNIMPLEMENTED;
198 #endif /* DPDK */
199
200 out:
201
202   REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
203 #endif
204 }
205
206 /*
207  * sr_api_hookup
208  * Add vpe's API message handlers to the table.
209  * vlib has alread mapped shared memory and
210  * added the client registration handlers.
211  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
212  */
213 #define vl_msg_name_crc_list
214 #include <vnet/vnet_all_api_h.h>
215 #undef vl_msg_name_crc_list
216
217 static void
218 setup_message_id_table (api_main_t * am)
219 {
220 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
221   foreach_vl_msg_name_crc_sr;
222 #undef _
223 }
224
225 static clib_error_t *
226 sr_api_hookup (vlib_main_t * vm)
227 {
228   api_main_t *am = &api_main;
229
230 #define _(N,n)                                                  \
231     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
232                            vl_api_##n##_t_handler,              \
233                            vl_noop_handler,                     \
234                            vl_api_##n##_t_endian,               \
235                            vl_api_##n##_t_print,                \
236                            sizeof(vl_api_##n##_t), 1);
237   foreach_vpe_api_msg;
238 #undef _
239
240   /*
241    * Manually register the sr tunnel add del msg, so we trace
242    * enough bytes to capture a typical segment list
243    */
244   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
245                            "sr_tunnel_add_del",
246                            vl_api_sr_tunnel_add_del_t_handler,
247                            vl_noop_handler,
248                            vl_api_sr_tunnel_add_del_t_endian,
249                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
250
251
252   /*
253    * Manually register the sr policy add del msg, so we trace
254    * enough bytes to capture a typical tunnel name list
255    */
256   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
257                            "sr_policy_add_del",
258                            vl_api_sr_policy_add_del_t_handler,
259                            vl_noop_handler,
260                            vl_api_sr_policy_add_del_t_endian,
261                            vl_api_sr_policy_add_del_t_print, 256, 1);
262
263   /*
264    * Set up the (msg_name, crc, message-id) table
265    */
266   setup_message_id_table (am);
267
268   return 0;
269 }
270
271 VLIB_API_INIT_FUNCTION (sr_api_hookup);
272
273 /*
274  * fd.io coding-style-patch-verification: ON
275  *
276  * Local Variables:
277  * eval: (c-set-style "gnu")
278  * End:
279  */