Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ------------------------------------------------------------------ |
| 3 | * sr_api.c - ipv6 segment routing api |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. Licensed under the Apache |
| 6 | * License, Version 2.0 (the "License"); you may not use this file except in |
| 7 | * compliance with the License. You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | * ------------------------------------------------------------------ |
| 17 | */ |
| 18 | |
| 19 | #include <vnet/vnet.h> |
| 20 | #include <vnet/srmpls/sr_mpls.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/feature/feature.h> |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 26 | #include <vnet/ip/ip_types_api.h> |
| 27 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 28 | #include <vnet/format_fns.h> |
| 29 | #include <vnet/srmpls/sr_mpls.api_enum.h> |
| 30 | #include <vnet/srmpls/sr_mpls.api_types.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 31 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 32 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 33 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 34 | #define vl_api_version(n, v) static u32 api_version = v; |
| 35 | #include <vnet/srmpls/sr_mpls.api.h> |
| 36 | #undef vl_api_version |
| 37 | |
| 38 | #define vl_endianfun |
| 39 | #include <vnet/srmpls/sr_mpls.api.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 40 | #undef vl_endianfun |
| 41 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 42 | #define vl_printfun |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 43 | #include <vnet/srmpls/sr_mpls.api.h> |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 44 | #undef vl_printfun |
| 45 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 46 | #define vl_msg_name_crc_list |
| 47 | #include <vnet/srmpls/sr_mpls.api.h> |
| 48 | #undef vl_msg_name_crc_list |
| 49 | |
| 50 | #define REPLY_MSG_ID_BASE msg_id_base |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 51 | #include <vlibapi/api_helper_macros.h> |
| 52 | |
| 53 | #define foreach_vpe_api_msg \ |
| 54 | _(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \ |
| 55 | _(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \ |
| 56 | _(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color) |
| 57 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 58 | static u16 msg_id_base; |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 59 | |
| 60 | static void |
| 61 | vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp) |
| 62 | { |
| 63 | vl_api_sr_mpls_policy_add_reply_t *rmp; |
| 64 | |
| 65 | mpls_label_t *segments = 0, *seg; |
| 66 | mpls_label_t this_address = 0; |
| 67 | |
| 68 | int i; |
| 69 | for (i = 0; i < mp->n_segments; i++) |
| 70 | { |
| 71 | vec_add2 (segments, seg, 1); |
| 72 | this_address = ntohl (mp->segments[i]); |
| 73 | clib_memcpy (seg, &this_address, sizeof (this_address)); |
| 74 | } |
| 75 | |
| 76 | int rv = 0; |
| 77 | rv = sr_mpls_policy_add (ntohl (mp->bsid), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 78 | segments, mp->is_spray, ntohl (mp->weight)); |
John Lo | d23d39c | 2018-09-13 15:08:08 -0400 | [diff] [blame] | 79 | vec_free (segments); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 80 | |
| 81 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY); |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp) |
| 86 | { |
| 87 | vl_api_sr_mpls_policy_mod_reply_t *rmp; |
| 88 | |
| 89 | mpls_label_t *segments = 0, *seg; |
| 90 | mpls_label_t this_address = 0; |
| 91 | |
| 92 | int i; |
| 93 | for (i = 0; i < mp->n_segments; i++) |
| 94 | { |
| 95 | vec_add2 (segments, seg, 1); |
| 96 | this_address = ntohl (mp->segments[i]); |
| 97 | clib_memcpy (seg, &this_address, sizeof (this_address)); |
| 98 | } |
| 99 | |
| 100 | int rv = 0; |
| 101 | rv = sr_mpls_policy_mod (ntohl (mp->bsid), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 102 | ntohl (mp->operation), segments, |
| 103 | ntohl (mp->sl_index), ntohl (mp->weight)); |
John Lo | d23d39c | 2018-09-13 15:08:08 -0400 | [diff] [blame] | 104 | vec_free (segments); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 105 | |
| 106 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp) |
| 111 | { |
| 112 | vl_api_sr_mpls_policy_del_reply_t *rmp; |
| 113 | int rv = 0; |
| 114 | rv = sr_mpls_policy_del (ntohl (mp->bsid)); |
| 115 | |
| 116 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY); |
| 117 | } |
| 118 | |
| 119 | static void vl_api_sr_mpls_steering_add_del_t_handler |
| 120 | (vl_api_sr_mpls_steering_add_del_t * mp) |
| 121 | { |
| 122 | vl_api_sr_mpls_steering_add_del_reply_t *rmp; |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 123 | fib_prefix_t prefix; |
| 124 | ip46_address_t next_hop; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 125 | clib_memset (&prefix, 0, sizeof (ip46_address_t)); |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 126 | |
| 127 | ip_prefix_decode (&mp->prefix, &prefix); |
| 128 | ip_address_decode (&mp->next_hop, &next_hop); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 129 | |
| 130 | int rv = 0; |
| 131 | if (mp->is_del) |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 132 | rv = sr_mpls_steering_policy_del (&prefix.fp_addr, |
| 133 | prefix.fp_len, |
| 134 | ip46_address_is_ip4 (&prefix.fp_addr) ? |
| 135 | SR_STEER_IPV4 : SR_STEER_IPV6, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 136 | ntohl (mp->table_id), |
| 137 | ntohl (mp->color)); |
| 138 | else |
| 139 | rv = sr_mpls_steering_policy_add (ntohl (mp->bsid), |
| 140 | ntohl (mp->table_id), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 141 | &prefix.fp_addr, |
| 142 | prefix.fp_len, |
| 143 | ip46_address_is_ip4 (&prefix.fp_addr) ? |
| 144 | SR_STEER_IPV4 : SR_STEER_IPV6, |
| 145 | &next_hop, |
| 146 | ip46_address_is_ip4 (&next_hop) ? |
| 147 | SR_STEER_IPV4 : SR_STEER_IPV6, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 148 | ntohl (mp->color), mp->co_bits, |
| 149 | ntohl (mp->vpn_label)); |
| 150 | |
| 151 | REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY); |
| 152 | } |
| 153 | |
| 154 | static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler |
| 155 | (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp) |
| 156 | { |
| 157 | vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp; |
| 158 | int rv = 0; |
| 159 | |
| 160 | ip46_address_t endpoint; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 161 | clib_memset (&endpoint, 0, sizeof (ip46_address_t)); |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 162 | ip_address_decode (&mp->endpoint, &endpoint); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 163 | |
| 164 | rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid), |
Jakub Grajciar | 00ec401 | 2020-01-31 10:17:29 +0100 | [diff] [blame] | 165 | &endpoint, |
| 166 | ip46_address_is_ip4 (&endpoint) ? |
| 167 | SR_STEER_IPV4 : SR_STEER_IPV6, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 168 | ntohl (mp->color)); |
| 169 | |
| 170 | REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY); |
| 171 | } |
| 172 | |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 173 | static void |
| 174 | setup_message_id_table (api_main_t * am) |
| 175 | { |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 176 | #define _(id, n, crc) \ |
| 177 | vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + REPLY_MSG_ID_BASE); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 178 | foreach_vl_msg_name_crc_sr_mpls; |
| 179 | #undef _ |
| 180 | } |
| 181 | |
| 182 | static clib_error_t * |
| 183 | sr_mpls_api_hookup (vlib_main_t * vm) |
| 184 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 185 | api_main_t *am = vlibapi_get_main (); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 186 | |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 187 | u8 *name = format (0, "sr_mpls_%08x%c", api_version, 0); |
| 188 | REPLY_MSG_ID_BASE = |
| 189 | vl_msg_api_get_msg_ids ((char *) name, VL_MSG_SR_MPLS_LAST); |
| 190 | vec_free (name); |
| 191 | |
| 192 | #define _(N, n) \ |
| 193 | vl_msg_api_set_handlers (REPLY_MSG_ID_BASE + VL_API_##N, #n, \ |
| 194 | vl_api_##n##_t_handler, vl_noop_handler, \ |
| 195 | vl_api_##n##_t_endian, vl_api_##n##_t_print, \ |
| 196 | sizeof (vl_api_##n##_t), 1); |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 197 | foreach_vpe_api_msg; |
| 198 | #undef _ |
| 199 | |
| 200 | /* |
| 201 | * Manually register the sr policy add msg, so we trace enough bytes |
| 202 | * to capture a typical segment list |
| 203 | */ |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 204 | vl_msg_api_set_handlers (REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_ADD, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 205 | "sr_mpls_policy_add", |
| 206 | vl_api_sr_mpls_policy_add_t_handler, |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 207 | vl_noop_handler, vl_api_sr_mpls_policy_add_t_endian, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 208 | vl_api_sr_mpls_policy_add_t_print, 256, 1); |
| 209 | |
| 210 | /* |
| 211 | * Manually register the sr policy mod msg, so we trace enough bytes |
| 212 | * to capture a typical segment list |
| 213 | */ |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 214 | vl_msg_api_set_handlers (REPLY_MSG_ID_BASE + VL_API_SR_MPLS_POLICY_MOD, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 215 | "sr_mpls_policy_mod", |
| 216 | vl_api_sr_mpls_policy_mod_t_handler, |
Filip Tehlar | 63c0fd3 | 2021-06-28 00:52:00 +0000 | [diff] [blame^] | 217 | vl_noop_handler, vl_api_sr_mpls_policy_mod_t_endian, |
Pablo Camarillo | 4299882 | 2017-07-13 09:41:32 +0200 | [diff] [blame] | 218 | vl_api_sr_mpls_policy_mod_t_print, 256, 1); |
| 219 | |
| 220 | /* |
| 221 | * Set up the (msg_name, crc, message-id) table |
| 222 | */ |
| 223 | setup_message_id_table (am); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup); |
| 229 | |
| 230 | /* |
| 231 | * fd.io coding-style-patch-verification: ON |
| 232 | * |
| 233 | * Local Variables: eval: (c-set-style "gnu") End: |
| 234 | */ |