blob: 45107f08ab18fc1c982a1b985fbe398cb54b2fc6 [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
Filip Tehlar63c0fd32021-06-28 00:52:00 +000032#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
Pablo Camarillo42998822017-07-13 09:41:32 +020033
Filip Tehlar63c0fd32021-06-28 00:52:00 +000034#define vl_api_version(n, v) static u32 api_version = v;
35#include <vnet/srmpls/sr_mpls.api.h>
36#undef vl_api_version
37
38#define vl_endianfun
39#include <vnet/srmpls/sr_mpls.api.h>
Pablo Camarillo42998822017-07-13 09:41:32 +020040#undef vl_endianfun
41
Klement Sekera9b7e8ac2021-11-22 21:26:20 +010042#define vl_calcsizefun
43#include <vnet/srmpls/sr_mpls.api.h>
44#undef vl_calcsizefun
45
Pablo Camarillo42998822017-07-13 09:41:32 +020046#define vl_printfun
Filip Tehlar63c0fd32021-06-28 00:52:00 +000047#include <vnet/srmpls/sr_mpls.api.h>
Pablo Camarillo42998822017-07-13 09:41:32 +020048#undef vl_printfun
49
Filip Tehlar63c0fd32021-06-28 00:52:00 +000050#define vl_msg_name_crc_list
51#include <vnet/srmpls/sr_mpls.api.h>
52#undef vl_msg_name_crc_list
53
54#define REPLY_MSG_ID_BASE msg_id_base
Pablo Camarillo42998822017-07-13 09:41:32 +020055#include <vlibapi/api_helper_macros.h>
56
57#define foreach_vpe_api_msg \
58_(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \
59_(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \
60_(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
61
Filip Tehlar63c0fd32021-06-28 00:52:00 +000062static u16 msg_id_base;
Pablo Camarillo42998822017-07-13 09:41:32 +020063
64static void
65vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
66{
67 vl_api_sr_mpls_policy_add_reply_t *rmp;
68
69 mpls_label_t *segments = 0, *seg;
70 mpls_label_t this_address = 0;
71
72 int i;
73 for (i = 0; i < mp->n_segments; i++)
74 {
75 vec_add2 (segments, seg, 1);
76 this_address = ntohl (mp->segments[i]);
77 clib_memcpy (seg, &this_address, sizeof (this_address));
78 }
79
80 int rv = 0;
81 rv = sr_mpls_policy_add (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +010082 segments, mp->is_spray, ntohl (mp->weight));
John Lod23d39c2018-09-13 15:08:08 -040083 vec_free (segments);
Pablo Camarillo42998822017-07-13 09:41:32 +020084
85 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
86}
87
88static void
89vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
90{
91 vl_api_sr_mpls_policy_mod_reply_t *rmp;
92
93 mpls_label_t *segments = 0, *seg;
94 mpls_label_t this_address = 0;
95
96 int i;
97 for (i = 0; i < mp->n_segments; i++)
98 {
99 vec_add2 (segments, seg, 1);
100 this_address = ntohl (mp->segments[i]);
101 clib_memcpy (seg, &this_address, sizeof (this_address));
102 }
103
104 int rv = 0;
105 rv = sr_mpls_policy_mod (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100106 ntohl (mp->operation), segments,
107 ntohl (mp->sl_index), ntohl (mp->weight));
John Lod23d39c2018-09-13 15:08:08 -0400108 vec_free (segments);
Pablo Camarillo42998822017-07-13 09:41:32 +0200109
110 REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
111}
112
113static void
114vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
115{
116 vl_api_sr_mpls_policy_del_reply_t *rmp;
117 int rv = 0;
118 rv = sr_mpls_policy_del (ntohl (mp->bsid));
119
120 REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
121}
122
123static void vl_api_sr_mpls_steering_add_del_t_handler
124 (vl_api_sr_mpls_steering_add_del_t * mp)
125{
126 vl_api_sr_mpls_steering_add_del_reply_t *rmp;
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100127 fib_prefix_t prefix;
128 ip46_address_t next_hop;
Dave Barachb7b92992018-10-17 10:38:51 -0400129 clib_memset (&prefix, 0, sizeof (ip46_address_t));
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100130
131 ip_prefix_decode (&mp->prefix, &prefix);
132 ip_address_decode (&mp->next_hop, &next_hop);
Pablo Camarillo42998822017-07-13 09:41:32 +0200133
134 int rv = 0;
135 if (mp->is_del)
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100136 rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
137 prefix.fp_len,
138 ip46_address_is_ip4 (&prefix.fp_addr) ?
139 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200140 ntohl (mp->table_id),
141 ntohl (mp->color));
142 else
143 rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
144 ntohl (mp->table_id),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100145 &prefix.fp_addr,
146 prefix.fp_len,
147 ip46_address_is_ip4 (&prefix.fp_addr) ?
148 SR_STEER_IPV4 : SR_STEER_IPV6,
149 &next_hop,
150 ip46_address_is_ip4 (&next_hop) ?
151 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200152 ntohl (mp->color), mp->co_bits,
153 ntohl (mp->vpn_label));
154
155 REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
156}
157
158static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
159 (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
160{
161 vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
162 int rv = 0;
163
164 ip46_address_t endpoint;
Dave Barachb7b92992018-10-17 10:38:51 -0400165 clib_memset (&endpoint, 0, sizeof (ip46_address_t));
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100166 ip_address_decode (&mp->endpoint, &endpoint);
Pablo Camarillo42998822017-07-13 09:41:32 +0200167
168 rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100169 &endpoint,
170 ip46_address_is_ip4 (&endpoint) ?
171 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200172 ntohl (mp->color));
173
174 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
175}
176
Pablo Camarillo42998822017-07-13 09:41:32 +0200177static void
178setup_message_id_table (api_main_t * am)
179{
Filip Tehlar63c0fd32021-06-28 00:52:00 +0000180#define _(id, n, crc) \
181 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + REPLY_MSG_ID_BASE);
Pablo Camarillo42998822017-07-13 09:41:32 +0200182 foreach_vl_msg_name_crc_sr_mpls;
183#undef _
184}
185
186static clib_error_t *
187sr_mpls_api_hookup (vlib_main_t * vm)
188{
Dave Barach39d69112019-11-27 11:42:13 -0500189 api_main_t *am = vlibapi_get_main ();
Pablo Camarillo42998822017-07-13 09:41:32 +0200190
Filip Tehlar63c0fd32021-06-28 00:52:00 +0000191 u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0);
192 REPLY_MSG_ID_BASE =
193 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST);
194 vec_free (name);
195
196#define _(N, n) \
Filip Tehlar36217e32021-07-23 08:51:10 +0000197 vl_msg_api_set_handlers ( \
198 REPLY_MSG_ID_BASE + VL_API_##N, #n, vl_api_##n##_t_handler, \
199 vl_noop_handler, vl_api_##n##_t_endian, vl_api_##n##_t_print, \
200 sizeof (vl_api_##n##_t), 1, vl_api_##n##_t_print_json, \
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100201 vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson, \
202 vl_api_##n##_t_calc_size);
Pablo Camarillo42998822017-07-13 09:41:32 +0200203 foreach_vpe_api_msg;
204#undef _
205
206 /*
207 * Manually register the sr policy add msg, so we trace enough bytes
208 * to capture a typical segment list
209 */
Filip Tehlar36217e32021-07-23 08:51:10 +0000210 vl_msg_api_set_handlers (
211 REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD, "sr_mpls_policy_add",
212 vl_api_sr_mpls_policy_add_t_handler, vl_noop_handler,
213 vl_api_sr_mpls_policy_add_t_endian, vl_api_sr_mpls_policy_add_t_print, 256,
214 1, vl_api_sr_mpls_policy_add_t_print_json,
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100215 vl_api_sr_mpls_policy_add_t_tojson, vl_api_sr_mpls_policy_add_t_fromjson,
216 vl_api_sr_mpls_policy_add_t_calc_size);
Pablo Camarillo42998822017-07-13 09:41:32 +0200217
218 /*
219 * Manually register the sr policy mod msg, so we trace enough bytes
220 * to capture a typical segment list
221 */
Filip Tehlar36217e32021-07-23 08:51:10 +0000222 vl_msg_api_set_handlers (
223 REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_MOD, "sr_mpls_policy_mod",
224 vl_api_sr_mpls_policy_mod_t_handler, vl_noop_handler,
225 vl_api_sr_mpls_policy_mod_t_endian, vl_api_sr_mpls_policy_mod_t_print, 256,
226 1, vl_api_sr_mpls_policy_mod_t_print_json,
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100227 vl_api_sr_mpls_policy_mod_t_tojson, vl_api_sr_mpls_policy_mod_t_fromjson,
228 vl_api_sr_mpls_policy_mod_t_calc_size);
Pablo Camarillo42998822017-07-13 09:41:32 +0200229
230 /*
231 * Set up the (msg_name, crc, message-id) table
232 */
233 setup_message_id_table (am);
234
235 return 0;
236}
237
238VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
239
240/*
241 * fd.io coding-style-patch-verification: ON
242 *
243 * Local Variables: eval: (c-set-style "gnu") End:
244 */