blob: 17d4821ec1d2040d8716ec82f8fa72712b35c5f8 [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) \
Pablo Camarillo3337bd22018-06-19 15:49:02 +020053_(SR_LOCALSIDS_DUMP, sr_localsids_dump) \
54_(SR_POLICIES_DUMP, sr_policies_dump) \
55_(SR_STEERING_POL_DUMP, sr_steering_pol_dump)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010056
Pablo Camarillofb380952016-12-07 18:34:18 +010057static void vl_api_sr_localsid_add_del_t_handler
58 (vl_api_sr_localsid_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010059{
Pablo Camarillofb380952016-12-07 18:34:18 +010060 vl_api_sr_localsid_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010061 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +010062/*
63 * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
64 * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
65 * ip46_address_t *nh_addr, void *ls_plugin_mem)
66 */
Pablo Camarillo3337bd22018-06-19 15:49:02 +020067 if (mp->behavior == SR_BEHAVIOR_X ||
68 mp->behavior == SR_BEHAVIOR_DX6 ||
69 mp->behavior == SR_BEHAVIOR_DX4 || mp->behavior == SR_BEHAVIOR_DX2)
70 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -040071
Pablo Camarillo3337bd22018-06-19 15:49:02 +020072 ip46_address_t prefix;
73
74 memset (&prefix, 0, sizeof (ip46_address_t));
75 if ((mp->nh_addr4[0] | mp->nh_addr4[1] | mp->
76 nh_addr4[2] | mp->nh_addr4[3]) != 0)
77 memcpy (&prefix.ip4, mp->nh_addr4, sizeof (prefix.ip4));
78 else
79 memcpy (&prefix.ip6, mp->nh_addr6, sizeof (prefix.ip6));
Chris Luke879ace32017-09-26 13:15:16 -040080
Pablo Camarillofb380952016-12-07 18:34:18 +010081 rv = sr_cli_localsid (mp->is_del,
Pablo Camarillo3337bd22018-06-19 15:49:02 +020082 (ip6_address_t *) & mp->localsid,
Pablo Camarillofb380952016-12-07 18:34:18 +010083 mp->end_psp,
84 mp->behavior,
85 ntohl (mp->sw_if_index),
86 ntohl (mp->vlan_index),
Pablo Camarillo3337bd22018-06-19 15:49:02 +020087 ntohl (mp->fib_table), &prefix, NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +010088
Chris Luke879ace32017-09-26 13:15:16 -040089 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +010090 REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
91}
92
93static void
94vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp)
95{
96 vl_api_sr_policy_add_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010097 ip6_address_t *segments = 0, *seg;
Pablo Camarillo3337bd22018-06-19 15:49:02 +020098 ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
Pablo Camarillofb380952016-12-07 18:34:18 +010099
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100100 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200101 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100102 {
103 vec_add2 (segments, seg, 1);
104 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
105 this_address++;
106 }
Pablo Camarillofb380952016-12-07 18:34:18 +0100107
108/*
109 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
110 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap)
111 */
112 int rv = 0;
113 rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr,
114 segments,
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200115 ntohl (mp->sids.weight),
Pablo Camarillofb380952016-12-07 18:34:18 +0100116 mp->type, ntohl (mp->fib_table), mp->is_encap);
117
118 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
119}
120
121static void
122vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
123{
124 vl_api_sr_policy_mod_reply_t *rmp;
125
126 ip6_address_t *segments = 0, *seg;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200127 ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
Pablo Camarillofb380952016-12-07 18:34:18 +0100128
129 int i;
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200130 for (i = 0; i < mp->sids.num_sids; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100131 {
Pablo Camarillofb380952016-12-07 18:34:18 +0100132 vec_add2 (segments, seg, 1);
133 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100134 this_address++;
135 }
136
Pablo Camarillofb380952016-12-07 18:34:18 +0100137 int rv = 0;
138/*
139 * int
140 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
141 * u8 operation, ip6_address_t *segments, u32 sl_index,
142 * u32 weight, u8 is_encap)
143 */
144 rv = sr_policy_mod ((ip6_address_t *) & mp->bsid_addr,
145 ntohl (mp->sr_policy_index),
146 ntohl (mp->fib_table),
147 mp->operation,
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200148 segments, ntohl (mp->sl_index),
149 ntohl (mp->sids.weight));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100150
Pablo Camarillofb380952016-12-07 18:34:18 +0100151 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100152}
153
Pablo Camarillofb380952016-12-07 18:34:18 +0100154static void
155vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100156{
Pablo Camarillofb380952016-12-07 18:34:18 +0100157 vl_api_sr_policy_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100158 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100159/*
160 * int
161 * sr_policy_del (ip6_address_t *bsid, u32 index)
162 */
163 rv = sr_policy_del ((ip6_address_t *) & mp->bsid_addr,
164 ntohl (mp->sr_policy_index));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100165
Pablo Camarillofb380952016-12-07 18:34:18 +0100166 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100167}
168
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100169static void
170vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp)
171{
172 vl_api_sr_set_encap_source_reply_t *rmp;
173 int rv = 0;
174 sr_set_source ((ip6_address_t *) & mp->encaps_source);
175
176 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
177}
178
Pablo Camarillofb380952016-12-07 18:34:18 +0100179static void vl_api_sr_steering_add_del_t_handler
180 (vl_api_sr_steering_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100181{
Pablo Camarillofb380952016-12-07 18:34:18 +0100182 vl_api_sr_steering_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100183 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100184/*
185 * int
186 * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
187 * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
188 * u8 traffic_type)
189 */
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200190 if (mp->traffic_type == SR_STEER_L2)
191 VALIDATE_SW_IF_INDEX (mp);
Chris Luke879ace32017-09-26 13:15:16 -0400192
Pablo Camarillofb380952016-12-07 18:34:18 +0100193 rv = sr_steering_policy (mp->is_del,
194 (ip6_address_t *) & mp->bsid_addr,
195 ntohl (mp->sr_policy_index),
196 ntohl (mp->table_id),
197 (ip46_address_t *) & mp->prefix_addr,
198 ntohl (mp->mask_width),
199 ntohl (mp->sw_if_index), mp->traffic_type);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100200
Chris Luke879ace32017-09-26 13:15:16 -0400201 BAD_SW_IF_INDEX_LABEL;
Pablo Camarillofb380952016-12-07 18:34:18 +0100202 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100203}
204
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100205static void send_sr_localsid_details
Florin Coras6c4dae22018-01-09 06:39:23 -0800206 (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context)
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100207{
208 vl_api_sr_localsids_details_t *rmp;
209
210 rmp = vl_msg_api_alloc (sizeof (*rmp));
211 memset (rmp, 0, sizeof (*rmp));
212 rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200213 clib_memcpy (rmp->addr.addr, &t->localsid, sizeof (ip6_address_t));
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100214 rmp->end_psp = t->end_psp;
215 rmp->behavior = htons (t->behavior);
216 rmp->fib_table = htonl (t->fib_table);
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200217 rmp->vlan_index = htonl (t->vlan_index);
218 if (ip46_address_is_ip4 (&t->next_hop))
219 clib_memcpy (rmp->xconnect_nh_addr4, &t->next_hop.ip4,
220 sizeof (ip4_address_t));
221 else
222 clib_memcpy (rmp->xconnect_nh_addr6, &t->next_hop.ip6,
223 sizeof (ip6_address_t));
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100224 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
225 rmp->context = context;
226
Florin Coras6c4dae22018-01-09 06:39:23 -0800227 vl_api_send_msg (reg, (u8 *) rmp);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100228}
229
230static void vl_api_sr_localsids_dump_t_handler
231 (vl_api_sr_localsids_dump_t * mp)
232{
Florin Coras6c4dae22018-01-09 06:39:23 -0800233 vl_api_registration_t *reg;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100234 ip6_sr_main_t *sm = &sr_main;
235 ip6_sr_localsid_t *t;
236
Florin Coras6c4dae22018-01-09 06:39:23 -0800237 reg = vl_api_client_index_to_registration (mp->client_index);
238 if (!reg)
239 return;
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100240
241 /* *INDENT-OFF* */
242 pool_foreach (t, sm->localsids,
243 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800244 send_sr_localsid_details(t, reg, mp->context);
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100245 }));
246 /* *INDENT-ON* */
247}
248
Pablo Camarillo3337bd22018-06-19 15:49:02 +0200249static void send_sr_policies_details
250 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
251{
252 vl_api_sr_policies_details_t *rmp;
253 ip6_sr_main_t *sm = &sr_main;
254
255 u32 *sl_index;
256 ip6_sr_sl_t *segment_list = 0;
257 vl_api_srv6_sid_list_t *write_sid_list;
258
259 rmp = vl_msg_api_alloc (sizeof (*rmp) +
260 vec_len (t->segments_lists) *
261 sizeof (vl_api_srv6_sid_list_t));
262 memset (rmp, 0,
263 (sizeof (*rmp) +
264 vec_len (t->segments_lists) * sizeof (vl_api_srv6_sid_list_t)));
265
266 rmp->_vl_msg_id = ntohs (VL_API_SR_POLICIES_DETAILS);
267 clib_memcpy (rmp->bsid.addr, &t->bsid, sizeof (ip6_address_t));
268 rmp->is_encap = t->is_encap;
269 rmp->type = t->type;
270 rmp->fib_table = htonl (t->fib_table);
271 rmp->num_sid_lists = vec_len (t->segments_lists);
272
273 /* Fill in all the segments lists */
274 vec_foreach (sl_index, t->segments_lists)
275 {
276 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
277 write_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
278 write_sid_list->num_sids = vec_len (segment_list->segments);
279 write_sid_list->weight = htonl (segment_list->weight);
280 clib_memcpy (write_sid_list->sids, segment_list->segments,
281 vec_len (segment_list->segments) * sizeof (ip6_address_t));
282 }
283
284 rmp->context = context;
285 vl_api_send_msg (reg, (u8 *) rmp);
286}
287
288static void
289vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
290{
291 vl_api_registration_t *reg;
292 ip6_sr_main_t *sm = &sr_main;
293 ip6_sr_policy_t *t;
294
295 reg = vl_api_client_index_to_registration (mp->client_index);
296 if (!reg)
297 return;
298
299 /* *INDENT-OFF* */
300 pool_foreach (t, sm->sr_policies,
301 ({
302 send_sr_policies_details(t, reg, mp->context);
303 }));
304 /* *INDENT-ON* */
305}
306
307static void send_sr_steering_pol_details
308 (ip6_sr_steering_policy_t * t, vl_api_registration_t * reg, u32 context)
309{
310 vl_api_sr_steering_pol_details_t *rmp;
311 ip6_sr_main_t *sm = &sr_main;
312
313 rmp = vl_msg_api_alloc (sizeof (*rmp));
314 memset (rmp, 0, sizeof (*rmp));
315 rmp->_vl_msg_id = ntohs (VL_API_SR_STEERING_POL_DETAILS);
316
317 //Get the SR policy BSID
318 ip6_sr_policy_t *p;
319 p = pool_elt_at_index (sm->sr_policies, t->sr_policy);
320 clib_memcpy (rmp->bsid.addr, &p->bsid, sizeof (ip6_address_t));
321
322 //Get the steering
323 rmp->traffic_type = t->classify.traffic_type;
324 rmp->fib_table = htonl (t->classify.l3.fib_table);
325 rmp->mask_width = htonl (t->classify.l3.mask_width);
326 if (ip46_address_is_ip4 (&t->classify.l3.prefix))
327 clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip4,
328 sizeof (ip4_address_t));
329 else
330 clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip6,
331 sizeof (ip6_address_t));
332
333 rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
334
335 rmp->context = context;
336 vl_api_send_msg (reg, (u8 *) rmp);
337}
338
339static void vl_api_sr_steering_pol_dump_t_handler
340 (vl_api_sr_policies_dump_t * mp)
341{
342 vl_api_registration_t *reg;
343 ip6_sr_main_t *sm = &sr_main;
344 ip6_sr_steering_policy_t *t;
345
346 reg = vl_api_client_index_to_registration (mp->client_index);
347 if (!reg)
348 return;
349
350 /* *INDENT-OFF* */
351 pool_foreach (t, sm->steer_policies,
352 ({
353 send_sr_steering_pol_details(t, reg, mp->context);
354 }));
355 /* *INDENT-ON* */
356}
Pablo Camarillo1a5e3012017-11-16 16:02:50 +0100357
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100358/*
359 * sr_api_hookup
360 * Add vpe's API message handlers to the table.
361 * vlib has alread mapped shared memory and
362 * added the client registration handlers.
363 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
364 */
365#define vl_msg_name_crc_list
366#include <vnet/vnet_all_api_h.h>
367#undef vl_msg_name_crc_list
368
369static void
370setup_message_id_table (api_main_t * am)
371{
372#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
373 foreach_vl_msg_name_crc_sr;
374#undef _
375}
376
377static clib_error_t *
378sr_api_hookup (vlib_main_t * vm)
379{
380 api_main_t *am = &api_main;
381
382#define _(N,n) \
383 vl_msg_api_set_handlers(VL_API_##N, #n, \
384 vl_api_##n##_t_handler, \
385 vl_noop_handler, \
386 vl_api_##n##_t_endian, \
387 vl_api_##n##_t_print, \
388 sizeof(vl_api_##n##_t), 1);
389 foreach_vpe_api_msg;
390#undef _
391
392 /*
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100393 * Set up the (msg_name, crc, message-id) table
394 */
395 setup_message_id_table (am);
396
397 return 0;
398}
399
400VLIB_API_INIT_FUNCTION (sr_api_hookup);
401
402/*
403 * fd.io coding-style-patch-verification: ON
404 *
405 * Local Variables:
406 * eval: (c-set-style "gnu")
407 * End:
408 */