blob: a44c30981122cf1ad358e5e51e5507b2a58a2842 [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
Takeru Hayasakac4c205b2022-12-30 16:41:44 +090085 /*
86 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
87 * ip6_address_t *encap_src,
88 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap,
89 * u16 behavior, void *plugin_mem)
90 */
Pablo Camarillofb380952016-12-07 18:34:18 +010091 int rv = 0;
Takeru Hayasakac4c205b2022-12-30 16:41:44 +090092 rv =
93 sr_policy_add (&bsid_addr, segments, NULL, ntohl (mp->sids.weight),
94 mp->is_spray, ntohl (mp->fib_table), mp->is_encap, 0, NULL);
John Lod23d39c2018-09-13 15:08:08 -040095 vec_free (segments);
Pablo Camarillofb380952016-12-07 18:34:18 +010096
97 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
98}
99
100static void
101vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
102{
103 vl_api_sr_policy_mod_reply_t *rmp;
Pablo Camarillofb380952016-12-07 18:34:18 +0100104 ip6_address_t *segments = 0, *seg;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100105 ip6_address_t bsid_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +0100106
107 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200108 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100109 {
Pablo Camarillofb380952016-12-07 18:34:18 +0100110 vec_add2 (segments, seg, 1);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100111 ip6_address_decode (mp->sids.sids[i], seg);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100112 }
113
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100114 ip6_address_decode (mp->bsid_addr, &bsid_addr);
115
Pablo Camarillofb380952016-12-07 18:34:18 +0100116 int rv = 0;
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900117 /*
118 * int
119 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
120 * u8 operation, ip6_address_t *segments,
121 * ip6_address_t *encap_src, u32 sl_index,
122 * u32 weight, u8 is_encap)
123 */
124 rv = sr_policy_mod (&bsid_addr, ntohl (mp->sr_policy_index),
125 ntohl (mp->fib_table), mp->operation, segments, NULL,
126 ntohl (mp->sl_index), ntohl (mp->sids.weight));
127 vec_free (segments);
128
129 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
130}
131
132static void
133vl_api_sr_policy_add_v2_t_handler (vl_api_sr_policy_add_v2_t *mp)
134{
135 vl_api_sr_policy_add_v2_reply_t *rmp;
136 ip6_address_t *segments = 0, *seg;
137 ip6_address_t bsid_addr;
138 ip6_address_t encap_src;
139
140 int i;
141 for (i = 0; i < mp->sids.num_sids; i++)
142 {
143 vec_add2 (segments, seg, 1);
144 ip6_address_decode (mp->sids.sids[i], seg);
145 }
146
147 ip6_address_decode (mp->bsid_addr, &bsid_addr);
148 ip6_address_decode (mp->encap_src, &encap_src);
149
150 if (ip6_address_is_zero (&encap_src))
151 {
152 encap_src = *sr_get_encaps_source ();
153 }
154 /*
155 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
156 * ip6_address_t *encap_src,
157 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap,
158 * u16 behavior, void *plugin_mem)
159 */
160 int rv = 0;
161 rv =
162 sr_policy_add (&bsid_addr, segments, &encap_src, ntohl (mp->sids.weight),
163 mp->type, ntohl (mp->fib_table), mp->is_encap, 0, NULL);
164 vec_free (segments);
165
Vratko Polak3a05db62024-04-11 16:06:29 +0200166 REPLY_MACRO (VL_API_SR_POLICY_ADD_V2_REPLY);
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900167}
168
169static void
170vl_api_sr_policy_mod_v2_t_handler (vl_api_sr_policy_mod_v2_t *mp)
171{
172 vl_api_sr_policy_mod_v2_reply_t *rmp;
173 ip6_address_t *segments = 0, *seg;
174 ip6_address_t bsid_addr;
175 ip6_address_t encap_src;
176
177 int i;
178 for (i = 0; i < mp->sids.num_sids; i++)
179 {
180 vec_add2 (segments, seg, 1);
181 ip6_address_decode (mp->sids.sids[i], seg);
182 }
183
184 ip6_address_decode (mp->bsid_addr, &bsid_addr);
185 ip6_address_decode (mp->encap_src, &encap_src);
186
187 if (ip6_address_is_zero (&encap_src))
188 {
189 encap_src = *sr_get_encaps_source ();
190 }
191
192 int rv = 0;
193 /*
194 * int
195 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
196 * u8 operation, ip6_address_t *segments,
197 * ip6_address_t *encap_src, u32 sl_index,
198 * u32 weight, u8 is_encap)
199 */
200 rv =
201 sr_policy_mod (&bsid_addr, ntohl (mp->sr_policy_index),
202 ntohl (mp->fib_table), mp->operation, segments, &encap_src,
203 ntohl (mp->sl_index), ntohl (mp->sids.weight));
John Lod23d39c2018-09-13 15:08:08 -0400204 vec_free (segments);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100205
Pablo Camarillofb380952016-12-07 18:34:18 +0100206 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100207}
208
Pablo Camarillofb380952016-12-07 18:34:18 +0100209static void
210vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100211{
Pablo Camarillofb380952016-12-07 18:34:18 +0100212 vl_api_sr_policy_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100213 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100214 ip6_address_t bsid_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +0100215/*
216 * int
217 * sr_policy_del (ip6_address_t *bsid, u32 index)
218 */
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100219 ip6_address_decode (mp->bsid_addr, &bsid_addr);
220 rv = sr_policy_del (&bsid_addr, ntohl (mp->sr_policy_index));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100221
Pablo Camarillofb380952016-12-07 18:34:18 +0100222 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100223}
224
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100225static void
226vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp)
227{
228 vl_api_sr_set_encap_source_reply_t *rmp;
229 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100230 ip6_address_t encaps_source;
231
232 ip6_address_decode (mp->encaps_source, &encaps_source);
233 sr_set_source (&encaps_source);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100234
Vratko Polak1096b462019-08-21 18:40:03 +0200235 REPLY_MACRO (VL_API_SR_SET_ENCAP_SOURCE_REPLY);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100236}
237
Ignas Bačiuseeb5fb32019-10-03 17:15:38 +0300238static void
239vl_api_sr_set_encap_hop_limit_t_handler (vl_api_sr_set_encap_hop_limit_t * mp)
240{
241 vl_api_sr_set_encap_hop_limit_reply_t *rmp;
242 int rv = 0;
243
244 if (mp->hop_limit == 0)
245 rv = VNET_API_ERROR_INVALID_VALUE;
246 else
247 sr_set_hop_limit (mp->hop_limit);
248
249 REPLY_MACRO (VL_API_SR_SET_ENCAP_HOP_LIMIT_REPLY);
250}
251
Pablo Camarillofb380952016-12-07 18:34:18 +0100252static void vl_api_sr_steering_add_del_t_handler
253 (vl_api_sr_steering_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100254{
Pablo Camarillofb380952016-12-07 18:34:18 +0100255 vl_api_sr_steering_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100256 int rv = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100257 ip6_address_t bsid_addr;
258 ip46_address_t prefix_addr;
Pablo Camarillofb380952016-12-07 18:34:18 +0100259/*
260 * int
261 * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
262 * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
263 * u8 traffic_type)
264 */
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100265
266 ip6_address_decode (mp->bsid_addr, &bsid_addr);
267 ip_address_decode (&mp->prefix.address, &prefix_addr);
268
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200269 if (mp->traffic_type == SR_STEER_L2)
270 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -0400271
Pablo Camarillofb380952016-12-07 18:34:18 +0100272 rv = sr_steering_policy (mp->is_del,
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100273 &bsid_addr,
Pablo Camarillofb380952016-12-07 18:34:18 +0100274 ntohl (mp->sr_policy_index),
275 ntohl (mp->table_id),
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100276 &prefix_addr,
Ignas Bacius483a3d82020-04-06 16:31:27 +0300277 mp->prefix.len,
Pablo Camarillofb380952016-12-07 18:34:18 +0100278 ntohl (mp->sw_if_index), mp->traffic_type);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100279
Chris Luke879ace32017-09-26 13:15:16 -0400280 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +0100281 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100282}
283
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100284static void send_sr_localsid_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800285 (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context)
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100286{
287 vl_api_sr_localsids_details_t *rmp;
288
289 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400290 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlare2fc0032021-06-22 13:00:40 +0000291 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_DETAILS);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100292 ip6_address_encode (&t->localsid, rmp->addr);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100293 rmp->end_psp = t->end_psp;
Francesco Lombardo38659d82021-07-30 15:54:01 +0200294 rmp->behavior = t->behavior;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100295 rmp->fib_table = htonl (t->fib_table);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200296 rmp->vlan_index = htonl (t->vlan_index);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100297 ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +0100298
299 if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
300 rmp->xconnect_iface_or_vrf_table =
301 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP6));
302 else if (t->behavior == SR_BEHAVIOR_DT4)
303 rmp->xconnect_iface_or_vrf_table =
304 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP4));
305 else
306 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
307
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100308 rmp->context = context;
309
Florin Coras6c4dae22018-01-09 06:39:23 -0800310 vl_api_send_msg (reg, (u8 *) rmp);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100311}
312
313static void vl_api_sr_localsids_dump_t_handler
314 (vl_api_sr_localsids_dump_t * mp)
315{
Florin Coras6c4dae22018-01-09 06:39:23 -0800316 vl_api_registration_t *reg;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100317 ip6_sr_main_t *sm = &sr_main;
318 ip6_sr_localsid_t *t;
319
Florin Coras6c4dae22018-01-09 06:39:23 -0800320 reg = vl_api_client_index_to_registration (mp->client_index);
321 if (!reg)
322 return;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100323
Damjan Marionb2c31b62020-12-13 21:47:40 +0100324 pool_foreach (t, sm->localsids)
325 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800326 send_sr_localsid_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100327 }
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100328}
329
ChinmayaAgarwal9503eb52022-09-27 12:25:22 +0530330static void
331send_sr_localsid_with_packet_stats_details (int local_sid_index,
332 ip6_sr_localsid_t *t,
333 vl_api_registration_t *reg,
334 u32 context)
335{
336 vl_api_sr_localsids_with_packet_stats_details_t *rmp;
337 vlib_counter_t good_traffic, bad_traffic;
338 ip6_sr_main_t *sm = &sr_main;
339
340 rmp = vl_msg_api_alloc (sizeof (*rmp));
341 clib_memset (rmp, 0, sizeof (*rmp));
342 rmp->_vl_msg_id =
343 ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_WITH_PACKET_STATS_DETAILS);
344 ip6_address_encode (&t->localsid, rmp->addr);
345 rmp->end_psp = t->end_psp;
346 rmp->behavior = t->behavior;
347 rmp->fib_table = htonl (t->fib_table);
348 rmp->vlan_index = htonl (t->vlan_index);
349 ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
350
351 if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
352 rmp->xconnect_iface_or_vrf_table =
353 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP6));
354 else if (t->behavior == SR_BEHAVIOR_DT4)
355 rmp->xconnect_iface_or_vrf_table =
356 htonl (fib_table_get_table_id (t->sw_if_index, FIB_PROTOCOL_IP4));
357 else
358 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
359
360 rmp->context = context;
361 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), local_sid_index,
362 &good_traffic);
363 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), local_sid_index,
364 &bad_traffic);
365 rmp->good_traffic_bytes = clib_host_to_net_u64 (good_traffic.bytes);
366 rmp->good_traffic_pkt_count = clib_host_to_net_u64 (good_traffic.packets);
367 rmp->bad_traffic_bytes = clib_host_to_net_u64 (bad_traffic.bytes);
368 rmp->bad_traffic_pkt_count = clib_host_to_net_u64 (bad_traffic.packets);
369 vl_api_send_msg (reg, (u8 *) rmp);
370}
371
372static void
373vl_api_sr_localsids_with_packet_stats_dump_t_handler (
374 vl_api_sr_localsids_with_packet_stats_dump_t *mp)
375{
376 vl_api_registration_t *reg;
377 ip6_sr_main_t *sm = &sr_main;
378 ip6_sr_localsid_t **localsid_list = 0;
379 ip6_sr_localsid_t *t;
380 int i;
381
382 reg = vl_api_client_index_to_registration (mp->client_index);
383 if (!reg)
384 return;
385
386 pool_foreach (t, sm->localsids)
387 {
388 vec_add1 (localsid_list, t);
389 }
390 for (i = 0; i < vec_len (localsid_list); i++)
391 {
392 t = localsid_list[i];
393 send_sr_localsid_with_packet_stats_details (i, t, reg, mp->context);
394 }
395}
396
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200397static void send_sr_policies_details
398 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
399{
400 vl_api_sr_policies_details_t *rmp;
401 ip6_sr_main_t *sm = &sr_main;
402
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100403 u32 *sl_index, slidx = 0;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200404 ip6_sr_sl_t *segment_list = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100405 ip6_address_t *segment;
406 vl_api_srv6_sid_list_t *api_sid_list;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200407
408 rmp = vl_msg_api_alloc (sizeof (*rmp) +
409 vec_len (t->segments_lists) *
410 sizeof (vl_api_srv6_sid_list_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400411 clib_memset (rmp, 0,
412 (sizeof (*rmp) +
413 vec_len (t->segments_lists) *
414 sizeof (vl_api_srv6_sid_list_t)));
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200415
Filip Tehlare2fc0032021-06-22 13:00:40 +0000416 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_DETAILS);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100417 ip6_address_encode (&t->bsid, rmp->bsid);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200418 rmp->is_encap = t->is_encap;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100419 rmp->is_spray = t->type;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200420 rmp->fib_table = htonl (t->fib_table);
421 rmp->num_sid_lists = vec_len (t->segments_lists);
422
423 /* Fill in all the segments lists */
424 vec_foreach (sl_index, t->segments_lists)
425 {
426 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100427
428 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
429
430 api_sid_list->num_sids = vec_len (segment_list->segments);
431 api_sid_list->weight = htonl (segment_list->weight);
Chinmaya Agarwaledc2ea42020-06-23 12:38:23 +0000432 slidx = 0;
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100433 vec_foreach (segment, segment_list->segments)
434 {
435 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
436 }
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200437 }
438
439 rmp->context = context;
440 vl_api_send_msg (reg, (u8 *) rmp);
441}
442
443static void
444vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
445{
446 vl_api_registration_t *reg;
447 ip6_sr_main_t *sm = &sr_main;
448 ip6_sr_policy_t *t;
449
450 reg = vl_api_client_index_to_registration (mp->client_index);
451 if (!reg)
452 return;
453
Damjan Marionb2c31b62020-12-13 21:47:40 +0100454 pool_foreach (t, sm->sr_policies)
455 {
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200456 send_sr_policies_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100457 }
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200458}
459
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900460static void
461send_sr_policies_v2_details (ip6_sr_policy_t *t, vl_api_registration_t *reg,
462 u32 context)
463{
464 vl_api_sr_policies_v2_details_t *rmp;
465 ip6_sr_main_t *sm = &sr_main;
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530466
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900467 u32 *sl_index, slidx = 0;
468 ip6_sr_sl_t *segment_list = 0;
469 ip6_address_t *segment;
470 vl_api_srv6_sid_list_t *api_sid_list;
471
472 rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (t->segments_lists) *
473 sizeof (vl_api_srv6_sid_list_t));
474 clib_memset (rmp, 0,
475 (sizeof (*rmp) + vec_len (t->segments_lists) *
476 sizeof (vl_api_srv6_sid_list_t)));
477
478 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_V2_DETAILS);
479 ip6_address_encode (&t->bsid, rmp->bsid);
480 ip6_address_encode (&t->encap_src, rmp->encap_src);
481 rmp->is_encap = t->is_encap;
482 rmp->type = t->type;
483 rmp->fib_table = htonl (t->fib_table);
484 rmp->num_sid_lists = vec_len (t->segments_lists);
485
486 /* Fill in all the segments lists */
487 vec_foreach (sl_index, t->segments_lists)
488 {
489 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
490
491 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
492
493 api_sid_list->num_sids = vec_len (segment_list->segments);
494 api_sid_list->weight = htonl (segment_list->weight);
495 slidx = 0;
496 vec_foreach (segment, segment_list->segments)
497 {
498 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
499 }
500 }
501
502 rmp->context = context;
503 vl_api_send_msg (reg, (u8 *) rmp);
504}
505
506static void
507vl_api_sr_policies_v2_dump_t_handler (vl_api_sr_policies_v2_dump_t *mp)
508{
509 vl_api_registration_t *reg;
510 ip6_sr_main_t *sm = &sr_main;
511 ip6_sr_policy_t *t;
512
513 reg = vl_api_client_index_to_registration (mp->client_index);
514 if (!reg)
515 return;
516
517 pool_foreach (t, sm->sr_policies)
518 {
519 send_sr_policies_v2_details (t, reg, mp->context);
520 }
521}
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530522
523static void send_sr_policies_details_with_sl_index
524 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
525{
526 vl_api_sr_policies_with_sl_index_details_t *rmp;
527 ip6_sr_main_t *sm = &sr_main;
528
529 u32 *sl_index, slidx = 0;
530 ip6_sr_sl_t *segment_list = 0;
531 ip6_address_t *segment;
532 vl_api_srv6_sid_list_with_sl_index_t *api_sid_list;
533
534 rmp = vl_msg_api_alloc (sizeof (*rmp) +
535 vec_len (t->segments_lists) *
536 sizeof (vl_api_srv6_sid_list_with_sl_index_t));
537 clib_memset (rmp, 0,
538 (sizeof (*rmp) +
539 vec_len (t->segments_lists) *
540 sizeof (vl_api_srv6_sid_list_with_sl_index_t)));
541
Filip Tehlare2fc0032021-06-22 13:00:40 +0000542 rmp->_vl_msg_id =
543 ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_WITH_SL_INDEX_DETAILS);
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530544 ip6_address_encode (&t->bsid, rmp->bsid);
545 rmp->is_encap = t->is_encap;
546 rmp->is_spray = t->type;
547 rmp->fib_table = htonl (t->fib_table);
548 rmp->num_sid_lists = vec_len (t->segments_lists);
549
550 /* Fill in all the segments lists */
551 vec_foreach (sl_index, t->segments_lists)
552 {
553 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
554
555 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
556 api_sid_list->sl_index = htonl (*sl_index);
557 api_sid_list->num_sids = vec_len (segment_list->segments);
558 api_sid_list->weight = htonl (segment_list->weight);
559 slidx = 0;
560 vec_foreach (segment, segment_list->segments)
561 {
562 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
563 }
564 }
565
566 rmp->context = context;
567 vl_api_send_msg (reg, (u8 *) rmp);
568}
569
570static void
571 vl_api_sr_policies_with_sl_index_dump_t_handler
572 (vl_api_sr_policies_with_sl_index_dump_t * mp)
573{
574 vl_api_registration_t *reg;
575 ip6_sr_main_t *sm = &sr_main;
576 ip6_sr_policy_t *t;
577
578 reg = vl_api_client_index_to_registration (mp->client_index);
579 if (!reg)
580 return;
581
Damjan Marionb2c31b62020-12-13 21:47:40 +0100582 pool_foreach (t, sm->sr_policies)
583 {
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530584 send_sr_policies_details_with_sl_index(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100585 }
Chinmaya Agarwal30fa97d2020-07-13 22:34:12 +0530586}
587
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200588static void send_sr_steering_pol_details
589 (ip6_sr_steering_policy_t * t, vl_api_registration_t * reg, u32 context)
590{
591 vl_api_sr_steering_pol_details_t *rmp;
592 ip6_sr_main_t *sm = &sr_main;
593
594 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400595 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlare2fc0032021-06-22 13:00:40 +0000596 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_STEERING_POL_DETAILS);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200597
598 //Get the SR policy BSID
599 ip6_sr_policy_t *p;
600 p = pool_elt_at_index (sm->sr_policies, t->sr_policy);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100601 ip6_address_encode (&p->bsid, rmp->bsid);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200602
603 //Get the steering
604 rmp->traffic_type = t->classify.traffic_type;
605 rmp->fib_table = htonl (t->classify.l3.fib_table);
Jakub Grajciar0938eba2020-03-04 13:08:27 +0100606 ip_address_encode (&t->classify.l3.prefix, IP46_TYPE_ANY,
607 &rmp->prefix.address);
Ignas Bacius483a3d82020-04-06 16:31:27 +0300608 rmp->prefix.len = t->classify.l3.mask_width;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200609
610 rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
611
612 rmp->context = context;
613 vl_api_send_msg (reg, (u8 *) rmp);
614}
615
616static void vl_api_sr_steering_pol_dump_t_handler
617 (vl_api_sr_policies_dump_t * mp)
618{
619 vl_api_registration_t *reg;
620 ip6_sr_main_t *sm = &sr_main;
621 ip6_sr_steering_policy_t *t;
622
623 reg = vl_api_client_index_to_registration (mp->client_index);
624 if (!reg)
625 return;
626
Damjan Marionb2c31b62020-12-13 21:47:40 +0100627 pool_foreach (t, sm->steer_policies)
628 {
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200629 send_sr_steering_pol_details(t, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100630 }
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200631}
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100632
Filip Tehlare2fc0032021-06-22 13:00:40 +0000633#include <vnet/srv6/sr.api.c>
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100634static clib_error_t *
635sr_api_hookup (vlib_main_t * vm)
636{
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100637 /*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100638 * Set up the (msg_name, crc, message-id) table
639 */
Filip Tehlare2fc0032021-06-22 13:00:40 +0000640 REPLY_MSG_ID_BASE = setup_message_id_table ();
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100641
642 return 0;
643}
644
645VLIB_API_INIT_FUNCTION (sr_api_hookup);
646
647/*
648 * fd.io coding-style-patch-verification: ON
649 *
650 * Local Variables:
651 * eval: (c-set-style "gnu")
652 * End:
653 */