blob: 7d42f1ba451c8c8cb76bc8962312add8d7bec383 [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
Pablo Camarillo42998822017-07-13 09:41:32 +020042#define vl_printfun
Filip Tehlar63c0fd32021-06-28 00:52:00 +000043#include <vnet/srmpls/sr_mpls.api.h>
Pablo Camarillo42998822017-07-13 09:41:32 +020044#undef vl_printfun
45
Filip Tehlar63c0fd32021-06-28 00:52:00 +000046#define vl_msg_name_crc_list
47#include <vnet/srmpls/sr_mpls.api.h>
48#undef vl_msg_name_crc_list
49
50#define REPLY_MSG_ID_BASE msg_id_base
Pablo Camarillo42998822017-07-13 09:41:32 +020051#include <vlibapi/api_helper_macros.h>
52
53#define foreach_vpe_api_msg \
54_(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \
55_(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \
56_(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
57
Filip Tehlar63c0fd32021-06-28 00:52:00 +000058static u16 msg_id_base;
Pablo Camarillo42998822017-07-13 09:41:32 +020059
60static void
61vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
62{
63 vl_api_sr_mpls_policy_add_reply_t *rmp;
64
65 mpls_label_t *segments = 0, *seg;
66 mpls_label_t this_address = 0;
67
68 int i;
69 for (i = 0; i < mp->n_segments; i++)
70 {
71 vec_add2 (segments, seg, 1);
72 this_address = ntohl (mp->segments[i]);
73 clib_memcpy (seg, &this_address, sizeof (this_address));
74 }
75
76 int rv = 0;
77 rv = sr_mpls_policy_add (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +010078 segments, mp->is_spray, ntohl (mp->weight));
John Lod23d39c2018-09-13 15:08:08 -040079 vec_free (segments);
Pablo Camarillo42998822017-07-13 09:41:32 +020080
81 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
82}
83
84static void
85vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
86{
87 vl_api_sr_mpls_policy_mod_reply_t *rmp;
88
89 mpls_label_t *segments = 0, *seg;
90 mpls_label_t this_address = 0;
91
92 int i;
93 for (i = 0; i < mp->n_segments; i++)
94 {
95 vec_add2 (segments, seg, 1);
96 this_address = ntohl (mp->segments[i]);
97 clib_memcpy (seg, &this_address, sizeof (this_address));
98 }
99
100 int rv = 0;
101 rv = sr_mpls_policy_mod (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100102 ntohl (mp->operation), segments,
103 ntohl (mp->sl_index), ntohl (mp->weight));
John Lod23d39c2018-09-13 15:08:08 -0400104 vec_free (segments);
Pablo Camarillo42998822017-07-13 09:41:32 +0200105
106 REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
107}
108
109static void
110vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
111{
112 vl_api_sr_mpls_policy_del_reply_t *rmp;
113 int rv = 0;
114 rv = sr_mpls_policy_del (ntohl (mp->bsid));
115
116 REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
117}
118
119static void vl_api_sr_mpls_steering_add_del_t_handler
120 (vl_api_sr_mpls_steering_add_del_t * mp)
121{
122 vl_api_sr_mpls_steering_add_del_reply_t *rmp;
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100123 fib_prefix_t prefix;
124 ip46_address_t next_hop;
Dave Barachb7b92992018-10-17 10:38:51 -0400125 clib_memset (&prefix, 0, sizeof (ip46_address_t));
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100126
127 ip_prefix_decode (&mp->prefix, &prefix);
128 ip_address_decode (&mp->next_hop, &next_hop);
Pablo Camarillo42998822017-07-13 09:41:32 +0200129
130 int rv = 0;
131 if (mp->is_del)
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100132 rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
133 prefix.fp_len,
134 ip46_address_is_ip4 (&prefix.fp_addr) ?
135 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200136 ntohl (mp->table_id),
137 ntohl (mp->color));
138 else
139 rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
140 ntohl (mp->table_id),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100141 &prefix.fp_addr,
142 prefix.fp_len,
143 ip46_address_is_ip4 (&prefix.fp_addr) ?
144 SR_STEER_IPV4 : SR_STEER_IPV6,
145 &next_hop,
146 ip46_address_is_ip4 (&next_hop) ?
147 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200148 ntohl (mp->color), mp->co_bits,
149 ntohl (mp->vpn_label));
150
151 REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
152}
153
154static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
155 (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
156{
157 vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
158 int rv = 0;
159
160 ip46_address_t endpoint;
Dave Barachb7b92992018-10-17 10:38:51 -0400161 clib_memset (&endpoint, 0, sizeof (ip46_address_t));
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100162 ip_address_decode (&mp->endpoint, &endpoint);
Pablo Camarillo42998822017-07-13 09:41:32 +0200163
164 rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
Jakub Grajciar00ec4012020-01-31 10:17:29 +0100165 &endpoint,
166 ip46_address_is_ip4 (&endpoint) ?
167 SR_STEER_IPV4 : SR_STEER_IPV6,
Pablo Camarillo42998822017-07-13 09:41:32 +0200168 ntohl (mp->color));
169
170 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
171}
172
Pablo Camarillo42998822017-07-13 09:41:32 +0200173static void
174setup_message_id_table (api_main_t * am)
175{
Filip Tehlar63c0fd32021-06-28 00:52:00 +0000176#define _(id, n, crc) \
177 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + REPLY_MSG_ID_BASE);
Pablo Camarillo42998822017-07-13 09:41:32 +0200178 foreach_vl_msg_name_crc_sr_mpls;
179#undef _
180}
181
182static clib_error_t *
183sr_mpls_api_hookup (vlib_main_t * vm)
184{
Dave Barach39d69112019-11-27 11:42:13 -0500185 api_main_t *am = vlibapi_get_main ();
Pablo Camarillo42998822017-07-13 09:41:32 +0200186
Filip Tehlar63c0fd32021-06-28 00:52:00 +0000187 u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0);
188 REPLY_MSG_ID_BASE =
189 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST);
190 vec_free (name);
191
192#define _(N, n) \
Filip Tehlar36217e32021-07-23 08:51:10 +0000193 vl_msg_api_set_handlers ( \
194 REPLY_MSG_ID_BASE + VL_API_##N, #n, vl_api_##n##_t_handler, \
195 vl_noop_handler, vl_api_##n##_t_endian, vl_api_##n##_t_print, \
196 sizeof (vl_api_##n##_t), 1, vl_api_##n##_t_print_json, \
197 vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson);
Pablo Camarillo42998822017-07-13 09:41:32 +0200198 foreach_vpe_api_msg;
199#undef _
200
201 /*
202 * Manually register the sr policy add msg, so we trace enough bytes
203 * to capture a typical segment list
204 */
Filip Tehlar36217e32021-07-23 08:51:10 +0000205 vl_msg_api_set_handlers (
206 REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD, "sr_mpls_policy_add",
207 vl_api_sr_mpls_policy_add_t_handler, vl_noop_handler,
208 vl_api_sr_mpls_policy_add_t_endian, vl_api_sr_mpls_policy_add_t_print, 256,
209 1, vl_api_sr_mpls_policy_add_t_print_json,
210 vl_api_sr_mpls_policy_mod_t_tojson, vl_api_sr_mpls_policy_mod_t_fromjson);
Pablo Camarillo42998822017-07-13 09:41:32 +0200211
212 /*
213 * Manually register the sr policy mod msg, so we trace enough bytes
214 * to capture a typical segment list
215 */
Filip Tehlar36217e32021-07-23 08:51:10 +0000216 vl_msg_api_set_handlers (
217 REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_MOD, "sr_mpls_policy_mod",
218 vl_api_sr_mpls_policy_mod_t_handler, vl_noop_handler,
219 vl_api_sr_mpls_policy_mod_t_endian, vl_api_sr_mpls_policy_mod_t_print, 256,
220 1, vl_api_sr_mpls_policy_mod_t_print_json,
221 vl_api_sr_mpls_policy_mod_t_tojson, vl_api_sr_mpls_policy_mod_t_fromjson);
Pablo Camarillo42998822017-07-13 09:41:32 +0200222
223 /*
224 * Set up the (msg_name, crc, message-id) table
225 */
226 setup_message_id_table (am);
227
228 return 0;
229}
230
231VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
232
233/*
234 * fd.io coding-style-patch-verification: ON
235 *
236 * Local Variables: eval: (c-set-style "gnu") End:
237 */