blob: ffd0c4d04003a339e33866e91438e865b5527b77 [file] [log] [blame]
Pavel Kotucek0f971d82017-01-03 10:48:54 +01001/*
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>
Pablo Camarillo5d73eec2017-04-24 17:51:56 +020021#include <vnet/srv6/sr.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010022#include <vlibmemory/api.h>
23
24#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26#include <vnet/feature/feature.h>
27
28#include <vnet/vnet_msg_enum.h>
29
30#define vl_typedefs /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_typedefs
33
34#define vl_endianfun /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40#define vl_printfun
41#include <vnet/vnet_all_api_h.h>
42#undef vl_printfun
43
44#include <vlibapi/api_helper_macros.h>
45
46#define foreach_vpe_api_msg \
Pablo Camarillofb380952016-12-07 18:34:18 +010047_(SR_LOCALSID_ADD_DEL, sr_localsid_add_del) \
Pablo Camarillo3337bd22018-06-19 15:49:02 +020048_(SR_POLICY_ADD, sr_policy_add) \
49_(SR_POLICY_MOD, sr_policy_mod) \
Pablo Camarillofb380952016-12-07 18:34:18 +010050_(SR_POLICY_DEL, sr_policy_del) \
Pablo Camarillo1a5e3012017-11-16 16:02:50 +010051_(SR_STEERING_ADD_DEL, sr_steering_add_del) \
52_(SR_SET_ENCAP_SOURCE, sr_set_encap_source) \
Ignas Bačiuseeb5fb32019-10-03 17:15:38 +030053_(SR_SET_ENCAP_HOP_LIMIT, sr_set_encap_hop_limit) \
Pablo Camarillo3337bd22018-06-19 15:49:02 +020054_(SR_LOCALSIDS_DUMP, sr_localsids_dump) \
55_(SR_POLICIES_DUMP, sr_policies_dump) \
56_(SR_STEERING_POL_DUMP, sr_steering_pol_dump)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010057
Pablo Camarillofb380952016-12-07 18:34:18 +010058static void vl_api_sr_localsid_add_del_t_handler
59 (vl_api_sr_localsid_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010060{
Pablo Camarillofb380952016-12-07 18:34:18 +010061 vl_api_sr_localsid_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010062 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +010063/*
64 * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
65 * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
66 * ip46_address_t *nh_addr, void *ls_plugin_mem)
67 */
Pablo Camarillo3337bd22018-06-19 15:49:02 +020068 if (mp->behavior == SR_BEHAVIOR_X ||
69 mp->behavior == SR_BEHAVIOR_DX6 ||
70 mp->behavior == SR_BEHAVIOR_DX4 || mp->behavior == SR_BEHAVIOR_DX2)
71 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -040072
Pablo Camarillo3337bd22018-06-19 15:49:02 +020073 ip46_address_t prefix;
74
Dave Barachb7b92992018-10-17 10:38:51 -040075 clib_memset (&prefix, 0, sizeof (ip46_address_t));
Pablo Camarillo3337bd22018-06-19 15:49:02 +020076 if ((mp->nh_addr4[0] | mp->nh_addr4[1] | mp->
77 nh_addr4[2] | mp->nh_addr4[3]) != 0)
78 memcpy (&prefix.ip4, mp->nh_addr4, sizeof (prefix.ip4));
79 else
80 memcpy (&prefix.ip6, mp->nh_addr6, sizeof (prefix.ip6));
Chris Luke879ace32017-09-26 13:15:16 -040081
Pablo Camarillofb380952016-12-07 18:34:18 +010082 rv = sr_cli_localsid (mp->is_del,
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -080083 (ip6_address_t *) & mp->localsid, 0,
Pablo Camarillofb380952016-12-07 18:34:18 +010084 mp->end_psp,
85 mp->behavior,
86 ntohl (mp->sw_if_index),
87 ntohl (mp->vlan_index),
Pablo Camarillo3337bd22018-06-19 15:49:02 +020088 ntohl (mp->fib_table), &prefix, NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +010089
Chris Luke879ace32017-09-26 13:15:16 -040090 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +010091 REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
92}
93
94static void
95vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp)
96{
97 vl_api_sr_policy_add_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010098 ip6_address_t *segments = 0, *seg;
Pablo Camarillo3337bd22018-06-19 15:49:02 +020099 ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
Pablo Camarillofb380952016-12-07 18:34:18 +0100100
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100101 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200102 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100103 {
104 vec_add2 (segments, seg, 1);
105 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
106 this_address++;
107 }
Pablo Camarillofb380952016-12-07 18:34:18 +0100108
109/*
110 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
Tetsuya Murakami70d8ef82019-12-04 18:57:46 -0800111 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap,
112 * u16 behavior, void *plugin_mem)
Pablo Camarillofb380952016-12-07 18:34:18 +0100113 */
114 int rv = 0;
115 rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr,
116 segments,
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200117 ntohl (mp->sids.weight),
Tetsuya Murakami70d8ef82019-12-04 18:57:46 -0800118 mp->type, ntohl (mp->fib_table), mp->is_encap, 0, NULL);
John Lod23d39c2018-09-13 15:08:08 -0400119 vec_free (segments);
Pablo Camarillofb380952016-12-07 18:34:18 +0100120
121 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
122}
123
124static void
125vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
126{
127 vl_api_sr_policy_mod_reply_t *rmp;
128
129 ip6_address_t *segments = 0, *seg;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200130 ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
Pablo Camarillofb380952016-12-07 18:34:18 +0100131
132 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200133 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100134 {
Pablo Camarillofb380952016-12-07 18:34:18 +0100135 vec_add2 (segments, seg, 1);
136 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100137 this_address++;
138 }
139
Pablo Camarillofb380952016-12-07 18:34:18 +0100140 int rv = 0;
141/*
142 * int
143 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
144 * u8 operation, ip6_address_t *segments, u32 sl_index,
145 * u32 weight, u8 is_encap)
146 */
147 rv = sr_policy_mod ((ip6_address_t *) & mp->bsid_addr,
148 ntohl (mp->sr_policy_index),
149 ntohl (mp->fib_table),
150 mp->operation,
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200151 segments, ntohl (mp->sl_index),
152 ntohl (mp->sids.weight));
John Lod23d39c2018-09-13 15:08:08 -0400153 vec_free (segments);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100154
Pablo Camarillofb380952016-12-07 18:34:18 +0100155 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100156}
157
Pablo Camarillofb380952016-12-07 18:34:18 +0100158static void
159vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100160{
Pablo Camarillofb380952016-12-07 18:34:18 +0100161 vl_api_sr_policy_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100162 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100163/*
164 * int
165 * sr_policy_del (ip6_address_t *bsid, u32 index)
166 */
167 rv = sr_policy_del ((ip6_address_t *) & mp->bsid_addr,
168 ntohl (mp->sr_policy_index));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100169
Pablo Camarillofb380952016-12-07 18:34:18 +0100170 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100171}
172
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100173static void
174vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp)
175{
176 vl_api_sr_set_encap_source_reply_t *rmp;
177 int rv = 0;
178 sr_set_source ((ip6_address_t *) & mp->encaps_source);
179
Vratko Polak1096b462019-08-21 18:40:03 +0200180 REPLY_MACRO (VL_API_SR_SET_ENCAP_SOURCE_REPLY);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100181}
182
Ignas Bačiuseeb5fb32019-10-03 17:15:38 +0300183static void
184vl_api_sr_set_encap_hop_limit_t_handler (vl_api_sr_set_encap_hop_limit_t * mp)
185{
186 vl_api_sr_set_encap_hop_limit_reply_t *rmp;
187 int rv = 0;
188
189 if (mp->hop_limit == 0)
190 rv = VNET_API_ERROR_INVALID_VALUE;
191 else
192 sr_set_hop_limit (mp->hop_limit);
193
194 REPLY_MACRO (VL_API_SR_SET_ENCAP_HOP_LIMIT_REPLY);
195}
196
Pablo Camarillofb380952016-12-07 18:34:18 +0100197static void vl_api_sr_steering_add_del_t_handler
198 (vl_api_sr_steering_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100199{
Pablo Camarillofb380952016-12-07 18:34:18 +0100200 vl_api_sr_steering_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100201 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100202/*
203 * int
204 * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
205 * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
206 * u8 traffic_type)
207 */
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200208 if (mp->traffic_type == SR_STEER_L2)
209 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -0400210
Pablo Camarillofb380952016-12-07 18:34:18 +0100211 rv = sr_steering_policy (mp->is_del,
212 (ip6_address_t *) & mp->bsid_addr,
213 ntohl (mp->sr_policy_index),
214 ntohl (mp->table_id),
215 (ip46_address_t *) & mp->prefix_addr,
216 ntohl (mp->mask_width),
217 ntohl (mp->sw_if_index), mp->traffic_type);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100218
Chris Luke879ace32017-09-26 13:15:16 -0400219 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +0100220 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100221}
222
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100223static void send_sr_localsid_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800224 (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context)
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100225{
226 vl_api_sr_localsids_details_t *rmp;
227
228 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400229 clib_memset (rmp, 0, sizeof (*rmp));
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100230 rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200231 clib_memcpy (rmp->addr.addr, &t->localsid, sizeof (ip6_address_t));
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100232 rmp->end_psp = t->end_psp;
233 rmp->behavior = htons (t->behavior);
234 rmp->fib_table = htonl (t->fib_table);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200235 rmp->vlan_index = htonl (t->vlan_index);
236 if (ip46_address_is_ip4 (&t->next_hop))
237 clib_memcpy (rmp->xconnect_nh_addr4, &t->next_hop.ip4,
238 sizeof (ip4_address_t));
239 else
240 clib_memcpy (rmp->xconnect_nh_addr6, &t->next_hop.ip6,
241 sizeof (ip6_address_t));
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100242 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
243 rmp->context = context;
244
Florin Coras6c4dae22018-01-09 06:39:23 -0800245 vl_api_send_msg (reg, (u8 *) rmp);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100246}
247
248static void vl_api_sr_localsids_dump_t_handler
249 (vl_api_sr_localsids_dump_t * mp)
250{
Florin Coras6c4dae22018-01-09 06:39:23 -0800251 vl_api_registration_t *reg;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100252 ip6_sr_main_t *sm = &sr_main;
253 ip6_sr_localsid_t *t;
254
Florin Coras6c4dae22018-01-09 06:39:23 -0800255 reg = vl_api_client_index_to_registration (mp->client_index);
256 if (!reg)
257 return;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100258
259 /* *INDENT-OFF* */
260 pool_foreach (t, sm->localsids,
261 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800262 send_sr_localsid_details(t, reg, mp->context);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100263 }));
264 /* *INDENT-ON* */
265}
266
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200267static void send_sr_policies_details
268 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
269{
270 vl_api_sr_policies_details_t *rmp;
271 ip6_sr_main_t *sm = &sr_main;
272
273 u32 *sl_index;
274 ip6_sr_sl_t *segment_list = 0;
275 vl_api_srv6_sid_list_t *write_sid_list;
276
277 rmp = vl_msg_api_alloc (sizeof (*rmp) +
278 vec_len (t->segments_lists) *
279 sizeof (vl_api_srv6_sid_list_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400280 clib_memset (rmp, 0,
281 (sizeof (*rmp) +
282 vec_len (t->segments_lists) *
283 sizeof (vl_api_srv6_sid_list_t)));
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200284
285 rmp->_vl_msg_id = ntohs (VL_API_SR_POLICIES_DETAILS);
286 clib_memcpy (rmp->bsid.addr, &t->bsid, sizeof (ip6_address_t));
287 rmp->is_encap = t->is_encap;
288 rmp->type = t->type;
289 rmp->fib_table = htonl (t->fib_table);
290 rmp->num_sid_lists = vec_len (t->segments_lists);
291
292 /* Fill in all the segments lists */
293 vec_foreach (sl_index, t->segments_lists)
294 {
295 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
296 write_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
297 write_sid_list->num_sids = vec_len (segment_list->segments);
298 write_sid_list->weight = htonl (segment_list->weight);
299 clib_memcpy (write_sid_list->sids, segment_list->segments,
300 vec_len (segment_list->segments) * sizeof (ip6_address_t));
301 }
302
303 rmp->context = context;
304 vl_api_send_msg (reg, (u8 *) rmp);
305}
306
307static void
308vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
309{
310 vl_api_registration_t *reg;
311 ip6_sr_main_t *sm = &sr_main;
312 ip6_sr_policy_t *t;
313
314 reg = vl_api_client_index_to_registration (mp->client_index);
315 if (!reg)
316 return;
317
318 /* *INDENT-OFF* */
319 pool_foreach (t, sm->sr_policies,
320 ({
321 send_sr_policies_details(t, reg, mp->context);
322 }));
323 /* *INDENT-ON* */
324}
325
326static void send_sr_steering_pol_details
327 (ip6_sr_steering_policy_t * t, vl_api_registration_t * reg, u32 context)
328{
329 vl_api_sr_steering_pol_details_t *rmp;
330 ip6_sr_main_t *sm = &sr_main;
331
332 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400333 clib_memset (rmp, 0, sizeof (*rmp));
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200334 rmp->_vl_msg_id = ntohs (VL_API_SR_STEERING_POL_DETAILS);
335
336 //Get the SR policy BSID
337 ip6_sr_policy_t *p;
338 p = pool_elt_at_index (sm->sr_policies, t->sr_policy);
339 clib_memcpy (rmp->bsid.addr, &p->bsid, sizeof (ip6_address_t));
340
341 //Get the steering
342 rmp->traffic_type = t->classify.traffic_type;
343 rmp->fib_table = htonl (t->classify.l3.fib_table);
344 rmp->mask_width = htonl (t->classify.l3.mask_width);
345 if (ip46_address_is_ip4 (&t->classify.l3.prefix))
346 clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip4,
347 sizeof (ip4_address_t));
348 else
349 clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip6,
350 sizeof (ip6_address_t));
351
352 rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
353
354 rmp->context = context;
355 vl_api_send_msg (reg, (u8 *) rmp);
356}
357
358static void vl_api_sr_steering_pol_dump_t_handler
359 (vl_api_sr_policies_dump_t * mp)
360{
361 vl_api_registration_t *reg;
362 ip6_sr_main_t *sm = &sr_main;
363 ip6_sr_steering_policy_t *t;
364
365 reg = vl_api_client_index_to_registration (mp->client_index);
366 if (!reg)
367 return;
368
369 /* *INDENT-OFF* */
370 pool_foreach (t, sm->steer_policies,
371 ({
372 send_sr_steering_pol_details(t, reg, mp->context);
373 }));
374 /* *INDENT-ON* */
375}
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100376
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100377/*
378 * sr_api_hookup
379 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500380 * vlib has already mapped shared memory and
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100381 * added the client registration handlers.
382 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
383 */
384#define vl_msg_name_crc_list
385#include <vnet/vnet_all_api_h.h>
386#undef vl_msg_name_crc_list
387
388static void
389setup_message_id_table (api_main_t * am)
390{
391#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
392 foreach_vl_msg_name_crc_sr;
393#undef _
394}
395
396static clib_error_t *
397sr_api_hookup (vlib_main_t * vm)
398{
Dave Barach39d69112019-11-27 11:42:13 -0500399 api_main_t *am = vlibapi_get_main ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100400
401#define _(N,n) \
402 vl_msg_api_set_handlers(VL_API_##N, #n, \
403 vl_api_##n##_t_handler, \
404 vl_noop_handler, \
405 vl_api_##n##_t_endian, \
406 vl_api_##n##_t_print, \
407 sizeof(vl_api_##n##_t), 1);
408 foreach_vpe_api_msg;
409#undef _
410
411 /*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100412 * Set up the (msg_name, crc, message-id) table
413 */
414 setup_message_id_table (am);
415
416 return 0;
417}
418
419VLIB_API_INIT_FUNCTION (sr_api_hookup);
420
421/*
422 * fd.io coding-style-patch-verification: ON
423 *
424 * Local Variables:
425 * eval: (c-set-style "gnu")
426 * End:
427 */