blob: 920856acff6578a53eeafc02d03cb8a2a5851291 [file] [log] [blame]
Pablo Camarillo42998822017-07-13 09:41:32 +02001/*
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>
Jakub Grajciar00ec4012020-01-31 10:17:29 +010026#include <vnet/ip/ip_types_api.h>
27
Filip Tehlar63c0fd32021-06-28 00:52:00 +000028#include <vnet/format_fns.h>
29#include <vnet/srmpls/sr_mpls.api_enum.h>
30#include <vnet/srmpls/sr_mpls.api_types.h>
Pablo Camarillo42998822017-07-13 09:41:32 +020031
Pablo Camarillo42998822017-07-13 09:41:32 +020032
Filip Tehlar63c0fd32021-06-28 00:52:00 +000033#define vl_api_version(n, v) static u32 api_version = v;
34#include <vnet/srmpls/sr_mpls.api.h>
35#undef vl_api_version
36
37#define vl_endianfun
38#include <vnet/srmpls/sr_mpls.api.h>
Pablo Camarillo42998822017-07-13 09:41:32 +020039#undef vl_endianfun
40
Klement Sekera9b7e8ac2021-11-22 21:26:20 +010041#define vl_calcsizefun
42#include <vnet/srmpls/sr_mpls.api.h>
43#undef vl_calcsizefun
44
Pablo Camarillo42998822017-07-13 09:41:32 +020045#define vl_printfun
Filip Tehlar63c0fd32021-06-28 00:52:00 +000046#include <vnet/srmpls/sr_mpls.api.h>
Pablo Camarillo42998822017-07-13 09:41:32 +020047#undef vl_printfun
48
Filip Tehlar63c0fd32021-06-28 00:52:00 +000049#define vl_msg_name_crc_list
50#include <vnet/srmpls/sr_mpls.api.h>
51#undef vl_msg_name_crc_list
52
53#define REPLY_MSG_ID_BASE msg_id_base
Pablo Camarillo42998822017-07-13 09:41:32 +020054#include <vlibapi/api_helper_macros.h>
55
56#define foreach_vpe_api_msg \
57_(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \
58_(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \
59_(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
60
Filip Tehlar63c0fd32021-06-28 00:52:00 +000061static u16 msg_id_base;
Pablo Camarillo42998822017-07-13 09:41:32 +020062
63static void
64vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
65{
66 vl_api_sr_mpls_policy_add_reply_t *rmp;
67
68 mpls_label_t *segments = 0, *seg;
69 mpls_label_t this_address = 0;
70
71 int i;
72 for (i = 0; i < mp->n_segments; i++)
73 {
74 vec_add2 (segments, seg, 1);
75 this_address = ntohl (mp->segments[i]);
76 clib_memcpy (seg, &this_address, sizeof (this_address));
77 }
78
79 int rv = 0;
80 rv = sr_mpls_policy_add (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +010081 segments, mp->is_spray, ntohl (mp->weight));
John Lod23d39c2018-09-13 15:08:08 -040082 vec_free (segments);
Pablo Camarillo42998822017-07-13 09:41:32 +020083
84 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
85}
86
87static void
88vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
89{
90 vl_api_sr_mpls_policy_mod_reply_t *rmp;
91
92 mpls_label_t *segments = 0, *seg;
93 mpls_label_t this_address = 0;
94
95 int i;
96 for (i = 0; i < mp->n_segments; i++)
97 {
98 vec_add2 (segments, seg, 1);
99 this_address = ntohl (mp->segments[i]);
100 clib_memcpy (seg, &this_address, sizeof (this_address));
101 }
102
103 int rv = 0;
104 rv = sr_mpls_policy_mod (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100105 ntohl (mp->operation), segments,
106 ntohl (mp->sl_index), ntohl (mp->weight));
John Lod23d39c2018-09-13 15:08:08 -0400107 vec_free (segments);
Pablo Camarillo42998822017-07-13 09:41:32 +0200108
109 REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
110}
111
112static void
113vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
114{
115 vl_api_sr_mpls_policy_del_reply_t *rmp;
116 int rv = 0;
117 rv = sr_mpls_policy_del (ntohl (mp->bsid));
118
119 REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
120}
121
122static void vl_api_sr_mpls_steering_add_del_t_handler
123 (vl_api_sr_mpls_steering_add_del_t * mp)
124{
125 vl_api_sr_mpls_steering_add_del_reply_t *rmp;
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100126 fib_prefix_t prefix;
127 ip46_address_t next_hop;
Dave Barachb7b92992018-10-17 10:38:51 -0400128 clib_memset (&prefix, 0, sizeof (ip46_address_t));
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100129
130 ip_prefix_decode (&mp->prefix, &prefix);
131 ip_address_decode (&mp->next_hop, &next_hop);
Pablo Camarillo42998822017-07-13 09:41:32 +0200132
133 int rv = 0;
134 if (mp->is_del)
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100135 rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
136 prefix.fp_len,
137 ip46_address_is_ip4 (&prefix.fp_addr) ?
138 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200139 ntohl (mp->table_id),
140 ntohl (mp->color));
141 else
142 rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
143 ntohl (mp->table_id),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100144 &prefix.fp_addr,
145 prefix.fp_len,
146 ip46_address_is_ip4 (&prefix.fp_addr) ?
147 SR_STEER_IPV4 : SR_STEER_IPV6,
148 &next_hop,
149 ip46_address_is_ip4 (&next_hop) ?
150 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200151 ntohl (mp->color), mp->co_bits,
152 ntohl (mp->vpn_label));
153
154 REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
155}
156
157static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
158 (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
159{
160 vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
161 int rv = 0;
162
163 ip46_address_t endpoint;
Dave Barachb7b92992018-10-17 10:38:51 -0400164 clib_memset (&endpoint, 0, sizeof (ip46_address_t));
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100165 ip_address_decode (&mp->endpoint, &endpoint);
Pablo Camarillo42998822017-07-13 09:41:32 +0200166
167 rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100168 &endpoint,
169 ip46_address_is_ip4 (&endpoint) ?
170 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200171 ntohl (mp->color));
172
173 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
174}
175
Pablo Camarillo42998822017-07-13 09:41:32 +0200176static void
177setup_message_id_table (api_main_t * am)
178{
Filip Tehlar63c0fd32021-06-28 00:52:00 +0000179#define _(id, n, crc) \
180 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + REPLY_MSG_ID_BASE);
Pablo Camarillo42998822017-07-13 09:41:32 +0200181 foreach_vl_msg_name_crc_sr_mpls;
182#undef _
183}
184
185static clib_error_t *
186sr_mpls_api_hookup (vlib_main_t * vm)
187{
Dave Barach39d69112019-11-27 11:42:13 -0500188 api_main_t *am = vlibapi_get_main ();
Pablo Camarillo42998822017-07-13 09:41:32 +0200189
Filip Tehlar63c0fd32021-06-28 00:52:00 +0000190 u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0);
191 REPLY_MSG_ID_BASE =
192 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST);
193 vec_free (name);
194
195#define _(N, n) \
Damjan Mariona2eb5072022-05-20 20:06:01 +0200196 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
197 .id = REPLY_MSG_ID_BASE + VL_API_##N, \
198 .name = #n, \
199 .handler = vl_api_##n##_t_handler, \
200 .endian = vl_api_##n##_t_endian, \
201 .format_fn = vl_api_##n##_t_format, \
202 .size = sizeof (vl_api_##n##_t), \
203 .traced = 1, \
204 .tojson = vl_api_##n##_t_tojson, \
205 .fromjson = vl_api_##n##_t_fromjson, \
206 .calc_size = vl_api_##n##_t_calc_size, \
207 });
Pablo Camarillo42998822017-07-13 09:41:32 +0200208 foreach_vpe_api_msg;
209#undef _
210
211 /*
212 * Manually register the sr policy add msg, so we trace enough bytes
213 * to capture a typical segment list
214 */
Damjan Mariona2eb5072022-05-20 20:06:01 +0200215 vl_msg_api_config (&(vl_msg_api_msg_config_t){
216 .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD,
217 .name = "sr_mpls_policy_add",
218 .handler = vl_api_sr_mpls_policy_add_t_handler,
219 .endian = vl_api_sr_mpls_policy_add_t_endian,
220 .format_fn = vl_api_sr_mpls_policy_add_t_format,
221 .size = 256,
222 .traced = 1,
223 .tojson = vl_api_sr_mpls_policy_add_t_tojson,
224 .fromjson = vl_api_sr_mpls_policy_add_t_fromjson,
225 .calc_size = vl_api_sr_mpls_policy_add_t_calc_size,
226 });
Pablo Camarillo42998822017-07-13 09:41:32 +0200227 /*
228 * Manually register the sr policy mod msg, so we trace enough bytes
229 * to capture a typical segment list
230 */
Damjan Mariona2eb5072022-05-20 20:06:01 +0200231 vl_msg_api_config (&(vl_msg_api_msg_config_t){
232 .id = REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_MOD,
233 .name = "sr_mpls_policy_mod",
234 .handler = vl_api_sr_mpls_policy_mod_t_handler,
235 .endian = vl_api_sr_mpls_policy_mod_t_endian,
236 .format_fn = vl_api_sr_mpls_policy_mod_t_format,
237 .size = 256,
238 .traced = 1,
239 .tojson = vl_api_sr_mpls_policy_mod_t_tojson,
240 .fromjson = vl_api_sr_mpls_policy_mod_t_fromjson,
241 .calc_size = vl_api_sr_mpls_policy_mod_t_calc_size,
242 });
Pablo Camarillo42998822017-07-13 09:41:32 +0200243
244 /*
245 * Set up the (msg_name, crc, message-id) table
246 */
247 setup_message_id_table (am);
248
249 return 0;
250}
251
252VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
253
254/*
255 * fd.io coding-style-patch-verification: ON
256 *
257 * Local Variables: eval: (c-set-style "gnu") End:
258 */