blob: 1b95125bc90905103280574a49e131aa3d387320 [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>
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +010027#include <vnet/fib/fib_table.h>
Jakub Grajciar0938eba2020-03-04 13:08:27 +010028#include <vnet/ip/ip_types_api.h>
29
Filip Tehlare2fc0032021-06-22 13:00:40 +000030#include <vnet/format_fns.h>
31#include <vnet/srv6/sr.api_enum.h>
32#include <vnet/srv6/sr.api_types.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010033
Filip Tehlare2fc0032021-06-22 13:00:40 +000034#define REPLY_MSG_ID_BASE sr_main.msg_id_base
Pavel Kotucek0f971d82017-01-03 10:48:54 +010035#include <vlibapi/api_helper_macros.h>
36
Pablo Camarillofb380952016-12-07 18:34:18 +010037static void vl_api_sr_localsid_add_del_t_handler
38 (vl_api_sr_localsid_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010039{
Pablo Camarillofb380952016-12-07 18:34:18 +010040 vl_api_sr_localsid_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010041 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +010042 ip46_address_t prefix;
43 ip6_address_t localsid;
Pablo Camarillofb380952016-12-07 18:34:18 +010044/*
45 * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
46 * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
47 * ip46_address_t *nh_addr, void *ls_plugin_mem)
48 */
Pablo Camarillo3337bd22018-06-19 15:49:02 +020049 if (mp->behavior == SR_BEHAVIOR_X ||
50 mp->behavior == SR_BEHAVIOR_DX6 ||
51 mp->behavior == SR_BEHAVIOR_DX4 || mp->behavior == SR_BEHAVIOR_DX2)
52 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -040053
Jakub Grajciar0938eba2020-03-04 13:08:27 +010054 ip6_address_decode (mp->localsid, &localsid);
55 ip_address_decode (&mp->nh_addr, &prefix);
Chris Luke879ace32017-09-26 13:15:16 -040056
Pablo Camarillofb380952016-12-07 18:34:18 +010057 rv = sr_cli_localsid (mp->is_del,
Jakub Grajciar0938eba2020-03-04 13:08:27 +010058 &localsid, 128,
Pablo Camarillofb380952016-12-07 18:34:18 +010059 mp->end_psp,
60 mp->behavior,
61 ntohl (mp->sw_if_index),
62 ntohl (mp->vlan_index),
Tetsuya Murakamiec9cb962020-03-23 16:10:28 -070063 ntohl (mp->fib_table), &prefix, 0, NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +010064
Chris Luke879ace32017-09-26 13:15:16 -040065 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +010066 REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
67}
68
69static void
70vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp)
71{
72 vl_api_sr_policy_add_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010073 ip6_address_t *segments = 0, *seg;
Jakub Grajciar0938eba2020-03-04 13:08:27 +010074 ip6_address_t bsid_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +010075
Pavel Kotucek0f971d82017-01-03 10:48:54 +010076 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +020077 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010078 {
79 vec_add2 (segments, seg, 1);
Jakub Grajciar0938eba2020-03-04 13:08:27 +010080 ip6_address_decode (mp->sids.sids[i], seg);
Pavel Kotucek0f971d82017-01-03 10:48:54 +010081 }
Pablo Camarillofb380952016-12-07 18:34:18 +010082
Jakub Grajciar0938eba2020-03-04 13:08:27 +010083 ip6_address_decode (mp->bsid_addr, &bsid_addr);
84
Pablo Camarillofb380952016-12-07 18:34:18 +010085/*
86 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
Tetsuya Murakami70d8ef82019-12-04 18:57:46 -080087 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap,
88 * u16 behavior, void *plugin_mem)
Pablo Camarillofb380952016-12-07 18:34:18 +010089 */
90 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +010091 rv = sr_policy_add (&bsid_addr,
Pablo Camarillofb380952016-12-07 18:34:18 +010092 segments,
Pablo Camarillo3337bd22018-06-19 15:49:02 +020093 ntohl (mp->sids.weight),
Jakub Grajciar0938eba2020-03-04 13:08:27 +010094 mp->is_spray, ntohl (mp->fib_table), mp->is_encap, 0,
95 NULL);
John Lod23d39c2018-09-13 15:08:08 -040096 vec_free (segments);
Pablo Camarillofb380952016-12-07 18:34:18 +010097
98 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
99}
100
101static void
102vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
103{
104 vl_api_sr_policy_mod_reply_t *rmp;
Pablo Camarillofb380952016-12-07 18:34:18 +0100105 ip6_address_t *segments = 0, *seg;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100106 ip6_address_t bsid_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +0100107
108 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200109 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100110 {
Pablo Camarillofb380952016-12-07 18:34:18 +0100111 vec_add2 (segments, seg, 1);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100112 ip6_address_decode (mp->sids.sids[i], seg);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100113 }
114
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100115 ip6_address_decode (mp->bsid_addr, &bsid_addr);
116
Pablo Camarillofb380952016-12-07 18:34:18 +0100117 int rv = 0;
118/*
119 * int
120 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
121 * u8 operation, ip6_address_t *segments, u32 sl_index,
122 * u32 weight, u8 is_encap)
123 */
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100124 rv = sr_policy_mod (&bsid_addr,
Pablo Camarillofb380952016-12-07 18:34:18 +0100125 ntohl (mp->sr_policy_index),
126 ntohl (mp->fib_table),
127 mp->operation,
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200128 segments, ntohl (mp->sl_index),
129 ntohl (mp->sids.weight));
John Lod23d39c2018-09-13 15:08:08 -0400130 vec_free (segments);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100131
Pablo Camarillofb380952016-12-07 18:34:18 +0100132 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100133}
134
Pablo Camarillofb380952016-12-07 18:34:18 +0100135static void
136vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100137{
Pablo Camarillofb380952016-12-07 18:34:18 +0100138 vl_api_sr_policy_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100139 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100140 ip6_address_t bsid_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +0100141/*
142 * int
143 * sr_policy_del (ip6_address_t *bsid, u32 index)
144 */
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100145 ip6_address_decode (mp->bsid_addr, &bsid_addr);
146 rv = sr_policy_del (&bsid_addr, ntohl (mp->sr_policy_index));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100147
Pablo Camarillofb380952016-12-07 18:34:18 +0100148 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100149}
150
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100151static void
152vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp)
153{
154 vl_api_sr_set_encap_source_reply_t *rmp;
155 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100156 ip6_address_t encaps_source;
157
158 ip6_address_decode (mp->encaps_source, &encaps_source);
159 sr_set_source (&encaps_source);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100160
Vratko Polak1096b462019-08-21 18:40:03 +0200161 REPLY_MACRO (VL_API_SR_SET_ENCAP_SOURCE_REPLY);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100162}
163
Ignas Bačiuseeb5fb32019-10-03 17:15:38 +0300164static void
165vl_api_sr_set_encap_hop_limit_t_handler (vl_api_sr_set_encap_hop_limit_t * mp)
166{
167 vl_api_sr_set_encap_hop_limit_reply_t *rmp;
168 int rv = 0;
169
170 if (mp->hop_limit == 0)
171 rv = VNET_API_ERROR_INVALID_VALUE;
172 else
173 sr_set_hop_limit (mp->hop_limit);
174
175 REPLY_MACRO (VL_API_SR_SET_ENCAP_HOP_LIMIT_REPLY);
176}
177
Pablo Camarillofb380952016-12-07 18:34:18 +0100178static void vl_api_sr_steering_add_del_t_handler
179 (vl_api_sr_steering_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100180{
Pablo Camarillofb380952016-12-07 18:34:18 +0100181 vl_api_sr_steering_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100182 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100183 ip6_address_t bsid_addr;
184 ip46_address_t prefix_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +0100185/*
186 * int
187 * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
188 * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
189 * u8 traffic_type)
190 */
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100191
192 ip6_address_decode (mp->bsid_addr, &bsid_addr);
193 ip_address_decode (&mp->prefix.address, &prefix_addr);
194
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200195 if (mp->traffic_type == SR_STEER_L2)
196 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -0400197
Pablo Camarillofb380952016-12-07 18:34:18 +0100198 rv = sr_steering_policy (mp->is_del,
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100199 &bsid_addr,
Pablo Camarillofb380952016-12-07 18:34:18 +0100200 ntohl (mp->sr_policy_index),
201 ntohl (mp->table_id),
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100202 &prefix_addr,
Ignas Bacius483a3d82020-04-06 16:31:27 +0300203 mp->prefix.len,
Pablo Camarillofb380952016-12-07 18:34:18 +0100204 ntohl (mp->sw_if_index), mp->traffic_type);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100205
Chris Luke879ace32017-09-26 13:15:16 -0400206 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +0100207 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100208}
209
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100210static void send_sr_localsid_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800211 (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context)
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100212{
213 vl_api_sr_localsids_details_t *rmp;
214
215 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400216 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlare2fc0032021-06-22 13:00:40 +0000217 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_DETAILS);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100218 ip6_address_encode (&t->localsid, rmp->addr);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100219 rmp->end_psp = t->end_psp;
Francesco Lombardo38659d82021-07-30 15:54:01 +0200220 rmp->behavior = t->behavior;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100221 rmp->fib_table = htonl (t->fib_table);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200222 rmp->vlan_index = htonl (t->vlan_index);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100223 ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +0100224
225 if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
226 rmp->xconnect_iface_or_vrf_table =
227 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP6));
228 else if (t->behavior == SR_BEHAVIOR_DT4)
229 rmp->xconnect_iface_or_vrf_table =
230 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP4));
231 else
232 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
233
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100234 rmp->context = context;
235
Florin Coras6c4dae22018-01-09 06:39:23 -0800236 vl_api_send_msg (reg, (u8 *) rmp);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100237}
238
239static void vl_api_sr_localsids_dump_t_handler
240 (vl_api_sr_localsids_dump_t * mp)
241{
Florin Coras6c4dae22018-01-09 06:39:23 -0800242 vl_api_registration_t *reg;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100243 ip6_sr_main_t *sm = &sr_main;
244 ip6_sr_localsid_t *t;
245
Florin Coras6c4dae22018-01-09 06:39:23 -0800246 reg = vl_api_client_index_to_registration (mp->client_index);
247 if (!reg)
248 return;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100249
250 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100251 pool_foreach (t, sm->localsids)
252 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800253 send_sr_localsid_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100254 }
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100255 /* *INDENT-ON* */
256}
257
ChinmayaAgarwal9503eb52022-09-27 12:25:22 +0530258static void
259send_sr_localsid_with_packet_stats_details (int local_sid_index,
260 ip6_sr_localsid_t *t,
261 vl_api_registration_t *reg,
262 u32 context)
263{
264 vl_api_sr_localsids_with_packet_stats_details_t *rmp;
265 vlib_counter_t good_traffic, bad_traffic;
266 ip6_sr_main_t *sm = &sr_main;
267
268 rmp = vl_msg_api_alloc (sizeof (*rmp));
269 clib_memset (rmp, 0, sizeof (*rmp));
270 rmp->_vl_msg_id =
271 ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_WITH_PACKET_STATS_DETAILS);
272 ip6_address_encode (&t->localsid, rmp->addr);
273 rmp->end_psp = t->end_psp;
274 rmp->behavior = t->behavior;
275 rmp->fib_table = htonl (t->fib_table);
276 rmp->vlan_index = htonl (t->vlan_index);
277 ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
278
279 if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
280 rmp->xconnect_iface_or_vrf_table =
281 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP6));
282 else if (t->behavior == SR_BEHAVIOR_DT4)
283 rmp->xconnect_iface_or_vrf_table =
284 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP4));
285 else
286 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
287
288 rmp->context = context;
289 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), local_sid_index,
290 &good_traffic);
291 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), local_sid_index,
292 &bad_traffic);
293 rmp->good_traffic_bytes = clib_host_to_net_u64 (good_traffic.bytes);
294 rmp->good_traffic_pkt_count = clib_host_to_net_u64 (good_traffic.packets);
295 rmp->bad_traffic_bytes = clib_host_to_net_u64 (bad_traffic.bytes);
296 rmp->bad_traffic_pkt_count = clib_host_to_net_u64 (bad_traffic.packets);
297 vl_api_send_msg (reg, (u8 *) rmp);
298}
299
300static void
301vl_api_sr_localsids_with_packet_stats_dump_t_handler (
302 vl_api_sr_localsids_with_packet_stats_dump_t *mp)
303{
304 vl_api_registration_t *reg;
305 ip6_sr_main_t *sm = &sr_main;
306 ip6_sr_localsid_t **localsid_list = 0;
307 ip6_sr_localsid_t *t;
308 int i;
309
310 reg = vl_api_client_index_to_registration (mp->client_index);
311 if (!reg)
312 return;
313
314 pool_foreach (t, sm->localsids)
315 {
316 vec_add1 (localsid_list, t);
317 }
318 for (i = 0; i < vec_len (localsid_list); i++)
319 {
320 t = localsid_list[i];
321 send_sr_localsid_with_packet_stats_details (i, t, reg, mp->context);
322 }
323}
324
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200325static void send_sr_policies_details
326 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
327{
328 vl_api_sr_policies_details_t *rmp;
329 ip6_sr_main_t *sm = &sr_main;
330
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100331 u32 *sl_index, slidx = 0;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200332 ip6_sr_sl_t *segment_list = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100333 ip6_address_t *segment;
334 vl_api_srv6_sid_list_t *api_sid_list;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200335
336 rmp = vl_msg_api_alloc (sizeof (*rmp) +
337 vec_len (t->segments_lists) *
338 sizeof (vl_api_srv6_sid_list_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400339 clib_memset (rmp, 0,
340 (sizeof (*rmp) +
341 vec_len (t->segments_lists) *
342 sizeof (vl_api_srv6_sid_list_t)));
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200343
Filip Tehlare2fc0032021-06-22 13:00:40 +0000344 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_DETAILS);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100345 ip6_address_encode (&t->bsid, rmp->bsid);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200346 rmp->is_encap = t->is_encap;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100347 rmp->is_spray = t->type;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200348 rmp->fib_table = htonl (t->fib_table);
349 rmp->num_sid_lists = vec_len (t->segments_lists);
350
351 /* Fill in all the segments lists */
352 vec_foreach (sl_index, t->segments_lists)
353 {
354 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100355
356 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
357
358 api_sid_list->num_sids = vec_len (segment_list->segments);
359 api_sid_list->weight = htonl (segment_list->weight);
Chinmaya Agarwaledc2ea42020-06-23 12:38:23 +0000360 slidx = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100361 vec_foreach (segment, segment_list->segments)
362 {
363 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
364 }
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200365 }
366
367 rmp->context = context;
368 vl_api_send_msg (reg, (u8 *) rmp);
369}
370
371static void
372vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
373{
374 vl_api_registration_t *reg;
375 ip6_sr_main_t *sm = &sr_main;
376 ip6_sr_policy_t *t;
377
378 reg = vl_api_client_index_to_registration (mp->client_index);
379 if (!reg)
380 return;
381
382 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100383 pool_foreach (t, sm->sr_policies)
384 {
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200385 send_sr_policies_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100386 }
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200387 /* *INDENT-ON* */
388}
389
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530390
391
392static void send_sr_policies_details_with_sl_index
393 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
394{
395 vl_api_sr_policies_with_sl_index_details_t *rmp;
396 ip6_sr_main_t *sm = &sr_main;
397
398 u32 *sl_index, slidx = 0;
399 ip6_sr_sl_t *segment_list = 0;
400 ip6_address_t *segment;
401 vl_api_srv6_sid_list_with_sl_index_t *api_sid_list;
402
403 rmp = vl_msg_api_alloc (sizeof (*rmp) +
404 vec_len (t->segments_lists) *
405 sizeof (vl_api_srv6_sid_list_with_sl_index_t));
406 clib_memset (rmp, 0,
407 (sizeof (*rmp) +
408 vec_len (t->segments_lists) *
409 sizeof (vl_api_srv6_sid_list_with_sl_index_t)));
410
Filip Tehlare2fc0032021-06-22 13:00:40 +0000411 rmp->_vl_msg_id =
412 ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_WITH_SL_INDEX_DETAILS);
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530413 ip6_address_encode (&t->bsid, rmp->bsid);
414 rmp->is_encap = t->is_encap;
415 rmp->is_spray = t->type;
416 rmp->fib_table = htonl (t->fib_table);
417 rmp->num_sid_lists = vec_len (t->segments_lists);
418
419 /* Fill in all the segments lists */
420 vec_foreach (sl_index, t->segments_lists)
421 {
422 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
423
424 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
425 api_sid_list->sl_index = htonl (*sl_index);
426 api_sid_list->num_sids = vec_len (segment_list->segments);
427 api_sid_list->weight = htonl (segment_list->weight);
428 slidx = 0;
429 vec_foreach (segment, segment_list->segments)
430 {
431 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
432 }
433 }
434
435 rmp->context = context;
436 vl_api_send_msg (reg, (u8 *) rmp);
437}
438
439static void
440 vl_api_sr_policies_with_sl_index_dump_t_handler
441 (vl_api_sr_policies_with_sl_index_dump_t * mp)
442{
443 vl_api_registration_t *reg;
444 ip6_sr_main_t *sm = &sr_main;
445 ip6_sr_policy_t *t;
446
447 reg = vl_api_client_index_to_registration (mp->client_index);
448 if (!reg)
449 return;
450
451 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100452 pool_foreach (t, sm->sr_policies)
453 {
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530454 send_sr_policies_details_with_sl_index(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100455 }
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530456 /* *INDENT-ON* */
457}
458
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200459static void send_sr_steering_pol_details
460 (ip6_sr_steering_policy_t * t, vl_api_registration_t * reg, u32 context)
461{
462 vl_api_sr_steering_pol_details_t *rmp;
463 ip6_sr_main_t *sm = &sr_main;
464
465 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400466 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlare2fc0032021-06-22 13:00:40 +0000467 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_STEERING_POL_DETAILS);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200468
469 //Get the SR policy BSID
470 ip6_sr_policy_t *p;
471 p = pool_elt_at_index (sm->sr_policies, t->sr_policy);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100472 ip6_address_encode (&p->bsid, rmp->bsid);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200473
474 //Get the steering
475 rmp->traffic_type = t->classify.traffic_type;
476 rmp->fib_table = htonl (t->classify.l3.fib_table);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100477 ip_address_encode (&t->classify.l3.prefix, IP46_TYPE_ANY,
478 &rmp->prefix.address);
Ignas Bacius483a3d82020-04-06 16:31:27 +0300479 rmp->prefix.len = t->classify.l3.mask_width;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200480
481 rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
482
483 rmp->context = context;
484 vl_api_send_msg (reg, (u8 *) rmp);
485}
486
487static void vl_api_sr_steering_pol_dump_t_handler
488 (vl_api_sr_policies_dump_t * mp)
489{
490 vl_api_registration_t *reg;
491 ip6_sr_main_t *sm = &sr_main;
492 ip6_sr_steering_policy_t *t;
493
494 reg = vl_api_client_index_to_registration (mp->client_index);
495 if (!reg)
496 return;
497
498 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100499 pool_foreach (t, sm->steer_policies)
500 {
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200501 send_sr_steering_pol_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100502 }
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200503 /* *INDENT-ON* */
504}
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100505
Filip Tehlare2fc0032021-06-22 13:00:40 +0000506#include <vnet/srv6/sr.api.c>
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100507static clib_error_t *
508sr_api_hookup (vlib_main_t * vm)
509{
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100510 /*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100511 * Set up the (msg_name, crc, message-id) table
512 */
Filip Tehlare2fc0032021-06-22 13:00:40 +0000513 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100514
515 return 0;
516}
517
518VLIB_API_INIT_FUNCTION (sr_api_hookup);
519
520/*
521 * fd.io coding-style-patch-verification: ON
522 *
523 * Local Variables:
524 * eval: (c-set-style "gnu")
525 * End:
526 */