2 *------------------------------------------------------------------
3 * sr_api.c - ipv6 segment routing api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vnet/srv6/sr.h>
22 #include <vlibmemory/api.h>
24 #include <vnet/interface.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/feature/feature.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/ip/ip_types_api.h>
30 #include <vnet/format_fns.h>
31 #include <vnet/srv6/sr.api_enum.h>
32 #include <vnet/srv6/sr.api_types.h>
34 #define REPLY_MSG_ID_BASE sr_main.msg_id_base
35 #include <vlibapi/api_helper_macros.h>
37 static void vl_api_sr_localsid_add_del_t_handler
38 (vl_api_sr_localsid_add_del_t * mp)
40 vl_api_sr_localsid_add_del_reply_t *rmp;
42 ip46_address_t prefix;
43 ip6_address_t localsid;
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)
49 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);
54 ip6_address_decode (mp->localsid, &localsid);
55 ip_address_decode (&mp->nh_addr, &prefix);
57 rv = sr_cli_localsid (mp->is_del,
61 ntohl (mp->sw_if_index),
62 ntohl (mp->vlan_index),
63 ntohl (mp->fib_table), &prefix, 0, NULL);
65 BAD_SW_IF_INDEX_LABEL;
66 REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
70 vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp)
72 vl_api_sr_policy_add_reply_t *rmp;
73 ip6_address_t *segments = 0, *seg;
74 ip6_address_t bsid_addr;
77 for (i = 0; i < mp->sids.num_sids; i++)
79 vec_add2 (segments, seg, 1);
80 ip6_address_decode (mp->sids.sids[i], seg);
83 ip6_address_decode (mp->bsid_addr, &bsid_addr);
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)
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);
97 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
101 vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
103 vl_api_sr_policy_mod_reply_t *rmp;
104 ip6_address_t *segments = 0, *seg;
105 ip6_address_t bsid_addr;
108 for (i = 0; i < mp->sids.num_sids; i++)
110 vec_add2 (segments, seg, 1);
111 ip6_address_decode (mp->sids.sids[i], seg);
114 ip6_address_decode (mp->bsid_addr, &bsid_addr);
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)
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));
129 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
133 vl_api_sr_policy_add_v2_t_handler (vl_api_sr_policy_add_v2_t *mp)
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;
141 for (i = 0; i < mp->sids.num_sids; i++)
143 vec_add2 (segments, seg, 1);
144 ip6_address_decode (mp->sids.sids[i], seg);
147 ip6_address_decode (mp->bsid_addr, &bsid_addr);
148 ip6_address_decode (mp->encap_src, &encap_src);
150 if (ip6_address_is_zero (&encap_src))
152 encap_src = *sr_get_encaps_source ();
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)
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);
166 REPLY_MACRO (VL_API_SR_POLICY_ADD_V2_REPLY);
170 vl_api_sr_policy_mod_v2_t_handler (vl_api_sr_policy_mod_v2_t *mp)
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;
178 for (i = 0; i < mp->sids.num_sids; i++)
180 vec_add2 (segments, seg, 1);
181 ip6_address_decode (mp->sids.sids[i], seg);
184 ip6_address_decode (mp->bsid_addr, &bsid_addr);
185 ip6_address_decode (mp->encap_src, &encap_src);
187 if (ip6_address_is_zero (&encap_src))
189 encap_src = *sr_get_encaps_source ();
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)
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));
206 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
210 vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
212 vl_api_sr_policy_del_reply_t *rmp;
214 ip6_address_t bsid_addr;
217 * sr_policy_del (ip6_address_t *bsid, u32 index)
219 ip6_address_decode (mp->bsid_addr, &bsid_addr);
220 rv = sr_policy_del (&bsid_addr, ntohl (mp->sr_policy_index));
222 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
226 vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp)
228 vl_api_sr_set_encap_source_reply_t *rmp;
230 ip6_address_t encaps_source;
232 ip6_address_decode (mp->encaps_source, &encaps_source);
233 sr_set_source (&encaps_source);
235 REPLY_MACRO (VL_API_SR_SET_ENCAP_SOURCE_REPLY);
239 vl_api_sr_set_encap_hop_limit_t_handler (vl_api_sr_set_encap_hop_limit_t * mp)
241 vl_api_sr_set_encap_hop_limit_reply_t *rmp;
244 if (mp->hop_limit == 0)
245 rv = VNET_API_ERROR_INVALID_VALUE;
247 sr_set_hop_limit (mp->hop_limit);
249 REPLY_MACRO (VL_API_SR_SET_ENCAP_HOP_LIMIT_REPLY);
252 static void vl_api_sr_steering_add_del_t_handler
253 (vl_api_sr_steering_add_del_t * mp)
255 vl_api_sr_steering_add_del_reply_t *rmp;
257 ip6_address_t bsid_addr;
258 ip46_address_t prefix_addr;
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,
266 ip6_address_decode (mp->bsid_addr, &bsid_addr);
267 ip_address_decode (&mp->prefix.address, &prefix_addr);
269 if (mp->traffic_type == SR_STEER_L2)
270 VALIDATE_SW_IF_INDEX (mp);
272 rv = sr_steering_policy (mp->is_del,
274 ntohl (mp->sr_policy_index),
275 ntohl (mp->table_id),
278 ntohl (mp->sw_if_index), mp->traffic_type);
280 BAD_SW_IF_INDEX_LABEL;
281 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
284 static void send_sr_localsid_details
285 (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context)
287 vl_api_sr_localsids_details_t *rmp;
289 rmp = vl_msg_api_alloc (sizeof (*rmp));
290 clib_memset (rmp, 0, sizeof (*rmp));
291 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_LOCALSIDS_DETAILS);
292 ip6_address_encode (&t->localsid, rmp->addr);
293 rmp->end_psp = t->end_psp;
294 rmp->behavior = t->behavior;
295 rmp->fib_table = htonl (t->fib_table);
296 rmp->vlan_index = htonl (t->vlan_index);
297 ip_address_encode (&t->next_hop, IP46_TYPE_ANY, &rmp->xconnect_nh_addr);
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));
306 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
308 rmp->context = context;
310 vl_api_send_msg (reg, (u8 *) rmp);
313 static void vl_api_sr_localsids_dump_t_handler
314 (vl_api_sr_localsids_dump_t * mp)
316 vl_api_registration_t *reg;
317 ip6_sr_main_t *sm = &sr_main;
318 ip6_sr_localsid_t *t;
320 reg = vl_api_client_index_to_registration (mp->client_index);
324 pool_foreach (t, sm->localsids)
326 send_sr_localsid_details(t, reg, mp->context);
331 send_sr_localsid_with_packet_stats_details (int local_sid_index,
332 ip6_sr_localsid_t *t,
333 vl_api_registration_t *reg,
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;
340 rmp = vl_msg_api_alloc (sizeof (*rmp));
341 clib_memset (rmp, 0, sizeof (*rmp));
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);
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));
358 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
360 rmp->context = context;
361 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), local_sid_index,
363 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), local_sid_index,
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);
373 vl_api_sr_localsids_with_packet_stats_dump_t_handler (
374 vl_api_sr_localsids_with_packet_stats_dump_t *mp)
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;
382 reg = vl_api_client_index_to_registration (mp->client_index);
386 pool_foreach (t, sm->localsids)
388 vec_add1 (localsid_list, t);
390 for (i = 0; i < vec_len (localsid_list); i++)
392 t = localsid_list[i];
393 send_sr_localsid_with_packet_stats_details (i, t, reg, mp->context);
397 static void send_sr_policies_details
398 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
400 vl_api_sr_policies_details_t *rmp;
401 ip6_sr_main_t *sm = &sr_main;
403 u32 *sl_index, slidx = 0;
404 ip6_sr_sl_t *segment_list = 0;
405 ip6_address_t *segment;
406 vl_api_srv6_sid_list_t *api_sid_list;
408 rmp = vl_msg_api_alloc (sizeof (*rmp) +
409 vec_len (t->segments_lists) *
410 sizeof (vl_api_srv6_sid_list_t));
413 vec_len (t->segments_lists) *
414 sizeof (vl_api_srv6_sid_list_t)));
416 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_DETAILS);
417 ip6_address_encode (&t->bsid, rmp->bsid);
418 rmp->is_encap = t->is_encap;
419 rmp->is_spray = t->type;
420 rmp->fib_table = htonl (t->fib_table);
421 rmp->num_sid_lists = vec_len (t->segments_lists);
423 /* Fill in all the segments lists */
424 vec_foreach (sl_index, t->segments_lists)
426 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
428 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
430 api_sid_list->num_sids = vec_len (segment_list->segments);
431 api_sid_list->weight = htonl (segment_list->weight);
433 vec_foreach (segment, segment_list->segments)
435 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
439 rmp->context = context;
440 vl_api_send_msg (reg, (u8 *) rmp);
444 vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
446 vl_api_registration_t *reg;
447 ip6_sr_main_t *sm = &sr_main;
450 reg = vl_api_client_index_to_registration (mp->client_index);
454 pool_foreach (t, sm->sr_policies)
456 send_sr_policies_details(t, reg, mp->context);
461 send_sr_policies_v2_details (ip6_sr_policy_t *t, vl_api_registration_t *reg,
464 vl_api_sr_policies_v2_details_t *rmp;
465 ip6_sr_main_t *sm = &sr_main;
467 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;
472 rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len (t->segments_lists) *
473 sizeof (vl_api_srv6_sid_list_t));
475 (sizeof (*rmp) + vec_len (t->segments_lists) *
476 sizeof (vl_api_srv6_sid_list_t)));
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;
483 rmp->fib_table = htonl (t->fib_table);
484 rmp->num_sid_lists = vec_len (t->segments_lists);
486 /* Fill in all the segments lists */
487 vec_foreach (sl_index, t->segments_lists)
489 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
491 api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
493 api_sid_list->num_sids = vec_len (segment_list->segments);
494 api_sid_list->weight = htonl (segment_list->weight);
496 vec_foreach (segment, segment_list->segments)
498 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
502 rmp->context = context;
503 vl_api_send_msg (reg, (u8 *) rmp);
507 vl_api_sr_policies_v2_dump_t_handler (vl_api_sr_policies_v2_dump_t *mp)
509 vl_api_registration_t *reg;
510 ip6_sr_main_t *sm = &sr_main;
513 reg = vl_api_client_index_to_registration (mp->client_index);
517 pool_foreach (t, sm->sr_policies)
519 send_sr_policies_v2_details (t, reg, mp->context);
523 static void send_sr_policies_details_with_sl_index
524 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
526 vl_api_sr_policies_with_sl_index_details_t *rmp;
527 ip6_sr_main_t *sm = &sr_main;
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;
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));
539 vec_len (t->segments_lists) *
540 sizeof (vl_api_srv6_sid_list_with_sl_index_t)));
543 ntohs (REPLY_MSG_ID_BASE + VL_API_SR_POLICIES_WITH_SL_INDEX_DETAILS);
544 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);
550 /* Fill in all the segments lists */
551 vec_foreach (sl_index, t->segments_lists)
553 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
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);
560 vec_foreach (segment, segment_list->segments)
562 ip6_address_encode (segment, api_sid_list->sids[slidx++]);
566 rmp->context = context;
567 vl_api_send_msg (reg, (u8 *) rmp);
571 vl_api_sr_policies_with_sl_index_dump_t_handler
572 (vl_api_sr_policies_with_sl_index_dump_t * mp)
574 vl_api_registration_t *reg;
575 ip6_sr_main_t *sm = &sr_main;
578 reg = vl_api_client_index_to_registration (mp->client_index);
582 pool_foreach (t, sm->sr_policies)
584 send_sr_policies_details_with_sl_index(t, reg, mp->context);
588 static void send_sr_steering_pol_details
589 (ip6_sr_steering_policy_t * t, vl_api_registration_t * reg, u32 context)
591 vl_api_sr_steering_pol_details_t *rmp;
592 ip6_sr_main_t *sm = &sr_main;
594 rmp = vl_msg_api_alloc (sizeof (*rmp));
595 clib_memset (rmp, 0, sizeof (*rmp));
596 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SR_STEERING_POL_DETAILS);
598 //Get the SR policy BSID
600 p = pool_elt_at_index (sm->sr_policies, t->sr_policy);
601 ip6_address_encode (&p->bsid, rmp->bsid);
604 rmp->traffic_type = t->classify.traffic_type;
605 rmp->fib_table = htonl (t->classify.l3.fib_table);
606 ip_address_encode (&t->classify.l3.prefix, IP46_TYPE_ANY,
607 &rmp->prefix.address);
608 rmp->prefix.len = t->classify.l3.mask_width;
610 rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
612 rmp->context = context;
613 vl_api_send_msg (reg, (u8 *) rmp);
616 static void vl_api_sr_steering_pol_dump_t_handler
617 (vl_api_sr_policies_dump_t * mp)
619 vl_api_registration_t *reg;
620 ip6_sr_main_t *sm = &sr_main;
621 ip6_sr_steering_policy_t *t;
623 reg = vl_api_client_index_to_registration (mp->client_index);
627 pool_foreach (t, sm->steer_policies)
629 send_sr_steering_pol_details(t, reg, mp->context);
633 #include <vnet/srv6/sr.api.c>
634 static clib_error_t *
635 sr_api_hookup (vlib_main_t * vm)
638 * Set up the (msg_name, crc, message-id) table
640 REPLY_MSG_ID_BASE = setup_message_id_table ();
645 VLIB_API_INIT_FUNCTION (sr_api_hookup);
648 * fd.io coding-style-patch-verification: ON
651 * eval: (c-set-style "gnu")