SR-MPLS: binary API and automated steering
[vpp.git] / src / vnet / srmpls / sr_mpls_api.c
1 /*
2  * ------------------------------------------------------------------
3  * sr_api.c - ipv6 segment routing api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates. Licensed under the Apache
6  * License, Version 2.0 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  * ------------------------------------------------------------------
17  */
18
19 #include <vnet/vnet.h>
20 #include <vnet/srmpls/sr_mpls.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_MPLS_POLICY_DEL, sr_mpls_policy_del)                         \
47 _(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del)             \
48 _(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
49
50
51 static void
52 vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
53 {
54   vl_api_sr_mpls_policy_add_reply_t *rmp;
55
56   mpls_label_t *segments = 0, *seg;
57   mpls_label_t this_address = 0;
58
59   int i;
60   for (i = 0; i < mp->n_segments; i++)
61     {
62       vec_add2 (segments, seg, 1);
63       this_address = ntohl (mp->segments[i]);
64       clib_memcpy (seg, &this_address, sizeof (this_address));
65     }
66
67   int rv = 0;
68   rv = sr_mpls_policy_add (ntohl (mp->bsid),
69                            segments, mp->type, ntohl (mp->weight));
70
71   REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
72 }
73
74 static void
75 vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
76 {
77   vl_api_sr_mpls_policy_mod_reply_t *rmp;
78
79   mpls_label_t *segments = 0, *seg;
80   mpls_label_t this_address = 0;
81
82   int i;
83   for (i = 0; i < mp->n_segments; i++)
84     {
85       vec_add2 (segments, seg, 1);
86       this_address = ntohl (mp->segments[i]);
87       clib_memcpy (seg, &this_address, sizeof (this_address));
88     }
89
90   int rv = 0;
91   rv = sr_mpls_policy_mod (ntohl (mp->bsid),
92                            mp->operation, segments, ntohl (mp->sl_index),
93                            ntohl (mp->weight));
94
95   REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
96 }
97
98 static void
99 vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
100 {
101   vl_api_sr_mpls_policy_del_reply_t *rmp;
102   int rv = 0;
103   rv = sr_mpls_policy_del (ntohl (mp->bsid));
104
105   REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
106 }
107
108 static void vl_api_sr_mpls_steering_add_del_t_handler
109   (vl_api_sr_mpls_steering_add_del_t * mp)
110 {
111   vl_api_sr_mpls_steering_add_del_reply_t *rmp;
112   ip46_address_t prefix;
113   memset (&prefix, 0, sizeof (ip46_address_t));
114   if (mp->traffic_type == SR_STEER_IPV4)
115     memcpy (&prefix.ip4, mp->prefix_addr, sizeof (prefix.ip4));
116   else
117     memcpy (&prefix, mp->prefix_addr, sizeof (prefix.ip6));
118
119   int rv = 0;
120   if (mp->is_del)
121     rv = sr_mpls_steering_policy_del (&prefix,
122                                       ntohl (mp->mask_width),
123                                       mp->traffic_type,
124                                       ntohl (mp->table_id),
125                                       ntohl (mp->color));
126   else
127     rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
128                                       ntohl (mp->table_id),
129                                       &prefix,
130                                       ntohl (mp->mask_width),
131                                       mp->traffic_type,
132                                       (ip46_address_t *) & mp->next_hop,
133                                       mp->nh_type,
134                                       ntohl (mp->color), mp->co_bits,
135                                       ntohl (mp->vpn_label));
136
137   REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
138 }
139
140 static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
141   (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
142 {
143   vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
144   int rv = 0;
145
146   ip46_address_t endpoint;
147   memset (&endpoint, 0, sizeof (ip46_address_t));
148   if (mp->endpoint_type == SR_STEER_IPV4)
149     memcpy (&endpoint.ip4, mp->endpoint, sizeof (endpoint.ip4));
150   else
151     memcpy (&endpoint, mp->endpoint, sizeof (endpoint.ip6));
152
153   rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
154                                              &endpoint, mp->endpoint_type,
155                                              ntohl (mp->color));
156
157   REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
158 }
159
160 /*
161  * sr_mpls_api_hookup Add vpe's API message handlers to the table. vlib has
162  * alread mapped shared memory and added the client registration handlers.
163  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
164  */
165 #define vl_msg_name_crc_list
166 #include <vnet/vnet_all_api_h.h>
167 #undef vl_msg_name_crc_list
168
169 static void
170 setup_message_id_table (api_main_t * am)
171 {
172 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
173   foreach_vl_msg_name_crc_sr_mpls;
174 #undef _
175 }
176
177 static clib_error_t *
178 sr_mpls_api_hookup (vlib_main_t * vm)
179 {
180   api_main_t *am = &api_main;
181
182 #define _(N,n)                                                  \
183     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
184                            vl_api_##n##_t_handler,              \
185                            vl_noop_handler,                     \
186                            vl_api_##n##_t_endian,               \
187                            vl_api_##n##_t_print,                \
188                            sizeof(vl_api_##n##_t), 1);
189   foreach_vpe_api_msg;
190 #undef _
191
192   /*
193    * Manually register the sr policy add msg, so we trace enough bytes
194    * to capture a typical segment list
195    */
196   vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_ADD,
197                            "sr_mpls_policy_add",
198                            vl_api_sr_mpls_policy_add_t_handler,
199                            vl_noop_handler,
200                            vl_api_sr_mpls_policy_add_t_endian,
201                            vl_api_sr_mpls_policy_add_t_print, 256, 1);
202
203   /*
204    * Manually register the sr policy mod msg, so we trace enough bytes
205    * to capture a typical segment list
206    */
207   vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_MOD,
208                            "sr_mpls_policy_mod",
209                            vl_api_sr_mpls_policy_mod_t_handler,
210                            vl_noop_handler,
211                            vl_api_sr_mpls_policy_mod_t_endian,
212                            vl_api_sr_mpls_policy_mod_t_print, 256, 1);
213
214   /*
215    * Set up the (msg_name, crc, message-id) table
216    */
217   setup_message_id_table (am);
218
219   return 0;
220 }
221
222 VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
223
224 /*
225  * fd.io coding-style-patch-verification: ON
226  *
227  * Local Variables: eval: (c-set-style "gnu") End:
228  */